Minotaur 0.4.1
Docs for developers
CoverCutGenerator.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 MINOTAURCOVERCUTGENERATOR_H
15#define MINOTAURCOVERCUTGENERATOR_H
16
17#include <map>
18#include <fstream>
19using std::ofstream;
20#include <string>
21using std::string;
22
23#include "KnapsackList.h"
24#include "Problem.h"
25#include "Solution.h"
26#include "Types.h"
27#include "Cut.h"
28#include "Relaxation.h"
29#include "Environment.h"
30
31namespace Minotaur {
32
33 typedef enum
34 {
35 Cover = 0,
36 Cons,
37 Obj,
38 Set
39 } PrintType;
40
41 typedef enum
42 {
43 Totalcuts = 0,
44 Cuts,
45 Violated,
46 Extended,
47 Simple,
48 Gns,
49 Singlectwo,
50 Basic,
51 Noviol,
52 Noinitcover
53 } KnapCovType;
54
56 {
57 UInt knaps;
64 UInt
68 // Time is not calculated yet.
69 UInt
70 noviol; // GNS procedure terminated since there is no violation after
71 // upliftin F
72 UInt noinitcov; // GNS procedure terminated since there is no initial
73 // cover generated.
74 double time; // Total time used by generator.
75 };
76
77
78 typedef const Cut *ConstCutPtr;
80 typedef CovCutGenStats const *ConstCovCutGenStatsPtr;
81 typedef ofstream *OfstreamPtr;
82
92 public:
93 // Default constructor
95
96 // Constructor that uses a problem and a solution given.
98
99 // Constructor that uses a relaxation and a solution given.
101
102 // Destructor
104
105 // Initialize data elements.
106 bool initialize(RelaxationPtr p, ConstSolutionPtr s, EnvPtr env);
107
108 // Obtain the knapsack constraint list from problem.
109 void generateKnapList();
110
111 // Checks if the constraint has a cover set.
112 bool hasCover(ConstraintIterator it);
113
114 // Check if it is a GUB.
115 bool GUB(ConstraintIterator itcons);
116
117 // Constructs a vector of nonzero variables in the given solution.
118 void nonzeroVars(LinearFunctionPtr lf, CoverSetPtr nonzerovars,
119 CoverSetPtr zerovars);
120
121 // Sort nonzero variables array in nonincreasing order.
122 void sortNonIncreasing(CoverSetPtr nonzeros);
123
124 // Sort the variables in nondecreasing order of their reduced costs.
125 void sortReducedCosts(CoverSetPtr &vars);
126
131 bool coverSetGeneratorGNS(ConstConstraintPtr cons, CoverSetPtr cover);
132
133
134 /* Modified GNS cover set generator such that it always generates a cover.
135 This function generates a cover set by using different strategies.
136 * For now, it generates a modified version of Gu, Nemhauser, Savelsbergh
137 * such that it generates a cover set even though the number of nonzero
138 * elements in solution vector given is not enough for cover set
139 generation
140 * s.t sum(a_i) <= b for i s.t. x^*_i != 0.
141 */
142 CoverSetPtr coverSetGenGNSModified(ConstConstraintPtr cons);
143
144
152
153
154 // Obtain initial cover.
155 //CoverSetPtr initialCover(ConstConstraintPtr constraintPtr);
156
161 CoverSetPtr varCoeff(LinearFunctionPtr lf);
162
166 void variableCoeffPair(CoverSetPtr cover, LinearFunctionPtr lf);
167
171 double sumCoeffs(CoverSetPtr cover);
172
176 void minimalCover(CoverSetPtr cover, ConstConstraintPtr cons);
177
178 // Generates all the cover cuts from all knapsack constraints.
179 void generateAllCuts();
180
181 // Generate cover partitions C1, C2 and Cbar according to Gu, Nemhauser,
182 // Savelsbergh.
183 void coverPartitionGNS(const ConstConstraintPtr cons,
184 const ConstCoverSetPtr cover, CoverSetPtr cone,
185 CoverSetPtr ctwo, CoverSetPtr fset,
186 CoverSetPtr rset);
187
188 // Generates set N\C, the variables outside of cover set.
189 void cBar(const ConstCoverSetPtr coverSetPtr, CoverSetPtr cBar,
190 const ConstConstraintPtr constraint);
191
192 // Lifts a variables up or down as it is specified by uplift.
193 double lift(const ConstCoverSetPtr obj, const ConstCoverSetPtr constraint,
194 const CoverSetConstIterator variable, double &rhs,
195 double &inititalb, bool uplift);
196
197 // Simple lifted cover
198 void simple(const ConstCoverSetPtr cover, const ConstCoverSetPtr cbar,
199 const ConstConstraintPtr cons);
200
201 // Simple lifted cover
202 void allCTwo(const ConstCoverSetPtr cover, const ConstCoverSetPtr cone,
203 const ConstCoverSetPtr cbar, const ConstConstraintPtr cons);
204
205 // Initialize the cover inequality by changing the coefficients of cover
206 // set by 1s.
207 void initCoverIneq(const ConstCoverSetPtr coverset,
208 CoverSetPtr coverineq);
209
210 // Lifting strategy of Gu, Nemhauser, and Savelsbergh.i
211 bool liftingGNS(const ConstCoverSetPtr cone, const ConstCoverSetPtr ctwo,
212 const ConstCoverSetPtr fset, const ConstCoverSetPtr rset,
213 CoverSetPtr constraint, const ConstConstraintPtr cons,
214 double &ub);
215
216
217 // This lifts the variables in a given set according to Gu, Nemhauser and
218 // Savelsbergh algorithm by considering the amount of contribution.
219 void liftSetF(CoverSetPtr obj, CoverSetPtr consknap,
220 const ConstCoverSetPtr setf, CoverSetPtr coverineq,
221 double &ub, double &initialb, const bool liftup);
222
223 // This function lifts up and lifts down the variables similar to liftSet
224 // but the assumprions are changed as the same as liftSetGNS.
225 void liftSet(CoverSetPtr obj, CoverSetPtr consknap,
226 const ConstCoverSetPtr varset, CoverSetPtr constraint,
227 double &ub, double &initialb, bool liftup);
228
229 // Generates a Gu, Nemhauser, Savelsbergh lifted cover inequality.
230 bool GNS(const ConstConstraintPtr cons);
231
232 // Generates a cover cut from a cover set.
233 CutPtr generateCut(const ConstCoverSetPtr constraint, const double ub);
234
235 // Add the cut to the list and insert the corresponding violation to
236 // violation list.
237 void addCut(CutPtr cut);
238
239 // Check if the same cut is already included.
240 bool checkExists(CoverSetPtr cov, double rhs);
241
242 // Generates all the cover cuts from a given knapsack constraint.
243 void generateCuts(ConstConstraintPtr constraint);
244
245 // Generates an extended cover from a given cover set.
246 void extendedCover(CoverSetPtr cover, ConstConstraintPtr cons);
247
248 // Implementation based on Horowitz-Shahni algorithm implementation in
249 // CglKnapsackCover
250 UInt binaryKnapsackSolver(size_t n, double b, double const *c,
251 double const *a, double &z, int *x);
252
253 // Calculates the violation for the given cut.
254 double violation(CutPtr cut);
255
256 // Return const pointer for problem, check if this works!!!.
257 ConstProblemPtr getProblem() const { return ConstProblemPtr(p_); }
258
259 // Return const knapsack list.
260 ConstKnapsackListPtr getKnapsackList() const
261 {
262 return ConstKnapsackListPtr(knapsackListPtr_);
263 }
264
265 // Return const solution.
266 ConstSolutionPtr getSolution() const { return ConstSolutionPtr(s_); }
267
268 // Return const cut list.
269 CutVector getCutList() const { return cutVec_; }
270
271 // Return violation list.
272 DoubleVector getViolList() const { return violList_; }
273
274 // Return number of constraints considered.
275 UInt getNumCons() const { return numCons_; }
276
277 // Return statistics of cover cut generator.
278 ConstCovCutGenStatsPtr getStats() const
279 {
280 return ConstCovCutGenStatsPtr(stats_);
281 }
282
283 // Initialize statistics
284 void initStats();
285
286 // Adds a cut from a given cover set and rhs by checking integrality and
287 // if the cut already exists.
288 bool addCut(CoverSetPtr cov, double rhs, UInt cuttype);
289
290 // Return only the violated cuts.
291 CutVector getViolatedCutList() const { return violatedCuts_; };
292
293 // Check if the given solution satisfies integrality for given problem.
294 bool checkIntegral(RelaxationPtr p, ConstSolutionPtr s);
295
296 // Print inequality
297 void printIneq(const ConstCoverSetPtr cov, double rhs, PrintType type,
298 string message);
299
300 // Print lifting problem.
301 void printLiftProb(const ConstCoverSetPtr obj,
302 const ConstCoverSetPtr consknap,
303 const CoverSetConstIterator variable, double rhs,
304 double initialb, bool uplift, double b, double gamma,
305 double alpha);
306
307 private:
308 // Environment.
309 EnvPtr env_;
310
311 // Problem that cover cuts will be generated for.
312 ProblemPtr p_;
313
314 // List of cuts generated.
315 CutVector cutVec_;
316
317 // List of violated cuts.
318 CutVector violatedCuts_;
319
320 // List of violations obtained from cuts generated in the same order.
321 DoubleVector violList_;
322
323 // List of knapsack inequalities in the problem.
324 KnapsackListPtr knapsackListPtr_;
325
330 ConstSolutionPtr s_;
331
332 // Number of knapsack inequalities considered.
333 UInt numCons_;
334
335 // Statistics for cover cut generator.
336 CovCutGenStatsPtr stats_;
337
338 // Hash map that is used to check if a cut is already created or not.
339 std::map<std::vector<double>, UInt> cutmap;
340
341 // Integer tolerance.
342 double intTol_;
343
344 // Objective tolerance.
345 double objtol_;
346
347 // Output file.
348 OfstreamPtr output_;
349
350 // Output file name
351 string outfile_;
352 };
353} //namespace Minotaur
354
355#endif // MINOTAURCOVERCUTGENERATOR_H
Declare the Cut class of valid inequalities.
Define the Environment class.
Declare base class KnapsackList.
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
Definition: CoverCutGenerator.h:91
void minimalCover(CoverSetPtr cover, ConstConstraintPtr cons)
Definition: CoverCutGenerator.cpp:520
void sortNonIncreasing(CoverSetPtr nonzeros)
Definition: CoverCutGenerator.cpp:280
double sumCoeffs(CoverSetPtr cover)
Definition: CoverCutGenerator.cpp:506
void variableCoeffPair(CoverSetPtr cover, LinearFunctionPtr lf)
CoverSetPtr varCoeff(LinearFunctionPtr lf)
Definition: CoverCutGenerator.cpp:336
CoverSetPtr coverSetGeneratorDefault(ConstConstraintPtr cons)
Definition: CoverCutGenerator.cpp:358
bool coverSetGeneratorGNS(ConstConstraintPtr cons, CoverSetPtr cover)
Definition: CoverCutGenerator.cpp:395
Store function, bounds and other information about a cut.
Definition: Cut.h:52
Definition: Environment.h:28
Definition: KnapsackList.h:25
The base class linear function is of the form c'x.
Definition: LinearFunction.h:31
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: CoverCutGenerator.h:56
UInt gns
Number of simple lifted cover cuts generated.
Definition: CoverCutGenerator.h:63
UInt violated
Number of total cover cuts generated.
Definition: CoverCutGenerator.h:60
UInt extended
Number of violated cuts.
Definition: CoverCutGenerator.h:61
UInt cuts
Number of all cuts i.e. included duplicates as well.
Definition: CoverCutGenerator.h:59
UInt totalcuts
Number of total knapsacks solved.
Definition: CoverCutGenerator.h:58
UInt basic
is downlifted.
Definition: CoverCutGenerator.h:67
UInt noviol
Number of cuts generated from initial cover set.
Definition: CoverCutGenerator.h:70
UInt singlectwo
Number of Gu, Nemhauser, Savelsbergh cuts generated.
Definition: CoverCutGenerator.h:65
UInt simple
Number of extended cuts generated.
Definition: CoverCutGenerator.h:62

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