1 /******************************************************************************
2 * Top contributors (to current version):
3 * Tim King, Morgan Deters
5 * This file is part of the cvc5 project.
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 * ****************************************************************************
13 * [[ Add one-line brief description here ]]
15 * [[ Add lengthier description here ]]
16 * \todo document this file
19 #include "cvc5_private.h"
26 #include "theory/arith/arithvar.h"
27 #include "theory/arith/delta_rational.h"
28 #include "util/dense_map.h"
29 #include "util/rational.h"
30 #include "util/statistics_stats.h"
37 LinUnknown
, /* Unknown error */
38 LinFeasible
, /* Relaxation is feasible */
39 LinInfeasible
, /* Relaxation is infeasible/all integer branches closed */
44 MipUnknown
, /* Unknown error */
45 MipBingo
, /* Integer feasible */
46 MipClosed
, /* All integer branches closed */
47 BranchesExhausted
, /* Exhausted number of branches */
48 PivotsExhauasted
, /* Exhausted number of pivots */
49 ExecExhausted
/* Exhausted total operations */
51 std::ostream
& operator<<(std::ostream
& out
, MipResult res
);
53 class ApproximateStatistics
{
55 ApproximateStatistics();
57 IntStat d_branchMaxDepth
;
58 IntStat d_branchesMaxOnAVar
;
60 TimerStat d_gaussianElimConstructTime
;
61 IntStat d_gaussianElimConstruct
;
62 AverageStat d_averageGuesses
;
71 class ApproximateSimplex
{
73 static bool enabled();
76 * If glpk is enabled, return a subclass that can do something.
77 * If glpk is disabled, return a subclass that does nothing.
79 static ApproximateSimplex
* mkApproximateSimplexSolver(const ArithVariables
& vars
, TreeLog
& l
, ApproximateStatistics
& s
);
80 ApproximateSimplex(const ArithVariables
& v
, TreeLog
& l
, ApproximateStatistics
& s
);
81 virtual ~ApproximateSimplex(){}
83 /* the maximum pivots allowed in a query. */
84 void setPivotLimit(int pl
);
86 /* maximum branches allowed on a variable */
87 void setBranchOnVariableLimit(int bl
);
89 /* maximum branches allowed on a variable */
90 void setBranchingDepth(int bd
);
92 /** A result is either sat, unsat or unknown.*/
95 DenseMap
<DeltaRational
> newValues
;
96 Solution() : newBasis(), newValues(){}
99 virtual ArithVar
getBranchVar(const NodeLog
& nl
) const = 0;
101 /** Sets a maximization criteria for the approximate solver.*/
102 virtual void setOptCoeffs(const ArithRatPairVec
& ref
) = 0;
104 virtual ArithRatPairVec
heuristicOptCoeffs() const = 0;
106 virtual LinResult
solveRelaxation() = 0;
107 virtual Solution
extractRelaxation() const = 0;
109 virtual MipResult
solveMIP(bool activelyLog
) = 0;
111 virtual Solution
extractMIP() const = 0;
113 virtual std::vector
<const CutInfo
*> getValidCuts(const NodeLog
& node
) = 0;
115 virtual void tryCut(int nid
, CutInfo
& cut
) = 0;
117 /** UTILITIES FOR DEALING WITH ESTIMATES */
119 static const double SMALL_FIXED_DELTA
;
120 static const double TOLERENCE
;
122 /** Returns true if two doubles are roughly equal based on TOLERENCE and SMALL_FIXED_DELTA.*/
123 static bool roughlyEqual(double a
, double b
);
126 * Estimates a double as a Rational using continued fraction expansion that
127 * cuts off the estimate once the value is approximately zero.
128 * This is designed for removing rounding artifacts.
130 static std::optional
<Rational
> estimateWithCFE(double d
);
131 static std::optional
<Rational
> estimateWithCFE(double d
, const Integer
& D
);
134 * Converts a rational to a continued fraction expansion representation
135 * using a maximum number of expansions equal to depth as long as the expression
136 * is not roughlyEqual() to 0.
138 static std::vector
<Integer
> rationalToCfe(const Rational
& q
, int depth
);
140 /** Converts a continued fraction expansion representation to a rational. */
141 static Rational
cfeToRational(const std::vector
<Integer
>& exp
);
143 /** Estimates a rational as a continued fraction expansion.*/
144 static Rational
estimateWithCFE(const Rational
& q
, const Integer
& K
);
146 virtual double sumInfeasibilities(bool mip
) const = 0;
149 const ArithVariables
& d_vars
;
151 ApproximateStatistics
& d_stats
;
153 /* the maximum pivots allowed in a query. */
156 /* maximum branches allowed on a variable */
159 /* maxmimum branching depth allowed.*/
162 /* Default denominator for diophatine approximation, 2^{26} .*/
163 static Integer s_defaultMaxDenom
;
164 };/* class ApproximateSimplex */
167 } // namespace theory