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