Scippy

SCIP

Solving Constraint Integer Programs

objbenders.cpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2019 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License. */
12 /* along with SCIP; see the file COPYING. If not visit scip.zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file objbenders.cpp
17  * @brief C++ wrapper for the Benders' decomposition plugins
18  * @author Stephen J. Maher
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #include <cassert>
24 
25 #include "objbenders.h"
26 
27 
28 
29 
30 /*
31  * Data structures
32  */
33 
34 /** Benders' decomposition data */
35 struct SCIP_BendersData
36 {
37  scip::ObjBenders* objbenders; /**< the Benders' decomposition object */
38  SCIP_Bool deleteobject; /**< should the Benders' decomposition object be deleted when benders is freed? */
39 };
40 
41 
42 
43 
44 /*
45  * Callback methods of the Benders' decomposition framework
46  */
47 
48 extern "C"
49 {
50 
51 /** copy method for Benders' decomposition plugins (called when SCIP copies plugins) */
52 static
53 SCIP_DECL_BENDERSCOPY(bendersCopyObj)
54 { /*lint --e{715}*/
55  SCIP_BENDERSDATA* bendersdata;
56 
57  assert(scip != NULL);
58 
59  bendersdata = SCIPbendersGetData(benders);
60  assert(bendersdata != NULL);
61  assert(bendersdata->objbenders != NULL);
62  assert(bendersdata->objbenders->scip_ != scip);
63 
64  if( bendersdata->objbenders->iscloneable() )
65  {
66  scip::ObjBenders* newobjbenders;
67  newobjbenders = dynamic_cast<scip::ObjBenders*> (bendersdata->objbenders->clone(scip));
68 
69  /* call include method of Benders' decomposition object */
70  SCIP_CALL( SCIPincludeObjBenders(scip, newobjbenders, TRUE) );
71  }
72 
73  return SCIP_OKAY;
74 }
75 
76 /** destructor of Benders' decomposition to free user data (called when SCIP is exiting) */
77 static
78 SCIP_DECL_BENDERSFREE(bendersFreeObj)
79 { /*lint --e{715}*/
80  SCIP_BENDERSDATA* bendersdata;
81 
82  bendersdata = SCIPbendersGetData(benders);
83  assert(bendersdata != NULL);
84  assert(bendersdata->objbenders != NULL);
85  assert(bendersdata->objbenders->scip_ == scip);
86 
87  /* call virtual method of benders object */
88  SCIP_CALL( bendersdata->objbenders->scip_free(scip, benders) );
89 
90  /* free benders object */
91  if( bendersdata->deleteobject )
92  delete bendersdata->objbenders;
93 
94  /* free benders data */
95  delete bendersdata;
96  SCIPbendersSetData(benders, NULL); /*lint !e64*/
97 
98  return SCIP_OKAY;
99 }
100 
101 
102 /** initialization method of Benders' decomposition (called after problem was transformed) */
103 static
104 SCIP_DECL_BENDERSINIT(bendersInitObj)
105 { /*lint --e{715}*/
106  SCIP_BENDERSDATA* bendersdata;
107 
108  bendersdata = SCIPbendersGetData(benders);
109  assert(bendersdata != NULL);
110  assert(bendersdata->objbenders != NULL);
111  assert(bendersdata->objbenders->scip_ == scip);
112 
113  /* call virtual method of benders object */
114  SCIP_CALL( bendersdata->objbenders->scip_init(scip, benders) );
115 
116  return SCIP_OKAY;
117 }
118 
119 
120 /** deinitialization method of Benders' decomposition (called before transformed problem is freed) */
121 static
122 SCIP_DECL_BENDERSEXIT(bendersExitObj)
123 { /*lint --e{715}*/
124  SCIP_BENDERSDATA* bendersdata;
125 
126  bendersdata = SCIPbendersGetData(benders);
127  assert(bendersdata != NULL);
128  assert(bendersdata->objbenders != NULL);
129 
130  /* call virtual method of benders object */
131  SCIP_CALL( bendersdata->objbenders->scip_exit(scip, benders) );
132 
133  return SCIP_OKAY;
134 }
135 
136 
137 /** presolving initialization method of Benders' decomposition (called when presolving is about to begin) */
138 static
139 SCIP_DECL_BENDERSINITPRE(bendersInitpreObj)
140 { /*lint --e{715}*/
141  SCIP_BENDERSDATA* bendersdata;
142 
143  bendersdata = SCIPbendersGetData(benders);
144  assert(bendersdata != NULL);
145  assert(bendersdata->objbenders != NULL);
146 
147  /* call virtual method of benders object */
148  SCIP_CALL( bendersdata->objbenders->scip_initpre(scip, benders) );
149 
150  return SCIP_OKAY;
151 }
152 
153 
154 /** presolving deinitialization method of Benders' decomposition (called after presolving has been finished) */
155 static
156 SCIP_DECL_BENDERSEXITPRE(bendersExitpreObj)
157 { /*lint --e{715}*/
158  SCIP_BENDERSDATA* bendersdata;
159 
160  bendersdata = SCIPbendersGetData(benders);
161  assert(bendersdata != NULL);
162  assert(bendersdata->objbenders != NULL);
163 
164  /* call virtual method of benders object */
165  SCIP_CALL( bendersdata->objbenders->scip_exitpre(scip, benders) );
166 
167  return SCIP_OKAY;
168 }
169 
170 
171 /** solving process initialization method of Benders' decomposition (called when branch and bound process is about to begin) */
172 static
173 SCIP_DECL_BENDERSINITSOL(bendersInitsolObj)
174 { /*lint --e{715}*/
175  SCIP_BENDERSDATA* bendersdata;
176 
177  bendersdata = SCIPbendersGetData(benders);
178  assert(bendersdata != NULL);
179  assert(bendersdata->objbenders != NULL);
180 
181  /* call virtual method of benders object */
182  SCIP_CALL( bendersdata->objbenders->scip_initsol(scip, benders) );
183 
184  return SCIP_OKAY;
185 }
186 
187 
188 /** solving process deinitialization method of Benders' decomposition (called before branch and bound process data is freed) */
189 static
190 SCIP_DECL_BENDERSEXITSOL(bendersExitsolObj)
191 { /*lint --e{715}*/
192  SCIP_BENDERSDATA* bendersdata;
193 
194  bendersdata = SCIPbendersGetData(benders);
195  assert(bendersdata != NULL);
196  assert(bendersdata->objbenders != NULL);
197 
198  /* call virtual method of benders object */
199  SCIP_CALL( bendersdata->objbenders->scip_exitsol(scip, benders) );
200 
201  return SCIP_OKAY;
202 }
203 
204 
205 /** method that is called to create the subproblem and register it with the Benders' decomposition structure. */
206 static
207 SCIP_DECL_BENDERSCREATESUB(bendersCreatesubObj)
208 { /*lint --e{715}*/
209  SCIP_BENDERSDATA* bendersdata;
210 
211  bendersdata = SCIPbendersGetData(benders);
212  assert(bendersdata != NULL);
213  assert(bendersdata->objbenders != NULL);
214 
215  /* call virtual method of benders object */
216  SCIP_CALL( bendersdata->objbenders->scip_createsub(scip, benders, probnumber) );
217 
218  return SCIP_OKAY;
219 }
220 
221 
222 /** methods called prior to solving the subproblems */
223 static
224 SCIP_DECL_BENDERSPRESUBSOLVE(bendersPresubsolveObj)
225 { /*lint --e{715}*/
226  SCIP_BENDERSDATA* bendersdata;
227 
228  bendersdata = SCIPbendersGetData(benders);
229  assert(bendersdata != NULL);
230  assert(bendersdata->objbenders != NULL);
231 
232  /* call virtual method of benders object */
233  SCIP_CALL( bendersdata->objbenders->scip_presubsolve(scip, benders, sol, type, checkint, skipsolve, result) );
234 
235  return SCIP_OKAY;
236 }
237 
238 
239 /** method called to solve the convex relaxation of an individual subproblem of the Benders' decomposition */
240 static
241 SCIP_DECL_BENDERSSOLVESUBCONVEX(bendersSolvesubconvexObj)
242 { /*lint --e{715}*/
243  SCIP_BENDERSDATA* bendersdata;
244 
245  bendersdata = SCIPbendersGetData(benders);
246  assert(bendersdata != NULL);
247  assert(bendersdata->objbenders != NULL);
248 
249  /* call virtual method of benders object */
250  SCIP_CALL( bendersdata->objbenders->scip_solvesubconvex(scip, benders, sol, probnumber, onlyconvexcheck, objective,
251  result) );
252 
253  return SCIP_OKAY;
254 }
255 
256 
257 /** method called to solve an individual subproblem of the Benders' decomposition */
258 static
259 SCIP_DECL_BENDERSSOLVESUB(bendersSolvesubObj)
260 { /*lint --e{715}*/
261  SCIP_BENDERSDATA* bendersdata;
262 
263  bendersdata = SCIPbendersGetData(benders);
264  assert(bendersdata != NULL);
265  assert(bendersdata->objbenders != NULL);
266 
267  /* call virtual method of benders object */
268  SCIP_CALL( bendersdata->objbenders->scip_solvesub(scip, benders, sol, probnumber, objective, result) );
269 
270  return SCIP_OKAY;
271 }
272 
273 
274 /** method called after the subproblems are solved in the Benders' decomposition algorithm */
275 static
276 SCIP_DECL_BENDERSPOSTSOLVE(bendersPostsolveObj)
277 { /*lint --e{715}*/
278  SCIP_BENDERSDATA* bendersdata;
279 
280  bendersdata = SCIPbendersGetData(benders);
281  assert(bendersdata != NULL);
282  assert(bendersdata->objbenders != NULL);
283 
284  /* call virtual method of benders object */
285  SCIP_CALL( bendersdata->objbenders->scip_postsolve(scip, benders, sol, type, mergecands, npriomergecands,
286  nmergecands, checkint, infeasible, merged) );
287 
288  return SCIP_OKAY;
289 }
290 
291 
292 /** frees an individual subproblem. Called in each iteration of the Benders' decomposition algorithm */
293 static
294 SCIP_DECL_BENDERSFREESUB(bendersFreesubObj)
295 { /*lint --e{715}*/
296  SCIP_BENDERSDATA* bendersdata;
297 
298  bendersdata = SCIPbendersGetData(benders);
299  assert(bendersdata != NULL);
300  assert(bendersdata->objbenders != NULL);
301 
302  /* call virtual method of benders object */
303  SCIP_CALL( bendersdata->objbenders->scip_freesub(scip, benders, probnumber) );
304 
305  return SCIP_OKAY;
306 }
307 
308 
309 /** callback method to retrieve the master (subproblem) variable corresponding to the input subproblem (master) variable */
310 static
311 SCIP_DECL_BENDERSGETVAR(bendersGetvarObj)
312 { /*lint --e{715}*/
313  SCIP_BENDERSDATA* bendersdata;
314 
315  bendersdata = SCIPbendersGetData(benders);
316  assert(bendersdata != NULL);
317  assert(bendersdata->objbenders != NULL);
318 
319  /* call virtual method of benders object */
320  SCIP_CALL( bendersdata->objbenders->scip_getvar(scip, benders, var, mappedvar, probnumber) );
321 
322  return SCIP_OKAY;
323 }
324 
325 
326 }
327 
328 
329 /*
330  * Benders' decomposition specific interface methods
331  */
332 
333 /** creates the Benders' decomposition for the given Benders' decomposition object and includes it in SCIP */
335  SCIP* scip, /**< SCIP data structure */
336  scip::ObjBenders* objbenders, /**< Benders' decomposition object */
337  SCIP_Bool deleteobject /**< should the Benders' decomposition object be deleted when benders is freed? */
338  )
339 {
340  SCIP_BENDERSDATA* bendersdata;
341 
342  assert(scip != NULL);
343  assert(objbenders != NULL);
344  assert(objbenders->scip_ == scip);
345 
346  /* create obj Benders' decomposition data */
347  bendersdata = new SCIP_BENDERSDATA;
348  bendersdata->objbenders = objbenders;
349  bendersdata->deleteobject = deleteobject;
350 
351  /* include Benders' decomposition */
352  SCIP_CALL( SCIPincludeBenders(scip, objbenders->scip_name_, objbenders->scip_desc_,
353  objbenders->scip_priority_, objbenders->scip_cutlp_, objbenders->scip_cutpseudo_,
354  objbenders->scip_cutrelax_, objbenders->scip_shareauxvars_, bendersCopyObj, bendersFreeObj, bendersInitObj,
355  bendersExitObj, bendersInitpreObj, bendersExitpreObj, bendersInitsolObj, bendersExitsolObj, bendersGetvarObj,
356  bendersCreatesubObj, bendersPresubsolveObj, bendersSolvesubconvexObj, bendersSolvesubObj, bendersPostsolveObj,
357  bendersFreesubObj, bendersdata) ); /*lint !e429*/
358 
359  return SCIP_OKAY; /*lint !e429*/
360 }
361 
362 /** returns the benders object of the given name, or 0 if not existing */
364  SCIP* scip, /**< SCIP data structure */
365  const char* name /**< name of Benders' decomposition */
366  )
367 {
368  SCIP_BENDERS* benders;
369  SCIP_BENDERSDATA* bendersdata;
370 
371  benders = SCIPfindBenders(scip, name);
372  if( benders == NULL )
373  return 0;
374 
375  bendersdata = SCIPbendersGetData(benders);
376  assert(bendersdata != NULL);
377 
378  return bendersdata->objbenders;
379 }
380 
381 /** returns the benders object for the given Benders' decomposition */
383  SCIP* scip, /**< SCIP data structure */
384  SCIP_BENDERS* benders /**< Benders' decomposition */
385  )
386 {
387  SCIP_BENDERSDATA* bendersdata;
388 
389  bendersdata = SCIPbendersGetData(benders);
390  assert(bendersdata != NULL);
391 
392  return bendersdata->objbenders;
393 }
void SCIPbendersSetData(SCIP_BENDERS *benders, SCIP_BENDERSDATA *bendersdata)
Definition: benders.c:4057
static SCIP_DECL_BENDERSEXITSOL(bendersExitsolObj)
Definition: objbenders.cpp:190
scip::ObjBenders * SCIPgetObjBenders(SCIP *scip, SCIP_BENDERS *benders)
Definition: objbenders.cpp:382
static SCIP_DECL_BENDERSSOLVESUB(bendersSolvesubObj)
Definition: objbenders.cpp:259
#define NULL
Definition: def.h:253
static SCIP_DECL_BENDERSPOSTSOLVE(bendersPostsolveObj)
Definition: objbenders.cpp:276
static SCIP_DECL_BENDERSFREE(bendersFreeObj)
Definition: objbenders.cpp:78
SCIP_RETCODE SCIPincludeObjBenders(SCIP *scip, scip::ObjBenders *objbenders, SCIP_Bool deleteobject)
Definition: objbenders.cpp:334
const SCIP_Bool scip_shareauxvars_
Definition: objbenders.h:74
C++ wrapper for Benders&#39; decomposition.
char * scip_name_
Definition: objbenders.h:56
static SCIP_DECL_BENDERSGETVAR(bendersGetvarObj)
Definition: objbenders.cpp:311
#define TRUE
Definition: def.h:72
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
SCIP_RETCODE SCIPincludeBenders(SCIP *scip, const char *name, const char *desc, int priority, SCIP_Bool cutlp, SCIP_Bool cutpseudo, SCIP_Bool cutrelax, SCIP_Bool shareauxvars, SCIP_DECL_BENDERSCOPY((*benderscopy)), SCIP_DECL_BENDERSFREE((*bendersfree)), SCIP_DECL_BENDERSINIT((*bendersinit)), SCIP_DECL_BENDERSEXIT((*bendersexit)), SCIP_DECL_BENDERSINITPRE((*bendersinitpre)), SCIP_DECL_BENDERSEXITPRE((*bendersexitpre)), SCIP_DECL_BENDERSINITSOL((*bendersinitsol)), SCIP_DECL_BENDERSEXITSOL((*bendersexitsol)), SCIP_DECL_BENDERSGETVAR((*bendersgetvar)), SCIP_DECL_BENDERSCREATESUB((*benderscreatesub)), SCIP_DECL_BENDERSPRESUBSOLVE((*benderspresubsolve)), SCIP_DECL_BENDERSSOLVESUBCONVEX((*benderssolvesubconvex)), SCIP_DECL_BENDERSSOLVESUB((*benderssolvesub)), SCIP_DECL_BENDERSPOSTSOLVE((*benderspostsolve)), SCIP_DECL_BENDERSFREESUB((*bendersfreesub)), SCIP_BENDERSDATA *bendersdata)
Definition: scip_benders.c:62
static SCIP_DECL_BENDERSEXIT(bendersExitObj)
Definition: objbenders.cpp:122
static SCIP_DECL_BENDERSINITSOL(bendersInitsolObj)
Definition: objbenders.cpp:173
C++ wrapper for Benders&#39; decomposition plugins.
Definition: objbenders.h:47
SCIP_BENDERS * SCIPfindBenders(SCIP *scip, const char *name)
Definition: scip_benders.c:455
static SCIP_DECL_BENDERSPRESUBSOLVE(bendersPresubsolveObj)
Definition: objbenders.cpp:224
char * scip_desc_
Definition: objbenders.h:59
SCIP_BENDERSDATA * SCIPbendersGetData(SCIP_BENDERS *benders)
Definition: benders.c:4047
struct SCIP_BendersData SCIP_BENDERSDATA
Definition: type_benders.h:63
const int scip_priority_
Definition: objbenders.h:62
#define SCIP_CALL(x)
Definition: def.h:365
scip::ObjBenders * SCIPfindObjBenders(SCIP *scip, const char *name)
Definition: objbenders.cpp:363
static SCIP_DECL_BENDERSSOLVESUBCONVEX(bendersSolvesubconvexObj)
Definition: objbenders.cpp:241
#define SCIP_Bool
Definition: def.h:70
static SCIP_DECL_BENDERSCREATESUB(bendersCreatesubObj)
Definition: objbenders.cpp:207
static SCIP_DECL_BENDERSINITPRE(bendersInitpreObj)
Definition: objbenders.cpp:139
const SCIP_Bool scip_cutpseudo_
Definition: objbenders.h:68
static SCIP_DECL_BENDERSEXITPRE(bendersExitpreObj)
Definition: objbenders.cpp:156
const SCIP_Bool scip_cutlp_
Definition: objbenders.h:65
static SCIP_DECL_BENDERSFREESUB(bendersFreesubObj)
Definition: objbenders.cpp:294
static SCIP_DECL_BENDERSINIT(bendersInitObj)
Definition: objbenders.cpp:104
const SCIP_Bool scip_cutrelax_
Definition: objbenders.h:71
static SCIP_DECL_BENDERSCOPY(bendersCopyObj)
Definition: objbenders.cpp:53