Minotaur 0.4.1
Docs for developers
Loading...
Searching...
No Matches
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
308 virtual bool isFeasible(const double* x, double objval, double& inf_meas) const;
309
310
311
316 virtual bool isLinear();
317
319 virtual bool isMarkedDel(ConstConstraintPtr con);
320
322 virtual bool isMarkedDel(ConstVariablePtr var);
323
329 virtual bool isQP();
330
336 virtual bool isQuadratic();
337
345 virtual void markDelete(ConstraintPtr con);
346
347 // Helper function for common quadratic checks
348 inline bool checkQuadCond(const ConstraintStats& stats)
349 {
350 return stats.nsqterm == stats.nterms + stats.nvars;
351 }
352
353 // Helper function for coefficient checks
354 inline bool checkCoeffCond(const ConstraintStats& stats,
355 bool positive)
356 {
357 if (positive) {
358 return stats.nposcoefone == stats.nterms + stats.nvars ||
359 (stats.nposcoef == stats.nterms + stats.nvars &&
360 stats.eqwt == stats.nterms + stats.nvars);
361 } else {
362 return stats.nnegcoefone == stats.nterms + stats.nvars ||
363 (stats.nnegcoef == stats.nterms + stats.nvars &&
364 stats.eqwt == stats.nterms + stats.nvars);
365 }
366 }
367
375 virtual void markDelete(VariablePtr var);
376
378 virtual void negateObj();
379
384 virtual VariablePtr newBinaryVariable();
385
391 virtual VariablePtr newBinaryVariable(std::string name);
392
402 virtual ConstraintPtr newConstraint(FunctionPtr f, double lb, double ub);
403
413 virtual ConstraintPtr newConstraint(FunctionPtr f, double lb, double ub,
414 std::string name);
415
425 virtual ObjectivePtr newObjective(FunctionPtr f, double cb,
426 ObjectiveType otyp);
427
428 virtual ObjectivePtr newObjective(double cb, ObjectiveType otyp);
429
441 virtual ObjectivePtr newObjective(FunctionPtr f, double cb,
442 ObjectiveType otyp, std::string name);
443
459 virtual SOSPtr newSOS(int n, SOSType type, const double* weights,
460 const VarVector& vars, int priority,
461 std::string name);
462
476 virtual SOSPtr newSOS(int n, SOSType type, const double* weights,
477 const VarVector& vars, int priority);
478
483 virtual VariablePtr newVariable(VarSrcType stype = VarOrig);
484
494 virtual VariablePtr newVariable(double lb, double ub, VariableType vtype,
495 VarSrcType = VarOrig);
496
506 virtual VariablePtr newVariable(double lb, double ub, VariableType vtype,
507 std::string name, VarSrcType = VarOrig);
508
519 virtual void newVariables(VariableConstIterator v_begin,
520 VariableConstIterator v_end,
521 VarSrcType stype = VarOrig);
522
530 virtual void prepareForSolve();
531
536 virtual void objToCons();
537
539 virtual void removeObjective();
540
542 virtual QuadraticFunctionPtr removeQuadFromObj();
543
544 virtual NonlinearFunctionPtr removeNonlinFromObj();
545
550 virtual void resetDer();
551
552 virtual void resConTypCnt();
553
559 virtual void reverseSense(ConstraintPtr cons);
560
568 virtual void setDebugSol(const DoubleVector& x);
569
578 virtual void setEngine(Engine* engine);
579
585 virtual void setHessian(HessianOfLagPtr hessian);
586
596 virtual void setInitialPoint(const double* x);
597
607 virtual void setInitialPoint(const double* x, size_t k);
608
609 virtual void setInitVal(VariablePtr v, double val);
610 virtual void setInitValByInd(UInt ind, double val);
611
617 virtual void setJacobian(JacobianPtr jacobian);
618
623 void setNativeDer();
624
631 virtual void setVarType(VariablePtr var, VariableType type);
632
633 virtual SOSConstIterator sos1Begin() const
634 {
635 return sos1_.begin();
636 };
637 virtual SOSConstIterator sos1End() const
638 {
639 return sos1_.end();
640 };
641 virtual SOSConstIterator sos2Begin() const
642 {
643 return sos2_.begin();
644 };
645 virtual SOSConstIterator sos2End() const
646 {
647 return sos2_.end();
648 };
649
659 virtual void subst(VariablePtr out, VariablePtr in, double rat = 1.0);
660
662 virtual void unsetEngine();
663
665 virtual VariableConstIterator varsBegin() const
666 {
667 return vars_.begin();
668 }
669
671 virtual VariableConstIterator varsEnd() const
672 {
673 return vars_.end();
674 }
675
677 virtual void write(std::ostream& out, std::streamsize out_p = 6) const;
678
680 virtual void writeSize(std::ostream& out) const;
681
682 //Print statistics in log_level 2
683 void statistics(std::ostream& out) const;
684
685 const double INFTY = std::numeric_limits<double>::infinity();
686
687 protected:
688 //To check type of constraint
689 bool isAggregation_(ConstraintPtr c);
690 bool isPrecedence_(ConstraintPtr c, const ConstraintStats &stats);
691 bool isVariableBound_(ConstraintPtr c, const ConstraintStats &stats);
692 bool isSetPartitioning_(ConstraintPtr c, const ConstraintStats &stats);
693 bool isSetPacking_(ConstraintPtr c, const ConstraintStats &stats);
694 bool isSetCovering_(ConstraintPtr c, const ConstraintStats &stats);
695 bool isCardinality_(ConstraintPtr c, const ConstraintStats &stats);
696 bool isInvariantKnapsack_(ConstraintPtr c, const ConstraintStats &stats);
697 bool isEquationKnapsack_(ConstraintPtr c, const ConstraintStats &stats);
698 bool isBinPacking_(ConstraintPtr c, const ConstraintStats &stats);
699 bool isKnapsack_(ConstraintPtr c, const ConstraintStats &stats);
700 bool isIntegerKnapsack_(ConstraintPtr c, const ConstraintStats &stats);
701 bool isMixedBinary_(ConstraintPtr c, const ConstraintStats &stats);
702 bool isNoSpecificStructure_(ConstraintPtr c);
703 bool isDiagonalQuadratic_(ConstraintPtr c, const ConstraintStats& stats);
704 bool isSimpleBall_(ConstraintPtr c, const ConstraintStats& stats);
705 bool isEllipsoid_(ConstraintPtr c, const ConstraintStats& stats);
706 bool isComplementEllipsoid_(ConstraintPtr c, const ConstraintStats& stats);
707 bool isComplementSimpleBall_(ConstraintPtr c, const ConstraintStats& stats);
708 bool isSocSimple_(ConstraintPtr c, const ConstraintStats& stats);
709 bool isPureBilinear_(ConstraintPtr c, const ConstraintStats& stats);
710 bool isOtherQuadType_(ConstraintPtr c, const ConstraintStats& stats);
711
712 //Print Count table for constraint size
713 void printConstraintStatistics_();
714
715 //Print Count table for Quadratic constraint size
716 void printConstraintStatisticsQuad_();
717
718 // function for lock number
719 void lockNum_();
720
722 ConstraintVector cons_;
723
734
739 DoubleVector* debugSol_;
740
743
746
749
752
754 static const std::string me_;
755
758
761
764
767
770
773
776
779
781 SOSVector sos1_;
782
784 SOSVector sos2_;
785
787 VarVector vars_;
788
793 VarVector varsRem_;
794
797
799 virtual void countConsTypes_();
800
802 virtual void countObjTypes_();
803
805 virtual void countVarTypes_();
806
812 virtual void findVarFunTypes_();
813
814 bool isPolyp_();
815
816 void setIndex_(VariablePtr v, UInt i);
817};
818} // namespace Minotaur
819#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:2546
virtual void changeConstraint(ConstraintPtr con, LinearFunctionPtr lf, double lb, double ub)
Change the linear function, and the bounds of a constraint.
Definition Problem.cpp:274
ConstProblemSizePtr getSize() const
Fill up the statistics about the size of the problem into size_.
Definition Problem.cpp:1869
virtual void prepareForSolve()
Setup problem data-structures for solving it.
Definition Problem.cpp:2400
UInt nextCId_
ID of the next constraint.
Definition Problem.h:760
virtual void addToObj(LinearFunctionPtr lf)
Add a linear function to the objective.
Definition Problem.cpp:98
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:152
virtual ConstraintPtr getConstraint(UInt index) const
Return a pointer to the constraint with a given index.
Definition Problem.cpp:1802
virtual void unsetEngine()
Should be called in the Engine's destructor.
Definition Problem.cpp:2631
virtual bool isLinear()
Returns true if the problem has only linear constraints and linear objectives.
Definition Problem.cpp:2080
VarVector vars_
Vector of variables.
Definition Problem.h:787
virtual void resetDer()
Definition Problem.cpp:2446
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:2424
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:2176
virtual void removeObjective()
Remove objective from the Problem.
Definition Problem.cpp:2413
ProblemPtr clone(EnvPtr env) const
Clone the given Problem class. Jacobian and Hessian in the cloned problem are NULL.
Definition Problem.cpp:1035
virtual void countVarTypes_()
Fill up the size_ with number of variables of each type.
Definition Problem.cpp:1588
SOSVector sos2_
SOS2 constraints.
Definition Problem.h:784
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:2351
virtual void countObjTypes_()
Count the types of objectives and fill the values in size_.
Definition Problem.cpp:1563
virtual void countConsTypes_()
Count the types of constraints and fill the values in size_.
Definition Problem.cpp:1494
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:2216
virtual void writeSize(std::ostream &out) const
Write the problem size to logger_.
Definition Problem.cpp:2691
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:1848
virtual DoubleVector * getDebugSol() const
Give a pointer to the debug solution.
Definition Problem.cpp:1807
void setNativeDer()
Ask Problem to construct its own jacobian and hessian using Minotaur's native code for nonlinear func...
Definition Problem.cpp:2551
virtual bool isFeasible(const double *x, double objval, double &inf_meas) const
Check feasibility of original constraints and objective. Evaluates all constraints and the objective ...
Definition Problem.cpp:1960
DoubleVector * debugSol_
A solution to be used for debugging against accidentally cutting of feasible points.
Definition Problem.h:739
HessianOfLagPtr hessian_
Pointer to the hessian of the lagrangean. Could be NULL.
Definition Problem.h:745
virtual void setEngine(Engine *engine)
Set the engine that is used to solve this problem.
Definition Problem.cpp:2491
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:183
virtual void setVarType(VariablePtr var, VariableType type)
Change the variable type.
Definition Problem.cpp:2567
UInt numDVars_
Number of variables marked for deletion.
Definition Problem.h:772
ConstraintVector cons_
Vector of constraints.
Definition Problem.h:722
Engine * engine_
Engine that must be updated if problem is loaded to it, could be null.
Definition Problem.h:742
bool consModed_
Flag that is turned on if the constraints are added or modified.
Definition Problem.h:733
double getSizeEstimate()
Calculate and return a measure of the size of the problem.
Definition Problem.cpp:2667
virtual void delMarkedVars(bool keep=false)
Definition Problem.cpp:1665
virtual void reverseSense(ConstraintPtr cons)
Reverse the sense of a constraint.
Definition Problem.cpp:2477
virtual void negateObj()
The objective is multiplied by -1.
Definition Problem.cpp:2163
virtual void addToCons(ConstraintPtr cons, double c)
Add 'c' to both lb and ub of a constraint.
Definition Problem.cpp:122
virtual JacobianPtr getJacobian() const
Return the jacobian. Could be NULL.
Definition Problem.cpp:1817
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:2604
virtual int checkConVars() const
Check whether variables used in the constraints belong to the problem or not.
Definition Problem.cpp:320
virtual ~Problem()
Destroy.
Definition Problem.cpp:48
virtual VariablePtr newVariable(VarSrcType stype=VarOrig)
Add a new continuous, unbounded variable to the Problem.
Definition Problem.cpp:2311
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:2483
virtual VariablePtr getVariable(UInt index) const
Return a pointer to the variable with a given index.
Definition Problem.cpp:1874
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:1884
virtual bool isQP()
Returns true if the problem has (1) linear or quadratic objective, and (2) linear constraints only.
Definition Problem.cpp:2120
UInt getNumSOS1()
Return the number of SOS Type 1 constraints.
Definition Problem.cpp:1843
virtual void changeObj(FunctionPtr f, double cb)
Replace the objective function with a new function.
Definition Problem.cpp:306
virtual void calculateSize(bool shouldRedo=false)
Fill up the statistics about the size of the problem into size_.
Definition Problem.cpp:127
virtual UInt getNumJacNnzs() const
Return the number of non zerors in the jacobian of the constraints.
Definition Problem.cpp:1830
virtual void findVarFunTypes_()
Update the function types of all variables based on whether they appear as linear,...
Definition Problem.cpp:1724
ProblemPtr shuffle(bool varshuff, bool conshuff, EnvPtr env)
shuffle variables and constraints while making a clone of the problem
Definition Problem.cpp:1145
LoggerPtr logger_
Pointer to the log manager. All output messages are sent to it.
Definition Problem.h:751
virtual void setHessian(HessianOfLagPtr hessian)
Add a pointer to the hessian of the Lagrangean.
Definition Problem.cpp:2499
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:2281
VarVector varsRem_
Vector of variables removed from the problem but not yet freed from memory.
Definition Problem.h:793
UInt numDCons_
Number of constraints marked for deletion.
Definition Problem.h:769
virtual void setInitialPoint(const double *x)
Set an initial point.
Definition Problem.cpp:2509
virtual HessianOfLagPtr getHessian() const
Return the hessian of the lagrangean. Could be NULL.
Definition Problem.cpp:1812
virtual void markDelete(ConstraintPtr con)
Mark a constraint for deleting.
Definition Problem.cpp:2149
UInt nextVId_
ID of the next variable.
Definition Problem.h:766
ObjectivePtr obj_
Objective, could be NULL.
Definition Problem.h:775
ProblemSizePtr size_
Size statistics for this Problem.
Definition Problem.h:778
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:1822
bool varsModed_
True if variables delete, added or their bounds changed.
Definition Problem.h:796
virtual VariableConstIterator varsEnd() const
Iterate over variables.
Definition Problem.h:671
virtual ProblemType findType()
Return what type of problem it is. May result in re-calculation of the problem size.
Definition Problem.cpp:1702
SOSVector sos1_
SOS1 constraints.
Definition Problem.h:781
virtual void delMarkedCons()
Delete marked constraints.
Definition Problem.cpp:1621
double getObjValue(const double *x, int *err) const
Return the value of objective function at given point x.
Definition Problem.cpp:1860
virtual VariableConstIterator varsBegin() const
Iterate over variables.
Definition Problem.h:665
virtual bool hasNativeDer() const
Return true if the derivative is available through Minotaur's own routines for storing nonlinear func...
Definition Problem.cpp:1879
int nextSId_
ID of the next SOS constraint.
Definition Problem.h:763
virtual void objToCons()
Definition Problem.cpp:2364
bool nativeDer_
If true, set up our own Hessian and Jacobian.
Definition Problem.h:757
virtual ObjectivePtr newObjective(FunctionPtr f, double cb, ObjectiveType otyp)
Add a new objective. A name is automatically generated by default.
Definition Problem.cpp:2237
void cg2qf()
Convert all quadratic forms that are stored as CGraph into QuadraticFunction. The order of constraint...
Definition Problem.cpp:1360
virtual bool isMarkedDel(ConstConstraintPtr con)
Return true if a constraint is marked deleted.
Definition Problem.cpp:2091
virtual ObjectivePtr getObjective() const
Return a pointer to the objective Function.
Definition Problem.cpp:1853
virtual bool isQuadratic()
Returns true if the problem has only linear or quadratic constraints and linear or quadratic objectiv...
Definition Problem.cpp:2134
JacobianPtr jacobian_
Pointer to the jacobian of constraints. Can be NULL.
Definition Problem.h:748
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:1838
static const std::string me_
For logging.
Definition Problem.h:754
virtual void write(std::ostream &out, std::streamsize out_p=6) const
only for debugging, developing etc.
Definition Problem.cpp:2636
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.8 on Tue Apr 7 2026