Minotaur 0.4.1
Docs for developers
Problem.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
13#ifndef MINOTAURPROBLEM_H
14#define MINOTAURPROBLEM_H
15
16#include <ios>
17#include "Types.h"
18#include "Constraint.h"
19#include "Engine.h"
20#include "HessianOfLag.h"
21#include "Jacobian.h"
22#include "Objective.h"
23#include "ProblemSize.h"
24#include "SOS.h"
25#include "Types.h"
26#include "Variable.h"
27#include <limits>
28namespace Minotaur
29{
30
51 {
52 int nvars = 0;
53 int nterms = 0;
54 int nsqterm = 0;
55 int nbilterm = 0;
56 int nposcoefone = 0;
57 int nnegcoefone = 0;
58 int nposcoef = 0;
59 int nnegcoef = 0;
60 int nposcont = 0;
61 int nnegcont = 0;
62 int nposbin = 0;
63 int nnegbin = 0;
64 int nposint = 0;
65 int nnegint = 0;
66 int eqwt = 0;
67 double wt1 = 0.0;
68 double wt2 = 0.0;
69 double sumnegwt = 0.0;
70 int con = 0;
71 };
72
74{
75public:
77 Problem(EnvPtr env);
78
80 virtual ~Problem();
81
83 virtual void addToCons(ConstraintPtr cons, double c);
84
86 virtual void addToObj(LinearFunctionPtr lf);
87
89 virtual void addToObj(double cb);
90
91
93 virtual void calculateSize(bool shouldRedo = false);
94
96 virtual void changeBoundByInd(UInt ind, BoundType lu, double new_val);
97
99 virtual void changeBoundByInd(UInt ind, double new_lb, double new_ub);
100
102 virtual void changeBound(VariablePtr var, BoundType lu, double new_val);
103
105 virtual void changeBound(VariablePtr var, double new_lb, double new_ub);
106
108 virtual void changeBound(ConstraintPtr con, BoundType lu, double new_val);
109
111 virtual void changeBound(ConstraintPtr con, double new_lb, double new_ub);
112
121 double lb, double ub);
122
129
137 virtual void changeObj(FunctionPtr f, double cb);
138
146 virtual int checkConVars() const;
147
148 // Classification of Constraints
149 virtual void classifyCon();
150
160 ProblemPtr clone(EnvPtr env) const;
161
170 ProblemPtr shuffle(bool varshuff, bool conshuff, EnvPtr env);
171
177 void cg2qf();
178
180 virtual ConstraintConstIterator consBegin() const
181 {
182 return cons_.begin();
183 }
184
186 virtual ConstraintConstIterator consEnd() const
187 {
188 return cons_.end();
189 }
190
192 virtual void delMarkedCons();
193
201 virtual void delMarkedVars(bool keep = false);
202
207 virtual ProblemType findType();
208
210 virtual ConstraintPtr getConstraint(UInt index) const;
211
218 virtual DoubleVector* getDebugSol() const;
219
221 virtual HessianOfLagPtr getHessian() const;
222
224 virtual JacobianPtr getJacobian() const;
225
227 virtual UInt getNumCons() const
228 {
229 return cons_.size();
230 }
231
233 virtual UInt getNumDCons() const
234 {
235 return numDCons_;
236 }
237
239 virtual UInt getNumDVars() const
240 {
241 return numDVars_;
242 }
243
256 virtual UInt getNumHessNnzs() const;
257
259 virtual UInt getNumJacNnzs() const;
260
263
266
269
271 virtual UInt getNumVars() const
272 {
273 return vars_.size();
274 }
275
277 virtual ObjectivePtr getObjective() const;
278
280 double getObjValue(const double* x, int* err) const;
281
284
286 double getSizeEstimate();
287
289 virtual VariablePtr getVariable(UInt index) const;
290
295 virtual bool hasNativeDer() const;
296
301 virtual bool isDebugSolFeas(double atol, double rtol);
302
307 virtual bool isLinear();
308
310 virtual bool isMarkedDel(ConstConstraintPtr con);
311
313 virtual bool isMarkedDel(ConstVariablePtr var);
314
320 virtual bool isQP();
321
327 virtual bool isQuadratic();
328
336 virtual void markDelete(ConstraintPtr con);
337
338 // Helper function for common quadratic checks
339 inline bool checkQuadCond(const ConstraintStats& stats)
340 {
341 return stats.nsqterm == stats.nterms + stats.nvars;
342 }
343
344 // Helper function for coefficient checks
345 inline bool checkCoeffCond(const ConstraintStats& stats,
346 bool positive)
347 {
348 if (positive) {
349 return stats.nposcoefone == stats.nterms + stats.nvars ||
350 (stats.nposcoef == stats.nterms + stats.nvars &&
351 stats.eqwt == stats.nterms + stats.nvars);
352 } else {
353 return stats.nnegcoefone == stats.nterms + stats.nvars ||
354 (stats.nnegcoef == stats.nterms + stats.nvars &&
355 stats.eqwt == stats.nterms + stats.nvars);
356 }
357 }
358
366 virtual void markDelete(VariablePtr var);
367
369 virtual void negateObj();
370
375 virtual VariablePtr newBinaryVariable();
376
382 virtual VariablePtr newBinaryVariable(std::string name);
383
393 virtual ConstraintPtr newConstraint(FunctionPtr f, double lb, double ub);
394
404 virtual ConstraintPtr newConstraint(FunctionPtr f, double lb, double ub,
405 std::string name);
406
416 virtual ObjectivePtr newObjective(FunctionPtr f, double cb,
417 ObjectiveType otyp);
418
419 virtual ObjectivePtr newObjective(double cb, ObjectiveType otyp);
420
432 virtual ObjectivePtr newObjective(FunctionPtr f, double cb,
433 ObjectiveType otyp, std::string name);
434
450 virtual SOSPtr newSOS(int n, SOSType type, const double* weights,
451 const VarVector& vars, int priority,
452 std::string name);
453
467 virtual SOSPtr newSOS(int n, SOSType type, const double* weights,
468 const VarVector& vars, int priority);
469
474 virtual VariablePtr newVariable(VarSrcType stype = VarOrig);
475
485 virtual VariablePtr newVariable(double lb, double ub, VariableType vtype,
486 VarSrcType = VarOrig);
487
497 virtual VariablePtr newVariable(double lb, double ub, VariableType vtype,
498 std::string name, VarSrcType = VarOrig);
499
510 virtual void newVariables(VariableConstIterator v_begin,
511 VariableConstIterator v_end,
512 VarSrcType stype = VarOrig);
513
521 virtual void prepareForSolve();
522
527 virtual void objToCons();
528
530 virtual void removeObjective();
531
533 virtual QuadraticFunctionPtr removeQuadFromObj();
534
535 virtual NonlinearFunctionPtr removeNonlinFromObj();
536
541 virtual void resetDer();
542
543 virtual void resConTypCnt();
544
550 virtual void reverseSense(ConstraintPtr cons);
551
559 virtual void setDebugSol(const DoubleVector& x);
560
569 virtual void setEngine(Engine* engine);
570
576 virtual void setHessian(HessianOfLagPtr hessian);
577
587 virtual void setInitialPoint(const double* x);
588
598 virtual void setInitialPoint(const double* x, size_t k);
599
600 virtual void setInitVal(VariablePtr v, double val);
601 virtual void setInitValByInd(UInt ind, double val);
602
608 virtual void setJacobian(JacobianPtr jacobian);
609
614 void setNativeDer();
615
622 virtual void setVarType(VariablePtr var, VariableType type);
623
624 virtual SOSConstIterator sos1Begin() const
625 {
626 return sos1_.begin();
627 };
628 virtual SOSConstIterator sos1End() const
629 {
630 return sos1_.end();
631 };
632 virtual SOSConstIterator sos2Begin() const
633 {
634 return sos2_.begin();
635 };
636 virtual SOSConstIterator sos2End() const
637 {
638 return sos2_.end();
639 };
640
650 virtual void subst(VariablePtr out, VariablePtr in, double rat = 1.0);
651
653 virtual void unsetEngine();
654
656 virtual VariableConstIterator varsBegin() const
657 {
658 return vars_.begin();
659 }
660
662 virtual VariableConstIterator varsEnd() const
663 {
664 return vars_.end();
665 }
666
668 virtual void write(std::ostream& out, std::streamsize out_p = 6) const;
669
671 virtual void writeSize(std::ostream& out) const;
672
673 //Print statistics in log_level 2
674 void statistics(std::ostream& out) const;
675
676 const double INFTY = std::numeric_limits<double>::infinity();
677
678 protected:
679 //To check type of constraint
680 bool isAggregation_(ConstraintPtr c);
681 bool isPrecedence_(ConstraintPtr c, const ConstraintStats &stats);
682 bool isVariableBound_(ConstraintPtr c, const ConstraintStats &stats);
683 bool isSetPartitioning_(ConstraintPtr c, const ConstraintStats &stats);
684 bool isSetPacking_(ConstraintPtr c, const ConstraintStats &stats);
685 bool isSetCovering_(ConstraintPtr c, const ConstraintStats &stats);
686 bool isCardinality_(ConstraintPtr c, const ConstraintStats &stats);
687 bool isInvariantKnapsack_(ConstraintPtr c, const ConstraintStats &stats);
688 bool isEquationKnapsack_(ConstraintPtr c, const ConstraintStats &stats);
689 bool isBinPacking_(ConstraintPtr c, const ConstraintStats &stats);
690 bool isKnapsack_(ConstraintPtr c, const ConstraintStats &stats);
691 bool isIntegerKnapsack_(ConstraintPtr c, const ConstraintStats &stats);
692 bool isMixedBinary_(ConstraintPtr c, const ConstraintStats &stats);
693 bool isNoSpecificStructure_(ConstraintPtr c);
694 bool isDiagonalQuadratic_(ConstraintPtr c, const ConstraintStats& stats);
695 bool isSimpleBall_(ConstraintPtr c, const ConstraintStats& stats);
696 bool isEllipsoid_(ConstraintPtr c, const ConstraintStats& stats);
697 bool isComplementEllipsoid_(ConstraintPtr c, const ConstraintStats& stats);
698 bool isComplementSimpleBall_(ConstraintPtr c, const ConstraintStats& stats);
699 bool isSocSimple_(ConstraintPtr c, const ConstraintStats& stats);
700 bool isPureBilinear_(ConstraintPtr c, const ConstraintStats& stats);
701 bool isOtherQuadType_(ConstraintPtr c, const ConstraintStats& stats);
702
703 //Print Count table for constraint size
704 void printConstraintStatistics_();
705
706 //Print Count table for Quadratic constraint size
707 void printConstraintStatisticsQuad_();
708
709 // function for lock number
710 void lockNum_();
711
713 ConstraintVector cons_;
714
725
730 DoubleVector* debugSol_;
731
734
737
740
743
745 static const std::string me_;
746
749
752
755
758
761
764
767
770
772 SOSVector sos1_;
773
775 SOSVector sos2_;
776
778 VarVector vars_;
779
784 VarVector varsRem_;
785
788
790 virtual void countConsTypes_();
791
793 virtual void countObjTypes_();
794
796 virtual void countVarTypes_();
797
803 virtual void findVarFunTypes_();
804
805 bool isPolyp_();
806
807 void setIndex_(VariablePtr v, UInt i);
808};
809} // namespace Minotaur
810#endif
Get information about a constraint in a given Problem.
Define the base class Engine.
Declare class HessianOfLag to get information about hessian of Lagrangean of a Problem.
Get information about Jacobian of a given Problem.
Declare the Objective class for storing and manipulating objective function.
Declare structure ProblemSize for storing statistics about the problem.
Declare data structures for an SOS-I and SOS-II constraint.
Declare important 'types' used in Minotaur.
Define the Variable class.
The Constraint class is used to manage a constraint.
Definition: Constraint.h:61
Definition: Engine.h:34
Definition: Environment.h:28
Definition: Function.h:37
Definition: HessianOfLag.h:47
Definition: Jacobian.h:53
The base class linear function is of the form c'x.
Definition: LinearFunction.h:31
Definition: Logger.h:37
Base class for nonlinear functions.
Definition: NonlinearFunction.h:31
Definition: Objective.h:39
Definition: Problem.h:74
virtual void setJacobian(JacobianPtr jacobian)
Set the jacobian of the constraints.
Definition: Problem.cpp:2424
virtual void changeConstraint(ConstraintPtr con, LinearFunctionPtr lf, double lb, double ub)
Change the linear function, and the bounds of a constraint.
Definition: Problem.cpp:273
ConstProblemSizePtr getSize() const
Fill up the statistics about the size of the problem into size_.
Definition: Problem.cpp:1868
virtual void prepareForSolve()
Setup problem data-structures for solving it.
Definition: Problem.cpp:2278
UInt nextCId_
ID of the next constraint.
Definition: Problem.h:751
virtual void addToObj(LinearFunctionPtr lf)
Add a linear function to the objective.
Definition: Problem.cpp:97
virtual void changeBoundByInd(UInt ind, BoundType lu, double new_val)
Change a bound (lower or upper) on a variable with ID=id.
Definition: Problem.cpp:151
virtual ConstraintPtr getConstraint(UInt index) const
Return a pointer to the constraint with a given index.
Definition: Problem.cpp:1801
virtual void unsetEngine()
Should be called in the Engine's destructor.
Definition: Problem.cpp:2509
virtual bool isLinear()
Returns true if the problem has only linear constraints and linear objectives.
Definition: Problem.cpp:1958
VarVector vars_
Vector of variables.
Definition: Problem.h:778
virtual void resetDer()
Definition: Problem.cpp:2324
virtual ConstraintConstIterator consBegin() const
Iterate over constraints. Returns the 'begin' iterator.
Definition: Problem.h:180
virtual QuadraticFunctionPtr removeQuadFromObj()
Remove the quadratic part of objective and return it.
Definition: Problem.cpp:2302
virtual UInt getNumVars() const
Return the number of variables.
Definition: Problem.h:271
virtual VariablePtr newBinaryVariable()
Add a new binary variable and return a pointer to it. A name is automatically generated by default.
Definition: Problem.cpp:2054
virtual void removeObjective()
Remove objective from the Problem.
Definition: Problem.cpp:2291
ProblemPtr clone(EnvPtr env) const
Clone the given Problem class. Jacobian and Hessian in the cloned problem are NULL.
Definition: Problem.cpp:1034
virtual void countVarTypes_()
Fill up the size_ with number of variables of each type.
Definition: Problem.cpp:1587
SOSVector sos2_
SOS2 constraints.
Definition: Problem.h:775
virtual void newVariables(VariableConstIterator v_begin, VariableConstIterator v_end, VarSrcType stype=VarOrig)
Clone the variables pointed by the iterators and add them.
Definition: Problem.cpp:2229
virtual void countObjTypes_()
Count the types of objectives and fill the values in size_.
Definition: Problem.cpp:1562
Problem(EnvPtr env)
Default constructor.
Definition: Problem.cpp:25
virtual void countConsTypes_()
Count the types of constraints and fill the values in size_.
Definition: Problem.cpp:1493
virtual ConstraintPtr newConstraint(FunctionPtr f, double lb, double ub)
Add a new constraint and return a pointer to it. A name is automatically generated by default.
Definition: Problem.cpp:2094
virtual void writeSize(std::ostream &out) const
Write the problem size to logger_.
Definition: Problem.cpp:2569
virtual UInt getNumDCons() const
Return the number of constraints marked for deletion.
Definition: Problem.h:233
UInt getNumSOS2()
Return the number of SOS Type 2 constraints.
Definition: Problem.cpp:1847
virtual DoubleVector * getDebugSol() const
Give a pointer to the debug solution.
Definition: Problem.cpp:1806
void setNativeDer()
Ask Problem to construct its own jacobian and hessian using Minotaur's native code for nonlinear func...
Definition: Problem.cpp:2429
DoubleVector * debugSol_
A solution to be used for debugging against accidentally cutting of feasible points.
Definition: Problem.h:730
HessianOfLagPtr hessian_
Pointer to the hessian of the lagrangean. Could be NULL.
Definition: Problem.h:736
virtual void setEngine(Engine *engine)
Set the engine that is used to solve this problem.
Definition: Problem.cpp:2369
virtual UInt getNumDVars() const
Return the number of variables marked for deletion.
Definition: Problem.h:239
virtual void changeBound(VariablePtr var, BoundType lu, double new_val)
Change a bound (lower or upper) on a variable 'var'.
Definition: Problem.cpp:182
virtual void setVarType(VariablePtr var, VariableType type)
Change the variable type.
Definition: Problem.cpp:2445
UInt numDVars_
Number of variables marked for deletion.
Definition: Problem.h:763
ConstraintVector cons_
Vector of constraints.
Definition: Problem.h:713
Engine * engine_
Engine that must be updated if problem is loaded to it, could be null.
Definition: Problem.h:733
bool consModed_
Flag that is turned on if the constraints are added or modified.
Definition: Problem.h:724
double getSizeEstimate()
Calculate and return a measure of the size of the problem.
Definition: Problem.cpp:2545
virtual void delMarkedVars(bool keep=false)
Definition: Problem.cpp:1664
virtual void reverseSense(ConstraintPtr cons)
Reverse the sense of a constraint.
Definition: Problem.cpp:2355
virtual void negateObj()
The objective is multiplied by -1.
Definition: Problem.cpp:2041
virtual void addToCons(ConstraintPtr cons, double c)
Add 'c' to both lb and ub of a constraint.
Definition: Problem.cpp:121
virtual JacobianPtr getJacobian() const
Return the jacobian. Could be NULL.
Definition: Problem.cpp:1816
virtual void subst(VariablePtr out, VariablePtr in, double rat=1.0)
Substitute a variable 'out' with the variable 'in' through out the problem.
Definition: Problem.cpp:2482
virtual int checkConVars() const
Check whether variables used in the constraints belong to the problem or not.
Definition: Problem.cpp:319
virtual ~Problem()
Destroy.
Definition: Problem.cpp:47
virtual VariablePtr newVariable(VarSrcType stype=VarOrig)
Add a new continuous, unbounded variable to the Problem.
Definition: Problem.cpp:2189
virtual void setDebugSol(const DoubleVector &x)
Set a solution that can be checked for accidental cutting off by cuts, branching, reformulations etc.
Definition: Problem.cpp:2361
virtual VariablePtr getVariable(UInt index) const
Return a pointer to the variable with a given index.
Definition: Problem.cpp:1873
virtual bool isDebugSolFeas(double atol, double rtol)
Returns true if the debug solution is cut off by the constraints of the problem.
Definition: Problem.cpp:1883
virtual bool isQP()
Returns true if the problem has (1) linear or quadratic objective, and (2) linear constraints only.
Definition: Problem.cpp:1998
UInt getNumSOS1()
Return the number of SOS Type 1 constraints.
Definition: Problem.cpp:1842
virtual void changeObj(FunctionPtr f, double cb)
Replace the objective function with a new function.
Definition: Problem.cpp:305
virtual void calculateSize(bool shouldRedo=false)
Fill up the statistics about the size of the problem into size_.
Definition: Problem.cpp:126
virtual UInt getNumJacNnzs() const
Return the number of non zerors in the jacobian of the constraints.
Definition: Problem.cpp:1829
virtual void findVarFunTypes_()
Update the function types of all variables based on whether they appear as linear,...
Definition: Problem.cpp:1723
ProblemPtr shuffle(bool varshuff, bool conshuff, EnvPtr env)
shuffle variables and constraints while making a clone of the problem
Definition: Problem.cpp:1144
LoggerPtr logger_
Pointer to the log manager. All output messages are sent to it.
Definition: Problem.h:742
virtual void setHessian(HessianOfLagPtr hessian)
Add a pointer to the hessian of the Lagrangean.
Definition: Problem.cpp:2377
virtual SOSPtr newSOS(int n, SOSType type, const double *weights, const VarVector &vars, int priority, std::string name)
Add a new SOS constraint with a name.
Definition: Problem.cpp:2159
VarVector varsRem_
Vector of variables removed from the problem but not yet freed from memory.
Definition: Problem.h:784
UInt numDCons_
Number of constraints marked for deletion.
Definition: Problem.h:760
virtual void setInitialPoint(const double *x)
Set an initial point.
Definition: Problem.cpp:2387
virtual HessianOfLagPtr getHessian() const
Return the hessian of the lagrangean. Could be NULL.
Definition: Problem.cpp:1811
virtual void markDelete(ConstraintPtr con)
Mark a constraint for deleting.
Definition: Problem.cpp:2027
UInt nextVId_
ID of the next variable.
Definition: Problem.h:757
ObjectivePtr obj_
Objective, could be NULL.
Definition: Problem.h:766
ProblemSizePtr size_
Size statistics for this Problem.
Definition: Problem.h:769
virtual UInt getNumCons() const
Return the number of constraints.
Definition: Problem.h:227
virtual UInt getNumHessNnzs() const
Return the number of non-zeros in the hessian of the lagrangean of the problem.
Definition: Problem.cpp:1821
bool varsModed_
True if variables delete, added or their bounds changed.
Definition: Problem.h:787
virtual VariableConstIterator varsEnd() const
Iterate over variables.
Definition: Problem.h:662
virtual ProblemType findType()
Return what type of problem it is. May result in re-calculation of the problem size.
Definition: Problem.cpp:1701
SOSVector sos1_
SOS1 constraints.
Definition: Problem.h:772
virtual void delMarkedCons()
Delete marked constraints.
Definition: Problem.cpp:1620
double getObjValue(const double *x, int *err) const
Return the value of objective function at given point x.
Definition: Problem.cpp:1859
virtual VariableConstIterator varsBegin() const
Iterate over variables.
Definition: Problem.h:656
virtual bool hasNativeDer() const
Return true if the derivative is available through Minotaur's own routines for storing nonlinear func...
Definition: Problem.cpp:1878
int nextSId_
ID of the next SOS constraint.
Definition: Problem.h:754
virtual void objToCons()
Definition: Problem.cpp:2242
bool nativeDer_
If true, set up our own Hessian and Jacobian.
Definition: Problem.h:748
virtual ObjectivePtr newObjective(FunctionPtr f, double cb, ObjectiveType otyp)
Add a new objective. A name is automatically generated by default.
Definition: Problem.cpp:2115
void cg2qf()
Convert all quadratic forms that are stored as CGraph into QuadraticFunction. The order of constraint...
Definition: Problem.cpp:1359
virtual bool isMarkedDel(ConstConstraintPtr con)
Return true if a constraint is marked deleted.
Definition: Problem.cpp:1969
virtual ObjectivePtr getObjective() const
Return a pointer to the objective Function.
Definition: Problem.cpp:1852
virtual bool isQuadratic()
Returns true if the problem has only linear or quadratic constraints and linear or quadratic objectiv...
Definition: Problem.cpp:2012
JacobianPtr jacobian_
Pointer to the jacobian of constraints. Can be NULL.
Definition: Problem.h:739
virtual ConstraintConstIterator consEnd() const
Iterate over constraints. Returns the 'end' iterator.
Definition: Problem.h:186
UInt getNumLinCons()
Return the number of linear constraints in the problem.
Definition: Problem.cpp:1837
static const std::string me_
For logging.
Definition: Problem.h:745
virtual void write(std::ostream &out, std::streamsize out_p=6) const
only for debugging, developing etc.
Definition: Problem.cpp:2514
Definition: Variable.h:31
Definition: ActiveNodeStore.h:20
BoundType
Different types of variable-bounds.
Definition: Types.h:131
ObjectiveType
Definition: Types.h:62
VariableType
Different types of variables.
Definition: Types.h:76
ProblemType
The different classes of problems.
Definition: Types.h:44
SOSType
SOS types.
Definition: Types.h:134
VarSrcType
Where did the variable originate from?
Definition: Types.h:85
unsigned int UInt
Unsigned integer.
Definition: Types.h:30
The Problem that needs to be solved.
Definition: Problem.h:51
ProblemSize stores several statistics on the size of the Problem.
Definition: ProblemSize.h:23

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