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