GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
class_partialdecomp.cpp
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the program and library */
4/* GCG --- Generic Column Generation */
5/* a Dantzig-Wolfe decomposition based extension */
6/* of the branch-cut-and-price framework */
7/* SCIP --- Solving Constraint Integer Programs */
8/* */
9/* Copyright (C) 2010-2026 Operations Research, RWTH Aachen University */
10/* Zuse Institute Berlin (ZIB) */
11/* */
12/* Licensed under the Apache License, Version 2.0 (the "License"); */
13/* you may not use this file except in compliance with the License. */
14/* You may obtain a copy of the License at */
15/* */
16/* http://www.apache.org/licenses/LICENSE-2.0 */
17/* */
18/* Unless required by applicable law or agreed to in writing, software */
19/* distributed under the License is distributed on an "AS IS" BASIS, */
20/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
21/* See the License for the specific language governing permissions and */
22/* limitations under the License. */
23/* */
24/* You should have received a copy of the Apache-2.0 license */
25/* along with GCG; see the file LICENSE. If not visit gcg.or.rwth-aachen.de.*/
26/* */
27/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28
29/**@file class_partialdecomp.cpp
30 * @brief class storing incomplete decompositions
31 * @author Michael Bastubbe
32 * @author Hanna Franzen
33 * @author Erik Muehmer
34 *
35 */
36
37/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
38
39/*#define SCIP_DEBUG*/
40
43#include "scip/cons_setppc.h"
44#include "scip/scip.h"
45#include "gcg/scip_misc.h"
46#include "gcg/struct_detector.h"
47#include "gcg/struct_score.h"
48#include "gcg/struct_decomp.h"
49#include "gcg/cons_decomp.h"
50#include "gcg/cons_decomp.hpp"
51#include "gcg/params_visu.h"
53#include "gcg/reader_gp.h"
54
55#include <sstream>
56#include <iostream>
57#include <exception>
58#include <algorithm>
59#include <queue>
60#include <utility>
61#include <stdlib.h>
62
63#ifndef NO_AUT_LIB
66#endif
67
68
69/** macro to throw error if SCIP return status of the called function is not SCIP_OKAY */
70#define SCIP_CALL_EXC( x ) do \
71 { \
72 SCIP_RETCODE _restat_; \
73 if( ( _restat_ = ( x ) ) != SCIP_OKAY ) \
74 { \
75 SCIPerrorMessage( "Error <%d> in function call\n", _restat_ ); \
76 throw std::exception(); \
77 } \
78 } \
79 while( FALSE )
80
81namespace gcg {
82
83/** array of prime numbers */
84const int PARTIALDECOMP::primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
85 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227,
86 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349};
87
88/** length of primes array */
89const int PARTIALDECOMP::nprimes = 70;
90
91
93{
94 for( auto block : blockstructures )
95 delete block;
96}
97
98
100 DETPROBDATA* parentdetprobdata,
101 DETPROBDATA* newdetprobdata,
102 int probnr
103 )
104{
105 PARTIALDECOMP* partialdec = new PARTIALDECOMP(newdetprobdata->getGcg(), newdetprobdata->isAssignedToOrigProb());
106 int nblocks = (int) blockconss.size();
107 char buffer[SCIP_MAXSTRLEN];
108 std::vector<int> rowmapping(parentdetprobdata->getNConss(), -1);
109 std::vector<int> colmapping(parentdetprobdata->getNVars(), -1);
110 std::vector<int> reversecolmapping(newdetprobdata->getNVars(), -1);
111
112 for( int cons = 0; cons < (int)rowmapping.size(); ++cons)
113 {
114 SCIPsnprintf(buffer, SCIP_MAXSTRLEN, "p%d_%s", probnr, SCIPconsGetName(parentdetprobdata->getCons(cons)));
115 int idx = newdetprobdata->getIndexForCons(buffer);
116 if( idx >= 0 )
117 rowmapping[cons] = idx;
118 }
119
120 for( int var = 0; var < (int)colmapping.size(); ++var)
121 {
122 SCIPsnprintf(buffer, SCIP_MAXSTRLEN, "pr%d_%s", probnr, SCIPvarGetName(parentdetprobdata->getVar(var)));
123 int idx = newdetprobdata->getIndexForVar(buffer);
124 if( idx >= 0 )
125 {
126 colmapping[var] = idx;
127 assert(idx < (int) reversecolmapping.size());
128 reversecolmapping[idx] = var;
129 }
130 }
131
132 for( auto cons : masterconss )
133 {
134 int idx = rowmapping[cons];
135 if( idx >= 0 )
136 partialdec->fixConsToMaster(idx);
137 }
138
139 partialdec->setNBlocks(nblocks);
140 for( int block = 0; block < nblocks; ++block )
141 {
142 for( auto cons : blockconss[block] )
143 {
144 int idx = rowmapping[cons];
145 if( idx >= 0 )
146 partialdec->fixConsToBlock(idx, block);
147 }
148 for( int subblock = 0; subblock < (int)blockstructures.size(); ++subblock )
149 {
150 BLOCK_STRUCTURE* subblockstructure = blockstructures[subblock];
151 BLOCK_STRUCTURE* newsubblockstructure = NULL;
152 if( subblockstructure )
153 {
154 newsubblockstructure = subblockstructure->translateStructure(rowmapping, colmapping, TRUE);
155 }
156 partialdec->setBlockStructure(subblock, newsubblockstructure);
157 }
158 }
159
160 GCGconshdlrDecompAddPreexisitingPartialDec(newdetprobdata->getGcg(), partialdec);
161
162 if( !symmetryvardata.empty() )
163 {
164 bool success = true;
165 success = partialdec->setSymmetryInformation(
166 [&] (int b)
167 {
168 assert(b < nblocks);
169 return symmetricalblocks[b];
170 },
171 [&] (int b, int vi)
172 {
173 int var = reversecolmapping[partialdec->getVarsForBlock(b)[vi]];
176 int repvar = colmapping[symmetryvardata[var]];
177 assert(partialdec->getVarProbindexForBlock(repvar, symmetricalblocks[b]) >= 0);
178 return partialdec->getVarProbindexForBlock(repvar, symmetricalblocks[b]);
179 }
180 );
181 assert(partialdec->aggInfoCalculated());
182
183 if( !success )
184 {
185 SCIPwarningMessage(parentdetprobdata->getScip(), "Could not set symmetry information.\n");
186 }
187 }
188
189 return partialdec;
190}
191
192
194 std::vector<int>& rowmapping,
195 std::vector<int>& colmapping,
196 SCIP_Bool translatesymmetry
197 )
198{
199 BLOCK_STRUCTURE* blockstructure = new BLOCK_STRUCTURE();
200
201 for( int cons : masterconss )
202 {
203 int idx = rowmapping[cons];
204 if( idx >= 0 )
205 blockstructure->masterconss.push_back(idx);
206 }
207
208 for( auto& conss : blockconss )
209 {
210 blockstructure->blockconss.emplace_back();
211 for( int cons : conss )
212 {
213 int idx = rowmapping[cons];
214 if( idx >= 0 )
215 blockstructure->blockconss.back().push_back(idx);
216 }
217 }
218
219 if( translatesymmetry )
220 {
221 blockstructure->symmetricalblocks = symmetricalblocks;
222
223 for( auto& it : symmetryvardata )
224 {
225 int idx1 = colmapping[it.first];
226 int idx2 = colmapping[it.second];
227 assert((idx1 >= 0 && idx2 >= 0) || (idx1 < 0 && idx2 < 0));
228 if( idx1 >= 0 && idx2 >= 0 )
229 blockstructure->symmetryvardata.emplace(idx1, idx2);
230 }
231 }
232
233 for( auto block : blockstructures )
234 {
235 if( block )
236 blockstructure->blockstructures.push_back(block->translateStructure(rowmapping, colmapping, translatesymmetry));
237 else
238 blockstructure->blockstructures.emplace_back();
239 }
240
241 return blockstructure;
242}
243
245 GCG* gcgstruct,
246 bool originalProblem
247 ) :
248 gcg(gcgstruct), scip(GCGgetOrigprob(gcg)), nblocks( 0 ), masterconss( 0 ),
249 mastervars( 0 ), conssforblocks( 0 ), varsforblocks( 0 ), linkingvars( 0 ), linkingvarsforblocks(0), stairlinkingvars( 0 ),
250 ncoeffsforblock(std::vector<int>(0)), ncoeffsforblockformastercons(0),
251 varsforblocksorted(true), stairlinkingvarsforblocksorted(true),
252 conssforblocksorted(true), linkingvarssorted(true), mastervarssorted(true),
253 masterconsssorted(true), hashvalue( 0 ), hvoutdated(true), isselected( false ), isagginfoalreadytoexpensive(false), isfinishedbyfinisher( false ),
254 nequivalenceclasses(0), eqclasstoblocks(std::vector<std::vector<int>>(0)), blockstoeqclasses(std::vector<int>(0) ), eqclassesvarmappings(std::vector<std::vector<std::vector<int> > >(0)),
255 detectorchain( 0 ), detectorclocktimes( 0 ), pctvarstoborder( 0 ),
256 pctvarstoblock( 0 ), pctvarsfromfree( 0 ), pctconsstoborder( 0 ), pctconsstoblock( 0 ), pctconssfromfree( 0 ),
257 nnewblocks( 0 ), usedpartition(0 ), classestomaster(0 ), classestolinking(0 ), listofancestorids(0 ),
258 usergiven( USERGIVEN::NOT ), stemsfromorig( false ), original( originalProblem ), translatedpartialdecid( -1 )
259{
260 // unique id
262
263 DETPROBDATA* detprobdata = getDetprobdata();
264
265 nvars = detprobdata->getNVars();
266 nconss = detprobdata->getNConss();
267
268 // vector of bools are a special case, no "(n fields, default value)" constructor available
269 for(int i = 0; i < nvars; i++)
270 {
271 isvaropen.push_back(true);
272 isvarmaster.push_back(false);
273 openvars.push_back(i);
274 }
275
276 for(int i = 0; i < nconss; i++)
277 {
278 isconsopen.push_back(true);
279 isconsmaster.push_back(false);
280 openconss.push_back(i);
281 }
282
284
285 for( int i = 0; i < GCGconshdlrDecompGetNScores(gcg); i++ )
286 {
288
290 }
291
293}
294
295
297 const PARTIALDECOMP *partialdectocopy
298 )
299{
300 gcg = partialdectocopy->gcg;
301 scip = partialdectocopy->scip;
302
303 // unique id
305
306 // rest is copied
307 nblocks = partialdectocopy->nblocks;
308 nvars = partialdectocopy->nvars;
309 nconss = partialdectocopy->nconss;
310 masterconss = partialdectocopy->masterconss;
311 mastervars = partialdectocopy->mastervars;
312 conssforblocks = partialdectocopy->conssforblocks;
313 varsforblocks = partialdectocopy->varsforblocks;
314 linkingvars = partialdectocopy->linkingvars;
315 stairlinkingvars = partialdectocopy->stairlinkingvars;
316 openvars = partialdectocopy->openvars;
317 openconss = partialdectocopy->openconss;
318 linkingvarsforblocks = partialdectocopy->linkingvarsforblocks;
319
320 isvaropen = partialdectocopy->isvaropen;
321 masterconsssorted = partialdectocopy->masterconsssorted;
322
323 isconsopen = partialdectocopy->isconsopen;
324
325 isvarmaster = partialdectocopy->isvarmaster;
326 isconsmaster = partialdectocopy->isconsmaster;
327
328 detectorchain = partialdectocopy->detectorchain;
329 detectorchaininfo = partialdectocopy->detectorchaininfo;
330 hashvalue = partialdectocopy->hashvalue;
331 usergiven = partialdectocopy->usergiven;
332 detectorclocktimes = partialdectocopy->detectorclocktimes;
333 pctvarstoborder = partialdectocopy->pctvarstoborder;
334 pctvarstoblock = partialdectocopy->pctvarstoblock;
335 pctvarsfromfree = partialdectocopy->pctvarsfromfree;
336 pctconsstoborder = partialdectocopy->pctconsstoborder;
337 pctconsstoblock = partialdectocopy->pctconsstoblock;
338 pctconssfromfree = partialdectocopy->pctconssfromfree;
339 usedpartition = partialdectocopy->usedpartition;
340 classestomaster = partialdectocopy->classestomaster;
341 classestolinking = partialdectocopy->classestolinking;
342 isfinishedbyfinisher = partialdectocopy->isfinishedbyfinisher;
343 ncoeffsforblockformastercons = partialdectocopy->ncoeffsforblockformastercons;
344 nnewblocks = partialdectocopy->nnewblocks;
345 stemsfromorig = partialdectocopy->stemsfromorig;
346 isselected = false;
347 original = partialdectocopy->original;
348 listofancestorids = partialdectocopy->listofancestorids;
349 listofancestorids.push_back(partialdectocopy->id);
350
351 varsforblocksorted = partialdectocopy->varsforblocksorted;
352 stairlinkingvarsforblocksorted = partialdectocopy->stairlinkingvarsforblocksorted;
353 conssforblocksorted = partialdectocopy->conssforblocksorted;
354 linkingvarssorted = partialdectocopy->linkingvarssorted;
355 mastervarssorted = partialdectocopy->mastervarssorted;
356 hvoutdated = partialdectocopy->hvoutdated;
357
358 nequivalenceclasses = partialdectocopy->nequivalenceclasses;
359 eqclasstoblocks = partialdectocopy->eqclasstoblocks;
360 blockstoeqclasses = partialdectocopy->blockstoeqclasses;
361 eqclassesvarmappings = partialdectocopy->eqclassesvarmappings;
362 ncoeffsforblock = partialdectocopy->ncoeffsforblock;
363 translatedpartialdecid = partialdectocopy->getTranslatedpartialdecid();
364
366
367 for( int i = 0; i < GCGconshdlrDecompGetNScores(gcg); i++ )
368 {
370
372 }
373
374 isagginfoalreadytoexpensive = partialdectocopy->isagginfoalreadytoexpensive;
375
377}
378
379
381{
383 for( auto block : blockstructures )
384 delete block;
385 SCIPhashmapFree(&maptoscores);
386}
387
388
389/** @brief checks whether two arrays of SCIP_Real's are identical
390 * @returns true iff the given arrays are identical */
391static
393 SCIP* scip, /**< SCIP data structure */
394 SCIP_Real* array1, /**< first array */
395 int array1length, /**< length of first array */
396 SCIP_Real* array2, /**< second array */
397 int array2length /**< length of second array */
398 )
399{
400 int i;
401
402 if( array1length != array2length )
403 return FALSE;
404
405 if( array1length == 0 )
406 return TRUE;
407
408 assert(array1 != NULL);
409 assert(array2 != NULL);
410
411 for( i = 0; i < array1length; i++ )
412 {
413 if( !SCIPisEQ(scip, array1[i], array2[i]) )
414 return FALSE;
415 }
416
417 return TRUE;
418}
419
420
421/** Checks whether the second value of a is lower than the second value of b
422 * @returns true iff the second value of a is lower than the second value of b */
423static
425 std::pair<int, int> const & a,
426 std::pair<int, int> const & b
427 )
428{
429 return a.second < b.second;
430}
431
432
434{
435 assert( (int) conssforblocks.size() == nblocks );
436 assert( (int) varsforblocks.size() == nblocks );
437 assert( (int) stairlinkingvars.size() == nblocks );
438
439 conssforblocks.emplace_back();
440 varsforblocks.emplace_back();
441 stairlinkingvars.emplace_back();
442 if( !blockstructures.empty() )
443 blockstructures.push_back(NULL);
444 if( !linkingvarsforblocks.empty() )
445 linkingvarsforblocks.emplace_back();
446 if( !ncoeffsforblock.empty() )
447 ncoeffsforblock.push_back(0);
448 nblocks++;
449 return nblocks - 1;
450}
451
452
454 SCIP_Real clocktime
455 )
456{
457 detectorclocktimes.push_back(clocktime);
458}
459
460
462 PARTIALDECOMP* ancestor
463 )
464{
465 /* add number of new blocks */
466 assert( ancestor != NULL );
467
468 nnewblocks.push_back( getNBlocks() - ancestor->getNBlocks() );
469 pctconssfromfree.push_back( getNConss() != 0 ? ( ancestor->getNOpenconss() - getNOpenconss() ) / (SCIP_Real) getNConss() : 0. );
470 pctvarsfromfree.push_back( getNVars() != 0 ? ( ancestor->getNOpenvars() - getNOpenvars() ) / (SCIP_Real) getNVars() : 0. );
471 pctconsstoblock.push_back( getNConss() != 0 ?
472 ( - getNOpenconss() - getNMasterconss() + ancestor->getNOpenconss() + ancestor->getNMasterconss() ) / getNConss() : 0. );
473 pctvarstoblock.push_back( getNVars() != 0 ? ( - getNOpenvars() - getNMastervars() - getNLinkingvars() - getNTotalStairlinkingvars() + ancestor->getNOpenvars()
474 + ancestor->getNMastervars() + ancestor->getNLinkingvars() + ancestor->getNTotalStairlinkingvars() ) / getNVars() : 0. );
475 pctconsstoborder.push_back( getNConss() != 0 ? ( getNMasterconss() - ancestor->getNMasterconss() ) / (SCIP_Real) getNConss() : 0. );
476 pctvarstoborder.push_back( getNVars() != 0 ? ( getNMastervars() + getNLinkingvars() + getNTotalStairlinkingvars() - ancestor->getNMastervars()
477 - ancestor->getNLinkingvars() - ancestor->getNTotalStairlinkingvars() ) / (SCIP_Real) getNVars() : 0. );
478 listofancestorids.push_back( ancestor->getID() );
479}
480
481
483 const char* decinfo
484 )
485{
486 std::stringstream help;
487 help << decinfo;
488 detectorchaininfo.push_back( help.str() );
489}
490
491
492void PARTIALDECOMP::addEmptyPartitionStatistics()
493{
494 std::vector<int> emptyVector( 0 );
495 usedpartition.push_back(NULL );
496 classestomaster.push_back( emptyVector );
497 classestolinking.push_back( emptyVector );
498}
499
500
502 int newblocks
503 )
504 {
505 nnewblocks.push_back( newblocks );
506 }
507
508
510 SCIP_Real pct
511 )
512 {
513 pctconssfromfree.push_back( pct );
514 }
515
516
518 SCIP_Real pct
519 )
520 {
521 pctconsstoblock.push_back( pct );
522 }
523
524
526 SCIP_Real pct
527 )
528 {
529 pctconsstoborder.push_back( pct );
530 }
531
532
534 SCIP_Real pct
535 )
536 {
537 pctvarsfromfree.push_back( pct );
538 }
539
540
542 SCIP_Real pct
543 )
544 {
545 pctvarstoblock.push_back( pct );
546 }
547
548
550 SCIP_Real pct
551 )
552 {
553 pctvarstoborder.push_back( pct );
554 }
555
556
558{
559 for( int b = 0; b < this->nblocks; ++ b )
560 if( conssforblocks[b].size() != 0 )
561 return true;
562 return false;
563}
564
565
567 SCIP_HASHMAP* constoblock,
568 int givenNBlocks
569 )
570{
571 int cons;
572 std::vector<int> del;
573
574 for( int i = 0; i < getNOpenconss(); ++ i )
575 {
576 cons = openconss[i];
577 if( ! SCIPhashmapExists( constoblock, (void*) (size_t) cons ) )
578 continue;
579 if( (int) (size_t) SCIPhashmapGetImage( constoblock, (void*) (size_t) cons ) - 1 == givenNBlocks )
580 {
581 setConsToMaster( cons );
582 del.push_back(cons);
583 }
584 }
585
586 /* remove assigned conss from list of open conss */
587 for(auto c : del)
589
590 sort();
592 return SCIP_OKAY;
593}
594
595
597 )
598{
599 std::vector<int> blocksOfOpenvar;
600 bool assigned = false;
601 int var;
602 int cons;
603 std::vector<int> del;
604
605 DETPROBDATA* detprobdata = this->getDetprobdata();
606
607 /* look for blocks in which the var appears */
608 for( int i = 0; i < getNOpenvars(); ++ i )
609 {
610 blocksOfOpenvar.clear();
611 var = openvars.at(i);
612 for( int b = 0; b < nblocks; ++ b )
613 {
614 for( int c = 0; c < getNConssForBlock( b ); ++ c )
615 {
616 cons = conssforblocks.at(b).at(c);
617 if( detprobdata->getVal( cons, var ) != 0 )
618 {
619 blocksOfOpenvar.push_back( b );
620 break;
621 }
622 }
623 }
624 /* assign all vars included in two consecutive blocks to stairlinking */
625 if( blocksOfOpenvar.size() == 2 && blocksOfOpenvar.at(0) + 1 == blocksOfOpenvar.at(1) )
626 {
627 setVarToStairlinking(var, blocksOfOpenvar.at(0), blocksOfOpenvar.at(1));
628 del.push_back(var);
629 assigned = true;
630 }
631 }
632
633 /* remove assigned vars from open vars list */
634 for(auto v : del)
635 deleteOpenvar(v);
636
637 sort();
638
639 return assigned;
640}
641
642
643bool PARTIALDECOMP::assignHittingOpenconss(
644 )
645{
646 int cons;
647 int var;
648 int block;
649 bool stairlinking; /* true if the cons includes stairlinkingvars */
650 bool assigned = false; /* true if open conss get assigned in this function */
651 std::vector<int>::iterator it;
652 std::vector<int> blocksOfStairlinkingvars; /* first block of the stairlinkingvars which can be found in the conss */
653 std::vector<int> blocksOfVars; /* blocks of the vars which can be found in the conss */
654 std::vector<int> blocks; /* cons can be assigned to the blocks stored in this vector */
655 std::vector<int> eraseBlock;
656 std::vector<int> del;
657
658 DETPROBDATA* detprobdata = this->getDetprobdata();
659
660 for( size_t c = 0; c < openconss.size(); ++ c )
661 {
662 cons = openconss[c];
663 stairlinking = false;
664
665 blocksOfVars.clear();
666 blocks.clear();
667 blocksOfStairlinkingvars.clear();
668 eraseBlock.clear();
669
670 /* fill out blocksOfStairlinkingvars and blocksOfBlockvars */
671 for( int b = 0; b < nblocks; ++ b )
672 {
673 for( int v = 0; v < detprobdata->getNVarsForCons( cons ); ++ v )
674 {
675 var = detprobdata->getVarsForCons( cons )[v];
676 if( isVarBlockvarOfBlock( var, b ) )
677 {
678 blocksOfVars.push_back( b );
679 break;
680 }
681 }
682 }
683
684 for( int b = 0; b < nblocks; ++ b )
685 {
686 for( int v = 0; v < detprobdata->getNVarsForCons( cons ); ++ v )
687 {
688 int var2 = detprobdata->getVarsForCons(cons)[v];
689 std::vector<int>::iterator lb = lower_bound( stairlinkingvars[b].begin(), stairlinkingvars[b].end(), var2 );
690 if( lb != stairlinkingvars[b].end() && *lb == var2 )
691 {
692 stairlinking = true;
693 blocksOfStairlinkingvars.push_back( b );
694 break;
695 }
696 }
697 }
698
699 /* fill out blocks */
700 if( stairlinking && blocksOfVars.size() < 2 )
701 {
702 if( blocksOfVars.size() == 0 )
703 {
704 blocks.push_back( blocksOfStairlinkingvars[0] );
705 blocks.push_back( blocksOfStairlinkingvars[0] + 1 );
706 for( size_t i = 1; i < blocksOfStairlinkingvars.size(); ++ i )
707 {
708 for( it = blocks.begin(); it != blocks.end(); ++ it )
709 {
710 if( * it != blocksOfStairlinkingvars[i] && * it != blocksOfStairlinkingvars[i] + 1 )
711 eraseBlock.push_back( * it );
712 }
713 for( size_t j = 0; j < eraseBlock.size(); ++ j )
714 {
715 it = find( blocks.begin(), blocks.end(), eraseBlock[j] );
716 assert( it != blocks.end() );
717 blocks.erase( it );
718 }
719 }
720 }
721 else
722 {
723 blocks.push_back( blocksOfVars[0] );
724 for( size_t i = 0; i < blocksOfStairlinkingvars.size(); ++ i )
725 {
726 if( blocks[0] != blocksOfStairlinkingvars[i] && blocks[0] != blocksOfStairlinkingvars[i] + 1 )
727 {
728 blocks.clear();
729 break;
730 }
731 }
732 }
733 }
734
735 if( blocksOfVars.size() > 1 )
736 {
737 setConsToMaster( cons );
738 del.push_back(cons);
739 assigned = true;
740 }
741 else if( ! stairlinking && blocksOfVars.size() == 1 )
742 {
743 setConsToBlock( cons, blocksOfVars[0] );
744 del.push_back(cons);
745 assigned = true;
746 }
747 else if( stairlinking && blocks.size() == 0 )
748 {
749 setConsToMaster( cons );
750 del.push_back(cons);
751 assigned = true;
752 }
753 else if( stairlinking && blocks.size() == 1 )
754 {
755 setConsToBlock( cons, blocks[0] );
756 del.push_back(cons);
757 assigned = true;
758 }
759 else if( stairlinking && blocks.size() > 1 )
760 {
761 block = blocks[0];
762 for( size_t i = 1; i < blocks.size(); ++ i )
763 {
764 if( getNConssForBlock((int) i ) < getNConssForBlock( block ) )
765 block = (int) i;
766 }
767 setConsToBlock( cons, block );
768 del.push_back(cons);
769 assigned = true;
770 }
771 }
772
773 /* remove assigned conss from list of open conss */
774 if( assigned )
775 {
776 for(auto c : del)
778 sort();
779 }
780
781 return assigned;
782}
783
784
785bool PARTIALDECOMP::assignHittingOpenvars(
786 )
787{
788 int cons;
789 int var;
790 std::vector<int> blocksOfOpenvar;
791 bool found;
792 bool assigned = false;
793 std::vector<int> del;
794
795 DETPROBDATA* detprobdata = this->getDetprobdata();
796
797 /* set vars to linking, if they can be found in more than one block;
798 * set vars to block if they can be found in only one block */
799 for( size_t i = 0; i < openvars.size(); ++ i )
800 {
801 blocksOfOpenvar.clear();
802 var = openvars.at(i);
803 assert( var >= 0 && var < nvars );
804 for( int b = 0; b < nblocks; ++ b )
805 {
806 found = false;
807 for( int c = 0; c < getNConssForBlock( b ) && ! found; ++ c )
808 {
809 cons = conssforblocks[b][c];
810 for( int v = 0; v < detprobdata->getNVarsForCons( cons ) && ! found; ++ v )
811 {
812 if( detprobdata->getVarsForCons( cons )[v] == var )
813 {
814 blocksOfOpenvar.push_back( b );
815 found = true;
816 }
817 }
818 }
819 }
820 if( blocksOfOpenvar.size() == 1 )
821 {
822 setVarToBlock(var, blocksOfOpenvar.at(0));
823 del.push_back(var);
824 assigned = true;
825 }
826 else if( blocksOfOpenvar.size() > 1 )
827 {
829 del.push_back(var);
830 assigned = true;
831 }
832 }
833
834 /* remove the stored vars from open vars list */
835 if( assigned )
836 {
837 for(auto v : del)
838 deleteOpenvar(v);
839
840 sort();
841 }
842
843 return assigned;
844}
845
846
848 )
849{
850 for( auto cons : openconss )
851 {
852 setConsToMaster(cons);
853 isconsopen[cons] = false;
854 }
855 openconss.clear();
856}
857
858
859void PARTIALDECOMP::assignOpenPartialHittingConsToMaster(
860 )
861{
862 int cons;
863 int var;
864 std::vector<int> blocksOfBlockvars; /* blocks with blockvars which can be found in the cons */
865 std::vector<int> blocksOfOpenvar; /* blocks in which the open var can be found */
866 bool master;
867 bool hitsOpenVar;
868 std::vector<bool> isblockhit;
869 std::vector<int> del;
870
871 DETPROBDATA* detprobdata = this->getDetprobdata();
872
873 /* set openconss with more than two blockvars to master */
874 for( size_t c = 0; c < openconss.size(); ++ c )
875 {
876 isblockhit= std::vector<bool>(getNBlocks(), false );
877 blocksOfBlockvars.clear();
878 master = false;
879 hitsOpenVar = false;
880 cons = openconss[c];
881
882 for( int v = 0; v < detprobdata->getNVarsForCons( cons ) && ! master; ++ v )
883 {
884 var = detprobdata->getVarsForCons( cons )[v];
885
886 if( isVarOpenvar( var ) )
887 {
888 hitsOpenVar = true;
889 continue;
890 }
891
892 if( isVarMastervar( var ) )
893 {
894 master = true;
895 setConsToMaster( cons );
896 del.push_back(cons);
897 continue;
898 }
899
900 for( int b = 0; b < nblocks; ++ b )
901 {
902 if( isblockhit[b] )
903 continue;
904
905 if( isVarBlockvarOfBlock( var, b ) )
906 {
907 blocksOfBlockvars.push_back( b );
908 isblockhit[b] = true;
909 break;
910 }
911 }
912 }
913 if( blocksOfBlockvars.size() == 1 && hitsOpenVar )
914 {
915 setConsToMaster( cons );
916 del.push_back(cons);
917 }
918 }
919
920 /* remove assigned conss from list of open conss */
921 for(auto c : del)
923 sort();
924}
925
926
927void PARTIALDECOMP::assignOpenPartialHittingToMaster(
928 )
929{
930 assignOpenPartialHittingConsToMaster( );
931 assignOpenPartialHittingVarsToMaster( );
932}
933
934
935void PARTIALDECOMP::assignOpenPartialHittingVarsToMaster(
936 )
937{
938 int cons;
939 int var;
940 std::vector<int> blocksOfOpenvar; /* blocks in which the open var can be found */
941 bool hitsOpenCons;
942 std::vector<bool> isblockhit;
943 SCIP_Bool benders;
944 std::vector<int> del;
945
946 DETPROBDATA* detprobdata = this->getDetprobdata();
947
948 SCIPgetBoolParam(scip, "detection/benders/enabled", &benders);
949
950 /* set open var to linking if it can be found in one block and open constraint */
951 for( size_t i = 0; i < openvars.size(); ++ i )
952 {
953 isblockhit= std::vector<bool>(getNBlocks(), false );
954 blocksOfOpenvar.clear();
955 var = openvars[i];
956 hitsOpenCons = false;
957
958 for( int c = 0; c < detprobdata->getNConssForVar( var ); ++ c )
959 {
960 cons = detprobdata->getConssForVar( var )[c];
961
962 if( benders && isConsMastercons( cons ) )
963 {
964 continue;
965 }
966
967 if( isConsOpencons( cons ) )
968 {
969 hitsOpenCons = true;
970 continue;
971 }
972 for( int b = 0; b < nblocks; ++ b )
973 {
974 if ( isblockhit[b] )
975 continue;
976
977 if( isConsBlockconsOfBlock( cons, b ) )
978 {
979 blocksOfOpenvar.push_back( b );
980 isblockhit[b] = true;
981 break;
982 }
983 }
984 }
985
986 if( blocksOfOpenvar.size() == 1 && hitsOpenCons )
987 {
989 del.push_back(var);
990 }
991 }
992
993 /* revome assigned vars from list of open vars */
994 for(auto v : del)
995 deleteOpenvar(v);
996
997 sort();
998}
999
1000
1002 SCIP_HASHMAP* constoblock,
1003 int additionalNBlocks
1004)
1005{
1006 int oldNBlocks = nblocks;
1007 int consblock;
1008 int cons;
1009 std::vector<int> del;
1010
1011 assert( additionalNBlocks >= 0 );
1012
1013 for( int b = 0; b < additionalNBlocks; ++ b )
1014 addBlock();
1015
1016 for( int i = 0; i < getNOpenconss(); ++ i )
1017 {
1018 cons = openconss[i];
1019
1020 if( ! SCIPhashmapExists( constoblock, (void*) (size_t) cons ) )
1021 continue;
1022 consblock = oldNBlocks + ( (int) (size_t) SCIPhashmapGetImage( constoblock, (void*) (size_t) cons ) - 1 );
1023 assert( consblock >= oldNBlocks && consblock <= nblocks );
1024 if( consblock == nblocks )
1025 {
1026 setConsToMaster( cons );
1027 del.push_back(cons);
1028 }
1029 else
1030 {
1031 setConsToBlock( cons, consblock );
1032 del.push_back(cons);
1033 }
1034 }
1035
1036 /* remove assigned conss from list of open conss */
1037 for( auto c : del )
1039
1040 deleteEmptyBlocks(false);
1041 sort();
1043 return SCIP_OKAY;
1044}
1045
1046
1048 std::vector<int> constoblock,
1049 int additionalNBlocks
1050 )
1051{
1052 int oldNBlocks = nblocks;
1053 int consblock;
1054 int cons;
1055 std::vector<int> del;
1056
1057 assert( additionalNBlocks >= 0 );
1058
1059 for( int b = 0; b < additionalNBlocks; ++ b )
1060 addBlock();
1061
1062 for( int i = 0; i < getNOpenconss(); ++ i )
1063 {
1064 cons = openconss[i];
1065
1066 if( constoblock[cons] == - 1 )
1067 continue;
1068
1069 consblock = oldNBlocks + ( constoblock[cons] - 1 );
1070 assert( consblock >= oldNBlocks && consblock <= nblocks );
1071 if( consblock == nblocks )
1072 {
1073 setConsToMaster( cons );
1074 del.push_back(cons);
1075 }
1076 else
1077 {
1078 setConsToBlock( cons, consblock );
1079 del.push_back(cons);
1080 }
1081 }
1082
1083 /* remove assigned conss from list of open conss */
1084 for( auto c : del )
1086
1087 deleteEmptyBlocks(false);
1088 sort();
1090 return SCIP_OKAY;
1091}
1092
1093
1095{
1096 /* tools to check if the openvars can still be found in a constraint yet */
1097 std::vector<int> varinblocks(nvars, -1); /* stores, in which block the variable can be found */
1098
1099 /* tools to update openvars */
1100 std::vector<int> oldOpenconss;
1101 std::vector<int> openvarsToDelete;
1102 gcg::DETPROBDATA* detprobdata = getDetprobdata();
1103
1104 if( getNLinkingvars() != 0 )
1105 {
1106 complete();
1107 return;
1108 }
1109
1110 if ( !detprobdata->isConssAdjInitialized() )
1111 detprobdata->createConssAdjacency();
1112
1113 std::vector<bool> isConsVisited(nconss, false);
1114
1115 std::vector<std::vector<int>> conssfornewblocks;
1116 std::vector<std::vector<int>> varsfornewblocks;
1117
1118 int newblocks;
1119 int largestcomponent;
1120 int sizelargestcomponent;
1121
1122 newblocks = 0;
1123 largestcomponent = -1;
1124 sizelargestcomponent = 0;
1125
1126 std::queue<int> helpqueue;
1127 std::vector<int> neighborConss;
1128
1129 assert( (int) conssforblocks.size() == getNBlocks() );
1130 assert( (int)varsforblocks.size() == getNBlocks() );
1131 assert( (int)stairlinkingvars.size() == getNBlocks() );
1132
1133 if( getNBlocks() < 0 )
1134 setNBlocks(0);
1135
1136 /* do breadth first search to find connected conss */
1137 auto constoconsider = getOpenconss();
1138 while( !constoconsider.empty() )
1139 {
1140 std::vector<int> newconss;
1141 std::vector<int> newvars;
1142
1143 assert( helpqueue.empty() );
1144 helpqueue.push(constoconsider[0]);
1145 neighborConss.clear();
1146 neighborConss.push_back(constoconsider[0]);
1147 isConsVisited[constoconsider[0]] = true;
1148
1149 while( !helpqueue.empty() )
1150 {
1151 int nodeCons = helpqueue.front();
1152 assert(isConsOpencons(nodeCons));
1153 helpqueue.pop();
1154 for( int cons : detprobdata->getConssForCons(nodeCons) )
1155 {
1156 if( isConsVisited[cons] || isConsMastercons(cons) )
1157 continue;
1158
1159 assert(isConsOpencons(cons));
1160 isConsVisited[cons] = true;
1161 neighborConss.push_back(cons);
1162 helpqueue.push(cons);
1163 }
1164 }
1165
1166 /* assign found conss and vars to a new block */
1167 for( int cons : neighborConss )
1168 {
1169 std::vector<int>::iterator consiter = std::lower_bound(constoconsider.begin(), constoconsider.end(), cons);
1170 assert(consiter != constoconsider.end() && *consiter == cons);
1171 constoconsider.erase(consiter);
1172 assert(isConsOpencons(cons));
1173 newconss.push_back(cons);
1174
1175 for( int var : detprobdata->getVarsForCons(cons) )
1176 {
1177 if( isVarLinkingvar(var) || varinblocks[var] != -1 )
1178 continue;
1179
1181 newvars.push_back(var);
1182 varinblocks[var] = newblocks;
1183 }
1184 }
1185 ++newblocks;
1186 conssfornewblocks.push_back(newconss);
1187 varsfornewblocks.push_back(newvars);
1188 }
1189
1190 for( int i = 0; i < newblocks; ++i )
1191 {
1192 if( (int)conssfornewblocks[i].size() > sizelargestcomponent )
1193 {
1194 sizelargestcomponent = (int)conssfornewblocks[i].size();
1195 largestcomponent = i;
1196 }
1197 }
1198
1199 if( newblocks > 1 )
1200 {
1201 int oldnblocks = getNBlocks();;
1202 bool largestdone = false;
1203
1204 setNBlocks(newblocks - 1 + getNBlocks());
1205
1206 for( int i = 0; i < newblocks; ++i)
1207 {
1208 if( i == largestcomponent )
1209 {
1210 largestdone = true;
1211 continue;
1212 }
1213 for( int c = 0; c < (int) conssfornewblocks[i].size() ; ++c)
1214 {
1215 fixConsToBlock(conssfornewblocks[i][c], oldnblocks + i - (largestdone ? 1 : 0) );
1216 }
1217
1218 for( int v = 0; v < (int) varsfornewblocks[i].size() ; ++v )
1219 {
1220 fixVarToBlock(varsfornewblocks[i][v], oldnblocks + i - (largestdone ? 1 : 0) );
1221 }
1222 }
1223 prepare();
1224 }
1225
1227}
1228
1229
1230SCIP_Bool PARTIALDECOMP::isAgginfoTooExpensive()
1231{
1232
1233 int limitfornconss;
1234 int limitfornvars;
1235
1236 if( isagginfoalreadytoexpensive )
1237 return TRUE;
1238
1239 SCIPgetIntParam(scip, "relaxing/gcg/aggregation/limitnconssperblock", &limitfornconss);
1240 SCIPgetIntParam(scip, "relaxing/gcg/aggregation/limitnvarsperblock", &limitfornvars);
1241
1242 /* check if calculating aggregation information is too expensive */
1243 for( int b1 = 0; b1 < getNBlocks() ; ++b1 )
1244 {
1245 for( int b2 = b1+1; b2 < getNBlocks(); ++b2 )
1246 {
1247 if( getNVarsForBlock(b1) != getNVarsForBlock(b2) )
1248 continue;
1249
1250 if( getNConssForBlock(b1) != getNConssForBlock(b2) )
1251 continue;
1252
1253 SCIPdebugMessage("Checking if agg info is too expensive for blocks %d and %d, nconss: %d, nvars: %d . \n", b1, b2, getNConssForBlock(b2), getNVarsForBlock(b2) );
1254 if( getNConssForBlock(b2) >= limitfornconss || getNVarsForBlock(b2) >= limitfornvars )
1255 {
1256 SCIPdebugMessage("Calculating agg info is too expensive, nconss: %d, nvars: %d . \n", getNConssForBlock(b2), getNVarsForBlock(b2) );
1257 isagginfoalreadytoexpensive = true;
1258 return TRUE;
1259 }
1260 }
1261
1262 }
1263
1264 /* check if there are too many master coeffs */
1265 SCIPdebugMessage("Calculated: agg info is NOT too expensive.\n");
1266 return FALSE;
1267}
1268
1269
1271 bool ignoreDetectionLimits
1272 )
1273{
1274#ifndef NO_AUT_LIB
1275 SCIP_Bool tooexpensive;
1276 SCIP_Bool useautomorphism;
1277 int searchnodelimit;
1278 int generatorlimit;
1279#endif
1280
1281 int nreps = 1;
1282
1283 if( aggInfoCalculated() || !isComplete() )
1284 return;
1285
1286#ifndef NO_AUT_LIB
1287 if( !ignoreDetectionLimits && isAgginfoTooExpensive() )
1288 tooexpensive = TRUE;
1289 else
1290 tooexpensive = FALSE;
1291 SCIPgetBoolParam(scip, "relaxing/gcg/aggregation/usesymmetrylib", &useautomorphism);
1292 SCIPgetIntParam(scip, "relaxing/gcg/aggregation/searchnodelimit", &searchnodelimit);
1293 SCIPgetIntParam(scip, "relaxing/gcg/aggregation/generatorlimit", &generatorlimit);
1294#endif
1295
1296 std::vector<std::vector<int>> identblocksforblock(getNBlocks());
1297 blockstoeqclasses.assign(getNBlocks(), -1);
1298
1299 for( int b1 = 0; b1 < getNBlocks() ; ++b1 )
1300 {
1301 std::vector<int> currrep(0);
1302 std::vector< std::vector<int> > currrepvarmapforthisrep(0);
1303 std::vector<int> identityvec(0);
1304
1305 if( !identblocksforblock[b1].empty() )
1306 continue;
1307
1308 for( int i = 0; i < getNVarsForBlock(b1); ++i )
1309 identityvec.push_back(i);
1310
1311 currrep.push_back(b1);
1312 currrepvarmapforthisrep.push_back(identityvec);
1313
1314 if( getLinkingvarsForBlock(b1).empty() )
1315 {
1316 for( int b2 = b1 + 1; b2 < getNBlocks(); ++b2 )
1317 {
1318 if( identblocksforblock[b2].empty() && getLinkingvarsForBlock(b2).empty() )
1319 {
1320 SCIP_Bool identical = FALSE;
1321 SCIP_Bool notidentical = FALSE;
1322 SCIP_HASHMAP* varmap2 = NULL;
1323 std::vector<int> varmap(getNVarsForBlock(b1), -1);
1324
1327 5 * getNVarsForBlock(b1) + 1)); /* +1 to deal with empty subproblems */
1328
1329 SCIPdebugMessage("Check identity for block %d and block %d!\n", b1, b2);
1330
1331 checkIdenticalBlocksTrivial(b1, b2, &notidentical);
1332
1333 if( !notidentical )
1334 {
1335 checkIdenticalBlocksBrute(b1, b2, varmap, varmap2, &identical);
1336
1337 #ifndef NO_AUT_LIB
1338 if( useautomorphism && !tooexpensive && !identical )
1339 checkIdenticalBlocksAutomorphism(b1, b2, varmap, varmap2, &identical,
1340 searchnodelimit >= 0 ? searchnodelimit : 0u, generatorlimit >= 0 ? generatorlimit : 0u);
1341 #endif
1342 }
1343 else
1344 identical = FALSE;
1345
1346 if( identical )
1347 {
1348 SCIPdebugMessage("Block %d is identical to block %d!\n", b1, b2);
1349 identblocksforblock[b1].push_back(b2);
1350 identblocksforblock[b2].push_back(b1);
1351 currrep.push_back(b2);
1352 /* handle varmap */
1353 currrepvarmapforthisrep.push_back(varmap);
1354
1355 }
1356 else
1357 {
1358 SCIPdebugMessage("Block %d is not identical to block %d!\n", b1, b2);
1359 }
1360 SCIPhashmapFree(&varmap2);
1361 }
1362 }
1363 }
1364 else
1365 {
1366 SCIPdebugMessage("Block %d contains linking vars and can not be aggregated.\n", b1);
1367 }
1368
1369 eqclasstoblocks.push_back(currrep);
1370 eqclassesvarmappings.push_back(currrepvarmapforthisrep);
1371 for( size_t i = 0; i < currrep.size(); ++i )
1372 blockstoeqclasses[currrep[i]] = nreps - 1;
1373 ++nreps;
1374 }
1375 nequivalenceclasses = nreps - 1;
1376}
1377
1378
1379void PARTIALDECOMP::calcHashvalue()
1380{
1381 if( !hvoutdated )
1382 return;
1383
1384 std::vector<std::pair<int, int>> blockorder = std::vector < std::pair<int, int> > ( 0 );
1385 unsigned long hashval = 0;
1386 unsigned long borderval = 0;
1387
1388 sort();
1389
1390 /* find sorting for blocks (non decreasing according smallest row index) */
1391 for( int i = 0; i < this->nblocks; ++ i )
1392 {
1393 if( !this->conssforblocks[i].empty() )
1394 blockorder.emplace_back( i, this->conssforblocks[i][0] );
1395 else
1396 {
1397 assert( this->varsforblocks[i].size() > 0 );
1398 blockorder.emplace_back( i, this->getNConss() + this->varsforblocks[i][0] );
1399 }
1400 }
1401
1402 std::sort( blockorder.begin(), blockorder.end(), compare_blocks );
1403
1404 for( int i = 0; i < nblocks; ++ i )
1405 {
1406 unsigned long blockval = 0;
1407 int blockid = blockorder[i].first;
1408
1409 for( size_t tau = 0; tau < conssforblocks[blockid].size(); ++ tau )
1410 {
1411 blockval += ( 2 * conssforblocks[blockid][tau] + 1 ) * (unsigned long) pow( 2, tau % 16 );
1412 }
1413
1414 hashval += primes[i % ( nprimes - 1 )] * blockval;
1415 }
1416
1417 for( size_t tau = 0; tau < masterconss.size(); ++ tau )
1418 {
1419 borderval += ( 2 * masterconss[tau] + 1 ) * (unsigned long) pow( 2, tau % 16 );
1420 }
1421
1422 hashval += primes[nblocks % nprimes] * borderval;
1423 hashval += primes[( nblocks + 1 ) % nprimes] * (unsigned long) openvars.size();
1424
1425 hashvalue = hashval;
1426 hvoutdated = false;
1427}
1428
1429
1430void PARTIALDECOMP::calcNCoeffsForBlocks()
1431{
1432 ncoeffsforblock = std::move(std::vector<int>(getNBlocks(), 0));
1433 int counter;
1434 DETPROBDATA* detprobdata = this->getDetprobdata();
1435
1436 for( int b = 0; b < getNBlocks(); ++b )
1437 {
1438 counter = 0;
1439 for( int blco = 0; blco < getNConssForBlock(b); ++blco )
1440 {
1441 int consid = getConssForBlock(b)[blco];
1442 for( int cva = 0; cva < detprobdata->getNVarsForCons(consid) ;++cva )
1443 if( isVarBlockvarOfBlock(detprobdata->getVarsForCons(consid)[cva], b ) )
1444 ++counter;
1445 }
1446 ncoeffsforblock[b] = counter;
1447 }
1448
1449 counter = 0;
1450 for( int mco = 0; mco < getNMasterconss(); ++mco )
1451 {
1452 int consid = getMasterconss()[mco];
1453 counter += detprobdata->getNVarsForCons(consid);
1454 }
1455 ncoeffsformaster = counter;
1456}
1457
1458
1460 )
1461{
1463
1464 /* data structure containing pairs of varindices and blocknumbers */
1465 std::vector< std::pair< int, std::vector< int > > > blocksOfVars = findLinkingVarsPotentiallyStairlinking( );
1466
1467 /* if there are no vars that are potentially stairlinking, return without further calculations */
1468 if( blocksOfVars.size() == 0 )
1469 return;
1470
1471 GraphGCG* g = new GraphGCG( getNBlocks(), true );
1472
1473 /* create block graph: every block is represented by a node and two nodes are adjacent if there exists a
1474 * var that potentially links these blocks, the edge weight is the number of such variables */
1475 for( int i = 0; i < (int) blocksOfVars.size(); ++i )
1476 {
1477 assert( blocksOfVars[i].second.size() == 2 );
1478 int v = blocksOfVars[i].second[0];
1479 int w = blocksOfVars[i].second[1];
1480
1481 if ( g->isEdge( v, w ) )
1482 {
1483 g->setEdge( v, w, g->getEdgeWeight( v, w ) + 1 );
1484 }
1485 else
1486 {
1487 g->setEdge( v, w, 1 );
1488 }
1489 }
1490
1491 bool isstaircase = true; /* maintains information whether staircase structure is still possible */
1492 std::vector< int > sources( 0 ); /* all nodes with degree one */
1493 std::vector< bool > marks( getNBlocks() ); /* a node is marked if its degree is zero or it is reachable from a source */
1494
1495 /* firstly, check whether every node has an degree of at most 2 */
1496 for( int b = 0; b < getNBlocks(); ++b )
1497 {
1498 if( g->getNNeighbors( b ) > 2 )
1499 {
1500 isstaircase = false;
1501 break;
1502 }
1503 else if( g->getNNeighbors( b ) == 1 )
1504 {
1505 sources.push_back( b );
1506 }
1507 else if ( g->getNNeighbors( b ) == 0 )
1508 {
1509 marks[b] = true;
1510 }
1511 }
1512
1513 /* secondly, check whether there exists a circle in the graph by moving along all paths starting from a source */
1514 for( int s = 0; s < (int) sources.size() && isstaircase; ++s )
1515 {
1516 int curBlock = sources[s];
1517 if( marks[curBlock] )
1518 continue;
1519
1520 marks[curBlock] = true;
1521
1522 /* check whether there is an unmarked neighbor
1523 * if there is none, a circle is detected */
1524 do
1525 {
1526 std::vector< int > neighbors = g->getNeighbors( curBlock );
1527 if( !marks[neighbors[0]] )
1528 {
1529 marks[neighbors[0]] = true;
1530 curBlock = neighbors[0];
1531 }
1532 else if ( !marks[neighbors[1]] )
1533 {
1534 marks[neighbors[1]] = true;
1535 curBlock = neighbors[1];
1536 }
1537 else
1538 {
1539 isstaircase = false;
1540 break;
1541 }
1542 }
1543 while( g->getNNeighbors( curBlock ) != 1 );
1544 }
1545
1546 /* thirdly, check whether all nodes with neighbors are reachable from a source,
1547 * since there is a circle if this is not the case */
1548 for( int b = 0; b < getNBlocks() && isstaircase; ++b )
1549 {
1550 if( !marks[b] )
1551 {
1552 isstaircase = false;
1553 break;
1554 }
1555 }
1556
1557 if( isstaircase )
1558 {
1559 changeBlockOrderStaircase( g );
1560 }
1561 else
1562 {
1563 return;
1564 }
1565
1567}
1568
1569
1570void PARTIALDECOMP::changeBlockOrderStaircase(
1571 GraphGCG* g
1572 )
1573{
1574 int blockcounter = 0; /* counts current new block to assign an old one to */
1575 std::vector< int > blockmapping( getNBlocks() ); /* stores new block order */
1576 for( int b = 0; b < getNBlocks(); ++b )
1577 blockmapping[b] = -1;
1578
1579 for( int b = 0; b < getNBlocks(); ++b )
1580 {
1581 if( g->getNNeighbors( b ) == 0 )
1582 {
1583 /* if block does not have a neighbor, just assign it to current blockindex */
1584 assert( blockmapping[b] == -1 );
1585 blockmapping[b] = blockcounter;
1586 ++blockcounter;
1587 }
1588 else if( blockmapping[b] == -1 && g->getNNeighbors( b ) == 1 )
1589 {
1590 /* if the block is the source of an yet unconsidered path, assign whole path to ascending new block ids */
1591 int curBlock = b;
1592 blockmapping[b] = blockcounter;
1593 ++blockcounter;
1594
1595 do
1596 {
1597 std::vector< int > neighbors = g->getNeighbors( curBlock );
1598
1599 if( blockmapping[neighbors[0]] == -1 )
1600 {
1601 blockmapping[neighbors[0]] = blockcounter;
1602 curBlock = neighbors[0];
1603 }
1604 else if ( blockmapping[neighbors[1]] == -1 )
1605 {
1606 blockmapping[neighbors[1]] = blockcounter;
1607 curBlock = neighbors[1];
1608 }
1609 else
1610 {
1611 assert( false );
1612 }
1613 ++blockcounter;
1614 }
1615 while( g->getNNeighbors( curBlock ) != 1 );
1616 }
1617 }
1618
1619 changeBlockOrder( blockmapping );
1620}
1621
1622
1623void PARTIALDECOMP::changeBlockOrder(
1624 std::vector<int> oldToNewBlockIndex
1625 )
1626{
1627 assert((int) oldToNewBlockIndex.size() == getNBlocks());
1630
1631 std::vector<std::vector<int>> newconssforblocks(getNBlocks());
1632 std::vector<std::vector<int>> newvarsforblocks(getNBlocks());
1633
1634 for( int b = 0; b < getNBlocks(); ++b )
1635 {
1636 assert(0 <= oldToNewBlockIndex[b] && oldToNewBlockIndex[b] < getNBlocks());
1637
1638 newconssforblocks[oldToNewBlockIndex[b]] = conssforblocks[b];
1639 newvarsforblocks[oldToNewBlockIndex[b]] = varsforblocks[b];
1640 }
1641
1642 conssforblocks = std::move(newconssforblocks);
1643 varsforblocks = std::move(newvarsforblocks);
1644
1645 if( !linkingvarsforblocks.empty() )
1646 {
1647 std::vector<std::vector<int>> newlinkingvarsforblocks(getNBlocks());
1648 for( int b = 0; b < getNBlocks(); ++b )
1649 {
1650 newlinkingvarsforblocks[oldToNewBlockIndex[b]] = linkingvarsforblocks[b];
1651 }
1652 linkingvarsforblocks = std::move(newlinkingvarsforblocks);
1653 }
1654
1655 if( !ncoeffsforblock.empty() )
1656 {
1657 std::vector<int> newncoeffsforblock(getNBlocks());
1658 for( int b = 0; b < getNBlocks(); ++b )
1659 {
1660 newncoeffsforblock[oldToNewBlockIndex[b]] = ncoeffsforblock[b];
1661 }
1662 ncoeffsforblock = std::move(newncoeffsforblock);
1663 }
1664
1665 if( !ncoeffsforblockformastercons.empty() )
1666 {
1667 std::vector<std::vector<int>> newncoeffsforblockformastercons(getNBlocks());
1668 for( int b = 0; b < getNBlocks(); ++b )
1669 {
1670 newncoeffsforblockformastercons[oldToNewBlockIndex[b]] = ncoeffsforblockformastercons[b];
1671 }
1672 ncoeffsforblockformastercons = std::move(newncoeffsforblockformastercons);
1673 }
1674
1675 if( isNested() )
1676 {
1677 std::vector<BLOCK_STRUCTURE*> newblockstructures(getNBlocks());
1678 for( int b = 0; b < getNBlocks(); ++b )
1679 {
1680 newblockstructures[oldToNewBlockIndex[b]] = blockstructures[b];
1681 }
1682 blockstructures = std::move(newblockstructures);
1683 }
1684}
1685
1686
1688{
1689 for( size_t i = 0; i < openconss.size(); ++ i )
1690 {
1691 bool consfound = false;
1692 for( size_t k = 0; k < masterconss.size(); ++ k )
1693 {
1694 if( openconss[i] == masterconss[k] )
1695 {
1696 consfound = true;
1697 break;
1698 }
1699 }
1700 for( int b = 0; b < nblocks && ! consfound; ++ b )
1701 {
1702 for( size_t k = 0; k < conssforblocks[b].size(); ++ k )
1703 {
1704 if( openconss[i] == conssforblocks[b][k] )
1705 {
1706 consfound = true;
1707 break;
1708 }
1709 }
1710 }
1711 if( ! consfound )
1712 {
1713 return false;
1714 }
1715 }
1716 openconss.clear();
1717 isconsopen = std::vector<bool>(nconss, false);
1718 return true;
1719}
1720
1721
1723 )
1724{
1725 std::vector<bool> openvarsBool( nvars, true );
1726 std::vector<int> stairlinkingvarsvec( 0 );
1727 std::vector<int>::const_iterator varIter = linkingvars.begin();
1728 std::vector<int>::const_iterator varIterEnd = linkingvars.end();
1729
1730 int value;
1731
1732 /* check if nblocks is set appropriately */
1733 if( nblocks != (int) conssforblocks.size() )
1734 {
1735 SCIPwarningMessage(scip, "In (partialdec %d) nblocks %d and size of conssforblocks %ld are not identical! \n" , id, nblocks, conssforblocks.size() );
1736 assert( false );
1737 return false;
1738 }
1739
1740 if( nblocks != (int) varsforblocks.size() )
1741 {
1742 SCIPwarningMessage(scip, "In (partialdec %d) nblocks %d and size of varsforblocks %ld are not identical! \n" , id, nblocks, varsforblocks.size() );
1743 assert( false );
1744 return false;
1745 }
1746
1747 /* check for empty (row- and col-wise) blocks */
1748 for( int b = 0; b < nblocks; ++ b )
1749 {
1750 if( conssforblocks[b].empty() && varsforblocks[b].empty() )
1751 {
1752 SCIPwarningMessage(scip, "In (partialdec %d) block %d is empty! \n" , id, b );
1753 assert( false );
1754 return false;
1755 }
1756 }
1757
1758 /* check variables (every variable is assigned at most once) */
1759 for( ; varIter != varIterEnd; ++ varIter )
1760 {
1761 if( ! openvarsBool[ * varIter] )
1762 {
1763 SCIPwarningMessage(scip, "In (partialdec %d) linking variable with index %d is already assigned! \n" , id, * varIter );
1764
1765 assert( false );
1766 return false;
1767 }
1768 openvarsBool[ * varIter] = false;
1769 }
1770
1771 for( int b = 0; b < nblocks; ++ b )
1772 {
1773 varIterEnd = varsforblocks[b].end();
1774 for( varIter = varsforblocks[b].begin(); varIter != varIterEnd; ++ varIter )
1775 {
1776 if( ! openvarsBool[ * varIter] )
1777 {
1778 SCIPwarningMessage(scip, "In (partialdec %d) variable with index %d is already assigned but also assigned to block %d! \n" , id, * varIter, b );
1779 assert( false );
1780 return false;
1781 }
1782 openvarsBool[ * varIter] = false;
1783 }
1784 }
1785
1786 varIterEnd = mastervars.end();
1787 for( varIter = mastervars.begin(); varIter != varIterEnd; ++ varIter )
1788 {
1789 if( ! openvarsBool[ * varIter] )
1790 {
1791 SCIPwarningMessage(scip, "In (partialdec %d) variable with index %d is already assigned but also assigned to master! \n" , id, * varIter);
1792 assert( false );
1793 return false;
1794 }
1795 openvarsBool[ * varIter] = false;
1796 }
1797
1798 for( int b = 0; b < nblocks; ++ b )
1799 {
1800 varIter = stairlinkingvars[b].begin();
1801 varIterEnd = stairlinkingvars[b].end();
1802 for( ; varIter != varIterEnd; ++ varIter )
1803 {
1804 if( ! openvarsBool[ * varIter] )
1805 {
1806 SCIPwarningMessage(scip, "In (partialdec %d) variable with index %d is already assigned but also assigned to stairlinking block %d! \n" , id, * varIter, b );
1807 assert( false );
1808 return false;
1809 }
1810 openvarsBool[ * varIter] = false;
1811 }
1812 if( ( b == nblocks - 1 ) && ( (int) stairlinkingvars[b].size() != 0 ) )
1813 {
1814 SCIPwarningMessage(scip, "In (partialdec %d) variable with index %d is is as assigned as stairlinking var of last block! \n" , id, * varIter );
1815 assert( false );
1816 return false;
1817 }
1818 }
1819
1820 /* check if all not assigned variables are open vars */
1821 for( int v = 0; v < nvars; ++ v )
1822 {
1823 if( openvarsBool[v] && !isVarOpenvar(v) )
1824 {
1825 SCIPwarningMessage(scip, "In (partialdec %d) variable with index %d is not assigned and not an open var! \n" , id, v );
1826 assert( false );
1827 return false;
1828 }
1829 }
1830
1831 /* check if all open vars are not assigned */
1832 for( size_t i = 0; i < openvars.size(); ++ i )
1833 {
1834 if( !openvarsBool[openvars[i]] )
1835 {
1836 SCIPwarningMessage(scip, "In (partialdec %d) variable with index %d is an open var but assigned! \n" , id, openvars[i] );
1837 assert( false );
1838 return false;
1839 }
1840 }
1841
1842 for( size_t i = 0; i < openvarsBool.size(); ++ i )
1843 {
1844 if( openvarsBool[i] != isvaropen[i] )
1845 {
1846 SCIPwarningMessage(scip, "In (partialdec %d) variable with index %d is causes asynchronity with isvaropen array ! \n" , id, openvars[i] );
1847 assert( false );
1848 return false;
1849
1850 }
1851 }
1852
1853 /* check constraints (every constraint is assigned at most once) */
1854 std::vector<bool> openconssBool( nconss, true );
1855 std::vector<int> openconssVec( 0 );
1856 std::vector<int>::const_iterator consIter = masterconss.begin();
1857 std::vector<int>::const_iterator consIterEnd = masterconss.end();
1858
1859 for( ; consIter != consIterEnd; ++ consIter )
1860 {
1861 if( ! openconssBool[ * consIter] )
1862 {
1863 SCIPwarningMessage(scip, "In (partialdec %d) constraint with index %d is at least two times assigned as a master constraint! \n" , id, * consIter );
1864 assert( false );
1865 return false;
1866 }
1867 openconssBool[ * consIter] = false;
1868 }
1869
1870 for( int b = 0; b < nblocks; ++ b )
1871 {
1872 consIterEnd = conssforblocks[b].end();
1873 for( consIter = conssforblocks[b].begin(); consIter != consIterEnd; ++ consIter )
1874 {
1875 if( ! openconssBool[ * consIter] )
1876 {
1877 SCIPwarningMessage(scip, "In (partialdec %d) constraint with index %d is already assigned but also assigned to block %d! \n" , id, * consIter, b );
1878 assert( false );
1879 return false;
1880 }
1881 openconssBool[ * consIter] = false;
1882 }
1883 }
1884
1885 /* check if all not assigned constraints are open cons */
1886 for( int v = 0; v < nconss; ++ v )
1887 {
1888 if( openconssBool[v] && !isConsOpencons(v) )
1889 {
1890 SCIPwarningMessage(scip, "In (partialdec %d) constraint with index %d is not assigned and not an open cons! \n" , id, v );
1891 assert( false );
1892 return false;
1893 }
1894 }
1895
1896 /* check if all open conss are not assigned */
1897 for( size_t i = 0; i < openconss.size(); ++ i )
1898 {
1899 if( !openconssBool[openconss[i]] )
1900 {
1901 SCIPwarningMessage(scip, "In (partialdec %d) constraint with index %d is an open cons but assigned! \n" , id, openconss[i] );
1902 assert( false );
1903 return false;
1904 }
1905 }
1906
1907 /* check if the partialdec is sorted */
1908 for( int b = 0; b < nblocks; ++ b )
1909 {
1910 value = - 1;
1911 for( int v = 0; v < getNVarsForBlock( b ); ++ v )
1912 {
1913 if( value >= getVarsForBlock(b)[v] )
1914 {
1915 SCIPwarningMessage(scip, "In (partialdec %d) variables of block %d are not sorted! \n" , id, b );
1916 assert( false );
1917 return false;
1918 }
1919 value = getVarsForBlock( b )[v];
1920 }
1921 }
1922 for( int b = 0; b < nblocks; ++ b )
1923 {
1924 value = - 1;
1925 for( int v = 0; v < getNStairlinkingvars( b ); ++ v )
1926 {
1927 if( value >= getStairlinkingvars(b)[v] )
1928 {
1929 SCIPwarningMessage(scip, "In (partialdec %d) stairlinking variables of block %d are not sorted! \n" , id, b );
1930 assert( false );
1931 return false;
1932 }
1933 value = getStairlinkingvars( b )[v];
1934 }
1935 }
1936 value = - 1;
1937 for( int v = 0; v < getNLinkingvars(); ++ v )
1938 {
1939 if( value >= getLinkingvars()[v] )
1940 {
1941 SCIPwarningMessage(scip, "In (partialdec %d) linking variables are not sorted! \n" , id );
1942 assert( false );
1943 return false;
1944 }
1945 value = getLinkingvars()[v];
1946 }
1947 value = - 1;
1948 for( int v = 0; v < getNMastervars(); ++ v )
1949 {
1950 if( value >= getMastervars()[v] )
1951 {
1952 SCIPwarningMessage(scip, "In (partialdec %d) master variables are not sorted! \n" , id );
1953 assert( false );
1954 return false;
1955 }
1956 value = getMastervars()[v];
1957 }
1958 for( int b = 0; b < nblocks; ++ b )
1959 {
1960 value = - 1;
1961 for( int v = 0; v < getNConssForBlock(b); ++ v )
1962 {
1963 if( value >= getConssForBlock(b)[v] )
1964 {
1965 SCIPwarningMessage(scip, "In (partialdec %d) constraints of block %d are not sorted! \n" , id, b );
1966 assert( false );
1967 return false;
1968 }
1969 value = getConssForBlock(b)[v];
1970 }
1971 }
1972 value = - 1;
1973 for( int v = 0; v < getNMasterconss(); ++ v )
1974 {
1975 if( value >= getMasterconss()[v] )
1976 {
1977 SCIPwarningMessage(scip, "In (partialdec %d) master constraints are not sorted! \n" , id);
1978 assert( false );
1979 return false;
1980 }
1981 value = getMasterconss()[v];
1982 }
1983
1984 DETPROBDATA* detprobdata = this->getDetprobdata();
1985 /* check if variables hitting a cons are either in the cons's block or border or still open */
1986 for( int b = 0; b < nblocks; ++ b )
1987 {
1988 for( int c = 0; c < getNConssForBlock( b ); ++ c )
1989 {
1990 for( int v = 0; v < detprobdata->getNVarsForCons( getConssForBlock( b )[c] ); ++ v )
1991 {
1992 int varid = detprobdata->getVarsForCons( getConssForBlock( b )[c] )[v];
1993
1994 if( !(isVarBlockvarOfBlock(varid, b) || (isVarLinkingvar(varid) && (linkingvarsforblocks.empty() || isVarLinkingvarOfBlock(varid, b)))
1995 || isVarStairlinkingvarOfBlock(varid, b) || isVarOpenvar(varid)) )
1996 {
1997 SCIP_Bool partofblock;
1998
1999 partofblock = FALSE;
2000
2002 "This should only happen during translation of (partial) decompositions from orginal to transformed problem, and means that translation has failed for this particaluar partial decomposition. Variable %d is not part of block %d or linking or open as constraint %d suggests! \n ", varid, b,
2003 getConssForBlock(b)[c]);
2004
2005 for( int b2 = 0; b2 < getNBlocks(); ++b2 )
2006 {
2007 if ( isVarBlockvarOfBlock(varid, b2) )
2008 {
2009 partofblock = TRUE;
2011 "instead Variable %d is part of block %d \n ", varid, b2);
2012 break;
2013 }
2014 }
2015
2016 if( !partofblock )
2017 {
2018 if( isvarmaster[varid] )
2019 SCIPwarningMessage(scip, "instead Variable %d is part of master \n ", varid);
2020 else
2021 SCIPwarningMessage(scip, "in fact Variable %d is completely unassigned \n ", varid);
2022 }
2023 assert(false);
2024 return false;
2025 }
2026 }
2027 }
2028 }
2029
2030 if( getDetectorchain().size() != getDetectorchainInfo().size() )
2031 {
2032 assert(false);
2033 return false;
2034 }
2035
2036 if( getNDetectors() != (int) pctvarstoblock.size() || getNDetectors() != (int) pctvarstoborder.size()
2037 || getNDetectors() != (int) pctvarsfromfree.size() || getNDetectors() != (int) pctconsstoblock.size()
2038 || getNDetectors() != (int) pctconsstoborder.size() || getNDetectors() != (int) pctconssfromfree.size() )
2039 {
2040 assert(false);
2041 return false;
2042 }
2043
2044 return true;
2045}
2046
2047
2048#ifndef NO_AUT_LIB
2049void PARTIALDECOMP::checkIdenticalBlocksAutomorphism(
2050 int b1,
2051 int b2,
2052 std::vector<int>& varmap,
2053 SCIP_HASHMAP* varmap2,
2054 SCIP_Bool* identical,
2055 unsigned int searchnodelimit,
2056 unsigned int generatorlimit
2057 )
2058{
2059 *identical = FALSE;
2060 SCIP_HASHMAP* consmap;
2062
2063 assert((int)varmap.size() == getNVarsForBlock(b1));
2064
2066 SCIPblkmem(scip ),
2067 getNConssForBlock(b1)+1) ); /* +1 to deal with empty subproblems */
2068
2069
2070 SCIPdebugMessage("obvious test fails, start building graph \n");
2071
2072 cmpGraphPair(scip, this, b1, b2, &result, varmap2, consmap, searchnodelimit, generatorlimit);
2073 if( result == SCIP_SUCCESS )
2074 {
2075 *identical = TRUE;
2076 /** TODO translate varmaps */
2077 for( int var2idinblock = 0; var2idinblock < getNVarsForBlock(b2) ; ++var2idinblock )
2078 {
2079 SCIP_VAR* var2;
2080 SCIP_VAR* var1;
2081 int var1idinblock;
2082 int var1id;
2083 auto detprobdata = this->getDetprobdata();
2084
2085 var2 = detprobdata->getVar(getVarsForBlock(b2)[var2idinblock]);
2086 var1 = (SCIP_VAR*) SCIPhashmapGetImage(varmap2, (void*) var2);
2087 var1id = detprobdata->getIndexForVar(var1);
2088 var1idinblock = getVarProbindexForBlock(var1id, b1);
2089 varmap[var2idinblock] = var1idinblock;
2090 }
2091
2092 }
2093 else
2094 *identical = FALSE;
2095
2096 SCIPhashmapFree(&consmap);
2097
2098 return;
2099
2100}
2101#endif
2102
2103
2104void PARTIALDECOMP::checkIdenticalBlocksBrute(
2105 int b1,
2106 int b2,
2107 std::vector<int>& varmap,
2108 SCIP_HASHMAP* varmap2,
2109 SCIP_Bool* identical
2110 )
2111{
2112 *identical = FALSE;
2113 SCIPdebugMessage("check block %d and block %d for identity...\n", b1, b2);
2114 std::vector<int> tmpvarmap(getNVars(), -1);
2115 DETPROBDATA* detprobdata = this->getDetprobdata();
2116
2117 /* check variables */
2118 for( int i = 0; i < getNVarsForBlock(b1); ++i )
2119 {
2120 SCIP_VAR* var1;
2121 SCIP_VAR* var2;
2122
2123 var1 = detprobdata->getVar(getVarsForBlock(b1)[i]);
2124 var2 = detprobdata->getVar(getVarsForBlock(b2)[i]);
2125
2126 if( !SCIPisEQ(scip, SCIPvarGetObj(var1), SCIPvarGetObj(var2) ) )
2127 {
2128 SCIPdebugMessage("--> obj differs for var %s and var %s!\n", SCIPvarGetName(var1), SCIPvarGetName(var2));
2129 return;
2130 }
2131 if( !SCIPisEQ(scip, SCIPvarGetLbGlobal(var1), SCIPvarGetLbGlobal(var2) ) )
2132 {
2133 SCIPdebugMessage("--> lb differs for var %s and var %s!\n", SCIPvarGetName(var1), SCIPvarGetName(var2));
2134 return;
2135 }
2136 if( !SCIPisEQ(scip, SCIPvarGetUbGlobal(var1), SCIPvarGetUbGlobal(var2) ) )
2137 {
2138 SCIPdebugMessage("--> ub differs for var %s and var %s!\n", SCIPvarGetName(var1), SCIPvarGetName(var2));
2139 return;
2140 }
2141 if( SCIPvarGetType(var1) != SCIPvarGetType(var2) )
2142 {
2143 SCIPdebugMessage("--> type differs for var %s and var %s!\n", SCIPvarGetName(var1), SCIPvarGetName(var2));
2144 return;
2145 }
2146
2147 for( int mc = 0; mc < getNMasterconss(); ++mc )
2148 {
2149
2150 if( !SCIPisEQ(scip, detprobdata->getVal(getMasterconss()[mc], getVarsForBlock(b1)[i]), detprobdata->getVal(getMasterconss()[mc], getVarsForBlock(b2)[i]) ))
2151 {
2152 SCIPdebugMessage("--> master coefficients differ for var %s (%f) and var %s (%f) !\n", SCIPvarGetName(
2153 detprobdata->getVar(getVarsForBlock(b1)[i]) ), detprobdata->getVal(getMasterconss()[mc], getVarsForBlock(b1)[i]), SCIPvarGetName(
2154 detprobdata->getVar(getVarsForBlock(b2)[i])), detprobdata->getVal(getMasterconss()[mc], getVarsForBlock(b2)[i]) );
2155 return;
2156 }
2157 }
2158
2159 /* variables seem to be identical so far */
2160 tmpvarmap[getVarsForBlock(b2)[i]] = getVarsForBlock(b1)[i];
2161 }
2162
2163 for( int i = 0; i < getNConssForBlock(b1); ++i )
2164 {
2165 int cons1id;
2166 int cons2id;
2167 SCIP_CONS* cons1;
2168 SCIP_CONS* cons2;
2169 SCIP_Real* vals1;
2170 SCIP_Real* vals2;
2171 int nvals1;
2172 int nvals2;
2173
2174 cons1id = getConssForBlock(b1)[i];
2175 cons2id = getConssForBlock(b2)[i];
2176
2177 cons1 = detprobdata->getCons(cons1id);
2178 cons2 = detprobdata->getCons(cons2id);
2179
2180 vals1 = NULL;
2181 vals2 = NULL;
2182
2183 if( detprobdata->getNVarsForCons(cons1id) != detprobdata->getNVarsForCons(cons2id) )
2184 {
2185 SCIPdebugMessage("--> nvars differs for cons %s and cons %s!\n", SCIPconsGetName(cons1), SCIPconsGetName(cons2));
2186 return;
2187 }
2188
2189 if( !SCIPisEQ(scip, GCGconsGetLhs(scip, cons1), GCGconsGetLhs(scip, cons2) ) )
2190 {
2191 SCIPdebugMessage("--> lhs differs for cons %s and cons %s!\n", SCIPconsGetName(cons1), SCIPconsGetName(cons2));
2192 return;
2193 }
2194
2195 if( !SCIPisEQ(scip, GCGconsGetRhs(scip, cons1), GCGconsGetRhs(scip, cons2) ) )
2196 {
2197 SCIPdebugMessage("--> rhs differs for cons %s and cons %s!\n", SCIPconsGetName(cons1), SCIPconsGetName(cons2));
2198 return;
2199 }
2200
2201 nvals1 = GCGconsGetNVars(scip, cons1);
2202 nvals2 = GCGconsGetNVars(scip, cons2);
2203 SCIP_CALL_ABORT( SCIPallocBufferArray(scip, &vals1, nvals1) );
2204 SCIP_CALL_ABORT( SCIPallocBufferArray(scip, &vals2, nvals2) );
2205 GCGconsGetVals(scip, cons1, vals1, nvals1);
2206 GCGconsGetVals(scip, cons2, vals2, nvals2);
2207
2208 if( !realArraysAreEqual(scip, vals1, nvals1, vals2, nvals2) )
2209 {
2210 SCIPdebugMessage("--> coefs differ for cons %s and cons %s!\n", SCIPconsGetName(cons1), SCIPconsGetName(cons2));
2211 SCIPfreeBufferArray(scip, &vals1);
2212 SCIPfreeBufferArray(scip, &vals2);
2213 return;
2214 }
2215
2216 for( int v = 0; v < detprobdata->getNVarsForCons(cons1id) ; ++v )
2217 {
2218 if( tmpvarmap[detprobdata->getVarsForCons(cons2id)[v]] != detprobdata->getVarsForCons(cons1id)[v])
2219 {
2220 SCIPfreeBufferArray(scip, &vals1);
2221 SCIPfreeBufferArray(scip, &vals2);
2222 SCIPdebugMessage("--> vars differ for cons %s and cons %s!\n", SCIPconsGetName(cons1), SCIPconsGetName(cons2));
2223 return;
2224 }
2225 }
2226
2227 SCIPfreeBufferArray(scip, &vals1);
2228 SCIPfreeBufferArray(scip, &vals2);
2229 }
2230
2231 assert((int)varmap.size() == getNVarsForBlock(b1));
2232 for( int i = 0; i < getNVarsForBlock(b1); ++i )
2233 varmap[i] = i;
2234
2235 *identical = TRUE;
2236}
2237
2238void PARTIALDECOMP::calcNCoeffsForBlockForMastercons()
2239{
2240 ncoeffsforblockformastercons = std::move(std::vector<std::vector<int>>(getNBlocks()));
2241 DETPROBDATA* detprobdata = this->getDetprobdata();
2242
2243 for( int b = 0; b < getNBlocks(); ++b )
2244 ncoeffsforblockformastercons[b].resize(getNMasterconss(), 0);
2245
2246 for( int mc = 0; mc < getNMasterconss(); ++mc )
2247 {
2248 int cons = getMasterconss()[mc];
2249 for ( int vmc = 0; vmc < detprobdata->getNVarsForCons(cons); ++vmc )
2250 {
2251 int var = detprobdata->getVarsForCons(cons)[vmc];
2252 for( int b = 0; b < getNBlocks(); ++b )
2253 {
2254 if( isVarBlockvarOfBlock(var, b) )
2255 ++ncoeffsforblockformastercons[b][mc];
2256 }
2257 }
2258 }
2259 return;
2260}
2261
2262
2263void PARTIALDECOMP::checkIdenticalBlocksTrivial(
2264 int b1,
2265 int b2,
2266 SCIP_Bool* notidentical)
2267{
2268 if( getNConssForBlock(b1) != getNConssForBlock(b2) )
2269 {
2270 SCIPdebugMessage("--> number of constraints differs!\n");
2271 *notidentical = TRUE;
2272 }
2273 else if( getNVarsForBlock(b1) != getNVarsForBlock(b2) )
2274 {
2275 SCIPdebugMessage("--> number of variables differs!\n");
2276 *notidentical = TRUE;
2277 }
2278 else if( getNCoeffsForBlock(b1) != getNCoeffsForBlock( b2) )
2279 {
2280 SCIPdebugMessage("--> number of nonzero coeffs differs!\n");
2281 *notidentical = TRUE;
2282 }
2283 else
2284 {
2285 if( ncoeffsforblockformastercons.size() == 0 )
2286 calcNCoeffsForBlockForMastercons();
2287
2288 for( int mc = 0; mc < getNMasterconss(); ++mc )
2289 {
2290 if ( ncoeffsforblockformastercons[b1][mc] != ncoeffsforblockformastercons[b2][mc] )
2291 {
2292 SCIPdebugMessage("--> number of nonzero coeffs in %d-th master cons differs!\n", mc);
2293 *notidentical = TRUE;
2294 break;
2295 }
2296 }
2297 }
2298}
2299
2300
2302{
2304
2305 // assign the open components to the master (without removing them to avoid screwing with the vector indices)
2306 for(auto cons : openconss)
2307 {
2308 setConsToMaster(cons);
2309 isconsopen[cons] = false;
2310 }
2311 // remove them from the open lists
2312 openconss.clear();
2313
2314 for(auto var: openvars)
2315 {
2317 isvaropen[var] = false;
2318 }
2319
2320 openvars.clear();
2321
2322 // consider implicits, calc hash, and check whether the partialdec is still consistent
2323 prepare();
2324}
2325
2326
2328{
2329 /* tools to update openvars */
2330 std::vector<int> oldOpenconss;
2331
2332 std::vector<bool> isConsVisited( nconss, false );
2333 std::vector<bool> isVarVisited( nvars, false );
2334
2335 std::queue<int> helpqueue;
2336 std::vector<int> neighborConss;
2337 std::vector<int> neighborVars;
2338
2339 assert( (int) conssforblocks.size() == getNBlocks() );
2340 assert( (int) varsforblocks.size() == getNBlocks() );
2341 assert( (int) stairlinkingvars.size() == getNBlocks() );
2342
2344
2345 if( getNBlocks() < 0 )
2346 {
2347 setNBlocks(0);
2348 }
2349
2350 gcg::DETPROBDATA* detprobdata = getDetprobdata();
2351
2352 /* do breadth first search to find connected conss and vars */
2353 while( !openconss.empty() )
2354 {
2355 int newBlockNr;
2356
2357 assert( helpqueue.empty() );
2358 helpqueue.push(openconss[0]);
2359 neighborConss.clear();
2360 neighborConss.push_back(openconss[0]);
2361 isConsVisited[openconss[0]] = true;
2362 neighborVars.clear();
2363
2364 while( !helpqueue.empty() )
2365 {
2366 int nodeCons = helpqueue.front();
2367 assert( isConsOpencons(nodeCons) );
2368 helpqueue.pop();
2369 for( int var : detprobdata->getVarsForCons(nodeCons) )
2370 {
2372
2373 if( isVarVisited[var] || isVarLinkingvar(var) )
2374 continue;
2375
2376 for( int cons : detprobdata->getConssForVar(var) )
2377 {
2378 if( !isConsOpencons(cons) || isConsVisited[cons] )
2379 {
2380 continue;
2381 }
2382 assert( isConsOpencons(cons) );
2383 isConsVisited[cons] = true;
2384 neighborConss.push_back(cons);
2385 helpqueue.push(cons);
2386 }
2387 isVarVisited[var] = true;
2388 neighborVars.push_back(var);
2389 }
2390 }
2391
2392 /* assign found conss and vars to a new block */
2393 newBlockNr = getNBlocks() + 1;
2394 setNBlocks(newBlockNr);
2395 for( int cons : neighborConss )
2396 {
2397 setConsToBlock(cons, newBlockNr - 1);
2398 if( isConsOpencons(cons) )
2399 deleteOpencons(cons);
2400 }
2401 for( int var : neighborVars )
2402 {
2403 setVarToBlock(var, newBlockNr - 1);
2404 if( isVarOpenvar(var) )
2406 }
2407 }
2408
2409 /* assign left open vars to block 0, if it exists, and to master, otherwise */
2410 for( int var : openvars )
2411 {
2412 if( getNBlocks() != 0 )
2413 setVarToBlock(var, 0);
2414 else
2416 isvaropen[var] = false;
2417 }
2418 openvars.clear();
2419
2420 assert( getNOpenconss() == 0 );
2421 assert( getNOpenvars() == 0 );
2422
2423 prepare();
2424
2426}
2427
2428
2429/**
2430* @brief assigns all open constraints and open variables
2431*
2432* strategy: assigns all conss and vars to the same block if they are connected
2433* a cons and a var are adjacent if the var appears in the cons
2434* \note this relies on the consadjacency structure of the detprobdata
2435* hence it cannot be applied in presence of linking variables
2436*/
2438{
2439 /* tools to check if the openvars can still be found in a constraint yet */
2440 std::vector<int> varinblocks(nvars, -1); /* stores in which block the variable can be found */
2441
2442 // note: this should not happen
2443 if( getNLinkingvars() != 0 )
2445
2446 std::vector<bool> isConsVisited(nconss, false);
2447
2448 std::queue<int> helpqueue;
2449 std::vector<int> neighborConss;
2450
2451 assert( (int) conssforblocks.size() == getNBlocks() );
2452 assert( (int) varsforblocks.size() == getNBlocks() );
2453 assert( (int) stairlinkingvars.size() == getNBlocks() );
2454
2456
2458 gcg::DETPROBDATA* detprobdata = getDetprobdata();
2459
2460 if( getNBlocks() < 0 )
2461 {
2462 setNBlocks(0);
2463 }
2464
2465 /* do breadth first search to find connected conss */
2466 while( !openconss.empty() )
2467 {
2468 int newBlockNr;
2469
2470 assert( helpqueue.empty() );
2471 helpqueue.push(openconss[0]);
2472 neighborConss.clear();
2473 neighborConss.push_back(openconss[0]);
2474 isConsVisited[openconss[0]] = true;
2475
2476 while( !helpqueue.empty() )
2477 {
2478 int nodeCons = helpqueue.front();
2479 assert( isConsOpencons(nodeCons) );
2480 helpqueue.pop();
2481 for( int cons : detprobdata->getConssForCons(nodeCons) )
2482 {
2483 if( isConsVisited[cons] || isConsMastercons(cons) || !isConsOpencons(cons) )
2484 continue;
2485
2486 assert( isConsOpencons(cons) );
2487 isConsVisited[cons] = true;
2488 neighborConss.push_back(cons);
2489 helpqueue.push(cons);
2490 }
2491 }
2492
2493 /* assign found conss and vars to a new block */
2494 newBlockNr = getNBlocks() + 1;
2495 setNBlocks( newBlockNr );
2496 for( int cons : neighborConss )
2497 {
2498 setConsToBlock(cons, newBlockNr - 1);
2499 if(isConsOpencons(cons))
2500 deleteOpencons(cons);
2501
2502 for( int var : detprobdata->getVarsForCons(cons) )
2503 {
2504
2505 if( isVarLinkingvar(var) || varinblocks[var] != -1 )
2506 continue;
2507
2509 setVarToBlock(var, newBlockNr - 1);
2510 varinblocks[var] = newBlockNr - 1;
2511 if( isVarOpenvar(var) )
2513 }
2514 }
2515 }
2516
2517 /* assign left open vars to block 0, if it exists, and to master, otherwise */
2518 for( int var : openvars )
2519 {
2520 if( getNBlocks() != 0 )
2521 setVarToBlock(var, 0);
2522 else
2524 isvaropen[var] = false;
2525 }
2526 openvars.clear();
2527
2528 assert( getNOpenconss() == 0 );
2529 assert( getNOpenvars() == 0 );
2530
2531 prepare();
2532
2534}
2535
2536
2538 )
2539{
2540 bool checkvar;
2541 bool isvarinblock;
2542 bool notassigned;
2543 DETPROBDATA* detprobdata = getDetprobdata();
2544
2545 /* tools to check if the openvars can still be found in a constraint yet*/
2546 std::vector<int> varinblocks; /* stores in which block the variable can be found */
2547
2548 if( getNBlocks() == 0 && getNOpenconss() > 0 )
2549 {
2550 int block = addBlock();
2551 fixConsToBlock( openconss[0], block );
2552 }
2553
2554 std::vector<int> del;
2555
2556 /* check if the openvars can already be found in a constraint */
2557 for( int i = 0; i < getNOpenvars(); ++ i )
2558 {
2559 varinblocks.clear();
2560
2561 /* test if the variable can be found in blocks */
2562 for( int b = 0; b < getNBlocks(); ++ b )
2563 {
2564 isvarinblock = false;
2565 std::vector<int>& conssforblock = getConssForBlock(b);
2566 for( int k = 0; k < getNConssForBlock(b) && !isvarinblock; ++ k )
2567 {
2568 for( int l = 0; l < detprobdata->getNVarsForCons( conssforblock[k] ); ++ l )
2569 {
2570 if( openvars[i] == detprobdata->getVarsForCons( conssforblock[k] )[l] )
2571 {
2572 varinblocks.push_back( b );
2573 isvarinblock = true;
2574 break;
2575 }
2576 }
2577 }
2578 }
2579 if( varinblocks.size() == 1 ) /* if the variable can be found in one block set the variable to a variable of the block*/
2580 {
2581 setVarToBlock(openvars[i], varinblocks[0]);
2582 del.push_back(openvars[i]);
2583 continue; /* the variable doesn't need to be checked any more */
2584 }
2585 else if( varinblocks.size() == 2 ) /* if the variable can be found in two blocks check if it is a linking var or a stairlinking var*/
2586 {
2587 if( varinblocks[0] + 1 == varinblocks[1] )
2588 {
2589 setVarToStairlinking(openvars[i], varinblocks[0], varinblocks[1]);
2590 del.push_back(openvars[i]);
2591 continue; /* the variable doesn't need to be checked any more */
2592 }
2593 else
2594 {
2595 setVarToLinking(openvars[i]);
2596 del.push_back(openvars[i]);
2597 continue; /* the variable doesn't need to be checked any more */
2598 }
2599 }
2600 else if( varinblocks.size() > 2 ) /* if the variable can be found in more than two blocks it is a linking var */
2601 {
2602 setVarToLinking(openvars[i]);
2603 del.push_back(openvars[i]);
2604 continue; /* the variable doesn't need to be checked any more */
2605 }
2606
2607 checkvar = true;
2608
2609 /* if the variable can be found in an open constraint it is still an open var */
2610 for( int j = 0; j < getNOpenconss(); ++ j )
2611 {
2612 checkvar = true;
2613 for( int k = 0; k < detprobdata->getNVarsForCons( j ); ++ k )
2614 {
2615 if( openvars[i] == detprobdata->getVarsForCons( j )[k] )
2616 {
2617 checkvar = false;
2618 break;
2619 }
2620 }
2621 if( ! checkvar )
2622 {
2623 break;
2624 }
2625 }
2626
2627 /* test if the variable can be found in a master constraint yet */
2628 for( int k = 0; k < detprobdata->getNConssForVar( openvars[i] ) && checkvar; ++ k )
2629 {
2630 if( isConsMastercons(detprobdata->getConssForVar(openvars[i])[k]) )
2631 {
2632 setVarToMaster(openvars[i]);
2633 del.push_back(openvars[i]);
2634 checkvar = false; /* the variable does'nt need to be checked any more */
2635 break;
2636 }
2637 }
2638 }
2639
2640 if( !del.empty() )
2641 {
2642 /* remove assigned vars from list of open vars */
2643 if( del.size() == openvars.size() )
2644 {
2645 openvars.clear();
2646 for( auto v : del )
2647 {
2648 assert(isvaropen[v]);
2649 isvaropen[v] = false;
2650 }
2651 }
2652 else
2653 {
2654 for( auto v : del )
2655 deleteOpenvar(v);
2656 }
2657 del.clear();
2658 }
2659 sort();
2660
2661 /* assign open conss greedily */
2662 for( int i = 0; i < getNOpenconss(); ++ i )
2663 {
2664 std::vector<int> vecOpenvarsOfBlock; /* stores the open vars of the blocks */
2665 bool consGotBlockcons = false; /* if the constraint can be assigned to a block */
2666
2667 /* check if the constraint can be assigned to a block */
2668 for( int j = 0; j < getNBlocks(); ++ j )
2669 {
2670 /* check if all vars of the constraint are a block var of the current block, an open var, a linkingvar or a mastervar*/
2671 consGotBlockcons = true;
2672 for( int k = 0; k < detprobdata->getNVarsForCons( openconss[i] ); ++ k )
2673 {
2674 if( isVarBlockvarOfBlock( detprobdata->getVarsForCons( openconss[i] )[k], j )
2675 || isVarOpenvar( detprobdata->getVarsForCons( openconss[i] )[k] )
2676 || isVarLinkingvar( detprobdata->getVarsForCons( openconss[i] )[k] )
2677 || isVarStairlinkingvarOfBlock( detprobdata->getVarsForCons( openconss[i] )[k], j )
2678 || ( j != 0 && isVarStairlinkingvarOfBlock( detprobdata->getVarsForCons( openconss[i] )[k], j - 1 ) ) )
2679 {
2680 if( isVarOpenvar( detprobdata->getVarsForCons( openconss[i] )[k] ) )
2681 {
2682 vecOpenvarsOfBlock.push_back( detprobdata->getVarsForCons( openconss[i] )[k] );
2683 }
2684 }
2685 else
2686 {
2687 vecOpenvarsOfBlock.clear(); /* the open vars don't get vars of the block */
2688 consGotBlockcons = false; /* the constraint can't be constraint of the block, check the next block */
2689 break;
2690 }
2691 }
2692 if( consGotBlockcons ) /* the constraint can be assigned to the current block */
2693 {
2694 setConsToBlock( openconss[i], j );
2695 del.push_back(openconss[i]);
2696 for( size_t k = 0; k < vecOpenvarsOfBlock.size(); ++ k ) /* the openvars in the constraint get block vars */
2697 {
2698 setVarToBlock( vecOpenvarsOfBlock[k], j );
2699 deleteOpenvar( vecOpenvarsOfBlock[k] );
2700 }
2701 vecOpenvarsOfBlock.clear();
2702
2703 break;
2704 }
2705 }
2706
2707 if( !consGotBlockcons ) /* the constraint can not be assigned to a block, set it to master */
2708 {
2709 setConsToMaster( openconss[i] );
2710 del.push_back(openconss[i]);
2711 }
2712 }
2713
2714 if( !del.empty() )
2715 {
2716 /* remove assigned conss from list of open conss */
2717 if( del.size() == openconss.size() )
2718 {
2719 openconss.clear();
2720 for( auto c : del )
2721 {
2722 assert(isconsopen[c]);
2723 isconsopen[c] = false;
2724 }
2725 }
2726 else
2727 {
2728 for( auto c : del )
2730 }
2731 del.clear();
2732 }
2733 sort();
2734
2735 /* assign open vars greedily */
2736 for( int i = 0; i < getNOpenvars(); ++ i )
2737 {
2738 notassigned = true;
2739 for( int j = 0; j < getNMasterconss() && notassigned; ++ j )
2740 {
2741 for( int k = 0; k < detprobdata->getNVarsForCons(masterconss[j]); ++ k )
2742 {
2743 if( openvars[i] == detprobdata->getVarsForCons(masterconss[j])[k] )
2744 {
2745 setVarToMaster(openvars[i]);
2746 del.push_back(openvars[i]);
2747 notassigned = false;
2748 break;
2749 }
2750 }
2751 }
2752 }
2753
2754 if( !del.empty() )
2755 {
2756 /* remove assigned vars from list of open vars */
2757 if( del.size() == openvars.size() )
2758 {
2759 openvars.clear();
2760 for( auto v : del )
2761 {
2762 assert(isvaropen[v]);
2763 isvaropen[v] = false;
2764 }
2765 }
2766 else
2767 {
2768 for( auto v : del )
2769 deleteOpenvar(v);
2770 }
2771 }
2772 sort();
2773
2774 /* check if the open conss are all assigned */
2776
2777 /* check if the open vars are all assigned */
2778 assert( getNOpenvars() == 0 );
2779
2781}
2782
2783
2785 int consid
2786 )
2787{
2788 std::vector<int>::iterator todelete = lower_bound(masterconss.begin(), masterconss.end(), consid);
2789 assert(todelete != masterconss.end() && *todelete == consid);
2790 if( todelete != masterconss.end() && *todelete == consid )
2791 masterconss.erase(todelete);
2792}
2793
2794
2795bool PARTIALDECOMP::consPartitionUsed(
2796 int detectorchainindex
2797 )
2798{
2799 assert( 0 <= detectorchainindex && detectorchainindex < (int) usedpartition.size() );
2800
2801 return (usedpartition[detectorchainindex] != NULL )
2802 && (dynamic_cast<ConsPartition*>( usedpartition[detectorchainindex] ) != NULL );
2803}
2804
2805
2807 )
2808{
2809 int cons;
2810 int var;
2811 std::vector<int> foundblocks;
2812 bool master;
2813 bool hitsOpenVar;
2814 bool hitsOpenCons;
2815 SCIP_Bool benders;
2816 std::vector<int> del;
2817
2818 DETPROBDATA* detprobdata = this->getDetprobdata();
2819
2820 SCIPgetBoolParam(scip, "detection/benders/enabled", &benders);
2821
2822 sort();
2823
2824 /* set openconss with more than two blockvars to master */
2825 for( size_t c = 0; c < openconss.size(); ++ c )
2826 {
2827 std::vector<bool> hitsblock(nblocks, false);
2828 foundblocks.clear();
2829 master = false;
2830 hitsOpenVar = false;
2831 cons = openconss[c];
2832
2833 for( int v = 0; v < detprobdata->getNVarsForCons(cons) && !master; ++v )
2834 {
2835 var = detprobdata->getVarsForCons(cons)[v];
2836
2837 if( isVarMastervar(var) )
2838 {
2839 master = true;
2840 fixConsToMaster(cons);
2841 continue;
2842 }
2843
2844 if( isVarOpenvar(var) )
2845 {
2846 hitsOpenVar = true;
2847 if( !benders )
2848 continue;
2849 }
2850
2851 for( int b = 0; b < nblocks && ! master; ++ b )
2852 {
2853 if( isVarBlockvarOfBlock(var, b) && !hitsblock[b] )
2854 {
2855 hitsblock[b] = true;
2856 foundblocks.push_back(b);
2857 break;
2858 }
2859 }
2860 }
2861
2862 if ( benders && foundblocks.size() == 1 && !master )
2863 {
2864 setConsToBlock(cons, foundblocks[0]);
2865 del.push_back(cons);
2866 }
2867
2868 if( !benders && foundblocks.size() > 1 )
2869 {
2870 setConsToMaster(cons);
2871 del.push_back(cons);
2872 }
2873
2874 /* also assign open constraints that only have vars assigned to one single block and no open vars*/
2875 if( foundblocks.size() == 1 && !hitsOpenVar && !master && !benders )
2876 {
2877 setConsToBlock(cons, foundblocks[0]);
2878 del.push_back(cons);
2879 }
2880 }
2881
2882 if( !del.empty() )
2883 {
2884 /* remove assigned conss from list of open conss */
2885 if( del.size() == openconss.size() )
2886 {
2887 openconss.clear();
2888 for( auto c : del )
2889 {
2890 assert(isconsopen[c]);
2891 isconsopen[c] = false;
2892 }
2893 }
2894 else
2895 {
2896 for( auto c : del )
2898 }
2899 }
2900 sort();
2901 del.clear();
2902
2903 /* set open var to linking, if it can be found in more than one block
2904 * or set it to a block if it has only constraints in that block and no open constraints
2905 * or set it to master if it only hits master constraints */
2906 for( size_t i = 0; i < openvars.size(); ++ i )
2907 {
2908 bool hitsmasterconss = false;
2909 bool hitonlymasterconss = true;
2910 bool hitonlyblockconss = true;
2911 foundblocks.clear();
2912 var = openvars[i];
2913 hitsOpenCons = false;
2914
2915
2916 for( int c = 0; c < detprobdata->getNConssForVar( var ); ++ c )
2917 {
2918 cons = detprobdata->getConssForVar( var )[c];
2919 if ( isConsMastercons(cons) )
2920 {
2921 hitsmasterconss = true;
2922 hitonlyblockconss = false;
2923 continue;
2924 }
2925
2926 if( isConsOpencons(cons) )
2927 {
2928 hitsOpenCons = true;
2929 hitonlyblockconss = false;
2930 hitonlymasterconss = false;
2931 continue;
2932 }
2933 }
2934 for( int b = 0; b < nblocks; ++ b )
2935 {
2936 for( int c = 0; c < detprobdata->getNConssForVar( var ); ++ c )
2937 {
2938 cons = detprobdata->getConssForVar(var)[c];
2939 if( isConsBlockconsOfBlock(cons, b) )
2940 {
2941 assert(std::find(foundblocks.begin(), foundblocks.end(), b) == foundblocks.end());
2942 hitonlymasterconss = false;
2943 foundblocks.push_back(b);
2944 break;
2945 }
2946 }
2947 }
2948
2949 if( foundblocks.size() > 1 )
2950 {
2952 del.push_back(var);
2953 continue;
2954 }
2955
2956 if( benders && foundblocks.size() == 1 && hitsmasterconss )
2957 {
2959 del.push_back(var);
2960 }
2961
2962 if( benders && hitonlyblockconss && foundblocks.size() == 1 )
2963 {
2964 setVarToBlock(var, foundblocks[0]);
2965 del.push_back(var);
2966 }
2967
2968 if( benders && hitonlymasterconss)
2969 {
2971 del.push_back(var);
2972 }
2973
2974 if( !benders && foundblocks.size() == 1 && !hitsOpenCons )
2975 {
2976 setVarToBlock(var, foundblocks[0]);
2977 del.push_back(var);
2978 }
2979
2980 if( !benders && foundblocks.size() == 0 && !hitsOpenCons )
2981 {
2983 del.push_back(var);
2984 }
2985 }
2986
2987 if( !del.empty() )
2988 {
2989 /* remove assigned vars from list of open vars */
2990 if( del.size() == openvars.size() )
2991 {
2992 openvars.clear();
2993 for( auto v : del )
2994 {
2995 assert(isvaropen[v]);
2996 isvaropen[v] = false;
2997 }
2998 }
2999 else
3000 {
3001 for( auto v : del )
3002 deleteOpenvar(v);
3003 }
3004 }
3005 sort();
3006}
3007
3008
3010 const PARTIALDECOMP* otherpartialdec
3011 )
3012{
3013 usedpartition = otherpartialdec->usedpartition;
3014 classestomaster = otherpartialdec->classestomaster;
3015 classestolinking = otherpartialdec->classestolinking;
3016}
3017
3018
3020 bool variables /* if TRUE a block is only considered to be empty if it contains neither constraints or variables */
3021 )
3022{
3023 bool emptyBlocks = true;
3024 SCIP_Bool benders = FALSE;
3025 int block = - 1;
3026 int b;
3027
3028 assert( (int) conssforblocks.size() == nblocks );
3029 assert( (int) varsforblocks.size() == nblocks );
3030 assert( (int) stairlinkingvars.size() == nblocks );
3031
3032 SCIPgetBoolParam(scip, "detection/benders/enabled", &benders);
3033
3034 while( emptyBlocks )
3035 {
3036 emptyBlocks = false;
3037 for( b = nblocks - 1; b >= 0; --b )
3038 {
3039 if( conssforblocks[b].empty() && ( variables ? varsforblocks[b].empty() : true) )
3040 {
3041 emptyBlocks = true;
3042 block = b;
3043 break;
3044 }
3045 if( benders && ( conssforblocks[b].empty() || varsforblocks[b].empty()) )
3046 {
3047 emptyBlocks = true;
3048 block = b;
3049 break;
3050 }
3051
3052 }
3053 if( emptyBlocks )
3054 {
3055 nblocks--;
3056
3057 stairlinkingvars.erase(stairlinkingvars.begin() + block);
3058
3059 for( int j : conssforblocks[block] )
3060 {
3061 masterconss.push_back(j);
3062 isconsmaster[j] = true;
3063 }
3064 std::sort(masterconss.begin(), masterconss.end());
3065 conssforblocks.erase(conssforblocks.begin() + block);
3066
3067 for( int j : varsforblocks[block] )
3068 {
3069 mastervars.push_back(j);
3070 isvarmaster[j] = true;
3071 }
3072 varsforblocks.erase(varsforblocks.begin() + block);
3073 std::sort( mastervars.begin(), mastervars.end() );
3074
3075 //set stairlinkingvars of the previous block to block vars
3076 if( block != 0 && !stairlinkingvars[block - 1].empty() )
3077 {
3078 for( int j : stairlinkingvars[block - 1] )
3079 {
3080 fixVarToBlock(j, block - 1);
3081 }
3082 stairlinkingvars[block - 1].clear();
3083 sort();
3084 }
3085 }
3086 }
3087}
3088
3089
3091 int opencons
3092 )
3093{
3094 assert(opencons >= 0 && opencons < nconss);
3095 std::vector<int>::iterator it = lower_bound(openconss.begin(), openconss.end(), opencons);
3096 assert(isconsopen[opencons] && it != openconss.end() && *it == opencons);
3097 if( it != openconss.end() && *it == opencons )
3098 {
3099 openconss.erase(it);
3100 isconsopen[opencons] = false;
3101 }
3102}
3103
3104
3105std::vector<int>::const_iterator PARTIALDECOMP::deleteOpencons(
3106 std::vector<int>::const_iterator itr
3107 )
3108{
3109 isconsopen[*itr] = false;
3110 return openconss.erase(itr);
3111}
3112
3113
3115 int openvar
3116 )
3117{
3118 assert(openvar >= 0 && openvar < nvars);
3119 assert(isvaropen[openvar]);
3120 std::vector<int>::iterator it = lower_bound(openvars.begin(), openvars.end(), openvar);
3121 assert(it != openvars.end() && *it == openvar);
3122 if(it != openvars.end() && *it == openvar)
3123 {
3124 openvars.erase(it);
3125 isvaropen[openvar] = false;
3126 }
3127}
3128
3129
3130std::vector<int>::const_iterator PARTIALDECOMP::deleteOpenvar(
3131 std::vector<int>::const_iterator itr
3132 )
3133{
3134 assert( itr != openvars.cend() );
3135
3136 isvaropen[*itr] = false;
3137 return openvars.erase(itr);
3138}
3139
3140
3141void PARTIALDECOMP::displayAggregationInformation()
3142{
3143 if( !aggInfoCalculated() )
3144 {
3145 SCIPinfoMessage(scip, NULL, " Aggregation information is not calculated yet \n ");
3146 }
3147 else
3148 {
3149 SCIPinfoMessage(scip, NULL, " number of equivalence classes: %d \n", nequivalenceclasses);
3150 for( int i = 0; i < nequivalenceclasses; ++i )
3151 {
3152 SCIPinfoMessage(scip, NULL, "equivalence class %d : ", i);
3153
3154 for( size_t b = 0; b < eqclasstoblocks[i].size(); ++b )
3155 SCIPinfoMessage(scip, NULL, "block %d ", eqclasstoblocks[i][b] );
3156
3157 SCIPinfoMessage(scip, NULL, "\n");
3158 }
3159 }
3160}
3161
3162
3164 int detailLevel
3165 )
3166{
3167 assert( 0 <= detailLevel );
3168 DETPROBDATA* detprobdata = this->getDetprobdata();
3169
3170 std::cout << std::endl;
3171
3172 /* general information */
3173 std::cout << "-- General information --" << std::endl;
3174 std::cout << " ID: " << id << std::endl;
3175 std::cout << " Hashvalue: " << hashvalue << std::endl;
3176 if( getNOpenconss() + getNOpenconss() == 0 )
3177 {
3178 for( int i = 0; i < GCGconshdlrDecompGetNScores(gcg); ++i )
3179 {
3181 std::cout << " " << GCGscoreGetName(score) << ": " << getScore(score) << std::endl;
3182 }
3183 }
3184
3185 std::cout << " HassetppMaster: " << hasSetppMaster() << std::endl;
3186 std::cout << " HassetppcMaster: " << hasSetppcMaster() << std::endl;
3187 std::cout << " HassetppccardMaster: " << hasSetppccardMaster() << std::endl;
3188 std::cout << " PARTIALDECOMP is for the " << (original ? "original" : "presolved" ) << " problem and "
3189 << ( usergiven ? "usergiven" : "not usergiven" ) << "." << std::endl;
3190 std::cout << " Number of constraints: " << getNConss() << std::endl;
3191 std::cout << " Number of variables: " << getNVars() << std::endl;
3192
3193 displayAggregationInformation();
3194 std::cout << std::endl;
3195
3196 /* detection information */
3197 std::cout << "-- Detection and detectors --" << std::endl;
3198 std::cout << " PARTIALDECOMP stems from the " << ( stemsfromorig ? "original" : "presolved" ) << " problem." << std::endl;
3199
3200 /* ancestor partialdecs' ids */
3201 std::cout << " IDs of ancestor partialdecs: ";
3202 if( !listofancestorids.empty() )
3203 std::cout << listofancestorids[0];
3204 for( int i = 1; i < (int) listofancestorids.size(); ++i )
3205 std::cout << ", " << listofancestorids[i];
3206 std::cout << std::endl;
3207
3208 /* detector chain information */
3209 std::cout << " " << getNDetectors() << " detector" << ( getNDetectors() > 1 ? "s" : "" ) << " worked on this partialdec:";
3210 if( getNDetectors() != 0 )
3211 {
3212 std::string detectorrepres;
3213
3214 if( detectorchain[0] == NULL )
3215 detectorrepres = "user";
3216 else
3217 {
3218 /* potentially add finisher label */
3219 detectorrepres = (
3220 getNDetectors() != 1 || !isfinishedbyfinisher ? GCGdetectorGetName(detectorchain[0]) :
3221 "(finish) " + std::string(GCGdetectorGetName(detectorchain[0])));
3222 }
3223
3224 if( detailLevel > 0 )
3225 {
3226 std::cout << std::endl << " 1.: " << detectorrepres << std::endl;
3227 std::cout << getDetectorStatistics( 0 );
3228 std::cout << getDetectorPartitionInfo(0, detailLevel > 1 && (!stemsfromorig || original));
3229 }
3230 else
3231 {
3232 std::cout << " " << detectorrepres;
3233 }
3234
3235 for( int d = 1; d < getNDetectors(); ++d )
3236 {
3237 /* potentially add finisher label */
3238 detectorrepres = (
3239 getNDetectors() != d + 1 || !isfinishedbyfinisher ? GCGdetectorGetName(detectorchain[d]) :
3240 "(finish) " + std::string(GCGdetectorGetName(detectorchain[d])));
3241
3242
3243 if( detailLevel > 0 )
3244 {
3245 std::cout << " " << ( d + 1 ) << ".: " << detectorrepres << std::endl;
3246 std::cout << getDetectorStatistics( d );
3247 std::cout << getDetectorPartitionInfo(d, detailLevel > 1 && (!stemsfromorig || original));
3248 }
3249 else
3250 {
3251 std::cout << ", " << detectorrepres;
3252 }
3253 }
3254
3255 if( detailLevel <= 0 )
3256 {
3257 std::cout << std::endl;
3258 }
3259 }
3260
3261 std::cout << std::endl;
3262
3263 /* variable information */
3264 std::cout << "-- Border and unassigned --" << std::endl;
3265 std::cout << " Linkingvariables";
3266 if( detailLevel > 1 )
3267 {
3268 std::cout << " (" << getNLinkingvars() << ")";
3269 if( getNLinkingvars() > 0 )
3270 std::cout << ": " << SCIPvarGetName(detprobdata->getVar(getLinkingvars()[0]));
3271 for( int v = 1; v < getNLinkingvars(); ++v )
3272 {
3273 std::cout << ", " << SCIPvarGetName(detprobdata->getVar(getLinkingvars()[v]));
3274 }
3275 std::cout << std::endl;
3276 }
3277 else
3278 {
3279 std::cout << ": " << getNLinkingvars() << std::endl;
3280 }
3281 std::cout << " Masterconstraints";
3282 if( detailLevel > 1 )
3283 {
3284 std::cout << " (" << getNMasterconss() << ")";
3285 if( getNMasterconss() > 0 )
3286 std::cout << ": " << SCIPconsGetName(detprobdata->getCons(getMasterconss()[0]));
3287 for( int c = 1; c < getNMasterconss(); ++c )
3288 {
3289 std::cout << ", " << SCIPconsGetName(detprobdata->getCons(getMasterconss()[c]));
3290 }
3291 std::cout << std::endl;
3292 }
3293 else
3294 {
3295 std::cout << ": " << getNMasterconss() << std::endl;
3296 }
3297 std::cout << " Mastervariables";
3298 if( detailLevel > 1 )
3299 {
3300 std::cout << " (" << getNMastervars() << ")";
3301 if( getNMastervars() > 0 )
3302 std::cout << ": " << SCIPvarGetName(detprobdata->getVar(getMastervars()[0]));
3303 for( int v = 1; v < getNMastervars(); ++v )
3304 {
3305 std::cout << ", " << SCIPvarGetName(detprobdata->getVar(getMastervars()[v]));
3306 }
3307 std::cout << std::endl;
3308 }
3309 else
3310 {
3311 std::cout << ": " << getNMastervars() << std::endl;
3312 }
3313 std::cout << " Open constraints";
3314 if( detailLevel > 1 )
3315 {
3316 std::cout << " (" << getNOpenconss() << ")";
3317 if( getNOpenconss() > 0 )
3318 std::cout << ": " << SCIPconsGetName(detprobdata->getCons(getOpenconss()[0]));
3319 for( int c = 1; c < getNOpenconss(); ++c )
3320 {
3321 std::cout << ", " << SCIPconsGetName(detprobdata->getCons(getOpenconss()[c]));
3322 }
3323 std::cout << std::endl;
3324 }
3325 else
3326 {
3327 std::cout << ": " << getNOpenconss() << std::endl;
3328 }
3329 std::cout << " Open variables";
3330 if( detailLevel > 1 )
3331 {
3332 std::cout << " (" << getNOpenvars() << ")";
3333 if( getNOpenvars() > 0 )
3334 std::cout << ": " << SCIPvarGetName(detprobdata->getVar(getOpenvars()[0]));
3335 for( int v = 1; v < getNOpenvars(); ++v )
3336 {
3337 std::cout << ", " << SCIPvarGetName(detprobdata->getVar(getOpenvars()[v]));
3338 }
3339 std::cout << std::endl;
3340 }
3341 else
3342 {
3343 std::cout << ": " << getNOpenvars() << std::endl;
3344 }
3345
3346 std::cout << std::endl;
3347
3348 /* block information */
3349 std::cout << "-- Blocks --" << std::endl;
3350 std::cout << " Number of blocks: " << nblocks << std::endl;
3351
3352 if( detailLevel > 0 )
3353 {
3354 for( int b = 0; b < nblocks; ++b )
3355 {
3356 std::cout << " Block " << b << ":" << std::endl;
3357
3358 std::cout << " Constraints";
3359 if( detailLevel > 1 )
3360 {
3361 std::cout << " (" << getNConssForBlock(b) << ")";
3362 if( getNConssForBlock(b) > 0 )
3363 std::cout << ": " << SCIPconsGetName(detprobdata->getCons(getConssForBlock(b)[0]));
3364 for( int c = 1; c < getNConssForBlock(b); ++c )
3365 {
3366 std::cout << ", " << SCIPconsGetName(detprobdata->getCons(getConssForBlock(b)[c]));
3367 }
3368 std::cout << std::endl;
3369 }
3370 else
3371 {
3372 std::cout << ": " << getNConssForBlock(b) << std::endl;
3373 }
3374
3375 std::cout << " Variables";
3376 if( detailLevel > 1 )
3377 {
3378 std::cout << " (" << getNVarsForBlock(b) << ")";
3379 if( getNVarsForBlock(b) > 0 )
3380 std::cout << ": " << SCIPvarGetName(detprobdata->getVar(getVarsForBlock(b)[0]));
3381 for( int v = 1; v < getNVarsForBlock(b); ++v )
3382 {
3383 std::cout << ", " << SCIPvarGetName(detprobdata->getVar(getVarsForBlock(b)[v]));
3384 }
3385 std::cout << std::endl;
3386 }
3387 else
3388 {
3389 std::cout << ": " << getNVarsForBlock(b) << std::endl;
3390 }
3391
3392 std::cout << " Stairlinkingvariables";
3393 if( detailLevel > 1 )
3394 {
3395 std::cout << " (" << getNStairlinkingvars(b) << ")";
3396 if( getNStairlinkingvars(b) > 0 )
3397 std::cout << ": " << SCIPvarGetName(detprobdata->getVar(getStairlinkingvars(b)[0]));
3398 for( int v = 1; v < getNStairlinkingvars(b); ++v )
3399 {
3400 std::cout << ", " << SCIPvarGetName(detprobdata->getVar(getStairlinkingvars(b)[v]));
3401 }
3402 std::cout << std::endl;
3403 }
3404 else
3405 {
3406 std::cout << ": " << getNStairlinkingvars(b) << std::endl;
3407 }
3408 }
3409 }
3410
3411 std::cout << std::endl;
3412}
3413
3414
3416)
3417{
3418 SCIP_Bool hassetpartmaster;
3419 SCIP_Bool verbose;
3420 hassetpartmaster = TRUE;
3421 verbose = FALSE;
3422
3424 return FALSE;
3425
3426 DETPROBDATA* detprobdata = this->getDetprobdata();
3427
3428 for( int l = 0; l < getNMasterconss(); ++l )
3429 {
3430 int consid = getMasterconss()[l];
3431 if( !detprobdata->isConsSetppc(consid) && !detprobdata->isConsCardinalityCons(consid) )
3432 {
3433 hassetpartmaster = FALSE;
3434 if( verbose )
3435 std::cout << " cons with name " << SCIPconsGetName(detprobdata->getCons(consid)) << " is no setppccard constraint." << std::endl;
3436 break;
3437 }
3438 }
3439
3440 return hassetpartmaster;
3441}
3442
3443
3445)
3446{
3447 SCIP_Bool hassetpartmaster;
3448 hassetpartmaster = TRUE;
3449
3451 return FALSE;
3452
3453 DETPROBDATA* detprobdata = this->getDetprobdata();
3454
3455 for( int l = 0; l < getNMasterconss(); ++l )
3456 {
3457 int consid = getMasterconss()[l];
3458 if( !detprobdata->isConsSetppc(consid) )
3459 {
3460 hassetpartmaster = FALSE;
3461 break;
3462 }
3463 }
3464 return hassetpartmaster;
3465}
3466
3467
3469)
3470{
3471 SCIP_Bool hassetpartmaster;
3472 hassetpartmaster = TRUE;
3473
3475 return FALSE;
3476
3477 DETPROBDATA* detprobdata = this->getDetprobdata();
3478
3479 for( int l = 0; l < getNMasterconss(); ++l )
3480 {
3481 int consid = getMasterconss()[l];
3482 if( !detprobdata->isConsSetpp(consid) )
3483 {
3484 hassetpartmaster = FALSE;
3485 break;
3486 }
3487 }
3488 return hassetpartmaster;
3489}
3490
3491
3493 SCIP_HASHMAP* constoblock,
3494 int givenNBlocks
3495 )
3496{
3497 assert( givenNBlocks >= 0 );
3498 assert( nblocks == 0 );
3499 assert( (int) conssforblocks.size() == nblocks );
3500 assert( (int) varsforblocks.size() == nblocks );
3501 assert( (int) stairlinkingvars.size() == nblocks );
3503 nblocks = givenNBlocks;
3504 DETPROBDATA* detprobdata = this->getDetprobdata();
3505 nvars = detprobdata->getNVars();
3506 nconss = detprobdata->getNConss();
3507 int consnum;
3508 int consblock;
3509
3510 for( int i = 0; i < nconss; ++ i )
3511 {
3512 consnum = i;
3513 consblock = ( (int) (size_t) SCIPhashmapGetImage( constoblock, (void*) (size_t) i ) ) - 1;
3514 assert( consblock >= 0 && consblock <= nblocks );
3515 if( consblock == nblocks )
3516 {
3517 setConsToMaster( consnum );
3518 deleteOpencons( consnum );
3519 }
3520 }
3521
3522 nblocks = 0;
3523 sort();
3524
3526
3527 return SCIP_OKAY;
3528}
3529
3530
3532 SCIP_HASHMAP* constoblock,
3533 int givenNBlocks
3534 )
3535{
3536 assert( givenNBlocks >= 0 );
3537 assert( nblocks == 0 );
3538 assert( (int) conssforblocks.size() == nblocks );
3539 assert( (int) varsforblocks.size() == nblocks );
3540 assert( (int) stairlinkingvars.size() == nblocks );
3542 nblocks = givenNBlocks;
3543 DETPROBDATA* detprobdata = this->getDetprobdata();
3544 nvars = detprobdata->getNVars();
3545 nconss = detprobdata->getNConss();
3546 int consnum;
3547 int consblock;
3548 int varnum;
3549 bool varInBlock;
3550 std::vector<int> varinblocks;
3551 std::vector<int> emptyVector;
3552
3553 for( int c = 0; c < nconss; ++ c )
3554 {
3556 assert( SCIPhashmapExists( constoblock, (void*) (size_t) c ) );
3557 assert( (int) (size_t) SCIPhashmapGetImage( constoblock, (void*) (size_t) c ) - 1 <= nblocks );
3558 assert( (int) (size_t) SCIPhashmapGetImage( constoblock, (void*) (size_t) c ) - 1 >= 0 );
3559 }
3560
3561 for( int b = (int) conssforblocks.size(); b < nblocks; b ++ )
3562 conssforblocks.push_back( emptyVector );
3563
3564 for( int b = (int) varsforblocks.size(); b < nblocks; b ++ )
3565 varsforblocks.push_back( emptyVector );
3566
3567 for( int b = (int) stairlinkingvars.size(); b < nblocks; b ++ )
3568 stairlinkingvars.push_back( emptyVector );
3569
3570 for( int i = 0; i < nconss; ++ i )
3571 {
3572 consnum = i;
3573 consblock = ( (int) (size_t) SCIPhashmapGetImage( constoblock, (void*) (size_t) i ) ) - 1;
3574 assert( consblock >= 0 && consblock <= nblocks );
3575 if( consblock == nblocks )
3576 setConsToMaster( consnum );
3577 else
3578 setConsToBlock( consnum, consblock );
3579 }
3580
3581 for( int i = 0; i < nvars; ++ i )
3582 {
3583 varinblocks.clear();
3584 varnum = i;
3585
3586 /* test if the variable can be found in blocks */
3587 for( int b = 0; b < nblocks; ++ b )
3588 {
3589 varInBlock = false;
3590 for( size_t k = 0; k < conssforblocks[b].size() && ! varInBlock; ++ k )
3591 {
3592 for( int l = 0; l < detprobdata->getNVarsForCons( conssforblocks[b][k] ) && ! varInBlock; ++ l )
3593 {
3594 if( varnum == ( detprobdata->getVarsForCons( conssforblocks[b][k] ) )[l] )
3595 {
3596 varinblocks.push_back( b );
3597 varInBlock = true;
3598 }
3599 }
3600 }
3601 }
3602 if( varinblocks.size() == 1 ) /* if the var can be found in one block set the var to block var */
3603 setVarToBlock( varnum, varinblocks[0] );
3604 else if( varinblocks.size() == 2 ) /* if the variable can be found in two blocks check if it is a linking var or a stairlinking var*/
3605 {
3606 if( varinblocks[0] + 1 == varinblocks[1] )
3607 setVarToStairlinking( varnum, varinblocks[0], varinblocks[1] );
3608 else
3609 setVarToLinking(varnum);
3610 }
3611 else if( varinblocks.size() > 2 ) /* if the variable can be found in more than two blocks it is a linking var */
3612 setVarToLinking(varnum);
3613 else
3614 assert( varinblocks.size() == 0 );
3615 setVarToMaster( varnum );
3616 }
3617 sort();
3618 openvars = std::vector<int>( 0 );
3619 openconss = std::vector<int>( 0 );
3620 isvaropen = std::vector<bool>(nvars, false);
3621 isconsopen = std::vector<bool>(nconss, false);
3622
3623 deleteEmptyBlocks(false);
3624 sort();
3626
3627 return SCIP_OKAY;
3628}
3629
3630
3632 )
3633{
3634 int i;
3635 int j;
3636 bool isMasterVar;
3637 std::vector<int> foundMasterVarIndices;
3638 std::vector<int>& lvars = getLinkingvars();
3639
3640 DETPROBDATA* detprobdata = this->getDetprobdata();
3641
3642 // sort Master constraints for binary search
3643 sort();
3644
3645 for( i = 0; i < getNLinkingvars(); ++ i )
3646 {
3647 isMasterVar = true;
3648 std::vector<int>& varcons = detprobdata->getConssForVar(lvars[i]);
3649 for( j = 0; j < detprobdata->getNConssForVar(lvars[i]); ++ j )
3650 {
3651 if( !isconsmaster[varcons[j]] )
3652 {
3653 isMasterVar = false;
3654 break;
3655 }
3656 }
3657
3658 if( isMasterVar )
3659 {
3660 foundMasterVarIndices.push_back(i);
3661 }
3662 }
3663
3664 for( auto it = foundMasterVarIndices.rbegin(); it != foundMasterVarIndices.rend(); ++ it )
3665 {
3666 int lvar = lvars[*it];
3667 mastervars.push_back(lvar);
3668 mastervarssorted = false;
3669 hvoutdated = true;
3670 isvarmaster[lvar] = true;
3671 linkingvars.erase(linkingvars.begin() + *it);
3672 if( !linkingvarsforblocks.empty() )
3673 {
3674 for( int b = 0; b < nblocks; ++b )
3675 {
3676 auto itr = std::lower_bound(linkingvarsforblocks[b].begin(), linkingvarsforblocks[b].end(), lvar);
3677 if( itr != linkingvarsforblocks[b].end() && *itr == lvar )
3678 linkingvarsforblocks[b].erase(itr);
3679 }
3680 }
3681 }
3682}
3683
3684
3686 )
3687{
3688 int i;
3689 int j;
3690 int k;
3691
3692 int consblock;
3693 int block1 = - 1;
3694 int block2 = - 1;
3695
3696 std::vector<int>& lvars = getLinkingvars();
3697
3698 std::vector<int> foundMasterVarIndices;
3699 DETPROBDATA* detprobdata = this->getDetprobdata();
3700
3701 sort();
3702
3703 for( i = 0; i < getNLinkingvars(); ++ i )
3704 {
3705 block1 = -1;
3706 block2 = -1;
3707 std::vector<int>& varcons = detprobdata->getConssForVar(lvars[i]);
3708 for( j = 0; j < detprobdata->getNConssForVar(lvars[i]); ++ j )
3709 {
3710 consblock = -1;
3711 for( k = 0; k < nblocks; ++ k )
3712 {
3713 if( std::binary_search(conssforblocks[k].begin(), conssforblocks[k].end(), varcons[j]) )
3714 {
3715 consblock = k;
3716 break;
3717 }
3718 }
3719
3720 if( consblock == -1 )
3721 {
3722 block1 = -1;
3723 block2 = -1;
3724 break;
3725 }
3726 else if( block1 == consblock || block2 == consblock )
3727 {
3728 continue;
3729 }
3730 else if( block1 == -1 )
3731 {
3732 block1 = consblock;
3733 continue;
3734 }
3735 else if( block2 == -1 )
3736 {
3737 block2 = consblock;
3738 continue;
3739 }
3740 else
3741 {
3742 block1 = -1;
3743 block2 = -1;
3744 break;
3745 }
3746 }
3747
3748 if( block1 != -1 && block2 != -1 && ( block1 == block2 + 1 || block1 + 1 == block2 ) )
3749 {
3750 setVarToStairlinking(lvars[i], block1, block2);
3751 foundMasterVarIndices.push_back(i);
3752 if( !linkingvarsforblocks.empty() )
3753 {
3754 auto it = std::lower_bound(linkingvarsforblocks[block1].begin(), linkingvarsforblocks[block1].end(), lvars[i]);
3755 if( it != linkingvarsforblocks[block1].end() && *it == lvars[i] )
3756 linkingvarsforblocks[block1].erase(it);
3757 it = std::lower_bound(linkingvarsforblocks[block2].begin(), linkingvarsforblocks[block2].end(), lvars[i]);
3758 if( it != linkingvarsforblocks[block2].end() && *it == lvars[i] )
3759 linkingvarsforblocks[block2].erase(it);
3760 }
3761 }
3762 }
3763
3764 for( auto it = foundMasterVarIndices.rbegin(); it != foundMasterVarIndices.rend(); ++ it )
3765 {
3766 linkingvars.erase(linkingvars.begin() + *it);
3767 }
3768}
3769
3770
3771void PARTIALDECOMP::findLinkingvarBlocks()
3772{
3773 if( linkingvarsforblocks.empty() && isComplete() )
3774 {
3775 auto* detprobdata = getDetprobdata();
3776
3777 // sort linking vars
3778 sort();
3779
3780 // add linking vars to vectors
3781 linkingvarsforblocks.resize(nblocks);
3782 for( int lvar : linkingvars )
3783 {
3784 auto& conss = detprobdata->getConssForVar(lvar);
3785 for( int cons : conss )
3786 {
3787 assert(!isConsMastercons(cons));
3788 for( int b = 0; b < nblocks; ++b )
3789 {
3790 if( isConsBlockconsOfBlock(cons, b) )
3791 {
3792 linkingvarsforblocks[b].push_back(lvar);
3793 }
3794 }
3795 }
3796 }
3797 }
3798}
3799
3800
3801std::vector< std::pair< int, std::vector< int > > > PARTIALDECOMP::findLinkingVarsPotentiallyStairlinking(
3802 )
3803{
3804 std::vector< std::pair< int, std::vector< int > > > blocksOfVars( 0 );
3805 int blockcounter;
3806 DETPROBDATA* detprobdata = this->getDetprobdata();
3807
3808 /* if there is at least one linking variable, then the blocks of vars must be created. */
3809 if( getNLinkingvars() > 0 )
3810 {
3811 std::vector<int>& lvars = getLinkingvars();
3812 sort();
3813
3814 /* check every linking var */
3815 for ( int v = 0; v < getNLinkingvars(); ++v )
3816 {
3817 std::vector< int > blocksOfVar( 0 );
3818 blockcounter = 0;
3819
3820 std::vector<int>& varcons = detprobdata->getConssForVar( lvars[v] );
3821
3822 /* find all blocks that are hit by this linking var */
3823 for ( int c = 0; c < detprobdata->getNConssForVar( lvars[v] ) && blockcounter <= 2; ++c )
3824 {
3825 for ( int b = 0; b < nblocks && blockcounter <= 2; ++b )
3826 {
3827 if ( std::binary_search( conssforblocks[b].begin(),
3828 conssforblocks[b].end(), varcons[c] ) )
3829 {
3830 /* if the hit block is new, add it to blockOfVar vector */
3831 if ( std::find( blocksOfVar.begin(), blocksOfVar.end(), b ) == blocksOfVar.end() )
3832 {
3833 ++blockcounter;
3834 blocksOfVar.push_back( b );
3835 }
3836 }
3837 }
3838 }
3839
3840 /* if the var hits exactly two blocks, it is potentially stairlinking */
3841 if ( blockcounter == 2 )
3842 {
3843 std::pair< int, std::vector< int > > pair( v, blocksOfVar );
3844 blocksOfVars.push_back( pair );
3845 }
3846 }
3847 }
3848
3849 return blocksOfVars;
3850}
3851
3852
3854 int ancestorindex
3855 )
3856{
3857 assert( 0 <= ancestorindex && ancestorindex < (int) listofancestorids.size() );
3858
3859 return listofancestorids[ancestorindex];
3860}
3861
3862
3864{
3865 return listofancestorids;
3866}
3867
3868const std::vector<int>& PARTIALDECOMP::getBlocksForEqClass(int eqclass)
3869{
3871 assert((int) eqclasstoblocks.size() > eqclass);
3872 return eqclasstoblocks[eqclass];
3873}
3874
3876{
3878 assert((int) eqclasstoblocks.size() > eqclass && !eqclasstoblocks[eqclass].empty());
3879 return eqclasstoblocks[eqclass][0];
3880}
3881
3882
3884 std::vector<int>& newlist
3885 )
3886{
3887 listofancestorids = newlist;
3888}
3889
3890
3892 int ancestor
3893 )
3894{
3895 assert( 0 <= ancestor );
3896 listofancestorids.push_back(ancestor);
3897}
3898
3899
3901 int ancestorid
3902 )
3903{
3904 int i;
3905 std::vector<int> indices;
3906 // look for id (including duplicates)
3907 for(i = 0; i < (int) listofancestorids.size(); i++)
3908 {
3909 // add indices, descending
3910 if(listofancestorids.at(i) == ancestorid)
3911 {
3912 indices.insert(indices.begin(), i);
3913 }
3914 }
3915 // as indices are descending this is consistent
3916 for(auto index : indices)
3917 listofancestorids.erase(listofancestorids.begin() + index);
3918}
3919
3920
3922 int detectorchainindex
3923 )
3924{
3925 assert( 0 <= detectorchainindex && detectorchainindex < (int) detectorclocktimes.size() );
3926
3927 return detectorclocktimes[ detectorchainindex ];
3928}
3929
3930
3932{
3933 return detectorclocktimes;
3934}
3935
3936
3937void PARTIALDECOMP::getConsPartitionData(
3938 int detectorchainindex,
3939 ConsPartition** partition,
3940 std::vector<int>& consclassesmaster
3941 )
3942{
3943 assert(consPartitionUsed(detectorchainindex) );
3944
3945 *partition = dynamic_cast<ConsPartition*>( usedpartition[detectorchainindex] );
3946 consclassesmaster = classestomaster[detectorchainindex];
3947}
3948
3949
3951 std::vector<SCIP_Real>& newvector)
3952{
3953 detectorclocktimes = newvector;
3954}
3955
3956
3957bool PARTIALDECOMP::varPartitionUsed(
3958 int detectorchainindex
3959 )
3960{
3961 assert( 0 <= detectorchainindex && detectorchainindex < (int) usedpartition.size() );
3962
3963 return (usedpartition[detectorchainindex] != NULL )
3964 && (dynamic_cast<VarPartition*>( usedpartition[detectorchainindex] ) != NULL );
3965}
3966
3967
3969 int block
3970 )
3971{
3972 assert( block >= 0 && block < nblocks );
3973 return conssforblocks[block];
3974}
3975
3976
3977std::vector<GCG_DETECTOR*>& PARTIALDECOMP::getDetectorchain()
3978{
3979 return detectorchain;
3980}
3981
3982
3983std::string PARTIALDECOMP::getDetectorStatistics(
3984 int detectorchainindex
3985 )
3986{
3987 std::stringstream output;
3988
3989 if( (int) getDetectorClockTimes().size() > detectorchainindex )
3990 output << " Detection time: " << getDetectorClockTime( detectorchainindex ) << std::endl;
3991 if( (int) getPctConssFromFreeVector().size() > detectorchainindex )
3992 output << " % newly assigned constraints: " << getPctConssFromFree( detectorchainindex ) << std::endl;
3993 if( (int) getPctConssToBorderVector().size() > detectorchainindex )
3994 output << " % constraints the detector assigned to border: " << getPctConssToBorder( detectorchainindex ) << std::endl;
3995 if( (int) getPctConssToBlockVector().size() > detectorchainindex )
3996 output << " % constraints the detector assigned to blocks: " << getPctConssToBlock( detectorchainindex ) << std::endl;
3997 if( (int) getPctVarsFromFreeVector().size() > detectorchainindex )
3998 output << " % newly assigned variables: " << getPctVarsFromFree( detectorchainindex ) << std::endl;
3999 if( (int) getPctVarsToBorderVector().size() > detectorchainindex )
4000 output << " % variables the detector assigned to border: " << getPctVarsToBorder( detectorchainindex ) << std::endl;
4001 if( (int) getPctVarsToBlockVector().size() > detectorchainindex )
4002 output << " % variables the detector assigned to blocks: " << getPctVarsToBlock( detectorchainindex ) << std::endl;
4003 if( (int) getNNewBlocksVector().size() > detectorchainindex )
4004 output << " New blocks: " << getNNewBlocks( detectorchainindex ) << std::endl;
4005
4006 return output.str();
4007}
4008
4009
4010std::string PARTIALDECOMP::getDetectorPartitionInfo(
4011 int detectorchainindex,
4012 bool displayConssVars
4013 )
4014{
4015 std::stringstream output;
4016 DETPROBDATA* detprobdata = this->getDetprobdata();
4017
4018 if( consPartitionUsed(detectorchainindex) )
4019 {
4020 ConsPartition* partition;
4021 std::vector<int> constomaster;
4022
4023 getConsPartitionData(detectorchainindex, &partition, constomaster);
4024
4025 output << " Used conspartition: " << partition->getName() << std::endl;
4026 output << " Pushed to master:";
4027
4028 if( !constomaster.empty() )
4029 {
4030 if( displayConssVars )
4031 {
4032 output << std::endl << " " << partition->getClassName(constomaster[0] ) << " ("
4033 << partition->getClassDescription(constomaster[0] ) << "): ";
4034 bool first = true;
4035 for( int c = 0; c < partition->getNConss(); ++c )
4036 {
4037 if( partition->getClassOfCons(c ) == constomaster[0] )
4038 {
4039 if( first )
4040 {
4041 output << SCIPconsGetName(detprobdata->getCons(c));
4042 first = false;
4043 }
4044 else
4045 {
4046 output << ", " << SCIPconsGetName(detprobdata->getCons(c));
4047 }
4048 }
4049 }
4050 output << std::endl;
4051 }
4052 else
4053 {
4054 output << " " << partition->getClassName(constomaster[0] );
4055 }
4056 }
4057
4058 for( size_t i = 1; i < constomaster.size(); ++i )
4059 {
4060 if( displayConssVars )
4061 {
4062 output << " " << partition->getClassName(constomaster[i] ) << " ("
4063 << partition->getClassDescription(constomaster[i] ) << "): ";
4064 bool first = true;
4065 for( int c = 0; c < partition->getNConss(); ++c )
4066 {
4067 if( partition->getClassOfCons(c ) == constomaster[i] )
4068 {
4069 if( first )
4070 {
4071 output << SCIPconsGetName(detprobdata->getCons(c));
4072 first = false;
4073 }
4074 else
4075 {
4076 output << ", " << SCIPconsGetName(detprobdata->getCons(c));
4077 }
4078 }
4079 }
4080 output << std::endl;
4081 }
4082 else
4083 {
4084 output << ", " << partition->getClassName(constomaster[i] );
4085 }
4086 }
4087
4088 if ( !displayConssVars || constomaster.empty() )
4089 {
4090 output << std::endl;
4091 }
4092 }
4093
4094 if( varPartitionUsed(detectorchainindex) )
4095 {
4096 VarPartition* partition;
4097 std::vector<int> vartolinking;
4098 std::vector<int> vartomaster;
4099
4100 getVarPartitionData(detectorchainindex, &partition, vartolinking, vartomaster);
4101
4102 output << " Used varpartition: " << partition->getName() << std::endl;
4103 output << " Pushed to linking:";
4104
4105 if( !vartolinking.empty() )
4106 {
4107 if( displayConssVars )
4108 {
4109 output << std::endl << " " << partition->getClassName(vartolinking[0] ) << " ("
4110 << partition->getClassDescription(vartolinking[0] ) << "): ";
4111 bool first = true;
4112 for( int v = 0; v < partition->getNVars(); ++v )
4113 {
4114 if( partition->getClassOfVar(v ) == vartolinking[0] )
4115 {
4116 if( first )
4117 {
4118 output << SCIPvarGetName(detprobdata->getVar(v));
4119 first = false;
4120 }
4121 else
4122 {
4123 output << ", " << SCIPvarGetName(detprobdata->getVar(v));
4124 }
4125 }
4126 }
4127 output << std::endl;
4128 }
4129 else
4130 {
4131 output << " " << partition->getClassName(vartolinking[0] );
4132 }
4133 }
4134
4135 for( size_t i = 1; i < vartolinking.size(); ++i )
4136 {
4137 if( displayConssVars )
4138 {
4139 output << " " << partition->getClassName(vartolinking[i] ) << " ("
4140 << partition->getClassDescription(vartolinking[i] ) << "): ";
4141 bool first = true;
4142 for( int v = 0; v < partition->getNVars(); ++v )
4143 {
4144 if( partition->getClassOfVar(v ) == vartolinking[i] )
4145 {
4146 if( first )
4147 {
4148 output << SCIPvarGetName(detprobdata->getVar(v));
4149 first = false;
4150 }
4151 else
4152 {
4153 output << ", " << SCIPvarGetName(detprobdata->getVar(v));
4154 }
4155 }
4156 }
4157 output << std::endl;
4158 }
4159 else
4160 {
4161 output << ", " << partition->getClassName(vartolinking[i] );
4162 }
4163 }
4164
4165 if ( !displayConssVars || vartolinking.empty() )
4166 {
4167 output << std::endl;
4168 }
4169
4170 output << " Pushed to master:";
4171
4172 if( !vartomaster.empty() )
4173 {
4174 if( displayConssVars )
4175 {
4176 output << std::endl << " " << partition->getClassName(vartomaster[0] ) << " ("
4177 << partition->getClassDescription(vartomaster[0] ) << "): ";
4178 bool first = true;
4179 for( int v = 0; v < partition->getNVars(); ++v )
4180 {
4181 if( partition->getClassOfVar(v ) == vartomaster[0] )
4182 {
4183 if( first )
4184 {
4185 output << SCIPvarGetName(detprobdata->getVar(v));
4186 first = false;
4187 }
4188 else
4189 {
4190 output << ", " << SCIPvarGetName(detprobdata->getVar(v));
4191 }
4192 }
4193 }
4194 output << std::endl;
4195 }
4196 else
4197 {
4198 output << " " << partition->getClassName(vartomaster[0] );
4199 }
4200 }
4201
4202 for( size_t i = 1; i < vartomaster.size(); ++i )
4203 {
4204 if( displayConssVars )
4205 {
4206 output << " " << partition->getClassName(vartomaster[i] ) << " ("
4207 << partition->getClassDescription(vartomaster[i] ) << "): ";
4208 bool first = true;
4209 for( int v = 0; v < partition->getNVars(); ++v )
4210 {
4211 if( partition->getClassOfVar(v ) == vartolinking[i] )
4212 {
4213 if( first )
4214 {
4215 output << SCIPvarGetName(detprobdata->getVar(v));
4216 first = false;
4217 }
4218 else
4219 {
4220 output << ", " << SCIPvarGetName(detprobdata->getVar(v));
4221 }
4222 }
4223 }
4224 output << std::endl;
4225 }
4226 else
4227 {
4228 output << ", " << partition->getClassName(vartomaster[i] );
4229 }
4230 }
4231
4232 if ( !displayConssVars || vartomaster.empty() )
4233 {
4234 output << std::endl;
4235 }
4236 }
4237
4238 return output.str();
4239}
4240
4241
4243{
4244 return isfinishedbyfinisher;
4245}
4246
4247
4249{
4250 calcHashvalue();
4251 return hashvalue;
4252}
4253
4254
4256{
4257 return id;
4258}
4259
4260
4262{
4263 return linkingvars;
4264}
4265
4266
4268 int block
4269 )
4270{
4271 if( linkingvarsforblocks.empty() )
4272 {
4273 findLinkingvarBlocks();
4274 if( linkingvarsforblocks.empty() )
4275 {
4276 SCIPerrorMessage("Information about linking var blocks is not available for partialdec %d.", getID());
4277 throw std::exception();
4278 }
4279 }
4280
4281 assert(block >= 0 && block < (int) linkingvarsforblocks.size());
4282 return linkingvarsforblocks[block];
4283}
4284
4285
4287{
4288 return masterconss;
4289}
4290
4291
4293{
4294 return mastervars;
4295}
4296
4297
4299 int blockid
4300 ){
4301
4302 if( ncoeffsforblock.empty() )
4303 calcNCoeffsForBlocks();
4304
4305 return ncoeffsforblock[blockid];
4306}
4307
4308
4310 ){
4311
4312 if( ncoeffsforblock.empty() )
4313 calcNCoeffsForBlocks();
4314
4315 return ncoeffsformaster;
4316}
4317
4318
4320 int block
4321 )
4322{
4323 assert(block >= 0 && block < (int) blockstructures.size());
4324 return blockstructures[block];
4325}
4326
4327
4328std::vector<BLOCK_STRUCTURE*>& PARTIALDECOMP::getBlockStructures()
4329{
4330 return blockstructures;
4331}
4332
4333
4335 GCG_SCORE* score,
4336 SCIP_Real scorevalue
4337 )
4338{
4339 assert(score != NULL);
4340 assert(scorevalue == SCIP_INVALID || (scorevalue >= 0.0 && scorevalue <= 1.0));
4341 assert(SCIPhashmapExists(maptoscores, (void*)score));
4342
4343 SCIPhashmapSetImageReal(maptoscores, (void*)score, scorevalue);
4344}
4345
4346
4348 GCG_SCORE* score
4349 )
4350{
4351 assert(SCIPhashmapExists(maptoscores, (void*)score));
4352
4353 SCIP_Real scorevalue = SCIPhashmapGetImageReal(maptoscores, (void*)score);
4354
4355 if( scorevalue == SCIP_INVALID )
4356 {
4357 /* if there are indicator constraints in the master we want to reject this decomposition */
4358 DETPROBDATA* detprobdata = this->getDetprobdata();
4359
4360 for( int mc = 0; mc < getNMasterconss(); ++mc )
4361 {
4362 SCIP_CONS* cons;
4363 cons = detprobdata->getCons(getMasterconss()[mc]);
4365 {
4366 setScore(score, 0.);
4367 return 0.;
4368 }
4369 }
4371 SCIPstartClock(scip, clock);
4372 score->scorecalc(gcg, score, this->id, &scorevalue);
4373 SCIPstopClock(scip, clock);
4374 setScore(score, scorevalue);
4375 }
4376
4377 return scorevalue;
4378}
4379
4380
4382 )
4383{
4385}
4386
4387
4389 )
4390{
4391 unsigned long matrixarea;
4392 unsigned long blockarea;
4393
4394 matrixarea = (unsigned long) this->getNVars() * (unsigned long) this->getNConss() ;
4395 blockarea = 0;
4396
4397 for( int i = 0; i < getNBlocks(); ++ i )
4398 {
4399 blockarea += (unsigned long) this->getNConssForBlock(i) * ( (unsigned long) this->getNVarsForBlock(i) );
4400 }
4401 SCIP_Real blockareascore = 1. - (matrixarea == 0 ? 0 : ( (SCIP_Real) blockarea / (SCIP_Real) matrixarea ));
4402
4403 return blockareascore;
4404}
4405
4406
4407
4409{
4410 return usergiven;
4411}
4412
4413
4415{
4416 return (int) listofancestorids.size();
4417}
4418
4419
4421{
4422 return nblocks;
4423}
4424
4425
4427{
4428 return nconss;
4429}
4430
4431
4433 int block
4434 )
4435{
4436 assert( block >= 0 && block < nblocks );
4437 return (int) conssforblocks[block].size();
4438}
4439
4440
4441std::vector<std::string>& PARTIALDECOMP::getDetectorchainInfo()
4442{
4443 return detectorchaininfo;
4444}
4445
4446
4448{
4449 return (int) detectorchain.size();
4450}
4451
4452
4453int PARTIALDECOMP::getNUsedPartitions()
4454{
4455 return (int) usedpartition.size();
4456}
4457
4458
4460{
4461 return (int) linkingvars.size();
4462}
4463
4464
4466 int detectorchainindex
4467 )
4468{
4469 assert( 0 <= detectorchainindex && detectorchainindex < (int) detectorchain.size() );
4470
4471 return nnewblocks[detectorchainindex];
4472}
4473
4474
4476{
4477 return nnewblocks;
4478}
4479
4480
4482{
4483 return (int) masterconss.size();
4484}
4485
4486
4488{
4489 return (int) mastervars.size();
4490}
4491
4492
4494{
4495 int nstairlinkingvars = 0;
4496 for( int b = 0; b < getNBlocks(); ++ b )
4497 nstairlinkingvars += getNStairlinkingvars( b );
4498
4499 return nstairlinkingvars;
4500}
4501
4502
4504{
4505 return (int) openconss.size();
4506}
4507
4508
4510{
4511 return (int) openvars.size();
4512}
4513
4514
4516{
4517 return nequivalenceclasses;
4518}
4519
4520
4522 int block
4523 )
4524{
4525 assert( block >= 0 && block < nblocks );
4526 assert( (int) stairlinkingvars.size() > block );
4527 return (int) stairlinkingvars[block].size();
4528}
4529
4530
4532{
4533 return nvars;
4534}
4535
4536
4538 int block
4539 )
4540{
4541 assert( block >= 0 && block < nblocks );
4542 return (int) varsforblocks[block].size();
4543}
4544
4545
4547{
4548 int count = 0;
4549 for( auto& block : varsforblocks )
4550 {
4551 count += (int) block.size();
4552 }
4553
4554 return count;
4555}
4556
4557
4559{
4560 return openconss;
4561}
4562
4563
4565{
4566 return openvars;
4567}
4568
4569
4571 int detectorchainindex
4572 )
4573{
4574 assert( 0 <= detectorchainindex && detectorchainindex < (int) detectorchain.size() );
4575
4576 return pctvarstoborder[detectorchainindex];
4577}
4578
4579
4581{
4582 return pctvarstoborder;
4583}
4584
4585
4587 std::vector<SCIP_Real>& newvector
4588 )
4589{
4590 pctvarstoborder = newvector;
4591}
4592
4593
4595 int detectorchainindex
4596 )
4597{
4598 assert( 0 <= detectorchainindex && detectorchainindex < (int) detectorchain.size() );
4599
4600 return pctvarstoblock[detectorchainindex];
4601}
4602
4603
4605{
4606 return pctvarstoblock;
4607}
4608
4609
4610
4612 std::vector<SCIP_Real>& newvector
4613)
4614{
4615 pctvarstoblock = newvector;
4616}
4617
4618
4620 int detectorchainindex
4621 )
4622{
4623 assert( 0 <= detectorchainindex && detectorchainindex < (int) detectorchain.size() );
4624
4625 return pctvarsfromfree[detectorchainindex];
4626}
4627
4628
4630{
4631 return pctvarsfromfree;
4632}
4633
4634
4636 std::vector<SCIP_Real>& newvector
4637 )
4638{
4639 pctvarsfromfree = newvector;
4640}
4641
4642
4644 int detectorchainindex
4645 )
4646{
4647 assert( 0 <= detectorchainindex && detectorchainindex < (int) detectorchain.size() );
4648
4649 return pctconsstoborder[detectorchainindex];
4650}
4651
4652
4654{
4655 return pctconsstoborder;
4656}
4657
4658
4660 std::vector<SCIP_Real>& newvector
4661 )
4662{
4663 pctconsstoborder = newvector;
4664}
4665
4666
4668 int detectorchainindex
4669 )
4670{
4671 assert( 0 <= detectorchainindex && detectorchainindex < (int) detectorchain.size() );
4672
4673 return pctconsstoblock[detectorchainindex];
4674}
4675
4676
4678{
4679 return pctconsstoblock;
4680}
4681
4682
4684 std::vector<SCIP_Real>& newvector
4685 )
4686{
4687 pctconsstoblock = newvector;
4688}
4689
4690
4692 int detectorchainindex
4693 )
4694{
4695 assert( 0 <= detectorchainindex && detectorchainindex < (int) detectorchain.size() );
4696
4697 return pctconssfromfree[detectorchainindex];
4698}
4699
4700
4702{
4703 return pctconssfromfree;
4704}
4705
4706
4708 int blockid
4709 )
4710{
4712 return blockstoeqclasses[blockid];
4713}
4714
4716 int eqclass,
4717 int eqclassblock
4718 )
4719{
4720 assert(eqclassesvarmappings[eqclass][eqclassblock].size() == getVarsForBlock(getBlocksForEqClass(eqclass).at(eqclassblock)).size());
4721 return eqclassesvarmappings[eqclass][eqclassblock];
4722}
4723
4724
4726{
4727 DETPROBDATA* detprobdata;
4728 if( original )
4730 else
4732
4733 assert(detprobdata != NULL);
4734
4735 return detprobdata;
4736}
4737
4738
4740 std::vector<SCIP_Real>& newvector
4741 )
4742{
4743 pctconssfromfree = newvector;
4744}
4745
4746
4748 int block
4749 )
4750{
4751 assert( block >= 0 && block < (int) stairlinkingvars.size() );
4752 return stairlinkingvars[block];
4753}
4754
4755
4756void PARTIALDECOMP::getVarPartitionData(
4757 int detectorchainindex,
4758 VarPartition** partition,
4759 std::vector<int>& varclasseslinking,
4760 std::vector<int>& varclassesmaster
4761 )
4762{
4763 assert(varPartitionUsed(detectorchainindex) );
4764
4765 *partition = dynamic_cast<VarPartition*>( usedpartition[detectorchainindex] );
4766 varclasseslinking = classestolinking[detectorchainindex];
4767 varclassesmaster = classestomaster[detectorchainindex];
4768}
4769
4770
4772 int block
4773 )
4774{
4775 assert( block >= 0 && block < (int) varsforblocks.size() );
4776 return varsforblocks[block];
4777}
4778
4779
4781 int varid,
4782 int block
4783 )
4784{
4785 assert(block >= 0 && block < (int) varsforblocks.size());
4786 std::vector<int>::iterator lb = lower_bound(varsforblocks[block].begin(), varsforblocks[block].end(), varid);
4787
4788 if( lb != varsforblocks[block].end() && *lb == varid )
4789 return (int) (lb - varsforblocks[block].begin());
4790 else
4791 return -1;
4792
4793}
4794
4795
4797{
4798 return ( 0 == getNOpenconss() && 0 == getNOpenvars() );
4799}
4800
4801
4802bool PARTIALDECOMP::isConsBlockconsOfBlock(
4803 int cons,
4804 int block
4805 )
4806{
4807 assert( cons >= 0 && cons < nconss );
4808 assert( block >= 0 && block < (int) conssforblocks.size() );
4809 std::vector<int>::iterator lb = lower_bound( conssforblocks[block].begin(), conssforblocks[block].end(), cons );
4810 if( lb != conssforblocks[block].end() && *lb == cons )
4811 return true;
4812 else
4813 return false;
4814}
4815
4816
4818 int cons
4819 )
4820{
4821 assert( cons >= 0 && cons < (int) isconsmaster.size() );
4822 return isconsmaster[cons];
4823}
4824
4825
4827 int cons
4828 )
4829{
4830 assert( cons >= 0 && cons < (int) isconsopen.size() );
4831 return isconsopen[cons];
4832}
4833
4834
4836{
4837 return original;
4838}
4839
4840
4842{
4843 return !original;
4844}
4845
4846
4848 PARTIALDECOMP* otherpartialdec,
4849 SCIP_Bool* isequal,
4850 bool sortpartialdecs
4851 )
4852{
4853 if( sortpartialdecs )
4854 {
4855 sort();
4856 otherpartialdec->sort();
4857 }
4858
4859 * isequal = isEqual( otherpartialdec );
4860
4861 return SCIP_OKAY;
4862}
4863
4864
4866 PARTIALDECOMP* other
4867 )
4868{
4869 if( getNMasterconss() != other->getNMasterconss() || getNMastervars() != other->getNMastervars()
4870 || getNBlocks() != other->getNBlocks() || getNLinkingvars() != other->getNLinkingvars() )
4871 return false;
4872
4873 std::vector<std::pair<int, int>> blockorderthis;
4874 std::vector<std::pair<int, int>> blockorderother;
4875
4876 /* find sorting for blocks (non decreasing according smallest row index) */
4877 for( int i = 0; i < this->nblocks; ++ i )
4878 {
4879 blockorderthis.emplace_back(i, conssforblocks[i][0]);
4880 blockorderother.emplace_back(i, other->conssforblocks[i][0]);
4881 }
4882
4883 std::sort(blockorderthis.begin(), blockorderthis.end(), compare_blocks);
4884 std::sort(blockorderother.begin(), blockorderother.end(), compare_blocks);
4885
4886 /* compares the number of stairlinking vars */
4887 for( int b = 0; b < getNBlocks(); ++ b )
4888 {
4889 int blockthis = blockorderthis[b].first;
4890 int blockother = blockorderother[b].first;
4891
4892 if( getNStairlinkingvars(blockthis) != other->getNStairlinkingvars(blockother) )
4893 return false;
4894 }
4895
4896 /* compares the number of constraints and variables in the blocks*/
4897 for( int b = 0; b < getNBlocks(); ++ b )
4898 {
4899 int blockthis = blockorderthis[b].first;
4900 int blockother = blockorderother[b].first;
4901
4902 if( ( getNVarsForBlock( blockthis ) != other->getNVarsForBlock(blockother) )
4903 || ( getNConssForBlock( blockthis ) != other->getNConssForBlock(blockother) ) )
4904 return false;
4905 }
4906
4907 /* compares the master cons */
4908 for( int j = 0; j < getNMasterconss(); ++ j )
4909 {
4910 if( getMasterconss()[j] != other->getMasterconss()[j] )
4911 return false;
4912 }
4913
4914 /* compares the master vars */
4915 for( int j = 0; j < getNMastervars(); ++ j )
4916 {
4917 if( getMastervars()[j] != other->getMastervars()[j] )
4918 return false;
4919 }
4920
4921 /* compares the constrains and variables in the blocks */
4922 for( int b = 0; b < getNBlocks(); ++ b )
4923 {
4924 int blockthis = blockorderthis[b].first;
4925 int blockother = blockorderother[b].first;
4926
4927 for( int j = 0; j < getNConssForBlock( blockthis ); ++ j )
4928 {
4929 if( getConssForBlock( blockthis )[j] != other->getConssForBlock(blockother)[j] )
4930 return false;
4931 }
4932
4933 for( int j = 0; j < getNVarsForBlock( blockthis ); ++ j )
4934 {
4935 if( getVarsForBlock(blockthis)[j] != other->getVarsForBlock(blockother)[j] )
4936 return false;
4937 }
4938
4939 for( int j = 0; j < getNStairlinkingvars( blockthis ); ++ j )
4940 {
4941 if( getStairlinkingvars(blockthis)[j] != other->getStairlinkingvars(blockother)[j] )
4942 return false;
4943 }
4944 }
4945
4946 /* compares the linking vars */
4947 for( int j = 0; j < getNLinkingvars(); ++ j )
4948 {
4949 if( getLinkingvars()[j] != other->getLinkingvars()[j] )
4950 return false;
4951 }
4952
4953 return true;
4954}
4955
4956
4958{
4959 return !blockstructures.empty();
4960}
4961
4962
4964 GCG_DETECTOR* detector
4965 )
4966{
4967 std::vector<GCG_DETECTOR*>::const_iterator iter = std::find(detectorchain.begin(), detectorchain.end(), detector);
4968
4969 return iter != detectorchain.end();
4970}
4971
4972
4974{
4975 if( getNBlocks() == 1 && (SCIP_Real) getNConssForBlock( 0 ) >= 0.95 * getNConss() )
4976 return true;
4977
4978 if( getNConss() == getNMasterconss() )
4979 return true;
4980
4981 if( getNConss() == getNOpenconss() && getNVars() == getNOpenvars() )
4982 return true;
4983
4984 if( getNVars() == getNMastervars() + getNLinkingvars() )
4985 return true;
4986
4987 return false;
4988}
4989
4990
4992{
4993 return isselected;
4994}
4995
4996
4998 int var,
4999 int block
5000 )
5001{
5002 assert( var >= 0 && var < nvars );
5003 assert( block >= 0 && block < nconss );
5004
5005 return std::binary_search(varsforblocks[block].begin(), varsforblocks[block].end(), var);
5006}
5007
5008
5010 int var
5011 )
5012{
5013 assert( var >= 0 && var < nvars );
5014 return isvarmaster[var];
5015}
5016
5017
5019 int var
5020 )
5021{
5022 assert(var >= 0 && var < nvars);
5023 return std::binary_search(linkingvars.begin(), linkingvars.end(), var);
5024}
5025
5026
5028 int var,
5029 int block
5030 )
5031{
5032 assert(var >= 0 && var < nvars);
5033 if( linkingvarsforblocks.empty() )
5034 {
5035 findLinkingvarBlocks();
5036 if( linkingvarsforblocks.empty() )
5037 {
5038 SCIPerrorMessage("Information about linking var blocks is not available for partialdec %d.", getID());
5039 throw std::exception();
5040 }
5041 }
5042
5043 assert(block >= 0 && block < (int) linkingvarsforblocks.size());
5044 return std::binary_search(linkingvarsforblocks[block].begin(), linkingvarsforblocks[block].end(), var);
5045}
5046
5047
5049 int var
5050 )
5051{
5052 assert( var >= 0 && var < nvars );
5053 return isvaropen[var];
5054}
5055
5056
5058 int var
5059 )
5060{
5061 for( int b = 0; b < nblocks; ++ b )
5062 {
5063 std::vector<int>::iterator lb = lower_bound(stairlinkingvars[b].begin(), stairlinkingvars[b].end(), var);
5064 if( lb != stairlinkingvars[b].end() && *lb == var )
5065 return true;
5066 }
5067 return false;
5068}
5069
5070
5072 int var,
5073 int block
5074 )
5075{
5076 assert(var >= 0 && var < nvars);
5077 assert(block >= 0 && block < nblocks);
5078 std::vector<int>::iterator lb = lower_bound(stairlinkingvars[block].begin(), stairlinkingvars[block].end(), var);
5079 if( lb != stairlinkingvars[block].end() && *lb == var )
5080 return true;
5081 else
5082 {
5083 if( block == 0 )
5084 return false;
5085 else
5086 {
5087 lb = lower_bound(stairlinkingvars[block - 1].begin(), stairlinkingvars[block - 1].end(), var);
5088 return ( lb != stairlinkingvars[block-1].end() && *lb == var );
5089 }
5090 }
5091}
5092
5093
5095 FILE* file
5096 )
5097{
5098
5099 int nusedpartitions = (int) getNUsedPartitions();
5100 int nconspartitions = 0;
5101 int nvarpartitions = 0;
5102
5103 for( int i = 0; i < nusedpartitions; ++i)
5104 {
5105 if( usedpartition[i] == NULL )
5106 continue;
5107
5108 if( dynamic_cast<ConsPartition*>( usedpartition[i] ) != NULL )
5109 {
5110 /* partition is cons partition */
5111 ++nconspartitions;
5112 }
5113 else
5114 {
5115 /* partition is var partition */
5116 ++nvarpartitions;
5117 }
5118 }
5119
5120 SCIPmessageFPrintInfo(SCIPgetMessagehdlr(scip), file, "%d\n", nconspartitions);
5121
5122 for( int i = 0; i < nusedpartitions; ++i)
5123 {
5124 if( dynamic_cast<ConsPartition*>( usedpartition[i] ) != NULL )
5125 {
5126 /* partition is cons partition */
5127 int nmasterclasses = (int) classestomaster[i].size();
5128 SCIPmessageFPrintInfo(SCIPgetMessagehdlr(scip), file, "%s\n", usedpartition[i]->getName());
5129 SCIPmessageFPrintInfo(SCIPgetMessagehdlr(scip), file, "%d\n", nmasterclasses);
5130 for ( int mclass = 0; mclass < (int) classestomaster[i].size(); ++mclass )
5131 {
5132 int classid = classestomaster[i][mclass];
5133 SCIPmessageFPrintInfo(SCIPgetMessagehdlr(scip), file, "%s\n", usedpartition[i]->getClassName(classid));
5134 }
5135 }
5136 }
5137
5138 SCIPmessageFPrintInfo(SCIPgetMessagehdlr(scip), file, "%d\n", nvarpartitions);
5139
5140 for( int i = 0; i < nusedpartitions; ++i)
5141 {
5142 if( dynamic_cast<VarPartition*>( usedpartition[i] ) != NULL )
5143 {
5144 /* partition is var partition */
5145 int nmasterclasses = (int) classestomaster[i].size();
5146 int nlinkingclasses = (int) classestolinking[i].size();
5147 SCIPmessageFPrintInfo(SCIPgetMessagehdlr(scip), file, "%s\n", usedpartition[i]->getName());
5148 SCIPmessageFPrintInfo(SCIPgetMessagehdlr(scip), file, "%d\n", nmasterclasses);
5149 for ( int mclass = 0; mclass < (int) classestomaster[i].size(); ++mclass )
5150 {
5151 int classid = classestomaster[i][mclass];
5152 SCIPmessageFPrintInfo(SCIPgetMessagehdlr(scip), file, "%s : %s\n", usedpartition[i]->getClassName(classid), usedpartition[i]->getClassDescription(classid));
5153 }
5154
5155 SCIPmessageFPrintInfo(SCIPgetMessagehdlr(scip), file, "%d\n", nlinkingclasses );
5156 for ( int linkingclass = 0; linkingclass < nlinkingclasses; ++linkingclass )
5157 {
5158 int classid = classestolinking[i][linkingclass];
5159 SCIPmessageFPrintInfo(SCIPgetMessagehdlr(scip), file, "%s : %s\n", usedpartition[i]->getClassName(classid), usedpartition[i]->getClassDescription(classid));
5160 }
5161
5162 }
5163 }
5164}
5165
5166
5168 )
5169{
5170 bool success = true;
5171
5172 while( success )
5173 success = assignHittingOpenconss() || assignHittingOpenvars();
5174 sort();
5175}
5176
5177
5179 )
5180{
5182 assignOpenPartialHittingToMaster();
5183}
5184
5185
5187 int detectorchainindex,
5188 ConsPartition* partition,
5189 std::vector<int>& consclassesmaster
5190 )
5191{
5192 assert( 0 <= detectorchainindex );
5193
5194 if( detectorchainindex >= (int) usedpartition.size() )
5195 {
5196 usedpartition.resize(detectorchainindex + 1);
5197 classestomaster.resize(detectorchainindex + 1);
5198 }
5199
5200 usedpartition[detectorchainindex] = partition;
5201 classestomaster[detectorchainindex] = consclassesmaster;
5202}
5203
5204
5206 int consToBlock,
5207 int block
5208 )
5209{
5210 assert( consToBlock >= 0 && consToBlock < nconss );
5211 assert( block >= 0 && block < nblocks );
5212 assert( (int) conssforblocks.size() > block );
5213
5214 conssforblocks[block].push_back( consToBlock );
5215 conssforblocksorted = false;
5216 hvoutdated = true;
5217}
5218
5219
5221 int cons,
5222 int block
5223 )
5224{
5225 assert( cons >= 0 && cons < nconss );
5226 assert( isconsopen[cons] );
5227
5228 if( block >= nblocks )
5229 setNBlocks(block+1);
5230 assert( block >= 0 && block < nblocks );
5231
5232 setConsToBlock(cons, block);
5233 deleteOpencons(cons);
5234}
5235
5237 SCIP_CONS* cons, /**< pointer of the constraint */
5238 int block /**< block index (counting from 0) */
5239 )
5240{
5241 int consindex = getDetprobdata()->getIndexForCons(cons);
5242
5243 if( consindex >= 0 )
5244 {
5245 fixConsToBlock(consindex, block);
5246 return true;
5247 }
5248 return false;
5249}
5250
5251
5253 int consToMaster
5254 )
5255{
5256 assert( consToMaster >= 0 && consToMaster < nconss );
5257 masterconss.push_back( consToMaster );
5258 isconsmaster[consToMaster] = true;
5259 masterconsssorted = false;
5260 hvoutdated = true;
5261}
5262
5263
5264std::vector<int>::const_iterator PARTIALDECOMP::fixConsToMaster(
5265 std::vector<int>::const_iterator itr
5266 )
5267{
5268 assert(itr != openconss.cend());
5269 assert(isconsopen[*itr]);
5270 setConsToMaster(*itr);
5271 return deleteOpencons(itr);
5272}
5273
5274
5276 int cons
5277 )
5278{
5279 assert( cons >= 0 && cons < nconss );
5280 assert(isconsopen[cons]);
5281
5282 setConsToMaster(cons);
5283 deleteOpencons(cons);
5284}
5285
5287 SCIP_CONS* cons /**< pointer of cons to fix as master cons */
5288 )
5289{
5290 int consindex = getDetprobdata()->getIndexForCons(cons);
5291 if( consindex >= 0 )
5292 {
5293 fixConsToMaster(consindex);
5294 return true;
5295 }
5296 return false;
5297}
5298
5299
5301 std::vector<GCG_DETECTOR*>& givenDetectorChain
5302 )
5303{
5304 detectorchain = givenDetectorChain;
5305}
5306
5307
5309 GCG_DETECTOR* detectorID
5310 )
5311{
5312 detectorchain.push_back(detectorID);
5313 addEmptyPartitionStatistics();
5314}
5315
5316
5318 GCG_DETECTOR* detectorID
5319 )
5320{
5321 isfinishedbyfinisher = true;
5322 detectorchain.push_back(detectorID);
5323 addEmptyPartitionStatistics();
5324}
5325
5326
5328 )
5329{
5330 isfinishedbyfinisherorig = true;
5331}
5332
5333
5335 bool finished
5336 )
5337{
5338 isfinishedbyfinisher = finished;
5339}
5340
5341
5343 bool finished
5344 )
5345{
5346 isfinishedbyfinisherorig = finished;
5347}
5348
5349
5351 int newNBlocks
5352 )
5353{
5354 assert(newNBlocks >= nblocks);
5355
5356 assert((int) conssforblocks.size() == nblocks);
5357 assert((int) varsforblocks.size() == nblocks);
5358 assert((int) stairlinkingvars.size() == nblocks);
5359 /* increase number of blocks in conssforblocks and varsforblocks */
5360
5361 conssforblocks.resize(newNBlocks);
5362 varsforblocks.resize(newNBlocks);
5363 stairlinkingvars.resize(newNBlocks);
5364
5365 if( !linkingvarsforblocks.empty() )
5366 linkingvarsforblocks.resize(newNBlocks);
5367
5368 if( !blockstructures.empty() )
5369 blockstructures.resize(newNBlocks, NULL);
5370
5371 if( !ncoeffsforblock.empty() )
5372 ncoeffsforblock.resize(newNBlocks, 0);
5373
5374 nblocks = newNBlocks;
5375}
5376
5377
5379 int block,
5380 BLOCK_STRUCTURE* blockstructure
5381 )
5382{
5383 if( !blockstructures.empty() || blockstructure != NULL )
5384 {
5385 if( block >= nblocks )
5386 setNBlocks(block+1);
5387 assert(block >= 0 && block < nblocks);
5388 if( blockstructures.empty() )
5389 blockstructures.resize(nblocks, NULL);
5390 blockstructures[block] = blockstructure;
5391 }
5392}
5393
5394
5396 bool selected
5397 )
5398{
5399 isselected = selected;
5400}
5401
5402
5404 bool fromorig
5405 )
5406{
5407 stemsfromorig = fromorig;
5408}
5409
5410
5412 USERGIVEN givenusergiven
5413 )
5414{
5415 usergiven = givenusergiven;
5416}
5417
5418
5420 int detectorchainindex,
5421 VarPartition* partition,
5422 std::vector<int>& varclasseslinking,
5423 std::vector<int>& varclassesmaster
5424 )
5425{
5426 assert( 0 <= detectorchainindex );
5427
5428 if( detectorchainindex >= (int) usedpartition.size() )
5429 {
5430 usedpartition.resize(detectorchainindex + 1);
5431 classestomaster.resize(detectorchainindex + 1);
5432 classestolinking.resize(detectorchainindex + 1);
5433 }
5434
5435
5436 usedpartition[detectorchainindex] = partition;
5437 classestolinking[detectorchainindex] = varclasseslinking;
5438 classestomaster[detectorchainindex] = varclassesmaster;
5439}
5440
5441
5443 int varToBlock,
5444 int block
5445 )
5446{
5447 assert(varToBlock >= 0 && varToBlock < nvars);
5448 assert(block >= 0 && block < nblocks);
5449 assert((int) varsforblocks.size() > block);
5450
5451 varsforblocks[block].push_back(varToBlock);
5452 varsforblocksorted = false;
5453 hvoutdated = true;
5454}
5455
5456
5458 int var,
5459 int block
5460 )
5461{
5462 assert(var >= 0 && var < nvars);
5463 assert(isvaropen[var]);
5464 assert(block >= 0 && block < nblocks);
5465
5466 if( isVarLinkingvar(var) )
5467 return;
5468
5469 setVarToBlock(var, block);
5471}
5472
5473
5474std::vector<int>::const_iterator PARTIALDECOMP::fixVarToBlock(
5475 std::vector<int>::const_iterator itr,
5476 int block
5477)
5478{
5479 assert(itr != openvars.cend());
5480 assert(isvaropen[*itr]);
5481 assert(block >= 0 && block < nblocks);
5482
5483 if( isVarLinkingvar(*itr) )
5484 return ++itr;
5485
5486 setVarToBlock(*itr, block);
5487 return deleteOpenvar(itr);
5488}
5489
5490
5492 int varToLinking
5493 )
5494{
5495 assert(varToLinking >= 0 && varToLinking < nvars);
5496 linkingvars.push_back(varToLinking);
5497 linkingvarssorted = false;
5498 hvoutdated = true;
5499}
5500
5501
5503 int var
5504 )
5505{
5506 assert(var >= 0 && var < nvars);
5507 assert(isvaropen[var]);
5508
5511
5512 // delete this var in blocks
5513 for( auto& block : varsforblocks )
5514 {
5515 for( auto iter = block.begin(); iter != block.end(); iter++ )
5516 {
5517 if( (*iter) == var )
5518 {
5519 block.erase(iter);
5520 assert(std::find(block.begin(), block.end(), var) == block.end());
5521 break;
5522 }
5523 }
5524 }
5525}
5526
5527
5528std::vector<int>::const_iterator PARTIALDECOMP::fixVarToLinking(
5529 std::vector<int>::const_iterator itr
5530 )
5531{
5532 assert(itr != openvars.cend());
5533 assert(isvaropen[*itr]);
5534
5535 setVarToLinking(*itr);
5536
5537 // delete this var in blocks
5538 for( auto& block : varsforblocks )
5539 {
5540 for( auto iter = block.begin(); iter != block.end(); iter++ )
5541 {
5542 if( (*iter) == *itr )
5543 {
5544 block.erase(iter);
5545 assert(std::find(block.begin(), block.end(), *itr) == block.end());
5546 break;
5547 }
5548 }
5549 }
5550 return deleteOpenvar(itr);;
5551}
5552
5553
5555 int varToMaster
5556 )
5557{
5558 assert(varToMaster >= 0 && varToMaster < nvars);
5559 mastervars.push_back(varToMaster);
5560 isvarmaster[varToMaster] = true;
5561 mastervarssorted = false;
5562 hvoutdated = true;
5563}
5564
5565
5567 int var
5568 )
5569{
5570 assert(var >= 0 && var < nvars);
5571 assert(isvaropen[var]);
5572
5575}
5576
5577
5578std::vector<int>::const_iterator PARTIALDECOMP::fixVarToMaster(
5579 std::vector<int>::const_iterator itr
5580 )
5581{
5582 assert(itr != openvars.cend());
5583 assert(isvaropen[*itr]);
5584
5585 setVarToMaster(*itr);
5586 return deleteOpenvar(itr);
5587}
5588
5589
5591 int varToStairlinking,
5592 int block1,
5593 int block2
5594 )
5595{
5596 assert(varToStairlinking >= 0 && varToStairlinking < nvars);
5597 assert(block1 >= 0 && block1 <= nblocks);
5598 assert(block2 >= 0 && block2 <= nblocks);
5599 assert((block1 + 1 == block2) || (block2 + 1 == block1));
5600
5601 if( block1 > block2 )
5602 stairlinkingvars[block2].push_back(varToStairlinking);
5603 else
5604 stairlinkingvars[block1].push_back(varToStairlinking);
5605
5606 stairlinkingvarsforblocksorted = false;
5607 hvoutdated = true;
5608}
5609
5610
5612 int var,
5613 int firstblock
5614 )
5615{
5616 assert(isvaropen[var]);
5617 assert(var >= 0 && var < nvars);
5618 assert(firstblock >= 0 && firstblock < (nblocks - 1));
5619
5620 setVarToStairlinking(var, firstblock, firstblock + 1);
5622}
5623
5624
5625std::vector<int>::const_iterator PARTIALDECOMP::fixVarToStairlinking(
5626 std::vector<int>::const_iterator itr,
5627 int firstblock
5628 )
5629{
5630 assert(isvaropen[*itr]);
5631 assert(itr != openvars.cend());
5632 assert(firstblock >= 0 && firstblock < (nblocks - 1));
5633
5634 setVarToStairlinking(*itr, firstblock, firstblock + 1);
5635 return openvars.erase(itr);
5636}
5637
5638
5640 const char* consname, /**< name of the constraint */
5641 int blockid /**< block index (counting from 0) */
5642 )
5643{
5644 int consindex = getDetprobdata()->getIndexForCons(consname);
5645
5646 if( consindex >= 0 )
5647 {
5648 fixConsToBlock(consindex, blockid);
5649 return true;
5650 }
5651 return false;
5652}
5653
5654
5656 const char* varname,
5657 int blockid
5658 )
5659{
5660 int varindex = getDetprobdata()->getIndexForVar(varname);
5661
5662 if( varindex >= 0 )
5663 {
5664 if( blockid >= nblocks )
5665 nblocks = blockid + 1;
5666 fixVarToBlock(varindex, blockid);
5667 return true;
5668 }
5669 return false;
5670}
5671
5672
5674 const char* consname /**< name of cons to fix as master cons */
5675 )
5676{
5677 int consindex = getDetprobdata()->getIndexForCons(consname);
5678 if( consindex >= 0 )
5679 {
5680 fixConsToMaster(consindex);
5681 return true;
5682 }
5683 return false;
5684}
5685
5686
5688 const char* varname
5689 )
5690{
5691 int varindex = getDetprobdata()->getIndexForVar(varname);
5692 if( varindex >= 0 )
5693 {
5694 fixVarToMaster(varindex);
5695 return true;
5696 }
5697 return false;
5698}
5699
5700
5702 const char* varname /**< name of the variable */
5703 )
5704{
5705 int varindex = getDetprobdata()->getIndexForVar(varname);
5706 if( varindex >= 0 )
5707 {
5708 fixVarToLinking(varindex);
5709 return true;
5710 }
5711 return false;
5712}
5713
5714
5716{
5717 int returnvalue;
5718
5719 /* get names for gp file and output file */
5720 char filename[SCIP_MAXSTRLEN];
5721 char outname[SCIP_MAXSTRLEN];
5722 GCGgetVisualizationFilename(gcg, this, ".gp", filename);
5723 GCGgetVisualizationFilename(gcg, this, ".pdf", outname);
5724
5725 this->generateVisualization(filename, outname);
5726
5727 /* open outputfile */
5728 char command[SCIP_MAXSTRLEN];
5729 /* command: e.g. evince "outname" && rm "filename" */
5730 strcpy(command, GCGVisuGetPdfReader(gcg));
5731 if( strcmp(GCGVisuGetPdfReader(gcg), "start") == 0 )
5732 strcat(command, " \"\"");
5733 strcat(command, " \"");
5734 strcat(command, outname);
5735 #if defined(_WIN32) || defined(_WIN64)
5736 strcat(command, "\" && del /f \"");
5737 #else
5738 strcat(command, "\" && rm \"");
5739 #endif
5740 strcat(command, filename);
5741 strcat(command, "\"");
5742#ifdef SCIP_DEBUG
5743 SCIPinfoMessage(scip, NULL, "%s\n", command);
5744#endif
5745 returnvalue = system(command);
5746 if( returnvalue == -1 )
5747 SCIPwarningMessage(scip, "Unable to open gnuplot file\n");
5748 SCIPinfoMessage(scip, NULL, "Please note that the generated pdf file was not deleted automatically! \n");
5749}
5750
5752 char* filename,
5753 char* outname,
5754 GP_OUTPUT_FORMAT outputformat
5755 )
5756{
5757 this->writeVisualizationFile(filename, outname, outputformat);
5758
5759 int returnvalue;
5760
5761 /* compile gp file */
5762 char command[SCIP_MAXSTRLEN];
5763 /* command: gnuplot "filename" */
5764 strcpy(command, "gnuplot \"");
5765 strcat(command, filename);
5766 strcat(command, "\"");
5767#ifdef SCIP_DEBUG
5768 SCIPinfoMessage(scip, NULL, "%s\n", command);
5769#endif
5770 returnvalue = system(command);
5771 if( returnvalue == -1 )
5772 {
5773 SCIPwarningMessage(scip, "Unable to write gnuplot file\n");
5774 return;
5775 }
5776}
5777
5779 char* filename,
5780 char* outname,
5781 GP_OUTPUT_FORMAT outputformat
5782 )
5783{
5784 /* generate gp file */
5785 GCGwriteGpVisualizationFormat(gcg, filename, outname, getID(), outputformat);
5786}
5787
5788
5790{
5791 /* get names for gp file and output file */
5792 char filename[SCIP_MAXSTRLEN];
5793 char outname[SCIP_MAXSTRLEN];
5794 GCGgetVisualizationFilename(gcg, this, ".gp", filename);
5795 GCGgetVisualizationFilename(gcg, this, ".pdf", outname);
5796
5797 /* generate gp file */
5798 GCGwriteGpVisualization(gcg, filename, outname, getID());
5799}
5800
5805
5806
5808{
5809 if( varsforblocksorted && stairlinkingvarsforblocksorted && conssforblocksorted && linkingvarssorted
5810 && mastervarssorted && masterconsssorted )
5811 {
5812 return false;
5813 }
5814
5815 for( int b = 0; b < nblocks; ++ b )
5816 {
5817 if( !varsforblocksorted )
5818 std::sort(varsforblocks[b].begin(), varsforblocks[b].end());
5819 if( !stairlinkingvarsforblocksorted )
5820 std::sort(stairlinkingvars[b].begin(), stairlinkingvars[b].end());
5821 if( !conssforblocksorted )
5822 std::sort(conssforblocks[b].begin(), conssforblocks[b].end() );
5823 }
5824 if( !linkingvarssorted )
5825 std::sort(linkingvars.begin(), linkingvars.end());
5826 if( !mastervarssorted )
5827 std::sort(mastervars.begin(), mastervars.end());
5828 if( !masterconsssorted )
5829 std::sort(masterconss.begin(), masterconss.end());
5830
5831 varsforblocksorted = true;
5832 stairlinkingvarsforblocksorted = true;
5833 conssforblocksorted = true;
5834 linkingvarssorted = true;
5835 mastervarssorted = true;
5836 masterconsssorted = true;
5837
5838 return true;
5839}
5840
5841
5843 char* buffer
5844 )
5845{
5846 /* set detector chain info string */
5847 SCIPsnprintf(buffer, SCIP_MAXSTRLEN, "");
5848 if( this->usergiven == USERGIVEN::PARTIAL || this->usergiven == USERGIVEN::COMPLETE
5849 || this->usergiven == USERGIVEN::COMPLETED_CONSTOMASTER || this->getDetectorchain().empty() )
5850 {
5851 (void) strncat(buffer, "U", SCIP_MAXSTRLEN-2);
5852 }
5853
5854 for( int d = 0; d < this->getNDetectors(); ++ d )
5855 {
5856 if( d == 0 && this->getDetectorchain()[d] == NULL )
5857 continue;
5858 char str[2] = "\0"; /* gives {\0, \0} */
5859 str[0] = GCGdetectorGetChar(this->getDetectorchain()[d]);
5860 (void) strncat(buffer, str, SCIP_MAXSTRLEN-strlen(buffer)-1);
5861 }
5862}
5863
5864
5866 )
5867{
5868 char buffer[SCIP_MAXSTRLEN];
5869 buildDecChainString(buffer);
5870 return std::string(buffer);
5871}
5872
5873
5875{
5876 GCG_SCORE* score = GCGconshdlrDecompFindScore(gcg, "max white");
5877 assert(score != NULL);
5878 return getScore(score);
5879}
5880
5881
5883{
5885 deleteEmptyBlocks(true);
5886
5887 if( usergiven != USERGIVEN::NOT && isComplete() )
5888 {
5891 }
5892
5893 calcHashvalue();
5894}
5895
5896
5897std::vector<std::vector<int>>& PARTIALDECOMP::getConssForBlocks()
5898{
5899 return conssforblocks;
5900}
5901
5903{
5904 return getNBlocks() == 0 || getNEquivalenceClasses() > 0;
5905}
5906
5908{
5909 return translatedpartialdecid;
5910}
5911
5913 int decid
5914 )
5915{
5916 PARTIALDECOMP::translatedpartialdecid = decid;
5917}
5918
5920 int masterconsindex,
5921 int block
5922 )
5923{
5924 if( ncoeffsforblockformastercons.empty() )
5925 calcNCoeffsForBlockForMastercons();
5926 return ncoeffsforblockformastercons[block][masterconsindex];
5927}
5928
5930 std::function<int(int)> blockmapping,
5931 std::function<int(int, int)> varmapping
5932 )
5933{
5934 bool success = true;
5935 if( !isComplete() )
5936 return false;
5937 blockstoeqclasses.resize(getNBlocks(), -1);
5938 for( int b = 0; b < getNBlocks(); ++b )
5939 {
5940 int rb = blockmapping(b);
5941 if( !getLinkingvarsForBlock(b).empty() )
5942 rb = b;
5943 if( rb == b )
5944 {
5945 // new equivalence class
5946 blockstoeqclasses[b] = nequivalenceclasses;
5947 assert((int)eqclasstoblocks.size() == nequivalenceclasses);
5948 eqclasstoblocks.emplace_back();
5949 eqclasstoblocks[nequivalenceclasses].push_back(b);
5950 eqclassesvarmappings.emplace_back();
5951 eqclassesvarmappings[nequivalenceclasses].emplace_back();
5952 assert(eqclassesvarmappings[nequivalenceclasses].size() == 1);
5953
5954 for( int i = 0; i < getNVarsForBlock(b); ++i )
5955 {
5956 eqclassesvarmappings[nequivalenceclasses][0].push_back(i);
5957 }
5958 ++nequivalenceclasses;
5959 SCIPdebugMessage("Block %d is a representative block.\n", b);
5960 }
5961 else if( rb < b && rb >= 0 )
5962 {
5963 // existing equivalence class
5964 int eqclass = blockstoeqclasses[rb];
5965 if( eqclasstoblocks[eqclass][0] != rb )
5966 {
5967 // representative blocks are not consistent
5968 success = false;
5969 break;
5970 }
5971 int blockindex = (int)eqclasstoblocks[eqclass].size();
5972 blockstoeqclasses[b] = eqclass;
5973 eqclasstoblocks[eqclass].push_back(b);
5974 eqclassesvarmappings[eqclass].emplace_back();
5975 SCIPdebugMessage("Block %d is represented by block %d.\n", b, rb);
5976
5977 for( int i = 0; i < getNVarsForBlock(b); ++i )
5978 {
5979 assert(varmapping(b, i) >= 0 && varmapping(b, i) < getNVarsForBlock(rb));
5980 eqclassesvarmappings[eqclass][blockindex].push_back(varmapping(b, i));
5981 SCIPdebugMessage("Var <%s> is mapped to <%s>.\n",
5982 SCIPvarGetName(getDetprobdata()->getVar(varsforblocks[b][i])),
5983 SCIPvarGetName(getDetprobdata()->getVar(varsforblocks[rb][varmapping(b, i)])));
5984 }
5985 }
5986 else
5987 {
5988 success = false;
5989 break;
5990 }
5991 }
5992
5993 if( !success )
5994 nequivalenceclasses = 0;
5995
5996 return success;
5997}
5998
5999} /* namespace gcg */
SCIP_RETCODE cmpGraphPair(SCIP *scip, gcg::PARTIALDECOMP *partialdec, int block1, int block2, SCIP_RESULT *result, SCIP_HASHMAP *varmap, SCIP_HASHMAP *consmap, unsigned int searchnodelimit, unsigned int generatorlimit)
automorphism recognition of SCIPs
automorphism recognition (C++ interface)
SCIP_VAR * w
SCIP_VAR * a
SCIP_VAR ** b
#define SCIP_CALL_EXC(x)
class storing partialdecs and the problem matrix
class storing (potentially incomplete) decompositions
GCG_EXPORT int getClassOfCons(int consindex)
GCG_EXPORT std::vector< int > & getConssForVar(int varIndex)
returns the constraint indices of the coefficient matrix for a variable
GCG_EXPORT int getIndexForCons(SCIP_CONS *cons)
returns the constraint index related to a SCIP constraint
GCG_EXPORT int getNConss()
returns the number of variables considered in the detprobdata
GCG_EXPORT SCIP * getScip()
returns the corresponding scip data structure
GCG_EXPORT int getNVarsForCons(int consIndex)
returns the number of variables for a given constraint
GCG_EXPORT bool isConsCardinalityCons(int consindexd)
returns whether a constraint is a cardinality constraint, i.e. of the
GCG_EXPORT int getNVars()
return the number of variables considered in the detprobdata
GCG_EXPORT bool isConsSetppc(int consindexd)
is cons with specified index partitioning packing, or covering constraint?
GCG_EXPORT int getNConssForVar(int varIndex)
returns the number of constraints for a given variable where the var has a nonzero entry in
GCG_EXPORT std::vector< int > & getConssForCons(int consIndex)
return array of constraint indices that have a common variable with the given constraint
GCG_EXPORT SCIP_VAR * getVar(int varIndex)
returns SCIP variable related to a variable index
GCG_EXPORT GCG * getGcg()
returns the corresponding gcg data structure
GCG_EXPORT void createConssAdjacency()
create the constraint adjacency datastructure that is used (if created) for some methods to faster ac...
GCG_EXPORT SCIP_Bool isConssAdjInitialized()
determines whether or not the constraint-constraint adjacency data structure is initilized
GCG_EXPORT SCIP_Real getVal(int row, int col)
returns a coefficient from the coefficient matrix
GCG_EXPORT int getIndexForVar(SCIP_VAR *var)
returns the variable index related to a SCIP variable
GCG_EXPORT bool isConsSetpp(int consindexd)
is cons with specified indec partitioning, or packing covering constraint?
GCG_EXPORT std::vector< int > & getVarsForCons(int consIndex)
returns the variable indices of the coefficient matrix for a constraint
GCG_EXPORT SCIP_CONS * getCons(int consIndex)
returns the SCIP constraint related to a constraint index
GCG_EXPORT SCIP_Bool isAssignedToOrigProb()
returns true if the matrix structure corresponds to the presolved problem
virtual double getEdgeWeight(int node_i, int node_j)
virtual int getNNeighbors(int node)
virtual std::vector< int > getNeighbors(int node)
virtual SCIP_RETCODE setEdge(int node_i, int node_j, double weight)
virtual SCIP_Bool isEdge(int node_i, int node_j)
GCG_EXPORT const char * getClassName(int classindex)
GCG_EXPORT const char * getName()
GCG_EXPORT const char * getClassDescription(int classindex)
class to manage partial decompositions
GCG_EXPORT std::vector< SCIP_Real > & getPctVarsToBorderVector()
Gets fraction of variables assigned to the border for detectors in detectorchain.
GCG_EXPORT SCIP_Real getDetectorClockTime(int detectorchainindex)
returns the time that the detector related to the given detectorchainindex needed for detecting
GCG_EXPORT int getNConss()
Gets the number of constraints.
GCG_EXPORT int getNMasterconss()
Gets size of the vector containing master conss.
GCG_EXPORT bool fixVarToMasterByName(const char *varname)
assigns a variable with given name as master
GCG_EXPORT SCIP_Bool shouldCompletedByConsToMaster()
Checks whether this partialdec is a userpartialdec that should be completed.
GCG_EXPORT SCIP_Bool hasSetppcMaster()
checks iff all master constraints set partitioning, set packing, or set cover constraints
GCG_EXPORT bool isVarStairlinkingvarOfBlock(int var, int block)
Checks whether the var is a stairlinkingvar of a specified block.
GCG_EXPORT bool fixConsToMasterByName(const char *consname)
assgins a constraint by name as master
GCG_EXPORT std::vector< GCG_DETECTOR * > & getDetectorchain()
returns detector chain as vector of detector pointers
GCG_EXPORT void fixVarToStairlinking(int var, int firstblock)
adds a variable to the stairlinking variables
GCG_EXPORT int getNEquivalenceClasses()
Gets the number of blockrepresentatives.
GCG_EXPORT void assignSmallestComponentsButOneConssAdjacency()
computes components by connectedness of conss and vars
GCG_EXPORT bool isConsMastercons(int cons)
Gets whether the cons is a master cons.
GCG_EXPORT SCIP_RETCODE filloutBorderFromConstoblock(SCIP_HASHMAP *constoblock, int givenNBlocks)
every constraint is either assigned to master or open
GCG_EXPORT std::vector< int > & getStairlinkingvars(int block)
Gets vector containing stairlinking vars,.
GCG_EXPORT void setVarToBlock(int varToBlock, int block)
adds a variable to the linking variables, does not delete this var from list of open vars
GCG_EXPORT void setAncestorList(std::vector< int > &newlist)
GCG_EXPORT SCIP_Real getScore(GCG_SCORE *score)
returns the score of the partialdec
GCG_EXPORT void completeByConnectedConssAdjacency()
assigns all open constraints and open variables
GCG_EXPORT void setTranslatedpartialdecid(int decid)
GCG_EXPORT void setPctVarsToBlockVector(std::vector< SCIP_Real > &newvector)
set statistical vector of fractions of variables assigned to a block per involved detector
GCG_EXPORT void refineToMaster()
refine partialdec with focus on master
GCG_EXPORT int getAncestorID(int ancestorindex)
gets partialdec id of given ancestor id
GCG_EXPORT void setVarToStairlinking(int varToStairLinking, int block1, int block2)
adds a variable to the stairlinking variables, does not delete this var from list of open vars
GCG_EXPORT SCIP_Real getPctConssToBorder(int detectorchainindex)
Gets fraction of constraints assigned to the border for a detector.
GCG_EXPORT void setVarToLinking(int varToLinking)
adds a variable to the linking variables, does not delete this var from list of open vars
GCG_EXPORT bool checkConsistency()
Checks whether the assignments in the partialdec are consistent.
GCG_EXPORT void setSelected(bool selected)
set the selection status of this partialdecs
GCG_EXPORT void setPctVarsToBorderVector(std::vector< SCIP_Real > &newvector)
set statistical vector of fraction of variables assigned to the border per involved detector
GCG_EXPORT void addAncestorID(int ancestor)
GCG_EXPORT SCIP_Real getPctVarsToBorder(int detectorchainindex)
Gets fraction of variables assigned to the border for a detector.
GCG_EXPORT int getNStairlinkingvars(int block)
Gets size of the vector containing stairlinking vars.
GCG_EXPORT void exportVisualization()
generates a gp visualization of the partialdec without compilation or opening
GCG_EXPORT bool fixConsToBlockByName(const char *consname, int blockid)
assigns a constraint by name to a block
GCG_EXPORT std::vector< SCIP_Real > & getPctConssToBlockVector()
Gets fraction of constraints assigned to a block for detectors in detectorchain.
GCG_EXPORT USERGIVEN getUsergiven()
Gets the USERGIVEN status of this partialdecs.
GCG_EXPORT void setNBlocks(int nblocks)
sets number of blocks, only increasing number allowed
GCG_EXPORT std::vector< SCIP_Real > & getDetectorClockTimes()
returns a vector of the clock times that each detector needed that was involved in this partialdec
GCG_EXPORT bool isAssignedToPresolvedProb()
Gets whether the partialdec is from the presolved problem.
GCG_EXPORT bool fixVarToLinkingByName(const char *varname)
assigns a variable by name to the linking variables
std::vector< BLOCK_STRUCTURE * > & getBlockStructures()
Returns the structure information of all blocks.
GCG_EXPORT void fixConsToBlock(int cons, int block)
adds a constraint to a block
GCG_EXPORT int getNOpenvars()
Gets size of vector containing variables not assigned yet.
GCG_EXPORT void displayInfo(int detailLevel)
displays the relevant information of the partialdec
GCG_EXPORT bool isVarLinkingvar(int var)
Checks whether the var is a linking var.
GCG_EXPORT int getNCoeffsForBlock(int blockid)
Gets the number of nonzero coeffs in a certain block.
GCG_EXPORT bool fixVarToBlockByName(const char *varname, int blockid)
assigns a variable by name to a block
GCG_EXPORT void removeAncestorID(int ancestorid)
GCG_EXPORT int getVarProbindexForBlock(int varid, int block)
Gets the index of a variable in the vector returned by PARTIALDECOMP::getVarsForBlock.
GCG_EXPORT bool setSymmetryInformation(std::function< int(int)> blockmapping, std::function< int(int, int)> varmapping)
sets symmetry information
GCG_EXPORT unsigned long getHashValue()
returns the calculated hash value of this partialdec
GCG_EXPORT std::vector< int > & getMasterconss()
Gets array containing all master conss indices.
GCG_EXPORT PARTIALDECOMP(GCG *gcgstruct, bool originalProblem)
Standard constructor, creates empty partialdec with unique id.
GCG_EXPORT bool checkAllConssAssigned()
checks if all conss are assigned
GCG_EXPORT void addPctVarsToBorder(SCIP_Real pct)
adds percentage of variables assigned to border
GCG_EXPORT int getNDetectors()
Gets the number of detectors the partialdec is propagated by.
GCG_EXPORT void completeGreedily()
assigns all open constraints and open variables
GCG_EXPORT std::string buildDecChainString()
creates a detector chain short string for this partialdec, is built from detector chain
GCG_EXPORT bool alreadyAssignedConssToBlocks()
method to check if at least one constraint is assigned to some block
GCG_EXPORT std::vector< int > & getConssForBlock(int block)
returns array containing constraints assigned to a block
GCG_EXPORT const std::vector< int > & getBlocksForEqClass(int eqclass)
get a vector of block ids that are contained in the equivalence class eqclass
GCG_EXPORT void setVarToMaster(int varToMaster)
adds a variable to the master variables, does not delete this var from list of open vars
GCG_EXPORT void buildDecChainString(char *buffer)
creates a detector chain short string for this partialdec, is built from detector chain
GCG_EXPORT void addDetectorChainInfo(const char *decinfo)
add information about the detector chain
GCG_EXPORT void removeMastercons(int consid)
removes the given cons from master
void setBlockStructure(int block, BLOCK_STRUCTURE *blockstructure)
sets structure information of a specific block
GCG_EXPORT DETPROBDATA * getDetprobdata()
Gets the corresponding detprobdata.
GCG_EXPORT bool isComplete()
Gets whether this partialdec is complete, i.e. it has no more open constraints and variables.
GCG_EXPORT void setFinishedByFinisher(bool finished)
sets whether this partialdec was finished by a finishing detector
GCG_EXPORT void setPctConssFromFreeVector(std::vector< SCIP_Real > &newvector)
set statistical vector of fractions of constraints that are not longer open per involved detector
GCG_EXPORT void addPctVarsFromFree(SCIP_Real pct)
adds percentage of closed variables
int getNVarsOfBlockInMasterCons(int masterconsindex, int block)
returns the number of block vars contained in a master constraint
GCG_EXPORT SCIP_Real getPctVarsToBlock(int detectorchainindex)
Gets fraction of variables assigned to a block for a detector.
GCG_EXPORT bool isVarMastervar(int var)
Checks whether the var is a master var.
GCG_EXPORT int getNOpenconss()
Gets size of vector containing constraints not assigned yet.
GCG_EXPORT void calcAggregationInformation(bool ignoreDetectionLimits)
computes if aggregation of sub problems is possible
bool isNested()
Checks whether this is a nested decomposition.
GCG_EXPORT int getNVarsForBlock(int block)
Gets size of the vector containing vars assigned to a block.
GCG_EXPORT bool isVarOpenvar(int var)
Checks whether the var is an open var.
GCG_EXPORT void setDetectorFinished(GCG_DETECTOR *detector)
sets detector that finished the partialdec
GCG_EXPORT void fixVarToLinking(int var)
adds a variable to the linking variables
GCG_EXPORT bool aggInfoCalculated()
Checks if the aggregation information was already calculated.
GCG_EXPORT std::vector< int > & getLinkingvars()
returns array containing all linking vars indices
SCIP_Real calcBlockAreaScore()
gets an intermediate score value for the blocks of a partialdec
GCG_EXPORT void fixVarToMaster(int var)
adds a variable to the master variables
GCG_EXPORT void addPctConssToBorder(SCIP_Real pct)
adds percentage of constraints assigned to border
GCG_EXPORT void assignOpenConssToMaster()
assigns open conss to master
GCG_EXPORT int getEqClassForBlock(int blockid)
Gets index of the representative block for a block, this might be blockid itself.
GCG_EXPORT SCIP_Bool hasSetppccardMaster()
checks if all master constraints set partitioning, set packing, set cover, or cardinality constraints
GCG_EXPORT bool isAssignedToOrigProb()
Gets whether the partialdec is from the original problem.
GCG_EXPORT int getNVars()
Gets number of vars.
GCG_EXPORT void setDetectorPropagated(GCG_DETECTOR *detector)
sets partialdec to be propagated by a detector
GCG_EXPORT SCIP_Real getScore()
returns the score of the partialdec (depending on enabled score)
GCG_EXPORT void addPctConssToBlock(SCIP_Real pct)
adds percentage of constraints assigned to blocks
GCG_EXPORT void completeByConnected()
assigns all open constraints and open variables
GCG_EXPORT SCIP_RETCODE isEqual(PARTIALDECOMP *otherpartialdec, SCIP_Bool *isequal, bool sortpartialdecs)
method to check whether this partialdec is equal to a given other partialdec (
GCG_EXPORT std::vector< int > & getMastervars()
Gets array containing all master vars indices.
GCG_EXPORT void setStemsFromOrig(bool fromorig)
sets whether this partialdec stems from an orig problem partialdec
GCG_EXPORT void setUsergiven(USERGIVEN usergiven)
sets whether this partialdec is user given
GCG_EXPORT std::vector< SCIP_Real > & getPctConssToBorderVector()
Gets fraction of constraints assigned to the border for detectors in detectorchain.
GCG_EXPORT SCIP_RETCODE assignPartialdecFromConstoblock(SCIP_HASHMAP *constoblock, int additionalNBlocks)
assigns conss structure according to given hashmap
GCG_EXPORT void setDetectorFinishedOrig()
sets detector that finished the partialdec in the original problem
GCG_EXPORT SCIP_Real getPctConssToBlock(int detectorchainindex)
Gets fraction of constraints assigned to a block for a detector.
GCG_EXPORT int getNLinkingvars()
Gets size of the vector containing linking vars.
GCG_EXPORT std::vector< SCIP_Real > & getPctVarsFromFreeVector()
Gets fraction of variables that are not longer open for detectors in detectorchain.
GCG_EXPORT void deleteEmptyBlocks(bool variables)
deletes empty blocks and sets nblocks accordingly
GCG_EXPORT void refineToBlocks()
refine partialdec with focus on blocks
GCG_EXPORT SCIP_Real getMaxWhiteScore()
gets the maximum white area score
GCG_EXPORT SCIP_Real getPctVarsFromFree(int detectorchainindex)
Gets fraction of variables that are not longer open for a detector.
GCG_EXPORT bool isSelected()
GCG_EXPORT void setVarPartitionStatistics(int detectorchainindex, VarPartition *partition, std::vector< int > &varclasseslinking, std::vector< int > &varclassesmaster)
registers statistics for a used varpartition
GCG_EXPORT void setConsToBlock(int consToBlock, int block)
adds a constraint to a block, does not delete this cons from list of open conss
GCG_EXPORT std::vector< int > & getRepVarmap(int eqclass, int eqclassblock)
Returns a vector that maps probvar indices of a block contained in an equivalence class to the probva...
GCG_EXPORT int addBlock()
adds a block
GCG_EXPORT void setDetectorClockTimes(std::vector< SCIP_Real > &newvector)
set statistical vector of the times that the detectors needed for detecting per involved detector
void setScore(GCG_SCORE *score, SCIP_Real scorevalue)
sets the scorevalue of score
GCG_EXPORT void deleteOpencons(int opencons)
deletes a cons from list of open conss
GCG_EXPORT SCIP_RETCODE assignPartialdecFromConstoblockVector(std::vector< int > constoblock, int additionalNBlocks)
assigns conss structure according to given vector
GCG_EXPORT SCIP_RETCODE filloutPartialdecFromConstoblock(SCIP_HASHMAP *constoblock, int givenNBlocks)
assigns all conss to master or a block
GCG_EXPORT void generateVisualization(char *filename, char *outname, GP_OUTPUT_FORMAT outputformat=GP_OUTPUT_FORMAT_PDF)
generates a visualization of the partialdec using gnuplot
GCG_EXPORT void setDetectorchain(std::vector< GCG_DETECTOR * > &givenDetectorChain)
sets the detectorchain with the given vector of detector pointers
GCG_EXPORT void addNNewBlocks(int nnewblocks)
adds how many new blocks were introduced
GCG_EXPORT void findVarsLinkingToMaster()
reassigns linking variables to master if appropriate
GCG_EXPORT bool isVarLinkingvarOfBlock(int var, int block)
Checks whether the var is a linking var contained by the block.
GCG_EXPORT int getNAncestors()
Gets number of ancestor partialdecs.
GCG_EXPORT SCIP_RETCODE assignBorderFromConstoblock(SCIP_HASHMAP *constoblock, int givenNBlocks)
assigns open conss to master
GCG_EXPORT std::vector< SCIP_Real > & getPctConssFromFreeVector()
Gets fraction of constraints that are not longer open for detectors in detectorchain.
GCG_EXPORT bool getFinishedByFinisher()
returns true iff this partialdec was finished by finishPartialdec() method of a detector
GCG_EXPORT int getReprBlockForEqClass(int eqclass)
get the representative block of the given equivalence class
GCG_EXPORT void considerImplicits()
: assigns every open cons/var
GCG_EXPORT int getNNewBlocks(int detectorchainindex)
Gets number of blocks a detector added.
GCG_EXPORT std::vector< std::vector< int > > & getConssForBlocks()
gets vector of indices of all constraints assigned to blocks
GCG_EXPORT int getNCoeffsForMaster()
GCG_EXPORT int getNTotalStairlinkingvars()
Gets total number of stairlinking vars.
GCG_EXPORT int getNMastervars()
Gets size of the vector containing master vars.
GCG_EXPORT int getNConssForBlock(int block)
Gets size of the vector containing conss assigned to a block.
GCG_EXPORT void showVisualization()
generates and opens a gp visualization of the partialdec
GCG_EXPORT void addDecChangesFromAncestor(PARTIALDECOMP *ancestor)
adds the statistical differences to an ancestor
GCG_EXPORT void addPctConssFromFree(SCIP_Real pct)
adds percentage of closed constraints
GCG_EXPORT void setConsPartitionStatistics(int detectorchainindex, ConsPartition *partition, std::vector< int > &consclassesmaster)
registers statistics for a used conspartition
GCG_EXPORT void setFinishedByFinisherOrig(bool finished)
sets whether this partialdec was finished by a finishing detector in the original problem
GCG_EXPORT bool isVarBlockvarOfBlock(int var, int block)
Checks whether the var is assigned to the block.
GCG_EXPORT int getID()
returns the unique id of the partialdec
GCG_EXPORT void addClockTime(SCIP_Real clocktime)
adds detection time of one detector
GCG_EXPORT int getTranslatedpartialdecid() const
GCG_EXPORT SCIP_Real getPctConssFromFree(int detectorchainindex)
Gets fraction of constraints that are not longer open for a detector.
GCG_EXPORT void setPctVarsFromFreeVector(std::vector< SCIP_Real > &newvector)
set statistical vector of variables that are not longer open per involved detector
GCG_EXPORT SCIP_Bool hasSetppMaster()
checks iff all master constraints set partitioning, or set packing constraints
GCG_EXPORT std::vector< int > & getOpenconss()
Gets vector containing constraints not assigned yet.
GCG_EXPORT std::vector< std::string > & getDetectorchainInfo()
Gets the detectorchain info vector.
GCG_EXPORT void writeVisualizationFile(char *filename, char *outname, GP_OUTPUT_FORMAT outputformat=GP_OUTPUT_FORMAT_PDF)
writes a gp visualization of the partialdec to a file
GCG_EXPORT void copyPartitionStatistics(const PARTIALDECOMP *otherpartialdec)
copies the given partialdec's partition statistics
GCG_EXPORT bool isConsOpencons(int cons)
Gets whether the cons is an open cons.
GCG_EXPORT void findVarsLinkingToStairlinking()
reassigns variables classified as linking to stairlinking if appropriate
GCG_EXPORT int getNVarsForBlocks()
Gets overall number of vars assigned to a block.
GCG_EXPORT void reorderBlocksForStairlinkingVars()
reorders blocks for making a maximum number of linking vars stairlinking
GCG_EXPORT void printPartitionInformation(FILE *file)
prints partition information as described in
GCG_EXPORT std::vector< SCIP_Real > & getPctVarsToBlockVector()
returns fraction of variables assigned to a block for detectors in detectorchain
GCG_EXPORT std::vector< int >::const_iterator fixConsToMaster(std::vector< int >::const_iterator itr)
fixes a constraint to the master constraints
GCG_EXPORT void deleteOpenvar(int openvar)
deletes a var from the list of open vars
GCG_EXPORT bool isTrivial()
Gets whether this partialdec is considered to be trivial.
GCG_EXPORT void setPctConssToBlockVector(std::vector< SCIP_Real > &newvector)
set statistical vector of fractions of constraints set to blocks per involved detector
GCG_EXPORT bool sort()
sorts the vars and conss data structures by their indices
GCG_EXPORT bool isVarStairlinkingvar(int var)
Checks whether the var is a stairlinking var.
BLOCK_STRUCTURE * getBlockStructure(int block)
Returns the structure information of a specific block.
GCG_EXPORT std::vector< int > getNNewBlocksVector()
gets number of blocks the detectors in the detectorchain added
GCG_EXPORT std::vector< int > & getOpenvars()
Gets vector containing variables not assigned yet.
GCG_EXPORT std::vector< int > & getVarsForBlock(int block)
Gets array containing vars of a block.
GCG_EXPORT void addPctVarsToBlock(SCIP_Real pct)
adds percentage of variables assigned to blocks
GCG_EXPORT void complete()
assigns all open constraints and open variables trivially
GCG_EXPORT bool isPropagatedBy(GCG_DETECTOR *detector)
Gets whether this partialdec was propagated by specified detector.
GCG_EXPORT void setPctConssToBorderVector(std::vector< SCIP_Real > &newvector)
set statistical vector of fractions of constraints assigned to the border per involved detector
GCG_EXPORT bool assignCurrentStairlinking()
assigns open vars to stairlinking if appropriate
GCG_EXPORT int getNBlocks()
Gets the number of blocks.
GCG_EXPORT void fixVarToBlock(int var, int block)
adds a variable to the linking variables
GCG_EXPORT void setConsToMaster(int consToMaster)
adds a constraint to the master constraints, does not delete this cons from list of open conss
GCG_EXPORT std::vector< int > & getAncestorList()
get ancestor ids as vector
GCG_EXPORT std::vector< int > & getLinkingvarsForBlock(int block)
returns vector of all linking var indices contained by the block
GCG_EXPORT int getClassOfVar(int varindex)
GCG_EXPORT int getNVars()
const char * GCGdetectorGetName(GCG_DETECTOR *detector)
returns the name of the provided detector
GCG_SCORE ** GCGconshdlrDecompGetScores(GCG *gcg)
Gets an array of all scores.
int GCGconshdlrDecompGetNextPartialdecID(GCG *gcg)
Gets the next partialdec id managed by cons_decomp.
void GCGconshdlrDecompRegisterPartialdec(GCG *gcg, PARTIALDECOMP *partialdec)
registers a partialdec in the conshdlr
void GCGconshdlrDecompDeregisterPartialdec(GCG *gcg, PARTIALDECOMP *partialdec)
deregisters a partialdec in the conshdlr
GCG_SCORE * GCGgetCurrentScore(GCG *gcg)
Gets the currently enabled score.
DETPROBDATA * GCGconshdlrDecompGetDetprobdataPresolved(GCG *gcg)
help method to access detprobdata for transformed problem
DETPROBDATA * GCGconshdlrDecompGetDetprobdataOrig(GCG *gcg)
help method to access detprobdata for unpresolved problem
char GCGdetectorGetChar(GCG_DETECTOR *detector)
Gets the character of the detector.
int GCGconshdlrDecompGetNScores(GCG *gcg)
Gets the number of all scores.
SCIP_CLOCK * GCGconshdlrDecompGetScoreClock(GCG *gcg)
gets the clock tracking the score computation time
SCIP_RETCODE GCGconshdlrDecompAddPreexisitingPartialDec(GCG *gcg, PARTIALDECOMP *partialdec)
adds a preexisting partial dec to be considered at the beginning of the detection
GCG_SCORE * GCGconshdlrDecompFindScore(GCG *gcg, const char *name)
searches for the score with the given name and returns it or NULL if score is not found
constraint handler for structure detection
C++ interface of cons_decomp.
#define SCIP_MAXSTRLEN
#define SCIP_INVALID
#define SCIP_Bool
#define SCIP_Real
#define SCIP_CALL_ABORT(x)
#define TRUE
#define FALSE
SCIP * GCGgetOrigprob(GCG *gcg)
Definition gcg.cpp:87
void SCIPhashmapFree(SCIP_HASHMAP **hashmap)
SCIP_Real SCIPhashmapGetImageReal(SCIP_HASHMAP *hashmap, void *origin)
void * SCIPhashmapGetImage(SCIP_HASHMAP *hashmap, void *origin)
SCIP_RETCODE SCIPhashmapSetImageReal(SCIP_HASHMAP *hashmap, void *origin, SCIP_Real image)
SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP **hashmap, BMS_BLKMEM *blkmem, int mapsize)
SCIP_Bool SCIPhashmapExists(SCIP_HASHMAP *hashmap, void *origin)
SCIP_Real GCGconsGetRhs(SCIP *scip, SCIP_CONS *cons)
Definition scip_misc.c:109
consType GCGconsGetType(SCIP *scip, SCIP_CONS *cons)
Definition scip_misc.c:52
SCIP_Real GCGconsGetLhs(SCIP *scip, SCIP_CONS *cons)
Definition scip_misc.c:207
SCIP_RETCODE GCGconsGetVals(SCIP *scip, SCIP_CONS *cons, SCIP_Real *vals, int nvals)
Definition scip_misc.c:618
int GCGconsGetNVars(SCIP *scip, SCIP_CONS *cons)
Definition scip_misc.c:433
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
SCIP_MESSAGEHDLR * SCIPgetMessagehdlr(SCIP *scip)
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
SCIP_RETCODE SCIPgetBoolParam(SCIP *scip, const char *name, SCIP_Bool *value)
SCIP_RETCODE SCIPgetIntParam(SCIP *scip, const char *name, int *value)
const char * SCIPconsGetName(SCIP_CONS *cons)
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
#define SCIPallocBufferArray(scip, ptr, num)
GCG_EXPORT const char * GCGscoreGetName(GCG_SCORE *score)
Definition score.c:145
SCIP_RETCODE SCIPstopClock(SCIP *scip, SCIP_CLOCK *clck)
SCIP_RETCODE SCIPstartClock(SCIP *scip, SCIP_CLOCK *clck)
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
const char * SCIPvarGetName(SCIP_VAR *var)
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
int SCIPsnprintf(char *t, int len, const char *s,...)
static assert(heur !=NULL)
* result
SCIP_VAR * var
int j
SCIP_Bool success
SCIPdebugMessage("feasibility pump finished [%d iterations done].\n", nloops)
int i
SCIPfreeBufferArray(scip, &cycles)
#define NULL
void SCIPmessageFPrintInfo(SCIP_MESSAGEHDLR *messagehdlr, FILE *file, const char *formatstr,...)
void GCGgetVisualizationFilename(GCG *gcg, PARTIALDECOMP *partialdec, const char *extension, char *filename)
miscellaneous methods for visualizations
static SCIP_Bool realArraysAreEqual(SCIP *scip, SCIP_Real *array1, int array1length, SCIP_Real *array2, int array2length)
checks whether two arrays of SCIP_Real's are identical
USERGIVEN
enumeration to display if a decomposition was given by the user and if so, how it was processed after...
@ COMPLETED_CONSTOMASTER
static bool compare_blocks(std::pair< int, int > const &a, std::pair< int, int > const &b)
const char * GCGVisuGetPdfReader(GCG *gcg)
#define SCIPerrorMessage
SCIP_RETCODE GCGwriteGpVisualization(GCG *gcg, const char *filename, const char *outputname, int partialdecid)
SCIP_RETCODE GCGwriteGpVisualizationFormat(GCG *gcg, const char *filename, const char *outputname, int partialdecid, GP_OUTPUT_FORMAT outputformat)
GP file reader writing decompositions to gnuplot files.
enum GPOutputFormat GP_OUTPUT_FORMAT
Definition reader_gp.h:58
various SCIP helper methods
@ indicator
Definition scip_misc.h:51
structure information for decomposition information in GCG projects
data structures for detectors
data structures for score
BLOCK_STRUCTURE * translateStructure(std::vector< int > &rowmapping, std::vector< int > &colmapping, SCIP_Bool translatesymmetry)
std::vector< std::vector< int > > blockconss
std::vector< int > masterconss
std::unordered_map< int, int > symmetryvardata
std::vector< int > symmetricalblocks
PARTIALDECOMP * createPartialdec(DETPROBDATA *parentdetprobdata, DETPROBDATA *newdetprobdata, int probnr)
std::vector< BLOCK_STRUCTURE * > blockstructures
struct SCIP_Clock SCIP_CLOCK
struct SCIP_Cons SCIP_CONS
struct GCG_Detector GCG_DETECTOR
struct Gcg GCG
Definition type_gcg.h:44
SCIP_VERBLEVEL_FULL
struct SCIP_HashMap SCIP_HASHMAP
SCIP_Result
SCIP_SUCCESS
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
struct GCG_Score GCG_SCORE
Definition type_score.h:45
struct SCIP_Var SCIP_VAR