Scippy

UG

Ubiquity Generator framework

scipParaInstance.cpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and software framework */
4 /* UG --- Ubquity Generator Framework */
5 /* */
6 /* Copyright Written by Yuji Shinano <shinano@zib.de>, */
7 /* Copyright (C) 2021 by Zuse Institute Berlin, */
8 /* licensed under LGPL version 3 or later. */
9 /* Commercial licenses are available through <licenses@zib.de> */
10 /* */
11 /* This code is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public License */
13 /* as published by the Free Software Foundation; either version 3 */
14 /* of the License, or (at your option) any later version. */
15 /* */
16 /* This program is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
19 /* GNU Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* */
24 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
25 /* $Id: scipParaInstance.cpp,v 1.18 2014/04/29 20:12:51 bzfshina Exp $ */
26 
27 /**@file $RCSfile: scipParaInstance.cpp,v $
28  * @brief ParaInstance extenstion for SCIP solver.
29  * @author Yuji Shinano
30  *
31  *
32  *
33  */
34 
35 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
36 
37 
38 #include <cstdlib>
39 #include <cassert>
40 #include <cstring>
41 #include <cctype>
42 #ifdef _MSC_VER
43 #include <direct.h> //this is for _getcwd()
44 #define getcwd(X, Y) _getcwd(X, Y)
45 #else
46 #include <unistd.h>
47 #endif
48 #include "ug/paraDef.h"
49 #include "ug_bb/bbParaInstance.h"
50 #include "scip/scipdefplugins.h"
51 #include "scip/reader_lp.h"
52 #include "scip/cons_knapsack.h"
53 #include "scip/cons_linear.h"
54 #include "scip/cons_logicor.h"
55 #include "scip/cons_setppc.h"
56 #include "scip/cons_varbound.h"
57 #include "scip/cons_bounddisjunction.h"
58 #include "scip/cons_sos1.h"
59 #include "scip/cons_sos2.h"
60 #include "scip/pub_misc.h"
61 #include "scipParaInstance.h"
62 
63 using namespace ParaSCIP;
64 
65 const static char *PRESOLVED_INSTANCE = "presolved.cip";
66 
67 /** hash key retrieval function for variables */
68 static
69 SCIP_DECL_HASHGETKEY(hashGetKeyVar)
70 { /*lint --e{715}*/
71  return elem;
72 }
73 
74 /** returns TRUE iff the indices of both variables are equal */
75 static
76 SCIP_DECL_HASHKEYEQ(hashKeyEqVar)
77 { /*lint --e{715}*/
78  if ( key1 == key2 )
79  return TRUE;
80  return FALSE;
81 }
82 
83 /** returns the hash value of the key */
84 static
85 SCIP_DECL_HASHKEYVAL(hashKeyValVar)
86 { /*lint --e{715}*/
87  assert( SCIPvarGetIndex((SCIP_VAR*) key) >= 0 );
88  return (unsigned int) SCIPvarGetIndex((SCIP_VAR*) key);
89 }
90 
91 void
92 ScipParaInstance::allocateMemoryForOrdinaryConstraints(
93  )
94 {
95  /** for linear constraints */
96  if( nLinearConss > 0 )
97  {
98  idxLinearConsNames = new int[nLinearConss];
99  linearLhss = new SCIP_Real[nLinearConss];
100  linearRhss = new SCIP_Real[nLinearConss];
101  nLinearCoefs = new int[nLinearConss];
102  linearCoefs = new SCIP_Real*[nLinearConss];
103  idxLinearCoefsVars = new int*[nLinearConss];
104  }
105  /** for setppc constraints */
106  if( nSetppcConss )
107  {
108  idxSetppcConsNames = new int[nSetppcConss];
109  nIdxSetppcVars = new int[nSetppcConss];
110  setppcTypes = new int[nSetppcConss];
111  idxSetppcVars = new int*[nSetppcConss];
112  }
113  /** for logical constraints */
114  if(nLogicorConss)
115  {
116  idxLogicorConsNames = new int[nLogicorConss];
117  nIdxLogicorVars = new int[nLogicorConss];
118  idxLogicorVars = new int*[nLogicorConss];
119  }
120  /** for knapsack constraints */
121  if(nKnapsackConss)
122  {
123  idxKnapsackConsNames = new int[nKnapsackConss];
124  capacities = new SCIP_Longint[nKnapsackConss];
125  nLKnapsackCoefs = new int[nKnapsackConss];
126  knapsackCoefs = new SCIP_Longint*[nKnapsackConss];
127  idxKnapsackCoefsVars = new int*[nKnapsackConss];
128  }
129  /** for varbound constraints */
130  if( nVarboundConss ){
131  idxVarboundConsNames = new int[nVarboundConss];
132  varboundLhss = new SCIP_Real[nVarboundConss];
133  varboundRhss = new SCIP_Real[nVarboundConss];
134  idxVarboundCoefVar1s = new int[nVarboundConss];
135  varboundCoef2s = new SCIP_Real[nVarboundConss];
136  idxVarboundCoefVar2s = new int[nVarboundConss];
137  }
138  /** for bounddisjunction constraints */
139  if( nVarBoundDisjunctionConss ){
140 abort();
141  idxBoundDisjunctionConsNames = new int[nVarBoundDisjunctionConss];
142  nVarsBoundDisjunction = new int[nVarBoundDisjunctionConss];
143  boundsBoundDisjunction = new SCIP_Real*[nVarBoundDisjunctionConss];
144  boundTypesBoundDisjunction = new SCIP_BOUNDTYPE*[nVarBoundDisjunctionConss];
145  idxVarBoundDisjunction = new int*[nVarBoundDisjunctionConss];
146  }
147  /** for SOS1 constraints */
148  if( nSos1Conss ){
149  idxSos1ConsNames = new int[nSos1Conss];
150  nSos1Coefs = new int[nSos1Conss];
151  sos1Coefs = new SCIP_Real*[nSos1Conss];
152  idxSos1CoefsVars = new int*[nSos1Conss];
153  }
154  /** for SOS2 constraints */
155  if( nSos2Conss ){
156  idxSos2ConsNames = new int[nSos2Conss];
157  nSos2Coefs = new int[nSos2Conss];
158  sos2Coefs = new SCIP_Real*[nSos2Conss];
159  idxSos2CoefsVars = new int*[nSos2Conss];
160  }
161 }
162 
163 void
164 ScipParaInstance::addOrdinaryConstraintName(
165  int c,
166  SCIP_CONS *cons
167  )
168 {
169  posConsNames[c] = lConsNames;
170  (void) strcpy( &consNames[lConsNames], SCIPconsGetName(cons) );
171  lConsNames += strlen( SCIPconsGetName(cons) )+ 1;
172 }
173 
174 void
175 ScipParaInstance::setLinearConstraint(
176  SCIP *scip,
177  int c,
178  SCIP_CONS *cons
179  )
180 {
181  addOrdinaryConstraintName(c, cons);
182  idxLinearConsNames[nLinearConss] = c;
183  linearLhss[nLinearConss] = SCIPgetLhsLinear(scip, cons);
184  linearRhss[nLinearConss] = SCIPgetRhsLinear(scip, cons);
185  int nvars = SCIPgetNVarsLinear(scip, cons);
186  nLinearCoefs[nLinearConss] = nvars;
187  linearCoefs[nLinearConss] = new SCIP_Real[nvars];
188  idxLinearCoefsVars[nLinearConss] = new int[nvars];
189  SCIP_Real *coefs = SCIPgetValsLinear(scip, cons);
190  SCIP_VAR **vars = SCIPgetVarsLinear(scip, cons);
191  for(int v = 0; v < nvars; ++v)
192  {
193  linearCoefs[nLinearConss][v] = coefs[v];
194  idxLinearCoefsVars[nLinearConss][v] = SCIPvarGetProbindex(vars[v]);
195  }
196  nLinearConss++;
197 }
198 
199 void
200 ScipParaInstance::createLinearConstraintsInSCIP(
201  SCIP *scip
202  )
203 {
204  SCIP_VAR **vars = SCIPgetVars(scip);
205  for(int c = 0; c < nLinearConss; ++c )
206  {
207  SCIP_CONS* cons;
208  SCIP_VAR **varsInCons = new SCIP_VAR*[nLinearCoefs[c]];
209  for( int v = 0; v < nLinearCoefs[c]; v++)
210  {
211  varsInCons[v] = vars[idxLinearCoefsVars[c][v]];
212  }
213  SCIP_CALL_ABORT( SCIPcreateConsLinear(scip, &cons,
214  &consNames[posConsNames[idxLinearConsNames[c]]], nLinearCoefs[c], varsInCons, linearCoefs[c],linearLhss[c], linearRhss[c],
215  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) );
216  SCIP_CALL_ABORT( SCIPaddCons(scip, cons) );
217  SCIPdebug( SCIP_CALL_ABORT( SCIPprintCons(scip, cons, NULL) ) );
218  SCIP_CALL_ABORT( SCIPreleaseCons(scip, &cons) );
219  delete [] varsInCons;
220  }
221 }
222 
223 /*************************************************************/
224 /*** NOTE: setSetppcConstraint routine cannot work correctly */
225 /*************************************************************/
226 void
227 ScipParaInstance::setSetppcConstraint(
228  SCIP *scip,
229  int c,
230  SCIP_CONS *cons
231  )
232 {
233  addOrdinaryConstraintName(c, cons);
234  idxSetppcConsNames[nSetppcConss] = c;
235  int nvars = SCIPgetNVarsSetppc(scip,cons);
236  nIdxSetppcVars[nSetppcConss] = nvars;
237  setppcTypes[nSetppcConss] = SCIPgetTypeSetppc(scip,cons);
238  idxSetppcVars[nSetppcConss] = new int[nvars];
239  SCIP_VAR **vars = SCIPgetVarsSetppc(scip, cons);
240  for( int v = 0; v < nvars; ++v)
241  {
242  idxSetppcVars[nSetppcConss][v] = SCIPvarGetProbindex(vars[v]);
243  }
244  nSetppcConss++;
245 }
246 
247 void
248 ScipParaInstance::createSetppcConstraintsInSCIP(
249  SCIP *scip
250  )
251 {
252  SCIP_VAR **vars = SCIPgetVars(scip);
253  for(int c = 0; c < nSetppcConss; ++c )
254  {
255  SCIP_CONS* cons;
256  SCIP_VAR **varsInCons = new SCIP_VAR*[nIdxSetppcVars[c]];
257  for( int v = 0; v < nIdxSetppcVars[c]; v++)
258  {
259  varsInCons[v] = vars[idxSetppcVars[c][v]];
260  }
261  switch (setppcTypes[c])
262  {
263  case SCIP_SETPPCTYPE_PARTITIONING:
264  SCIP_CALL_ABORT( SCIPcreateConsSetpart(scip, &cons,
265  &consNames[posConsNames[idxSetppcConsNames[c]]], nIdxSetppcVars[c], varsInCons,
266  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) );
267  break;
268  case SCIP_SETPPCTYPE_PACKING:
269  SCIP_CALL_ABORT( SCIPcreateConsSetpack(scip, &cons,
270  &consNames[posConsNames[idxSetppcConsNames[c]]], nIdxSetppcVars[c], varsInCons,
271  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) );
272  break;
273  case SCIP_SETPPCTYPE_COVERING:
274  SCIP_CALL_ABORT( SCIPcreateConsSetcover(scip, &cons,
275  &consNames[posConsNames[idxSetppcConsNames[c]]], nIdxSetppcVars[c], varsInCons,
276  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) );
277  break;
278  default:
279  THROW_LOGICAL_ERROR2("invalid setppc constraint type: type = ", setppcTypes[c]);
280  }
281  SCIP_CALL_ABORT( SCIPaddCons(scip, cons) );
282  SCIPdebug( SCIP_CALL_ABORT( SCIPprintCons(scip, cons, NULL) ) );
283  SCIP_CALL_ABORT( SCIPreleaseCons(scip, &cons) );
284  delete [] varsInCons;
285  }
286 }
287 
288 /**************************************************************/
289 /*** NOTE: setLogicorConstraint routine cannot work correctly */
290 /**************************************************************/
291 void
292 ScipParaInstance::setLogicorConstraint(
293  SCIP *scip,
294  int c,
295  SCIP_CONS *cons
296  )
297 {
298  addOrdinaryConstraintName(c, cons);
299  idxLogicorConsNames[nLogicorConss] = c;
300  int nvars = SCIPgetNVarsLogicor(scip, cons);
301  nIdxLogicorVars[nLogicorConss] = nvars;
302  idxLogicorVars[nLogicorConss] = new int[nvars];
303 
304  SCIP_VAR **vars = SCIPgetVarsLogicor(scip, cons);
305  for( int v = 0; v < nvars; ++v )
306  {
307  if( SCIPvarIsActive(vars[v]) )
308  idxLogicorVars[nLogicorConss][v] = SCIPvarGetProbindex(vars[v]);
309  else if( SCIPvarIsNegated(vars[v]) )
310  idxLogicorVars[nLogicorConss][v] = -SCIPvarGetProbindex(SCIPvarGetNegationVar(vars[v])) - 1;
311  else
312  idxLogicorVars[nLogicorConss][v] = INT_MAX;
313  }
314  nLogicorConss++;
315 }
316 
317 void
318 ScipParaInstance::createLogicorConstraintsInSCIP(
319  SCIP *scip
320  )
321 {
322  SCIP_VAR **vars = SCIPgetVars(scip);
323  for(int c = 0; c < nLogicorConss; ++c )
324  {
325  SCIP_CONS* cons;
326  SCIP_VAR **varsInCons = new SCIP_VAR*[nIdxLogicorVars[c]];
327  int v;
328  for( v = 0; v < nIdxLogicorVars[c]; v++)
329  {
330  if( idxLogicorVars[c][v] == INT_MAX )
331  break;
332  /* negated variable */
333  if( idxLogicorVars[c][v] < 0 )
334  {
335  SCIP_CALL_ABORT( SCIPgetNegatedVar(scip, vars[-(idxLogicorVars[c][v] + 1)], &varsInCons[v]) );
336  }
337  else
338  varsInCons[v] = vars[idxLogicorVars[c][v]];
339  }
340  if( v == nIdxLogicorVars[c] )
341  {
342  SCIP_CALL_ABORT( SCIPcreateConsLogicor(scip, &cons,
343  &consNames[posConsNames[idxLogicorConsNames[c]]], nIdxLogicorVars[c], varsInCons,
344  TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) );
345  SCIP_CALL_ABORT( SCIPaddCons(scip, cons) );
346  SCIPdebug( SCIP_CALL_ABORT( SCIPprintCons(scip, cons, NULL) ) );
347  SCIP_CALL_ABORT( SCIPreleaseCons(scip, &cons) );
348  }
349  delete [] varsInCons;
350  }
351 }
352 
353 /***************************************************************/
354 /*** NOTE: setKnapsackConstraint routine cannot work correctly */
355 /***************************************************************/
356 void
357 ScipParaInstance::setKnapsackConstraint(
358  SCIP *scip,
359  int c,
360  SCIP_CONS *cons
361  )
362 {
363  addOrdinaryConstraintName(c, cons);
364  idxKnapsackConsNames[nKnapsackConss] = c;
365  capacities[nKnapsackConss] = SCIPgetCapacityKnapsack(scip, cons);
366  int nvars = SCIPgetNVarsKnapsack(scip, cons);
367  nLKnapsackCoefs[nKnapsackConss] = nvars;
368  knapsackCoefs[nKnapsackConss] = new SCIP_Longint[nvars];
369  idxKnapsackCoefsVars[nKnapsackConss] = new int[nvars];
370  SCIP_Longint *weights = SCIPgetWeightsKnapsack(scip, cons);
371  SCIP_VAR **vars = SCIPgetVarsKnapsack(scip,cons);
372  for(int v = 0; v < nvars; ++v )
373  {
374  knapsackCoefs[nKnapsackConss][v] = weights[v];
375  idxKnapsackCoefsVars[nKnapsackConss][v] = SCIPvarGetProbindex(vars[v]);
376  }
377  nKnapsackConss++;
378 }
379 
380 
381 void
382 ScipParaInstance::createKnapsackConstraintsInSCIP(
383  SCIP *scip
384  )
385 {
386  SCIP_VAR **vars = SCIPgetVars(scip);
387  for(int c = 0; c < nKnapsackConss; ++c )
388  {
389  SCIP_CONS* cons;
390  SCIP_VAR **varsInCons = new SCIP_VAR*[nLKnapsackCoefs[c]];
391  for( int v = 0; v < nLKnapsackCoefs[c]; v++)
392  {
393  varsInCons[v] = vars[idxKnapsackCoefsVars[c][v]];
394  }
395  SCIP_CALL_ABORT( SCIPcreateConsKnapsack(scip, &cons,
396  &consNames[posConsNames[idxKnapsackConsNames[c]]], nLKnapsackCoefs[c], varsInCons, knapsackCoefs[c], capacities[c],
397  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) );
398  SCIP_CALL_ABORT( SCIPaddCons(scip, cons) );
399  SCIPdebug( SCIP_CALL_ABORT( SCIPprintCons(scip, cons, NULL) ) );
400  SCIP_CALL_ABORT( SCIPreleaseCons(scip, &cons) );
401  delete [] varsInCons;
402  }
403 }
404 
405 /***************************************************************/
406 /*** NOTE: setVarboundConstraint routine cannot work correctly */
407 /***************************************************************/
408 void
409 ScipParaInstance::setVarboundConstraint(
410  SCIP *scip,
411  int c,
412  SCIP_CONS *cons
413  )
414 {
415  addOrdinaryConstraintName(c, cons);
416  idxVarboundConsNames[nVarboundConss] = c;
417  varboundLhss[nVarboundConss] = SCIPgetLhsVarbound(scip,cons);
418  varboundRhss[nVarboundConss] = SCIPgetRhsVarbound(scip,cons);
419  idxVarboundCoefVar1s[nVarboundConss] = SCIPvarGetProbindex(SCIPgetVarVarbound(scip,cons));
420  varboundCoef2s[nVarboundConss] = SCIPgetVbdcoefVarbound(scip,cons);
421  idxVarboundCoefVar2s[nVarboundConss] = SCIPvarGetProbindex(SCIPgetVbdvarVarbound(scip,cons));
422  nVarboundConss++;
423 }
424 
425 void
426 ScipParaInstance::createVarboundConstraintsInSCIP(
427  SCIP *scip
428  )
429 {
430  SCIP_VAR **vars = SCIPgetVars(scip);
431  for(int c = 0; c < nVarboundConss; ++c )
432  {
433  SCIP_CONS* cons;
434  SCIP_VAR *var1InCons = vars[idxVarboundCoefVar1s[c]];
435  SCIP_VAR *var2InCons = vars[idxVarboundCoefVar2s[c]];
436  SCIP_CALL_ABORT( SCIPcreateConsVarbound(scip, &cons,
437  &consNames[posConsNames[idxVarboundConsNames[c]]], var1InCons, var2InCons, varboundCoef2s[c], varboundLhss[c], varboundRhss[c],
438  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) );
439  SCIP_CALL_ABORT( SCIPaddCons(scip, cons) );
440  SCIPdebug( SCIP_CALL_ABORT( SCIPprintCons(scip, cons, NULL) ) );
441  SCIP_CALL_ABORT( SCIPreleaseCons(scip, &cons) );
442  }
443 }
444 
445 /***************************************************************/
446 /*** NOTE: setVarboundConstraint routine cannot work correctly */
447 /***************************************************************/
448 void
449 ScipParaInstance::setBoundDisjunctionConstraint(
450  SCIP *scip,
451  int c,
452  SCIP_CONS *cons
453  )
454 {
455  addOrdinaryConstraintName(c, cons);
456  idxBoundDisjunctionConsNames[nVarBoundDisjunctionConss] = c;
457  nVarsBoundDisjunction[nVarBoundDisjunctionConss] = SCIPgetNVarsBounddisjunction(scip,cons);
458  idxVarBoundDisjunction[nVarBoundDisjunctionConss] = new int[nVarsBoundDisjunction[nVarBoundDisjunctionConss]];
459  boundsBoundDisjunction[nVarBoundDisjunctionConss] = new SCIP_Real[nVarsBoundDisjunction[nVarBoundDisjunctionConss]];
460  boundTypesBoundDisjunction[nVarBoundDisjunctionConss] = new SCIP_BOUNDTYPE[nVarsBoundDisjunction[nVarBoundDisjunctionConss]];
461  SCIP_VAR **vars = SCIPgetVarsBounddisjunction ( scip, cons );
462  SCIP_BOUNDTYPE *boundTypes = SCIPgetBoundtypesBounddisjunction( scip, cons );
463  SCIP_Real *bounds = SCIPgetBoundsBounddisjunction( scip, cons );
464  for( int v = 0; v < nVarsBoundDisjunction[nVarBoundDisjunctionConss]; ++v )
465  {
466  idxVarBoundDisjunction[nVarBoundDisjunctionConss][v] = SCIPvarGetProbindex(vars[v]);
467  boundsBoundDisjunction[nVarBoundDisjunctionConss][v] = bounds[v];
468  boundTypesBoundDisjunction[nVarBoundDisjunctionConss][v] = boundTypes[v];
469  }
470  nVarBoundDisjunctionConss++;
471 }
472 
473 void
474 ScipParaInstance::createBoundDisjunctionConstraintInSCIP(
475  SCIP *scip
476  )
477 {
478  SCIP_VAR **vars = SCIPgetVars(scip);
479  for(int c = 0; c < nVarBoundDisjunctionConss; ++c )
480  {
481  SCIP_CONS* cons;
482  SCIP_VAR **boundVars = new SCIP_VAR*[nVarsBoundDisjunction[c]];
483  SCIP_BOUNDTYPE *types = new SCIP_BOUNDTYPE[nVarsBoundDisjunction[c]];
484  for( int v = 0; v < nVarsBoundDisjunction[c]; ++v )
485  {
486  boundVars[v] = vars[idxVarBoundDisjunction[c][v]];
487  types[v] = boundTypesBoundDisjunction[c][v];
488  }
489 
490  SCIP_CALL_ABORT( SCIPcreateConsBounddisjunction(scip, &cons,
491  &consNames[posConsNames[idxBoundDisjunctionConsNames[c]]], nVarsBoundDisjunction[c], boundVars,
492  types, boundsBoundDisjunction[c],
493  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) );
494  SCIP_CALL_ABORT( SCIPaddCons(scip, cons) );
495  SCIPdebug( SCIP_CALL_ABORT( SCIPprintCons(scip, cons, NULL) ) );
496  SCIP_CALL_ABORT( SCIPreleaseCons(scip, &cons) );
497  delete [] boundVars;
498  delete [] types;
499  }
500 }
501 
502 void
503 ScipParaInstance::setSos1Constraint(
504  SCIP *scip,
505  int c,
506  SCIP_CONS *cons,
507  SCIP_CONS** consSOS1
508  )
509 {
510  addOrdinaryConstraintName(c, cons);
511  idxSos1ConsNames[nSos1Conss] = c;
512  int nvars = SCIPgetNVarsSOS1(scip, cons);
513  nSos1Coefs[nSos1Conss] = nvars;
514  sos1Coefs[nSos1Conss] = new SCIP_Real[nvars];
515  idxSos1CoefsVars[nSos1Conss] = new int[nvars];
516  SCIP_Real *weights = SCIPgetWeightsSOS1(scip, cons);
517  SCIP_VAR **vars = SCIPgetVarsSOS1(scip,cons);
518  for( int v = 0; v < nvars; v++ )
519  {
520  sos1Coefs[nSos1Conss][v] = weights[v];
521  idxSos1CoefsVars[nSos1Conss][v] = SCIPvarGetProbindex(vars[v]);
522  }
523  // store constraint
524  consSOS1[nSos1Conss++] = cons;
525 }
526 
527 void
528 ScipParaInstance::createSos1ConstraintsInSCIP(
529  SCIP *scip
530  )
531 {
532  SCIP_VAR **vars = SCIPgetVars(scip);
533  for(int c = 0; c < nSos1Conss; ++c )
534  {
535  SCIP_CONS* cons;
536  SCIP_VAR **varsInCons = new SCIP_VAR*[nSos1Coefs[c]];
537  for( int v = 0; v < nSos1Coefs[c]; v++)
538  {
539  varsInCons[v] = vars[idxSos1CoefsVars[c][v]];
540  }
541  SCIP_CALL_ABORT( SCIPcreateConsSOS1(scip, &cons,
542  &consNames[posConsNames[idxSos1ConsNames[c]]], nSos1Coefs[c], varsInCons, sos1Coefs[c],
543  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE ) );
544  SCIP_CALL_ABORT( SCIPaddCons(scip, cons) );
545  SCIPdebug( SCIP_CALL_ABORT( SCIPprintCons(scip, cons, NULL) ) );
546  SCIP_CALL_ABORT( SCIPreleaseCons(scip, &cons) );
547  delete [] varsInCons;
548  }
549 }
550 
551 void
552 ScipParaInstance::setSos2Constraint(
553  SCIP *scip,
554  int c,
555  SCIP_CONS *cons,
556  SCIP_CONS** consSOS2
557  )
558 {
559  addOrdinaryConstraintName(c, cons);
560  idxSos2ConsNames[nSos2Conss] = c;
561  int nvars = SCIPgetNVarsSOS2(scip, cons);
562  nSos2Coefs[nSos2Conss] = nvars;
563  sos2Coefs[nSos2Conss] = new SCIP_Real[nvars];
564  idxSos2CoefsVars[nSos2Conss] = new int[nvars];
565  SCIP_Real *weights = SCIPgetWeightsSOS2(scip, cons);
566  SCIP_VAR **vars = SCIPgetVarsSOS2(scip,cons);
567  for( int v = 0; v < nvars; v++ )
568  {
569  sos2Coefs[nSos2Conss][v] = weights[v];
570  idxSos2CoefsVars[nSos2Conss][v] = SCIPvarGetProbindex(vars[v]);
571  }
572  // store constraint
573  consSOS2[nSos2Conss++] = cons;
574 }
575 
576 void
577 ScipParaInstance::createSos2ConstraintsInSCIP(
578  SCIP *scip
579  )
580 {
581  SCIP_VAR **vars = SCIPgetVars(scip);
582  for(int c = 0; c < nSos2Conss; ++c )
583  {
584  SCIP_CONS* cons;
585  SCIP_VAR **varsInCons = new SCIP_VAR*[nSos2Coefs[c]];
586  for( int v = 0; v < nSos2Coefs[c]; v++)
587  {
588  varsInCons[v] = vars[idxSos2CoefsVars[c][v]];
589  }
590  SCIP_CALL_ABORT( SCIPcreateConsSOS2(scip, &cons,
591  &consNames[posConsNames[idxSos2ConsNames[c]]], nSos2Coefs[c], varsInCons, sos2Coefs[c],
592  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE ) );
593  SCIP_CALL_ABORT( SCIPaddCons(scip, cons) );
594  SCIPdebug( SCIP_CALL_ABORT( SCIPprintCons(scip, cons, NULL) ) );
595  SCIP_CALL_ABORT( SCIPreleaseCons(scip, &cons) );
596  delete [] varsInCons;
597  }
598 }
599 
600 /***********************************************************************************************
601  * getActiveVariables is copied from read_lp.c of SCIP code
602  */
603 void
604 ScipParaInstance::getActiveVariables(
605  SCIP* scip, /**< SCIP data structure */
606  SCIP_VAR** vars, /**< vars array to get active variables for */
607  SCIP_Real* scalars, /**< scalars a_1, ..., a_n in linear sum a_1*x_1 + ... + a_n*x_n + c */
608  int* nvars, /**< pointer to number of variables and values in vars and vals array */
609  SCIP_Real* constant, /**< pointer to constant c in linear sum a_1*x_1 + ... + a_n*x_n + c */
610  SCIP_Bool transformed /**< transformed constraint? */
611  )
612 {
613  int requiredsize;
614  int v;
615 
616  assert( scip != NULL );
617  assert( vars != NULL );
618  assert( scalars != NULL );
619  assert( nvars != NULL );
620  assert( constant != NULL );
621 
622  if( transformed )
623  {
624  SCIP_CALL_ABORT( SCIPgetProbvarLinearSum(scip, vars, scalars, nvars, *nvars, constant, &requiredsize, TRUE) );
625 
626  if( requiredsize > *nvars )
627  {
628  *nvars = requiredsize;
629  SCIP_CALL_ABORT( SCIPreallocBufferArray(scip, &vars, *nvars ) );
630  SCIP_CALL_ABORT( SCIPreallocBufferArray(scip, &scalars, *nvars ) );
631 
632  SCIP_CALL_ABORT( SCIPgetProbvarLinearSum(scip, vars, scalars, nvars, *nvars, constant, &requiredsize, TRUE) );
633  assert( requiredsize <= *nvars );
634  }
635  }
636  else
637  {
638  for( v = 0; v < *nvars; ++v )
639  SCIP_CALL_ABORT( SCIPvarGetOrigvarSum(&vars[v], &scalars[v], constant) );
640  }
641 }
642 
643 /***********************************************************************************************
644  * collectAggregatedVars is copied from read_lp.c of SCIP code
645  */
646 void
647 ScipParaInstance::collectAggregatedVars(
648  SCIP* scip, /**< SCIP data structure */
649  int nvars, /**< number of mutable variables in the problem */
650  SCIP_VAR** vars, /**< variable array */
651  int* nAggregatedVars, /**< number of aggregated variables on output */
652  SCIP_VAR*** aggregatedVars, /**< array storing the aggregated variables on output */
653  SCIP_HASHTABLE** varAggregated /**< hashtable for checking duplicates */
654  )
655 {
656  int j;
657 
658  /* check variables */
659  for (j = 0; j < nvars; ++j)
660  {
661  SCIP_VARSTATUS status;
662  SCIP_VAR* var;
663 
664  var = vars[j];
665  status = SCIPvarGetStatus(var);
666 
667  /* collect aggregated variables in a list */
668  if( status >= SCIP_VARSTATUS_AGGREGATED )
669  {
670  assert( status == SCIP_VARSTATUS_AGGREGATED ||
671  status == SCIP_VARSTATUS_MULTAGGR ||
672  status == SCIP_VARSTATUS_NEGATED );
673 
674  if ( ! SCIPhashtableExists(*varAggregated, (void*) var) )
675  {
676  (*aggregatedVars)[(*nAggregatedVars)++] = var;
677  SCIP_CALL_ABORT( SCIPhashtableInsert(*varAggregated, (void*) var) );
678  }
679  }
680  }
681 }
682 
683 void
684 ScipParaInstance::setAggregatedConstraint(
685  SCIP* scip, /**< SCIP data structure */
686  int c, /** aggregated constraint number */
687  const char* constName, /**< constraint name */
688  SCIP_VAR** vars, /**< array of variables */
689  SCIP_Real* vals, /**< array of values */
690  int nvars, /**< number of variables */
691  SCIP_Real lhsAndrhs /**< right hand side = left hand side */
692  )
693 {
694  posAggregatedVarNames[c] = lAggregatedVarNames;
695  (void) strcpy( &aggregatedVarNames[lAggregatedVarNames], SCIPvarGetName(vars[c]) );
696  lAggregatedVarNames += strlen( SCIPvarGetName(vars[c]) );
697 
698  posAggregatedConsNames[c] = lAggregatedConsNames;
699  (void) strcpy( &aggregatedConsNames[lAggregatedConsNames], constName );
700  lAggregatedConsNames += strlen( constName )+ 1;
701 
702  aggregatedLhsAndLhss[c] = lhsAndrhs;
703  nAggregatedCoefs[c] = nvars;
704  int v;
705  for(v = 0; v < nvars - 1; ++v)
706  {
707  aggregatedCoefs[nLinearConss][v] = vals[v];
708  idxLinearCoefsVars[nLinearConss][v] = SCIPvarGetProbindex(vars[v]);
709  }
710  aggregatedCoefs[nLinearConss][v] = vals[v];
711  idxLinearCoefsVars[nLinearConss][v] = c;
712  nAggregatedConss++;
713 }
714 
715 void
716 ScipParaInstance::setAggregatedConstrains(
717  SCIP* scip, /**< SCIP data structure */
718  int nvars, /**< number of mutable variables in the problem */
719  int nAggregatedVars, /**< number of aggregated variables */
720  SCIP_VAR** aggregatedVars /**< array storing the aggregated variables */
721  )
722 {
723  SCIP_VAR** activevars;
724  SCIP_Real* activevals;
725  int nactivevars;
726  SCIP_Real activeconstant = 0.0;
727  char consname[UG::LpMaxNamelen];
728 
729  assert( scip != NULL );
730 
731  /* write aggregation constraints */
732  SCIP_CALL_ABORT( SCIPallocBufferArray(scip, &activevars, nvars) );
733  SCIP_CALL_ABORT( SCIPallocBufferArray(scip, &activevals, nvars) );
734 
735  /* compute lengths of aggregatedVarNames and aggregatedConsNames areas */
736  lAggregatedVarNames = 0;
737  lAggregatedConsNames = 0;
738  for( int j = 0; j < nAggregatedVars; j++ )
739  {
740  lAggregatedVarNames += strlen(SCIPvarGetName(aggregatedVars[j])) + 1;
741  (void) SCIPsnprintf(consname, UG::LpMaxNamelen, "aggr_%s", SCIPvarGetName(aggregatedVars[j]));
742  lAggregatedConsNames += strlen(consname) + 1;
743  }
744 
745  if( nAggregatedVars )
746  {
747  /* allocate aggregatedVarNames and aggregatedConsNames areas */
748  aggregatedVarNames = new char[lAggregatedVarNames];
749  aggregatedConsNames = new char[lAggregatedConsNames];
750  }
751 
752  /* set aggregated constraints */
753  nAggregatedConss = 0;
754  lAggregatedVarNames = 0;
755  lAggregatedConsNames = 0;
756  for (int j = 0; j < nAggregatedVars; ++j)
757  {
758  /* set up list to obtain substitution variables */
759  nactivevars = 1;
760 
761  activevars[0] = aggregatedVars[j];
762  activevals[0] = 1.0;
763  activeconstant = 0.0;
764 
765  /* retransform given variables to active variables */
766  getActiveVariables(scip, activevars, activevals, &nactivevars, &activeconstant, TRUE);
767 
768  activevals[nactivevars] = -1.0;
769  activevars[nactivevars] = aggregatedVars[j];
770  ++nactivevars;
771 
772  /* set constraint */
773  (void) SCIPsnprintf(consname, UG::LpMaxNamelen, "aggr_%s", SCIPvarGetName(aggregatedVars[j]));
774  setAggregatedConstraint(scip, j, consname, activevars, activevals, nactivevars, - activeconstant );
775  }
776  assert(nAggregatedConss == nAggregatedVars);
777 
778  /* free buffer arrays */
779  SCIPfreeBufferArray(scip, &activevars);
780  SCIPfreeBufferArray(scip, &activevals);
781 }
782 
783 void
784 ScipParaInstance::createAggregatedVarsAndConstrainsInSCIP(
785  SCIP *scip
786  )
787 {
788  SCIP_VAR **vars = SCIPgetVars(scip);
789  for(int c = 0; c < nAggregatedConss; ++c )
790  {
791  SCIP_CONS* cons;
792  SCIP_VAR **varsInCons = new SCIP_VAR*[nAggregatedCoefs[c]];
793  int v;
794  for( v = 0; v < nAggregatedCoefs[c] - 1; v++)
795  {
796  varsInCons[v] = vars[idxAggregatedCoefsVars[c][v]];
797  }
798  SCIP_VAR* newvar;
799  /* create new variable of the given name */
800  SCIP_CALL_ABORT( SCIPcreateVar(scip, &newvar, &aggregatedVarNames[posAggregatedVarNames[c]], 0.0, 1.0, 0.0, SCIP_VARTYPE_BINARY,
801  TRUE, FALSE, NULL, NULL, NULL, NULL, NULL) );
802  SCIP_CALL_ABORT( SCIPaddVar(scip, newvar) );
803  /* because the variable was added to the problem, it is captured by SCIP and we can safely release it right now
804  * without making the returned *var invalid
805  */
806  varsInCons[v] = newvar;
807  assert( SCIPvarGetProbindex(newvar) == (nVars + c) );
808  SCIP_CALL_ABORT( SCIPreleaseVar(scip, &newvar) );
809 
810  SCIP_CALL_ABORT( SCIPcreateConsLinear(scip, &cons,
811  &aggregatedConsNames[posAggregatedConsNames[c]], nAggregatedCoefs[c], varsInCons, aggregatedCoefs[c],aggregatedLhsAndLhss[c], aggregatedLhsAndLhss[c],
812  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE) );
813  SCIP_CALL_ABORT( SCIPaddCons(scip, cons) );
814  SCIPdebug( SCIP_CALL_ABORT( SCIPprintCons(scip, cons, NULL) ) );
815  SCIP_CALL_ABORT( SCIPreleaseCons(scip, &cons) );
816  delete [] varsInCons;
817  }
818 }
819 
820 /** constractor : only called from ScipInitiator */
822  SCIP *scip,
823  int method
824  ) : orgScip(0), paraInstanceScip(0), lProbName(0), probName(0), nCopies(0), origObjSense(0), objScale(0.0), objOffset(0.0), nVars(0), varIndexRange(0),
825  varLbs(0), varUbs(0), objCoefs(0), varTypes(0), lVarNames(0), varNames(0),
826  posVarNames(0), mapToOriginalIndecies(0), mapToSolverLocalIndecies(0), // mapToProbIndecies(0),
827  nConss(0), lConsNames(0), consNames(0),
828  posConsNames(0), copyIncreasedVariables(false),
829  nLinearConss(0), idxLinearConsNames(0), linearLhss(0),
830  linearRhss(0), nLinearCoefs(0), linearCoefs(0), idxLinearCoefsVars(0),
831  nSetppcConss(0), idxSetppcConsNames(0), nIdxSetppcVars(0), setppcTypes(0),
832  idxSetppcVars(0), nLogicorConss(0), idxLogicorConsNames(0), nIdxLogicorVars(0), idxLogicorVars(0),
833  nKnapsackConss(0), idxKnapsackConsNames(0), capacities(0), nLKnapsackCoefs(0),
834  knapsackCoefs(0), idxKnapsackCoefsVars(0),
835  nVarboundConss(0), idxVarboundConsNames(0), varboundLhss(0),varboundRhss(0), idxVarboundCoefVar1s(0),
836  varboundCoef2s(0), idxVarboundCoefVar2s(0),
837  nVarBoundDisjunctionConss(0), idxBoundDisjunctionConsNames(0), nVarsBoundDisjunction(0),
838  idxVarBoundDisjunction(0), boundTypesBoundDisjunction(0), boundsBoundDisjunction(0),
839  nSos1Conss(0),idxSos1ConsNames(0), nSos1Coefs(0), sos1Coefs(0), idxSos1CoefsVars(0),
840  nSos2Conss(0), idxSos2ConsNames(0), nSos2Coefs(0), sos2Coefs(0), idxSos2CoefsVars(0),
841  nAggregatedConss(0), lAggregatedVarNames(0), aggregatedVarNames(0), posAggregatedVarNames(0),
842  lAggregatedConsNames(0), aggregatedConsNames(0), posAggregatedConsNames(0),
843  aggregatedLhsAndLhss(0), nAggregatedCoefs(0), aggregatedCoefs(0), idxAggregatedCoefsVars(0),
844  userPlugins(0)
845 {
846  assert( scip != NULL );
847  assert( SCIPgetStage(scip) == SCIP_STAGE_TRANSFORMED ||
848  SCIPgetStage(scip) == SCIP_STAGE_PRESOLVED ||
849  SCIPgetStage(scip) == SCIP_STAGE_SOLVING ||
850  SCIPgetStage(scip) == SCIP_STAGE_SOLVED ); // in case that presolving or root node solving solved the problem
851 
852  // assert( SCIPgetStage(scip) == SCIP_STAGE_PRESOLVED ||
853  // SCIPgetStage(scip) == SCIP_STAGE_SOLVING ||
854  // SCIPgetStage(scip) == SCIP_STAGE_SOLVED ); // in case that presolving or root node solving solved the problem
855 
856  lProbName = strlen(SCIPgetProbName(scip));
857  probName = new char[lProbName+1];
858  strcpy(probName, SCIPgetProbName(scip));
859 
860  /** set objsen */
861  origObjSense = SCIPgetObjsense(scip); // always minimization problem
862  objScale = SCIPgetTransObjscale(scip); // Stefan is making now
863  objOffset = SCIPgetTransObjoffset(scip); // Stefan is making now
864 
865  paraInstanceScip = scip;
866 
867  if( SCIPgetStage(scip) == SCIP_STAGE_TRANSFORMED || SCIPgetStage(scip) == SCIP_STAGE_SOLVED ) return;
868 
869  if( method == 2 )
870  {
871  return; // Solver reads given instance file.
872  }
873 
874  // if( method == 0 || method == 2 )
875  if( method == 0 )
876  {
877  nVars = SCIPgetNVars(scip);
879  SCIP_VAR **vars = SCIPgetVars(scip);
880 
881  /* make varName and objCoefs and ovnm */
882  posVarNames = new int[nVars];
883  objCoefs = new SCIP_Real[nVars];
884  // mapToOriginalIndecies = new int[nVars];
885 
886  lVarNames = 0;
887  for(int v = 0; v < nVars; ++v)
888  {
889  posVarNames[v] = lVarNames;
890  objCoefs[v] = SCIPvarGetObj(vars[v]);
891  assert(SCIPvarGetProbindex(vars[v])!=-1);
892  assert(SCIPvarGetProbindex(vars[v]) == v);
893  lVarNames += strlen(SCIPvarGetName(vars[v])) + 1;
894  }
895  varNames = new char[lVarNames];
896  varLbs = new SCIP_Real[nVars];
897  varUbs = new SCIP_Real[nVars];
898  varTypes = new int[nVars];
899  for(int v = 0; v < nVars; ++v )
900  {
901  SCIP_VAR *var = vars[v];
902  strcpy (&varNames[posVarNames[v]], SCIPvarGetName(var) );
903  varLbs[SCIPvarGetProbindex(var)] = SCIPvarGetLbLocal(var); //* we should use global?
904  varUbs[SCIPvarGetProbindex(var)] = SCIPvarGetUbLocal(var); //* we should use global?
905  varTypes[SCIPvarGetProbindex(var)] = SCIPvarGetType(var);
906  }
907 
908  /*
909  if( method == 2 )
910  {
911  return; // Solver reads given instance file.
912  }
913  */
914 
915  /* make constraints */
916  nConss = SCIPgetNConss(scip);
917  SCIP_CONS **conss = SCIPgetConss(scip);
918 
919  if( nConss > 0 )
920  {
921  posConsNames = new int[nConss];
922  }
923  int tempSizeConsNames = 0;
924  /* make consNames map */
925  for( int c = 0; c < nConss; c++ )
926  {
927  tempSizeConsNames += strlen(SCIPconsGetName(conss[c])) + 1;
928  }
929  if( tempSizeConsNames > 0 )
930  {
931  consNames = new char[tempSizeConsNames];
932  }
933 
934  SCIP_CONS** consSOS1;
935  SCIP_CONS** consSOS2;
936  nSos1Conss = 0;
937  nSos2Conss = 0;
938 
939  /** collect SOS constraints in array for later output */
940  SCIP_CALL_ABORT( SCIPallocBufferArray(scip, &consSOS1, nConss) );
941  SCIP_CALL_ABORT( SCIPallocBufferArray(scip, &consSOS2, nConss) );
942 
943  SCIP_CONS *cons;
944  const char* conshdlrname;
945  SCIP_CONSHDLR* conshdlr;
946 
947  int nNotInitialActiveConss = 0;
948 
949  /** count number of each constraint */
950  for (int c = 0; c < nConss; ++c)
951  {
952  cons = conss[c];
953  assert( cons != NULL);
954 
955  /* in case the transformed is written only constraint are posted which are enabled in the current node */
956  if( !SCIPconsIsEnabled(cons) )
957  continue;
958 
959  conshdlr = SCIPconsGetHdlr(cons);
960  assert( conshdlr != NULL );
961 
962  if( SCIPconshdlrGetStartNActiveConss(conshdlr) > 0 )
963  {
964  conshdlrname = SCIPconshdlrGetName(conshdlr);
965  assert( SCIPconsIsTransformed(cons) );
966 
967  if( strcmp(conshdlrname, "linear") == 0 )
968  {
969  nLinearConss++;
970  }
971  else if( strcmp(conshdlrname, "setppc") == 0 )
972  {
973  nSetppcConss++;
974  }
975  else if ( strcmp(conshdlrname, "logicor") == 0 )
976  {
977  nLogicorConss++;
978  }
979  else if ( strcmp(conshdlrname, "knapsack") == 0 )
980  {
981  nKnapsackConss++;
982  }
983  else if ( strcmp(conshdlrname, "varbound") == 0 )
984  {
985  nVarboundConss++;
986  }
987  else if ( strcmp(conshdlrname, "bounddisjunction") == 0 )
988  {
989  nVarBoundDisjunctionConss++;
990  }
991  else if ( strcmp(conshdlrname, "SOS1") == 0 )
992  {
993  nSos1Conss++;
994  }
995  else if ( strcmp(conshdlrname, "SOS2") == 0 )
996  {
997  nSos2Conss++;
998  }
999  else
1000  {
1001  THROW_LOGICAL_ERROR3("constraint handler <", conshdlrname, "> can not print requested format");
1002  }
1003  }
1004  else
1005  {
1006  nNotInitialActiveConss++;
1007  }
1008  }
1009 
1010  assert(nConss == (nLinearConss + nSetppcConss + nLogicorConss + nKnapsackConss + nVarboundConss + nVarBoundDisjunctionConss + nSos1Conss + nSos2Conss + nNotInitialActiveConss) );
1011 
1012  allocateMemoryForOrdinaryConstraints();
1013 
1014  /** re-initialize counters for ordinary constraints */
1015  nLinearConss = 0;
1016  nSetppcConss = 0;
1017  nLogicorConss = 0;
1018  nKnapsackConss = 0;
1019  nVarboundConss = 0;
1020  nVarBoundDisjunctionConss = 0;
1021  nSos1Conss = 0;
1022  nSos2Conss = 0;
1023  nNotInitialActiveConss = 0;
1024 
1025  /** initialize length of constraint names area */
1026  lConsNames = 0;
1027  for (int c = 0; c < nConss; ++c)
1028  {
1029  cons = conss[c];
1030 
1031  /* in case the transformed is written only constraint are posted which are enabled in the current node */
1032  if( !SCIPconsIsEnabled(cons) )
1033  continue;
1034 
1035  conshdlr = SCIPconsGetHdlr(cons);
1036 
1037  if( SCIPconshdlrGetStartNActiveConss(conshdlr) > 0 )
1038  {
1039  conshdlrname = SCIPconshdlrGetName(conshdlr);
1040 
1041  if( strcmp(conshdlrname, "linear") == 0 )
1042  {
1043  setLinearConstraint( scip, c, cons );
1044  }
1045  else if( strcmp(conshdlrname, "setppc") == 0 )
1046  {
1047  setSetppcConstraint( scip, c, cons );
1048  }
1049  else if ( strcmp(conshdlrname, "logicor") == 0 )
1050  {
1051  setLogicorConstraint( scip, c, cons );
1052  }
1053  else if ( strcmp(conshdlrname, "knapsack") == 0 )
1054  {
1055  setKnapsackConstraint( scip, c, cons );
1056  }
1057  else if ( strcmp(conshdlrname, "varbound") == 0 )
1058  {
1059  setVarboundConstraint( scip, c, cons );
1060  }
1061  else if ( strcmp(conshdlrname, "bounddisjunction") == 0 )
1062  {
1063  setBoundDisjunctionConstraint( scip, c, cons );
1064  }
1065  else if ( strcmp(conshdlrname, "SOS1") == 0 )
1066  {
1067  setSos1Constraint( scip, c, cons, consSOS1);
1068  }
1069  else if ( strcmp(conshdlrname, "SOS2") == 0 )
1070  {
1071  setSos2Constraint( scip, c, cons, consSOS2);
1072  }
1073  }
1074  else
1075  {
1076  nNotInitialActiveConss++;
1077  }
1078  }
1079 
1080  assert(nConss == (nLinearConss + nSetppcConss + nLogicorConss + nKnapsackConss + nVarboundConss + nVarBoundDisjunctionConss + nSos1Conss + nSos2Conss + nNotInitialActiveConss ) );
1081 
1082  SCIP_VAR** aggregatedVars;
1083  int nAggregatedVars = 0;
1084  SCIP_HASHTABLE* varAggregated;
1085 
1086  // create hashtable for storing aggregated variables
1087  SCIP_CALL_ABORT( SCIPallocBufferArray(scip, &aggregatedVars, nVars) );
1088  SCIP_CALL_ABORT( SCIPhashtableCreate(&varAggregated, SCIPblkmem(scip), 1000, hashGetKeyVar, hashKeyEqVar, hashKeyValVar,
1089  NULL) );
1090 
1091  // check for aggregated variables in SOS1 constraints and output aggregations as linear constraints
1092  for (int c = 0; c < nSos1Conss; ++c)
1093  {
1094  cons = consSOS1[c];
1095  SCIP_VAR **consvars = SCIPgetVarsSOS1(scip, cons);
1096  int nconsvars = SCIPgetNVarsSOS1(scip, cons);
1097 
1098  collectAggregatedVars(scip, nconsvars, consvars, &nAggregatedVars, &aggregatedVars, &varAggregated);
1099  }
1100 
1101  // check for aggregated variables in SOS2 constraints and output aggregations as linear constraints
1102  for (int c = 0; c < nSos2Conss; ++c)
1103  {
1104  cons = consSOS2[c];
1105  SCIP_VAR **consvars = SCIPgetVarsSOS2(scip, cons);
1106  int nconsvars = SCIPgetNVarsSOS2(scip, cons);
1107 
1108  collectAggregatedVars(scip, nconsvars, consvars, &nAggregatedVars, &aggregatedVars, &varAggregated);
1109  }
1110 
1111  // set aggregation constraints
1112  setAggregatedConstrains(scip, nVars, nAggregatedVars, aggregatedVars );
1113 
1114  // free space
1115  SCIPfreeBufferArray(scip, &aggregatedVars);
1116  SCIPhashtableFree(&varAggregated);
1117 
1118  // free space
1119  SCIPfreeBufferArray(scip, &consSOS1);
1120  SCIPfreeBufferArray(scip, &consSOS2);
1121  }
1122  else
1123  {
1124  SCIP_CALL_ABORT( SCIPwriteTransProblem(scip, PRESOLVED_INSTANCE, "cip", TRUE ) );
1125  }
1126 }
1127 
1129  )
1130 {
1131  if(probName) delete[] probName;
1132  if(varLbs) delete[] varLbs;
1133  if(varUbs) delete[] varUbs;
1134  if(objCoefs) delete[] objCoefs;
1135  if(varTypes) delete[] varTypes;
1136  if(varNames) delete[] varNames;
1137  if(posVarNames) delete[] posVarNames;
1140  // if(mapToProbIndecies) delete[] mapToProbIndecies;
1141  if(consNames) delete[] consNames;
1142  if(posConsNames) delete[] posConsNames;
1143 
1144  /** for linear constraints */
1145  if( nLinearConss > 0 )
1146  {
1147  if( idxLinearConsNames ) delete [] idxLinearConsNames;
1148  if( linearLhss ) delete [] linearLhss;
1149  if( linearRhss ) delete [] linearRhss;
1150  if( nLinearCoefs )
1151  {
1152  for(int i = 0; i < nLinearConss; i++ )
1153  {
1154  delete [] linearCoefs[i];
1155  delete [] idxLinearCoefsVars[i];
1156  }
1157  delete [] linearCoefs;
1158  delete [] idxLinearCoefsVars;
1159  delete [] nLinearCoefs;
1160  }
1161  }
1162  /** for setppc constraints */
1163  if( nSetppcConss )
1164  {
1165  if( idxSetppcConsNames ) delete [] idxSetppcConsNames;
1166  if( nIdxSetppcVars ) delete [] nIdxSetppcVars;
1167  if( setppcTypes ) delete [] setppcTypes;
1168  if( idxSetppcVars )
1169  {
1170  for(int i = 0; i < nSetppcConss; i++ )
1171  {
1172  delete idxSetppcVars[i];
1173  }
1174  delete [] idxSetppcVars;
1175  }
1176  }
1177  /** for logical constraints */
1178  if(nLogicorConss)
1179  {
1180  if( idxLogicorConsNames) delete [] idxLogicorConsNames;
1181  if( nIdxLogicorVars ) delete [] nIdxLogicorVars;
1182  if( idxLogicorVars )
1183  {
1184  for( int i = 0; i < nLogicorConss; i++ )
1185  {
1186  delete [] idxLogicorVars[i];
1187  }
1188  delete [] idxLogicorVars;
1189  }
1190  }
1191  /** for knapsack constraints */
1192  if(nKnapsackConss)
1193  {
1194  if( idxKnapsackConsNames ) delete [] idxKnapsackConsNames;
1195  if( capacities ) delete [] capacities;
1196  if( nLKnapsackCoefs ) delete [] nLKnapsackCoefs;
1197  if( knapsackCoefs )
1198  {
1199  for( int i = 0; i < nKnapsackConss; i++ )
1200  {
1201  delete [] knapsackCoefs[i];
1202  delete [] idxKnapsackCoefsVars[i];
1203  }
1204  delete [] knapsackCoefs;
1205  delete [] idxKnapsackCoefsVars;
1206  }
1207  }
1208  /** for varbound constraints */
1209  if( nVarboundConss )
1210  {
1211  if( idxVarboundConsNames ) delete [] idxVarboundConsNames;
1212  if( idxVarboundCoefVar1s ) delete [] idxVarboundCoefVar1s;
1213  if( varboundCoef2s ) delete [] varboundCoef2s;
1214  if( idxVarboundCoefVar2s ) delete [] idxVarboundCoefVar2s;
1215  }
1216 
1217  /** for bounddisjunction constraints */
1218  if( nVarBoundDisjunctionConss )
1219  {
1220  if( idxBoundDisjunctionConsNames ) delete [] idxBoundDisjunctionConsNames;
1221  if( nVarsBoundDisjunction ) delete[] nVarsBoundDisjunction;
1222  for( int c = 0; c < nVarBoundDisjunctionConss; c++ )
1223  {
1224  if( idxVarBoundDisjunction[c] ) delete[] idxVarBoundDisjunction[c];
1225  if( boundTypesBoundDisjunction[c] ) delete[] boundTypesBoundDisjunction[c];
1226  if( boundsBoundDisjunction[c] ) delete[] boundsBoundDisjunction[c];
1227  }
1228  }
1229 
1230  /** for SOS1 constraints */
1231  if( nSos1Conss )
1232  {
1233  if( nSos1Coefs ) delete [] nSos1Coefs;
1234  if( sos1Coefs )
1235  {
1236  for( int i = 0; i < nSos1Conss; i++ )
1237  {
1238  delete [] sos1Coefs[i];
1239  delete [] idxSos1CoefsVars[i];
1240  }
1241  delete [] sos1Coefs;
1242  delete [] idxSos1CoefsVars;
1243  }
1244  }
1245  /** for SOS2 constraints */
1246  if( nSos2Conss )
1247  {
1248  if( nSos2Coefs ) delete [] nSos2Coefs;
1249  if( sos2Coefs )
1250  {
1251  for( int i = 0; i < nSos1Conss; i++ )
1252  {
1253  delete [] sos2Coefs[i];
1254  delete [] idxSos2CoefsVars[i];
1255  }
1256  delete sos2Coefs;
1257  delete idxSos2CoefsVars;
1258  }
1259  }
1260  /** for agrregated constraints */
1261  if( nAggregatedConss )
1262  {
1263  for( int i = 0; i < nAggregatedConss; i++ )
1264  {
1265  delete aggregatedCoefs[i];
1266  delete idxAggregatedCoefsVars[i];
1267  }
1268  delete [] aggregatedCoefs;
1269  delete [] idxAggregatedCoefsVars;
1270  aggregatedCoefs = 0;
1271  idxAggregatedCoefsVars = 0;
1272 
1273  if( aggregatedVarNames ) delete [] aggregatedVarNames;
1274  if( posAggregatedVarNames ) delete[] posAggregatedVarNames;
1275  if( aggregatedConsNames ) delete [] aggregatedConsNames;
1276  if( posAggregatedConsNames ) delete[] posAggregatedConsNames;
1277  if( aggregatedLhsAndLhss ) delete[] aggregatedLhsAndLhss;
1278  }
1279 
1280  if( userPlugins ) delete userPlugins;
1281 }
1282 
1283 bool
1284 ScipParaInstance::addRootNodeCuts(
1285  SCIP *tempScip,
1286  ScipDiffParamSet *scipDiffParamSetRoot
1287  )
1288 {
1289  SCIP_Longint originalLimitsNodes;
1290  SCIP_CALL_ABORT( SCIPgetLongintParam(tempScip, "limits/nodes", &originalLimitsNodes) );
1291  SCIP_CALL_ABORT( SCIPsetLongintParam(tempScip, "limits/nodes", 1) );
1292  if( scipDiffParamSetRoot ) scipDiffParamSetRoot->setParametersInScip(tempScip);
1293  SCIP_RETCODE ret = SCIPsolve(tempScip);
1294  if( ret != SCIP_OKAY )
1295  {
1296  exit(1); // LC should abort
1297  }
1298  // Then, solver status should be checked
1299  SCIP_STATUS status = SCIPgetStatus(tempScip);
1300  if( status == SCIP_STATUS_OPTIMAL ) // when sub-MIP is solved at root node, the solution may not be saved
1301  {
1302  return false;
1303  }
1304  else
1305  {
1306  if( status == SCIP_STATUS_MEMLIMIT )
1307  {
1308  std::cout << "Warning: SCIP was interrupted because the memory limit was reached" << std::endl;
1309  return false;
1310  }
1311  }
1312 
1313  SCIP_CUT** cuts;
1314  int ncuts;
1315  int ncutsadded;
1316 
1317  ncutsadded = 0;
1318  cuts = SCIPgetPoolCuts(tempScip);
1319  ncuts = SCIPgetNPoolCuts(tempScip);
1320  for( int c = 0; c < ncuts; ++c )
1321  {
1322  SCIP_ROW* row;
1323 
1324  row = SCIPcutGetRow(cuts[c]);
1325  assert(!SCIProwIsLocal(row));
1326  assert(!SCIProwIsModifiable(row));
1327  if( SCIPcutGetAge(cuts[c]) == 0 && SCIProwIsInLP(row) )
1328  {
1329  char name[SCIP_MAXSTRLEN];
1330  SCIP_CONS* cons;
1331  SCIP_COL** cols;
1332  SCIP_VAR** vars;
1333  int ncols;
1334  int i;
1335 
1336  /* create a linear constraint out of the cut */
1337  cols = SCIProwGetCols(row);
1338  ncols = SCIProwGetNNonz(row);
1339 
1340  SCIP_CALL_ABORT( SCIPallocBufferArray(tempScip, &vars, ncols) );
1341  for( i = 0; i < ncols; ++i )
1342  vars[i] = SCIPcolGetVar(cols[i]);
1343 
1344  (void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_%d", SCIProwGetName(row), SCIPgetNRuns(tempScip));
1345  SCIP_CALL_ABORT( SCIPcreateConsLinear(tempScip, &cons, name, ncols, vars, SCIProwGetVals(row),
1346  SCIProwGetLhs(row) - SCIProwGetConstant(row), SCIProwGetRhs(row) - SCIProwGetConstant(row),
1347  TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE) );
1348  SCIP_CALL_ABORT( SCIPaddCons(tempScip, cons) );
1349  SCIP_CALL_ABORT( SCIPreleaseCons(tempScip, &cons) );
1350 
1351  SCIPfreeBufferArray(tempScip, &vars);
1352 
1353  ncutsadded++;
1354  }
1355  }
1356  SCIP_CALL_ABORT( SCIPsetLongintParam(tempScip, "limits/nodes", originalLimitsNodes) );
1357  return true;
1358 }
1359 
1360 /** create presolved problem instance that is solved by ParaSCIP */
1361 void
1363  SCIP *scip,
1364  int method,
1365  bool noPreprocessingInLC,
1366  bool usetRootNodeCuts,
1367  ScipDiffParamSet *scipDiffParamSetRoot,
1368  ScipDiffParamSet *scipDiffParamSet,
1369  char *settingsNameLC,
1370  char *isolname
1371  )
1372 {
1373 
1374  switch ( method )
1375  {
1376  case 0 :
1377  {
1378  SCIP_CALL_ABORT( SCIPcreateProb(scip, probName, NULL, NULL, NULL, NULL, NULL, NULL, NULL) );
1379  SCIP_CALL_ABORT( SCIPsetObjsense(scip, SCIP_OBJSENSE_MINIMIZE) );
1380  for(int v = 0; v < nVars; ++v)
1381  {
1382  SCIP_VAR* newvar;
1383 
1384  /* create new variable of the given name */
1385  SCIP_CALL_ABORT( SCIPcreateVar(scip, &newvar, &varNames[posVarNames[v]], varLbs[v], varUbs[v], objCoefs[v],
1386  SCIP_Vartype(varTypes[v]),
1387  TRUE, FALSE, NULL, NULL, NULL, NULL, NULL) );
1388  SCIP_CALL_ABORT( SCIPaddVar(scip, newvar) );
1389  assert( SCIPvarGetProbindex(newvar) == v );
1390  /* because the variable was added to the problem, it is captured by SCIP and we can safely release it right now
1391  * without making the returned *var invalid
1392  */
1393  SCIP_CALL_ABORT( SCIPreleaseVar(scip, &newvar) );
1394  }
1395 
1396  if( nLinearConss )
1397  createLinearConstraintsInSCIP( scip );
1398  if( nSetppcConss)
1399  createSetppcConstraintsInSCIP( scip );
1400  if( nLogicorConss )
1401  createLogicorConstraintsInSCIP( scip );
1402  if( nKnapsackConss )
1403  createKnapsackConstraintsInSCIP( scip );
1404  if( nVarboundConss )
1405  createVarboundConstraintsInSCIP( scip );
1406  if( nVarBoundDisjunctionConss )
1407  createBoundDisjunctionConstraintInSCIP( scip );
1408  if( nSos1Conss )
1409  createSos1ConstraintsInSCIP( scip );
1410  if( nSos2Conss )
1411  createSos2ConstraintsInSCIP( scip );
1412  if( nAggregatedConss )
1413  createAggregatedVarsAndConstrainsInSCIP( scip );
1414  break;
1415  }
1416  case 1 :
1417  {
1418  if( scipDiffParamSet ) scipDiffParamSet->setParametersInScip(scip);
1419  SCIP_CALL_ABORT( SCIPreadProb(scip, PRESOLVED_INSTANCE, "cip" ) );
1420  nVars = SCIPgetNVars(scip);
1421  varIndexRange = nVars;
1422  SCIP_VAR **vars = SCIPgetVars(scip);
1423  varLbs = new SCIP_Real[nVars];
1424  varUbs = new SCIP_Real[nVars];
1425  for(int v = 0; v < nVars; ++v )
1426  {
1427  SCIP_VAR *var = vars[v];
1428  varLbs[SCIPvarGetProbindex(var)] = SCIPvarGetLbLocal(var); //* we should use global?
1429  varUbs[SCIPvarGetProbindex(var)] = SCIPvarGetUbLocal(var); //* we should use global?
1430  }
1431  break;
1432  }
1433  case 2 :
1434  {
1435  SCIP *tempScip = 0;
1436  SCIP_Bool success = TRUE;
1437  SCIP_CALL_ABORT( SCIPcreate(&tempScip) );
1438  SCIP_CALL_ABORT( SCIPsetIntParam(tempScip, "timing/clocktype", 2) ); // always wall clock time
1439 
1440  /* include default SCIP plugins */
1441  SCIP_CALL_ABORT( SCIPincludeDefaultPlugins(tempScip) );
1442  /** user include plugins */
1443  includeUserPlugins(tempScip);
1444 
1445 #ifdef SCIP_THREADSAFE_MESSAGEHDLRS
1446  SCIPsetMessagehdlrQuiet(tempScip, TRUE);
1447 #endif
1448  // SCIP_CALL_ABORT( SCIPreadProb(tempScip, getFileName(), NULL ) );
1449  // std::cout << "file name = " << getFileName() << std::endl;
1450  // std::cout << "tempScip = " << tempScip << std::endl;
1451  if( scipDiffParamSet ) scipDiffParamSet->setParametersInScip(tempScip);
1452  int retcode = SCIPreadProb(tempScip, getFileName(), NULL );
1453  // assert( retcode == SCIP_OKAY );
1454  if( retcode != SCIP_OKAY )
1455  {
1456  std::cout << "SCIPreadProb returns: " << retcode << std::endl;
1457  if( getFileName()[0] == '.' )
1458  {
1459  char dir[512];
1460  std::cout << "Cannot read < " << getcwd(dir, 511) << "/" << getFileName() << std::endl;
1461  }
1462  else
1463  {
1464  std::cout << "Cannot read < " << getFileName() << std::endl;
1465  }
1466  exit(1);
1467  }
1468 
1469  /* read initail solution, if it is specified */
1470  if( isolname )
1471  {
1472  SCIP_CALL_ABORT( SCIPtransformProb(tempScip));
1473  // NOTE:
1474  // When CPLEX license file cannot find, SCIPtransformProb(scip) may fail
1475  // SCIP_CALL_ABORT( SCIPreadSol(tempScip, isolname) );
1476  SCIP_CALL_ABORT( SCIPreadProb(tempScip, isolname, NULL ) );
1477  }
1478 
1479  /* change problem name */
1480  char *probNameFromFileName;
1481  char *temp = new char[strlen(getFileName())+1];
1482  (void) strcpy(temp, getFileName());
1483  SCIPsplitFilename(temp, NULL, &probNameFromFileName, NULL, NULL);
1484  SCIP_CALL_ABORT( SCIPsetProbName(tempScip, probNameFromFileName));
1485 
1486  /* presolve problem */
1487  if( noPreprocessingInLC )
1488  {
1489  SCIP_CALL_ABORT( SCIPsetIntParam(tempScip, "presolving/maxrounds", 0));
1490  }
1491  else
1492  {
1493  if( settingsNameLC )
1494  {
1495  SCIP_CALL_ABORT( SCIPreadParams(tempScip, settingsNameLC) );
1496  }
1497  /*
1498  else
1499  {
1500  SCIP_CALL_ABORT( SCIPsetPresolving(tempScip, SCIP_PARAMSETTING_FAST, TRUE) );
1501  }
1502  */
1503  }
1504  // SCIP_CALL_ABORT( SCIPsetIntParam(tempScip, "constraints/quadratic/replacebinaryprod", 0));
1505  // SCIP_CALL_ABORT( SCIPsetBoolParam(tempScip, "constraints/nonlinear/reformulate", FALSE));
1506  SCIP_CALL_ABORT( SCIPpresolve(tempScip) );
1507  SCIP_STATUS scipStatus = SCIPgetStatus(tempScip);
1508  if( scipStatus == SCIP_STATUS_OPTIMAL ) return; // the problem shold be solved in LC
1509 
1510  /* adding root node cuts, if necessary */
1511  if( usetRootNodeCuts )
1512  {
1513  if( !addRootNodeCuts(tempScip,scipDiffParamSetRoot) )
1514  {
1515  return; // the problem shold be solved in LC
1516  }
1517  }
1518 
1519  if( SCIPgetNVars(tempScip) == 0 ) // all variables were fixed in presolve
1520  {
1521  return; // the problem shold be solved in LC
1522  }
1523 
1524  assert( SCIPgetStage(scip) == SCIP_STAGE_INIT );
1525 
1526  assert( nCopies > 0 );
1527 
1528  SCIP_HASHMAP* varmap = 0;
1529  SCIP_HASHMAP* conssmap = 0;
1530 
1531  assert( nCopies == 1);
1532 
1533  if( nCopies == 1 )
1534  {
1535  // SCIP_CALL_ABORT( SCIPfree(&scip) );
1536  // SCIP_CALL_ABORT( SCIPcreate(&scip) );
1537 
1538  /* include default SCIP plugins */
1539  // SCIP_CALL_ABORT( SCIPincludeDefaultPlugins(scip) );
1540  /** user include plugins */
1541  // includeUserPlugins(scip);
1542 
1543  /* copy all plugins and settings */
1544  #if SCIP_VERSION == 211 && SCIP_SUBVERSION == 0
1545  SCIP_CALL_ABORT( SCIPcopyPlugins(tempScip, scip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
1546  TRUE, TRUE, TRUE, TRUE, &success) );
1547  #else
1548  #if SCIP_APIVERSION >= 100
1549  #if SCIP_APIVERSION >= 101
1550  SCIP_CALL_ABORT( SCIPcopyPlugins(tempScip, scip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
1551  TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
1552  #else
1553  SCIP_CALL_ABORT( SCIPcopyPlugins(tempScip, scip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
1554  TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
1555  #endif
1556  #elif SCIP_APIVERSION >= 17
1557  SCIP_CALL_ABORT( SCIPcopyPlugins(tempScip, scip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
1558  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
1559  #else
1560  SCIP_CALL_ABORT( SCIPcopyPlugins(tempScip, scip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
1561  TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
1562  #endif
1563  #endif
1564  SCIP_CALL_ABORT( SCIPcopyParamSettings(tempScip, scip) );
1565 
1566  /* create the variable mapping hash map */
1567  if( SCIPgetNVars(tempScip) > 0 )
1568  {
1569  SCIP_CALL_ABORT( SCIPhashmapCreate(&varmap, SCIPblkmem(scip), SCIPgetNVars(tempScip)) );
1570  }
1571  if( SCIPgetNConss(tempScip) > 0 )
1572  {
1573  SCIP_CALL_ABORT( SCIPhashmapCreate(&conssmap, SCIPblkmem(scip), SCIPgetNConss(tempScip)) );
1574  }
1575 
1576  /* create problem in the target SCIP and copying the source original problem data */
1577  SCIP_CALL_ABORT( SCIPcopyProb(tempScip, scip, varmap, conssmap, TRUE, probNameFromFileName) );
1578  delete [] temp; // probNameFromFileName is in temp
1579 
1580  /* copy all variables and constraints */
1581  if( SCIPgetNVars(tempScip) > 0 )
1582  {
1583 #if (SCIP_VERSION < 321 || ( SCIP_VERSION == 321 && SCIP_SUBVERSION < 2) )
1584  SCIP_CALL_ABORT( SCIPcopyVars(tempScip, scip, varmap, conssmap, TRUE) );
1585 #else
1586  SCIP_CALL_ABORT( SCIPcopyVars(tempScip, scip, varmap, conssmap, NULL, NULL, 0, TRUE) );
1587 #endif
1588  }
1589  if( SCIPgetNConss(tempScip) > 0 )
1590  {
1591  SCIP_CALL_ABORT( SCIPcopyConss(tempScip, scip, varmap, conssmap, TRUE, FALSE, &success) );
1592  }
1593 
1594 #if SCIP_APIVERSION > 39
1595  if( success )
1596  {
1597  SCIP_Bool valid;
1598 
1599  /* copy the Benders' decomposition plugins explicitly, because it requires the variable mapping hash map */
1600  SCIP_CALL_ABORT( SCIPcopyBenders(tempScip, scip, NULL, TRUE, &valid) );
1601  }
1602 #endif
1603 
1604  if( !success )
1605  {
1606  if( SCIPgetNConss(tempScip) > 0 )
1607  {
1608  SCIPhashmapFree(&conssmap);
1609  }
1610  if( SCIPgetNVars(tempScip) > 0 )
1611  {
1612  SCIPhashmapFree(&varmap);
1613  }
1614  SCIPfree(&tempScip);
1615  std::cerr << "Some constraint handler did not perform a valid copy. Cannot solve this instance." << std::endl;
1616  exit(1);
1617  }
1618  }
1619 
1620  nVars = SCIPgetNVars(tempScip);
1621  varIndexRange = nVars;
1622  int n = SCIPgetNVars(scip);
1623 
1624  // std::cout << "nVars = " << nVars << ", varIndexRange = " << varIndexRange << ", n = " << n << std::endl;
1625 
1626  assert( nVars <= n );
1627  if( nVars < n )
1628  {
1629  copyIncreasedVariables = true;
1630  varIndexRange = n;
1631  }
1632  // mapToProbIndecies = new int[SCIPgetNTotalVars(scip)];
1633  mapToOriginalIndecies = new int[SCIPgetNTotalVars(tempScip)]; // need to allocate enough for SCIPvarGetIndex(copyvar)
1634  mapToSolverLocalIndecies = new int[SCIPgetNTotalVars(tempScip)];
1635  for( int i = 0; i < SCIPgetNTotalVars(tempScip); i++ )
1636  {
1637  // mapToProbIndecies[i] = -1;
1638  mapToOriginalIndecies[i] = -1;
1639  mapToSolverLocalIndecies[i] = -1;
1640  }
1641 
1642  assert(SCIPgetNTotalVars(scip) >= SCIPgetNVars(tempScip));
1643 
1644  // SCIP_VAR **srcVars = SCIPgetVars(tempScip);
1645  SCIP_VAR **srcVars = SCIPgetVars(tempScip);
1646  // SCIP_VAR **targetVars = SCIPgetVars(scip);
1647  // for( int i = 0; i < SCIPgetNTotalVars(scip); i++ )
1648  for( int i = 0; i < SCIPgetNVars(tempScip); i++ )
1649  {
1650  SCIP_VAR* copyvar = (SCIP_VAR*)SCIPhashmapGetImage(varmap, (void*)srcVars[i]);
1651  // std::cout << i << ": index = " << SCIPvarGetIndex(copyvar) << std::endl;
1652  // assert(SCIPvarGetIndex(copyvar) >= 0);
1653  // if( copyvar && SCIPvarGetProbindex(copyvar) < n )
1654  if( copyvar )
1655  {
1656  // assert(SCIPvarGetProbindex(copyvar) >= 0);
1657  mapToOriginalIndecies[SCIPvarGetIndex(copyvar)] = i;
1658  mapToSolverLocalIndecies[i] = SCIPvarGetIndex(copyvar);
1659  // mapToProbIndecies[SCIPvarGetIndex(copyvar)] = SCIPvarGetProbindex(copyvar);
1660  // mapToOriginalIndecies[SCIPvarGetIndex(copyvar)] = SCIPvarGetProbindex(copyvar);
1661  // std::cout << i << ": " << SCIPvarGetName(copyvar) << std::endl;
1662  // std::cout << i << ": " << SCIPvarGetName(srcVars[i]) << std::endl;
1663  // std::cout << i << ": " << SCIPvarGetName(targetVars[SCIPvarGetProbindex(copyvar)]) << std::endl;
1664  }
1665  /*
1666  else
1667  {
1668  THROW_LOGICAL_ERROR1("the number of variables is inconsitent");
1669  }
1670  */
1671  }
1672 
1673  /* free hash map */
1674  if( SCIPgetNConss(tempScip) > 0 )
1675  {
1676  SCIPhashmapFree(&conssmap);
1677  }
1678  if( SCIPgetNVars(tempScip) > 0 )
1679  {
1680  SCIPhashmapFree(&varmap);
1681  }
1682 
1683  SCIPfree(&tempScip);
1684 
1685  // n = SCIPgetNVars(scip);
1686  SCIP_VAR **vars = SCIPgetVars(scip);
1687  varLbs = new SCIP_Real[nVars];
1688  varUbs = new SCIP_Real[nVars];
1689  for(int v = 0; v < nVars; ++v )
1690  {
1691  SCIP_VAR *var = vars[v];
1692  varLbs[SCIPvarGetProbindex(var)] = SCIPvarGetLbLocal(var); //* we should use global?
1693  varUbs[SCIPvarGetProbindex(var)] = SCIPvarGetUbLocal(var); //* we should use global?
1694  }
1695 
1696 // #ifdef UG_DEBUG_SOLUTION
1697 // assert(scip->set->debugsoldata == NULL);
1698 // SCIP_CALL_ABORT( SCIPdebugSolDataCreate(&((scip->set)->debugsoldata)));
1699 // SCIPdebugSetMainscipset(scip->set);
1700 // #endif
1701  break;
1702  }
1703  default :
1704  THROW_LOGICAL_ERROR1("Undefined instance transfer method");
1705  }
1706 }
1707 
1708 void
1709 ScipParaInstance::freeMemory()
1710 {
1711  /** free memory for ParaInstance to save memory */
1712  if(varLbs)
1713  {
1714  delete[] varLbs;
1715  varLbs = 0;
1716  }
1717  if(varUbs)
1718  {
1719  delete[] varUbs;
1720  varUbs = 0;
1721  }
1722  if(objCoefs)
1723  {
1724  delete[] objCoefs;
1725  objCoefs = 0;
1726  }
1727  if(varTypes)
1728  {
1729  delete[] varTypes;
1730  varTypes = 0;
1731  }
1732  if(posVarNames)
1733  {
1734  delete[] posVarNames;
1735  posVarNames = 0;
1736  }
1737 
1738  if( nLinearConss )
1739  {
1740  delete [] idxLinearConsNames;
1741  idxLinearConsNames = 0;
1742  delete [] linearLhss;
1743  linearLhss = 0;
1744  delete [] linearRhss;
1745  linearRhss = 0;
1746  for(int i = 0; i < nLinearConss; i++ )
1747  {
1748  delete [] linearCoefs[i];
1749  delete [] idxLinearCoefsVars[i];
1750  }
1751  delete [] linearCoefs;
1752  delete [] idxLinearCoefsVars;
1753  delete [] nLinearCoefs;
1754  linearCoefs = 0;
1755  idxLinearCoefsVars = 0;
1756  nLinearCoefs = 0;
1757  }
1758  if( nSetppcConss )
1759  {
1760  delete [] idxSetppcConsNames;
1761  idxSetppcConsNames = 0;
1762  delete [] nIdxSetppcVars;
1763  nIdxSetppcVars = 0;
1764  delete [] setppcTypes;
1765  setppcTypes = 0;
1766  for(int i = 0; i < nSetppcConss; i++ )
1767  {
1768  delete idxSetppcVars[i];
1769  }
1770  delete [] idxSetppcVars;
1771  idxSetppcVars = 0;
1772  }
1773  if( nLogicorConss )
1774  {
1775  delete [] idxLogicorConsNames;
1776  idxLogicorConsNames = 0;
1777  delete [] nIdxLogicorVars;
1778  nIdxLogicorVars = 0;
1779  for( int i = 0; i < nLogicorConss; i++ )
1780  {
1781  delete [] idxLogicorVars[i];
1782  }
1783  delete [] idxLogicorVars;
1784  idxLogicorVars = 0;
1785  }
1786  if( nKnapsackConss )
1787  {
1788  delete [] idxKnapsackConsNames;
1789  idxKnapsackConsNames = 0;
1790  delete [] capacities;
1791  capacities = 0;
1792  delete [] nLKnapsackCoefs;
1793  nLKnapsackCoefs = 0;
1794  for( int i = 0; i < nKnapsackConss; i++ )
1795  {
1796  delete [] knapsackCoefs[i];
1797  delete [] idxKnapsackCoefsVars[i];
1798  }
1799  delete [] knapsackCoefs;
1800  delete [] idxKnapsackCoefsVars;
1801  knapsackCoefs = 0;
1802  idxKnapsackCoefsVars = 0;
1803  }
1804  if( nSos1Conss )
1805  {
1806  delete [] nSos1Coefs;
1807  nSos1Coefs = 0;
1808  for( int i = 0; i < nSos1Conss; i++ )
1809  {
1810  delete [] sos1Coefs[i];
1811  delete [] idxSos1CoefsVars[i];
1812  }
1813  delete [] sos1Coefs;
1814  delete [] idxSos1CoefsVars;
1815  sos1Coefs = 0;
1816  idxSos1CoefsVars = 0;
1817  }
1818  if( nSos2Conss )
1819  {
1820  delete [] nSos2Coefs;
1821  nSos2Coefs = 0;
1822  for( int i = 0; i < nSos1Conss; i++ )
1823  {
1824  delete [] sos2Coefs[i];
1825  delete [] idxSos2CoefsVars[i];
1826  }
1827  delete sos2Coefs;
1828  delete idxSos2CoefsVars;
1829  sos2Coefs = 0;
1830  idxSos2CoefsVars = 0;
1831  }
1832  if( nAggregatedConss )
1833  {
1834  for( int i = 0; i < nAggregatedConss; i++ )
1835  {
1836  delete aggregatedCoefs[i];
1837  delete idxAggregatedCoefsVars[i];
1838  }
1839  delete [] aggregatedCoefs;
1840  delete [] idxAggregatedCoefsVars;
1841  delete[] aggregatedLhsAndLhss;
1842  aggregatedCoefs = 0;
1843  idxAggregatedCoefsVars = 0;
1844  aggregatedLhsAndLhss = 0;
1845  }
1846 }
1847 
1848 /** stringfy ScipParaInstance: for debug */
1849 const std::string
1851  )
1852 {
1853  std::ostringstream s;
1854 
1855  s << "lProbName = " << lProbName << std::endl;
1856  s << "probName = " << probName << std::endl;
1857  s << "objSense = " << origObjSense << std::endl;
1858  s << "objScale = " << objScale << std::endl;
1859  s << "objOffset = " << objOffset << std::endl;
1860  s << "nVars = " << nVars << ", lVarNames = " << lVarNames << std::endl;
1861  for( int i = 0; i < nVars; i++ )
1862  {
1863  s << "[" << i << "]: varNames = " << &varNames[posVarNames[i]]
1864  << ", varsLbs = " << varLbs[i] << ", varsUbs = " << varUbs[i]
1865  << ", objCoefs = " << objCoefs[i] << ", varTypes = " << varTypes[i];
1866  if( mapToOriginalIndecies )
1867  {
1868  s << ", mapToOriginalIndecies = " << mapToOriginalIndecies[i];
1869  }
1871  {
1872  s << ", mapToSolverLocalIndecies = " << mapToSolverLocalIndecies[i];
1873  }
1874  s << std::endl;
1875  }
1876  s << "nConss = " << nConss << ", lConsNames = " << lConsNames << std::endl;
1877  for( int i = 0; i < nConss; i ++ )
1878  {
1879  s<< "[" << i << "]: consNames = " << &consNames[posConsNames[i]] << std::endl;
1880  }
1881 
1882  /*************************
1883  * for linear constrains *
1884  * ***********************/
1885  s << "nLinearConss = " << nLinearConss << std::endl;
1886  for(int i = 0; i < nLinearConss; i++ )
1887  {
1888  s << "[" << i << "]: idxLinearConsNames = " << idxLinearConsNames[i]
1889  << ", linearLhss = " << linearLhss[i] << ", linearRhss = " << linearRhss[i]
1890  << ", nLinearCoefs = " << nLinearCoefs[i] << std::endl;
1891  for( int j = 0; j < nLinearCoefs[i]; j++ )
1892  {
1893  s << " [" << j << "]: linearCoefs = " << linearCoefs[i][j]
1894  << ", idxLinearCoefsVars = " << idxLinearCoefsVars[i][j] << std::endl;
1895  }
1896  }
1897 
1898  /*************************
1899  * for setppc constrains *
1900  * ***********************/
1901  s << "nSetppcConss = " << nSetppcConss << std::endl;
1902  for(int i = 0; i < nSetppcConss; i++)
1903  {
1904  s << "[" << i << "]: idxSetppcConsNames = " << idxSetppcConsNames[i]
1905  << ", nIdxSetppcVars = " << nIdxSetppcVars[i] << ", setppcTypes = " << setppcTypes[i] << std::endl;
1906  for(int j = 0; j < nIdxSetppcVars[i]; j++ )
1907  {
1908  s << " [" << j << "]: idxSetppcVars = " << idxSetppcVars[i][j] << std::endl;
1909  }
1910  }
1911 
1912  /*********************************
1913  * for logical constrains *
1914  *********************************/
1915  s << "nLogicorConss = " << nLogicorConss << std::endl;
1916  for(int i = 0; i < nLogicorConss; i++ )
1917  {
1918  s << "[" << i << "]: idxLogicorConsNames = " << idxLogicorConsNames[i]
1919  << ", nIdxLogicorVars = " << nIdxLogicorVars[i] << std::endl;
1920  for(int j = 0; j < nIdxLogicorVars[i]; j++ )
1921  {
1922  s << " [" << j << "]: idxLogicorVars = " << idxLogicorVars[i][j] << std::endl;
1923  }
1924  }
1925 
1926  /*********************************
1927  * for knapsack constrains *
1928  *********************************/
1929  s << "nKnapsackConss = " << nKnapsackConss << std::endl;
1930  for(int i = 0; i < nKnapsackConss; i++)
1931  {
1932  s << "[" << i << "]: idxKnapsackConsNames = " << idxKnapsackConsNames[i]
1933  << ", capacities = " << capacities[i] << ", nLKnapsackCoefs = " << nLKnapsackCoefs[i] << std::endl;
1934  for(int j = 0; j < nLKnapsackCoefs[i]; j++ )
1935  {
1936  s << " [" << j << "]: knapsackCoefs = " << knapsackCoefs[i][j] << ", idxKnapsackCoefsVars = " << idxKnapsackCoefsVars[i][j] << std::endl;
1937  }
1938  }
1939 
1940  /**********************************
1941  * for varbound constrains *
1942  *********************************/
1943  s << "nVarboundConss = " << nVarboundConss << std::endl;
1944  for(int i = 0; i < nVarboundConss; i++)
1945  {
1946  s << "[" << i << "]: idxVarboundConsNames = " << idxVarboundConsNames[i]
1947  << ", varboundLhss = " << varboundLhss[i] << ", varboundRhss = " << varboundRhss[i]
1948  << ", idxVarboundCoefVar1s = " << idxVarboundCoefVar1s[i] << ", varboundCoef2s = " << varboundCoef2s[i]
1949  << ", idxVarboundCoefVar2s = " << idxVarboundCoefVar2s[i] << std::endl;
1950  }
1951 
1952  /*********************************
1953  * for SOS1 constraints *
1954  *********************************/
1955  s << "nSos1Conss = " << nSos1Conss << std::endl;
1956  for( int i = 0; i < nSos1Conss; i++ )
1957  {
1958  s << "[" << i << "]: idxSos1ConsNames = " << idxSos1ConsNames[i]
1959  << ", nSos1Coefs = " << nSos1Coefs[i] << std::endl;
1960  for(int j = 0; j < nSos1Coefs[i]; j++ )
1961  {
1962  s << " [" << j << "]: sos1Coefs = " << sos1Coefs[i][j] << ", idxSos1CoefsVars = " << idxSos1CoefsVars[i][j] << std::endl;
1963  }
1964  }
1965 
1966  /*********************************
1967  * for SOS2 constraints *
1968  *********************************/
1969  s << "nSos2Conss = " << nSos2Conss << std::endl;
1970  for( int i = 0; i < nSos2Conss; i++ )
1971  {
1972  s << "[" << i << "]: idxSos2ConsNames = " << idxSos2ConsNames[i]
1973  << ", nSos2Coefs = " << nSos2Coefs[i] << std::endl;
1974  for(int j = 0; j < nSos2Coefs[i]; j++ )
1975  {
1976  s << " [" << j << "]: sos2Coefs = " << sos2Coefs[i][j] << ", idxSos2CoefsVars = " << idxSos2CoefsVars[i][j] << std::endl;
1977  }
1978  }
1979 
1980  /**********************************
1981  * for aggregated constrains *
1982  *********************************/
1983  s << "nAggregatedConss = " << nAggregatedConss << ", lAggregatedVarNames = " << lAggregatedVarNames
1984  << ", lAggregatedConsNames = " << lAggregatedConsNames << std::endl;
1985  for(int i = 0; i < nAggregatedConss; i++)
1986  {
1987  s << "[" << i << "]: aggregatedVarNames = " << aggregatedVarNames[posAggregatedVarNames[i]]
1988  << ", aggregatedConsNames = " << aggregatedConsNames[posAggregatedConsNames[i]]
1989  << ", aggregatedLhsAndLhss = " << aggregatedLhsAndLhss[i] << ", nAggregatedCoefs = " << nAggregatedCoefs[i] << std::endl;
1990  for(int j = 0; j < nAggregatedCoefs[i]; j++ )
1991  {
1992  s << " [" << j << "]: aggregatedCoefs = " << aggregatedCoefs[i][j]
1993  << ", idxAggregatedCoefsVars = " << idxAggregatedCoefsVars[i][j] << std::endl;
1994  }
1995  }
1996  return s.str();
1997 }
1998 
static const int LpMaxNamelen
Definition: paraDef.h:180
ParaInstance extenstion for SCIP solver.
void setParametersInScip(SCIP *scip)
virtual const char * getFileName()=0
void createProblem(SCIP *scip, int method, bool noPreprocessingInLC, bool usetRootNodeCuts, ScipDiffParamSet *scipDiffParamSetRoot, ScipDiffParamSet *scipDiffParamSet, char *settingsNameLC, char *isolname)
Defines for UG Framework.
static SCIP_DECL_HASHKEYVAL(hashKeyValVar)
#define THROW_LOGICAL_ERROR1(msg1)
Definition: paraDef.h:52
#define THROW_LOGICAL_ERROR2(msg1, msg2)
Definition: paraDef.h:69
static const char * PRESOLVED_INSTANCE
void includeUserPlugins(SCIP *inScip)
static SCIP_DECL_HASHGETKEY(hashGetKeyVar)
const std::string toString()
#define THROW_LOGICAL_ERROR3(msg1, msg2, msg3)
Definition: paraDef.h:86
static SCIP_DECL_HASHKEYEQ(hashKeyEqVar)