Scippy

GCG

Branch-and-Price & Column Generation for Everyone

dec_varclass.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_varclass.cpp
30 *
31 * @brief detector varclass
32 * @author Julius Hense
33 */
34
35/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
36
37#include "gcg/dec_varclass.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 "varclass" /**< name of detector */
55#define DEC_DESC "detector varclass" /**< 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 'v' /**< 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#define DEFAULT_MAXIMUMNCLASSES 8
71#define AGGRESSIVE_MAXIMUMNCLASSES 10
72#define FAST_MAXIMUMNCLASSES 6
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 freeVarclass NULL
99
100/** destructor of detector to free detector data (called before the solving process begins) */
101#define exitVarclass NULL
102
103/** detection initialization function of detector (called before solving is about to begin) */
104#define initVarclass NULL
105
106#define finishPartialdecVarclass NULL
107
108static GCG_DECL_PROPAGATEPARTIALDEC(propagatePartialdecVarclass)
109{
110 *result = SCIP_DIDNOTFIND;
111 char decinfo[SCIP_MAXSTRLEN];
112 SCIP* origprob = GCGgetOrigprob(gcg);
113
114 SCIP_CLOCK* temporaryClock;
115
116 if (partialdecdetectiondata->workonpartialdec->getNOpenconss() != partialdecdetectiondata->detprobdata->getNConss() || partialdecdetectiondata->workonpartialdec->getNOpenvars() != partialdecdetectiondata->detprobdata->getNVars() )
117 {
118 *result = SCIP_SUCCESS;
119 return SCIP_OKAY;
120 }
121
122 SCIP_CALL_ABORT( SCIPcreateClock(origprob, &temporaryClock) );
123 SCIP_CALL_ABORT( SCIPstartClock(origprob, temporaryClock) );
124
125 std::vector<gcg::PARTIALDECOMP*> foundpartialdecs(0);
126
127 gcg::PARTIALDECOMP* partialdecOrig;
128 gcg::PARTIALDECOMP* partialdec;
129
130 int maximumnclasses;
131
132 if( partialdecdetectiondata->detprobdata->getNConss() + partialdecdetectiondata->detprobdata->getNVars() >= 50000 )
133 SCIPgetIntParam(origprob, "detection/classification/maxnclassesperpartitionforlargeprobs", &maximumnclasses);
134 else
135 SCIPgetIntParam(origprob, "detection/classification/maxnclassesperpartition", &maximumnclasses);
136
137 for( int classifierIndex = 0; classifierIndex < partialdecdetectiondata->detprobdata->getNVarPartitions(); ++classifierIndex )
138 {
139 gcg::VarPartition* classifier = partialdecdetectiondata->detprobdata->getVarPartition(classifierIndex);
140 std::vector<int> varclassindices_master = std::vector<int>(0);
141 std::vector<int> varclassindices_linking = std::vector<int>(0);
142
143 if ( classifier->getNClasses() > maximumnclasses )
144 {
145 std::cout << " the current varclass distribution includes " << classifier->getNClasses() << " classes but only " << maximumnclasses << " are allowed for propagatePartialdec() of var class detector" << std::endl;
146 continue;
147 }
148
149 partialdecOrig = partialdecdetectiondata->workonpartialdec;
150
151 for( int i = 0; i < classifier->getNClasses(); ++ i )
152 {
153 switch( classifier->getClassDecompInfo(i) )
154 {
155 case gcg::ALL:
156 break;
157 case gcg::LINKING:
158 varclassindices_linking.push_back(i);
159 break;
160 case gcg::MASTER:
161 varclassindices_master.push_back(i);
162 break;
163 case gcg::BLOCK:
164 break;
165 }
166 }
167
168 std::vector< std::vector<int> > subsetsOfVarclasses = classifier->getAllSubsets( true, false, false, false );
169
170 for( auto& subset : subsetsOfVarclasses )
171 {
172 if( subset.empty() && varclassindices_master.empty() && varclassindices_linking.empty() )
173 continue;
174
175 partialdec = new gcg::PARTIALDECOMP(partialdecOrig);
176
177 /* fix open vars that have a) type of the current subset or b) decomp info LINKING as linking vars */
178 auto& openvars = partialdec->getOpenvars();
179 for( auto itr = openvars.cbegin(); itr != openvars.cend(); )
180 {
181 bool foundVar = false;
182 for( int varclassId : subset )
183 {
184 if( classifier->getClassOfVar(*itr) == varclassId )
185 {
186 itr = partialdec->fixVarToLinking(itr);
187 foundVar = true;
188 break;
189 }
190 }
191 /* only check varclassindices_linking if current var has not already been found in a subset */
192 if ( !foundVar )
193 {
194 for( int varclassId : varclassindices_linking )
195 {
196 if( classifier->getClassOfVar(*itr) == varclassId )
197 {
198 itr = partialdec->fixVarToLinking(itr);
199 foundVar = true;
200 break;
201 }
202 }
203 }
204 /* only check varclassindices_master if current var has not already been found in a subset */
205 if ( !foundVar )
206 {
207 for( int varclassId : varclassindices_master )
208 {
209 if( classifier->getClassOfVar(*itr) == varclassId )
210 {
211 itr = partialdec->fixVarToMaster(itr);
212 foundVar = true;
213 break;
214 }
215 }
216 }
217 if( !foundVar )
218 {
219 ++itr;
220 }
221 }
222
223 /* set decinfo to: varclass_<classfier_name>:<linking_class_name#1>-...-<linking_class_name#n> */
224 std::stringstream decdesc;
225 decdesc << "varclass" << "\\_" << classifier->getName() << ": \\\\ ";
226 std::vector<int> curlinkingclasses( varclassindices_linking );
227 for ( size_t varclassId = 0; varclassId < subset.size(); ++varclassId )
228 {
229 if ( varclassId > 0 )
230 {
231 decdesc << "-";
232 }
233 decdesc << classifier->getClassName(subset[varclassId]);
234
235 if( std::find( varclassindices_linking.begin(), varclassindices_linking.end(),
236 subset[varclassId] ) == varclassindices_linking.end() )
237 {
238 curlinkingclasses.push_back(subset[varclassId]);
239 }
240 }
241 for( int varclassId : varclassindices_linking )
242 {
243 if( varclassId > 0 || !subset.empty() )
244 {
245 decdesc << "-";
246 }
247 decdesc << classifier->getClassName(varclassId);
248 }
249
250 partialdec->sort();
251 (void) SCIPsnprintf(decinfo, SCIP_MAXSTRLEN, decdesc.str().c_str());
252 partialdec->addDetectorChainInfo(decinfo);
253 partialdec->setVarPartitionStatistics(partialdec->getNDetectors(), classifier, curlinkingclasses,
254 varclassindices_master);
255
256 foundpartialdecs.push_back(partialdec);
257 }
258 }
259
260 SCIP_CALL_ABORT( SCIPstopClock(origprob, temporaryClock) );
261
262 partialdecdetectiondata->detectiontime = SCIPgetClockTime(origprob, temporaryClock);
263 SCIP_CALL( SCIPallocMemoryArray(origprob, &(partialdecdetectiondata->newpartialdecs), foundpartialdecs.size()) );
264 partialdecdetectiondata->nnewpartialdecs = (int) foundpartialdecs.size();
265
266 for( int s = 0; s < partialdecdetectiondata->nnewpartialdecs; ++s )
267 {
268 partialdecdetectiondata->newpartialdecs[s] = foundpartialdecs[s];
269 partialdecdetectiondata->newpartialdecs[s]->addClockTime(partialdecdetectiondata->detectiontime / partialdecdetectiondata->nnewpartialdecs);
270 }
271
272 SCIP_CALL_ABORT(SCIPfreeClock(origprob, &temporaryClock) );
273
274 *result = SCIP_SUCCESS;
275
276 return SCIP_OKAY;
277}
278
279
280#define detectorPostprocessPartialdecVarclass NULL
281
282static
283GCG_DECL_SETPARAMAGGRESSIVE(setParamAggressiveVarclass)
284{
285 char setstr[SCIP_MAXSTRLEN];
286 SCIP_Real modifier;
287
288 int newval;
289 const char* name = GCGdetectorGetName(detector);
290 SCIP* origprob = GCGgetOrigprob(gcg);
291
292 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
293 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, TRUE) );
294
295 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
296 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, FALSE ) );
297
298 if( SCIPgetStage(origprob) < SCIP_STAGE_PROBLEM )
299 return SCIP_OKAY;
300
301 modifier = ((SCIP_Real)SCIPgetNConss(origprob) + (SCIP_Real)SCIPgetNVars(origprob) ) / SET_MULTIPLEFORSIZETRANSF;
302 modifier = log(modifier) / log(2.);
303
304 if (!SCIPisFeasPositive(origprob, modifier) )
305 modifier = -1.;
306
307 modifier = SCIPfloor(origprob, modifier);
308
309 newval = MAX( 2, AGGRESSIVE_MAXIMUMNCLASSES - modifier );
310 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/maxnclasses", name);
311
312 SCIP_CALL( SCIPsetIntParam(origprob, setstr, newval ) );
313 SCIPverbMessage(origprob, SCIP_VERBLEVEL_DIALOG, NULL, "\n%s = %d\n", setstr, newval);
314
315 return SCIP_OKAY;
316}
317
318
319static
320GCG_DECL_SETPARAMDEFAULT(setParamDefaultVarclass)
321{
322 char setstr[SCIP_MAXSTRLEN];
323 SCIP_Real modifier;
324
325 int newval;
326 const char* name = GCGdetectorGetName(detector);
327 SCIP* origprob = GCGgetOrigprob(gcg);
328
329 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
330 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, DEC_ENABLED) );
331
332 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
333 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, DEC_ENABLEDFINISHING ) );
334
335 if( SCIPgetStage(origprob) < SCIP_STAGE_PROBLEM )
336 return SCIP_OKAY;
337
338 modifier = ( (SCIP_Real)SCIPgetNConss(origprob) + (SCIP_Real)SCIPgetNVars(origprob) ) / SET_MULTIPLEFORSIZETRANSF;
339 modifier = log(modifier) / log(2);
340
341 if (!SCIPisFeasPositive(origprob, modifier) )
342 modifier = -1.;
343
344 modifier = SCIPfloor(origprob, modifier);
345
346 newval = MAX( 2, DEFAULT_MAXIMUMNCLASSES - modifier );
347 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/maxnclasses", name);
348
349 SCIP_CALL( SCIPsetIntParam(origprob, setstr, newval ) );
350 SCIPverbMessage(origprob, SCIP_VERBLEVEL_DIALOG, NULL, "\n%s = %d\n", setstr, newval);
351
352 return SCIP_OKAY;
353
354}
355
356static
357GCG_DECL_SETPARAMFAST(setParamFastVarclass)
358{
359 char setstr[SCIP_MAXSTRLEN];
360 SCIP_Real modifier;
361 int newval;
362 const char* name = GCGdetectorGetName(detector);
363 SCIP* origprob = GCGgetOrigprob(gcg);
364
365 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
366 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, FALSE) );
367
368 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
369 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, FALSE ) );
370
371 if( SCIPgetStage(origprob) < SCIP_STAGE_PROBLEM )
372 return SCIP_OKAY;
373
374 modifier = ( (SCIP_Real)SCIPgetNConss(origprob) + (SCIP_Real)SCIPgetNVars(origprob) ) / SET_MULTIPLEFORSIZETRANSF;
375
376 modifier = log(modifier) / log(2);
377
378 if (!SCIPisFeasPositive(origprob, modifier) )
379 modifier = -1.;
380
381 modifier = SCIPfloor(origprob, modifier);
382
383 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/maxnclasses", name);
384
385 newval = MAX( 2, FAST_MAXIMUMNCLASSES - modifier );
386
387 SCIP_CALL( SCIPsetIntParam(origprob, setstr, newval ) );
388 SCIPverbMessage(origprob, SCIP_VERBLEVEL_DIALOG, NULL, "\n%s = %d\n", setstr, newval);
389
390 return SCIP_OKAY;
391}
392
393
394
395/*
396 * detector specific interface methods
397 */
398
399/** creates the handler for varclass detector and includes it in SCIP */
401 GCG* gcg /**< GCG data structure */
402 )
403{
404 GCG_DETECTORDATA* detectordata;
405 char setstr[SCIP_MAXSTRLEN];
406 SCIP* origprob = GCGgetOrigprob(gcg);
407
408 /**@todo create varclass detector data here*/
409 detectordata = NULL;
410
411 SCIP_CALL(
414 freeVarclass, initVarclass, exitVarclass, propagatePartialdecVarclass, finishPartialdecVarclass, detectorPostprocessPartialdecVarclass, setParamAggressiveVarclass, setParamDefaultVarclass, setParamFastVarclass));
415
416 /**@todo add varclass detector parameters */
417
418 const char* name = DEC_NAME;
419 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/maxnclasses", name);
420 SCIP_CALL( SCIPaddIntParam(origprob, setstr, "maximum number of classes ", NULL, FALSE, DEFAULT_MAXIMUMNCLASSES, 1, INT_MAX, NULL, NULL ) );
421
422 return SCIP_OKAY;
423}
class storing partialdecs and the problem matrix
class storing (potentially incomplete) decompositions
class representing a partition of a set of variables
GCG_EXPORT int getNClasses()
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 void fixVarToLinking(int var)
adds a variable to the linking variables
GCG_EXPORT void fixVarToMaster(int var)
adds a variable to the master variables
GCG_EXPORT void setVarPartitionStatistics(int detectorchainindex, VarPartition *partition, std::vector< int > &varclasseslinking, std::vector< int > &varclassesmaster)
registers statistics for a used varpartition
GCG_EXPORT bool sort()
sorts the vars and conss data structures by their indices
GCG_EXPORT std::vector< int > & getOpenvars()
Gets vector containing variables not assigned yet.
GCG_EXPORT int getClassOfVar(int varindex)
GCG_EXPORT VAR_DECOMPINFO getClassDecompInfo(int classindex)
GCG_EXPORT std::vector< std::vector< int > > getAllSubsets(bool all, bool linking, bool master, bool block)
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)),)
constraint handler for structure detection
#define DEC_MAXCALLROUND
#define DEC_MINCALLROUNDORIGINAL
#define detectorPostprocessPartialdecVarclass
#define finishPartialdecVarclass
#define DEC_ENABLEDFINISHING
#define DEC_ENABLED
#define FAST_MAXIMUMNCLASSES
#define SET_MULTIPLEFORSIZETRANSF
static GCG_DECL_PROPAGATEPARTIALDEC(propagatePartialdecVarclass)
static GCG_DECL_SETPARAMAGGRESSIVE(setParamAggressiveVarclass)
#define DEFAULT_MAXIMUMNCLASSES
#define DEC_DECCHAR
#define DEC_NAME
static GCG_DECL_SETPARAMFAST(setParamFastVarclass)
#define DEC_MINCALLROUND
#define DEC_ENABLEDPOSTPROCESSING
SCIP_RETCODE GCGincludeDetectorVarclass(GCG *gcg)
#define freeVarclass
#define initVarclass
#define AGGRESSIVE_MAXIMUMNCLASSES
#define DEC_DESC
#define DEC_FREQCALLROUND
#define DEC_PRIORITY
#define DEC_USEFULRECALL
#define DEC_SKIP
#define DEC_MAXCALLROUNDORIGINAL
#define DEC_FREQCALLROUNDORIGINAL
#define exitVarclass
static GCG_DECL_SETPARAMDEFAULT(setParamDefaultVarclass)
varclass detector
#define SCIP_MAXSTRLEN
#define SCIP_Real
#define MAX(x, y)
#define SCIP_CALL_ABORT(x)
#define SCIP_CALL(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,...)
#define NULL
struct gcg::subset subset
various SCIP helper methods
Definition: struct_gcg.h:55
SCIP_VERBLEVEL_DIALOG
SCIP_DIDNOTFIND
SCIP_SUCCESS
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE
SCIP_STAGE_PROBLEM