Minotaur 0.4.1
Docs for developers
kPowHandler.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 "CGraph.h"
21#include "Handler.h"
22
23namespace Minotaur {
24
25class LinearFunction;
26class Timer;
27class LPEngine;
28typedef LPEngine *LPEnginePtr;
29typedef LinearFunction *LinearFunctionPtr;
30typedef Engine *EnginePtr;
31
36struct LinkPow {
37 VariablePtr y;
39 double k;
41};
42typedef LinkPow *LinkPowPtr;
43typedef std::vector<LinkPowPtr> LinkPowVec;
44typedef LinkPowVec::iterator LinkPowVecIter;
45
47typedef std::map<VariablePtr, LinkPowPtr, CompareVariablePtr> LinkPowMap;
48typedef LinkPowMap::iterator LinkPowMapIter;
49
55class kPowHandler : public Handler {
56 public:
58 // kPowHandler(EnvPtr env, ProblemPtr problem);
59
60 kPowHandler(EnvPtr env, ProblemPtr problem, ProblemPtr orig_p);
61
64
65 // base class method
66 void addConstraint(ConstraintPtr newcon);
67
68 // Implement Handler::getBranches().
69 Branches getBranches(BrCandPtr cand, DoubleVector &x, RelaxationPtr rel,
70 SolutionPoolPtr s_pool);
71
72 // base class method
73 void getBranchingCandidates(RelaxationPtr rel, const DoubleVector &x,
74 ModVector &mods, BrVarCandSet &cands,
75 BrCandVector &gencands, bool &is_inf);
76
77 // base class method
78 ModificationPtr getBrMod(BrCandPtr cand, DoubleVector &x, RelaxationPtr rel,
79 BranchDirection dir);
80
81 // base class method
82 std::string getName() const;
83
84 // base class method.
85 bool isFeasible(ConstSolutionPtr sol, RelaxationPtr relaxation,
86 bool &should_prune, double &inf_meas);
87
88 // base class method.
89 SolveStatus presolve(PreModQ *pre_mods, bool *changed, Solution **sol);
90
91 // base class method. Tightens bounds.
93 ModVector &p_mods, ModVector &r_mods);
94
95 // implement Handler::postSolveRootNode
97 ConstSolutionPtr sol, ModVector &p_mods,
98 ModVector &r_mods);
99
100 // implement Handler::fixNodeErr
102 SolutionPoolPtr s_pool, bool &sol_found);
103
104 // base class method. Adds linear inequalities
105 void relaxInitFull(RelaxationPtr rel, SolutionPool *, bool *is_inf);
106
107 // Does nothing.
108 void relaxInitInc(RelaxationPtr rel, SolutionPool *, bool *is_inf);
109
110 // Does nothing.
111 void relaxNodeFull(NodePtr node, RelaxationPtr rel, bool *is_inf);
112
113 // Does nothing.
114 void relaxNodeInc(NodePtr node, RelaxationPtr rel, bool *is_inf);
115
116 // base class method. Adds linearlization cuts when available.
118 CutManager *cutman, SolutionPoolPtr s_pool, ModVector &p_mods,
119 ModVector &r_mods, bool *sol_found, SeparationStatus *status);
120
121 void setBTEngine(LPEnginePtr engine);
122
123 void setNLPEngine(EnginePtr engine);
124
125 // base class method.
126 void writeStats(std::ostream &out) const;
127
128 private:
130 struct SepaStats {
131 int iters;
132 int cuts;
133 double time;
134 };
135
137 struct PresolveStats {
138 int iters;
139 double time;
140 double timeN;
141 int vBnd;
142 int nMods;
143 };
144
145 struct NLPStats {
146 bool flag;
147 UInt nlp;
148 UInt opt;
149 UInt inf;
150 UInt iter_limit;
151 };
152
153 struct BoundTighteningStats {
154 int niters;
155 // VariableSet qvars; ///> The variables with Quadratic terms
156 // int nqlb; ///> Number of Quadratic variables with finite lb
157 // int nqub; ///> Number of Quadratic variables with finite ub
158 // int nqlbs; ///> Number of Quadratic variables whose lb was
159 // ///> found by simple tightening
160 // int nqubs; ///> Number of Quadratic variables whose ub was
161 // ///> found by simple tightening
162 // int vBnds; ///> Number of times bounds tightened by
163 // ///> simple tightening
164 // int cBnds; ///> Number of times cons tightened by
165 // ///> simple tightening
166 // int nqlbq; ///> Number of Quadratic variables whose lb was
167 // ///> found by kPow tightening
168 // int nqubq; ///> Number of Quadratic variables whose ub was
169 // ///> found by kPow tightening
170 // int vBndq; ///> Number of times bounds tightened by
171 // ///> kPow tightening
172 // int cBndq; ///> Number of times cons tightened by
173 // ///> kPow tightening
174 // int nqlbl; ///> Number of Quadratic variables whose lb was
175 // ///> found by lp tightening
176 // int nqubl; ///> Number of Quadratic variables whose ub was
177 // ///> found by lp tightening
178 // int vBndl; ///> Number of times bounds tightened by
179 // ///> lp tightening
180 int nLP;
181 int dlb;
182 int dub;
183 double timeLP;
184 // double avg_range; ///> Average range of bounds of Quadratic variables
185 // double sd_range; ///> Standard deviation of range
186 // double body_diag; ///> The length of body diagonal of hypercube formed
187 // by
188 // ///> the range of Quadratic variables
189 };
190
192 double aTol_;
193
195 BoundTighteningStats bStats_;
196
198 double bTol_;
199
202 double defaultLb_;
203
206 double defaultUb_;
207
208 EnvPtr env_;
209
211 LoggerPtr logger_;
212
214 LPEnginePtr bte_;
215
217 EnginePtr nlpe_;
218
220 NLPStats nlpStats_;
221
223 static const std::string me_;
224
226 ProblemPtr orig_;
227
229 ProblemPtr p_;
230
232 PresolveStats pStats_;
233
235 bool doQT_;
236
238 double rTol_;
239
241 SepaStats sStats_;
242
244 const Timer *timer_;
245
250 LinkPowMap xkFuns_;
251
264 void addCut_(VariablePtr x, VariablePtr y, double xl, double yl, double xval,
265 double yval, RelaxationPtr rel, bool &ifcuts, double k);
266
269 double addDefaultBounds_(VariablePtr x, BoundType lu);
270
279 double calcUpperUnivar_(double a, double b, double lx, double ux);
280
289 bool calcVarBnd_(VariablePtr v, double coef, double lb, double ub, bool *c1);
290
291 bool calcVarBnd_(RelaxationPtr rel, VariablePtr v, double coef, double lb,
292 double ub, bool *c1, ModVector &p_mods, ModVector &r_mods);
293
304 bool calcVarBnd_(VariablePtr v1, VariablePtr v2, double coef, double lb,
305 double ub, bool *c1, bool *c2);
306
307 bool calcVarBnd_(RelaxationPtr rel, VariablePtr v1, VariablePtr v2,
308 double coef, double lb, double ub, bool *c1, bool *c2,
309 ModVector &p_mods, ModVector &r_mods);
310
321 bool calcVarBnd_(VariablePtr v, double a, double b, double ly, double uy,
322 bool *c1);
323
324 bool calcVarBnd_(RelaxationPtr rel, VariablePtr v, double a, double b,
325 double ly, double uy, bool *c1, ModVector &p_mods,
326 ModVector &r_mods);
327
328 void coeffImprov_();
329
338 void findLinPt_(double xval, double yval, double &xl, double &yl, double k);
339
346 double getBndByLP_(bool &is_inf);
347
358 void getLfBnds_(LinearFunctionPtr lf, double &implLb, double &implUb,
359 DoubleVector &fwdLb, DoubleVector &fwdUb, UInt &count_inf_lb,
360 UInt &count_inf_ub);
361
376 LinearFunctionPtr getNewKpowLf_(VariablePtr y, VariablePtr x, double k,
377 double lb, double ub, double &rhs, int type);
378
389 void getQfBnds_(QuadraticFunctionPtr qf, double &implLb, double &implUb,
390 DoubleVector &fwdLb, DoubleVector &fwdUb, UInt &count_inf_lb,
391 UInt &count_inf_ub);
392
406 bool getQfLfBnds_(LinearFunctionPtr lf, QuadraticFunctionPtr qf,
407 double &implLb, double &implUb, DoubleVector &fwdLb,
408 DoubleVector &fwdUb, UInt &count_inf_lb, UInt &count_inf_ub,
409 VarVector &qvars);
410
411 bool getQfLfBnds_(RelaxationPtr rel, LinearFunctionPtr lf,
412 QuadraticFunctionPtr qf, double &implLb, double &implUb,
413 DoubleVector &fwdLb, DoubleVector &fwdUb,
414 UInt &count_inf_lb, UInt &count_inf_ub, VarVector &qvars);
415
426 double getSumExcept1_(DoubleVector::iterator b, DoubleVector::iterator e,
427 DoubleVector::iterator curr, BoundType bt, double bound,
428 UInt inf_count);
429
437 void getTermBnds_(VariablePtr v, double coef, double &lb, double &ub);
438
447 void getTermBnds_(VariablePtr v1, VariablePtr v2, double coef, double &lb,
448 double &ub);
449
459 void getTermBnds_(VariablePtr v, double a, double b, double &lb, double &ub);
460
462 bool isAtBnds_(ConstVariablePtr x, double xval);
463
465 bool isFeasibleToRelaxation_(RelaxationPtr rel, const double *x);
466
475 bool propKPowBnds_(LinkPowMapIter lxk, bool *changed);
476
491 bool propKPowBnds_(LinkPowMapIter lxk, RelaxationPtr rel, bool mod_rel,
492 bool *changed, ModVector &p_mods, ModVector &r_mods);
493
503 void relax_(RelaxationPtr rel, bool *is_inf);
504
510 void resetBoundsinOrig_(DoubleVector &varlb, DoubleVector &varub);
511
513 void resetStats_();
514
516 void setItmpFromSol_(const double *x);
517
527 bool tightenLP_(RelaxationPtr rel, double bestSol, bool *changed,
528 ModVector &p_mods, ModVector &r_mods);
529
537 bool tightenkPow_(bool *changed);
538
539 bool tightenkPow_(RelaxationPtr rel, double bestSol, bool *changed,
540 ModVector &p_mods, ModVector &r_mods);
541
549 bool tightenSimple_(bool *changed);
550
559 void updateBoundsinOrig_(RelaxationPtr rel, const double *x,
560 DoubleVector &varlb, DoubleVector &varub);
561
574 int updatePBounds_(VariablePtr v, double lb, double ub, bool *changed);
575
595 int updatePBounds_(VariablePtr v, double lb, double ub, RelaxationPtr rel,
596 bool mod_rel, bool *changed, ModVector &p_mods,
597 ModVector &r_mods);
605 void updateUb_(SolutionPoolPtr s_pool, double nlpval, bool &sol_found);
606
619 void upSqCon_(ConstraintPtr con, VariablePtr x, VariablePtr y, double k,
620 RelaxationPtr rel, ModVector &r_mods);
621
630 bool varBndsFromCons_(bool *changed);
631};
632
635} // namespace Minotaur
636#endif
637
Declare class CGraph for storing computational graph of a nonlinear function.
Define abstract base class for handlers of various kinds.
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
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
Definition: QuadraticFunction.h:38
Definition: Relaxation.h:53
Definition: SolutionPool.h:28
Definition: Solution.h:30
Definition: Timer.h:40
Definition: Variable.h:31
‍Iterator for LinkPowMap
Definition: kPowHandler.h:55
void relaxNodeInc(NodePtr node, RelaxationPtr rel, bool *is_inf)
Create an incremental relaxation for a node.
Definition: kPowHandler.cpp:1132
kPowHandler(EnvPtr env, ProblemPtr problem, ProblemPtr orig_p)
Default constructor.
Definition: kPowHandler.cpp:62
void relaxInitInc(RelaxationPtr rel, SolutionPool *, bool *is_inf)
Create root relaxation if doing incremental node relaxations.
Definition: kPowHandler.cpp:1122
bool isFeasible(ConstSolutionPtr sol, RelaxationPtr relaxation, bool &should_prune, double &inf_meas)
Check if a solution is feasible.
Definition: kPowHandler.cpp:652
void addConstraint(ConstraintPtr newcon)
Add constraint to be handled by this handler.
Definition: kPowHandler.cpp:98
void writeStats(std::ostream &out) const
Write statistics to ostream out.
Definition: kPowHandler.cpp:2816
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: kPowHandler.cpp:1180
int fixNodeErr(RelaxationPtr rel, ConstSolutionPtr sol, SolutionPoolPtr s_pool, bool &sol_found)
Definition: kPowHandler.cpp:292
void relaxNodeFull(NodePtr node, RelaxationPtr rel, bool *is_inf)
Create a relaxation for a node, building from scratch.
Definition: kPowHandler.cpp:1127
void getBranchingCandidates(RelaxationPtr rel, const DoubleVector &x, ModVector &mods, BrVarCandSet &cands, BrCandVector &gencands, bool &is_inf)
find branching candidates.
Definition: kPowHandler.cpp:409
void relaxInitFull(RelaxationPtr rel, SolutionPool *, bool *is_inf)
Create root relaxation if doing full node relaxations.
Definition: kPowHandler.cpp:1116
~kPowHandler()
Destroy.
Definition: kPowHandler.cpp:82
ModificationPtr getBrMod(BrCandPtr cand, DoubleVector &x, RelaxationPtr rel, BranchDirection dir)
Get the modifcation that creates a given (up or down) branch.
Definition: kPowHandler.cpp:450
Branches getBranches(BrCandPtr cand, DoubleVector &x, RelaxationPtr rel, SolutionPoolPtr s_pool)
Return branches for branching.
Definition: kPowHandler.cpp:358
std::string getName() const
Return the name of the handler.
Definition: kPowHandler.cpp:486
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: kPowHandler.cpp:1012
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: kPowHandler.cpp:861
SolveStatus presolve(PreModQ *pre_mods, bool *changed, Solution **sol)
Initial presolve.
Definition: kPowHandler.cpp:766
Definition: ActiveNodeStore.h:20
BranchDirection
Two directions for branching.
Definition: Types.h:201
LinkPowVec::iterator LinkPowVecIter
‍Vector of LinkPow
Definition: kPowHandler.h:44
BoundType
Different types of variable-bounds.
Definition: Types.h:131
kPowHandler * kPowHandlerPtr
Shared pointer to kPowHandler.
Definition: kPowHandler.h:634
std::map< VariablePtr, LinkPowPtr, CompareVariablePtr > LinkPowMap
‍Iterator for LinkPow
Definition: kPowHandler.h:47
std::vector< LinkPowPtr > LinkPowVec
‍Pointer to LinkPow
Definition: kPowHandler.h:43
unsigned int UInt
Unsigned integer.
Definition: Types.h:30
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
A structure to save information about constraints of the form .
Definition: kPowHandler.h:36
double k
‍The variable x.
Definition: kPowHandler.h:39
ConstraintPtr oeCon
‍The power k
Definition: kPowHandler.h:40
VariablePtr x
‍The variable y.
Definition: kPowHandler.h:38

Minotaur source code documented by Doxygen 1.9.4 on Fri May 16 2025