Scippy

GCG

Branch-and-Price & Column Generation for Everyone

dec_generalmastersetcover.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_generalmastersetcover.cpp
30 *
31 * @brief detector for set covering constraints
32 * @author Michael Bastubbe
33 *
34 * This detector sets the following constraints to master:
35 * - set covering constraints
36 * - logical OR constraints
37 * - constraints with infinity rhs and nonnegative lhs
38 *
39 */
40
41/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
42
44#include "gcg/cons_decomp.h"
47#include "gcg/gcg.h"
48#include "scip/cons_setppc.h"
49#include "scip/scip.h"
50#include "gcg/scip_misc.h"
51#include "scip/clock.h"
52
53#include <iostream>
54
55/* constraint handler properties */
56#define DEC_NAME "generalmastersetcover" /**< name of detector */
57#define DEC_DESC "detector generalmastersetcover" /**< description of detector*/
58#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 */
59#define DEC_MAXCALLROUND 0 /** last round the detector gets called */
60#define DEC_MINCALLROUND 0 /** first round the detector gets called */
61#define DEC_FREQCALLROUNDORIGINAL 1 /** frequency the detector gets called in detection loop while detecting the original problem */
62#define DEC_MAXCALLROUNDORIGINAL 0 /** last round the detector gets called while detecting the original problem */
63#define DEC_MINCALLROUNDORIGINAL 0 /** first round the detector gets called while detecting the original problem */
64#define DEC_PRIORITY 0 /**< priority of the constraint handler for separation */
65#define DEC_DECCHAR '?' /**< display character of detector */
66#define DEC_ENABLED TRUE /**< should the detection be enabled */
67#define DEC_ENABLEDFINISHING FALSE /**< should the finishing be enabled */
68#define DEC_ENABLEDPOSTPROCESSING FALSE /**< should the postprocessing be enabled */
69#define DEC_SKIP FALSE /**< should detector be skipped if other detectors found decompositions */
70#define DEC_USEFULRECALL FALSE /**< is it useful to call this detector on a descendant of the propagated partialdec */
71
72/*
73 * Data structures
74 */
75
76/** @todo fill in the necessary detector data */
77
78/** detector handler data */
80{
81};
82
83/*
84 * Local methods
85 */
86
87/* put your local methods here, and declare them static */
88
89/*
90 * detector callback methods
91 */
92
93/** destructor of detector to free user data (called when GCG is exiting) */
94#define freeGeneralmastersetcover NULL
95
96/** destructor of detector to free detector data (called before the solving process begins) */
97#define exitGeneralmastersetcover NULL
98
99/** detection initialization function of detector (called before solving is about to begin) */
100#define initGeneralmastersetcover NULL
101
102static GCG_DECL_PROPAGATEPARTIALDEC(propagatePartialdecGeneralmastersetcover)
103{
104 *result = SCIP_DIDNOTFIND;
105 SCIP* origprob = GCGgetOrigprob(gcg);
106 char decinfo[SCIP_MAXSTRLEN];
107 SCIP_CLOCK* temporaryClock;
108 SCIP_CALL_ABORT(SCIPcreateClock(origprob, &temporaryClock) );
109 SCIP_CALL_ABORT( SCIPstartClock(origprob, temporaryClock) );
110
111 SCIP_CONS* cons;
112 SCIP_VAR** vars;
113 SCIP_Real* vals;
114 int nvars;
115 bool relevant = true;
116
117
118 gcg::PARTIALDECOMP* partialdec = partialdecdetectiondata->workonpartialdec;
119 auto& openconss = partialdec->getOpenconss();
120 for( auto itr = openconss.cbegin(); itr != openconss.cend(); )
121 {
122 bool found = false;
123 cons = partialdecdetectiondata->detprobdata->getCons(*itr);
124
125 /* set open setcovering and logicor constraints to master */
126 if( GCGconsGetType(origprob, cons) == setcovering || GCGconsGetType(origprob, cons) == logicor )
127 {
128 itr = partialdec->fixConsToMaster(itr);
129 found = true;
130 }
131 /* set constraints with infinity rhs and nonnegative lhs to master */
132 else if(GCGconsGetType(origprob, cons) != logicor && GCGconsGetType(origprob, cons) != setpacking && GCGconsGetType(origprob, cons) != setpartitioning )
133 {
134 nvars = GCGconsGetNVars(origprob, cons);
135 vars = NULL;
136 vals = NULL;
137 if( !SCIPisInfinity(origprob, GCGconsGetRhs(origprob, cons)) )
138 relevant = false;
139 if( SCIPisNegative(origprob, GCGconsGetLhs(origprob, cons)) )
140 relevant = false;
141 if( nvars > 0 )
142 {
143 SCIP_CALL( SCIPallocMemoryArray(origprob, &vars, nvars) );
144 SCIP_CALL( SCIPallocMemoryArray(origprob, &vals, nvars) );
145 SCIP_CALL( GCGconsGetVars(origprob, cons, vars, nvars) );
146 SCIP_CALL( GCGconsGetVals(origprob, cons, vals, nvars) );
147 }
148 for( int j = 0; j < nvars && relevant; ++j )
149 {
150 assert(vars != NULL);
151 assert(vals != NULL);
152
153 if( !SCIPvarIsIntegral(vars[j]) && !SCIPvarIsBinary(vars[j]) )
154 {
155 SCIPdebugPrintf("(%s is not integral) ", SCIPvarGetName(vars[j]) );
156 relevant = FALSE;
157 }
158 if( !SCIPisEQ(origprob, vals[j], 1.0) )
159 {
160 SCIPdebugPrintf("(coeff for var %s is %.2f != 1.0) ", SCIPvarGetName(vars[j]), vals[j] );
161 relevant = FALSE;
162 }
163 }
164 SCIPfreeMemoryArrayNull(origprob, &vals);
165 SCIPfreeMemoryArrayNull(origprob, &vars);
166
167 if(relevant)
168 {
169 itr = partialdec->fixConsToMaster(itr);
170 found = true;
171 }
172 }
173 if( !found )
174 {
175 ++itr;
176 }
177 }
178
179 partialdec->sort();
180 SCIP_CALL_ABORT( SCIPstopClock(origprob, temporaryClock) );
181
182 partialdecdetectiondata->detectiontime = SCIPgetClockTime(origprob, temporaryClock);
183 SCIP_CALL( SCIPallocMemoryArray(origprob, &(partialdecdetectiondata->newpartialdecs), 1) );
184 partialdecdetectiondata->newpartialdecs[0] = partialdec;
185 partialdecdetectiondata->nnewpartialdecs = 1;
186 (void) SCIPsnprintf(decinfo, SCIP_MAXSTRLEN, "genmastersetcover");
187 partialdecdetectiondata->newpartialdecs[0]->addDetectorChainInfo(decinfo);
188 partialdecdetectiondata->newpartialdecs[0]->addClockTime(SCIPgetClockTime(origprob, temporaryClock));
189 // we used the provided partialdec -> prevent deletion
190 partialdecdetectiondata->workonpartialdec = NULL;
191 SCIP_CALL_ABORT(SCIPfreeClock(origprob, &temporaryClock) );
192
193 *result = SCIP_SUCCESS;
194
195 return SCIP_OKAY;
196}
197
198#define finishPartialdecGeneralmastersetcover NULL
199#define detectorPostprocessPartialdecGeneralmastersetcover NULL
200
201static
202GCG_DECL_SETPARAMAGGRESSIVE(setParamAggressiveGeneralmastersetcover)
203{
204 char setstr[SCIP_MAXSTRLEN];
205 const char* name = GCGdetectorGetName(detector);
206 int newval;
207 SCIP* origprob = GCGgetOrigprob(gcg);
208
209 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
210 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, TRUE) );
211
212 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
213 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, FALSE ) );
214
215 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/maxcallround", name);
216 SCIP_CALL( SCIPgetIntParam(origprob, setstr, &newval) );
217 ++newval;
218 SCIP_CALL( SCIPsetIntParam(origprob, setstr, newval ) );
219 SCIPinfoMessage(origprob, NULL, "After Setting %s = %d\n", setstr, newval);
220
221 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/origmaxcallround", name);
222 SCIP_CALL( SCIPgetIntParam(origprob, setstr, &newval) );
223 ++newval;
224 SCIP_CALL( SCIPsetIntParam(origprob, setstr, newval ) );
225 SCIPinfoMessage(origprob, NULL, "%s = %d\n", setstr, newval);
226
227 return SCIP_OKAY;
228}
229
230
231static
232GCG_DECL_SETPARAMDEFAULT(setParamDefaultGeneralmastersetcover)
233{
234 char setstr[SCIP_MAXSTRLEN];
235 const char* name = GCGdetectorGetName(detector);
236 SCIP* origprob = GCGgetOrigprob(gcg);
237
238 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
239 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, DEC_ENABLED) );
240
241 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
242 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, DEC_ENABLEDFINISHING ) );
243
244 return SCIP_OKAY;
245}
246
247static
248GCG_DECL_SETPARAMFAST(setParamFastGeneralmastersetcover)
249{
250 char setstr[SCIP_MAXSTRLEN];
251 const char* name = GCGdetectorGetName(detector);
252 SCIP* origprob = GCGgetOrigprob(gcg);
253
254 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
255 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, FALSE) );
256
257 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
258 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, FALSE ) );
259
260 return SCIP_OKAY;
261}
262
263
264
265/*
266 * detector specific interface methods
267 */
268
269/** creates the handler for generalmastersetcover detector and includes it in SCIP */
271 GCG* gcg /**< GCG data structure */
272 )
273{
274 GCG_DETECTORDATA* detectordata;
275
276 /**@todo create generalmastersetcover detector data here*/
277 detectordata = NULL;
278
279 SCIP_CALL(
283 detectordata, freeGeneralmastersetcover, initGeneralmastersetcover, exitGeneralmastersetcover, propagatePartialdecGeneralmastersetcover, finishPartialdecGeneralmastersetcover, detectorPostprocessPartialdecGeneralmastersetcover, setParamAggressiveGeneralmastersetcover, setParamDefaultGeneralmastersetcover, setParamFastGeneralmastersetcover));
284
285 /**@todo add generalmastersetcover detector parameters */
286
287 return SCIP_OKAY;
288}
289
290
291
292
293
294
295
296
297
class storing partialdecs and the problem matrix
class storing (potentially incomplete) decompositions
class to manage partial decompositions
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)),)
constraint handler for structure detection
#define DEC_MAXCALLROUND
#define DEC_MINCALLROUNDORIGINAL
#define DEC_ENABLEDFINISHING
#define finishPartialdecGeneralmastersetcover
#define DEC_ENABLED
#define DEC_DECCHAR
#define DEC_NAME
#define exitGeneralmastersetcover
#define detectorPostprocessPartialdecGeneralmastersetcover
#define DEC_MINCALLROUND
#define initGeneralmastersetcover
SCIP_RETCODE GCGincludeDetectorGeneralmastersetcover(GCG *gcg)
#define DEC_ENABLEDPOSTPROCESSING
static GCG_DECL_SETPARAMAGGRESSIVE(setParamAggressiveGeneralmastersetcover)
#define freeGeneralmastersetcover
static GCG_DECL_PROPAGATEPARTIALDEC(propagatePartialdecGeneralmastersetcover)
static GCG_DECL_SETPARAMDEFAULT(setParamDefaultGeneralmastersetcover)
#define DEC_DESC
#define DEC_FREQCALLROUND
#define DEC_PRIORITY
#define DEC_USEFULRECALL
static GCG_DECL_SETPARAMFAST(setParamFastGeneralmastersetcover)
#define DEC_SKIP
#define DEC_MAXCALLROUNDORIGINAL
#define DEC_FREQCALLROUNDORIGINAL
generalmastersetcover detector
#define SCIP_MAXSTRLEN
#define SCIP_Real
#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_Real GCGconsGetRhs(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:109
consType GCGconsGetType(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:52
SCIP_RETCODE GCGconsGetVars(SCIP *scip, SCIP_CONS *cons, SCIP_VAR **vars, int nvars)
Definition: scip_misc.c:487
SCIP_Real GCGconsGetLhs(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:207
SCIP_RETCODE GCGconsGetVals(SCIP *scip, SCIP_CONS *cons, SCIP_Real *vals, int nvals)
Definition: scip_misc.c:618
int GCGconsGetNVars(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:433
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
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 SCIPfreeMemoryArrayNull(scip, ptr)
#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_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisNegative(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPvarIsBinary(SCIP_VAR *var)
const char * SCIPvarGetName(SCIP_VAR *var)
SCIP_Bool SCIPvarIsIntegral(SCIP_VAR *var)
int SCIPsnprintf(char *t, int len, const char *s,...)
#define NULL
#define SCIPdebugPrintf
various SCIP helper methods
@ setcovering
Definition: scip_misc.h:50
@ setpartitioning
Definition: scip_misc.h:50
@ setpacking
Definition: scip_misc.h:50
@ logicor
Definition: scip_misc.h:51
Definition: struct_gcg.h:55
SCIP_DIDNOTFIND
SCIP_SUCCESS
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE