GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
dec_neighborhoodmaster.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_neighborhoodmaster.cpp
30 *
31 * @brief detector neighborhoodmaster (This detector calculates cons-cons adjacency (if not already done), sorts constraints according size of neighborhood. Searching two consecutive constraints with largest size difference (according neighborhood size) in sorted constraints. All constraints having a larger neighborhood than the second one are assigned to the master)
32 * @author Michael Bastubbe
33 */
34
35/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
36
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 <sstream>
48
49#include <iostream>
50#include <algorithm>
51
52/**
53This detector calculates cons-cons adjacency (if not already done), and sorts constraints according size of neighborhood. Searching two consecutive constraints with largest size difference (according neighborhood size) in sorted constraints. All constraints having a larger neighborhood than the second one are assigned to the master
54*/
55
56/* constraint handler properties */
57#define DEC_NAME "neighborhoodmaster" /**< name of detector */
58#define DEC_DESC "detector neighborhoodmaster" /**< description of detector*/
59#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 */
60#define DEC_MAXCALLROUND 0 /** last round the detector gets called */
61#define DEC_MINCALLROUND 0 /** first round the detector gets called */
62#define DEC_FREQCALLROUNDORIGINAL 1 /** frequency the detector gets called in detection loop while detecting the original problem */
63#define DEC_MAXCALLROUNDORIGINAL INT_MAX /** last round the detector gets called while detecting the original problem */
64#define DEC_MINCALLROUNDORIGINAL 0 /** first round the detector gets called while detecting the original problem */
65#define DEC_PRIORITY 0 /**< priority of the constraint handler for separation */
66#define DEC_DECCHAR 'n' /**< display character of detector */
67#define DEC_ENABLED TRUE /**< should the detection be enabled */
68#define DEC_ENABLEDFINISHING FALSE /**< should the detection be enabled */
69#define DEC_ENABLEDPOSTPROCESSING FALSE /**< should the postprocessing be enabled */
70#define DEC_SKIP FALSE /**< should detector be skipped if other detectors found decompositions */
71#define DEC_USEFULRECALL FALSE /**< is it useful to call this detector on a descendant of the propagated partialdec */
72
73#define DEFAULT_MAXRATIO 0.2
74
75/*
76 * Data structures
77 */
78
79/** detector handler data */
81{
83};
84
85/*
86 * Local methods
87 */
88
89struct sort_pred {
90 bool operator()(const std::pair<int,int> &left, const std::pair<int,int> &right) {
91 return left.first > right.first;
92 }
93};
94
95/* put your local methods here, and declare them static */
96
97/*
98 * detector callback methods
99 */
100
101/** destructor of detector to free user data (called when GCG is exiting) */
102GCG_DECL_FREEDETECTOR(freeNeighborhoodmaster)
103{ /*lint --e{715}*/
104 GCG_DETECTORDATA* detectordata;
105
106 assert(detector != NULL);
107
108 assert(strcmp(GCGdetectorGetName(detector), DEC_NAME) == 0);
109
110 detectordata = GCGdetectorGetData(detector);
111 assert(detectordata != NULL);
112
113 SCIPfreeMemory(GCGgetOrigprob(gcg), &detectordata);
114
115 return SCIP_OKAY;
116}
117
118/** destructor of detector to free detector data (called before the solving process begins) */
119#define exitNeighborhoodmaster NULL
120
121/** detection initialization function of detector (called before solving is about to begin) */
122#define initNeighborhoodmaster NULL
123
124#define finishPartialdecNeighborhoodmaster NULL
125
126static GCG_DECL_PROPAGATEPARTIALDEC(propagatePartialdecNeighborhoodmaster)
127{
129 char decinfo[SCIP_MAXSTRLEN];
130 SCIP_CLOCK* temporaryClock;
131 gcg::DETPROBDATA* detprobdata;
132 gcg::PARTIALDECOMP* partialdec;
133 GCG_DetectorData* detectorData = GCGdetectorGetData(detector);
134 std::stringstream decdesc;
135 int maxdiff = -1;
136 int maxdiffindex = -1;
137 int lastindex = -1;
139
140 detprobdata = partialdecdetectiondata->detprobdata;
141 partialdec = partialdecdetectiondata->workonpartialdec;
142
143 if ( !detprobdata->isConssAdjInitialized() )
144 detprobdata->createConssAdjacency();
145
146 SCIP_CALL_ABORT( SCIPcreateClock(origprob, &temporaryClock) );
147 SCIP_CALL_ABORT( SCIPstartClock(origprob, temporaryClock) );
148
149 lastindex = (int) (detectorData->maxratio * partialdec->getNOpenconss());
150
151 /** fix open conss that have a) type of the current subset or b) decomp info ONLY_MASTER as master conss */
152 std::vector<std::pair<int,int>> neighborhoodsize;
153 neighborhoodsize.reserve(partialdec->getNOpenconss());
154 for( int opencons : partialdec->getOpenconss() )
155 {
156 neighborhoodsize.emplace_back(std::pair<int,int>(detprobdata->getNConssForCons(opencons), opencons));
157 }
158
159 std::sort(neighborhoodsize.begin(), neighborhoodsize.end(), sort_pred() );
160
161 for( int i = 0; i < lastindex && i < (int) neighborhoodsize.size() - 1; ++i )
162 {
163 if( maxdiff < neighborhoodsize[i].first - neighborhoodsize[i+1].first )
164 {
165 maxdiff = neighborhoodsize[i].first - neighborhoodsize[i+1].first;
166 maxdiffindex = i;
167 }
168 }
169
170 for( int i = 0; i <= maxdiffindex; ++i )
171 {
172 partialdec->fixConsToMaster(neighborhoodsize[i].second);
173 }
174
175 decdesc << "neighborhoodmaster" << "\\_" << maxdiffindex ;
176
177 partialdec->sort();
178 (void) SCIPsnprintf(decinfo, SCIP_MAXSTRLEN, decdesc.str().c_str());
179 partialdec->addDetectorChainInfo(decinfo);
180
181 SCIP_CALL_ABORT( SCIPstopClock(origprob, temporaryClock ) );
182
183 partialdecdetectiondata->detectiontime = SCIPgetClockTime(origprob, temporaryClock);
184
185 SCIP_CALL( SCIPallocMemoryArray(origprob, &(partialdecdetectiondata->newpartialdecs), 1) );
186 partialdecdetectiondata->nnewpartialdecs = 1;
187 partialdecdetectiondata->newpartialdecs[0] = partialdec;
188 partialdecdetectiondata->newpartialdecs[0]->addClockTime(SCIPgetClockTime(origprob, temporaryClock));
189 // we used the provided partialdec -> prevent deletion
190 partialdecdetectiondata->workonpartialdec = NULL;
191
192 SCIPverbMessage(origprob, SCIP_VERBLEVEL_HIGH, NULL, "dec_neighborhoodmaster found %d new partialdec \n", partialdecdetectiondata->nnewpartialdecs );
193
194 SCIP_CALL_ABORT(SCIPfreeClock(origprob, &temporaryClock) );
195
197
198 return SCIP_OKAY;
199}
200
201#define detectorPostprocessPartialdecNeighborhoodmaster NULL
202
203static
204GCG_DECL_SETPARAMAGGRESSIVE(setParamAggressiveNeighborhoodmaster)
205{
206 char setstr[SCIP_MAXSTRLEN];
207 const char* name = GCGdetectorGetName(detector);
209
210 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
212
213 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
215
216 return SCIP_OKAY;
217}
218
219
220static
221GCG_DECL_SETPARAMDEFAULT(setParamDefaultNeighborhoodmaster)
222{
223 char setstr[SCIP_MAXSTRLEN];
224 const char* name = GCGdetectorGetName(detector);
226
227 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
229
230 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
232
233 return SCIP_OKAY;
234}
235
236static
237GCG_DECL_SETPARAMFAST(setParamFastNeighborhoodmaster)
238{
239 char setstr[SCIP_MAXSTRLEN];
240 const char* name = GCGdetectorGetName(detector);
242
243 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
245
246 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
248
249 return SCIP_OKAY;
250}
251
252
253
254/*
255 * detector specific interface methods
256 */
257
258/** creates the handler for neighborhoodmaster detector and includes it in SCIP */
260 GCG* gcg /**< GCG data structure */
261 )
262{
263 GCG_DETECTORDATA* detectordata;
265 assert(origprob != NULL);
266
267 detectordata = NULL;
268
269 SCIP_CALL( SCIPallocMemory(origprob, &detectordata) );
270 assert(detectordata != NULL);
271
272 SCIP_CALL(
276 detectordata, freeNeighborhoodmaster, initNeighborhoodmaster,
277 exitNeighborhoodmaster, propagatePartialdecNeighborhoodmaster, finishPartialdecNeighborhoodmaster,
278 detectorPostprocessPartialdecNeighborhoodmaster, setParamAggressiveNeighborhoodmaster,
279 setParamDefaultNeighborhoodmaster, setParamFastNeighborhoodmaster));
280
281 SCIP_CALL( SCIPaddRealParam(origprob, "detection/detectors/neighborhoodmaster/maxratio",
282 "the maximal ratio of open constraints that are assigned to the master problem",
283 &detectordata->maxratio, FALSE, DEFAULT_MAXRATIO, 0.0, 1.0, NULL, NULL ) );
284
285 return SCIP_OKAY;
286}
class storing partialdecs and the problem matrix
class storing (potentially incomplete) decompositions
GCG_EXPORT int getNConssForCons(int consIndex)
returns the number of constraints 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
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 void addClockTime(SCIP_Real clocktime)
adds detection time of one detector
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
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 finishPartialdecNeighborhoodmaster
#define exitNeighborhoodmaster
#define detectorPostprocessPartialdecNeighborhoodmaster
SCIP_RETCODE GCGincludeDetectorNeighborhoodmaster(GCG *gcg)
#define DEFAULT_MAXRATIO
#define initNeighborhoodmaster
neighborhoodmaster detector
#define SCIP_MAXSTRLEN
#define SCIP_Real
#define SCIP_CALL_ABORT(x)
#define TRUE
#define FALSE
GCG interface methods.
SCIP * GCGgetOrigprob(GCG *gcg)
Definition gcg.cpp:87
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
SCIP_RETCODE SCIPaddRealParam(SCIP *scip, const char *name, const char *desc, SCIP_Real *valueptr, SCIP_Bool isadvanced, SCIP_Real defaultvalue, SCIP_Real minvalue, SCIP_Real maxvalue, 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
int i
#define NULL
various SCIP helper methods
bool operator()(const std::pair< int, int > &left, const std::pair< int, int > &right)
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_PROPAGATEPARTIALDEC(x)
struct GCG_DetectorData GCG_DETECTORDATA
struct Gcg GCG
Definition type_gcg.h:44
SCIP_VERBLEVEL_HIGH
SCIP_DIDNOTFIND
SCIP_SUCCESS
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP