Scippy

GCG

Branch-and-Price & Column Generation for Everyone

dec_constype.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_constype.cpp
30 *
31 * @brief detector constype (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_constype.h"
38#include "gcg/cons_decomp.h"
41#include "gcg/gcg.h"
42#include "scip/cons_setppc.h"
43#include "scip/scip.h"
44#include "gcg/scip_misc.h"
45#include "scip/clock.h"
46
47#include <iostream>
48
49/* constraint handler properties */
50#define DEC_NAME "constype" /**< name of detector */
51#define DEC_DESC "detector constype" /**< description of detector*/
52#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 */
53#define DEC_MAXCALLROUND 0 /** last round the detector gets called */
54#define DEC_MINCALLROUND 0 /** first round the detector gets called */
55#define DEC_FREQCALLROUNDORIGINAL 1 /** frequency the detector gets called in detection loop while detecting the original problem */
56#define DEC_MAXCALLROUNDORIGINAL 0 /** last round the detector gets called while detecting the original problem */
57#define DEC_MINCALLROUNDORIGINAL 0 /** first round the detector gets called while detecting the original problem */
58#define DEC_PRIORITY 0 /**< priority of the constraint handler for separation */
59#define DEC_DECCHAR 't' /**< display character of detector */
60#define DEC_ENABLED FALSE /**< should the detection be enabled */
61#define DEC_ENABLEDFINISHING FALSE /**< should the finishing be enabled */
62#define DEC_ENABLEDPOSTPROCESSING FALSE /**< should the finishing be enabled */
63#define DEC_SKIP FALSE /**< should detector be skipped if other detectors found decompositions */
64#define DEC_USEFULRECALL FALSE /**< is it useful to call this detector on a descendant of the propagated partialdec */
65/*
66 * Data structures
67 */
68
69/** @todo fill in the necessary detector data */
70
71/** detector handler data */
73{
74};
75
76/*
77 * Local methods
78 */
79
80/* put your local methods here, and declare them static */
81
82/** method to enumerate all subsets */
83std::vector< std::vector<int> > getSubsets(std::vector<int> set)
84{
85 std::vector< std::vector<int> > subset;
86 std::vector<int> empty;
87 subset.push_back( empty );
88
89 for ( size_t i = 0; i < set.size(); ++i )
90 {
91 std::vector< std::vector<int> > subsetTemp = subset;
92
93 for (size_t j = 0; j < subsetTemp.size(); ++j)
94 subsetTemp[j].push_back( set[i] );
95
96 for (size_t j = 0; j < subsetTemp.size(); ++j)
97 subset.push_back( subsetTemp[j] );
98 }
99 return subset;
100}
101
102/*
103 * detector callback methods
104 */
105
106/** destructor of detector to free user data (called when GCG is exiting) */
107#define freeConstype NULL
108
109/** destructor of detector to free detector data (called before the solving process begins) */
110#define exitConstype NULL
111
112/** detection initialization function of detector (called before solving is about to begin) */
113#define initConstype NULL
114
115static GCG_DECL_PROPAGATEPARTIALDEC(propagatePartialdecConstype)
116{
117 *result = SCIP_DIDNOTFIND;
118 char decinfo[SCIP_MAXSTRLEN];
119 SCIP* origprob = GCGgetOrigprob(gcg);
120
121 SCIP_CLOCK* temporaryClock;
122 SCIP_CALL_ABORT( SCIPcreateClock(origprob, &temporaryClock) );
123 SCIP_CALL_ABORT( SCIPstartClock(origprob, temporaryClock) );
124
125 SCIP_CONS* cons;
126
127 int partialdecCounter = 0;
128 gcg::PARTIALDECOMP* partialdecOrig;
129 gcg::PARTIALDECOMP* partialdec;
130
131 std::vector<consType> foundConstypes(0);
132 std::vector<int> constypesIndices(0);
133
134 partialdecOrig = partialdecdetectiondata->workonpartialdec;
135
136 for( int i = 0; i < partialdecOrig->getNOpenconss(); ++i)
137 {
138 cons = partialdecdetectiondata->detprobdata->getCons(partialdecOrig->getOpenconss()[i]);
139 consType cT = GCGconsGetType(origprob, cons);
140
141 /* find constype or not */
142 std::vector<consType>::const_iterator constypeIter = foundConstypes.begin();
143 for(; constypeIter != foundConstypes.end(); ++constypeIter)
144 {
145 if(*constypeIter == cT)
146 break;
147 }
148
149 if( constypeIter == foundConstypes.end() )
150 {
151 foundConstypes.push_back(GCGconsGetType(origprob, cons) );
152 }
153 }
154
155 for(size_t i = 0; i < foundConstypes.size(); ++i)
156 {
157 constypesIndices.push_back((int) i);
158 }
159
160 std::vector< std::vector<int> > subsetsOfConstypes = getSubsets(constypesIndices);
161
162 SCIP_CALL( SCIPallocMemoryArray(origprob, &(partialdecdetectiondata->newpartialdecs), subsetsOfConstypes.size() - 1) );
163 partialdecdetectiondata->nnewpartialdecs = (int) subsetsOfConstypes.size() - 1;
164
165 for(size_t subset = 0; subset < subsetsOfConstypes.size(); ++subset)
166 {
167 if(subsetsOfConstypes[subset].size() == 0)
168 continue;
169
170 partialdec = new gcg::PARTIALDECOMP(partialdecOrig);
171 /* set open cons that have type of the current subset to Master */
172 auto& openconss = partialdec->getOpenconss();
173 for( auto itr = openconss.cbegin(); itr != openconss.cend(); )
174 {
175 cons = partialdecdetectiondata->detprobdata->getCons(*itr);
176 bool found = false;
177 for(size_t constypeId = 0; constypeId < subsetsOfConstypes[subset].size(); ++constypeId )
178 {
179 if( GCGconsGetType(origprob, cons) == foundConstypes[subsetsOfConstypes[subset][constypeId]] )
180 {
181 itr = partialdec->fixConsToMaster(itr);
182 found = true;
183 break;
184 }
185 }
186 if( !found )
187 {
188 ++itr;
189 }
190 }
191 partialdec->sort();
192 (void) SCIPsnprintf(decinfo, SCIP_MAXSTRLEN, "constype-%llu", subset);
193 partialdec->addDetectorChainInfo(decinfo);
194 partialdecdetectiondata->newpartialdecs[partialdecCounter] = partialdec;
195 partialdecCounter++;
196 }
197
198 SCIP_CALL_ABORT( SCIPstopClock(origprob, temporaryClock) );
199 partialdecdetectiondata->detectiontime = SCIPgetClockTime(origprob, temporaryClock);
200 for( int s = 0; s < partialdecdetectiondata->nnewpartialdecs; ++s )
201 {
202 partialdecdetectiondata->newpartialdecs[s]->addClockTime(partialdecdetectiondata->detectiontime / partialdecdetectiondata->nnewpartialdecs);
203 }
204 SCIP_CALL_ABORT(SCIPfreeClock(origprob, &temporaryClock) );
205
206 *result = SCIP_SUCCESS;
207
208 return SCIP_OKAY;
209}
210
211#define finishPartialdecConstype NULL
212#define detectorPostprocessPartialdecConstype NULL
213static
214GCG_DECL_SETPARAMAGGRESSIVE(setParamAggressiveConstype)
215{
216 char setstr[SCIP_MAXSTRLEN];
217 const char* name = GCGdetectorGetName(detector);
218 SCIP* origprob = GCGgetOrigprob(gcg);
219
220 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
221 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, FALSE) );
222
223 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
224 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, FALSE ) );
225
226 return SCIP_OKAY;
227}
228
229
230static
231GCG_DECL_SETPARAMDEFAULT(setParamDefaultConstype)
232{
233 char setstr[SCIP_MAXSTRLEN];
234 const char* name = GCGdetectorGetName(detector);
235 SCIP* origprob = GCGgetOrigprob(gcg);
236
237 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
238 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, DEC_ENABLED) );
239
240 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
241 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, DEC_ENABLEDFINISHING ) );
242
243 return SCIP_OKAY;
244}
245
246static
247GCG_DECL_SETPARAMFAST(setParamFastConstype)
248{
249 char setstr[SCIP_MAXSTRLEN];
250 const char* name = GCGdetectorGetName(detector);
251 SCIP* origprob = GCGgetOrigprob(gcg);
252
253 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
254 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, FALSE) );
255
256 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
257 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, FALSE ) );
258
259
260 return SCIP_OKAY;
261
262}
263
264
265/*
266 * detector specific interface methods
267 */
268
269/** creates the handler for constype detector and includes it in SCIP */
271 GCG* gcg /**< SCIP data structure */
272)
273{
274 GCG_DETECTORDATA* detectordata;
275
276 /**@todo create constype detector data here*/
277 detectordata = NULL;
278
279 SCIP_CALL(
282 freeConstype, initConstype, exitConstype, propagatePartialdecConstype, finishPartialdecConstype, detectorPostprocessPartialdecConstype, setParamAggressiveConstype, setParamDefaultConstype, setParamFastConstype));
283
284 /**@todo add constype detector parameters */
285
286 return SCIP_OKAY;
287}
class storing partialdecs and the problem matrix
class storing (potentially incomplete) decompositions
class to manage partial decompositions
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 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 initConstype
#define freeConstype
#define finishPartialdecConstype
#define DEC_ENABLEDFINISHING
#define DEC_ENABLED
static GCG_DECL_SETPARAMDEFAULT(setParamDefaultConstype)
SCIP_RETCODE GCGincludeDetectorConstype(GCG *gcg)
#define detectorPostprocessPartialdecConstype
static GCG_DECL_PROPAGATEPARTIALDEC(propagatePartialdecConstype)
#define DEC_DECCHAR
#define DEC_NAME
std::vector< std::vector< int > > getSubsets(std::vector< int > set)
#define DEC_MINCALLROUND
#define DEC_ENABLEDPOSTPROCESSING
static GCG_DECL_SETPARAMAGGRESSIVE(setParamAggressiveConstype)
#define DEC_DESC
#define DEC_FREQCALLROUND
#define DEC_PRIORITY
#define DEC_USEFULRECALL
#define DEC_SKIP
#define DEC_MAXCALLROUNDORIGINAL
#define exitConstype
#define DEC_FREQCALLROUNDORIGINAL
static GCG_DECL_SETPARAMFAST(setParamFastConstype)
constype detector
#define SCIP_MAXSTRLEN
#define SCIP_CALL_ABORT(x)
#define SCIP_CALL(x)
#define FALSE
GCG interface methods.
SCIP * GCGgetOrigprob(GCG *gcg)
Definition: gcg.cpp:87
consType GCGconsGetType(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:52
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)
int SCIPsnprintf(char *t, int len, const char *s,...)
#define NULL
struct gcg::subset subset
various SCIP helper methods
consType
Definition: scip_misc.h:49
Definition: struct_gcg.h:55
int size
SCIP_DIDNOTFIND
SCIP_SUCCESS
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE