GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
dec_consclass.cpp
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the program and library */
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-2026 Operations Research, RWTH Aachen University */
10/* Zuse Institute Berlin (ZIB) */
11/* */
12/* Licensed under the Apache License, Version 2.0 (the "License"); */
13/* you may not use this file except in compliance with the License. */
14/* You may obtain a copy of the License at */
15/* */
16/* http://www.apache.org/licenses/LICENSE-2.0 */
17/* */
18/* Unless required by applicable law or agreed to in writing, software */
19/* distributed under the License is distributed on an "AS IS" BASIS, */
20/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
21/* See the License for the specific language governing permissions and */
22/* limitations under the License. */
23/* */
24/* You should have received a copy of the Apache-2.0 license */
25/* along with GCG; see the file LICENSE. If not visit gcg.or.rwth-aachen.de.*/
26/* */
27/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28
29/**@file dec_consclass.cpp
30 *
31 * @brief detector consclass (put your description here)
32 * @author Michael Bastubbe
33 */
34
35/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
36
37#include "gcg/dec_consclass.h"
38#include "gcg/cons_decomp.h"
42#include "gcg/gcg.h"
43#include "scip/cons_setppc.h"
44#include "scip/scip.h"
45#include "gcg/scip_misc.h"
46#include "scip/clock.h"
47
48#include <sstream>
49
50#include <iostream>
51#include <algorithm>
52
53/* constraint handler properties */
54#define DEC_NAME "consclass" /**< name of detector */
55#define DEC_DESC "detector consclass" /**< 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 INT_MAX /** 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 'c' /**< display character of detector */
64#define DEC_ENABLED TRUE /**< should the detection be enabled */
65#define DEC_ENABLEDFINISHING FALSE /**< should the detection be enabled */
66#define DEC_ENABLEDPOSTPROCESSING FALSE /**< should the finishing 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#define DEFAULT_MAXIMUMNCLASSES 5
71#define AGGRESSIVE_MAXIMUMNCLASSES 9
72#define FAST_MAXIMUMNCLASSES 3
73
74#define SET_MULTIPLEFORSIZETRANSF 12500
75
76/*
77 * Data structures
78 */
79
80/** @todo fill in the necessary detector data */
81
82/** detector handler data */
84{
85};
86
87/*
88 * Local methods
89 */
90
91/* put your local methods here, and declare them static */
92
93/*
94 * detector callback methods
95 */
96
97/** destructor of detector to free user data (called when GCG is exiting) */
98#define freeConsclass NULL
99
100/** destructor of detector to free detector data (called before the solving process begins) */
101#define exitConsclass NULL
102
103/** detection initialization function of detector (called before solving is about to begin) */
104#define initConsclass NULL
105
106#define detectConsclass NULL
107
108#define finishPartialdecConsclass NULL
109
110static GCG_DECL_PROPAGATEPARTIALDEC(propagatePartialdecConsclass)
111{
113 char decinfo[SCIP_MAXSTRLEN];
115 assert(origprob != NULL);
116
117 SCIP_CLOCK* temporaryClock;
118
119 SCIP_CALL_ABORT( SCIPcreateClock(origprob, &temporaryClock) );
120 SCIP_CALL_ABORT( SCIPstartClock(origprob, temporaryClock) );
121
122 std::vector<gcg::PARTIALDECOMP*> foundpartialdecs(0);
123
124 gcg::PARTIALDECOMP* partialdecOrig;
125 gcg::PARTIALDECOMP* partialdec;
126
127 int maximumnclasses;
128
129 if( partialdecdetectiondata->detprobdata->getNConss() + partialdecdetectiondata->detprobdata->getNVars() >= 50000 )
130 SCIPgetIntParam(origprob, "detection/classification/maxnclassesperpartitionforlargeprobs", &maximumnclasses);
131 else
132 SCIPgetIntParam(origprob, "detection/classification/maxnclassesperpartition", &maximumnclasses);
133
134 SCIPverbMessage(origprob, SCIP_VERBLEVEL_HIGH, NULL, " in dec_consclass: there are %d different constraint classes \n ",
135 partialdecdetectiondata->detprobdata->getNConsPartitions() );
136
137
138 for( int classifierIndex = 0; classifierIndex < partialdecdetectiondata->detprobdata->getNConsPartitions(); ++classifierIndex )
139 {
140 gcg::ConsPartition* classifier = partialdecdetectiondata->detprobdata->getConsPartition(classifierIndex);
141 std::vector<int> consclassindices_master;
142
143 if( classifier->getNClasses() > maximumnclasses )
144 {
146 " the current consclass distribution includes %d classes but only %d are allowed for propagatePartialdec() of cons class detector\n",
147 classifier->getNClasses(), maximumnclasses);
148 continue;
149 }
150
151 SCIPverbMessage(origprob, SCIP_VERBLEVEL_HIGH, NULL, " the current constraint classifier \"%s\" consists of %d different classes \n ", classifier->getName(), classifier->getNClasses() );
152
153 partialdecOrig = partialdecdetectiondata->workonpartialdec;
154
155 for( int i = 0; i < classifier->getNClasses(); ++ i )
156 {
157 if ( classifier->getClassDecompInfo(i) == gcg::ONLY_MASTER )
158 consclassindices_master.push_back(i);
159 }
160
161 std::vector< std::vector<int> > subsetsOfConsclasses = classifier->getAllSubsets( true, false, false );
162
163 for( auto& subset : subsetsOfConsclasses )
164 {
165 if( subset.empty() && consclassindices_master.empty() )
166 continue;
167
168 partialdec = new gcg::PARTIALDECOMP(partialdecOrig);
169
170 /* fix open conss that have a) type of the current subset or b) decomp info ONLY_MASTER as master conss */
171 auto& openconss = partialdec->getOpenconss();
172 for( auto itr = openconss.cbegin(); itr != openconss.cend(); )
173 {
174 bool foundCons = false;
175 for( int consclassId : subset )
176 {
177 if( classifier->getClassOfCons(*itr) == consclassId )
178 {
179 itr = partialdec->fixConsToMaster(itr);
180 foundCons = true;
181 break;
182 }
183 }
184 /* only check consclassindices_master if current cons has not already been found in a subset */
185 if ( !foundCons )
186 {
187 for(int consclassId : consclassindices_master )
188 {
189 if( classifier->getClassOfCons(*itr) == consclassId )
190 {
191 itr = partialdec->fixConsToMaster(itr);
192 foundCons = true;
193 break;
194 }
195 }
196 }
197 if( !foundCons )
198 {
199 ++itr;
200 }
201 }
202
203 if( partialdec->getNOpenconss() < partialdecOrig->getNOpenconss() )
204 {
205 /* set decinfo to: consclass_<classfier_name>:<master_class_name#1>-...-<master_class_name#n> */
206 std::stringstream decdesc;
207 decdesc << "consclass" << "\\_" << classifier->getName() << ": \\\\ ";
208 std::vector<int> curmasterclasses(consclassindices_master);
209 for( size_t consclassId = 0; consclassId < subset.size(); ++consclassId )
210 {
211 if( consclassId > 0 )
212 {
213 decdesc << "-";
214 }
215 decdesc << classifier->getClassName(subset[consclassId]);
216 if( std::find(consclassindices_master.begin(), consclassindices_master.end(),
217 subset[consclassId]) == consclassindices_master.end())
218 {
219 curmasterclasses.push_back(subset[consclassId]);
220 }
221 }
222 for( size_t consclassId = 0; consclassId < consclassindices_master.size(); ++consclassId )
223 {
224 if( consclassId > 0 || !subset.empty())
225 {
226 decdesc << "-";
227 }
228 decdesc << classifier->getClassName(consclassindices_master[consclassId]);
229 }
230
231 partialdec->sort();
232 (void) SCIPsnprintf(decinfo, SCIP_MAXSTRLEN, decdesc.str().c_str());
233 partialdec->addDetectorChainInfo(decinfo);
234 partialdec->setConsPartitionStatistics(partialdec->getNDetectors(), classifier, curmasterclasses);
235
236 foundpartialdecs.push_back(partialdec);
237 }
238 else
239 delete partialdec;
240 }
241 }
242
243 SCIP_CALL_ABORT( SCIPstopClock(origprob, temporaryClock) );
244
245 partialdecdetectiondata->detectiontime = SCIPgetClockTime(origprob, temporaryClock);
246 SCIP_CALL( SCIPallocMemoryArray(origprob, &(partialdecdetectiondata->newpartialdecs), foundpartialdecs.size()) );
247 partialdecdetectiondata->nnewpartialdecs = (int) foundpartialdecs.size();
248
249 SCIPverbMessage(origprob, SCIP_VERBLEVEL_HIGH, NULL, "dec_consclass found %d new partialdecs \n", partialdecdetectiondata->nnewpartialdecs );
250
251 for( int s = 0; s < partialdecdetectiondata->nnewpartialdecs; ++s )
252 {
253 partialdecdetectiondata->newpartialdecs[s] = foundpartialdecs[s];
254 partialdecdetectiondata->newpartialdecs[s]->addClockTime(partialdecdetectiondata->detectiontime / partialdecdetectiondata->nnewpartialdecs);
255 }
256
257 SCIP_CALL_ABORT(SCIPfreeClock(origprob, &temporaryClock) );
258
260
261 return SCIP_OKAY;
262}
263
264
265#define detectorPostprocessPartialdecConsclass NULL
266
267static
268GCG_DECL_SETPARAMAGGRESSIVE(setParamAggressiveConsclass)
269{
270 char setstr[SCIP_MAXSTRLEN];
271 SCIP_Real modifier;
273 assert(origprob != NULL);
274
275 int newval;
276 const char* name = GCGdetectorGetName(detector);
277
278 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
280
281 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
283
285 {
286 return SCIP_OKAY;
287 }
288
290 modifier = log(modifier) / log(2.);
291
292 if (!SCIPisFeasPositive(origprob, modifier) )
293 modifier = -1.;
294
295 modifier = SCIPfloor(origprob, modifier);
296
297 newval = MAX( 6, AGGRESSIVE_MAXIMUMNCLASSES - modifier );
298 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/maxnclasses", name);
299
300 SCIP_CALL( SCIPsetIntParam(origprob, setstr, newval ) );
301 SCIPverbMessage(origprob, SCIP_VERBLEVEL_DIALOG, NULL, "\n%s = %d\n", setstr, newval);
302
303
304 return SCIP_OKAY;
305
306}
307
308
309static
310GCG_DECL_SETPARAMDEFAULT(setParamDefaultConsclass)
311{
312 char setstr[SCIP_MAXSTRLEN];
313 SCIP_Real modifier;
314 int newval;
315 const char* name = GCGdetectorGetName(detector);
317 assert(origprob != NULL);
318
319 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
321
322 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
324
326 {
327 return SCIP_OKAY;
328 }
329
330
332 modifier = log(modifier) / log(2);
333
334 if (!SCIPisFeasPositive(origprob, modifier) )
335 modifier = -1.;
336
337 modifier = SCIPfloor(origprob, modifier);
338
339 newval = MAX( 6, DEFAULT_MAXIMUMNCLASSES - modifier );
340 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/maxnclasses", name);
341
342 SCIP_CALL( SCIPsetIntParam(origprob, setstr, newval ) );
343 SCIPverbMessage(origprob, SCIP_VERBLEVEL_DIALOG, NULL, "\n%s = %d\n", setstr, newval);
344
345 return SCIP_OKAY;
346
347}
348
349static
350GCG_DECL_SETPARAMFAST(setParamFastConsclass)
351{
352 char setstr[SCIP_MAXSTRLEN];
353 SCIP_Real modifier;
354 int newval;
355 const char* name = GCGdetectorGetName(detector);
357 assert(origprob != NULL);
358
359 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
361
362 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
364
366 {
367 return SCIP_OKAY;
368 }
369
370
372
373 modifier = log(modifier) / log(2);
374
375 if (!SCIPisFeasPositive(origprob, modifier) )
376 modifier = -1.;
377
378 modifier = SCIPfloor(origprob, modifier);
379
380 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/maxnclasses", name);
381
382 newval = MAX( 6, FAST_MAXIMUMNCLASSES - modifier );
383
384 SCIP_CALL( SCIPsetIntParam(origprob, setstr, newval ) );
385 SCIPverbMessage(origprob, SCIP_VERBLEVEL_DIALOG, NULL, "\n%s = %d\n", setstr, newval);
386
387 return SCIP_OKAY;
388
389}
390
391
392/*
393 * detector specific interface methods
394 */
395
396/** creates the handler for consclass detector and includes it in SCIP */
398 GCG* gcg /**< GCG data structure */
399)
400{
401 GCG_DETECTORDATA* detectordata;
402 char setstr[SCIP_MAXSTRLEN];
404 assert(origprob != NULL);
405
406 /**@todo create consclass detector data here*/
407 detectordata = NULL;
408
409 SCIP_CALL(
412 freeConsclass, initConsclass, exitConsclass, propagatePartialdecConsclass, finishPartialdecConsclass, detectorPostprocessPartialdecConsclass, setParamAggressiveConsclass, setParamDefaultConsclass, setParamFastConsclass));
413
414 /**@todo add consclass detector parameters */
415
416 const char* name = DEC_NAME;
417 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/maxnclasses", name);
418 SCIP_CALL( SCIPaddIntParam(origprob, setstr, "maximum number of classes ", NULL, FALSE, DEFAULT_MAXIMUMNCLASSES, 1, INT_MAX, NULL, NULL ) );
419
420 return SCIP_OKAY;
421}
class representing a partition of a set of constraints
class storing partialdecs and the problem matrix
class storing (potentially incomplete) decompositions
GCG_EXPORT int getClassOfCons(int consindex)
GCG_EXPORT CONS_DECOMPINFO getClassDecompInfo(int classindex)
GCG_EXPORT std::vector< std::vector< int > > getAllSubsets(bool both, bool only_master, bool only_pricing)
GCG_EXPORT const char * getClassName(int classindex)
GCG_EXPORT const char * getName()
class to manage partial decompositions
GCG_EXPORT int getNDetectors()
Gets the number of detectors the partialdec is propagated by.
GCG_EXPORT void addDetectorChainInfo(const char *decinfo)
add information about the detector chain
GCG_EXPORT int getNOpenconss()
Gets size of vector containing constraints not assigned yet.
GCG_EXPORT void setConsPartitionStatistics(int detectorchainindex, ConsPartition *partition, std::vector< int > &consclassesmaster)
registers statistics for a used conspartition
GCG_EXPORT std::vector< int > & getOpenconss()
Gets vector containing constraints not assigned yet.
GCG_EXPORT std::vector< int >::const_iterator fixConsToMaster(std::vector< int >::const_iterator itr)
fixes a constraint to the master constraints
GCG_EXPORT bool sort()
sorts the vars and conss data structures by their indices
const char * GCGdetectorGetName(GCG_DETECTOR *detector)
returns the name of the provided detector
SCIP_RETCODE GCGincludeDetector(GCG *gcg, 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, GCG_DETECTORDATA *detectordata, GCG_DECL_FREEDETECTOR((*freeDetector)), GCG_DECL_INITDETECTOR((*initDetector)), GCG_DECL_EXITDETECTOR((*exitDetector)), GCG_DECL_PROPAGATEPARTIALDEC((*propagatePartialdecDetector)), GCG_DECL_FINISHPARTIALDEC((*finishPartialdecDetector)), GCG_DECL_POSTPROCESSPARTIALDEC((*postprocessPartialdecDetector)), GCG_DECL_SETPARAMAGGRESSIVE((*setParamAggressiveDetector)), GCG_DECL_SETPARAMDEFAULT((*setParamDefaultDetector)),)
includes one detector
constraint handler for structure detection
#define DEC_MAXCALLROUND
#define DEC_MINCALLROUNDORIGINAL
#define DEC_ENABLEDFINISHING
#define DEC_ENABLED
#define DEC_DECCHAR
#define DEC_NAME
#define DEC_MINCALLROUND
#define DEC_ENABLEDPOSTPROCESSING
#define DEC_DESC
#define DEC_FREQCALLROUND
#define DEC_PRIORITY
#define DEC_USEFULRECALL
#define DEC_SKIP
#define DEC_MAXCALLROUNDORIGINAL
#define DEC_FREQCALLROUNDORIGINAL
#define exitConsclass
#define freeConsclass
#define initConsclass
#define finishPartialdecConsclass
#define FAST_MAXIMUMNCLASSES
#define SET_MULTIPLEFORSIZETRANSF
SCIP_RETCODE GCGincludeDetectorConsclass(GCG *gcg)
#define DEFAULT_MAXIMUMNCLASSES
#define detectorPostprocessPartialdecConsclass
#define AGGRESSIVE_MAXIMUMNCLASSES
consclass detector
#define SCIP_MAXSTRLEN
#define SCIP_Real
#define MAX(x, y)
#define SCIP_CALL_ABORT(x)
#define TRUE
#define FALSE
GCG interface methods.
SCIP * GCGgetOrigprob(GCG *gcg)
Definition gcg.cpp:87
SCIP_STAGE SCIPgetStage(SCIP *scip)
int SCIPgetNVars(SCIP *scip)
int SCIPgetNConss(SCIP *scip)
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
SCIP_RETCODE SCIPaddIntParam(SCIP *scip, const char *name, const char *desc, int *valueptr, SCIP_Bool isadvanced, int defaultvalue, int minvalue, int maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
SCIP_RETCODE SCIPsetIntParam(SCIP *scip, const char *name, int value)
SCIP_RETCODE SCIPgetIntParam(SCIP *scip, const char *name, int *value)
SCIP_RETCODE SCIPsetBoolParam(SCIP *scip, const char *name, SCIP_Bool value)
#define SCIPallocMemoryArray(scip, ptr, num)
SCIP_RETCODE SCIPcreateClock(SCIP *scip, SCIP_CLOCK **clck)
SCIP_RETCODE SCIPstopClock(SCIP *scip, SCIP_CLOCK *clck)
SCIP_RETCODE SCIPfreeClock(SCIP *scip, SCIP_CLOCK **clck)
SCIP_Real SCIPgetClockTime(SCIP *scip, SCIP_CLOCK *clck)
SCIP_RETCODE SCIPstartClock(SCIP *scip, SCIP_CLOCK *clck)
SCIP_Real SCIPfloor(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisFeasPositive(SCIP *scip, SCIP_Real val)
int SCIPsnprintf(char *t, int len, const char *s,...)
static assert(heur !=NULL)
SCIP_CALL(SCIPgetExternBranchCands(origprob, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL))
SCIP * origprob
* result
int i
#define NULL
various SCIP helper methods
struct SCIP_Clock SCIP_CLOCK
#define GCG_DECL_SETPARAMFAST(x)
#define GCG_DECL_SETPARAMDEFAULT(x)
#define GCG_DECL_SETPARAMAGGRESSIVE(x)
#define GCG_DECL_PROPAGATEPARTIALDEC(x)
struct GCG_DetectorData GCG_DETECTORDATA
struct Gcg GCG
Definition type_gcg.h:44
SCIP_VERBLEVEL_DIALOG
SCIP_VERBLEVEL_HIGH
SCIP_VERBLEVEL_NORMAL
SCIP_DIDNOTFIND
SCIP_SUCCESS
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
SCIP_STAGE_PROBLEM