1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Tim King, Morgan Deters |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 by the authors listed in the file AUTHORS |
8 |
|
* in the top-level source directory and their institutional affiliations. |
9 |
|
* All rights reserved. See the file COPYING in the top-level source |
10 |
|
* directory for licensing information. |
11 |
|
* **************************************************************************** |
12 |
|
* |
13 |
|
* [[ Add one-line brief description here ]] |
14 |
|
* |
15 |
|
* [[ Add lengthier description here ]] |
16 |
|
* \todo document this file |
17 |
|
*/ |
18 |
|
|
19 |
|
#include "cvc5_private.h" |
20 |
|
|
21 |
|
#pragma once |
22 |
|
#include <vector> |
23 |
|
|
24 |
|
#include "theory/arith/arithvar.h" |
25 |
|
#include "theory/arith/delta_rational.h" |
26 |
|
#include "util/dense_map.h" |
27 |
|
#include "util/maybe.h" |
28 |
|
#include "util/rational.h" |
29 |
|
#include "util/statistics_stats.h" |
30 |
|
|
31 |
|
namespace cvc5 { |
32 |
|
namespace theory { |
33 |
|
namespace arith { |
34 |
|
|
35 |
|
enum LinResult { |
36 |
|
LinUnknown, /* Unknown error */ |
37 |
|
LinFeasible, /* Relaxation is feasible */ |
38 |
|
LinInfeasible, /* Relaxation is infeasible/all integer branches closed */ |
39 |
|
LinExhausted |
40 |
|
}; |
41 |
|
|
42 |
|
enum MipResult { |
43 |
|
MipUnknown, /* Unknown error */ |
44 |
|
MipBingo, /* Integer feasible */ |
45 |
|
MipClosed, /* All integer branches closed */ |
46 |
|
BranchesExhausted, /* Exhausted number of branches */ |
47 |
|
PivotsExhauasted, /* Exhausted number of pivots */ |
48 |
|
ExecExhausted /* Exhausted total operations */ |
49 |
|
}; |
50 |
|
std::ostream& operator<<(std::ostream& out, MipResult res); |
51 |
|
|
52 |
|
class ApproximateStatistics { |
53 |
|
public: |
54 |
|
ApproximateStatistics(); |
55 |
|
|
56 |
|
IntStat d_branchMaxDepth; |
57 |
|
IntStat d_branchesMaxOnAVar; |
58 |
|
|
59 |
|
TimerStat d_gaussianElimConstructTime; |
60 |
|
IntStat d_gaussianElimConstruct; |
61 |
|
AverageStat d_averageGuesses; |
62 |
|
}; |
63 |
|
|
64 |
|
|
65 |
|
class NodeLog; |
66 |
|
class TreeLog; |
67 |
|
class ArithVariables; |
68 |
|
class CutInfo; |
69 |
|
|
70 |
|
class ApproximateSimplex{ |
71 |
|
public: |
72 |
|
static bool enabled(); |
73 |
|
|
74 |
|
/** |
75 |
|
* If glpk is enabled, return a subclass that can do something. |
76 |
|
* If glpk is disabled, return a subclass that does nothing. |
77 |
|
*/ |
78 |
|
static ApproximateSimplex* mkApproximateSimplexSolver(const ArithVariables& vars, TreeLog& l, ApproximateStatistics& s); |
79 |
|
ApproximateSimplex(const ArithVariables& v, TreeLog& l, ApproximateStatistics& s); |
80 |
|
virtual ~ApproximateSimplex(){} |
81 |
|
|
82 |
|
/* the maximum pivots allowed in a query. */ |
83 |
|
void setPivotLimit(int pl); |
84 |
|
|
85 |
|
/* maximum branches allowed on a variable */ |
86 |
|
void setBranchOnVariableLimit(int bl); |
87 |
|
|
88 |
|
/* maximum branches allowed on a variable */ |
89 |
|
void setBranchingDepth(int bd); |
90 |
|
|
91 |
|
/** A result is either sat, unsat or unknown.*/ |
92 |
|
struct Solution { |
93 |
|
DenseSet newBasis; |
94 |
|
DenseMap<DeltaRational> newValues; |
95 |
|
Solution() : newBasis(), newValues(){} |
96 |
|
}; |
97 |
|
|
98 |
|
virtual ArithVar getBranchVar(const NodeLog& nl) const = 0; |
99 |
|
|
100 |
|
/** Sets a maximization criteria for the approximate solver.*/ |
101 |
|
virtual void setOptCoeffs(const ArithRatPairVec& ref) = 0; |
102 |
|
|
103 |
|
virtual ArithRatPairVec heuristicOptCoeffs() const = 0; |
104 |
|
|
105 |
|
virtual LinResult solveRelaxation() = 0; |
106 |
|
virtual Solution extractRelaxation() const = 0; |
107 |
|
|
108 |
|
virtual MipResult solveMIP(bool activelyLog) = 0; |
109 |
|
|
110 |
|
virtual Solution extractMIP() const = 0; |
111 |
|
|
112 |
|
virtual std::vector<const CutInfo*> getValidCuts(const NodeLog& node) = 0; |
113 |
|
|
114 |
|
virtual void tryCut(int nid, CutInfo& cut) = 0; |
115 |
|
|
116 |
|
/** UTILITIES FOR DEALING WITH ESTIMATES */ |
117 |
|
|
118 |
|
static const double SMALL_FIXED_DELTA; |
119 |
|
static const double TOLERENCE; |
120 |
|
|
121 |
|
/** Returns true if two doubles are roughly equal based on TOLERENCE and SMALL_FIXED_DELTA.*/ |
122 |
|
static bool roughlyEqual(double a, double b); |
123 |
|
|
124 |
|
/** |
125 |
|
* Estimates a double as a Rational using continued fraction expansion that |
126 |
|
* cuts off the estimate once the value is approximately zero. |
127 |
|
* This is designed for removing rounding artifacts. |
128 |
|
*/ |
129 |
|
static Maybe<Rational> estimateWithCFE(double d); |
130 |
|
static Maybe<Rational> estimateWithCFE(double d, const Integer& D); |
131 |
|
|
132 |
|
/** |
133 |
|
* Converts a rational to a continued fraction expansion representation |
134 |
|
* using a maximum number of expansions equal to depth as long as the expression |
135 |
|
* is not roughlyEqual() to 0. |
136 |
|
*/ |
137 |
|
static std::vector<Integer> rationalToCfe(const Rational& q, int depth); |
138 |
|
|
139 |
|
/** Converts a continued fraction expansion representation to a rational. */ |
140 |
|
static Rational cfeToRational(const std::vector<Integer>& exp); |
141 |
|
|
142 |
|
/** Estimates a rational as a continued fraction expansion.*/ |
143 |
|
static Rational estimateWithCFE(const Rational& q, const Integer& K); |
144 |
|
|
145 |
|
virtual double sumInfeasibilities(bool mip) const = 0; |
146 |
|
|
147 |
|
protected: |
148 |
|
const ArithVariables& d_vars; |
149 |
|
TreeLog& d_log; |
150 |
|
ApproximateStatistics& d_stats; |
151 |
|
|
152 |
|
/* the maximum pivots allowed in a query. */ |
153 |
|
int d_pivotLimit; |
154 |
|
|
155 |
|
/* maximum branches allowed on a variable */ |
156 |
|
int d_branchLimit; |
157 |
|
|
158 |
|
/* maxmimum branching depth allowed.*/ |
159 |
|
int d_maxDepth; |
160 |
|
|
161 |
|
/* Default denominator for diophatine approximation, 2^{26} .*/ |
162 |
|
static Integer s_defaultMaxDenom; |
163 |
|
};/* class ApproximateSimplex */ |
164 |
|
|
165 |
|
} // namespace arith |
166 |
|
} // namespace theory |
167 |
|
} // namespace cvc5 |