Minotaur 0.4.1
Docs for developers
LGCIGenerator.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
7
14#ifndef MINOTAURLGCIGENERATOR_H
15#define MINOTAURLGCIGENERATOR_H
16
17#include <map>
18#include <fstream>
19using std::ofstream;
20#include <string>
21using std::string;
22
23
24#include "KnapsackList.h"
25#include "Problem.h"
26#include "Solution.h"
27#include "Types.h"
28#include "Cut.h"
29#include "Relaxation.h"
30#include "Environment.h"
31#include "ProbStructure.h"
32#include "LPEngine.h"
33
34namespace Minotaur {
35
36typedef enum {
37 Cover = 0,
38 Cons,
39 Obj,
40 Set
41} PrintType;
42
43typedef enum {
44 Totalcuts = 0,
45 Cuts,
46 Violated,
47 Gns,
48 Noviol,
49 Noinitcover
50} LGCIType;
51
52// Shows why cut is not added to the list.
53typedef enum {
54 Duplicate = 0,
55 NotViolated
56}CutFail;
57
59{
60 UInt liftsubprobs;
64 UInt knapcons; // Number of knapsack constraints.
65 UInt knapcov;
73 double time;
74};
75
76// Typedefs for ease of coding.
77class LGCIGenerator;
81typedef LGCIGenStats const * ConstLGCIGenStatsPtr;
83typedef std::map<ConstVariablePtr, ConstVariablePtr> OrigLiftVars;
84typedef OrigLiftVars* OrigLiftVarsPtr;
85
96public:
97 // Default constructor.
99
100 // Constructor that uses a problem and a solution.
102 EnvPtr env, LPEnginePtr lpengine);
103
104 // Constructor that uses a relaxation and a solution given.
106 EnvPtr env, LPEnginePtr lpengine);
107
108 // Destructor.
110
111 // Initialize data elements.
112 void initialize();
113
114 // Initialize statistics.
115 void initStats();
116
117 // Checks if the constraint has a cover set.
118 bool hasCover(ConstraintConstIterator it);
119
120 // Checks if there is a set of GUBs that covers the knapsack inequality.
121 bool hasGubCover(ConstraintConstIterator it,
122 ConstConstraintVectorPtr gublist);
123
124 // Generates initial cover by using GNS algorithm.
126 CoverSetPtr cover);
127
128 /* Modified GNS cover set generator such that it always generates a cover.
129 * This function generates a cover set by using different strategies.
130 * For now, it generates a modified version of Gu, Nemhauser, Savelsbergh
131 * such that it generates a cover set even though the number of nonzero
132 * elements in solution vector given is not enough for cover set generation
133 * s.t sum(a_i) <= b for i s.t. x^*_i != 0.
134 */
135 bool coverSetGenGNSModified(ConstConstraintPtr cons);
136
140 void varCoeff(ConstConstraintPtr cons, CoverSetPtr cover);
141
145 double sumCoeffs(CoverSetPtr cover);
146
150 void minimalCover(CoverSetPtr cover, ConstConstraintPtr cons);
151
152 // Generates all LGCI cover cuts from all constraints.
153 void generateAllCuts();
154
155 // Generate cover partitions C1, C2, F and R
157 const ConstCoverSetPtr cover,
158 CoverSetPtr cone,
159 CoverSetPtr ctwo,
160 CoverSetPtr fset,
161 CoverSetPtr rset);
162
163 // Lifts a variable up or down as it is specified by uplift.
164 double lift(const ConstCoverSetPtr obj,
165 const ConstCoverSetPtr inequality,
166 const CoverSetConstIterator variable,
167 double & rhs,
168 double & initialb,
169 bool uplift);
170
171 // Initialize cover inequality by changing the coefficients of cover set
172 // by ones.
173 void initCoverIneq(const ConstCoverSetPtr cover, CoverSetPtr coverineq);
174
179 bool liftingGNS(const ConstCoverSetPtr cone,
180 const ConstCoverSetPtr ctwo,
181 const ConstCoverSetPtr fset,
182 const ConstCoverSetPtr rset,
183 CoverSetPtr constraint,
184 const ConstConstraintPtr cons,
185 ConstConstraintVectorPtr gublist,
186 double & ub);
187
188
191 bool liftSet(CoverSetPtr obj,
192 CoverSetPtr constraintset,
193 const ConstCoverSetPtr varset,
194 CoverSetPtr inequality,
195 double & ub,
196 double & initialb,
197 bool liftup);
198
199 // Generates all the cover cuts from a given knapsack constraint.
200 void generateCuts(ConstConstraintPtr cons, ConstConstraintVectorPtr gublist);
201
202 // Generate a cut from a given variable set and rhs.
203 CutPtr generateCut(const ConstCoverSetPtr inequality, double ub);
204
205 // Generates a GNS LGCI cut.
206 bool GNS(const ConstConstraintPtr cons, ConstConstraintVectorPtr gublist);
207
208 // Add the cut to the list and insert corresponding violation to violation
209 // list.
210 void addCut(CutPtr cut);
211
212 bool addCut(CoverSetPtr cov, double rhs, UInt cuttype, CutFail& failtype);
213
214 // Check if the same cut is already included in the list.
215 bool checkExists(CoverSetPtr inequality, double rhs);
216
217 // Solves the lifting problem.
218 double liftingProbsolver();
219
220 // Calculates the violation for the given cut.
221 double violation(CutPtr cut);
222
223 // Constructs a vector of nonzero variables in the given solution.
224 void nonzeroVars(const ConstLinearFunctionPtr lf,
225 CoverSetPtr nonzerovars,
226 CoverSetPtr zerovars);
227
228 // Sort nonzero variab;e array in nonincreasing order.
229 void sortNonIncreasing(CoverSetPtr nonzeros);
230
231 // This function generates set N\C, the variables outside of cover set.
232 void cBar(const ConstCoverSetPtr cover,
233 CoverSetPtr cbar,
234 const ConstConstraintPtr cons);
235
236 // Generate GUBs from constraints in map data type.
237 void generateGubMaps(ConstConstraintVectorPtr gublist,
238 std::vector<CoverSetPtr>* gubcoverlist,
239 std::vector<CoverSetPtr>* guborigcoeffs);
240
241 // Generate non overlapping GUB constraint set.
242 void nonOverlap(std::vector<CoverSetPtr> * gubcoverlist);
243
244 // Initialize the GUB constraints in lifting problem.
245 void initGubCons(const ConstCoverSetPtr cone,
246 std::vector<CoverSetPtr> * gubcoverlist,
247 std::vector<CoverSetPtr> * gubcons);
248
249 // Lift elements in the set.
250 void liftSet(CoverSetPtr obj,
251 std::vector<CoverSetPtr>* origgubs,
252 CoverSetPtr consknap,
253 std::vector<CoverSetPtr> * gubcons,
254 const ConstCoverSetPtr varset,
255 CoverSetPtr coverineq,
256 double & rhs,
257 double & initialknap,
258 double * initialgub,
259 bool liftup);
260
261 // Lift the current variable.
262 double lift(CoverSetPtr obj,
263 std::vector<CoverSetPtr> * origgubs,
264 CoverSetPtr consknap,
265 std::vector<CoverSetPtr> * gubcons,
266 const CoverSetConstIterator variable,
267 double & rhs,
268 double & initialbknap,
269 double * initialgub,
270 bool liftup);
271
272 // Return const GUB identifier.
273 ConstProbStructPtr getProbStruct() const
274 {return probstruct_;}
275
276 // Return const knapsack list.
277 ConstKnapsackListPtr getKnapsackList() const
278 {return ConstKnapsackListPtr(knapList_);}
279
280 // Return statistics of LGCIGenerator.
281 ConstLGCIGenStatsPtr getStats() const
282 {return ConstLGCIGenStatsPtr(stats_);}
283
284 // Print inequality.
285 void printIneq(const ConstCoverSetPtr cov, double rhs,
286 PrintType type, string message);
287
288
289 void addCons(CoverSetPtr obj,
290 CoverSetPtr consknap,
291 double bknap,
292 std::vector<CoverSetPtr> * gubcons,
293 double * bgubs,
294 OrigLiftVarsPtr varmap,
295 ProblemPtr liftprob);
296
297 VariablePtr addVar(VariablePtr var, OrigLiftVarsPtr varmap, ProblemPtr liftprob);
298
299
300 double roundHeur(ProblemPtr prob);
301
302 // Return const pointer for problem.
303
304private:
305 // Environment.
306 EnvPtr env_;
307 // Problem that cover cuts will be generated for.
308 ProblemPtr p_;
309 // List of cuts generated.
310 CutVector cutVec_;
311 // List of violated cut.
312 CutVector violatedCuts_;
313 // List of violations for cuts corresponding in cut list.
314 DoubleVector violList_;
315 // List of knapsack inequalities.
316 KnapsackListPtr knapList_;
321 SolutionPtr s_;
322 // Problem structure to obtaib GUB list for variables in knapsack inequality.
323 ConstProbStructPtr probstruct_;
324 // Number of knapsack ineqs used for cut generation.
325 UInt numCons_;
326 // Statistics for LGCI generator.
327 LGCIGenStatsPtr stats_;
328 // Hash map that is used to check if a cut is already created or not.
329 std::map< std::vector<double>, UInt> cutmap_;
330 // Integer tolerance.
331 double intTol_;
332
333 // Output file
334 ofstream output_;
335 // Output file name
336 string outfile_;
337 // LP solver.
338 LPEnginePtr lpengine_;
339
340};
341
342}
343
344
345
346
347#endif // MINOTAURLGCIGENERATOR_H
348
349
350
351
352
353
Declare the Cut class of valid inequalities.
Define the Environment class.
Declare base class KnapsackList.
Declare the class LPEngine for solving LPs and getting solution.
Declare base class ProbStructure.
Declare the base class Modification.
Declare the class Relaxation for storing and manipulating relaxations.
Implement base class Solution.
Declare important 'types' used in Minotaur.
The Constraint class is used to manage a constraint.
Definition: Constraint.h:61
Store function, bounds and other information about a cut.
Definition: Cut.h:52
Definition: Environment.h:28
Definition: KnapsackList.h:25
Definition: LGCIGenerator.h:95
void minimalCover(CoverSetPtr cover, ConstConstraintPtr cons)
Definition: LGCIGenerator.cpp:1528
bool liftSet(CoverSetPtr obj, CoverSetPtr constraintset, const ConstCoverSetPtr varset, CoverSetPtr inequality, double &ub, double &initialb, bool liftup)
bool checkExists(CoverSetPtr inequality, double rhs)
Definition: LGCIGenerator.cpp:1713
bool liftingGNS(const ConstCoverSetPtr cone, const ConstCoverSetPtr ctwo, const ConstCoverSetPtr fset, const ConstCoverSetPtr rset, CoverSetPtr constraint, const ConstConstraintPtr cons, ConstConstraintVectorPtr gublist, double &ub)
Definition: LGCIGenerator.cpp:610
void cBar(const ConstCoverSetPtr cover, CoverSetPtr cbar, const ConstConstraintPtr cons)
Definition: LGCIGenerator.cpp:1479
void addCons(CoverSetPtr obj, CoverSetPtr consknap, double bknap, std::vector< CoverSetPtr > *gubcons, double *bgubs, OrigLiftVarsPtr varmap, ProblemPtr liftprob)
Definition: LGCIGenerator.cpp:926
void varCoeff(ConstConstraintPtr cons, CoverSetPtr cover)
Definition: LGCIGenerator.cpp:1583
VariablePtr addVar(VariablePtr var, OrigLiftVarsPtr varmap, ProblemPtr liftprob)
Definition: LGCIGenerator.cpp:1016
bool coverSetGeneratorGNS(ConstConstraintPtr cons, CoverSetPtr cover)
Definition: LGCIGenerator.cpp:408
void coverPartitionGNS(const ConstConstraintPtr cons, const ConstCoverSetPtr cover, CoverSetPtr cone, CoverSetPtr ctwo, CoverSetPtr fset, CoverSetPtr rset)
Definition: LGCIGenerator.cpp:516
void sortNonIncreasing(CoverSetPtr nonzeros)
Definition: LGCIGenerator.cpp:501
double sumCoeffs(CoverSetPtr cover)
Definition: LGCIGenerator.cpp:1565
Definition: LPEngine.h:29
The base class linear function is of the form c'x.
Definition: LinearFunction.h:31
Definition: ProbStructure.h:53
Definition: Problem.h:74
Definition: Relaxation.h:53
Definition: Solution.h:30
Definition: ActiveNodeStore.h:20
unsigned int UInt
Unsigned integer.
Definition: Types.h:30
Definition: LGCIGenerator.h:59
UInt gns
Definition: LGCIGenerator.h:69
UInt noinitcov
Definition: LGCIGenerator.h:72
UInt cuts
Number of all cuts i.e. including duplicates as well.
Definition: LGCIGenerator.h:62
UInt totalcuts
Number of total subproblems solved for lifting.
Definition: LGCIGenerator.h:61
UInt knapcons
Number of violated cuts.
Definition: LGCIGenerator.h:64
UInt knapgub
Definition: LGCIGenerator.h:67
double time
GNS procedure terminated since no inital GNS cover constructed.
Definition: LGCIGenerator.h:73
UInt violated
Number of total cover cuts generated excluding duplicates.
Definition: LGCIGenerator.h:63
UInt noviol
Number of Gu, Nemhauser, Savelsbergh cuts generated.
Definition: LGCIGenerator.h:70

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