1 /********************* */
2 /*! \file simplex_update.h
4 ** Top contributors (to current version):
5 ** Tim King, Morgan Deters
6 ** This file is part of the CVC4 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.\endverbatim
12 ** \brief This provides a class for summarizing pivot proposals.
14 ** This shares with the theory a Tableau, and a PartialModel that:
15 ** - satisfies the equalities in the Tableau, and
16 ** - the assignment for the non-basic variables satisfies their bounds.
17 ** This maintains the relationship needed by the SimplexDecisionProcedure.
19 ** In the language of Simplex for DPLL(T), this provides:
23 ** This class also provides utility functions that require
24 ** using both the Tableau and PartialModel.
27 #include "cvc4_private.h"
31 #include "theory/arith/delta_rational.h"
32 #include "theory/arith/arithvar.h"
33 #include "theory/arith/constraint_forward.h"
34 #include "util/maybe.h"
40 enum WitnessImprovement
{
47 HeuristicDegenerate
= 6,
51 inline bool strongImprovement(WitnessImprovement w
){
52 return w
<= FocusImproved
;
55 inline bool improvement(WitnessImprovement w
){
56 return w
<= FocusShrank
;
59 inline bool degenerate(WitnessImprovement w
){
62 case BlandsDegenerate
:
63 case HeuristicDegenerate
:
70 std::ostream
& operator<<(std::ostream
& out
, WitnessImprovement w
);
73 * This class summarizes both potential:
74 * - pivot-and-update operations or
75 * - a pure update operation.
76 * This stores enough information for the various algorithms hat consider these operations.
77 * These require slightly different pieces of information at different points
78 * so they are a bit verbose and paranoid.
84 * The nonbasic variables under consideration.
85 * This is either the entering variable on a pivot and update
86 * or the variable being updated.
87 * This can only be set in the constructor or assignment.
89 * If this uninitialized, then this is ARITHVAR_SENTINEL.
94 * The sgn of the "intended" derivative (delta) of the update to d_nonbasic.
95 * This is either 1, -1, or 0.
96 * It is "intended" as the delta is always allowed to be 0.
97 * (See debugSgnAgreement().)
99 * If this uninitialized, then this is 0.
100 * If this is initialized, then it is -1 or 1.
102 * This can only be set in the constructor or assignment.
104 int d_nonbasicDirection
;
107 * The change in the assignment of d_nonbasic.
108 * This is changed via the updateProposal(...) methods.
109 * The value needs to satisfy debugSgnAgreement() or it is in conflict.
111 Maybe
<DeltaRational
> d_nonbasicDelta
;
114 * This is true if the pivot-and-update is *known* to cause a conflict.
115 * This can only be true if it was constructed through the static conflict(...) method.
117 bool d_foundConflict
;
119 /** This is the change in the size of the error set. */
120 Maybe
<int> d_errorsChange
;
122 /** This is the sgn of the change in the value of the focus set.*/
123 Maybe
<int> d_focusDirection
;
125 /** This is the sgn of the change in the value of the focus set.*/
126 Maybe
<DeltaRational
> d_focusChange
;
128 /** This is the coefficient in the tableau for the entry.*/
129 Maybe
<const Rational
*> d_tableauCoefficient
;
132 * This is the constraint that nonbasic is basic is updating s.t. its variable is against it.
133 * This has 3 different possibilities:
134 * - Unbounded : then this is NullConstraint and unbounded() is true.
135 * - Pivot-And-Update: then this is not NullConstraint and the variable is not d_nonbasic.
136 * - Update: then this is not NullConstraint and the variable is d_nonbasic.
138 ConstraintP d_limiting
;
140 WitnessImprovement d_witness
;
143 * This returns true if
144 * d_nonbasicDelta is zero() or its sgn() must agree with d_nonbasicDirection.
146 bool debugSgnAgreement() const {
147 int deltaSgn
= d_nonbasicDelta
.value().sgn();
148 return deltaSgn
== 0 || deltaSgn
== d_nonbasicDirection
;
151 /** This private constructor allows for setting conflict to true. */
152 UpdateInfo(bool conflict
, ArithVar nb
, const DeltaRational
& delta
, const Rational
& r
, ConstraintP lim
);
156 /** This constructs an uninitialized UpdateInfo. */
160 * This constructs an initialized UpdateInfo.
161 * dir must be 1 or -1.
163 UpdateInfo(ArithVar nb
, int dir
);
166 * This updates the nonBasicDelta to d and limiting to NullConstraint.
167 * This describes an unbounded() update.
169 void updateUnbounded(const DeltaRational
& d
, int ec
, int f
);
172 void updatePureFocus(const DeltaRational
& d
, ConstraintP c
);
173 //void updatePureError(const DeltaRational& d, Constraint c, int e);
174 //void updatePure(const DeltaRational& d, Constraint c, int e, int f);
177 * This updates the nonBasicDelta to d and limiting to c.
178 * This clears errorChange() and focusDir().
180 void updatePivot(const DeltaRational
& d
, const Rational
& r
, ConstraintP c
);
183 * This updates the nonBasicDelta to d, limiting to c, and errorChange to e.
184 * This clears focusDir().
186 void updatePivot(const DeltaRational
& d
, const Rational
& r
, ConstraintP c
, int e
);
189 * This updates the nonBasicDelta to d, limiting to c, errorChange to e and
192 void witnessedUpdate(const DeltaRational
& d
, ConstraintP c
, int e
, int f
);
193 void update(const DeltaRational
& d
, const Rational
& r
, ConstraintP c
, int e
, int f
);
196 static UpdateInfo
conflict(ArithVar nb
, const DeltaRational
& delta
, const Rational
& r
, ConstraintP lim
);
198 inline ArithVar
nonbasic() const { return d_nonbasic
; }
199 inline bool uninitialized() const {
200 return d_nonbasic
== ARITHVAR_SENTINEL
;
204 * There is no limiting value to the improvement of the focus.
205 * If this is true, this never describes an update.
207 inline bool unbounded() const {
208 return d_limiting
== NullConstraint
;
212 * The update either describes a pivotAndUpdate operation
213 * or it describes just an update.
215 bool describesPivot() const;
217 /** Returns the . describesPivot() must be true. */
218 ArithVar
leaving() const;
221 * Returns true if this is *known* to find a conflict.
222 * If true, this must have been made through the static conflict(...) function.
224 bool foundConflict() const { return d_foundConflict
; }
226 /** Returns the direction nonbasic is supposed to move. */
227 inline int nonbasicDirection() const{ return d_nonbasicDirection
; }
229 /** Requires errorsChange to be set through setErrorsChange or updateProposal. */
230 inline int errorsChange() const { return d_errorsChange
.value(); }
233 * If errorsChange has been set, return errorsChange().
234 * Otherwise, return def.
236 inline int errorsChangeSafe(int def
) const {
239 return d_errorsChange
.value();
247 /** Sets the errorChange. */
248 void setErrorsChange(int ec
){
254 /** Requires errorsChange to be set through setErrorsChange or updateProposal. */
255 inline int focusDirection() const { return d_focusDirection
.value(); }
257 /** Sets the focusDirection. */
258 void setFocusDirection(int fd
){
259 Assert(-1 <= fd
&& fd
<= 1);
260 d_focusDirection
= fd
;
265 * nonbasicDirection must be the same as the sign for the focus function's
266 * coefficient for this to be safe.
267 * The burden for this being safe is on the user!
269 void determineFocusDirection(){
270 const int deltaSgn
= d_nonbasicDelta
.value().sgn();
271 setFocusDirection(deltaSgn
* d_nonbasicDirection
);
274 /** Requires nonbasicDelta to be set through updateProposal(...). */
275 const DeltaRational
& nonbasicDelta() const { return d_nonbasicDelta
.value(); }
276 const Rational
& getCoefficient() const {
277 Assert(describesPivot());
278 Assert(d_tableauCoefficient
.value() != NULL
);
279 return *(d_tableauCoefficient
.value());
281 int basicDirection() const {
282 return nonbasicDirection() * (getCoefficient().sgn());
285 /** Returns the limiting constraint. */
286 inline ConstraintP
limiting() const {
290 WitnessImprovement
getWitness(bool useBlands
= false) const{
291 Assert(d_witness
== computeWitness());
293 if(d_witness
== Degenerate
){
295 return BlandsDegenerate
;
297 return HeuristicDegenerate
;
304 const DeltaRational
& focusChange() const { return d_focusChange
.value(); }
305 void setFocusChange(const DeltaRational
& fc
) {
309 /** Outputs the UpdateInfo into out. */
310 void output(std::ostream
& out
) const;
313 void updateWitness() {
314 d_witness
= computeWitness();
315 Assert(describesPivot() || improvement(d_witness
));
319 * Determines the appropriate WitnessImprovement for the update.
320 * useBlands breaks ties for degenerate pivots.
323 * - d_foundConflict is true, or
324 * - d_foundConflict is false and d_errorsChange has been set and d_errorsChange < 0, or
325 * - d_foundConflict is false and d_errorsChange has been set and d_errorsChange >= 0 and d_focusDirection has been set.
327 WitnessImprovement
computeWitness() const {
329 return ConflictFound
;
331 else if (d_errorsChange
.just() && d_errorsChange
.value() < 0)
335 else if (d_errorsChange
.nothing() || d_errorsChange
.value() == 0)
337 if(d_focusDirection
.just()){
338 if (d_focusDirection
.value() > 0)
340 return FocusImproved
;
342 else if (d_focusDirection
.value() == 0)
348 return AntiProductive
;
353 std::ostream
& operator<<(std::ostream
& out
, const UpdateInfo
& up
);
356 } // namespace theory