Scippy

GCG

Branch-and-Price & Column Generation for Everyone

dec_generalmastersetpack.cpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program */
4 /* GCG --- Generic Column Generation */
5 /* a Dantzig-Wolfe decomposition based extension */
6 /* of the branch-cut-and-price framework */
7 /* SCIP --- Solving Constraint Integer Programs */
8 /* */
9 /* Copyright (C) 2010-2021 Operations Research, RWTH Aachen University */
10 /* Zuse Institute Berlin (ZIB) */
11 /* */
12 /* This program is free software; you can redistribute it and/or */
13 /* modify it under the terms of the GNU Lesser General Public License */
14 /* as published by the Free Software Foundation; either version 3 */
15 /* of the License, or (at your option) any later version. */
16 /* */
17 /* This program is distributed in the hope that it will be useful, */
18 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
19 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
20 /* GNU Lesser General Public License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with this program; if not, write to the Free Software */
24 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.*/
25 /* */
26 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
27 
28 /**@file dec_generalmastersetpack.cpp
29  * @ingroup DETECTORS
30  * @brief detector for set packing constraints
31  * @author Martin Bergner
32  *
33  * This detector sets the following constraints to master:
34  * - set packing constraints
35  * - constraints with -infinity lhs and nonnegative rhs
36  *
37  */
38 
39 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
40 
42 #include "cons_decomp.h"
43 #include "class_partialdecomp.h"
44 #include "class_detprobdata.h"
45 #include "gcg.h"
46 #include "scip/cons_setppc.h"
47 #include "scip/scip.h"
48 #include "scip_misc.h"
49 #include "scip/clock.h"
50 
51 #include <iostream>
52 
53 /* constraint handler properties */
54 #define DEC_DETECTORNAME "generalmastersetpack" /**< name of detector */
55 #define DEC_DESC "detector generalmastersetpack" /**< description of detector*/
56 #define DEC_FREQCALLROUND 1 /** frequency the detector gets called in detection loop ,ie it is called in round r if and only if minCallRound <= r <= maxCallRound AND (r - minCallRound) mod freqCallRound == 0 */
57 #define DEC_MAXCALLROUND 0 /** last round the detector gets called */
58 #define DEC_MINCALLROUND 0 /** first round the detector gets called */
59 #define DEC_FREQCALLROUNDORIGINAL 1 /** frequency the detector gets called in detection loop while detecting the original problem */
60 #define DEC_MAXCALLROUNDORIGINAL 0 /** last round the detector gets called while detecting the original problem */
61 #define DEC_MINCALLROUNDORIGINAL 0 /** first round the detector gets called while detecting the original problem */
62 #define DEC_PRIORITY 0 /**< priority of the constraint handler for separation */
63 #define DEC_DECCHAR '?' /**< display character of detector */
64 #define DEC_ENABLED TRUE /**< should the detection be enabled */
65 #define DEC_ENABLEDFINISHING FALSE /**< should the finishing be enabled */
66 #define DEC_ENABLEDPOSTPROCESSING FALSE /**< should the postprocessing be enabled */
67 #define DEC_SKIP FALSE /**< should detector be skipped if other detectors found decompositions */
68 #define DEC_USEFULRECALL FALSE /**< is it useful to call this detector on a descendant of the propagated partialdec */
69 
70 /*
71  * Data structures
72  */
73 
74 /** @todo fill in the necessary detector data */
75 
76 /** detector handler data */
77 struct DEC_DetectorData
78 {
79 };
80 
81 /*
82  * Local methods
83  */
84 
85 /* put your local methods here, and declare them static */
86 
87 /*
88  * detector callback methods
89  */
90 
91 /** destructor of detector to free user data (called when GCG is exiting) */
92 #define freeGeneralmastersetpack NULL
93 
94 /** destructor of detector to free detector data (called before the solving process begins) */
95 #define exitGeneralmastersetpack NULL
96 
97 /** detection initialization function of detector (called before solving is about to begin) */
98 #define initGeneralmastersetpack NULL
99 
100 
101 static DEC_DECL_PROPAGATEPARTIALDEC(propagatePartialdecGeneralmastersetpack)
102 {
103  *result = SCIP_DIDNOTFIND;
104  char decinfo[SCIP_MAXSTRLEN];
105 
106  SCIP_CLOCK* temporaryClock;
107  SCIP_CALL_ABORT( SCIPcreateClock(scip, &temporaryClock) );
108  SCIP_CALL_ABORT( SCIPstartClock(scip, temporaryClock) );
109 
110  SCIP_CONS* cons;
111  SCIP_VAR** vars;
112  SCIP_Real* vals;
113  int nvars;
114  bool relevant = true;
115 
116 
117  gcg::PARTIALDECOMP* partialdec = partialdecdetectiondata->workonpartialdec;
118  auto& openconss = partialdec->getOpenconssVec();
119  for( auto itr = openconss.cbegin(); itr!= openconss.cend(); )
120  {
121  bool found = false;
122  cons = partialdecdetectiondata->detprobdata->getCons(*itr);
123  /* set open setpacking constraints to master */
124  if( GCGconsGetType(scip, cons) == setpacking )
125  {
126  itr = partialdec->fixConsToMaster(itr);
127  found = true;
128  }
129  /* set constraints with -infinity lhs and nonnegative rhs to master */
130  else if(GCGconsGetType(scip, cons) != logicor && GCGconsGetType(scip, cons) != setcovering && GCGconsGetType(scip, cons) != setpartitioning )
131  {
132  relevant = true;
133  nvars = GCGconsGetNVars(scip, cons);
134  vars = NULL;
135  vals = NULL;
136  if( !SCIPisInfinity(scip, -GCGconsGetLhs(scip, cons)) )
137  relevant = false;
138  if( SCIPisNegative(scip, GCGconsGetRhs(scip, cons)) )
139  relevant = false;
140  if( nvars > 0 )
141  {
142  SCIP_CALL( SCIPallocMemoryArray(scip, &vars, nvars) );
143  SCIP_CALL( SCIPallocMemoryArray(scip, &vals, nvars) );
144  SCIP_CALL( GCGconsGetVars(scip, cons, vars, nvars) );
145  SCIP_CALL( GCGconsGetVals(scip, cons, vals, nvars) );
146  }
147  for( int j = 0; j < nvars && relevant; ++j )
148  {
149  assert(vars != NULL);
150  assert(vals != NULL);
151 
152  if( !SCIPvarIsIntegral(vars[j]) && !SCIPvarIsBinary(vars[j]) )
153  {
154  SCIPdebugPrintf("(%s is not integral) ", SCIPvarGetName(vars[j]) );
155  relevant = false;
156  }
157  if( !SCIPisEQ(scip, vals[j], 1.0) )
158  {
159  SCIPdebugPrintf("(coeff for var %s is %.2f != 1.0) ", SCIPvarGetName(vars[j]), vals[j] );
160  relevant = false;
161  }
162  }
163  SCIPfreeMemoryArrayNull(scip, &vals);
164  SCIPfreeMemoryArrayNull(scip, &vars);
165 
166  if(relevant)
167  {
168  itr = partialdec->fixConsToMaster(itr);
169  found = true;
170  }
171  }
172  if( !found )
173  {
174  ++itr;
175  }
176  }
177 
178  partialdec->sort();
179  SCIP_CALL_ABORT( SCIPstopClock(scip, temporaryClock) );
180 
181  partialdecdetectiondata->detectiontime = SCIPgetClockTime(scip, temporaryClock);
182  SCIP_CALL( SCIPallocMemoryArray(scip, &(partialdecdetectiondata->newpartialdecs), 1) );
183  partialdecdetectiondata->newpartialdecs[0] = partialdec;
184  partialdecdetectiondata->nnewpartialdecs = 1;
185  (void) SCIPsnprintf(decinfo, SCIP_MAXSTRLEN, "genmastersetpack");
186  partialdecdetectiondata->newpartialdecs[0]->addDetectorChainInfo(decinfo);
187  partialdecdetectiondata->newpartialdecs[0]->addClockTime(SCIPgetClockTime(scip, temporaryClock));
188  // we used the provided partialdec -> prevent deletion
189  partialdecdetectiondata->workonpartialdec = NULL;
190  SCIP_CALL_ABORT(SCIPfreeClock(scip, &temporaryClock) );
191 
192  *result = SCIP_SUCCESS;
193 
194  return SCIP_OKAY;
195 }
196 
197 #define finishPartialdecGeneralmastersetpack NULL
198 #define detectorPostprocessPartialdecGeneralmastersetpack NULL
199 
200 static
201 DEC_DECL_SETPARAMAGGRESSIVE(setParamAggressiveGeneralmastersetpack)
202 {
203  char setstr[SCIP_MAXSTRLEN];
204  const char* name = DECdetectorGetName(detector);
205  int newval;
206 
207  (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
208  SCIP_CALL( SCIPsetBoolParam(scip, setstr, TRUE) );
209 
210  (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
211  SCIP_CALL( SCIPsetBoolParam(scip, setstr, FALSE ) );
212 
213  (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/maxcallround", name);
214  SCIP_CALL( SCIPgetIntParam(scip, setstr, &newval) );
215  ++newval;
216  SCIP_CALL( SCIPsetIntParam(scip, setstr, newval ) );
217  SCIPinfoMessage(scip, NULL, "%s = %d\n", setstr, newval);
218 
219  (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/origmaxcallround", name);
220  SCIP_CALL( SCIPgetIntParam(scip, setstr, &newval) );
221  ++newval;
222  SCIP_CALL( SCIPsetIntParam(scip, setstr, newval ) );
223  SCIPinfoMessage(scip, NULL, "%s = %d\n", setstr, newval);
224 
225  return SCIP_OKAY;
226 
227 }
228 
229 
230 static
231 DEC_DECL_SETPARAMDEFAULT(setParamDefaultGeneralmastersetpack)
232 {
233  char setstr[SCIP_MAXSTRLEN];
234 
235  const char* name = DECdetectorGetName(detector);
236 
237  (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
238  SCIP_CALL( SCIPsetBoolParam(scip, setstr, DEC_ENABLED) );
239 
240  (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
241  SCIP_CALL( SCIPsetBoolParam(scip, setstr, DEC_ENABLEDFINISHING ) );
242 
243  return SCIP_OKAY;
244 
245 }
246 
247 static
248 DEC_DECL_SETPARAMFAST(setParamFastGeneralmastersetpack)
249 {
250  char setstr[SCIP_MAXSTRLEN];
251 
252  const char* name = DECdetectorGetName(detector);
253 
254  (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
255  SCIP_CALL( SCIPsetBoolParam(scip, setstr, FALSE) );
256 
257  (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
258  SCIP_CALL( SCIPsetBoolParam(scip, setstr, FALSE ) );
259 
260 
261  return SCIP_OKAY;
262 
263 }
264 
265 
266 /*
267  * detector specific interface methods
268  */
269 
270 /** creates the handler for generalmastersetpack detector and includes it in SCIP */
271 SCIP_RETCODE SCIPincludeDetectorGeneralmastersetpack(SCIP* scip /**< SCIP data structure */
272 )
273 {
274  DEC_DETECTORDATA* detectordata;
275 
276  /**@todo create generalmastersetpack detector data here*/
277  detectordata = NULL;
278 
279  SCIP_CALL(
281 
282  /**@todo add generalmastersetpack detector parameters */
283 
284  return SCIP_OKAY;
285 }
286 
287 
288 
289 
290 
#define DEC_MAXCALLROUNDORIGINAL
const char * DECdetectorGetName(DEC_DETECTOR *detector)
returns the name of the provided detector
static DEC_DECL_SETPARAMFAST(setParamFastGeneralmastersetpack)
#define DEC_ENABLEDFINISHING
SCIP_Real GCGconsGetRhs(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:108
#define DEC_DETECTORNAME
#define finishPartialdecGeneralmastersetpack
GCG interface methods.
std::vector< int >::const_iterator fixConsToMaster(std::vector< int >::const_iterator itr)
fixes a constraint to the master constraints
SCIP_Real GCGconsGetLhs(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:206
void addDetectorChainInfo(const char *decinfo)
add information about the detector chain
@ logicor
Definition: scip_misc.h:49
std::vector< int > & getOpenconssVec()
Gets a vector containing constraint ids not assigned yet as vector.
constraint handler for structure detection
SCIP_RETCODE GCGconsGetVars(SCIP *scip, SCIP_CONS *cons, SCIP_VAR **vars, int nvars)
Definition: scip_misc.c:490
#define DEC_MINCALLROUND
#define DEC_ENABLEDPOSTPROCESSING
static DEC_DECL_SETPARAMDEFAULT(setParamDefaultGeneralmastersetpack)
#define initGeneralmastersetpack
various SCIP helper methods
#define DEC_SKIP
SCIP_RETCODE SCIPincludeDetectorGeneralmastersetpack(SCIP *scip)
#define DEC_FREQCALLROUNDORIGINAL
#define DEC_PRIORITY
#define DEC_FREQCALLROUND
bool sort()
sorts the vars and conss data structures by their indices
static DEC_DECL_SETPARAMAGGRESSIVE(setParamAggressiveGeneralmastersetpack)
@ setpacking
Definition: scip_misc.h:48
@ setpartitioning
Definition: scip_misc.h:48
consType GCGconsGetType(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:51
SCIP_RETCODE GCGconsGetVals(SCIP *scip, SCIP_CONS *cons, SCIP_Real *vals, int nvals)
Definition: scip_misc.c:621
#define freeGeneralmastersetpack
#define DEC_USEFULRECALL
int GCGconsGetNVars(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:434
#define DEC_MAXCALLROUND
#define detectorPostprocessPartialdecGeneralmastersetpack
class to manage partial decompositions
#define exitGeneralmastersetpack
SCIP_RETCODE DECincludeDetector(SCIP *scip, const char *name, const char decchar, const char *description, int freqCallRound, int maxCallRound, int minCallRound, int freqCallRoundOriginal, int maxCallRoundOriginal, int minCallRoundOriginal, int priority, SCIP_Bool enabled, SCIP_Bool enabledFinishing, SCIP_Bool enabledPostprocessing, SCIP_Bool skip, SCIP_Bool usefulRecall, DEC_DETECTORDATA *detectordata, DEC_DECL_FREEDETECTOR((*freeDetector)), DEC_DECL_INITDETECTOR((*initDetector)), DEC_DECL_EXITDETECTOR((*exitDetector)), DEC_DECL_PROPAGATEPARTIALDEC((*propagatePartialdecDetector)), DEC_DECL_FINISHPARTIALDEC((*finishPartialdecDetector)), DEC_DECL_POSTPROCESSPARTIALDEC((*postprocessPartialdecDetector)), DEC_DECL_SETPARAMAGGRESSIVE((*setParamAggressiveDetector)), DEC_DECL_SETPARAMDEFAULT((*setParamDefaultDetector)),)
#define DEC_DECCHAR
class storing (potentially incomplete) decompositions
#define DEC_ENABLED
#define DEC_DESC
generalmastersetpack detector
static DEC_DECL_PROPAGATEPARTIALDEC(propagatePartialdecGeneralmastersetpack)
class storing partialdecs and the problem matrix
@ setcovering
Definition: scip_misc.h:48
#define DEC_MINCALLROUNDORIGINAL