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