Scippy

SCIP

Solving Constraint Integer Programs

probdata_scflp.c
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 probdata_scflp.c
17  * @brief Problem data for Stochastic Capacitated Facility Location problem
18  * @author Stephen J. Maher
19  *
20  * This file handles the main problem data used in that project. For more details see \ref SCFLP_PROBLEMDATA page.
21  *
22  * @page SCFLP_SOLVEPROB Solving the deterministic equivalent
23  *
24  * The probdata_scflp.c is used to store the global problem data and build the monolithic MIP and decomposed problems.
25  * First, the structure of the problem data is describe. This is followed by a description of how to solve the problem
26  * directly using SCIP or using Benders' decomposition.
27  *
28  * @section SCFLP_PROBLEMDATA The global problem data
29  *
30  * The problem data is accessible in all plugins. The function SCIPgetProbData() returns the pointer to that structure.
31  * We use this data structure to store all the information of the SCFLP. Since this structure is not visible in the
32  * other plugins, we implemented setter and getter functions to access this data. The problem data structure
33  * SCIP_ProbData is shown below.
34  *
35  * \code
36  * ** @brief Problem data which is accessible in all places
37  * *
38  * * This problem data is used to store the input of the cap instance, all variables which are created, and all
39  * * constraints. In addition, the probdata stores the data structures for the decomposed problem. This permits the
40  * * use of Benders' decomposition to solve the stochastic program.
41  * *
42  * struct SCIP_ProbData
43  * {
44  * SCIP** subproblems; **< the Benders' decomposition subproblems * SCIP_VAR**
45  * SCIP_VAR** facilityvars; **< all variables representing facilities *
46  * SCIP_VAR*** subfacilityvars; **< duplicates of the facility variables in the subproblems *
47  * SCIP_VAR**** customervars; **< all variables representing the satisfaction of demand per scenario *
48  * SCIP_CONS*** capconss; **< capacity constraints per facility per scenario *
49  * SCIP_CONS*** demandconss; **< demand constraints per customer per scenario *
50  * SCIP_CONS* sufficientcap; **< ensuring sufficient capacity is provided to satisfy demand (relatively complete recourse) *
51  * SCIP_Real** costs; **< the transportation costs to a customer from a facility *
52  * SCIP_Real** demands; **< the customer demands per scenario *
53  * SCIP_Real* capacity; **< the capacity of each facility *
54  * SCIP_Real* fixedcost; **< the fixed cost of opening each facility *
55  * int ncustomers; **< the number of customers *
56  * int nfacilities; **< the number of facilities *
57  * int nscenarios; **< the number of scenarios *
58  * SCIP_Bool usebenders; **< whether Benders' decomposition is used *
59  * };
60  * \endcode
61  *
62  * The function SCIPprobdataCreate() manages the creation of the SCFLP instance in SCIP. There are two types of
63  * formulations that can be produced in this example. The first is the monolithic deterministic equivalent. The second
64  * is the reformulated problem that decomposes the stochastic problem by scenarios. This alternative formulations is
65  * solved using Benders' decomposition. Depending on the solution method, some members of SCIP_ProbData will be unused.
66  * For example, subproblems and subfacilityvars are only used when Benders' decomposition is applied to solve the SCFLP.
67  *
68  * The probdata_scflp.c also provide interface methods to the global problem data. A list of all interface methods can be
69  * found in probdata_scflp.h.
70  *
71  * @section SCFLP_DETEQUIV Directly solving the deterministic equivalent using SCIP
72  *
73  * Within probdata_scflp.c, both the monolithic determinstic equivalent or the decomposed problem can be built within
74  * SCIP. The monolithic deterministic equivalent involve a since SCIP instances that is solved directly as a MIP. The
75  * problem that is build in SCIP is given in \ref SCFLP_DETEQUIVMODEL.
76  *
77  * @section SCFLP_BENDERS Solving the SCFLP using Benders' decomposition
78  *
79  * The model that is used to build the decomposed problem is given in \ref SCFLP_BENDERSMODEL. In this example, the
80  * default Benders' decomposition plugin is used to employ the Benders' decomposition framework, see
81  * src/scip/benders_default.h. Before calling SCIPcreateBendersDefault() to invoke the Benders' decomposition framework,
82  * the SCIP instances for the master problem and the subproblems must be created.
83  *
84  * The SCIP instance for the master problem includes only the first stage variables (the facility variables \f$x_{i}\f$)
85  * and the first stage constraints. Note, the auxiliary variables are not added to the master problem by the user, nor
86  * are any Benders' decomposition cuts.
87  *
88  * For each subproblem \f$s\f$, the SCIP instance is formulated with the second stage variables (the customer variables
89  * \f$y^{s}_{ij}\f$) and the second stage constraints. Also, the first stage variables are created for each scenario.
90  * These variables are copies of the master variables from the master SCIP instances and must be created by calling
91  * SCIPcreateVarBasic() or SCIPcreateVar(). The master problem variable copies that are created in the subproblem SCIP
92  * instances must have an objective coefficient of 0.0. This is inline with the classical application of Benders'
93  * decomposition.
94  *
95  * IMPORTANT: the master variables that are created for the subproblem SCIP instances must have the same name as the
96  * corresponding master variables in the master problem SCIP instance. This is because the mapping between the master
97  * and subproblem variables relies on the variable names. This mapping is used for setting up the subproblems to
98  * evaluate solutions from the master problem and generating Benders' cuts.
99  *
100  * Once the master and subproblem SCIP instances are created, the Benders' decomposition is invoked by calling the
101  * interface function SCIPcreateBendersDefault(). The parameters for this function are a SCIP instance for the master
102  * problem, an array of SCIP instances for the subproblems and the number of subproblems.
103  *
104  * The Benders' decomposition framework involves the use of constraint handlers within SCIP, src/scip/cons_benders.h and
105  * src/scip/cons_benderslp.h. In order to solve the master problem by adding Benders' cuts, src/scip/cons_benders.h and
106  * src/scip/cons_benderslp.h must be activated. This is done by setting the parameter "constraints/benders/active" and
107  * "constraints/benderslp/active" to TRUE.
108  *
109  * NOTE: it is not necessary to activate src/scip/cons_benderslp.h. The purpose of this constraint handler is to
110  * generate Benders' decomposition cut from solutions to the LP relaxation in the root node. These solutions are
111  * fractional, since the enforcement priority of benderslp is higher than the integer constraint handler. The benderslp
112  * constraint handler allows the user to employ the multi-phase algorithm of McDaniel and Devine (1977).
113  *
114  * McDaniel D, Devine M. A modified Benders’ partitioning algorithm for mixed integer programming. Management Science
115  * 1977;24(2):312–9
116  */
117 
118 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
119 
120 #include <string.h>
121 
122 #include "probdata_scflp.h"
123 
124 #include "scip/cons_linear.h"
125 #include "scip/benders_default.h"
126 #include "scip/scip.h"
127 #include "scip/scipdefplugins.h"
128 
129 /** @brief Problem data which is accessible in all places
130  *
131  * This problem data is used to store the input of the SCFLP, all variables which are created, and all constraints.
132  */
133 struct SCIP_ProbData
134 {
135  SCIP** subproblems; /**< the Benders' decomposition subproblems */
136  SCIP_VAR** facilityvars; /**< all variables representing facilities */
137  SCIP_VAR*** subfacilityvars; /**< duplicates of the facility variables in the subproblems */
138  SCIP_VAR**** customervars; /**< all variables representing the satisfaction of demand per scenario */
139  SCIP_CONS*** capconss; /**< capacity constraints per facility per scenario */
140  SCIP_CONS*** demandconss; /**< demand constraints per customer per scenario */
141  SCIP_CONS* sufficientcap; /**< ensuring sufficient capacity is provided to satisfy demand (relatively complete recourse) */
142  SCIP_Real** costs; /**< the transportation costs to a customer from a facility */
143  SCIP_Real** demands; /**< the customer demands per scenario */
144  SCIP_Real* capacity; /**< the capacity of each facility */
145  SCIP_Real* fixedcost; /**< the fixed cost of opening each facility */
146  int ncustomers; /**< the number of customers */
147  int nfacilities; /**< the number of facilities */
148  int nscenarios; /**< the number of scenarios */
149  SCIP_Bool usebenders; /**< whether Benders' decomposition is used */
150 };
151 
152 
153 
154 /**@name Local methods
155  *
156  * @{
157  */
158 
159 /** creates the original problem */
160 static
162  SCIP* scip, /**< SCIP data structure */
163  SCIP_VAR** facilityvars, /**< all variables representing facilities */
164  SCIP_VAR**** customervars, /**< all variables representing the satisfaction of demand */
165  SCIP_CONS*** capconss, /**< capacity constraints per facility */
166  SCIP_CONS*** demandconss, /**< demand constraints per customer */
167  SCIP_CONS** sufficientcap, /**< ensuring sufficient capacity is provided to satisfy demand */
168  SCIP_Real** costs, /**< the transportation costs from a facility to a customer */
169  SCIP_Real** demands, /**< the customer demands */
170  SCIP_Real* capacity, /**< the capacity of each facility */
171  SCIP_Real* fixedcost, /**< the fixed cost of opening a facility */
172  int ncustomers, /**< the number of customers */
173  int nfacilities, /**< the number of facilities */
174  int nscenarios /**< the number of scenarios */
175  )
176 {
177  SCIP_CONS* cons;
178  SCIP_VAR* var;
179  SCIP_Real maxdemand;
180  SCIP_Real coeff;
181  int i;
182  int j;
183  int k;
184  char name[SCIP_MAXSTRLEN];
185  assert(scip != NULL);
186 
187  /* adding the sufficient capacity constraints */
188  maxdemand = 0;
189  for( i = 0; i < nscenarios; i++)
190  {
191  SCIP_Real sumdemand = 0;
192  for( j = 0; j < ncustomers; j++ )
193  sumdemand += demands[j][i];
194 
195  if( sumdemand > maxdemand )
196  maxdemand = sumdemand;
197  }
198 
199  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "sufficientcapacity");
200  SCIP_CALL( SCIPcreateConsBasicLinear(scip, sufficientcap, name, 0, NULL, NULL, maxdemand, SCIPinfinity(scip)) );
201 
202  SCIP_CALL( SCIPaddCons(scip, (*sufficientcap)) );
203 
204  /* adds the capacity constraints to the scenario */
205  for( i = 0; i < nfacilities; i++ )
206  {
207  for( j = 0; j < nscenarios; j++ )
208  {
209  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "capacity_%d_%d", i, j);
210  SCIP_CALL( SCIPcreateConsBasicLinear(scip, &cons, name, 0, NULL, NULL, -SCIPinfinity(scip), 0.0) );
211 
212  SCIP_CALL( SCIPaddCons(scip, cons) );
213 
214  capconss[i][j] = cons;
215  }
216  }
217 
218  /* adds the demand constraints to the scenario */
219  for( i = 0; i < ncustomers; i++ )
220  {
221  for( j = 0; j < nscenarios; j++ )
222  {
223  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "demand_%d_%d", i, j);
224  SCIP_CALL( SCIPcreateConsBasicLinear(scip, &cons, name, 0, NULL, NULL, demands[i][j], SCIPinfinity(scip)) );
225 
226  SCIP_CALL( SCIPaddCons(scip, cons) );
227 
228  demandconss[i][j] = cons;
229  }
230  }
231 
232  for( i = 0; i < nfacilities; i++ )
233  {
234  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "facility_%d", i);
235  SCIP_CALL( SCIPcreateVarBasic(scip, &var, name, 0.0, 1.0, fixedcost[i], SCIP_VARTYPE_BINARY) );
236 
237  SCIP_CALL( SCIPaddVar(scip, var) );
238 
239  /* storing the variable in the facility variable list */
240  facilityvars[i] = var;
241 
242  /* adding the variable to the capacity constraints */
243  for( j = 0; j < nscenarios; j++ )
244  SCIP_CALL( SCIPaddCoefLinear(scip, capconss[i][j], var, -capacity[i]) );
245 
246  /* adding the variable to the sufficient capacity constraints */
247  SCIP_CALL( SCIPaddCoefLinear(scip, (*sufficientcap), var, capacity[i]) );
248  }
249 
250  /* adding the customer variables to the scenario */
251  for( i = 0; i < ncustomers; i++ )
252  {
253  for( j = 0; j < nfacilities; j++ )
254  {
255  for( k = 0; k < nscenarios; k++ )
256  {
257  coeff = costs[i][j]/(SCIP_Real)nscenarios;
258  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "customer(%d,%d,%d)", i, j, k);
259  SCIP_CALL( SCIPcreateVarBasic(scip, &var, name, 0.0, SCIPinfinity(scip), coeff, SCIP_VARTYPE_CONTINUOUS) );
260 
261  SCIP_CALL( SCIPaddVar(scip, var) );
262 
263  /* storing the customer variable in the list */
264  customervars[i][j][k] = var;
265 
266  if( costs[i][j] > 0 )
267  {
268  /* adding the variable to the capacity constraints */
269  SCIP_CALL( SCIPaddCoefLinear(scip, capconss[j][k], customervars[i][j][k], 1.0) );
270 
271  /* adding the variable to the demand constraints */
272  SCIP_CALL( SCIPaddCoefLinear(scip, demandconss[i][k], customervars[i][j][k], 1.0) );
273  }
274  }
275  }
276  }
277 
278  return SCIP_OKAY;
279 }
280 
281 /** creates the Benders' decomposition master problem */
282 static
284  SCIP* scip, /**< SCIP data structure */
285  SCIP_VAR** facilityvars, /**< all variables representing facilities */
286  SCIP_CONS** sufficientcap, /**< ensuring sufficient capacity is provided to satisfy demand */
287  SCIP_Real* capacity, /**< the capacity of each facility */
288  SCIP_Real* fixedcost, /**< the fixed cost of opening a facility */
289  SCIP_Real** demands, /**< the customer demands */
290  int ncustomers, /**< the number of customers */
291  int nfacilities, /**< the number of facilities */
292  int nscenarios /**< the number of scenarios */
293  )
294 {
295  SCIP_VAR* var;
296  SCIP_Real maxdemand;
297  int i;
298  int j;
299  char name[SCIP_MAXSTRLEN];
300  assert(scip != NULL);
301 
302  /* adding the sufficient capacity constraints */
303  maxdemand = 0;
304  for( i = 0; i < nscenarios; i++)
305  {
306  SCIP_Real sumdemand = 0;
307  for( j = 0; j < ncustomers; j++ )
308  sumdemand += demands[j][i];
309 
310  if( sumdemand > maxdemand )
311  maxdemand = sumdemand;
312  }
313 
314  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "sufficientcapacity");
315  SCIP_CALL( SCIPcreateConsBasicLinear(scip, sufficientcap, name, 0, NULL, NULL, maxdemand, SCIPinfinity(scip)) );
316 
317  SCIP_CALL( SCIPaddCons(scip, (*sufficientcap)) );
318 
319  /* adding the facility variables */
320  for( i = 0; i < nfacilities; i++ )
321  {
322  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "facility_%d", i);
323  SCIP_CALL( SCIPcreateVarBasic(scip, &var, name, 0.0, 1.0, fixedcost[i], SCIP_VARTYPE_BINARY) );
324 
325  SCIP_CALL( SCIPaddVar(scip, var) );
326 
327  /* storing the variable in the facility variable list */
328  facilityvars[i] = var;
329 
330  /* adding the variable to the sufficient capacity constraints */
331  SCIP_CALL( SCIPaddCoefLinear(scip, (*sufficientcap), var, capacity[i]) );
332  }
333 
334  return SCIP_OKAY;
335 }
336 
337 /** creates the scenario subproblems */
338 static
340  SCIP* scip, /**< SCIP data structure */
341  SCIP** subproblems, /**< the Benders' decomposition subproblems */
342  SCIP_VAR** facilityvars, /**< all variables representing facilities */
343  SCIP_VAR*** subfacilityvars, /**< the copies of the facility variables in the subproblems */
344  SCIP_VAR**** customervars, /**< all variables representing the satisfaction of demand */
345  SCIP_CONS*** capconss, /**< capacity constraints per facility */
346  SCIP_CONS*** demandconss, /**< demand constraints per customer */
347  SCIP_Real** costs, /**< the transportation costs from a facility to a customer */
348  SCIP_Real** demands, /**< the customer demands */
349  SCIP_Real* capacity, /**< the capacity of each facility */
350  SCIP_Real* fixedcost, /**< the fixed cost of opening a facility */
351  int ncustomers, /**< the number of customers */
352  int nfacilities, /**< the number of facilities */
353  int nscenarios /**< the number of scenarios */
354  )
355 {
356  SCIP_CONS* cons;
357  SCIP_VAR* var;
358  SCIP_Real coeff;
359  int i;
360  int j;
361  int k;
362  char name[SCIP_MAXSTRLEN];
363  assert(scip != NULL);
364 
365  /* adds the capacity constraints to the scenario */
366  for( i = 0; i < nfacilities; i++ )
367  {
368  for( j = 0; j < nscenarios; j++ )
369  {
370  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "capacity_%d_%d", i, j);
371  SCIP_CALL( SCIPcreateConsBasicLinear(subproblems[j], &cons, name, 0, NULL, NULL, -SCIPinfinity(subproblems[j]), 0.0) );
372 
373  SCIP_CALL( SCIPaddCons(subproblems[j], cons) );
374 
375  capconss[i][j] = cons;
376  }
377  }
378 
379  /* adds the demand constraints to the scenario */
380  for( i = 0; i < ncustomers; i++ )
381  {
382  for( j = 0; j < nscenarios; j++ )
383  {
384  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "demand_%d_%d", i, j);
385  SCIP_CALL( SCIPcreateConsBasicLinear(subproblems[j], &cons, name, 0, NULL, NULL, demands[i][j], SCIPinfinity(subproblems[j])) );
386 
387  SCIP_CALL( SCIPaddCons(subproblems[j], cons) );
388 
389  demandconss[i][j] = cons;
390  }
391  }
392 
393  for( i = 0; i < nfacilities; i++ )
394  {
395  for( j = 0; j < nscenarios; j++ )
396  {
397  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "facility_%d", i);
398  SCIP_CALL( SCIPcreateVarBasic(subproblems[j], &var, name, 0.0, 1.0, 0.0, SCIP_VARTYPE_CONTINUOUS) );
399 
400  SCIP_CALL( SCIPaddVar(subproblems[j], var) );
401 
402  /* storing the variable in the facility variable list */
403  subfacilityvars[i][j] = var;
404 
405  /* adding the variable to the capacity constraints */
406  SCIP_CALL( SCIPaddCoefLinear(subproblems[j], capconss[i][j], subfacilityvars[i][j], -capacity[i]) );
407  }
408  }
409 
410  /* adding the customer variables to the scenario */
411  for( i = 0; i < ncustomers; i++ )
412  {
413  for( j = 0; j < nfacilities; j++ )
414  {
415  for( k = 0; k < nscenarios; k++ )
416  {
417  coeff = costs[i][j]/(SCIP_Real)nscenarios;
418  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "customer(%d,%d,%d)", i, j, k);
419  SCIP_CALL( SCIPcreateVarBasic(subproblems[k], &var, name, 0.0, SCIPinfinity(subproblems[k]), coeff, SCIP_VARTYPE_CONTINUOUS) );
420 
421  SCIP_CALL( SCIPaddVar(subproblems[k], var) );
422 
423  /* storing the customer variable in the list */
424  customervars[i][j][k] = var;
425 
426  if( costs[i][j] > 0 )
427  {
428  /* adding the variable to the capacity constraints */
429  SCIP_CALL( SCIPaddCoefLinear(subproblems[k], capconss[j][k], customervars[i][j][k], 1.0) );
430 
431  /* adding the variable to the demand constraints */
432  SCIP_CALL( SCIPaddCoefLinear(subproblems[k], demandconss[i][k], customervars[i][j][k], 1.0) );
433  }
434  }
435  }
436  }
437 
438  return SCIP_OKAY;
439 }
440 
441 
442 /** creates problem data */
443 static
445  SCIP* scip, /**< SCIP data structure */
446  SCIP_PROBDATA** probdata, /**< pointer to problem data */
447  SCIP** subproblems, /**< the Benders' decomposition subproblems */
448  SCIP_VAR** facilityvars, /**< all variables representing facilities */
449  SCIP_VAR*** subfacilityvars, /**< the copies of the facility variables in the subproblems */
450  SCIP_VAR**** customervars, /**< all variables representing the satisfaction of demand */
451  SCIP_CONS*** capconss, /**< capacity constraints per facility per scenario */
452  SCIP_CONS*** demandconss, /**< demand constraints per customer per scenario */
453  SCIP_CONS* sufficientcap, /**< ensuring sufficient capacity is provided to satisfy demand */
454  SCIP_Real** costs, /**< the transportation costs to a customer from a facility */
455  SCIP_Real** demands, /**< the customer demands per scenario */
456  SCIP_Real* capacity, /**< the capacity of each facility */
457  SCIP_Real* fixedcost, /**< the fixed cost of opening a facility */
458  int ncustomers, /**< the number of customers */
459  int nfacilities, /**< the number of facilities */
460  int nscenarios, /**< the number of scenarios */
461  SCIP_Bool usebenders /**< whether Benders' decomposition is used */
462  )
463 {
464  int i;
465  int j;
466 
467  assert(scip != NULL);
468  assert(probdata != NULL);
469 
470  /* allocate memory */
471  SCIP_CALL( SCIPallocBlockMemory(scip, probdata) );
472 
473  /* copying the subproblem information */
474  if( usebenders )
475  {
476  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*probdata)->subproblems, subproblems, nscenarios) );
477 
478  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*probdata)->subfacilityvars, nfacilities) );
479  for( i = 0; i < nfacilities; i++ )
480  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*probdata)->subfacilityvars[i], subfacilityvars[i], nscenarios) );
481  }
482 
483  /* copy variable arrays */
484  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*probdata)->facilityvars, facilityvars, nfacilities) );
485  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*probdata)->customervars, ncustomers) );
486  for( i = 0; i < ncustomers; i++ )
487  {
488  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*probdata)->customervars[i], nfacilities) );
489  for( j = 0; j < nfacilities; j++ )
490  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*probdata)->customervars[i][j], customervars[i][j],
491  nscenarios) );
492  }
493 
494  /* duplicate the constraint arrays */
495  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*probdata)->capconss, nfacilities) );
496  for( i = 0; i < nfacilities; i++ )
497  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*probdata)->capconss[i], capconss[i], nscenarios) );
498 
499  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*probdata)->demandconss, ncustomers) );
500  for( i = 0; i < ncustomers; i++ )
501  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*probdata)->demandconss[i], demandconss[i], nscenarios) );
502 
503  /* duplicate the data arrays */
504  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*probdata)->demands, ncustomers) );
505  for( i = 0; i < ncustomers; i++ )
506  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*probdata)->demands[i], demands[i], nscenarios) );
507 
508  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(*probdata)->costs, ncustomers) );
509  for( i = 0; i < ncustomers; i++ )
510  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*probdata)->costs[i], costs[i], nfacilities) );
511 
512  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*probdata)->capacity, capacity, nfacilities) );
513  SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*probdata)->fixedcost, fixedcost, nfacilities) );
514 
515  (*probdata)->sufficientcap = sufficientcap;
516  (*probdata)->ncustomers = ncustomers;
517  (*probdata)->nfacilities = nfacilities;
518  (*probdata)->nscenarios = nscenarios;
519  (*probdata)->usebenders = usebenders;
520 
521  return SCIP_OKAY;
522 }
523 
524 /** frees the memory of the given problem data */
525 static
527  SCIP* scip, /**< SCIP data structure */
528  SCIP_PROBDATA** probdata /**< pointer to problem data */
529  )
530 {
531  int i;
532  int j;
533  int k;
534 
535  assert(scip != NULL);
536  assert(probdata != NULL);
537 
538 #if 1
539  /* release all variables */
540  for( i = 0; i < (*probdata)->nfacilities; i++ )
541  SCIP_CALL( SCIPreleaseVar(scip, &(*probdata)->facilityvars[i]) );
542 
543  for( i = 0; i < (*probdata)->nscenarios; i++ )
544  {
545  SCIP* varscip;
546  if( (*probdata)->usebenders )
547  varscip = (*probdata)->subproblems[i];
548  else
549  varscip = scip;
550 
551  for( j = 0; j < (*probdata)->nfacilities; j++ )
552  {
553  for( k = 0; k < (*probdata)->ncustomers; k++ )
554  SCIP_CALL( SCIPreleaseVar(varscip, &(*probdata)->customervars[k][j][i]) );
555  }
556  }
557 
558  /* release all constraints */
559  for( i = 0; i < (*probdata)->nscenarios; ++i )
560  {
561  SCIP* consscip;
562  if( (*probdata)->usebenders )
563  consscip = (*probdata)->subproblems[i];
564  else
565  consscip = scip;
566 
567  for( j = 0; j < (*probdata)->ncustomers; j++ )
568  SCIP_CALL( SCIPreleaseCons(consscip, &(*probdata)->demandconss[j][i]) );
569  }
570 
571  for( i = 0; i < (*probdata)->nscenarios; ++i )
572  {
573  SCIP* consscip;
574  if( (*probdata)->usebenders )
575  consscip = (*probdata)->subproblems[i];
576  else
577  consscip = scip;
578 
579  for( j = 0; j < (*probdata)->nfacilities; ++j )
580  SCIP_CALL( SCIPreleaseCons(consscip, &(*probdata)->capconss[j][i]) );
581  }
582 
583  SCIP_CALL( SCIPreleaseCons(scip, &(*probdata)->sufficientcap) );
584 #endif
585 
586  /* free memory of arrays */
587  SCIPfreeBlockMemoryArray(scip, &(*probdata)->fixedcost, (*probdata)->nfacilities);
588  SCIPfreeBlockMemoryArray(scip, &(*probdata)->capacity, (*probdata)->nfacilities);
589 
590  for( i = (*probdata)->ncustomers - 1; i >= 0; i-- )
591  SCIPfreeBlockMemoryArray(scip, &(*probdata)->costs[i], (*probdata)->nfacilities);
592  SCIPfreeBlockMemoryArray(scip, &(*probdata)->costs, (*probdata)->ncustomers);
593 
594  for( i = (*probdata)->ncustomers - 1; i >= 0; i-- )
595  SCIPfreeBlockMemoryArray(scip, &(*probdata)->demands[i], (*probdata)->nscenarios);
596  SCIPfreeBlockMemoryArray(scip, &(*probdata)->demands, (*probdata)->ncustomers);
597 
598  /* freeing the constraint memory arrays */
599  for( i = (*probdata)->ncustomers - 1; i >= 0; i-- )
600  SCIPfreeBlockMemoryArray(scip, &(*probdata)->demandconss[i], (*probdata)->nscenarios);
601  SCIPfreeBlockMemoryArray(scip, &(*probdata)->demandconss, (*probdata)->ncustomers);
602 
603  for( i = (*probdata)->nfacilities - 1; i >= 0; i-- )
604  SCIPfreeBlockMemoryArray(scip, &(*probdata)->capconss[i], (*probdata)->nscenarios);
605  SCIPfreeBlockMemoryArray(scip, &(*probdata)->capconss, (*probdata)->nfacilities);
606 
607  /* freeing the variable memory arrays */
608  for( i = (*probdata)->ncustomers - 1; i >= 0; i-- )
609  {
610  for( j = (*probdata)->nfacilities - 1; j >= 0; j-- )
611  SCIPfreeBlockMemoryArray(scip, &(*probdata)->customervars[i][j], (*probdata)->nscenarios);
612 
613  SCIPfreeBlockMemoryArray(scip, &(*probdata)->customervars[i], (*probdata)->nfacilities);
614  }
615  SCIPfreeBlockMemoryArray(scip, &(*probdata)->customervars, (*probdata)->ncustomers);
616 
617  SCIPfreeBlockMemoryArray(scip, &(*probdata)->facilityvars, (*probdata)->nfacilities);
618 
619  /* freeing the subproblem information */
620  if( (*probdata)->usebenders )
621  {
622  /* freeing the sub facility variables */
623  for( i = 0; i < (*probdata)->nscenarios; i++ )
624  {
625  for( j = 0; j < (*probdata)->nfacilities; j++ )
626  SCIP_CALL( SCIPreleaseVar((*probdata)->subproblems[i], &(*probdata)->subfacilityvars[j][i]) );
627  }
628 
629  for( i = (*probdata)->nfacilities - 1; i >= 0; i-- )
630  SCIPfreeBlockMemoryArray(scip, &(*probdata)->subfacilityvars[i], (*probdata)->nscenarios);
631 
632  SCIPfreeBlockMemoryArray(scip, &(*probdata)->subfacilityvars, (*probdata)->nfacilities);
633 
634  for( i = (*probdata)->nscenarios - 1; i >= 0 ; i-- )
635  SCIP_CALL( SCIPfree(&(*probdata)->subproblems[i]) );
636  SCIPfreeBlockMemoryArray(scip, &(*probdata)->subproblems, (*probdata)->nscenarios);
637  }
638 
639  /* free probdata */
640  SCIPfreeBlockMemory(scip, probdata);
641 
642  return SCIP_OKAY;
643 }
644 
645 /**@} */
646 
647 /**@name Callback methods of problem data
648  *
649  * @{
650  */
651 
652 /** frees user data of original problem (called when the original problem is freed) */
653 static
654 SCIP_DECL_PROBDELORIG(probdelorigScflp)
655 {
656  assert(scip != NULL);
657  assert(probdata != NULL);
658 
659  SCIPdebugMsg(scip, "free original problem data\n");
660 
661  SCIP_CALL( probdataFree(scip, probdata) );
662 
663  return SCIP_OKAY;
664 }
665 
666 /** creates user data of transformed problem by transforming the original user problem data
667  * (called after problem was transformed) */
668 static
669 SCIP_DECL_PROBTRANS(probtransScflp)
670 {
671  SCIPdebugMsg(scip, "transforming problem data\n");
672 
673  return SCIP_OKAY;
674 }
675 
676 /** frees user data of transformed problem (called when the transformed problem is freed) */
677 static
678 SCIP_DECL_PROBDELTRANS(probdeltransScflp)
679 {
680  SCIPdebugMsg(scip, "free transformed problem data\n");
681 
682  return SCIP_OKAY;
683 }
684 
685 /**@} */
686 
687 
688 /**@name Interface methods
689  *
690  * @{
691  */
692 
693 /** sets up the problem data */
695  SCIP* scip, /**< SCIP data structure */
696  const char* probname, /**< problem name */
697  SCIP_Real** costs, /**< the transportation costs from a facility to a customer */
698  SCIP_Real** demands, /**< the customer demands */
699  SCIP_Real* capacity, /**< the capacity of each facility */
700  SCIP_Real* fixedcost, /**< the fixed cost of opening a facility */
701  int ncustomers, /**< the number of customers */
702  int nfacilities, /**< the number of facilities */
703  int nscenarios, /**< the number of Benders' decomposition scenarios */
704  SCIP_Bool usebenders /**< will Benders' decomposition be used to solve the problem */
705  )
706 {
707  SCIP** subproblems;
708  SCIP_PROBDATA* probdata;
709  SCIP_CONS*** demandconss;
710  SCIP_CONS*** capconss;
711  SCIP_CONS* sufficientcap;
712  SCIP_VAR** facilityvars;
713  SCIP_VAR*** subfacilityvars;
714  SCIP_VAR**** customervars;
715  int i;
716  int j;
717 
718  assert(scip != NULL);
719 
720  /* create problem in SCIP and add non-NULL callbacks via setter functions */
721  SCIP_CALL( SCIPcreateProbBasic(scip, probname) );
722 
723  SCIP_CALL( SCIPsetProbDelorig(scip, probdelorigScflp) );
724  SCIP_CALL( SCIPsetProbTrans(scip, probtransScflp) );
725  SCIP_CALL( SCIPsetProbDeltrans(scip, probdeltransScflp) );
726 
727  /* set objective sense */
729 
730  SCIP_CALL( SCIPallocBufferArray(scip, &demandconss, ncustomers) );
731  for( i = 0; i < ncustomers; i++ )
732  SCIP_CALL( SCIPallocBufferArray(scip, &demandconss[i], nscenarios) );
733  SCIP_CALL( SCIPallocBufferArray(scip, &capconss, nfacilities) );
734  for( i = 0; i < nfacilities; i++ )
735  SCIP_CALL( SCIPallocBufferArray(scip, &capconss[i], nscenarios) );
736 
737  SCIP_CALL( SCIPallocBufferArray(scip, &facilityvars, nfacilities) );
738  SCIP_CALL( SCIPallocBufferArray(scip, &customervars, ncustomers) );
739  for( i = 0; i < ncustomers; i++ )
740  {
741  SCIP_CALL( SCIPallocBufferArray(scip, &customervars[i], nfacilities) );
742  for( j = 0; j < nfacilities; j++ )
743  SCIP_CALL( SCIPallocBufferArray(scip, &customervars[i][j], nscenarios) );
744  }
745 
746  sufficientcap = NULL;
747 
748  subproblems = NULL;
749  subfacilityvars = NULL;
750 
751  if( usebenders )
752  {
753  char subprobname[SCIP_MAXSTRLEN];
754 
755  /* allocting the memory for the subproblem specific information */
756  SCIP_CALL( SCIPallocBufferArray(scip, &subproblems, nscenarios) );
757  SCIP_CALL( SCIPallocBufferArray(scip, &subfacilityvars, nfacilities) );
758  for( i = 0; i < nfacilities; i++ )
759  SCIP_CALL( SCIPallocBufferArray(scip, &subfacilityvars[i], nscenarios) );
760 
761  /* creating the subproblems */
762  for( i = 0; i < nscenarios; i++ )
763  {
764  SCIP_CALL( SCIPcreate(&subproblems[i]) );
765 
766  /* include default SCIP plugins */
767  SCIP_CALL( SCIPincludeDefaultPlugins(subproblems[i]) );
768 
769  (void) SCIPsnprintf(subprobname, SCIP_MAXSTRLEN, "sub_%s_%d", probname, i);
770  SCIP_CALL( SCIPcreateProbBasic(subproblems[i], subprobname) );
771  }
772 
773  /* creating the master problem */
774  SCIP_CALL( createMasterproblem(scip, facilityvars, &sufficientcap, capacity, fixedcost, demands, ncustomers,
775  nfacilities, nscenarios) );
776  SCIP_CALL( createSubproblems(scip, subproblems, facilityvars, subfacilityvars, customervars, capconss,
777  demandconss, costs, demands, capacity, fixedcost, ncustomers, nfacilities, nscenarios) );
778 
779  /* including the Benders' decomposition plugin */
780  SCIP_CALL( SCIPcreateBendersDefault(scip, subproblems, nscenarios) );
781 
782  /* activating the Benders' decomposition constraint handlers */
783  SCIP_CALL( SCIPsetBoolParam(scip, "constraints/benders/active", TRUE) );
784  SCIP_CALL( SCIPsetBoolParam(scip, "constraints/benderslp/active", TRUE) );
785 
786  SCIP_CALL( SCIPsetIntParam(scip, "constraints/benders/maxprerounds", 1) );
787  SCIP_CALL( SCIPsetIntParam(scip, "presolving/maxrounds", 1) );
788  }
789  else
790  {
791  /* creating the original problem */
792  SCIP_CALL( createOriginalproblem(scip, facilityvars, customervars, capconss, demandconss, &sufficientcap, costs,
793  demands, capacity, fixedcost, ncustomers, nfacilities, nscenarios) );
794  }
795 
796  /* create problem data */
797  SCIP_CALL( probdataCreate(scip, &probdata, subproblems, facilityvars, subfacilityvars, customervars, capconss,
798  demandconss, sufficientcap, costs, demands, capacity, fixedcost, ncustomers, nfacilities, nscenarios, usebenders) );
799 
800  /* set user problem data */
801  SCIP_CALL( SCIPsetProbData(scip, probdata) );
802 
803  /* free local buffer arrays */
804  if( usebenders )
805  {
806  SCIPfreeBufferArray(scip, &subproblems);
807 
808  for( i = nfacilities - 1; i >= 0; i-- )
809  SCIPfreeBufferArray(scip, &subfacilityvars[i]);
810  SCIPfreeBufferArray(scip, &subfacilityvars);
811  }
812 
813  for( i = ncustomers - 1; i >= 0; i-- )
814  {
815  for( j = nfacilities - 1; j >= 0; j-- )
816  SCIPfreeBufferArray(scip, &customervars[i][j]);
817  SCIPfreeBufferArray(scip, &customervars[i]);
818  }
819  SCIPfreeBufferArray(scip, &customervars);
820  SCIPfreeBufferArray(scip, &facilityvars);
821 
822  for( i = nfacilities - 1; i >= 0; i-- )
823  SCIPfreeBufferArray(scip, &capconss[i]);
824  SCIPfreeBufferArray(scip, &capconss);
825 
826  for( i = ncustomers - 1; i >= 0; i-- )
827  SCIPfreeBufferArray(scip, &demandconss[i]);
828  SCIPfreeBufferArray(scip, &demandconss);
829 
830  return SCIP_OKAY;
831 }
832 
833 /** returns the number of facilities */
835  SCIP_PROBDATA* probdata /**< problem data */
836  )
837 {
838  assert(probdata != NULL);
839 
840  return probdata->nfacilities;
841 }
842 
843 /** returns the number of customers */
845  SCIP_PROBDATA* probdata /**< problem data */
846  )
847 {
848  assert(probdata != NULL);
849 
850  return probdata->ncustomers;
851 }
852 
853 /** returns the facility variables */
855  SCIP_PROBDATA* probdata /**< problem data */
856  )
857 {
858  assert(probdata != NULL);
859 
860  return probdata->facilityvars;
861 }
862 
863 /**@} */
#define SCIPfreeBlockMemoryArray(scip, ptr, num)
Definition: scip_mem.h:116
static SCIP_RETCODE createSubproblems(SCIP *scip, SCIP **subproblems, SCIP_VAR **facilityvars, SCIP_VAR ***subfacilityvars, SCIP_VAR ****customervars, SCIP_CONS ***capconss, SCIP_CONS ***demandconss, SCIP_Real **costs, SCIP_Real **demands, SCIP_Real *capacity, SCIP_Real *fixedcost, int ncustomers, int nfacilities, int nscenarios)
#define NULL
Definition: def.h:246
#define SCIPallocBlockMemoryArray(scip, ptr, num)
Definition: scip_mem.h:99
SCIP_RETCODE SCIPcreateConsBasicLinear(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Real *vals, SCIP_Real lhs, SCIP_Real rhs)
static SCIP_RETCODE createOriginalproblem(SCIP *scip, SCIP_VAR **facilityvars, SCIP_VAR ****customervars, SCIP_CONS ***capconss, SCIP_CONS ***demandconss, SCIP_CONS **sufficientcap, SCIP_Real **costs, SCIP_Real **demands, SCIP_Real *capacity, SCIP_Real *fixedcost, int ncustomers, int nfacilities, int nscenarios)
#define SCIP_MAXSTRLEN
Definition: def.h:267
SCIP_RETCODE SCIPreleaseVar(SCIP *scip, SCIP_VAR **var)
Definition: scip_var.c:1251
SCIP_Real SCIPinfinity(SCIP *scip)
SCIP_RETCODE SCIPcreateBendersDefault(SCIP *scip, SCIP **subproblems, int nsubproblems)
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10253
#define TRUE
Definition: def.h:71
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
int SCIPprobdataGetNCustomers(SCIP_PROBDATA *probdata)
SCIP_RETCODE SCIPsetProbData(SCIP *scip, SCIP_PROBDATA *probdata)
Definition: scip_prob.c:1070
static SCIP_DECL_PROBDELORIG(probdelorigScflp)
SCIP_RETCODE SCIPcreateVarBasic(SCIP *scip, SCIP_VAR **var, const char *name, SCIP_Real lb, SCIP_Real ub, SCIP_Real obj, SCIP_VARTYPE vartype)
Definition: scip_var.c:184
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:114
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip_mem.h:142
SCIP_RETCODE SCIPcreate(SCIP **scip)
Definition: scip_general.c:338
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip_mem.h:97
#define SCIPdebugMsg
Definition: scip_message.h:88
SCIP_RETCODE SCIPaddCoefLinear(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Real val)
SCIP_RETCODE SCIPcreateProbBasic(SCIP *scip, const char *name)
Definition: scip_prob.c:223
static SCIP_DECL_PROBTRANS(probtransScflp)
int SCIPprobdataGetNFacilities(SCIP_PROBDATA *probdata)
#define SCIPduplicateBlockMemoryArray(scip, ptr, source, num)
Definition: scip_mem.h:111
static SCIP_RETCODE probdataCreate(SCIP *scip, SCIP_PROBDATA **probdata, SCIP **subproblems, SCIP_VAR **facilityvars, SCIP_VAR ***subfacilityvars, SCIP_VAR ****customervars, SCIP_CONS ***capconss, SCIP_CONS ***demandconss, SCIP_CONS *sufficientcap, SCIP_Real **costs, SCIP_Real **demands, SCIP_Real *capacity, SCIP_Real *fixedcost, int ncustomers, int nfacilities, int nscenarios, SCIP_Bool usebenders)
SCIP_RETCODE SCIPsetObjsense(SCIP *scip, SCIP_OBJSENSE objsense)
Definition: scip_prob.c:1298
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip_prob.c:2822
SCIP_RETCODE SCIPsetBoolParam(SCIP *scip, const char *name, SCIP_Bool value)
Definition: scip_param.c:520
SCIP_RETCODE SCIPprobdataCreate(SCIP *scip, const char *probname, SCIP_Real **costs, SCIP_Real **demands, SCIP_Real *capacity, SCIP_Real *fixedcost, int ncustomers, int nfacilities, int nscenarios, SCIP_Bool usebenders)
#define SCIP_CALL(x)
Definition: def.h:358
Problem data for Stochastic Capacitated Facility Location problem.
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip_mem.h:130
#define SCIP_Bool
Definition: def.h:69
SCIP_RETCODE SCIPsetProbTrans(SCIP *scip, SCIP_DECL_PROBTRANS((*probtrans)))
Definition: scip_prob.c:264
SCIP_RETCODE SCIPincludeDefaultPlugins(SCIP *scip)
static SCIP_RETCODE probdataFree(SCIP *scip, SCIP_PROBDATA **probdata)
SCIP_RETCODE SCIPsetIntParam(SCIP *scip, const char *name, int value)
Definition: scip_param.c:578
Constraint handler for linear constraints in their most general form, .
SCIP_VAR ** SCIPprobdataGetFacilityVars(SCIP_PROBDATA *probdata)
static SCIP_DECL_PROBDELTRANS(probdeltransScflp)
struct SCIP_ProbData SCIP_PROBDATA
Definition: type_prob.h:44
SCIP_RETCODE SCIPaddVar(SCIP *scip, SCIP_VAR *var)
Definition: scip_prob.c:1724
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
Definition: scip_cons.c:1187
#define SCIP_Real
Definition: def.h:157
default Benders&#39; decomposition plugin
static SCIP_RETCODE createMasterproblem(SCIP *scip, SCIP_VAR **facilityvars, SCIP_CONS **sufficientcap, SCIP_Real *capacity, SCIP_Real *fixedcost, SCIP_Real **demands, int ncustomers, int nfacilities, int nscenarios)
SCIP_RETCODE SCIPsetProbDeltrans(SCIP *scip, SCIP_DECL_PROBDELTRANS((*probdeltrans)))
Definition: scip_prob.c:285
SCIP_RETCODE SCIPsetProbDelorig(SCIP *scip, SCIP_DECL_PROBDELORIG((*probdelorig)))
Definition: scip_prob.c:243
default SCIP plugins
SCIP callable library.
SCIP_RETCODE SCIPfree(SCIP **scip)
Definition: scip_general.c:370