Minotaur 0.4.1
Docs for developers
ParMINLPDiving.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
16#ifndef MINOTAURPARMINLPDIVING_H
17#define MINOTAURPARMINLPDIVING_H
18
19#include "Heuristic.h"
20#include <vector>
21#include <stack>
22#include <sys/time.h>
23
24
25namespace Minotaur {
26 class LinearHandler;
27 class Solution;
28 class Timer;
29 class VarBoundMod;
30 typedef const Solution* ConstSolutionPtr;
31 typedef VarBoundMod* VarBoundModPtr;
32
34 typedef enum {
35 Floor,
36 Ceil,
37 Nearest,
39 } Direction;
40
42 typedef enum {
43 Least,
44 Most
45 } Order;
46
48 typedef enum {
49 Fractional,
51 LexBound,
53 } Scoretype;
54
58 struct DivingheurStats {
59 UInt numNLPs[4];
60 UInt numInfeas[4];
61 UInt errors[4];
62 UInt numSol[4];
63 double time[4];
64 UInt iterations[4];
68 double best_obj_value;
69 double totalTime;
70 };
71
81 class ParMINLPDiving : public Heuristic {
82 public:
83
86
89
91 void solve(NodePtr node, RelaxationPtr rel, SolutionPoolPtr s_pool);
92
94 void writeStats(std::ostream &out) const;
95
97 void writeParStats(std::ostream &out, DivingheurStats *stats,
98 double wallTime) const;
99
101 virtual std::string getDirectionString(UInt i);
102
104 virtual std::string getScoreString(UInt i);
105
107 virtual std::string getOrderString(UInt i);
108
110 void setAltEngine(EnginePtr nlpe) { nlpe_ = nlpe; }
111
113 void setOrigProb(ProblemPtr minlp) { minlp_ = minlp; }
114
116 void solveNLP(ConstSolutionPtr sol, bool *solFound,
117 ProblemPtr minlp, EnginePtr nlpe);
118
120 void fixInts(const double *x,
121 std::stack<Modification *> &nlpMods,
122 ProblemPtr minlp);
123
124 //Unfix integer variables
125 void unfixInts(std::stack<Modification *> &nlpMods,
126 ProblemPtr minlp);
127
129 double getWallTime() {
130 struct timeval time;
131 if (gettimeofday(&time,NULL)) {
132 // Handle error
133 return 0;
134 }
135 return (double)time.tv_sec + (double)time.tv_usec * .000001;
136 }
137
138 protected:
139
140 const static std::string me_;
141
144 //DoubleVector avgDual_;
145
148
151
154
156 //double* gradientObj_;
157
160 double intTol_;
161
164 //ModVector lastNodeMods_;
165
167 //LinearHandler *lh_;
168
171
174
177
182 //std::stack<VarBoundModPtr> mods_;
183
186
189
192
195
197 //DoubleVector score_;
198
201
204
206 //UIntVector violated_;
207
210
213
214 typedef UInt (ParMINLPDiving::*FuncPtr) (UInt numfrac, const double* x,
215 Direction d, Order o,
216 ProblemPtr p,
217 UIntVector& violated,
218 std::stack<VarBoundModPtr>& mods,
219 LinearHandler* lh,
220 ModVector& lastNodeMods,
221 DoubleVector& score,
222 DoubleVector& avgDual,
223 double* gradientObj);
224
234 //void backtrack_(UInt n_flipped);
235 void backtrack_(UInt n_moded, ModVector& lastNodeMods, ProblemPtr p,
236 std::stack<VarBoundModPtr>& mods);
246 //UInt FracBounds_(UInt numfrac, const double* x,
247 //Direction d, Order o);
248
249
250 UInt FracBounds_(UInt numfrac, const double* x, Direction d, Order o,
251 ProblemPtr p, UIntVector& violated,
252 std::stack<VarBoundModPtr>& mods, LinearHandler* lh,
253 ModVector& lastNodesMods, DoubleVector& score,
254 DoubleVector& avgDual, double* gradientObj);
264 //void getScore_(const double* x, Scoretype s);
265
266 void getScore_(const double* x, Scoretype s, DoubleVector& score,
267 ProblemPtr p, DoubleVector& avgDual,
268 double* gradientObj);
280 //void implementDive_(int i, const double*x, SolutionPoolPtr s_pool);
281 void implementDive_(int i, const double* x, SolutionPoolPtr s_pool,
282 ModVector& lastNodesMods, EnginePtr e, ProblemPtr p,
283 DoubleVector& avgDual, UIntVector& violated,
284 std::stack<VarBoundModPtr>& mods, LinearHandler* lh,
285 DoubleVector& score, double* gradientObj,
286 DivingheurStats* stats);
294 //UInt isFrac_(const double* x);
295 UInt isFrac_(const double* x, UIntVector& violated, ProblemPtr p);
296
306 UInt LexBounds_(UInt numfrac, const double* x, Direction d, Order o,
307 ProblemPtr p, UIntVector& violated,
308 std::stack<VarBoundModPtr>& mods, LinearHandler* lh,
309 ModVector& lastNodesMods, DoubleVector& score,
310 DoubleVector& avgDual, double* gradientObj);
311
321 UInt ReducedCost_(UInt numfrac, const double* x, Direction d, Order o,
322 ProblemPtr p, UIntVector& violated,
323 std::stack<VarBoundModPtr>& mods, LinearHandler* lh,
324 ModVector& lastNodesMods, DoubleVector& score,
325 DoubleVector& avgDual, double* gradientObj);
333 void restoreBounds_(double* LB_copy, double* UB_copy, UInt vars,
334 ProblemPtr p);
335
344 double rounding_(double value, Direction d);
345
353 void saveBounds_(double* LB_copy, double* UB_copy, UInt vars);
354
364 FuncPtr selectHeur_(int i, Direction &d, Order &o);
365
374 bool shouldDive_();
375
385 void sort_(UInt left, UInt right, DoubleVector& score,
386 UIntVector& violated);
387
393 void updateAvgDual_(ConstSolutionPtr sol, DoubleVector& avgDual,
394 DivingheurStats* stats);
395
405 UInt VectorLength_(UInt numfrac, const double* x, Direction d, Order o,
406 ProblemPtr p, UIntVector& violated,
407 std::stack<VarBoundModPtr>& mods, LinearHandler* lh,
408 ModVector& lastNodesMods, DoubleVector& score,
409 DoubleVector& avgDual, double* gradientObj);
410
412 bool vectorFlag_(UInt min_vlength, ProblemPtr p);
413
414 };
415
417}
418#endif
419
Define abstract base class for heuristics of various kinds.
Definition: Engine.h:34
Definition: Environment.h:28
Definition: Heuristic.h:30
Definition: LinearHandler.h:60
Definition: Logger.h:37
Definition: Node.h:54
Parallel Diving heuristic for MINLPs.
Definition: ParMINLPDiving.h:81
UInt VectorLength_(UInt numfrac, const double *x, Direction d, Order o, ProblemPtr p, UIntVector &violated, std::stack< VarBoundModPtr > &mods, LinearHandler *lh, ModVector &lastNodesMods, DoubleVector &score, DoubleVector &avgDual, double *gradientObj)
Vector Length selection method for fractional variable.
Definition: ParMINLPDiving.cpp:1105
UInt numLevels_
Number of subsets to fix the fractional variables in each round.
Definition: ParMINLPDiving.h:212
UInt ReducedCost_(UInt numfrac, const double *x, Direction d, Order o, ProblemPtr p, UIntVector &violated, std::stack< VarBoundModPtr > &mods, LinearHandler *lh, ModVector &lastNodesMods, DoubleVector &score, DoubleVector &avgDual, double *gradientObj)
Reduced cost diving selection method for fractional variable.
Definition: ParMINLPDiving.cpp:647
double intTol_
Gradient of objective function for vector length diving.
Definition: ParMINLPDiving.h:160
void updateAvgDual_(ConstSolutionPtr sol, DoubleVector &avgDual, DivingheurStats *stats)
Update the average of dual multiplier.
Definition: ParMINLPDiving.cpp:1092
EnginePtr e_
Engine being used to solve the problems during dive.
Definition: ParMINLPDiving.h:147
DivingheurStats * stats_
Violated variable fraction part list.
Definition: ParMINLPDiving.h:200
FuncPtr selectHeur_(int i, Direction &d, Order &o)
Select the method, ordering and direction.
Definition: ParMINLPDiving.cpp:779
double getWallTime()
Get wall clock time.
Definition: ParMINLPDiving.h:129
UInt LexBounds_(UInt numfrac, const double *x, Direction d, Order o, ProblemPtr p, UIntVector &violated, std::stack< VarBoundModPtr > &mods, LinearHandler *lh, ModVector &lastNodesMods, DoubleVector &score, DoubleVector &avgDual, double *gradientObj)
Lexicographic selection method for fractional variable.
Definition: ParMINLPDiving.cpp:560
void sort_(UInt left, UInt right, DoubleVector &score, UIntVector &violated)
Sort the score.
Definition: ParMINLPDiving.cpp:1059
bool vectorFlag_(UInt min_vlength, ProblemPtr p)
Function to decide on vector length diving.
Definition: ParMINLPDiving.cpp:1191
Timer * timer_
Timer for this heuristic.
Definition: ParMINLPDiving.h:203
void writeStats(std::ostream &out) const
writing the statistics to the logger
Definition: ParMINLPDiving.cpp:1257
void writeParStats(std::ostream &out, DivingheurStats *stats, double wallTime) const
writing the statistics to the logger
Definition: ParMINLPDiving.cpp:1215
ParMINLPDiving(EnvPtr env, ProblemPtr p, EnginePtr e)
default constructor
Definition: ParMINLPDiving.cpp:46
void setOrigProb(ProblemPtr minlp)
Set the original problem pointer.
Definition: ParMINLPDiving.h:113
void setAltEngine(EnginePtr nlpe)
Set the alternate (typically NLP) engine pointer.
Definition: ParMINLPDiving.h:110
void restoreBounds_(double *LB_copy, double *UB_copy, UInt vars, ProblemPtr p)
Restore the bounds for the problem.
Definition: ParMINLPDiving.cpp:732
EnginePtr nlpe_
NLP Engine (required only if e_ is LP Engine)
Definition: ParMINLPDiving.h:150
~ParMINLPDiving()
default destructor
Definition: ParMINLPDiving.cpp:95
UInt numThreads_
Number of threads to be used for the heuristic.
Definition: ParMINLPDiving.h:188
UInt isFrac_(const double *x, UIntVector &violated, ProblemPtr p)
The number of fractional variables in current solution.
Definition: ParMINLPDiving.cpp:468
bool shouldDive_()
Function to decide on diving or not.
Definition: ParMINLPDiving.cpp:823
UInt maxSol_
Maximum number of feasible solution required from heuristic.
Definition: ParMINLPDiving.h:176
ProblemPtr minlp_
Original problem (required only if p_ is LP relaxation)
Definition: ParMINLPDiving.h:191
double wallTimeStart_
violated variable index list
Definition: ParMINLPDiving.h:209
virtual std::string getScoreString(UInt i)
Return a string that describes the scoring rule in simple words.
Definition: ParMINLPDiving.cpp:1314
void solve(NodePtr node, RelaxationPtr rel, SolutionPoolPtr s_pool)
call to heuristic
Definition: ParMINLPDiving.cpp:851
void fixInts(const double *x, std::stack< Modification * > &nlpMods, ProblemPtr minlp)
Fix integer variables as in x.
Definition: ParMINLPDiving.cpp:526
EnvPtr env_
Environment.
Definition: ParMINLPDiving.h:153
void solveNLP(ConstSolutionPtr sol, bool *solFound, ProblemPtr minlp, EnginePtr nlpe)
Solve NLP at LP integer feasible solution.
Definition: ParMINLPDiving.cpp:491
virtual std::string getDirectionString(UInt i)
Return a string that describes the rounding direction in simple words.
Definition: ParMINLPDiving.cpp:1287
ProblemPtr p_
Problem to be solved.
Definition: ParMINLPDiving.h:194
void getScore_(const double *x, Scoretype s, DoubleVector &score, ProblemPtr p, DoubleVector &avgDual, double *gradientObj)
Get the score of solution.
Definition: ParMINLPDiving.cpp:241
UInt nSelector_
Number of method for selection of variables.
Definition: ParMINLPDiving.h:185
LoggerPtr logger_
Linear Handler for presolving.
Definition: ParMINLPDiving.h:170
void saveBounds_(double *LB_copy, double *UB_copy, UInt vars)
Save bounds of the problem.
Definition: ParMINLPDiving.cpp:768
double rounding_(double value, Direction d)
Rounding a value in a given direction.
Definition: ParMINLPDiving.cpp:741
virtual std::string getOrderString(UInt i)
Return a string that describes the rounding order in simple words.
Definition: ParMINLPDiving.cpp:1303
void implementDive_(int i, const double *x, SolutionPoolPtr s_pool, ModVector &lastNodesMods, EnginePtr e, ProblemPtr p, DoubleVector &avgDual, UIntVector &violated, std::stack< VarBoundModPtr > &mods, LinearHandler *lh, DoubleVector &score, double *gradientObj, DivingheurStats *stats)
Function to implement diving.
Definition: ParMINLPDiving.cpp:325
UInt maxProbs_
Maximum number of problem-solves allowed for each thread.
Definition: ParMINLPDiving.h:173
UInt FracBounds_(UInt numfrac, const double *x, Direction d, Order o, ProblemPtr p, UIntVector &violated, std::stack< VarBoundModPtr > &mods, LinearHandler *lh, ModVector &lastNodesMods, DoubleVector &score, DoubleVector &avgDual, double *gradientObj)
Fractional selection method for fractional variable.
Definition: ParMINLPDiving.cpp:156
void backtrack_(UInt n_moded, ModVector &lastNodeMods, ProblemPtr p, std::stack< VarBoundModPtr > &mods)
Backtracking method.
Definition: ParMINLPDiving.cpp:110
Definition: Problem.h:74
Definition: Relaxation.h:53
Definition: SolutionPool.h:28
Definition: Solution.h:30
Definition: Timer.h:40
Definition: ActiveNodeStore.h:20
Order
Order of rounding: least fractional or most fractional.
Definition: MINLPDiving.h:42
@ Most
Select the variable with Least fractional part.
Definition: MINLPDiving.h:44
Scoretype
Type of score evaluation for fractional variable.
Definition: MINLPDiving.h:48
@ ReducedCost
Score of variable is the index of the variable.
Definition: MINLPDiving.h:52
@ LexBound
Score of variable is the Vector Length.
Definition: MINLPDiving.h:51
@ VectorLength
Score of variable is the fractional part`.
Definition: MINLPDiving.h:50
Direction
Direction of rounding.
Definition: MINLPDiving.h:34
@ Ceil
Round to the smallest integer.
Definition: MINLPDiving.h:36
@ Farthest
Round to the nearest integer.
Definition: MINLPDiving.h:38
@ Nearest
Round to the largest integer.
Definition: MINLPDiving.h:37
unsigned int UInt
Unsigned integer.
Definition: Types.h:30
A statistic struct for MINLP Diving heuristic.
Definition: MINLPDiving.h:58
UInt totalSol
NLPs solved.
Definition: MINLPDiving.h:66
double time[4]
Solutions obtained.
Definition: MINLPDiving.h:63
UInt iterations[4]
Time for each selection method.
Definition: MINLPDiving.h:64
UInt numSol[4]
NLPs that encountered errors.
Definition: MINLPDiving.h:62
UInt totalProbs
Iterations.
Definition: ParMINLPDiving.h:65
UInt errors[4]
Infeasible NLPs solved.
Definition: MINLPDiving.h:61
double best_obj_value
Local optimal solutions obtained.
Definition: MINLPDiving.h:68
UInt numInfeas[4]
NLPs solved in each selection method.
Definition: MINLPDiving.h:60
UInt numLocal
Solutions found.
Definition: MINLPDiving.h:67
double totalTime
Best objective value of feasible solution.
Definition: MINLPDiving.h:69

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