Scippy

UG

Ubiquity Generator framework

scipParaInstanceTh.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 
26 /**@file scipParaInstanceTh.cpp
27  * @brief ScipParaInstance extension for threads communication.
28  * @author Yuji Shinano
29  *
30  *
31  *
32  */
33 
34 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35 
36 
37 #include "ug_bb/bbParaComm.h"
38 #include "scipParaInstanceTh.h"
39 #include "scip/scipdefplugins.h"
40 // #ifdef UG_DEBUG_SOLUTION
41 // #include "scip/debug.h"
42 // #include "scip/struct_scip.h"
43 // #include "scip/struct_set.h"
44 // #endif
45 
46 using namespace UG;
47 using namespace ParaSCIP;
48 
49 const static char *PRESOLVED_INSTANCE = "presolved.cip";
50 
51 void
52 ScipParaInstance::copyScipEnvironment(
53  SCIP **targetscip
54  )
55 {
56  char probname[SCIP_MAXSTRLEN];
57 
58  assert(scip != NULL);
59  assert(*targetscip != NULL);
60  SCIP_Bool success = TRUE;
61 
62  /* copy all plugins and settings */
63 #if SCIP_VERSION == 211 && SCIP_SUBVERSION == 0
64  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, *targetscip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
65  TRUE, TRUE, TRUE, TRUE, &success) );
66 #else
67  #if SCIP_APIVERSION >= 100
68  #if SCIP_APIVERSION >= 101
69  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, *targetscip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
70  TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
71  #else
72  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, *targetscip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
73  TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
74  #endif
75  #elif SCIP_APIVERSION >= 17
76  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, *targetscip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
77  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
78  #else
79  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, *targetscip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
80  TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
81  #endif
82 #endif
83  SCIP_CALL_ABORT( SCIPcopyParamSettings(scip, *targetscip) );
84 
85  /* create the variable mapping hash map */
86  SCIP_HASHMAP* varmap = 0;
87  if( SCIPgetNVars(scip) > 0 )
88  {
89  SCIP_CALL_ABORT( SCIPhashmapCreate(&varmap, SCIPblkmem(*targetscip), SCIPgetNVars(scip)) );
90  }
91  SCIP_HASHMAP* conssmap = 0;
92  if( SCIPgetNConss(scip) > 0 )
93  {
94  SCIP_CALL_ABORT( SCIPhashmapCreate(&conssmap, SCIPblkmem(*targetscip), SCIPgetNConss(scip)) );
95  }
96 
97  /* create problem in the target SCIP */
98  /* get name of the original problem and add the suffix string */
99  // (void) SCIPsnprintf(probname, SCIP_MAXSTRLEN, "%s_%s", SCIPgetProbName(scip), "solver");
100  (void) SCIPsnprintf(probname, SCIP_MAXSTRLEN, "%s", SCIPgetProbName(scip));
101  // SCIP_CALL_ABORT( SCIPcreateProb(*targetscip, probname, NULL, NULL, NULL, NULL, NULL, NULL, NULL) );
102  /* create problem in the target SCIP and copying the source original problem data */
103  SCIP_CALL_ABORT( SCIPcopyProb(scip, *targetscip, varmap, conssmap, TRUE, probname) );
104 
105  /* copy all variables and constraints */
106  if( SCIPgetNVars(scip) > 0 )
107  {
108 #if (SCIP_VERSION < 321 || ( SCIP_VERSION == 321 && SCIP_SUBVERSION < 2) )
109  SCIP_CALL_ABORT( SCIPcopyVars(scip, *targetscip, varmap, conssmap, TRUE) );
110 #else
111  SCIP_CALL_ABORT( SCIPcopyVars(scip, *targetscip, varmap, conssmap, NULL, NULL, 0, TRUE) );
112 #endif
113  }
114  if( SCIPgetNConss(scip) > 0 )
115  {
116  SCIP_CALL_ABORT( SCIPcopyConss(scip, *targetscip, varmap, conssmap, TRUE, FALSE, &success) );
117  }
118 
119 #if SCIP_APIVERSION > 39
120  if( success )
121  {
122  SCIP_Bool valid;
123 
124  /* copy the Benders' decomposition plugins explicitly, because it requires the variable mapping hash map */
125  SCIP_CALL_ABORT( SCIPcopyBenders(scip, *targetscip, NULL, TRUE, &valid) );
126  }
127 #endif
128 
129  if( !success )
130  {
131  if( SCIPgetNConss(scip) > 0 )
132  {
133  SCIPhashmapFree(&conssmap);
134  }
135  if( SCIPgetNVars(scip) > 0 )
136  {
137  SCIPhashmapFree(&varmap);
138  }
139  std::cerr << "Some constraint handler did not perform a valid copy. Cannot solve this instance." << std::endl;
140  exit(1);
141  }
142 
143  /* free hash map */
144  if( SCIPgetNConss(scip) > 0 )
145  {
146  SCIPhashmapFree(&conssmap);
147  }
148  if( SCIPgetNVars(scip) > 0 )
149  {
150  SCIPhashmapFree(&varmap);
151  }
152 
153 }
154 
155 int
156 ScipParaInstanceTh::bcast(
157  ParaComm *comm,
158  int root,
159  int method
160  )
161 {
162  DEF_PARA_COMM( commTh, comm);
163 
164 #if 0 // sender side creation
165 
166  if( commTh->getRank() == root )
167  {
168  for( int i = 0; i < commTh->getSize(); i++ )
169  {
170  if( i != root )
171  {
172  SCIP *newScip;
173  SCIP_CALL_ABORT( SCIPcreate(&newScip) );
174  copyScipEnvironment(&newScip); // this copy call should be serialized
176  commTh->uTypeSend((void *)newScip, ParaInstanceType, i, TagParaInstance)
177  );
178  }
179  }
180  }
181  else
182  {
183  // SCIP *received;
185  commTh->uTypeReceive((void **)&scip, ParaInstanceType, root, TagParaInstance)
186  );
187  // scip = received;
188  }
189 #else // receiver side creation
190  if( commTh->getRank() == root )
191  {
192 
193  SCIP *tempScip;
194  SCIP_Bool success = TRUE;
195 
196  commTh->lockApp();
197  SCIP_CALL_ABORT( SCIPcreate(&tempScip) );
198 
199  /* copy all plugins and settings */
200 #if SCIP_VERSION == 211 && SCIP_SUBVERSION == 0
201  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, tempScip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
202  TRUE, TRUE, TRUE, TRUE, &success) );
203 #else
204  #if SCIP_APIVERSION >= 100
205  #if SCIP_APIVERSION >= 101
206  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, tempScip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
207  TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
208  #else
209  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, tempScip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
210  TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
211  #endif
212  #elif SCIP_APIVERSION >= 17
213  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, tempScip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
214  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
215  #else
216  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, tempScip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
217  TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
218  #endif
219 #endif
220  SCIP_CALL_ABORT( SCIPcopyParamSettings(scip, tempScip) );
221 
222  /* create the variable mapping hash map */
223  SCIP_HASHMAP* varmap = 0;
224  if( SCIPgetNVars(scip) > 0 )
225  {
226  SCIP_CALL_ABORT( SCIPhashmapCreate(&varmap, SCIPblkmem(tempScip), SCIPgetNVars(scip)) );
227  }
228  SCIP_HASHMAP* conssmap = 0;
229  if( SCIPgetNConss(scip) > 0 )
230  {
231  SCIP_CALL_ABORT( SCIPhashmapCreate(&conssmap, SCIPblkmem(tempScip), SCIPgetNConss(scip)) );
232  }
233  /* create problem in the target SCIP */
234  SCIP_CALL_ABORT( SCIPcopyProb(scip, tempScip, varmap, conssmap, TRUE, "") );
235 
236  // commTh->lockApp();
237  /* copy all variables and constraints */
238  if( SCIPgetNVars(scip) > 0 )
239  {
240 #if (SCIP_VERSION < 321 || ( SCIP_VERSION == 321 && SCIP_SUBVERSION < 2) )
241  SCIP_CALL_ABORT( SCIPcopyVars(scip, tempScip, varmap, conssmap, TRUE) );
242 #else
243  SCIP_CALL_ABORT( SCIPcopyVars(scip, tempScip, varmap, conssmap, NULL, NULL, 0, TRUE) );
244 #endif
245  }
246  if( SCIPgetNConss(scip) > 0 )
247  {
248  SCIP_CALL_ABORT( SCIPcopyConss(scip, tempScip, varmap, conssmap, TRUE, FALSE, &success) );
249  }
250 
251 #if SCIP_APIVERSION > 39
252  if( success )
253  {
254  SCIP_Bool valid;
255 
256  /* copy the Benders' decomposition plugins explicitly, because it requires the variable mapping hash map */
257  SCIP_CALL_ABORT( SCIPcopyBenders(scip, tempScip, NULL, TRUE, &valid) );
258  }
259 #endif
260 
261  commTh->unlockApp();
262  if( !success )
263  {
264  if( SCIPgetNConss(scip) > 0 )
265  {
266  SCIPhashmapFree(&conssmap);
267  }
268  if( SCIPgetNVars(scip) > 0 )
269  {
270  SCIPhashmapFree(&varmap);
271  }
272  SCIPfree(&tempScip);
273  std::cerr << "Some constraint handler did not perform a valid copy. Cannot solve this instance." << std::endl;
274  exit(1);
275  }
276 
277  nVars = SCIPgetNVars(scip); // original number
278  varIndexRange = nVars;
279  int n = SCIPgetNVars(tempScip); // copy may increase the number
280 
281  if( nVars == n )
282  {
283  if( SCIPgetNConss(scip) > 0 )
284  {
285  SCIPhashmapFree(&conssmap);
286  }
287  if( SCIPgetNVars(scip) > 0 )
288  {
289  SCIPhashmapFree(&varmap);
290  }
291  SCIPfree(&tempScip);
292  std::cout << "** ParaScipInstance copy does not increase the number of variables. **" << std::endl;
293  }
294  else
295  {
296  assert(n > nVars);
297  std::cout << "** ParaScipInstance copy increased the number of variables. **" << std::endl;
298  varIndexRange = SCIPgetNTotalVars(tempScip);
299  copyIncreasedVariables = true;
300  /**
301  // mapToOriginalIndecies = new int[n];
302  mapToOriginalIndecies = new int[SCIPgetNTotalVars(tempScip)]; // need to allocate enough for SCIPvarGetIndex(copyvar)
303  for( int i = 0; i < SCIPgetNTotalVars(tempScip); i++ )
304  {
305  mapToOriginalIndecies[i] = -1;
306  }
307  SCIP_VAR **tempVars = SCIPgetVars(tempScip);
308  for( int i = 0; i < n; i++ )
309  {
310  // mapToOriginalIndecies[i] = SCIPvarGetIndex(tempVars[i]);
311  mapToOriginalIndecies[SCIPvarGetIndex(tempVars[i])] = i;
312  }
313 
314  SCIP_CALL_ABORT( SCIPtransformProb(scip));
315  orgScip = scip;
316  nVars = n;
317  varIndexRange = SCIPgetNTotalVars(tempScip);
318  scip = tempScip;
319 
320  if( SCIPgetNConss(scip) > 0 )
321  {
322  SCIPhashmapFree(&conssmap);
323  }
324  if( SCIPgetNVars(scip) > 0 )
325  {
326  SCIPhashmapFree(&varmap);
327  }
328  SCIP_CALL_ABORT( SCIPtransformProb(scip));
329  std::cout << "** ParaScipInstance is copied twice. **" << std::endl;
330  ***/
331  }
332 
333  if( method == 0 )
334  {
335  for( int i = 0; i < commTh->getSize(); i++ )
336  {
337  if( i != root )
338  {
340  commTh->uTypeSend((void *)scip, ParaInstanceType, i, TagParaInstance)
341  );
342  }
343  }
344  }
345  else
346  {
347  for( int i = 0; i < commTh->getSize(); i++ )
348  {
349  if( i != root )
350  {
352  commTh->send(NULL, 0, ParaBYTE, i, TagParaInstance)
353  );
354  }
355  }
356  }
357  }
358  else
359  {
360  if( method == 0 )
361  {
362  SCIP *received;
363 
365  commTh->uTypeReceive((void **)&received, ParaInstanceType, root, TagParaInstance)
366  );
367 
368  commTh->lockApp();
369  SCIP_CALL_ABORT( SCIPcreate(&scip) );
370  char probname[SCIP_MAXSTRLEN];
371 
372  assert(received != NULL);
373  assert(scip != NULL);
374  SCIP_Bool success = TRUE;
375 
376  /* copy all plugins and settings */
377 #if SCIP_VERSION == 211 && SCIP_SUBVERSION == 0
378  SCIP_CALL_ABORT( SCIPcopyPlugins(received, scip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
379  TRUE, TRUE, TRUE, TRUE, &success) );
380 #else
381  #if SCIP_APIVERSION >= 100
382  #if SCIP_APIVERSION >= 101
383  SCIP_CALL_ABORT( SCIPcopyPlugins(received, scip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
384  TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
385  #else
386  SCIP_CALL_ABORT( SCIPcopyPlugins(received, scip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
387  TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
388  #endif
389  #elif SCIP_APIVERSION >= 17
390  SCIP_CALL_ABORT( SCIPcopyPlugins(received, scip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
391  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
392  #else
393  SCIP_CALL_ABORT( SCIPcopyPlugins(received, scip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
394  TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
395  #endif
396 #endif
397  // std::cout << "copy plugins : success = " << success << std::endl;
398  if( !success )
399  {
400  std::cout << "Error in SCIPcopyPlugins" << std::endl;
401  abort();
402  }
403  SCIP_CALL_ABORT( SCIPcopyParamSettings(received, scip) );
404 
405 
406  /* create the variable mapping hash map */
407  SCIP_HASHMAP* varmap = 0;
408  if( SCIPgetNVars(received) > 0 )
409  {
410  SCIP_CALL_ABORT( SCIPhashmapCreate(&varmap, SCIPblkmem(scip), SCIPgetNVars(received)) );
411  }
412  SCIP_HASHMAP* conssmap = 0;
413  if( SCIPgetNConss(received) > 0 )
414  {
415  SCIP_CALL_ABORT( SCIPhashmapCreate(&conssmap, SCIPblkmem(scip), SCIPgetNConss(received)) );
416  }
417 
418  /* create problem in the target SCIP */
419  /* get name of the original problem and add the suffix string */
420  // (void) SCIPsnprintf(probname, SCIP_MAXSTRLEN, "%s_%s", SCIPgetProbName(received), "solver");
421  (void) SCIPsnprintf(probname, SCIP_MAXSTRLEN, "%s", SCIPgetProbName(received));
422  // SCIP_CALL_ABORT( SCIPcreateProb(scip, probname, NULL, NULL, NULL, NULL, NULL, NULL, NULL) );
423  /* create problem in the target SCIP and copying the source original problem data */
424  SCIP_CALL_ABORT( SCIPcopyProb(received, scip, varmap, conssmap, TRUE, probname) );
425 
426  // commTh->lockApp();
427  /* copy all variables and constraints */
428  if( SCIPgetNVars(received) > 0 )
429  {
430 #if (SCIP_VERSION < 321 || ( SCIP_VERSION == 321 && SCIP_SUBVERSION < 2) )
431  SCIP_CALL_ABORT( SCIPcopyVars(received, scip, varmap, conssmap, TRUE) );
432 #else
433  SCIP_CALL_ABORT( SCIPcopyVars(received, scip, varmap, conssmap, NULL, NULL, 0, TRUE) );
434 #endif
435  }
436  if( SCIPgetNConss(received) > 0 )
437  {
438  SCIP_CALL_ABORT( SCIPcopyConss(received, scip, varmap, conssmap, TRUE, FALSE, &success) );
439  }
440 
441 #if SCIP_APIVERSION > 39
442  if( success )
443  {
444  SCIP_Bool valid;
445 
446  /* copy the Benders' decomposition plugins explicitly, because it requires the variable mapping hash map */
447  SCIP_CALL_ABORT( SCIPcopyBenders(received, scip, NULL, TRUE, &valid) );
448  }
449 #endif
450 
451  commTh->unlockApp();
452  if( !success )
453  {
454  if( SCIPgetNConss(received) > 0 )
455  {
456  SCIPhashmapFree(&conssmap);
457  }
458  if( SCIPgetNVars(received) > 0 )
459  {
460  SCIPhashmapFree(&varmap);
461  }
462  std::cerr << "Some constraint handler did not perform a valid copy. Cannot solve this instance." << std::endl;
463  exit(1);
464  }
465 
466  nVars = SCIPgetNVars(received);
467  varIndexRange = nVars;
468  int n = SCIPgetNVars(scip);
469 
470  // std::cout << "nVars = " << nVars << ", varIndexRange = " << varIndexRange << ", n = " << n << std::endl;
471 
472  // assert( nVars == n );
473  assert( nVars <= n );
474  if( nVars < n )
475  {
476  copyIncreasedVariables = true;
477  varIndexRange = n;
478  }
479  // mapToProbIndecies = new int[nVars];
480  // mapToProbIndecies = new int[SCIPgetNTotalVars(scip)];
481  mapToOriginalIndecies = new int[SCIPgetNTotalVars(scip)]; // need to allocate enough for SCIPvarGetIndex(copyvar)
482  mapToSolverLocalIndecies = new int[SCIPgetNTotalVars(scip)];
483  for( int i = 0; i < SCIPgetNTotalVars(scip); i++ )
484  {
485  // mapToProbIndecies[i] = -1;
486  mapToOriginalIndecies[i] = -1;
487  mapToSolverLocalIndecies[i] = -1;
488  }
489  // SCIP_VAR **srcVars = SCIPgetVars(received);
490  SCIP_VAR **srcVars = SCIPgetVars(received);
491 
492  assert(SCIPgetNTotalVars(scip) >= SCIPgetNVars(received));
493 
494  // SCIP_VAR **targetVars = SCIPgetVars(scip);
495  // int countOrigVars = 0;
496  // for( int i = 0; i < varIndexRange; i++ )
497  //for( int i = 0; i < SCIPgetNTotalVars(scip); i++ )
498  for( int i = 0; i < SCIPgetNVars(received); i++ )
499  {
500  SCIP_VAR* copyvar = (SCIP_VAR*)SCIPhashmapGetImage(varmap, (void*)srcVars[i]);
501 
502  // std::cout << i << ": index = " << SCIPvarGetIndex(copyvar) << std::endl;
503  // assert(SCIPvarGetIndex(copyvar) >= 0);
504  // if( copyvar && SCIPvarGetProbindex(copyvar) < nVars )
505  // {
506  // assert( copyvar );
507  if( copyvar )
508  {
509  // assert(SCIPvarGetProbindex(copyvar) >= 0);
510  // mapToOriginalIndecies[SCIPvarGetIndex(copyvar)] = SCIPvarGetProbindex(copyvar);
511  mapToOriginalIndecies[SCIPvarGetIndex(copyvar)] = i;
512  mapToSolverLocalIndecies[i] = SCIPvarGetIndex(copyvar);
513  // mapToProbIndecies[SCIPvarGetIndex(copyvar)] = SCIPvarGetProbindex(copyvar);
514  // assert( copyvar == targetVars[mapToProbIndecies[SCIPvarGetIndex(copyvar)]] );
515  // std::cout << "mapToOriginalIndecies[" << SCIPvarGetIndex(copyvar) << "] = " << SCIPvarGetProbindex(copyvar) << std::endl;
516  // std::cout << i << ": " << SCIPvarGetName(copyvar) << std::endl;
517  // std::cout << i << ": " << SCIPvarGetName(srcVars[SCIPvarGetProbindex(copyvar)]) << std::endl;
518  // std::cout << i << ": " << SCIPvarGetName(srcVars[mapToOriginalIndecies[SCIPvarGetIndex(copyvar)]]) << std::endl;
519  // countOrigVars++;
520  }
521  /*
522  else
523  {
524  // throw "Logical error occurred";
525  // assert( i >= nVars );
526  mapToOriginalIndecies[i] = -1; // should not be used
527  }
528  */
529  }
530  // std::cout << "%%% debg: nVars = " << nVars << std::endl;
531  // assert(nVars == countOrigVars);
532 
533  /* free hash map */
534  if( SCIPgetNConss(received) > 0 )
535  {
536  SCIPhashmapFree(&conssmap);
537  }
538  if( SCIPgetNVars(received) > 0 )
539  {
540  SCIPhashmapFree(&varmap);
541  }
542  }
543  else
544  {
546  commTh->receive( NULL, 0, ParaBYTE, root, TagParaInstance )
547  );
548  }
549 
550  }
551 #endif
552 
553  return 0;
554 
555 }
556 
557 ScipParaInstanceTh::ScipParaInstanceTh(
558  SCIP *inScip,
559  int method
560  ) : ScipParaInstance(inScip)
561 {
562  if( method == 1)
563  {
564  SCIP_CALL_ABORT( SCIPwriteTransProblem(scip, PRESOLVED_INSTANCE, "cip", TRUE ) );
565  }
566 }
567 
568 /** create presolved problem instance that is solved by ParaSCIP */
569 void
571  SCIP *inScip,
572  int method,
573  bool noPreprocessingInLC, // LC preprocesing settings
574  bool usetRootNodeCuts,
575  ScipDiffParamSet *scipDiffParamSetRoot,
576  ScipDiffParamSet *scipDiffParamSet,
577  char *settingsNameLC, // LC preprocesing settings
578  char *isolname
579  )
580 {
581  if( method == 1 )
582  {
583  scip = inScip;
584  // scipDiffParamSet->setParametersInScip(scip);
585  SCIP_CALL_ABORT( SCIPreadProb(scip, PRESOLVED_INSTANCE, "cip" ) );
586  }
587 // #ifdef UG_DEBUG_SOLUTION
588 // if( method == 0 )
589 // {
590 // assert(scip->set->debugsoldata == NULL);
591 // SCIP_CALL_ABORT( SCIPdebugSolDataCreate(&((scip->set)->debugsoldata)));
592 // SCIPdebugSetMainscipset(scip->set);
593 // }
594 // #endif
595  if( method == 0 && SCIPgetStage(inScip) == SCIP_STAGE_INIT )
596  {
597  if( SCIPgetStage(scip) == SCIP_STAGE_PROBLEM)
598  {
599  SCIP_CALL_ABORT( SCIPtransformProb(scip));
600  }
601  if( scip == inScip ) return;
602 
603  /****************************************/
604  /* the following code need to be tested */
605  /****************************************/
606  std::cout << "* If you use check mechanism, you should check the following codes. *" << std::endl;
607  SCIP_Bool success = TRUE;
608  char probname[SCIP_MAXSTRLEN];
609  /* copy all plugins and settings */
610 #if SCIP_VERSION == 211 && SCIP_SUBVERSION == 0
611  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, inScip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
612  TRUE, TRUE, TRUE, TRUE, &success) );
613 #else
614  #if SCIP_APIVERSION >= 100
615  #if SCIP_APIVERSION >= 101
616  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, inScip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
617  TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
618  #else
619  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, inScip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
620  TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
621  #endif
622  #elif SCIP_APIVERSION >= 17
623  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, inScip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
624  TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
625  #else
626  SCIP_CALL_ABORT( SCIPcopyPlugins(scip, inScip, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
627  TRUE, TRUE, TRUE, TRUE, FALSE, &success) );
628  #endif
629 #endif
630  SCIP_CALL_ABORT( SCIPcopyParamSettings(scip, inScip) );
631 
632  /* create the variable mapping hash map */
633  SCIP_HASHMAP* varmap = 0;
634  if( SCIPgetNVars(scip) > 0 )
635  {
636  SCIP_CALL_ABORT( SCIPhashmapCreate(&varmap, SCIPblkmem(inScip), SCIPgetNVars(scip)) );
637  }
638  SCIP_HASHMAP* conssmap = 0;
639  if( SCIPgetNConss(scip) > 0 )
640  {
641  SCIP_CALL_ABORT( SCIPhashmapCreate(&conssmap, SCIPblkmem(inScip), SCIPgetNConss(scip)) );
642  }
643 
644  /* create problem in the target SCIP */
645  /* get name of the original problem and add the suffix string */
646  (void) SCIPsnprintf(probname, SCIP_MAXSTRLEN, "%s_%s", SCIPgetProbName(scip), "solver_created");
647  SCIP_CALL_ABORT( SCIPcreateProb(inScip, probname, NULL, NULL, NULL, NULL, NULL, NULL, NULL) );
648  /* create problem in the target SCIP and copying the source original problem data */
649  SCIP_CALL_ABORT( SCIPcopyProb(scip, inScip, varmap, conssmap, TRUE, probname) );
650 
651  /* copy all variables and constraints */
652  if( SCIPgetNVars(scip) > 0 )
653  {
654 #if (SCIP_VERSION < 321 || ( SCIP_VERSION == 321 && SCIP_SUBVERSION < 2) )
655  SCIP_CALL_ABORT( SCIPcopyVars(scip, inScip, varmap, conssmap, TRUE) );
656 #else
657  SCIP_CALL_ABORT( SCIPcopyVars(scip, inScip, varmap, conssmap, NULL, NULL, 0, TRUE) );
658 #endif
659  }
660  if( SCIPgetNConss(scip) > 0 )
661  {
662  SCIP_CALL_ABORT( SCIPcopyConss(scip, inScip, varmap, conssmap, TRUE, FALSE, &success) );
663  }
664 
665 #if SCIP_APIVERSION > 39
666  if( success )
667  {
668  SCIP_Bool valid;
669 
670  /* copy the Benders' decomposition plugins explicitly, because it requires the variable mapping hash map */
671  SCIP_CALL_ABORT( SCIPcopyBenders(scip, inScip, NULL, TRUE, &valid) );
672  }
673 #endif
674 
675  if( !success )
676  {
677  if( SCIPgetNConss(scip) > 0 )
678  {
679  SCIPhashmapFree(&conssmap);
680  }
681  if( SCIPgetNVars(scip) > 0 )
682  {
683  SCIPhashmapFree(&varmap);
684  }
685  std::cerr << "Some constraint handler did not perform a valid copy. Cannot solve this instance." << std::endl;
686  exit(1);
687  }
688 
689  /* free hash map */
690  if( SCIPgetNConss(scip) > 0 )
691  {
692  SCIPhashmapFree(&conssmap);
693  }
694  if( SCIPgetNVars(scip) > 0 )
695  {
696  SCIPhashmapFree(&varmap);
697  }
698  }
699  if( method == 2 )
700  {
701  std::cout << "You should use instance transfer method 0 or 1!" << std::endl;
702  exit(0);
703  }
704 }
705 
static ScipParaCommTh * comm
Definition: fscip.cpp:73
static const int ParaInstanceType
Definition: paraCommCPP11.h:98
void createProblem(SCIP *scip, int method, bool noPreprocessingInLC, bool usetRootNodeCuts, ScipDiffParamSet *scipDiffParamSetRoot, ScipDiffParamSet *scipDiffParamSet, char *settingsNameLC, char *isolname)
#define PARA_COMM_CALL(paracommcall)
Definition: paraComm.h:47
#define DEF_PARA_COMM(para_comm, comm)
static const int ParaBYTE
Definition: paraComm.h:79
static const char * PRESOLVED_INSTANCE
ScipParaInstance extension for threads communication.
Base class of communicator object.
Definition: paraComm.h:101