Minotaur 0.4.1
Docs for developers
QuadHandler.h
Go to the documentation of this file.
1//
2// Minotaur -- It's only 1/2 bull
3//
4// (C)opyright 2008 - 2025 The Minotaur Team.
5//
6
17#ifndef MINOTAURQUADRATICHANDLER_H
18#define MINOTAURQUADRATICHANDLER_H
19
20#include "Handler.h"
21#include "LinBil.h"
22#include "SimplexQuadCutGen.h"
23
24namespace Minotaur
25{
26
27class LinearFunction;
28class Timer;
29typedef LinearFunction* LinearFunctionPtr;
30typedef Engine* EnginePtr;
31
36struct LinSqr {
37 VariablePtr y;
42 {
43 y = y0;
44 x = x0;
46 qcon = con;
47 }
48};
49typedef LinSqr* LinSqrPtr;
50typedef std::vector<LinSqrPtr> LinSqrVec;
51typedef LinSqrVec::iterator LinSqrVecIter;
52
54typedef std::map<VariablePtr, LinSqrPtr, CompareVariablePtr> LinSqrMap;
55typedef LinSqrMap::iterator LinSqrMapIter;
56
62class QuadHandler : public Handler
63{
64public:
66 QuadHandler(EnvPtr env, ProblemPtr problem);
67
68 QuadHandler(EnvPtr env, ProblemPtr problem, ProblemPtr orig_p);
69
72
73 // base class method
74 void addConstraint(ConstraintPtr newcon);
75
79
80 // Implement Handler::getBranches().
81 Branches getBranches(BrCandPtr cand, DoubleVector& x, RelaxationPtr rel,
82 SolutionPoolPtr s_pool);
83
84 // base class method
85 void getBranchingCandidates(RelaxationPtr rel, const DoubleVector& x,
86 ModVector& mods, BrVarCandSet& cands,
87 BrCandVector& gencands, bool& is_inf);
88
89 // base class method
90 ModificationPtr getBrMod(BrCandPtr cand, DoubleVector& x, RelaxationPtr rel,
91 BranchDirection dir);
92
93 // base class method
94 std::string getName() const;
95
96 // base class method.
97 bool isFeasible(ConstSolutionPtr sol, RelaxationPtr relaxation,
98 bool& should_prune, double& inf_meas);
99
100 // base class method.
101 SolveStatus presolve(PreModQ* pre_mods, bool* changed, Solution** sol);
102
103 // base class method. Tightens bounds.
105 ModVector& p_mods, ModVector& r_mods);
106
107 // implement Handler::postSolveRootNode
109 ConstSolutionPtr sol, ModVector& p_mods,
110 ModVector& r_mods);
111
112 void fillmap4auxVars(std::map<std::pair<int, int>, int>& map4auxVars);
113
114 // implement Handler::fixNodeErr
116 SolutionPoolPtr s_pool, bool& sol_found);
117
118 // base class method. Adds linear inequalities
119 void relaxInitFull(RelaxationPtr rel, SolutionPool* sp, bool* is_inf);
120
121 // Does nothing.
122 void relaxInitInc(RelaxationPtr rel, SolutionPool* sp, bool* is_inf);
123
124 // Does nothing.
125 void relaxNodeFull(NodePtr node, RelaxationPtr rel, bool* is_inf);
126
127 // Does nothing.
128 void relaxNodeInc(NodePtr node, RelaxationPtr rel, bool* is_inf);
129
131
132 // base class method. Adds linearlization cuts when available.
134 CutManager* cutman, SolutionPoolPtr s_pool, ModVector& p_mods,
135 ModVector& r_mods, bool* sol_found, SeparationStatus* status);
136
137 void setBTEngine(LPEnginePtr engine);
138
139 void setCutEngine(LPEnginePtr engine);
140
141 void setNLPEngine(EnginePtr engine);
142
143 void simplePresolve(ProblemPtr p, SolutionPoolPtr spool, ModVector& t_mods,
144 SolveStatus& status);
145 // base class method.
146 void writeStats(std::ostream& out) const;
147
148private:
150 struct SepaStats {
151 int iters;
152 int tangentcuts;
153 int optcuts;
154 int optrem;
155 double time;
156 };
157
159 struct PresolveStats {
160 int conDel;
161 int iters;
162 double time;
163 double timeN;
164 int vBnd;
165 int nMods;
166 };
167
168 struct NLPStats {
169 bool flag;
170 UInt nlp;
171 UInt opt;
172 UInt inf;
173 UInt iter_limit;
174 };
175
176 struct BoundTighteningStats {
177 int niters;
178 // VariableSet qvars; ///> The variables with quadratic terms
179 // int nqlb; ///> Number of quadratic variables with finite lb
180 // int nqub; ///> Number of quadratic variables with finite ub
181 // int nqlbs; ///> Number of quadratic variables whose lb was
182 // ///> found by simple tightening
183 // int nqubs; ///> Number of quadratic variables whose ub was
184 // ///> found by simple tightening
185 // int vBnds; ///> Number of times bounds tightened by
186 // ///> simple tightening
187 // int cBnds; ///> Number of times cons tightened by
188 // ///> simple tightening
189 // int nqlbq; ///> Number of quadratic variables whose lb was
190 // ///> found by quad tightening
191 // int nqubq; ///> Number of quadratic variables whose ub was
192 // ///> found by quad tightening
193 // int vBndq; ///> Number of times bounds tightened by
194 // ///> quad tightening
195 // int cBndq; ///> Number of times cons tightened by
196 // ///> quad tightening
197 // int nqlbl; ///> Number of quadratic variables whose lb was
198 // ///> found by lp tightening
199 // int nqubl; ///> Number of quadratic variables whose ub was
200 // ///> found by lp tightening
201 // int vBndl; ///> Number of times bounds tightened by
202 // ///> lp tightening
203 int nLP;
204 int dlb;
205 int dub;
206 double timeLP;
207 // double avg_range; ///> Average range of bounds of quadratic variables
208 // double sd_range; ///> Standard deviation of range
209 // double body_diag; ///> The length of body diagonal of hypercube formed
210 // by
211 // ///> the range of quadratic variables
212 };
213
215 double aTol_;
216
218 BoundTighteningStats bStats_;
219
221 double bTol_;
222
225 double defaultLb_;
226
229 double defaultUb_;
230
231 EnvPtr env_;
232
234 LoggerPtr logger_;
235
237 LPEnginePtr bte_;
238
240 LPEnginePtr cute_;
241
243 EnginePtr nlpe_;
244
246 NLPStats nlpStats_;
247
249 static const std::string me_;
250
252 ProblemPtr orig_;
253
255 ProblemPtr p_;
256
258 PresolveStats pStats_;
259
261 bool doQT_;
262
264 ConstraintVector optCuts_;
265
267 double rTol_;
268
270 SimplexQuadCutGenPtr simplexCut_;
271
273 SepaStats sStats_;
274
276 const Timer* timer_;
277
282 LinBilSet x0x1Funs_;
283
288 LinSqrMap x2Funs_;
289
302 void addCut_(VariablePtr x, VariablePtr y, double xl, double yl, double xval,
303 double yval, RelaxationPtr rel, bool& ifcuts);
304
305 ConstraintPtr addTangent_(VariablePtr x, VariablePtr y, double pt,
306 RelaxationPtr rel);
307
316 double calcUpperUnivar_(double a, double b, double lx, double ux);
317
326 bool calcVarBnd_(VariablePtr v, double coef, double lb, double ub, bool* c1);
327
328 bool calcVarBnd_(RelaxationPtr rel, VariablePtr v, double coef, double lb,
329 double ub, bool* c1, ModVector& p_mods, ModVector& r_mods);
330
341 bool calcVarBnd_(VariablePtr v1, VariablePtr v2, double coef, double lb,
342 double ub, bool* c1, bool* c2);
343
344 bool calcVarBnd_(RelaxationPtr rel, VariablePtr v1, VariablePtr v2,
345 double coef, double lb, double ub, bool* c1, bool* c2,
346 ModVector& p_mods, ModVector& r_mods);
347
358 bool calcVarBnd_(VariablePtr v, double a, double b, double ly, double uy,
359 bool* c1);
360
361 bool calcVarBnd_(RelaxationPtr rel, VariablePtr v, double a, double b,
362 double ly, double uy, bool* c1, ModVector& p_mods,
363 ModVector& r_mods);
364
365 void coeffImprov_();
366
371 void dupRows_(bool* changed);
372
381 void findLinPt_(double xval, double yval, double& xl, double& yl);
382
389 double getBndByLP_(bool& is_inf);
390
401 void getLfBnds_(LinearFunctionPtr lf, double& implLb, double& implUb,
402 DoubleVector& fwdLb, DoubleVector& fwdUb, UInt& count_inf_lb,
403 UInt& count_inf_ub);
404
421 LinearFunctionPtr getNewBilLf_(VariablePtr x0, double lb0, double ub0,
422 VariablePtr x1, double lb1, double ub1,
423 VariablePtr y, int type, double& rhs);
424
436 LinearFunctionPtr getNewSqLf_(VariablePtr x, VariablePtr y, double lb,
437 double ub, double& r);
438
449 void getQfBnds_(QuadraticFunctionPtr qf, double& implLb, double& implUb,
450 DoubleVector& fwdLb, DoubleVector& fwdUb, UInt& count_inf_lb,
451 UInt& count_inf_ub);
452
466 bool getQfLfBnds_(LinearFunctionPtr lf, QuadraticFunctionPtr qf,
467 double& implLb, double& implUb, DoubleVector& fwdLb,
468 DoubleVector& fwdUb, UInt& count_inf_lb, UInt& count_inf_ub,
469 VarVector& qvars);
470
471 bool getQfLfBnds_(RelaxationPtr rel, LinearFunctionPtr lf,
472 QuadraticFunctionPtr qf, double& implLb, double& implUb,
473 DoubleVector& fwdLb, DoubleVector& fwdUb,
474 UInt& count_inf_lb, UInt& count_inf_ub, VarVector& qvars);
475
486 double getSumExcept1_(DoubleVector::iterator b, DoubleVector::iterator e,
487 DoubleVector::iterator curr, BoundType bt, double bound,
488 UInt inf_count);
489
497 void getTermBnds_(VariablePtr v, double coef, double& lb, double& ub);
498
507 void getTermBnds_(VariablePtr v1, VariablePtr v2, double coef, double& lb,
508 double& ub);
509
519 void getTermBnds_(VariablePtr v, double a, double b, double& lb, double& ub);
520
522 bool isActive_(ConstraintPtr c, ConstSolutionPtr sol);
523
525 bool isAtBnds_(ConstVariablePtr x, double xval);
526
528 bool isFeasible_(const double* x, double objval, double& inf_meas);
529
531 bool isFeasibleToRelaxation_(RelaxationPtr rel, const double* x);
532
534 void linearize_(LinSqrMapIter lx2);
535
537 bool linearize_(LinBilSetIter linbil, bool isx0Binary, bool isx1Binary);
538
547 bool propBilBnds_(LinBil* lx0x1, bool* changed);
548
563 bool propBilBnds_(LinBil* lx0x1, RelaxationPtr rel, bool mod_rel,
564 bool* changed, ModVector& p_mods, ModVector& r_mods);
565
574 bool propSqrBnds_(LinSqrMapIter lx2, bool* changed);
575
590 bool propSqrBnds_(LinSqrMapIter lx2, RelaxationPtr rel, bool mod_rel,
591 bool* changed, ModVector& p_mods, ModVector& r_mods);
592
602 void relax_(RelaxationPtr rel, bool* is_inf);
603
605 void removeCut_(RelaxationPtr rel, ConstraintPtr c);
606
612 void resetBoundsinOrig_(DoubleVector& varlb, DoubleVector& varub);
613
615 void resetStats_();
616
618 void setItmpFromSol_(const double* x);
619
629 bool tightenLP_(RelaxationPtr rel, double bestSol, bool* changed,
630 ModVector& p_mods, ModVector& r_mods);
631
639 bool tightenQuad_(bool* changed);
640
641 bool tightenQuad_(RelaxationPtr rel, double bestSol, bool* changed,
642 ModVector& p_mods, ModVector& r_mods);
643
651 bool tightenSimple_(bool* changed);
652
661 void updateBoundsinOrig_(RelaxationPtr rel, const double* x,
662 DoubleVector& varlb, DoubleVector& varub);
663
670 bool treatDupRows_(ConstraintPtr c1, ConstraintPtr c2, double mult,
671 bool* changed);
672
685 int updatePBounds_(VariablePtr v, double lb, double ub, bool* changed);
686
699 int updatePBounds_(ProblemPtr p, VariablePtr v, double lb, double ub,
700 ModVector& mods);
701
721 int updatePBounds_(VariablePtr v, double lb, double ub, RelaxationPtr rel,
722 bool mod_rel, bool* changed, ModVector& p_mods,
723 ModVector& r_mods);
732 void updateUb_(SolutionPoolPtr s_pool, double nlpval, bool check_feas,
733 bool& sol_found);
734
745 void upBilCon_(LinBil* lx0x1, RelaxationPtr rel, ModVector& r_mods);
746
756 void upSqCon_(LinSqrPtr ls, RelaxationPtr rel, ModVector& r_mods);
757
766 bool varBndsFromCons_(bool* changed);
767};
768
771} // namespace Minotaur
772#endif
773
Define abstract base class for handlers of various kinds.
Declare a class for storing linear under and overestimators of bilinear functions ,...
Define class for generating cuts for quadratic constraints from the simplex tableau.
Base class for describing candidates for branching on a node in branch-and-bound.
Definition: BrCand.h:32
The Constraint class is used to manage a constraint.
Definition: Constraint.h:61
Abstract base class to manage cuts in the relaxation.
Definition: CutManager.h:42
Definition: Engine.h:34
Definition: Environment.h:28
Base class for handling specific types of constraints or objective.
Definition: Handler.h:49
Definition: LPEngine.h:29
Definition: LinBil.h:26
The base class linear function is of the form c'x.
Definition: LinearFunction.h:31
Definition: Logger.h:37
Definition: Modification.h:29
Definition: Node.h:54
Definition: Problem.h:74
‍Iterator for LinSqrMap
Definition: QuadHandler.h:63
Branches getBranches(BrCandPtr cand, DoubleVector &x, RelaxationPtr rel, SolutionPoolPtr s_pool)
Return branches for branching.
Definition: QuadHandler.cpp:437
std::string getName() const
Return the name of the handler.
Definition: QuadHandler.cpp:712
bool presolveNode(RelaxationPtr p, NodePtr node, SolutionPoolPtr s_pool, ModVector &p_mods, ModVector &r_mods)
Presolve the problem and its relaxation at a node.
Definition: QuadHandler.cpp:1232
void relaxInitFull(RelaxationPtr rel, SolutionPool *sp, bool *is_inf)
Create root relaxation if doing full node relaxations.
Definition: QuadHandler.cpp:1622
bool isFeasible(ConstSolutionPtr sol, RelaxationPtr relaxation, bool &should_prune, double &inf_meas)
Check if a solution is feasible.
Definition: QuadHandler.cpp:975
void removeCuts(RelaxationPtr rel, ConstSolutionPtr sol)
Definition: QuadHandler.cpp:1642
double addDefaultBounds(VariablePtr x, BoundType lu)
Definition: QuadHandler.cpp:184
SolveStatus presolve(PreModQ *pre_mods, bool *changed, Solution **sol)
Initial presolve.
Definition: QuadHandler.cpp:1134
QuadHandler(EnvPtr env, ProblemPtr problem)
Default constructor.
Definition: QuadHandler.cpp:60
void addConstraint(ConstraintPtr newcon)
Add constraint to be handled by this handler.
Definition: QuadHandler.cpp:130
void relaxNodeFull(NodePtr node, RelaxationPtr rel, bool *is_inf)
Create a relaxation for a node, building from scratch.
Definition: QuadHandler.cpp:1632
ModificationPtr getBrMod(BrCandPtr cand, DoubleVector &x, RelaxationPtr rel, BranchDirection dir)
Get the modifcation that creates a given (up or down) branch.
Definition: QuadHandler.cpp:631
void relaxInitInc(RelaxationPtr rel, SolutionPool *sp, bool *is_inf)
Create root relaxation if doing incremental node relaxations.
Definition: QuadHandler.cpp:1627
void simplePresolve(ProblemPtr p, SolutionPoolPtr spool, ModVector &t_mods, SolveStatus &status)
Definition: QuadHandler.cpp:1174
void relaxNodeInc(NodePtr node, RelaxationPtr rel, bool *is_inf)
Create an incremental relaxation for a node.
Definition: QuadHandler.cpp:1637
void getBranchingCandidates(RelaxationPtr rel, const DoubleVector &x, ModVector &mods, BrVarCandSet &cands, BrCandVector &gencands, bool &is_inf)
find branching candidates.
Definition: QuadHandler.cpp:488
bool postSolveRootNode(RelaxationPtr rel, SolutionPoolPtr s_pool, ConstSolutionPtr sol, ModVector &p_mods, ModVector &r_mods)
At the root node post solve the problem and its relaxation. LP based bound tightening (OBBT) is emplo...
Definition: QuadHandler.cpp:1425
void writeStats(std::ostream &out) const
Write statistics to ostream out.
Definition: QuadHandler.cpp:3697
void separate(ConstSolutionPtr sol, NodePtr node, RelaxationPtr rel, CutManager *cutman, SolutionPoolPtr s_pool, ModVector &p_mods, ModVector &r_mods, bool *sol_found, SeparationStatus *status)
add cuts to separate a given point.
Definition: QuadHandler.cpp:1713
~QuadHandler()
Destroy.
Definition: QuadHandler.cpp:105
int fixNodeErr(RelaxationPtr rel, ConstSolutionPtr sol, SolutionPoolPtr s_pool, bool &sol_found)
Definition: QuadHandler.cpp:369
Definition: QuadraticFunction.h:38
Definition: Relaxation.h:53
Definition: SimplexQuadCutGen.h:78
Definition: SolutionPool.h:28
Definition: Solution.h:30
Definition: Timer.h:40
Definition: Variable.h:31
Definition: ActiveNodeStore.h:20
QuadHandler * QuadHandlerPtr
Shared pointer to QuadHandler.
Definition: QuadHandler.h:770
BranchDirection
Two directions for branching.
Definition: Types.h:201
BoundType
Different types of variable-bounds.
Definition: Types.h:131
std::vector< LinSqrPtr > LinSqrVec
‍Pointer to LinSqr
Definition: QuadHandler.h:50
LinBilSet::iterator LinBilSetIter
Iterator of LinBil objects over a set.
Definition: LinBil.h:119
unsigned int UInt
Unsigned integer.
Definition: Types.h:30
std::map< VariablePtr, LinSqrPtr, CompareVariablePtr > LinSqrMap
‍Iterator for LinSqr
Definition: QuadHandler.h:54
LinSqrVec::iterator LinSqrVecIter
‍Vector of LinSqr
Definition: QuadHandler.h:51
SeparationStatus
Status from separation routine:
Definition: Types.h:217
SolveStatus
Different states an algorithm like branch-and-bound can be in.
Definition: Types.h:158
std::set< LinBil *, CompareLinBil > LinBilSet
A set of bilinear objects.
Definition: LinBil.h:116
A structure to save information about constraints of the form .
Definition: QuadHandler.h:36
VariablePtr x
‍The variable y.
Definition: QuadHandler.h:38
LinSqr(VariablePtr y0, VariablePtr x0, ConstraintPtr con)
‍Quadratic constraint in the transformed problem
Definition: QuadHandler.h:41
ConstraintPtr oeCon
‍The variable x.
Definition: QuadHandler.h:39
ConstraintPtr qcon
‍The linear constraint that gives the over estimator
Definition: QuadHandler.h:40

Minotaur source code documented by Doxygen 1.9.4 on Thu Apr 24 2025