GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
dec_postprocess.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_postprocess.cpp
30 *
31 * @brief checks if there are master constraints that can be assigned to one block (without any other changes)
32 * @author Michael Bastubbe
33 */
34
35/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
36
37#include "gcg/dec_postprocess.h"
38#include "gcg/cons_decomp.h"
39#include "gcg/gcg.h"
42#include "scip/scip.h"
43#include "gcg/scip_misc.h"
44#include "scip/clock.h"
45#include <iostream>
46#include <algorithm>
47#include <vector>
48#include <queue>
49
50/* constraint handler properties */
51#define DEC_NAME "postprocess" /**< name of detector */
52#define DEC_DESC "detector postprocess" /**< description of detector*/
53#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 */
54#define DEC_MAXCALLROUND INT_MAX /**< last round the detector gets called */
55#define DEC_MINCALLROUND 0 /**< first round the detector gets called */
56#define DEC_PRIORITY 1000000 /**< priority of the constraint handler for separation */
57#define DEC_FREQCALLROUNDORIGINAL 1 /**< frequency the detector gets called in detection loop while detecting the original problem */
58#define DEC_MAXCALLROUNDORIGINAL INT_MAX /**< last round the detector gets called while detecting the original problem */
59#define DEC_MINCALLROUNDORIGINAL 0 /**< first round the detector gets called while detecting the original problem */
60#define DEC_DECCHAR 'p' /**< display character of detector */
61#define DEC_ENABLED FALSE /**< should the detection be enabled */
62#define DEC_ENABLEDFINISHING FALSE /**< should the finishing be enabled */
63#define DEC_ENABLEDPOSTPROCESSING TRUE /**< should the postprocessing be enabled */
64#define DEC_SKIP FALSE /**< should detector be skipped if other detectors found decompositions */
65#define DEC_USEFULRECALL FALSE /**< is it useful to call this detector on a descendant of the propagated partialdec */
66#define DEFAULT_USECONSSADJ TRUE
67/*
68 * Data structures
69 */
70
71/** @todo fill in the necessary detector data */
72
73/** detector handler data */
75{
77};
78
79
80/*
81 * Local methods
82 */
83
84/* put your local methods here, and declare them static */
85
86
87/*
88 * detector callback methods
89 */
90
91/** destructor of detector to free user data (called when GCG is exiting) */
92/** destructor of detector to free detector data (called when SCIP is exiting) */
93static
94GCG_DECL_FREEDETECTOR(freePostprocess)
95{ /*lint --e{715}*/
96 GCG_DETECTORDATA *detectordata;
97 assert(detector != NULL);
98
99 assert(strcmp(GCGdetectorGetName(detector), DEC_NAME) == 0);
100
101 detectordata = GCGdetectorGetData(detector);
102 assert(detectordata != NULL);
103
104 SCIPfreeMemory(GCGgetOrigprob(gcg), &detectordata);
105
106 return SCIP_OKAY;
107}
108
109
110
111
112/** destructor of detector to free detector data (called before the solving process begins) */
113#define exitPostprocess NULL
114
115/** detection initialization function of detector (called before solving is about to begin) */
116#define initPostprocess NULL
117
118#define propagatePartialdecPostprocess NULL
119#define finishPartialdecPostprocess NULL
120
121static
122GCG_DECL_POSTPROCESSPARTIALDEC(postprocessPartialdecPostprocess)
123{
126
127 assert(partialdecdetectiondata->workonpartialdec->isComplete());
128
129 SCIP_CLOCK* temporaryClock;
130 SCIP_CALL_ABORT( SCIPcreateClock(origprob, &temporaryClock) );
131 SCIP_CALL_ABORT( SCIPstartClock(origprob, temporaryClock) );
132 char decinfo[SCIP_MAXSTRLEN];
134 SCIP_Bool byconssadj;
135
136 gcg::PARTIALDECOMP* partialdec = partialdecdetectiondata->workonpartialdec;
137 gcg::DETPROBDATA* detprobdata = partialdecdetectiondata->detprobdata;
138 assert(partialdecdetectiondata->workonpartialdec->getDetprobdata() == detprobdata);
139
140 SCIPgetBoolParam(origprob, "detection/detectors/postprocess/useconssadj", &byconssadj);
141
142 if ( byconssadj && !detprobdata->isConssAdjInitialized() )
143 detprobdata->createConssAdjacency();
144
145 //complete the partialdec by bfs
146 if ( byconssadj )
147 {
148 success = FALSE;
149 std::vector<int> constoreassign;
150 std::vector<int> blockforconstoreassign;
151
152 partialdec->sort();
153
154 std::vector<int> blockforvar(partialdec->getNVars(), -1 );
155
156 for( int b = 0; b < partialdec->getNBlocks(); ++b )
157 {
158 for( size_t j = 0; j < (size_t) partialdec->getNVarsForBlock(b); ++j )
159 {
160 blockforvar[partialdec->getVarsForBlock(b)[j]] = b;
161 }
162 }
163
164 for( int mc = 0; mc < partialdec->getNMasterconss(); ++mc )
165 {
166 int masterconsid = partialdec->getMasterconss()[mc];
167 int hittenblock = -1;
168
169 SCIP_Bool lockedcons = FALSE;
170
171 for( int var = 0; var < detprobdata->getNVarsForCons(masterconsid); ++var )
172 {
173 int varid = detprobdata->getVarsForCons(masterconsid)[var];
174 /* do not reassign cons that contain static master vars or stairlinking vars */
175 /* @todo: we could check if cons can be moved without destroying the staircase structure*/
176 if( partialdec->isVarMastervar(varid) || partialdec->isVarStairlinkingvar(varid) )
177 {
178 lockedcons = TRUE;
179 break;
180 }
181
182 if ( blockforvar[varid] != -1 )
183 {
184 if( hittenblock == -1 )
185 hittenblock = blockforvar[varid];
186 else if( hittenblock != blockforvar[varid] )
187 {
188 lockedcons = TRUE;
189 break;
190 }
191 }
192 }
193
194 if( lockedcons )
195 continue;
196
197 if ( hittenblock != -1 )
198 {
199 constoreassign.push_back(masterconsid);
200 blockforconstoreassign.push_back(hittenblock);
201 }
202 }
203
204 for( size_t i = 0; i < constoreassign.size() ; ++i )
205 {
206 assert(partialdec->isConsMastercons(constoreassign[i]));
207 partialdec->removeMastercons(constoreassign[i]);
208 partialdec->setConsToBlock(constoreassign[i], blockforconstoreassign[i]);
209 }
210
211 if( !constoreassign.empty() )
212 success = TRUE;
213
214 partialdec->prepare();
215 }
216 else
217 success = FALSE;
218
219 SCIP_CALL_ABORT( SCIPstopClock(origprob, temporaryClock ) );
220
221 assert(partialdec->checkConsistency());
222
223 if ( !success )
224 {
225 partialdecdetectiondata->nnewpartialdecs = 0;
227 }
228 else
229 {
230 partialdecdetectiondata->detectiontime = SCIPgetClockTime(origprob, temporaryClock);
231 SCIP_CALL( SCIPallocMemoryArray(origprob, &(partialdecdetectiondata->newpartialdecs), 1) );
232 partialdecdetectiondata->newpartialdecs[0] = partialdec;
233 partialdecdetectiondata->nnewpartialdecs = 1;
234 (void) SCIPsnprintf(decinfo, SCIP_MAXSTRLEN, "postprocess");
235 partialdecdetectiondata->newpartialdecs[0]->addDetectorChainInfo(decinfo);
236 partialdecdetectiondata->newpartialdecs[0]->addClockTime(SCIPgetClockTime(origprob, temporaryClock));
237 // we used the provided partialdec -> prevent deletion
238 partialdecdetectiondata->workonpartialdec = NULL;
240 }
241
242 SCIP_CALL_ABORT( SCIPfreeClock(origprob, &temporaryClock) );
243
244 return SCIP_OKAY;
245}
246
247
248static
249GCG_DECL_SETPARAMAGGRESSIVE(setParamAggressivePostprocess)
250{
251 char setstr[SCIP_MAXSTRLEN];
252 const char* name = GCGdetectorGetName(detector);
254
255 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
257
258 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
260
261 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/postprocessingenabled", name);
263
264
265 return SCIP_OKAY;
266
267}
268
269
270static
271GCG_DECL_SETPARAMDEFAULT(setParamDefaultPostprocess)
272{
273 char setstr[SCIP_MAXSTRLEN];
274 const char* name = GCGdetectorGetName(detector);
276
277 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
279
280 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
282
283 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/postprocessingenabled", name);
285
286
287 return SCIP_OKAY;
288
289}
290
291static
292GCG_DECL_SETPARAMFAST(setParamFastPostprocess)
293{
294 char setstr[SCIP_MAXSTRLEN];
295 const char* name = GCGdetectorGetName(detector);
297
298 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
300
301 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
303
304 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/postprocessingenabled", name);
306
307
308 return SCIP_OKAY;
309
310}
311
312
313
314/*
315 * detector specific interface methods
316 */
317
318/** creates the handler for postprocess detector and includes it in SCIP */
320 GCG* gcg /**< GCG data structure */
321 )
322{
323 GCG_DETECTORDATA* detectordata;
325 assert(origprob != NULL);
326
327 /**@todo create postprocess detector data here*/
328 detectordata = NULL;
329 SCIP_CALL( SCIPallocMemory(origprob, &detectordata) );
330 assert(detectordata != NULL);
331
332 detectordata->useconssadj = TRUE;
333
336 postprocessPartialdecPostprocess, setParamAggressivePostprocess, setParamDefaultPostprocess, setParamFastPostprocess) );
337
338 /* add consname detector parameters */
339 /**@todo add postprocess detector parameters */
340 SCIP_CALL( SCIPaddBoolParam(origprob, "detection/detectors/postprocess/useconssadj", "should the constraint adjacency be used", &detectordata->useconssadj, FALSE, DEFAULT_USECONSSADJ, NULL, NULL) );
341
342
343 return SCIP_OKAY;
344}
SCIP_VAR ** b
class storing partialdecs and the problem matrix
class storing (potentially incomplete) decompositions
GCG_EXPORT int getNVarsForCons(int consIndex)
returns the number of variables for a given constraint
GCG_EXPORT void createConssAdjacency()
create the constraint adjacency datastructure that is used (if created) for some methods to faster ac...
GCG_EXPORT SCIP_Bool isConssAdjInitialized()
determines whether or not the constraint-constraint adjacency data structure is initilized
GCG_EXPORT std::vector< int > & getVarsForCons(int consIndex)
returns the variable indices of the coefficient matrix for a constraint
class to manage partial decompositions
GCG_EXPORT int getNMasterconss()
Gets size of the vector containing master conss.
GCG_EXPORT bool isConsMastercons(int cons)
Gets whether the cons is a master cons.
GCG_EXPORT bool checkConsistency()
Checks whether the assignments in the partialdec are consistent.
GCG_EXPORT std::vector< int > & getMasterconss()
Gets array containing all master conss indices.
GCG_EXPORT void removeMastercons(int consid)
removes the given cons from master
GCG_EXPORT bool isVarMastervar(int var)
Checks whether the var is a master var.
GCG_EXPORT int getNVarsForBlock(int block)
Gets size of the vector containing vars assigned to a block.
GCG_EXPORT int getNVars()
Gets number of vars.
GCG_EXPORT void setConsToBlock(int consToBlock, int block)
adds a constraint to a block, does not delete this cons from list of open conss
GCG_EXPORT bool sort()
sorts the vars and conss data structures by their indices
GCG_EXPORT bool isVarStairlinkingvar(int var)
Checks whether the var is a stairlinking var.
GCG_EXPORT std::vector< int > & getVarsForBlock(int block)
Gets array containing vars of a block.
GCG_EXPORT int getNBlocks()
Gets the number of blocks.
const char * GCGdetectorGetName(GCG_DETECTOR *detector)
returns the name of the provided detector
GCG_DETECTORDATA * GCGdetectorGetData(GCG_DETECTOR *detector)
returns the data 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 DEFAULT_USECONSSADJ
#define finishPartialdecPostprocess
SCIP_RETCODE GCGincludeDetectorPostprocess(GCG *gcg)
#define propagatePartialdecPostprocess
#define exitPostprocess
#define initPostprocess
postprocess detector
#define SCIP_MAXSTRLEN
#define SCIP_Bool
#define SCIP_CALL_ABORT(x)
#define TRUE
#define FALSE
GCG interface methods.
SCIP * GCGgetOrigprob(GCG *gcg)
Definition gcg.cpp:87
SCIP_RETCODE SCIPgetBoolParam(SCIP *scip, const char *name, SCIP_Bool *value)
SCIP_RETCODE SCIPaddBoolParam(SCIP *scip, const char *name, const char *desc, SCIP_Bool *valueptr, SCIP_Bool isadvanced, SCIP_Bool defaultvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
SCIP_RETCODE SCIPsetBoolParam(SCIP *scip, const char *name, SCIP_Bool value)
#define SCIPallocMemoryArray(scip, ptr, num)
#define SCIPallocMemory(scip, ptr)
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)
int SCIPsnprintf(char *t, int len, const char *s,...)
SCIPfreeMemory(GCGgetOrigprob(gcg), &divingdata)
static assert(heur !=NULL)
SCIP_CALL(SCIPgetExternBranchCands(origprob, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL))
SCIP * origprob
* result
SCIP_VAR * var
int j
SCIP_Bool success
int i
#define NULL
various SCIP helper methods
struct SCIP_Clock SCIP_CLOCK
#define GCG_DECL_FREEDETECTOR(x)
#define GCG_DECL_SETPARAMFAST(x)
#define GCG_DECL_SETPARAMDEFAULT(x)
#define GCG_DECL_SETPARAMAGGRESSIVE(x)
#define GCG_DECL_POSTPROCESSPARTIALDEC(x)
struct GCG_DetectorData GCG_DETECTORDATA
struct Gcg GCG
Definition type_gcg.h:44
SCIP_DIDNOTFIND
SCIP_SUCCESS
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP