Scippy

GCG

Branch-and-Price & Column Generation for Everyone

dec_stairheur.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 dec_stairheur.cpp
30 *
31 * @brief detector for staircase structures via ROC algorithms
32 * @author Martin Bergner
33 * @author Mathias Luers
34 *
35 * This detector is based on Jayakumar, Maliyakal D., and Ranga V. Ramasesh.
36 * A clustering heuristic to detect staircase structures in large scale
37 * linear programming models. European journal of operational research 76.1 (1994): 229-239.
38 */
39
40/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
41
42#include <cassert>
43#include <cstring>
44#include <algorithm>
45#include <vector>
46#include <iostream>
47
48#include "gcg/dec_stairheur.h"
49#include "gcg/cons_decomp.h"
50#include "gcg/struct_decomp.h"
51#include "gcg/pub_decomp.h"
52#include "gcg/scip_misc.h"
53#include "scip/pub_misc.h"
54#include "scip/struct_var.h"
55#include "gcg/gcg.h"
58#include "scip/clock.h"
59
60#define DEC_NAME "stairheur" /**< name of the detector */
61#define DEC_DESC "detects staircase matrices via matrix reordering" /**< detector description */
62#define DEC_PRIORITY 1200 /**< priority of the detector */
63#define DEC_FREQCALLROUND 1 /**< frequency the detector gets called in detection loop ,ie it is called in round r if and only if minCallRound <= r <= maxCallRound AND (r - minCallRound) mod freqCallRound == 0 */
64#define DEC_MAXCALLROUND INT_MAX /**< last round the detector gets called */
65#define DEC_MINCALLROUND 0 /**< first round the detector gets called */
66#define DEC_FREQCALLROUNDORIGINAL 1 /**< frequency the detector gets called in detection loop while detecting the original problem */
67#define DEC_MAXCALLROUNDORIGINAL INT_MAX /**< last round the detector gets called while detecting the original problem */
68#define DEC_MINCALLROUNDORIGINAL 0 /**< first round the detector gets called while detecting the original problem */
69#define DEC_DECCHAR 's' /**< display character of detector */
70#define DEC_ENABLED FALSE /**< should detector be called by default */
71#define DEC_ENABLEDFINISHING FALSE /**< should the finishing be enabled */
72#define DEC_ENABLEDPOSTPROCESSING FALSE /**< should the postprocessing be enabled */
73#define DEC_SKIP FALSE /**< should detector be skipped if others found detections */
74#define DEC_USEFULRECALL FALSE /**< is it useful to call this detector on a descendant of the propagated partialdec */
75
76/* Default parameter settings*/
77#define DEFAULT_NCONSSPERBLOCK 32 /**< number of constraints per block (static blocking only) */
78#define DEFAULT_MAXBLOCKS 20 /**< value for the maximum number of blocks to be considered */
79#define DEFAULT_MINBLOCKS 2 /**< value for the minimum number of blocks to be considered */
80#define DEFAULT_DESIREDBLOCKS 0 /**< value for the desired number of blocks (for all blocking types). Set to zero for self determination of block number */
81#define DEFAULT_DYNAMICBLOCKING FALSE /**< Enable blocking type 'dynamic' */
82#define DEFAULT_STATICBLOCKING TRUE /**< Enable blocking type 'static' */
83#define DEFAULT_BLOCKINGASSOONASPOSSIBLE FALSE /**< Enable blocking type 'as soon as possible' */
84#define DEFAULT_MULTIPLEDECOMPS TRUE /**< Enables multiple decompositions for all enabled blocking types. Ranging from minblocks to maxblocks' */
85#define DEFAULT_MAXITERATIONSROC 1000000 /**< The maximum of iterations of the ROC-algorithm. -1 for no iteration limit */
86
87using std::find;
88using std::vector;
89using std::swap;
90
91//#define WRITEALLOUTPUT
92
93
94#ifdef WRITEALLOUTPUT
95#define DWSOLVER_REFNAME(name, blocks, cons, dummy) "%s_%d_%d_%.1f_ref.txt", (name), (blocks), (cons), (dummy)
96#define GP_NAME(name, blocks, cons, dummy) "%s_%d_%d_%.1f_%d.gp", (name), (blocks), (cons), (dummy)
97#endif
98/*
99 * Data structures
100 */
101
102/** A struct that contains 4 hashmaps, which maps variables and constraints to their position in the constraint matrix (Ax<=b) and vice versa */
104{
105 SCIP_HASHMAP* indexcons; /**< index in problem -> constraint */
106 SCIP_HASHMAP* consindex; /**< constraint -> index in problem */
107 SCIP_HASHMAP* indexvar; /**< index in problem -> variable */
108 SCIP_HASHMAP* varindex; /**< variable -> index in problem */
109};
110typedef struct IndexMap INDEXMAP;
111
112/** detector data */
113struct GCG_DetectorData
114{
115 SCIP_HASHMAP* constoblock; /**< hashmap mapping constraints to blocks */
116 int blocks; /**< number of blocks */
117 int nconssperblock; /**< number of constraints per block (static blocking only) */
118 int maxblocks; /**< maximum number of constraints per block */
119 int minblocks; /**< minimum number of constraints per block */
120 int usermaxblocks; /**< maximum number of constraints per block given by user */
121 int userminblocks; /**< minimum number of constraints per block given by user*/
122 INDEXMAP* indexmap; /**< index map (contains 4 hashmaps) */
123 int* ibegin; /**< array, ibegin[i]: index of first nonzero entry in row i */
124 int* iend; /**< array, iend[i]: index of last nonzero entry in row i */
125 int* jbegin; /**< array, jbegin[j]: index of first nonzero entry in column j */
126 int* jend; /**< array, jend[j]: index of last nonzero entry in column j */
127 int* jmin; /**< array, jmin[i]: index of first nonzero column of the i-th row */
128 int* jmax; /**< array, jmax[i]: the last nonzero entry among all rows prior to and including the i-th row */
129 int* minV; /**< array, minV[i]: number of linking variables corresponding to a partitioning after the i-th row */
130 int* width; /**< array, width[i]: width of the band (of nonzero entries after ROC) at row i */
131 int* hashmapindices; /**< array with integers running from 0 to maximum(nvars, ncons)+1 (for usage of hash maps) */
133 vector<int>* blockedAfterrow;
135 SCIP_Bool dynamicblocking; /**< Enable blocking type 'dynamic' */
136 SCIP_Bool staticblocking; /**< Enable blocking type 'static' */
137 SCIP_Bool blockingassoonaspossible; /**< Enable blocking type 'as soon as possible' */
138 SCIP_Bool multipledecomps; /**< Enables multiple decompositions for all enabled blocking types. Ranging from minblocks to maxblocks */
140};
141
142/*
143 * Local methods
144 */
145
146
147/* debugging methods */
149 vector<vector<int> > l
150 )
151{
152 vector<int>::iterator inner;
153 vector<vector<int> >::iterator outer;
154 SCIPdebugPrintf("S:");
155 for( outer = l.begin(); outer != l.end(); ++outer)
156 {
157 SCIPdebugPrintf("\t");
158 for( inner = outer->begin(); inner != outer->end(); ++inner)
159 {
160 SCIPdebugPrintf(" %d", *inner);
161 }
162 SCIPdebugPrintf(".\n");
163 }
164 SCIPdebugPrintf("Done\n");
165}
166
168 vector<int > l
169 )
170{
171 vector<int>::iterator inner;
172 for( inner = l.begin(); inner != l.end(); ++inner)
173 {
174 SCIPdebugPrintf(" %d", *inner);
175 }
176}
177
178
179/** TODO:
180 * currently, all vars from the first column where a linking var appears until the end of the block are considered as linking vars, although there might be empty columns. This could be changed so that these empty columns are considered as subscipvars and not linking vars.
181 *
182 * In some cases a block can consist of linking vars exclusively. This makes no real sense.
183 *
184 * For some instances the assertion regarding the consistency of the arrays ibegin and jbegin fails
185 * */
186
187
188/** creates a list with integers running from 'from' to 'to'. */
189static
191 int from, /**< Start index */
192 int to /**< End index */
193 )
194{
195 vector<int> v;
196
197 for( int i = from; i <= to; ++i )
198 {
199 v.push_back(i);
200 }
201 return v;
202}
203
204
205/** rearranges elements of vector according to the ordering of order.
206 *
207 * example: vector = (a b c d); order = (3 2 4 1)
208 * after calling SCIPvectorRearrange(vector, order): vector = (c b d a)
209 * both vectors must have the same size
210 * order must have elements from 1 to vector->size */
211static
213 vector<vector<int> > &l,
214 vector<int> order)
215{
216
217 vector<vector<int> > new_vector;
218 vector<int>::iterator it1;
219
220 for( it1 = order.begin(); it1 != order.end(); ++it1 )
221 {
222 new_vector.push_back(l[(size_t)*it1-1]);
223
224 }
225 l.swap(new_vector);
226
227}
228
229/** allocates memory for an indexmap. */
230static
232 SCIP* scip, /**< SCIP data structure */
233 INDEXMAP** indexmap, /**< address of the pointer which shall store the index map*/
234 int nconss, /**< number of constraints */
235 int nvars /**< number of variables */
236)
237{
238 INDEXMAP* imap = NULL;
239 assert(scip != NULL);
240 assert(nconss > 0);
241 assert(nvars > 0);
242 SCIP_CALL( SCIPallocMemory(scip, &imap) );
243 assert(imap != NULL);
244
249
250 *indexmap = imap;
251 return SCIP_OKAY;
252}
253
254/** deallocates memory of indexmap. */
255static
257 SCIP* scip, /**< SCIP data structure */
258 INDEXMAP** indexmap /**< index map */
259 )
260{
261 SCIPhashmapFree(&(*indexmap)->indexvar);
262 SCIPhashmapFree(&(*indexmap)->varindex);
263 SCIPhashmapFree(&(*indexmap)->indexcons);
264 SCIPhashmapFree(&(*indexmap)->consindex);
265 SCIPfreeMemory(scip, indexmap);
266}
267
268
269/** initialization method for the indexmap for partialdecs */
270static
272 INDEXMAP* indexmap, /**< index map */
273 gcg::PARTIALDECOMP* partialdec, /**< partial decomposition to create indexmap for */
274 int* hashmapindices /**< indices of variables and constraints */
275 )
276{
277 int i;
278 int* hashmapindex;
279 int nvars = partialdec->getNOpenvars();
280 int nconss = partialdec->getNOpenconss();
281
282 for( i = 0; i < nvars; ++i )
283 {
284 int var = partialdec->getOpenvars()[i];
285 /* careful: hashmapindex+1, because '0' is treated as an empty hashmap entry, which causes an error */
286 hashmapindex = hashmapindices + i+1;
287 assert( ! SCIPhashmapExists(indexmap->indexvar, (void*) hashmapindex));
288 SCIP_CALL( SCIPhashmapInsert(indexmap->indexvar, (void*) hashmapindex, (void*)(size_t) var) );
289 assert( ! SCIPhashmapExists(indexmap->varindex, (void*)(size_t) var));
290 SCIP_CALL( SCIPhashmapInsert(indexmap->varindex, (void*)(size_t) var, (void*) hashmapindex) );
291 }
292
293 for( i = 0; i < nconss; ++i )
294 {
295 int cons = partialdec->getOpenconss()[i];
296 /* careful: hashmapindex+1, because '0' is treated as an empty hashmap entry, which causes an error */
297 hashmapindex = hashmapindices + i+1;
298 assert( ! SCIPhashmapExists(indexmap->indexcons, (void*) hashmapindex));
299 SCIP_CALL( SCIPhashmapInsert(indexmap->indexcons, (void*) hashmapindex, (void*)(size_t) cons) );
300 assert( ! SCIPhashmapExists(indexmap->consindex, (void*)(size_t) cons));
301 SCIP_CALL( SCIPhashmapInsert(indexmap->consindex, (void*)(size_t) cons, (void*) hashmapindex) );
302 }
303
304 return SCIP_OKAY;
305}
306
307/* debug ? */
308#ifdef WRITEALLOUTPUT
309/** returns the problem name without the path */
310static const char* getProbNameWithoutPath(
311 SCIP* scip
312)
313{
314 const char* pname;
315 /* remove '/' from problem name */
316 pname = strrchr(SCIPgetProbName(scip), '/');
317 if( pname == NULL )
318 {
319 pname = SCIPgetProbName(scip);
320 }
321 else
322 {
323 pname = pname+1;
324 }
325 return pname;
326}
327
328
329static void checkConsistencyOfIndexarrays(DEC_DETECTORDATA* detectordata, int nvars, int nconss)
330{
331 int i;
332 for( i = 0; i < nconss - 1; ++i )
333 {
334 assert(detectordata->ibegin[i] <= detectordata->ibegin[i+1]);
335 }
336 for( i = 0; i < nvars - 1; ++i )
337 {
338 assert(detectordata->jbegin[i] <= detectordata->jbegin[i+1]);
339 }
340}
341
342
343/* debug ? */
344/** creates a data and a gnuplot file for the initial problem.
345 * @param scip < SCIP data structure
346 * @param detectordata < detector data data structure
347 * @param filename name of the output files (without any filename extension) */
348static
349SCIP_RETCODE plotInitialProblem(
350 SCIP* scip,
351 DEC_DETECTORDATA* detectordata,
352 char* filename
353)
354{
355 FILE* output;
356 char datafile[256];
357 char gpfile[256];
358 char pdffile[256];
359
360 int nconss;
361 nconss = SCIPgetNConss(scip);
362
363 /* filenames */
364 sprintf(datafile, "%s.dat", filename);
365 sprintf(gpfile, "%s.gp", filename);
366 sprintf(pdffile, "%s.pdf", filename);
367 output = fopen(datafile, "w");
368 if (output == NULL)
369 {
370 SCIPinfoMessage(scip, NULL, "Can't open file for output in plotProblem!\n");
371 }
372 else
373 {
374 int i;
375
376 for( i = 0; i < nconss; ++i )
377 {
378 int j;
379 SCIP_Bool success;
380 SCIP_VAR** curvars;
381 int ncurvars;
382 int consindex;
383 SCIP_CONS* cons = SCIPgetConss(scip)[i];
384 consindex = *((int*) SCIPhashmapGetImage(detectordata->indexmap->consindex, cons));
385 assert(SCIPhashmapExists(detectordata->indexmap->consindex, cons));
386
387 /* Get array of variables from constraint */
388 SCIP_CALL( SCIPgetConsNVars(scip, cons, &ncurvars, &success) );
389 assert(success);
390 SCIP_CALL( SCIPallocMemoryArray(scip, &curvars, ncurvars) );
391 SCIP_CALL( SCIPgetConsVars(scip, cons, curvars, ncurvars, &success) );
392 assert(success);
393 for( j = 0; j < ncurvars; ++j )
394 {
395 SCIP_VAR* var = SCIPvarGetProbvar(curvars[j]);
396 assert(SCIPhashmapExists(detectordata->indexmap->varindex, var));
397 int varindex = *((int*) SCIPhashmapGetImage(detectordata->indexmap->varindex, var));
398 assert(varindex <= SCIPgetNVars(scip));
399 assert(varindex > 0);
400 assert(consindex <= SCIPgetNConss(scip));
401 assert(consindex > 0);
402 fprintf(output, "%d %d\n", varindex, consindex);
403 }
404 SCIPfreeMemoryArray(scip, &curvars);
405 }
406 }
407 fclose(output);
408
409 /* write Gnuplot file */
410 output = fopen(gpfile, "w");
411 fprintf(output, "set terminal pdf\nset output \"%s\"\nunset xtics\nunset ytics\nunset border\nset pointsize 0.05\nset xrange [0:%i]\nset yrange[%i:0]\nplot '%s' lt 0 pt 5 notitle", pdffile, SCIPgetNVars(scip), nconss, datafile);
412 fclose(output);
413 return SCIP_OKAY;
414}
415
416/** creates a data and a gnuplot file for the graph representing the array minV (number of linking variables).
417 * @param detectordata < detector data data structure
418 * @param filename name of the output files (without any filename extension) */
419static
420void plotMinV(
421 SCIP* scip,
422 DEC_DETECTORDATA* detectordata,
423 char* filename
424)
425{
426 FILE* output;
427 char datafile[256];
428 char blockingfile[256];
429 char gpfile[256];
430 char pdffile[256];
431 int nconss;
432 vector<int>::iterator it1;
433
434 nconss = SCIPgetNConss(scip);
435
436 /* filenames */
437 sprintf(datafile, "%s.dat", filename);
438 sprintf(blockingfile, "%s_blocked_at.dat", filename);
439 sprintf(gpfile, "%s.gp", filename);
440 sprintf(pdffile, "%s.pdf", filename);
441
442 /* datafile */
443 output = fopen(datafile, "w");
444 if (output == NULL)
445 {
446 SCIPinfoMessage(scip, NULL, "Can't open file for output in plotMinV!\n");
447 }
448 else
449 {
450 int i;
451
452 /* write data to datafile */
453 for( i = 0; i < nconss -1; ++i )
454 {
455 fprintf(output, "%d\n", detectordata->minV[i]);
456 }
457 }
458 fclose(output);
459
460 /* blocking points */
461 output = fopen(blockingfile, "w");
462 if (output == NULL)
463 {
464 SCIPinfoMessage(scip, NULL, "Can't open file for blocking output in plotMinV!\n");
465 }
466 else
467 {
468 /* write data to blockingfile */
469 for( it1 = detectordata->blockedAfterrow->begin(); it1 != detectordata->blockedAfterrow->end(); ++it1 )
470 {
471 fprintf(output, "%d %d\n", *it1-1, detectordata->minV[*it1-1]);
472 }
473 }
474 fclose(output);
475 /* write Gnuplot file */
476 output = fopen(gpfile, "w");
477 fprintf(output, "set terminal pdf\nset output \"%s\"\nset style line 1 lt 1 lc rgb \"black\"\nplot '%s' title '# verb. Variablen' ls 1 with lines, \\\n '%s' lt 0 pt 4 with points title \"Blockgrenze\"", pdffile, datafile, blockingfile);
478 fclose(output);
479}
480
481
482#endif
483
484
485/** initialization method of detector data for partialdecs */
486static
488 SCIP* scip, /**< SCIP data structure */
489 gcg::PARTIALDECOMP* partialdec, /**< partial decomposition to use for initialization */
490 GCG_DETECTORDATA* detectordata /**< detector data structure */
491 )
492{
493 int i;
494 int nvars;
495 int nconss;
496
497 assert(partialdec != NULL);
498 assert(detectordata != NULL);
499
500 nvars = partialdec->getNOpenvars();
501 nconss = partialdec->getNOpenconss();
502 detectordata->maxblocks = MIN(nconss, detectordata->usermaxblocks);
503 detectordata->minblocks = MIN(detectordata->userminblocks, detectordata->maxblocks);
504
505 SCIP_CALL( SCIPallocMemoryArray(scip, &detectordata->ibegin, nconss) );
506 SCIP_CALL( SCIPallocMemoryArray(scip, &detectordata->iend, nconss) );
507 SCIP_CALL( SCIPallocMemoryArray(scip, &detectordata->jbegin, nvars) );
508 SCIP_CALL( SCIPallocMemoryArray(scip, &detectordata->jend, nvars) );
509 SCIP_CALL( SCIPallocMemoryArray(scip, &detectordata->jmin, nconss) );
510 SCIP_CALL( SCIPallocMemoryArray(scip, &detectordata->jmax, nconss) );
511 SCIP_CALL( SCIPallocMemoryArray(scip, &detectordata->minV, nconss-1) );
512 SCIP_CALL( SCIPallocMemoryArray(scip, &detectordata->width, nconss) );
513 SCIP_CALL( SCIPallocMemoryArray(scip, &detectordata->hashmapindices, (size_t)MAX(nvars, nconss) + 1) );
514 for( i = 0; i < MAX(nvars, nconss)+1; ++i )
515 {
516 detectordata->hashmapindices[i] = i;
517 }
518 detectordata->rowsWithConstrictions = new vector<int>();
519 detectordata->blockedAfterrow = new vector<int>();
520
521 /* create hash tables */
522 SCIP_CALL( indexmapCreate(scip, &detectordata->indexmap, nconss, nvars) );
523
524 return SCIP_OKAY;
525}
526
527/** deinitialization method of detector data (called after detection has been finished) */
528static
530 SCIP* scip, /**< SCIP data structure */
531 GCG_DETECTORDATA* detectordata /**< detector data structure */
532 )
533{
534 assert(scip != NULL);
535 assert(detectordata != NULL);
536
537 indexmapFree(scip, &detectordata->indexmap);
538
539 /* delete vectors */
540 delete detectordata->rowsWithConstrictions;
541 detectordata->rowsWithConstrictions = NULL;
542 delete detectordata->blockedAfterrow;
543 detectordata->blockedAfterrow = NULL;
544
545 if (detectordata->constoblock != NULL)
546 {
547 SCIPhashmapFree(&detectordata->constoblock);
548 detectordata->constoblock = NULL;
549 }
550
551 SCIPfreeMemoryArray(scip, &detectordata->ibegin);
552 SCIPfreeMemoryArray(scip, &detectordata->iend);
553 SCIPfreeMemoryArray(scip, &detectordata->jbegin);
554 SCIPfreeMemoryArray(scip, &detectordata->jend);
555 SCIPfreeMemoryArray(scip, &detectordata->jmin);
556 SCIPfreeMemoryArray(scip, &detectordata->jmax);
557 SCIPfreeMemoryArray(scip, &detectordata->minV);
558 SCIPfreeMemoryArray(scip, &detectordata->width);
559 SCIPfreeMemoryArray(scip, &detectordata->hashmapindices);
560
561 return SCIP_OKAY;
562}
563
564
565/** creates a nested vector with the indices of the nonzero entries of each row.
566 *
567 * example:
568 * constraint matrix:
569 *
570 * 1 1 0 1 0
571 *
572 * 0 1 1 0 0
573 *
574 * 0 0 0 0 1
575 *
576 * resulting vector:
577 * ( (1 2 4)
578 * (2 3)
579 * (5) )
580 */
581static
583 SCIP* scip, /**< SCIP data structure */
584 gcg::PARTIALDECOMP* partialdec, /**< partial decomposition to use for matrix */
585 gcg::DETPROBDATA* detprobdata, /**< detection process information and data */
586 GCG_DETECTORDATA* detectordata, /**< detector data data structure */
587 SCIP_HASHMAP* indexcons, /**< hashmap index -> constraint */
588 SCIP_HASHMAP* varindex, /**< hashmap variable -> index*/
589 vector<vector<int> > &rowindices /**< vector to store the row indices vector*/
590 )
591{
592 int i;
593 int nconss = partialdec->getNOpenconss();
594
595 for( i = 0; i < nconss; ++i )
596 {
597 int j;
598 int ncurrvars;
599 int* probindices = NULL;
600 int cons;
601 vector<int> rowindices_row;
602 int* hashmapindex = &detectordata->hashmapindices[(size_t)i+1];
603
604 cons = (int)(size_t) SCIPhashmapGetImage(indexcons, (void*) hashmapindex);
605 ncurrvars = detprobdata->getNVarsForCons(cons);
606
607 SCIP_CALL( SCIPallocClearBufferArray(scip, &probindices, ncurrvars) );
608
609 /* fill the array with the indices of the variables of the current constraint */
610 for( j = 0; j < ncurrvars; ++j )
611 {
612 if( !partialdec->isVarOpenvar(detprobdata->getVarsForCons(cons)[j]) )
613 continue;
614 probindices[j] = *(int*) SCIPhashmapGetImage(varindex, (void*)(size_t)detprobdata->getVarsForCons(cons)[j]);
615 }
616
617 std::sort(probindices, probindices+ncurrvars);
618
619 /* store a copy of the elements of probindices in the vector rowindices_row */
620 for( j = 0; j < ncurrvars; ++j )
621 {
622 if( probindices[j] <= 0 )
623 continue;
624 rowindices_row.push_back(probindices[j]);
625 }
626
627 SCIPfreeBufferArray(scip, &probindices);
628
629 /* add rowindices_row to the vector rowindices */
630 rowindices.push_back(rowindices_row);
631 rowindices_row.clear();
632 }
633
634 return SCIP_OKAY;
635}
636
637
638/** creates a nested vector with the indices of the nonzero entries of each column.
639 *
640 * example:
641 *
642 * constraint matrix:
643 *
644 * 1 1 0 1 0
645 *
646 * 0 1 1 0 0
647 *
648 * 0 0 0 0 1
649 *
650 * resulting vector:
651 * ( (1)
652 * (1 2)
653 * (2)
654 * (1)
655 * (3) )
656 */
657static
659 SCIP* scip, /**< SCIP data structure */
660 gcg::PARTIALDECOMP* partialdec, /**< partial decomposition to create column index for */
661 vector<vector<int> > &rowindices, /**< A vector with the row indices (achieved from calling rowindices_vector() ) */
662 vector<vector<int> > &columnindices /**< vector to store the column indices vector*/
663)
664{
665 int position;
666 int nvars;
667 int i;
668 vector<vector<int> >::iterator outer;
669 vector<int>::iterator inner;
670 nvars = partialdec->getNOpenvars();
671
672 vector<vector<int> > columnindices_array((size_t)nvars);
673
674 for( outer = rowindices.begin(), i = 1; outer != rowindices.end(); ++outer, ++i )
675 {
676 for( inner = outer->begin(); inner != outer->end(); ++inner )
677 {
678 position = (*inner)-1;
679 columnindices_array.at((size_t) position).push_back(i);
680 }
681 }
682
683 /* create a columnindices vector instead of an array */
684 for( i = 0; i < nvars; ++i )
685 {
686 columnindices.push_back(columnindices_array[(size_t)i]); /** @todo broken */
687 }
688
689 return SCIP_OKAY;
690}
691
692
693/** does the row ordering of the ROC2 algorithm.
694 *
695 * It also works for the column ordering. In this case the terms row<->column have to be exchanged.
696 *
697 * @return A vector with the new row order. E.g. (2 3 1) means the second row comes first now, and so on. */
698static
699vector<int> rowOrdering(
700 SCIP* scip, /**< SCIP data structure */
701 vector<vector<int> > &columnindices, /**< A vector of the nonzero entries in each column */
702 int nrows /**< The number of rows of the constraint matrix (=number of relevant constraints) */
703 )
704{
705 vector<int> roworder;
706 vector<int> new_roworder;
707 vector<vector<int> >::reverse_iterator it1;
708 vector<int>::reverse_iterator it2;
709
710 /* create a vector for the order of the rows ( 1 2 3 ... nrows ) */
711 roworder = SCIPvectorCreateInt(1, nrows);
712 new_roworder = SCIPvectorCreateInt(1, nrows);
713
714 /* first from back to front */
715 for( it1 = columnindices.rbegin(); it1 != columnindices.rend(); ++it1 )
716 {
717
718 /* second from back to front */
719 for( it2 = it1->rbegin(); it2 != it1->rend(); ++it2 )
720 {
721 vector<int>::iterator tmp;
722
723 tmp = std::find(new_roworder.begin(), new_roworder.end(), *it2); /*lint !e864*/
724 std::rotate(new_roworder.begin(), tmp, tmp+1); /*lint !e1702 !e747*/
725 }
726 roworder = new_roworder;
727 }
728
729 return roworder;
730}
731
732/** stores the first and last entry of the i-th column(row) in begin[i] and end[i] respectively.
733 *
734 * @param begin Array to store the first nonzero entry of the i-th column (row)
735 * @param end Array to store the last nonzero entry of the i-th column (row)
736 * @param indices columnindices vector (rowindices vector) */
737static
739 int* begin,
740 int* end,
741 vector<vector<int> > &indices
742 )
743{
744 vector<vector<int> >::iterator it1;
745 int i;
746 assert(begin != NULL && end != NULL);
747 for( it1 = indices.begin(), i = 0; it1 != indices.end(); ++it1, ++i )
748 {
749 /* case: vector not empty */
750 if( !it1->empty() )
751 {
752 begin[i] = it1->front();
753 end[i] = it1->back();
754 }
755 /* case: vector empty */
756 else
757 {
758 begin[i] = 0;
759 end[i] = 0;
760 }
761 }
762 return SCIP_OKAY;
763}
764
765
766/**returns FALSE if at least one entry of new_array and old_array are different.*/
767static
769 int* new_array, /**< new array */
770 int* old_array, /**< old array */
771 int num_elements /**< length of arrays */
772 )
773{
774 int i;
775 for( i = 0; i < num_elements; ++i )
776 {
777 if( new_array[i] != old_array[i] )
778 {
779 return FALSE;
780 }
781 }
782 /* case: all entries of old and new are equal */
783 return TRUE;
784}
785
786
787/**permutes the order of rows and columns in inputmap and stores the result in outputmap.
788 *
789 * One call of this function is equivalent to one iteration of the ROC2-algortihm. */
790static
792 SCIP* scip, /**< SCIP data structure */
793 gcg::PARTIALDECOMP* partialdec, /**< partial decomposition to use for permutation */
794 gcg::DETPROBDATA* detprobdata, /**< detection process information and data */
795 GCG_DETECTORDATA* detectordata, /**< detector data data structure */
796 INDEXMAP* inputmap, /**< indexmap for input */
797 INDEXMAP* outputmap /**< indexmap for output */
798 )
799{
800 vector<int> roworder;
801 vector<int> columnorder;
802 vector<vector<int> > rowindices;
803 vector<vector<int> > columnindices;
804 vector<int>::iterator it1;
805 int nvars;
806 int ncons;
807 size_t i;
808 int position;
809 int* hashmapindex;
810
811 SCIPdebugMessage("Entering rankOrderClusteringIteration\n");
812
813 assert(scip != NULL);
814 assert(detectordata != NULL);
815 nvars = partialdec->getNOpenvars();
816 ncons = partialdec->getNOpenconss();
817
818 /* create the vectors containing the positions of nonzero entries; row and column ordering */
819 SCIP_CALL( createRowindexList(scip, partialdec, detprobdata, detectordata, inputmap->indexcons, inputmap->varindex, rowindices) );
820 SCIP_CALL( createColumnindexList(scip, partialdec, rowindices, columnindices) );
821
822 roworder = rowOrdering(scip, columnindices, ncons);
823 SCIPvectorRearrange(rowindices, roworder);
824
825 columnorder = rowOrdering(scip, rowindices, nvars);
826 SCIPvectorRearrange(columnindices, columnorder);
827
828 /* consindex and indexcons */
829 for( it1 = roworder.begin(), i = 0; it1 != roworder.end() && i < (size_t) ncons; ++i,++it1 )
830 {
831 int cons;
832 position = *it1;
833 hashmapindex = &detectordata->hashmapindices[position];
834
835 cons = (int)(size_t) SCIPhashmapGetImage(inputmap->indexcons, (void*) hashmapindex);
836
837 /* consindex */
838 hashmapindex = &detectordata->hashmapindices[i+1];
839 assert( SCIPhashmapExists(outputmap->consindex, (void*)(size_t) cons));
840 assert(*hashmapindex <= ncons);
841 SCIP_CALL( SCIPhashmapSetImage(outputmap->consindex, (void*)(size_t) cons, (void*) hashmapindex) );
842
843 /* indexcons */
844 assert( SCIPhashmapExists(outputmap->indexcons, (void*) hashmapindex ));
845 SCIP_CALL( SCIPhashmapSetImage(outputmap->indexcons, (void*) hashmapindex, (void*)(size_t) cons) );
846 }
847
848 /* varindex and indexvar */
849 for( it1 = columnorder.begin(), i = 0; it1 != columnorder.end() &&i < (size_t) nvars; ++i, ++it1 )
850 {
851 int var;
852 position = *it1;
853 hashmapindex = &detectordata->hashmapindices[position];
854 var = (int)(size_t) SCIPhashmapGetImage(inputmap->indexvar, (void*) hashmapindex);
855 assert(*hashmapindex <= nvars);
856 /* varindex */
857 hashmapindex = &detectordata->hashmapindices[i+1];
858 assert( SCIPhashmapExists(outputmap->varindex, (void*)(size_t) var) );
859 SCIP_CALL( SCIPhashmapSetImage(outputmap->varindex, (void*)(size_t) var, (void*) hashmapindex) );
860
861 /* indexvar */
862 assert( SCIPhashmapExists(outputmap->indexvar, (void*) hashmapindex ));
863 SCIP_CALL( SCIPhashmapSetImage(outputmap->indexvar, (void*) hashmapindex, (void*)(size_t) var) );
864 }
865
866 return SCIP_OKAY;
867}
868
869
870static
872 SCIP* scip, /**< SCIP data structure */
873 gcg::PARTIALDECOMP* partialdec, /**< partial decomposition to use for clustering */
874 gcg::DETPROBDATA* detprobdata, /**< detection process information and data */
875 GCG_DETECTORDATA* detectordata, /**< detector data structure */
876 int max_iterations, /**< number of maximal iterations */
877 int* iterations /**< number of performed iterations */
878 )
879{
880 int i;
881 INDEXMAP* indexmap_permuted;
882 vector<vector<int> > rowindices;
883 vector<vector<int> > columnindices;
884 int* ibegin_permuted = NULL;
885 int* iend_permuted = NULL;
886 int* jbegin_permuted = NULL;
887 int* jend_permuted = NULL;
888 assert(scip != NULL);
889 assert(detectordata != NULL);
890
891 int nvars = partialdec->getNOpenvars();
892 int nconss = partialdec->getNOpenconss();
893
894 if( iterations != NULL )
895 *iterations = -1;
896
897 if( max_iterations <= 0 )
898 {
899 return SCIP_OKAY;
900 }
901
902
903
904 SCIP_CALL( indexmapCreate(scip, &indexmap_permuted, nconss, nvars) );
905 SCIP_CALL( SCIPallocMemoryArray(scip, &ibegin_permuted, nconss) );
906 SCIP_CALL( SCIPallocMemoryArray(scip, &iend_permuted, nconss) );
907 SCIP_CALL( SCIPallocMemoryArray(scip, &jbegin_permuted, nvars) );
908 SCIP_CALL( SCIPallocMemoryArray(scip, &jend_permuted, nvars) );
909 SCIP_CALL( indexmapInit(indexmap_permuted, partialdec, detectordata->hashmapindices) );
910 i = 0;
911
912 do
913 {
914 ++i;
915 /* not more than max_iterations loops. no iteration limit for max_iterations == -1 */
916 if(i > max_iterations && max_iterations != -1)
917 break;
918
919 SCIPdebugMessage("Iteration # %i of ROC2\n", i);
920 SCIP_CALL( rankOrderClusteringIteration(scip, partialdec, detprobdata, detectordata, detectordata->indexmap, indexmap_permuted) );
921
922 /* form the new index arrays after the permutation */
923 SCIP_CALL( createRowindexList(scip, partialdec, detprobdata, detectordata, indexmap_permuted->indexcons, indexmap_permuted->varindex, rowindices) );
924 SCIP_CALL( createColumnindexList(scip, partialdec, rowindices, columnindices) );
925 SCIP_CALL( formIndexArray(ibegin_permuted, iend_permuted, rowindices) );
926 SCIP_CALL( formIndexArray(jbegin_permuted, jend_permuted, columnindices) );
927 rowindices.clear();
928 columnindices.clear();
929
930 /* switch between index arrays containing new and old indices */
931 swap( detectordata->ibegin, ibegin_permuted);
932 swap( detectordata->iend, iend_permuted);
933 swap( detectordata->jbegin, jbegin_permuted);
934 swap( detectordata->jend, jend_permuted);
935
936 /* switch between hash maps containing new and old indices */
937 swap(detectordata->indexmap, indexmap_permuted);
938 }
939 /* while Index Arrays change */
940 while( ! (arraysAreEqual(detectordata->ibegin, ibegin_permuted, nconss )
941 && arraysAreEqual(detectordata->iend, iend_permuted, nconss)
942 && arraysAreEqual(detectordata->jbegin, jbegin_permuted, nvars)
943 && arraysAreEqual(detectordata->jend, jend_permuted, nvars)));
944
945 indexmapFree(scip, &indexmap_permuted);
946 SCIPfreeMemoryArray(scip, &ibegin_permuted);
947 SCIPfreeMemoryArray(scip, &iend_permuted);
948 SCIPfreeMemoryArray(scip, &jbegin_permuted);
949 SCIPfreeMemoryArray(scip, &jend_permuted);
950
951
952 if( iterations != NULL )
953 *iterations = i-1;
954
955 return SCIP_OKAY;
956}
957
958
959/** finds rows with local minima regarding the number of linking variables and stores them in detectordata->rowsWithConstrictions */
960static
962 SCIP* scip, /**< SCIP data structure */
963 gcg::PARTIALDECOMP* partialdec, /**< partial decomposition to use */
964 GCG_DETECTORDATA* detectordata /**< detector data structure */
965 )
966{
967 /* if blocking is performed after row i+1; local minima */
968 size_t i;
969 size_t nconss = (size_t) partialdec->getNOpenconss();
970 for( i = 1; i < nconss - 2; ++i )
971 {
972 /* is minV[i] a local minimum? < or <= ? What does make more sense? */
973 if( detectordata->minV[i] < detectordata->minV[i-1] && detectordata->minV[i] < detectordata->minV[i+1] )
974 {
975 detectordata->rowsWithConstrictions->push_back((int)(i+1));
976 }
977 }
978 return SCIP_OKAY;
979}
980
981
982/** assigns constraints in the interval [first_cons, last_cons] to 'block'. (for partialdecs) */
983static
985 SCIP* scip, /**< SCIP data structure */
986 gcg::PARTIALDECOMP* partialdec, /**< partial decomposition to use */
987 GCG_DETECTORDATA* detectordata, /**< detector data structure */
988 int block, /**< id of block */
989 int first_cons, /**< index of first constraint to assign */
990 int last_cons /**< index of last constraint to assign */
991 )
992{
993 int i;
994
995 /* assign the constraints to the current block */
996 for( i = first_cons; i <= last_cons; ++i )
997 {
998 int* hashmapindex = &detectordata->hashmapindices[i];
999 int cons = (int)(size_t) SCIPhashmapGetImage(detectordata->indexmap->indexcons, (void*) hashmapindex);
1000 /* insert cons into hash map vartoblock */
1001 assert(!SCIPhashmapExists(detectordata->constoblock, (void*)(size_t) cons));
1002 SCIP_CALL( SCIPhashmapInsert(detectordata->constoblock, (void*)(size_t) cons, (void*) (size_t) (detectordata->hashmapindices[block])) );
1003 }
1004 detectordata->blockedAfterrow->push_back(detectordata->hashmapindices[last_cons]);
1005 return SCIP_OKAY;
1006}
1007
1008/** returns the largest column index of a nonzero entry between rows [from_row, to_row] */
1009static
1011 GCG_DETECTORDATA* detectordata, /**< detector data structure */
1012 int from_row, /**< index of starting row to check */
1013 int to_row /**< index of last row to check */
1014 )
1015{
1016 /* some pointer arithmetic */
1017 return std::max_element(detectordata->iend + (from_row), detectordata->iend+((size_t)to_row + 1))-detectordata->iend; /*lint !e712*/
1018}
1019
1020/** returns the column index of the first nonzero entry in 'row'. Rows start counting at 1, not 0. */
1021static
1023 GCG_DETECTORDATA* detectordata, /**< detector data structure */
1024 int row /**< index of row */
1025 )
1026{
1027 return detectordata->ibegin[row-1];
1028}
1029
1030/** determines if a blocking at 'block_at_row' is a valid blocking
1031 *
1032 * @return TRUE if blocking is valid, else FALSE
1033 */
1034static
1036 GCG_DETECTORDATA* detectordata, /**< detector data structure */
1037 int prev_block_first_row, /**< first row of the previous block */
1038 int prev_block_last_row, /**< last row of the previous block */
1039 int block_at_row /**< the row for which you want to determine if the blocking is valid */
1040 )
1041{
1042 int last_column_prev_block;
1043 int first_column_current_block;
1044
1045 /* if the function is called for the first block, the blocking is always valid */
1046 if( prev_block_last_row == 0 )
1047 {
1048 return TRUE;
1049 }
1050 last_column_prev_block = getMaxColIndex(detectordata, prev_block_first_row, prev_block_last_row);
1051 first_column_current_block = getMinColIndex(detectordata, block_at_row);
1052 return ( first_column_current_block > last_column_prev_block ? TRUE : FALSE);
1053}
1054
1055/** this functions looks for rows to block at, which creates block of size min_block_size or bigger
1056 *
1057 * @return Iterator pointing to a node which contains a suitable row for blocking; If the iterator points after the last element, no candidate was found
1058 */
1059static
1060vector<int>::iterator findBlockingCandidate(
1061 vector<int>::iterator it_constrictions, /**< Iterator pointing to a vector of constraints (detectordata->rowsWithConstrictions) */
1062 vector<int>* it_vector, /**< minimum number of rows to be in a block */
1063 int min_block_size, /**< the last row of the preceding block */
1064 int prev_block_last_row /**< the last row of the preceding block */
1065 )
1066{
1067 for( ;; )
1068 {
1069 /* end of the vector? */
1070 if( it_constrictions == it_vector->end() )
1071 {
1072 return it_constrictions;
1073 }
1074 /* does a blocking to the next row forming a constriction comprise more rows than min_block_size */
1075 if( (*it_constrictions - prev_block_last_row) >= min_block_size )
1076 {
1077 return it_constrictions;
1078 }
1079 /* advance iterator to next element */
1080 ++it_constrictions;
1081 }
1082}
1083
1084/** this functions determines the next row to block at
1085 *
1086 * @return Iterator pointing to a node which contains a suitable row for blocking; If the iterator points after the last element, no row was found
1087 */
1088static
1089vector<int>::iterator nextRowToBlockAt(
1090 GCG_DETECTORDATA* detectordata, /**< detector data structure */
1091 vector<int>::iterator it_constrictions, /**< Iterator pointing to a vector of constraints (detectordata->rowsWithConstrictions) */
1092 vector<int>* it_vector, /**< vector of constrictions */
1093 int min_block_size, /**< minimum number of rows to be in a block */
1094 int prev_block_first_row, /**< the first row of the preceding block */
1095 int prev_block_last_row /**< the last row of the preceding block*/
1096 )
1097{
1098
1099 /* end of the constriction vector? */
1100 if( it_constrictions == it_vector->end() )
1101 {
1102 return it_constrictions;
1103 }
1104
1105 for( ;; )
1106 {
1107 /* find a blocking candidate */
1108 it_constrictions = findBlockingCandidate(it_constrictions, it_vector, min_block_size, prev_block_last_row);
1109 /* case: no candidate found */
1110 if( it_constrictions == it_vector->end() )
1111 {
1112 break;
1113 }
1114 /* case: candidate found */
1115 else
1116 {
1117 /* valid blocking */
1118 if( isValidBlocking(detectordata, prev_block_first_row, prev_block_last_row, *it_constrictions) )
1119 {
1120 break;
1121 }
1122 /* invalid blocking */
1123 else
1124 {
1125 ++it_constrictions;
1126 }
1127 }
1128 }
1129 return it_constrictions;
1130}
1131
1132/** calculate the number of decompositions in order to allocate decomps array */
1133static
1135 GCG_DETECTORDATA* detectordata /**< detector data structure */
1136 )
1137{
1138 int nblockingtypes;
1139 int nblockingspertype;
1140
1141 nblockingtypes = 0;
1142 /* get the number of enabled blocking types */
1143 if( detectordata->dynamicblocking )
1144 {
1145 ++nblockingtypes;
1146 }
1147 if( detectordata->staticblocking )
1148 {
1149 ++nblockingtypes;
1150 }
1151 if( detectordata->blockingassoonaspossible )
1152 {
1153 ++nblockingtypes;
1154 }
1155
1156 /* get the number of blockings per blocking type */
1157 if( detectordata->multipledecomps )
1158 {
1159 nblockingspertype = detectordata->maxblocks - detectordata->minblocks + 1;
1160 }
1161 else
1162 {
1163 nblockingspertype = 1;
1164 }
1165
1166 return nblockingtypes * nblockingspertype;
1167}
1168
1169/** check the consistency of the parameters */
1170static
1172 SCIP* scip, /**< SCIP data structure */
1173 GCG_DETECTORDATA* detectordata, /**< detector data structure */
1174 SCIP_RESULT* result /**< pointer to store result */
1175 )
1176{
1177 /* is minblocks <= maxblocks? */
1178 if( detectordata->multipledecomps )
1179 {
1180 if( detectordata->minblocks > detectordata->maxblocks )
1181 {
1182 SCIPwarningMessage(scip, "minblocks > maxblocks. Setting minblocks = maxblocks.\n");
1183 detectordata->minblocks = detectordata->maxblocks;
1184 }
1185 }
1186
1187 /* is at least one blocking type enabled? */
1188 if( ! detectordata->blockingassoonaspossible && ! detectordata->staticblocking &&! detectordata->dynamicblocking )
1189 {
1190 SCIPwarningMessage(scip, "No blocking type enabled, cannot perform blocking.\n");
1191 *result = SCIP_DIDNOTRUN;
1192 }
1193}
1194
1195
1196/** tries to dynamically divide the problem into subproblems (blocks)*/
1197static
1199 SCIP* scip, /**< SCIP data structure */
1200 gcg::PARTIALDECOMP* partialdec, /**< partial decomposition to use */
1201 GCG_DETECTORDATA* detectordata, /**< detector data data structure */
1202 int tau, /**< desired number of blocks */
1203 int nvars /**< number of variables in the problem*/
1204 )
1205{ /*lint -e715*/
1206 int block;
1207 int prev_block_first_row;
1208 int prev_block_last_row;
1209 int min_block_size;
1210 /* notation: i=current block; im1=i-1=previous block; ip1=i+1=next block */
1211#ifdef SCIP_DEBUG
1212 int max_col_index_im1 = 0;
1213#endif
1214 vector<int>::iterator it1;
1215 /* debug */
1216 SCIPdebugMessage("Starting Blocking...\n");
1217 SCIPdebugMessage("Max blocks: %i\n", detectordata->maxblocks);
1218 block = 1;
1219 prev_block_first_row = 0;
1220 prev_block_last_row = 0;
1221
1222 assert(tau > 0);
1223 min_block_size = (int) SCIPround(scip, ((SCIP_Real)partialdec->getNOpenconss()) / (2.0 * tau ));
1224 it1 = detectordata->rowsWithConstrictions->begin();
1225
1226 for( it1 = nextRowToBlockAt(detectordata, it1, detectordata->rowsWithConstrictions, min_block_size, prev_block_first_row, prev_block_last_row);
1227 it1 != detectordata->rowsWithConstrictions->end() && block < detectordata->maxblocks;
1228 it1 = nextRowToBlockAt(detectordata, it1, detectordata->rowsWithConstrictions, min_block_size, prev_block_first_row, prev_block_last_row) )
1229 {
1230 int current_row = * it1;
1231#ifdef SCIP_DEBUG
1232 int max_col_index_i = getMaxColIndex(detectordata, prev_block_last_row + 1, current_row);
1233 int min_col_index_ip1 = getMinColIndex(detectordata, current_row + 1);
1234 SCIPdebugMessage("vars in block: %i - %i, linking vars: %i - %i\n", max_col_index_im1+1, max_col_index_i, min_col_index_ip1, max_col_index_i);
1235#endif
1236 /* assign the variables and constraints to block */
1237 SCIP_CALL( assignConsToBlock(scip, partialdec, detectordata, block, prev_block_last_row + 1, current_row) );
1238 /* update variables in the while loop */
1239 prev_block_first_row = prev_block_last_row + 1;
1240 prev_block_last_row = current_row;
1241 ++block;
1242
1243#ifdef SCIP_DEBUG
1244 max_col_index_im1 = max_col_index_i;
1245#endif
1246 }
1247 /* assign the remaining (< M/2tau) cons and vars to the last block; no new linking vars are added */
1248#ifdef SCIP_DEBUG
1249 SCIPdebugMessage("last time: vars in block: %i - %i, linking vars: %i - %i\n", max_col_index_im1+1, nvars, nvars+1, nvars);
1250#endif
1251 SCIP_CALL( assignConsToBlock(scip, partialdec, detectordata, block, prev_block_last_row + 1, SCIPgetNConss(scip)) );
1252 detectordata->blockedAfterrow->pop_back();
1253
1254 detectordata->blocks = block;
1255
1256 /* debug plot the blocking plot for [i=1:2:1] 'test.dat' every :::i::i lt i pt 5 */
1257#ifdef WRITEALLOUTPUT
1258 {
1259 char filename[256];
1260 char paramfile[256];
1261
1262 sprintf(filename, "%s_dynamic_minV", getProbNameWithoutPath(scip));
1263 sprintf(paramfile, "%s_dynamic.params", getProbNameWithoutPath(scip));
1264 plotMinV(scip, detectordata, filename);
1265 }
1266#endif
1267
1268 return SCIP_OKAY;
1269}
1270
1271
1272/** creates blocks with the same number of rows (for partialdecs) */
1273static
1275 SCIP* scip, /**< SCIP data structure */
1276 gcg::PARTIALDECOMP* partialdec, /**< partial decomposition to create block in */
1277 GCG_DETECTORDATA* detectordata /**< detector data data structure */
1278 )
1279{
1280 int nblocks;
1281 int block;
1282 int prev_block_last_row;
1283 int current_row;
1284 int nconss;
1285 /* notation: i=current block; im1=i-1=previous block; ip1=i+1=next block */
1286
1287 assert(scip != NULL);
1288 assert(detectordata != NULL);
1289 nconss = partialdec->getNOpenconss();
1290 nblocks = nconss/detectordata->nconssperblock;
1291 prev_block_last_row = 0;
1292 current_row = 0;
1293
1294 /* blocks 1 to (desired_blocks-1) */
1295 for( block = 1; block <= nblocks; ++block )
1296 {
1297 current_row += detectordata->nconssperblock;
1298 SCIPdebugMessage("Blocking from %d to %d in block %d",prev_block_last_row + 1, current_row, block);
1299 SCIP_CALL( assignConsToBlock(scip, partialdec, detectordata, block, prev_block_last_row + 1, current_row) );
1300
1301
1302 prev_block_last_row = current_row;
1303 }
1304 /* last block */
1305 /* assign the remaining cons and vars to the last block; no new linking vars are added */
1306 if( prev_block_last_row + 1 <= nconss)
1307 {
1308 SCIPdebugMessage("last time: assignVarsToBlock: block, from_row, to_row: %i, %i, %i\n", block, prev_block_last_row + 1, SCIPgetNConss(scip));
1309
1310 SCIP_CALL( assignConsToBlock(scip, partialdec, detectordata, block, prev_block_last_row + 1, nconss) );
1311 detectordata->blockedAfterrow->pop_back();
1312 ++block;
1313 }
1314 detectordata->blocks = block-1;
1315
1316#ifdef WRITEALLOUTPUT
1317 {
1318 char filename[256];
1319 char paramfile[256];
1320
1321 /* debug */
1322 sprintf(filename, "%s_static_minV_%i", getProbNameWithoutPath(scip), detectordata->blocks);
1323 sprintf(paramfile, "%s_static.params", getProbNameWithoutPath(scip));
1324 plotMinV(scip, detectordata, filename);
1325 }
1326#endif
1327
1328 return SCIP_OKAY;
1329}
1330
1331static
1333 SCIP* scip, /**< SCIP data structure */
1334 GCG_DETECTORDATA* detectordata, /**< detector data data structure */
1335 int desired_blocks, /**< desired number of blocks */
1336 int nvars /**< number of variables in the problem*/
1337)
1338{
1339 int block;
1340 block = 0;
1341 detectordata->blocks = block;
1342
1343 return SCIP_OKAY;
1344}
1345
1346/** resets detectordata such that it can be used for the next decomposition */
1347static
1349 SCIP* scip, /**< SCIP data structure */
1350 GCG_DETECTORDATA* detectordata /**< detector data structure */
1351 )
1352{
1353 if(detectordata->constoblock != NULL)
1354 {
1355 SCIP_CALL( SCIPhashmapRemoveAll(detectordata->constoblock) );
1356 }
1357 else
1358 {
1360 }
1361 return SCIP_OKAY;
1362}
1363
1364static
1366 SCIP* scip, /**< SCIP data structure */
1367 GCG_DETECTORDATA* detectordata, /**< detector data structure */
1368 gcg::PARTIALDECOMP* partialdec, /**< partialdec to propagate */
1369 PARTIALDEC_DETECTION_DATA* detectiondata, /**< detection data */
1370 SCIP_Real time, /**< previous time */
1371 int maxndecs, /**< capacity of detectiondata->newpartialdecs */
1372 SCIP_RESULT* result /**< pointer to store result */
1373 )
1374{
1375 std::vector<SCIP_Real> clocktimes;
1376 SCIP_CLOCK* temporaryClock;
1377 int* nnewpartialdecs = &detectiondata->nnewpartialdecs;
1378 char decinfo[SCIP_MAXSTRLEN];
1379 int tau = 1; /* desired number of blocks */
1380 int ncons = partialdec->getNOpenconss();
1381 int nvars = partialdec->getNOpenvars();
1382
1383 SCIP_CALL_ABORT( SCIPcreateClock(scip, &temporaryClock) );
1384 SCIP_CALL_ABORT( SCIPstartClock(scip, temporaryClock) );
1385
1386 assert(*nnewpartialdecs == 0);
1387 clocktimes.reserve(maxndecs);
1388
1389 SCIPdebugMessage("Entering Blocking\n");
1390
1391 /* if multiple decompositions disabled */
1392 if( detectordata->multipledecomps == FALSE )
1393 {
1394 /* if desiredblocks == 0 let the algorithm determine the desired number of blocks */
1395 if( detectordata->desiredblocks == 0 )
1396 {
1397 int n = *std::max_element(detectordata->width, detectordata->width+ncons);
1398 int v = *std::min_element(detectordata->width, detectordata->width+ncons);
1399 tau = (int)SCIPround(scip, ((SCIP_Real)nvars - v)/((SCIP_Real)n - v));
1400 SCIPdebugMessage("<n><v><tau>: <%i><%i><%i>\n", n, v, tau);
1401 if( tau > detectordata->maxblocks )
1402 {
1403 tau = detectordata->maxblocks;
1404 }
1405
1406 SCIPdebugMessage("detectordata->enablemultipledecomps = 0. detectordata->desiredblocks == 0. Calculating tau = %i\n", tau);
1407 /* continue only if tau >= 2 */
1408 if( tau < 2 )
1409 {
1410 *result = SCIP_DIDNOTFIND;
1411 SCIP_CALL_ABORT( SCIPstopClock(scip, temporaryClock) );
1412 SCIP_CALL_ABORT(SCIPfreeClock(scip, &temporaryClock) );
1413 return SCIP_OKAY;
1414 }
1415 }
1416 else
1417 {
1418 tau = detectordata->desiredblocks;
1419 }
1420 }
1421
1422 SCIP_CALL_ABORT( SCIPstopClock(scip, temporaryClock) );
1423 SCIP_Real tempTime = SCIPgetClockTime(scip, temporaryClock);
1424 SCIP_CALL_ABORT( SCIPresetClock(scip, temporaryClock) );
1425 /* variant 2 */
1426 /* dynamic blocking */
1427 SCIP_Real tempTimeDynamicBlocking = 0;
1428 int ndynamicblocking = 0;
1429 if( detectordata->dynamicblocking )
1430 {
1431 SCIP_CALL_ABORT( SCIPstartClock(scip, temporaryClock) );
1432 SCIPdebugMessage("detectordata->enableblockingdynamic = 1.\n");
1433 SCIP_CALL( rowsWithConstriction(scip, partialdec, detectordata) );
1434
1435 SCIPdebugMessage("detectordata->enablemultipledecomps = %ud.\n", detectordata->multipledecomps);
1436
1437 SCIP_CALL_ABORT( SCIPstopClock(scip, temporaryClock) );
1438 tempTimeDynamicBlocking = SCIPgetClockTime(scip, temporaryClock);
1439 SCIP_CALL_ABORT( SCIPresetClock(scip, temporaryClock) );
1440
1441 if( detectordata->multipledecomps )
1442 {
1443 for( tau = detectordata->minblocks; tau <= detectordata->maxblocks; ++tau )
1444 {
1445 SCIP_CALL_ABORT( SCIPstartClock(scip, temporaryClock) );
1446 SCIPdebugMessage("tau = %i, dec = %i\n", tau, *nnewpartialdecs);
1447 SCIP_CALL( resetDetectordata(scip, detectordata) );
1448
1449 SCIP_CALL( blockingDynamic(scip, partialdec, detectordata, tau, nvars) );
1450 if( detectordata->blocks <= 1 )
1451 {
1452 SCIP_CALL_ABORT( SCIPstopClock(scip, temporaryClock) );
1453 SCIP_CALL_ABORT( SCIPresetClock(scip, temporaryClock) );
1454 continue;
1455 }
1456
1457 detectiondata->newpartialdecs[*nnewpartialdecs] = new gcg::PARTIALDECOMP(partialdec);
1458 SCIP_CALL(detectiondata->newpartialdecs[*nnewpartialdecs]->assignPartialdecFromConstoblock(detectordata->constoblock, detectordata->blocks) );
1459 detectiondata->newpartialdecs[*nnewpartialdecs]->assignCurrentStairlinking();
1460
1461 SCIP_CALL_ABORT( SCIPstopClock(scip, temporaryClock) );
1462 clocktimes.push_back(SCIPgetClockTime(scip, temporaryClock));
1463 SCIP_CALL_ABORT( SCIPresetClock(scip, temporaryClock) );
1464
1465 (void) SCIPsnprintf(decinfo, SCIP_MAXSTRLEN, "stairheu_db_md_%d", tau);
1466 detectiondata->newpartialdecs[*nnewpartialdecs]->addDetectorChainInfo(decinfo);
1467
1468 (*nnewpartialdecs) += 1;
1469 ndynamicblocking++;
1470 }
1471 }
1472 else
1473 {
1474 SCIP_CALL_ABORT( SCIPstartClock(scip, temporaryClock) );
1475 SCIPdebugMessage("tau = %i, dec = %i\n", tau, *nnewpartialdecs);
1476 SCIP_CALL( resetDetectordata(scip, detectordata) );
1477
1478 SCIP_CALL( blockingDynamic(scip, partialdec, detectordata, tau, nvars) );
1479 if( detectordata->blocks > 1 )
1480 {
1481 detectiondata->newpartialdecs[*nnewpartialdecs] = new gcg::PARTIALDECOMP(partialdec);
1482 SCIP_CALL(detectiondata->newpartialdecs[*nnewpartialdecs]->assignPartialdecFromConstoblock(detectordata->constoblock, detectordata->blocks) );
1483 detectiondata->newpartialdecs[*nnewpartialdecs]->assignCurrentStairlinking();
1484
1485 SCIP_CALL_ABORT( SCIPstopClock(scip, temporaryClock) );
1486 clocktimes.push_back(SCIPgetClockTime(scip, temporaryClock));
1487 SCIP_CALL_ABORT( SCIPresetClock(scip, temporaryClock) );
1488
1489 (void) SCIPsnprintf(decinfo, SCIP_MAXSTRLEN, "stairheu_db");
1490 detectiondata->newpartialdecs[*nnewpartialdecs]->addDetectorChainInfo(decinfo);
1491
1492 (*nnewpartialdecs) += 1;
1493 ndynamicblocking++;
1494 }
1495 else
1496 {
1497 SCIP_CALL_ABORT( SCIPstopClock(scip, temporaryClock) );
1498 SCIP_CALL_ABORT( SCIPresetClock(scip, temporaryClock) );
1499 }
1500 }
1501 }
1502
1503 /* static blocking */
1504 SCIPdebugMessage("detectordata->staticblocking = %ud. \n", detectordata->staticblocking);
1505
1506 if( detectordata->staticblocking )
1507 {
1508 SCIP_CALL_ABORT( SCIPstartClock(scip, temporaryClock) );
1509 SCIPdebugMessage("nconssperblock = %i, dec = %i\n", detectordata->nconssperblock, *nnewpartialdecs);
1510
1511 SCIP_CALL( resetDetectordata(scip, detectordata) );
1512 SCIP_CALL( blockingStatic(scip, partialdec, detectordata) );
1513
1514 if( detectordata->blocks > 1 )
1515 {
1516 detectiondata->newpartialdecs[*nnewpartialdecs] = new gcg::PARTIALDECOMP(partialdec);
1517 SCIP_CALL(detectiondata->newpartialdecs[*nnewpartialdecs]->assignPartialdecFromConstoblock(detectordata->constoblock, detectordata->blocks) );
1518 detectiondata->newpartialdecs[*nnewpartialdecs]->assignCurrentStairlinking();
1519
1520 SCIP_CALL_ABORT( SCIPstopClock(scip, temporaryClock) );
1521 clocktimes.push_back(SCIPgetClockTime(scip, temporaryClock));
1522 SCIP_CALL_ABORT( SCIPresetClock(scip, temporaryClock) );
1523
1524 (void) SCIPsnprintf(decinfo, SCIP_MAXSTRLEN, "stairheu_sb");
1525 detectiondata->newpartialdecs[*nnewpartialdecs]->addDetectorChainInfo(decinfo);
1526
1527 (*nnewpartialdecs) += 1;
1528 }
1529 else
1530 {
1531 SCIP_CALL_ABORT( SCIPstopClock(scip, temporaryClock) );
1532 SCIP_CALL_ABORT( SCIPresetClock(scip, temporaryClock) );
1533 }
1534 }
1535
1536 /* blocking ASAP */
1537 SCIPdebugMessage("detectordata->blockingassoonaspossible = %ud. \n", detectordata->blockingassoonaspossible);
1538
1539 if( detectordata->blockingassoonaspossible )
1540 {
1541 if( detectordata->multipledecomps )
1542 {
1543 for( tau = detectordata->minblocks; tau <= detectordata->maxblocks; ++tau )
1544 {
1545 SCIP_CALL_ABORT( SCIPstartClock(scip, temporaryClock) );
1546 SCIPdebugMessage("tau = %i, dec = %i\n", tau, *nnewpartialdecs);
1547 SCIP_CALL( resetDetectordata(scip, detectordata) );
1548
1549 SCIP_CALL( blockingAsSoonAsPossible(scip, detectordata, tau, nvars) );
1550 if( detectordata->blocks <= 1)
1551 {
1552 SCIP_CALL_ABORT( SCIPstopClock(scip, temporaryClock) );
1553 SCIP_CALL_ABORT( SCIPresetClock(scip, temporaryClock) );
1554 continue;
1555 }
1556
1557 detectiondata->newpartialdecs[*nnewpartialdecs] = new gcg::PARTIALDECOMP(partialdec);
1558 SCIP_CALL(detectiondata->newpartialdecs[*nnewpartialdecs]->assignPartialdecFromConstoblock(detectordata->constoblock, detectordata->blocks) );
1559 detectiondata->newpartialdecs[*nnewpartialdecs]->assignCurrentStairlinking();
1560
1561 SCIP_CALL_ABORT( SCIPstopClock(scip, temporaryClock) );
1562 clocktimes.push_back(SCIPgetClockTime(scip, temporaryClock));
1563 SCIP_CALL_ABORT( SCIPresetClock(scip, temporaryClock) );
1564
1565 (void) SCIPsnprintf(decinfo, SCIP_MAXSTRLEN, "stairheu_asap_md_%d", tau);
1566 detectiondata->newpartialdecs[*nnewpartialdecs]->addDetectorChainInfo(decinfo);
1567
1568 *nnewpartialdecs += 1;
1569 }
1570 }
1571 else
1572 {
1573 SCIP_CALL_ABORT( SCIPstartClock(scip, temporaryClock) );
1574 SCIPdebugMessage("tau = %i, dec = %i\n", tau, *nnewpartialdecs);
1575 SCIP_CALL( resetDetectordata(scip, detectordata) );
1576 SCIP_CALL( blockingAsSoonAsPossible(scip, detectordata, tau, nvars) );
1577 if( detectordata->blocks > 1)
1578 {
1579 detectiondata->newpartialdecs[*nnewpartialdecs] = new gcg::PARTIALDECOMP(partialdec);
1580 SCIP_CALL(detectiondata->newpartialdecs[*nnewpartialdecs]->assignPartialdecFromConstoblock(detectordata->constoblock, detectordata->blocks) );
1581 detectiondata->newpartialdecs[*nnewpartialdecs]->assignCurrentStairlinking();
1582 if( detectordata->constoblock != NULL )
1583 SCIPhashmapFree( &detectordata->constoblock );
1584 detectordata->constoblock = NULL;
1585
1586 SCIP_CALL_ABORT( SCIPstopClock(scip, temporaryClock) );
1587 clocktimes.push_back(SCIPgetClockTime(scip, temporaryClock));
1588 SCIP_CALL_ABORT( SCIPresetClock(scip, temporaryClock) );
1589
1590 (void) SCIPsnprintf(decinfo, SCIP_MAXSTRLEN, "stairheu_asap");
1591 detectiondata->newpartialdecs[*nnewpartialdecs]->addDetectorChainInfo(decinfo);
1592
1593 *nnewpartialdecs += 1;
1594 }
1595 else
1596 {
1597 SCIP_CALL_ABORT( SCIPstopClock(scip, temporaryClock) );
1598 SCIP_CALL_ABORT( SCIPresetClock(scip, temporaryClock) );
1599 }
1600 }
1601 }
1602
1603 SCIP_Real timeperdec = (time + tempTime) / detectiondata->nnewpartialdecs;
1604 SCIP_Real timeperdecdyn = tempTimeDynamicBlocking / ndynamicblocking;
1605 assert(detectiondata->nnewpartialdecs == (int) clocktimes.size());
1606 for( int i = 0; i < detectiondata->nnewpartialdecs; ++i)
1607 {
1608 if( i < ndynamicblocking )
1609 {
1610 detectiondata->newpartialdecs[i]->addClockTime(timeperdec + timeperdecdyn + clocktimes[i]);
1611 }
1612 else
1613 {
1614 detectiondata->newpartialdecs[i]->addClockTime(timeperdec + clocktimes[i]);
1615 }
1616 }
1617 SCIP_CALL_ABORT(SCIPfreeClock(scip, &temporaryClock) );
1618 return SCIP_OKAY;
1619}
1620
1621
1622/** destructor of detector to free user data (called when GCG is exiting) */
1623static
1624GCG_DECL_FREEDETECTOR(detectorFreeStairheur)
1625{
1626 GCG_DETECTORDATA* detectordata;
1627
1628 detectordata = GCGdetectorGetData(detector);
1629 assert(detectordata != NULL);
1630
1631 if (detectordata->constoblock != NULL)
1632 {
1633 SCIPhashmapFree(&detectordata->constoblock);
1634 detectordata->constoblock = NULL;
1635 }
1636
1637 assert(strcmp(GCGdetectorGetName(detector), DEC_NAME) == 0);
1638
1639 SCIPfreeMemory(GCGgetOrigprob(gcg), &detectordata);
1640 return SCIP_OKAY;
1641}
1642
1643/** detector initialization method (called after problem was transformed) */
1644static
1645GCG_DECL_INITDETECTOR(detectorInitStairheur)
1646{
1647 GCG_DETECTORDATA* detectordata;
1648
1649 detectordata = GCGdetectorGetData(detector);
1650 assert(detectordata != NULL);
1651
1652 detectordata->constoblock = NULL;
1653 detectordata->ibegin = NULL;
1654 detectordata->iend = NULL;
1655 detectordata->jbegin = NULL;
1656 detectordata->jend = NULL;
1657 detectordata->jmin = NULL;
1658 detectordata->jmax = NULL;
1659 detectordata->minV = NULL;
1660 detectordata->width = NULL;
1661 detectordata->hashmapindices = NULL;
1662 detectordata->indexmap = NULL;
1663 detectordata->rowsWithConstrictions = NULL;
1664 detectordata->blockedAfterrow = NULL;
1665
1666 return SCIP_OKAY;
1667}
1668
1669
1670static GCG_DECL_PROPAGATEPARTIALDEC(detectorPropagatePartialdecStairheur)
1671{
1672 int i;
1673 int nconss; /* number of constraints in the problem */
1674 int nPartialdecs;
1675 vector<vector<int> > rowindices;
1676 vector<vector<int> > columnindices;
1677 GCG_DETECTORDATA* detectordata = GCGdetectorGetData(detector);
1678 gcg::PARTIALDECOMP* partialdec;
1679 SCIP_Real temptime;
1680 SCIP* origprob = GCGgetOrigprob(gcg);
1681
1682 SCIP_CLOCK* temporaryClock;
1683 SCIP_CALL_ABORT( SCIPcreateClock(origprob, &temporaryClock) );
1684 SCIP_CALL_ABORT( SCIPstartClock(origprob, temporaryClock) );
1685
1686 partialdec = partialdecdetectiondata->workonpartialdec;
1687 partialdec->refineToMaster();
1688
1689#ifdef WRITEALLOUTPUT
1690 int ROC_iterations;
1691 SCIPwarningMessage(scip, "WRITEALLOUTPUT in detector stairheur is not implemented for partialdecs.\n");
1692#endif
1693
1694 if( partialdec->getNOpenconss() == 0 || partialdec->getNOpenvars() == 0 )
1695 {
1696 partialdecdetectiondata->nnewpartialdecs = 0;
1697 SCIP_CALL_ABORT( SCIPstopClock(origprob, temporaryClock ) );
1698 SCIP_CALL_ABORT(SCIPfreeClock(origprob, &temporaryClock) );
1699 *result = SCIP_SUCCESS;
1700 return SCIP_OKAY;
1701 }
1702
1703 assert(origprob != NULL);
1704 assert(detectordata != NULL);
1705
1706 SCIPverbMessage(origprob, SCIP_VERBLEVEL_NORMAL, NULL, "Detecting stairheur structure:");
1707
1708 SCIP_CALL( initData(origprob, partialdec, detectordata) );
1709
1710 checkParameterConsistency(origprob, detectordata, result);
1711 nPartialdecs = calculateNdecompositions(detectordata);
1712 SCIPdebugMessage("%i decompositions will be created\n", nPartialdecs);
1713 partialdecdetectiondata->nnewpartialdecs = 0;
1714
1715 /* allocate space for output data */
1716 SCIP_CALL( SCIPallocMemoryArray(origprob, &(partialdecdetectiondata->newpartialdecs), nPartialdecs) );
1717
1718 nconss = partialdec->getNOpenconss();
1719
1720 /* initialize hash maps for keeping track of variables and constraints and their corresponding indices after being permuted by the ROC2-algorithm */
1721 SCIP_CALL( indexmapInit(detectordata->indexmap, partialdec, detectordata->hashmapindices) );
1722
1723#ifdef WRITEALLOUTPUT
1724 {
1725 //char filename[256];
1726 //sprintf(filename, "%s_initial_problem", getProbNameWithoutPath(scip));
1727 }
1728#endif
1729
1730 /* initialize index arrays ibegin, iend, jbegin, jend */
1731 SCIP_CALL( createRowindexList(origprob, partialdec, partialdecdetectiondata->detprobdata, detectordata, detectordata->indexmap->indexcons, detectordata->indexmap->varindex, rowindices) );
1732
1733 SCIP_CALL( createColumnindexList(origprob, partialdec, rowindices, columnindices) );
1734
1735 SCIP_CALL( formIndexArray(detectordata->ibegin, detectordata->iend, rowindices) );
1736 SCIP_CALL( formIndexArray(detectordata->jbegin, detectordata->jend, columnindices) );
1737
1738 /* ==================== */
1739 /* ===ROC2 algorithm=== */
1740 /* ==================== */
1741 SCIPdebugMessage("starting ROC2 algorithm\n");
1742
1743
1744#ifdef WRITEALLOUTPUT
1745 SCIP_CALL( rankOrderClustering(scip, partialdec, partialdecdetectiondata->detprobdata, detectordata, detectordata->maxiterationsROC, &ROC_iterations) );
1746
1747 /* check conditions for arrays ibegin and jbegin: ibegin[i]<=ibegin[i+k] for all positive k */
1748 //if( ROC_iterations < detectordata->maxiterationsROC || detectordata->maxiterationsROC == -1 )
1749 //{
1750 // checkConsistencyOfIndexarrays(detectordata, partialdec->getNOpenvars(), nconss);
1751 //}
1752#else
1753 SCIP_CALL( rankOrderClustering(origprob, partialdec, partialdecdetectiondata->detprobdata, detectordata, detectordata->maxiterationsROC, NULL) );
1754#endif
1755 /* arrays jmin, jmax and minV */
1756 SCIPdebugMessage("calculating index arrays\n");
1757 detectordata->jmin[0] = detectordata->ibegin[0];
1758 detectordata->jmax[0] = detectordata->iend[0];
1759 detectordata->width[0] = detectordata->iend[0] - detectordata->ibegin[0];
1760 for( i = 1; i < nconss; ++i )
1761 {
1762 detectordata->width[i] = detectordata->iend[i] - detectordata->ibegin[i];
1763 detectordata->jmin[i] = detectordata->ibegin[i];
1764 detectordata->jmax[i] = MAX(detectordata->iend[i], detectordata->jmax[i-1]);
1765 detectordata->minV[i-1]=1 + (detectordata->jmax[i-1] - detectordata->jmin[i]);
1766 }
1767 /* ==================== */
1768 /* =====BLOCKING======= */
1769 /* ==================== */
1770
1771 SCIP_CALL_ABORT( SCIPstopClock(origprob, temporaryClock) );
1772 temptime = SCIPgetClockTime(origprob, temporaryClock);
1773 SCIP_CALL_ABORT( SCIPstartClock(origprob, temporaryClock) );
1774 SCIP_CALL( blocking(origprob, detectordata, partialdec, partialdecdetectiondata, temptime, nPartialdecs, result) );
1775 SCIP_CALL_ABORT( SCIPstopClock(origprob, temporaryClock ) );
1776 partialdecdetectiondata->detectiontime = SCIPgetClockTime(origprob, temporaryClock);
1777 SCIP_CALL_ABORT(SCIPfreeClock(origprob, &temporaryClock) );
1778 SCIPverbMessage(origprob, SCIP_VERBLEVEL_NORMAL, NULL, " found %d partialdecs.\n", partialdecdetectiondata->nnewpartialdecs);
1779 #ifdef WRITEALLOUTPUT
1780 {
1781 //char filename[256];
1782 //sprintf(filename, "%s_ROC", getProbNameWithoutPath(scip));
1783 //plotInitialProblem(scip, detectordata, filename);
1784 }
1785#endif
1786
1787 SCIP_CALL( SCIPreallocMemoryArray(origprob, &(partialdecdetectiondata->newpartialdecs), partialdecdetectiondata->nnewpartialdecs) );
1788
1789 SCIP_CALL( freeData(origprob, detectordata) );
1790
1791 *result = SCIP_SUCCESS;
1792 return SCIP_OKAY;
1793}
1794#define detectorExitStairheur NULL
1795#define detectorFinishPartialdecStairheur NULL
1796#define detectorPostprocessPartialdecStairheur NULL
1797
1798
1799static
1800GCG_DECL_SETPARAMFAST(setParamAggressiveStairheur)
1801{
1802 char setstr[SCIP_MAXSTRLEN];
1803 const char* name = GCGdetectorGetName(detector);
1804 SCIP* origprob = GCGgetOrigprob(gcg);
1805
1806 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
1807 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, TRUE) );
1808
1809 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
1810 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, FALSE ) );
1811
1812 return SCIP_OKAY;
1813}
1814
1815
1816static
1817GCG_DECL_SETPARAMFAST(setParamDefaultStairheur)
1818{
1819 char setstr[SCIP_MAXSTRLEN];
1820 const char* name = GCGdetectorGetName(detector);
1821 SCIP* origprob = GCGgetOrigprob(gcg);
1822
1823 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
1824 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, DEC_ENABLED) );
1825
1826 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
1827 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, DEC_ENABLEDFINISHING ) );
1828
1829 return SCIP_OKAY;
1830}
1831
1832
1833static
1834GCG_DECL_SETPARAMFAST(setParamFastStairheur)
1835{
1836 char setstr[SCIP_MAXSTRLEN];
1837 const char* name = GCGdetectorGetName(detector);
1838 SCIP* origprob = GCGgetOrigprob(gcg);
1839
1840 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/enabled", name);
1841 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, FALSE) );
1842
1843 (void) SCIPsnprintf(setstr, SCIP_MAXSTRLEN, "detection/detectors/%s/finishingenabled", name);
1844 SCIP_CALL( SCIPsetBoolParam(origprob, setstr, FALSE ) );
1845
1846 return SCIP_OKAY;
1847}
1848
1849
1850/** creates the stairheur detector and includes it in SCIP */
1851
1852extern "C"
1854 GCG* gcg /**< GCG data structure */
1855 )
1856{
1857 GCG_DETECTORDATA *detectordata = NULL;
1858 SCIP* origprob = GCGgetOrigprob(gcg);
1859 assert(origprob != NULL);
1860 assert(origprob != NULL);
1861
1862 SCIP_CALL( SCIPallocMemory(origprob, &detectordata) );
1863 assert(detectordata != NULL);
1864
1865 detectordata->constoblock = NULL;
1866
1868 detectordata, detectorFreeStairheur, detectorInitStairheur, detectorExitStairheur, detectorPropagatePartialdecStairheur, detectorFinishPartialdecStairheur, detectorPostprocessPartialdecStairheur, setParamAggressiveStairheur, setParamDefaultStairheur, setParamFastStairheur) );
1869
1870
1871 /* add stairheur detector parameters */
1872 SCIP_CALL( SCIPaddIntParam(origprob, "detection/detectors/stairheur/nconssperblock",
1873 "The number of constraints per block (static blocking only)",
1874 &detectordata->nconssperblock, FALSE, DEFAULT_NCONSSPERBLOCK, 2, 1000000, NULL, NULL) );
1875 SCIP_CALL( SCIPaddIntParam(origprob, "detection/detectors/stairheur/maxblocks",
1876 "The maximal number of blocks",
1877 &detectordata->usermaxblocks, FALSE, DEFAULT_MAXBLOCKS, 2, 1000000, NULL, NULL) );
1878 SCIP_CALL( SCIPaddIntParam(origprob, "detection/detectors/stairheur/minblocks", "The minimal number of blocks",
1879 &detectordata->userminblocks, FALSE, DEFAULT_MINBLOCKS, 2, 1000000, NULL, NULL) );
1880 SCIP_CALL( SCIPaddIntParam(origprob, "detection/detectors/stairheur/desiredblocks",
1881 "The desired number of blocks. 0 means automatic determination of the number of blocks.",
1882 &detectordata->desiredblocks, FALSE, DEFAULT_DESIREDBLOCKS, 0, 1000000, NULL, NULL) );
1883 SCIP_CALL( SCIPaddBoolParam(origprob, "detection/detectors/stairheur/dynamicblocking",
1884 "Enable blocking type 'dynamic'",
1886 SCIP_CALL( SCIPaddBoolParam(origprob, "detection/detectors/stairheur/staticblocking",
1887 "Enable blocking type 'static'",
1888 &detectordata->staticblocking, FALSE, DEFAULT_STATICBLOCKING, NULL, NULL) );
1889 SCIP_CALL( SCIPaddBoolParam(origprob, "detection/detectors/stairheur/blockingassoonaspossible",
1890 "Enable blocking type 'as soon as possible", &detectordata->blockingassoonaspossible,
1892 SCIP_CALL( SCIPaddBoolParam(origprob, "detection/detectors/stairheur/multipledecomps",
1893 "Enables multiple decompositions for all enabled blocking types. Ranging from minblocks to maxblocks",
1895 SCIP_CALL( SCIPaddIntParam(origprob, "detection/detectors/stairheur/maxiterationsROC",
1896 "The maximum number of iterations of the ROC-algorithm. -1 for no limit",
1897 &detectordata->maxiterationsROC, FALSE, DEFAULT_MAXITERATIONSROC, -1, 1000000, NULL, NULL) );
1898
1899 return SCIP_OKAY;
1900}
class storing partialdecs and the problem matrix
class storing (potentially incomplete) decompositions
GCG_EXPORT int getNVarsForCons(int consIndex)
returns the number of variables for a given constraint
GCG_EXPORT std::vector< int > & getVarsForCons(int consIndex)
returns the variable indices of the coefficient matrix for a constraint
class to manage partial decompositions
GCG_EXPORT void refineToMaster()
refine partialdec with focus on master
GCG_EXPORT int getNOpenvars()
Gets size of vector containing variables not assigned yet.
GCG_EXPORT void addDetectorChainInfo(const char *decinfo)
add information about the detector chain
GCG_EXPORT int getNOpenconss()
Gets size of vector containing constraints not assigned yet.
GCG_EXPORT bool isVarOpenvar(int var)
Checks whether the var is an open var.
GCG_EXPORT SCIP_RETCODE assignPartialdecFromConstoblock(SCIP_HASHMAP *constoblock, int additionalNBlocks)
assigns conss structure according to given hashmap
GCG_EXPORT void addClockTime(SCIP_Real clocktime)
adds detection time of one detector
GCG_EXPORT std::vector< int > & getOpenconss()
Gets vector containing constraints not assigned yet.
GCG_EXPORT std::vector< int > & getOpenvars()
Gets vector containing variables not assigned yet.
GCG_EXPORT bool assignCurrentStairlinking()
assigns open vars to stairlinking if appropriate
const char * GCGdetectorGetName(GCG_DETECTOR *detector)
returns the name of the provided detector
GCG_DETECTORDATA * GCGdetectorGetData(GCG_DETECTOR *detector)
returns the data of the provided detector
SCIP_RETCODE GCGincludeDetector(GCG *gcg, const char *name, const char decchar, const char *description, int freqCallRound, int maxCallRound, int minCallRound, int freqCallRoundOriginal, int maxCallRoundOriginal, int minCallRoundOriginal, int priority, SCIP_Bool enabled, SCIP_Bool enabledFinishing, SCIP_Bool enabledPostprocessing, SCIP_Bool skip, SCIP_Bool usefulRecall, GCG_DETECTORDATA *detectordata, GCG_DECL_FREEDETECTOR((*freeDetector)), GCG_DECL_INITDETECTOR((*initDetector)), GCG_DECL_EXITDETECTOR((*exitDetector)), GCG_DECL_PROPAGATEPARTIALDEC((*propagatePartialdecDetector)), GCG_DECL_FINISHPARTIALDEC((*finishPartialdecDetector)), GCG_DECL_POSTPROCESSPARTIALDEC((*postprocessPartialdecDetector)), GCG_DECL_SETPARAMAGGRESSIVE((*setParamAggressiveDetector)), GCG_DECL_SETPARAMDEFAULT((*setParamDefaultDetector)),)
constraint handler for structure detection
#define DEFAULT_MAXITERATIONSROC
static SCIP_RETCODE blockingDynamic(SCIP *scip, gcg::PARTIALDECOMP *partialdec, GCG_DETECTORDATA *detectordata, int tau, int nvars)
static SCIP_RETCODE createRowindexList(SCIP *scip, gcg::PARTIALDECOMP *partialdec, gcg::DETPROBDATA *detprobdata, GCG_DETECTORDATA *detectordata, SCIP_HASHMAP *indexcons, SCIP_HASHMAP *varindex, vector< vector< int > > &rowindices)
static vector< int > SCIPvectorCreateInt(int from, int to)
static void indexmapFree(SCIP *scip, INDEXMAP **indexmap)
static SCIP_Bool isValidBlocking(GCG_DETECTORDATA *detectordata, int prev_block_first_row, int prev_block_last_row, int block_at_row)
#define DEFAULT_NCONSSPERBLOCK
static GCG_DECL_SETPARAMFAST(setParamAggressiveStairheur)
#define DEC_MAXCALLROUND
#define DEC_MINCALLROUNDORIGINAL
static SCIP_RETCODE initData(SCIP *scip, gcg::PARTIALDECOMP *partialdec, GCG_DETECTORDATA *detectordata)
static SCIP_RETCODE createColumnindexList(SCIP *scip, gcg::PARTIALDECOMP *partialdec, vector< vector< int > > &rowindices, vector< vector< int > > &columnindices)
static SCIP_RETCODE rankOrderClustering(SCIP *scip, gcg::PARTIALDECOMP *partialdec, gcg::DETPROBDATA *detprobdata, GCG_DETECTORDATA *detectordata, int max_iterations, int *iterations)
#define DEFAULT_MAXBLOCKS
static SCIP_RETCODE resetDetectordata(SCIP *scip, GCG_DETECTORDATA *detectordata)
static GCG_DECL_INITDETECTOR(detectorInitStairheur)
#define detectorPostprocessPartialdecStairheur
#define DEC_ENABLEDFINISHING
#define DEC_ENABLED
static SCIP_RETCODE freeData(SCIP *scip, GCG_DETECTORDATA *detectordata)
#define DEFAULT_DESIREDBLOCKS
static SCIP_RETCODE indexmapCreate(SCIP *scip, INDEXMAP **indexmap, int nconss, int nvars)
#define DEFAULT_STATICBLOCKING
void printnested(vector< vector< int > > l)
#define DEFAULT_DYNAMICBLOCKING
static SCIP_Bool arraysAreEqual(int *new_array, int *old_array, int num_elements)
#define DEC_DECCHAR
#define DEC_NAME
static void checkParameterConsistency(SCIP *scip, GCG_DETECTORDATA *detectordata, SCIP_RESULT *result)
#define DEFAULT_MULTIPLEDECOMPS
static SCIP_RETCODE assignConsToBlock(SCIP *scip, gcg::PARTIALDECOMP *partialdec, GCG_DETECTORDATA *detectordata, int block, int first_cons, int last_cons)
static GCG_DECL_PROPAGATEPARTIALDEC(detectorPropagatePartialdecStairheur)
#define DEC_MINCALLROUND
void printvector(vector< int > l)
static int calculateNdecompositions(GCG_DETECTORDATA *detectordata)
static SCIP_RETCODE indexmapInit(INDEXMAP *indexmap, gcg::PARTIALDECOMP *partialdec, int *hashmapindices)
static vector< int > rowOrdering(SCIP *scip, vector< vector< int > > &columnindices, int nrows)
#define DEC_ENABLEDPOSTPROCESSING
#define DEFAULT_BLOCKINGASSOONASPOSSIBLE
static int getMinColIndex(GCG_DETECTORDATA *detectordata, int row)
static SCIP_RETCODE formIndexArray(int *begin, int *end, vector< vector< int > > &indices)
#define detectorExitStairheur
SCIP_RETCODE GCGincludeDetectorStairheur(GCG *gcg)
#define detectorFinishPartialdecStairheur
#define DEFAULT_MINBLOCKS
#define DEC_DESC
#define DEC_FREQCALLROUND
static vector< int >::iterator nextRowToBlockAt(GCG_DETECTORDATA *detectordata, vector< int >::iterator it_constrictions, vector< int > *it_vector, int min_block_size, int prev_block_first_row, int prev_block_last_row)
static void SCIPvectorRearrange(vector< vector< int > > &l, vector< int > order)
#define DEC_PRIORITY
static SCIP_RETCODE blockingStatic(SCIP *scip, gcg::PARTIALDECOMP *partialdec, GCG_DETECTORDATA *detectordata)
static SCIP_RETCODE rowsWithConstriction(SCIP *scip, gcg::PARTIALDECOMP *partialdec, GCG_DETECTORDATA *detectordata)
#define DEC_USEFULRECALL
static int getMaxColIndex(GCG_DETECTORDATA *detectordata, int from_row, int to_row)
static SCIP_RETCODE blocking(SCIP *scip, GCG_DETECTORDATA *detectordata, gcg::PARTIALDECOMP *partialdec, PARTIALDEC_DETECTION_DATA *detectiondata, SCIP_Real time, int maxndecs, SCIP_RESULT *result)
#define DEC_SKIP
#define DEC_MAXCALLROUNDORIGINAL
static vector< int >::iterator findBlockingCandidate(vector< int >::iterator it_constrictions, vector< int > *it_vector, int min_block_size, int prev_block_last_row)
#define DEC_FREQCALLROUNDORIGINAL
static SCIP_RETCODE rankOrderClusteringIteration(SCIP *scip, gcg::PARTIALDECOMP *partialdec, gcg::DETPROBDATA *detprobdata, GCG_DETECTORDATA *detectordata, INDEXMAP *inputmap, INDEXMAP *outputmap)
static GCG_DECL_FREEDETECTOR(detectorFreeStairheur)
static SCIP_RETCODE blockingAsSoonAsPossible(SCIP *scip, GCG_DETECTORDATA *detectordata, int desired_blocks, int nvars)
detector for staircase structures via ROC algorithms
#define SCIP_MAXSTRLEN
#define SCIP_Bool
#define MIN(x, y)
#define SCIP_Real
#define MAX(x, y)
#define SCIP_CALL_ABORT(x)
#define SCIP_CALL(x)
#define TRUE
#define FALSE
GCG interface methods.
SCIP * GCGgetOrigprob(GCG *gcg)
Definition: gcg.cpp:87
const char * SCIPgetProbName(SCIP *scip)
SCIP_CONS ** SCIPgetConss(SCIP *scip)
int SCIPgetNVars(SCIP *scip)
int SCIPgetNConss(SCIP *scip)
void SCIPhashmapFree(SCIP_HASHMAP **hashmap)
void * SCIPhashmapGetImage(SCIP_HASHMAP *hashmap, void *origin)
SCIP_RETCODE SCIPhashmapInsert(SCIP_HASHMAP *hashmap, void *origin, void *image)
SCIP_RETCODE SCIPhashmapSetImage(SCIP_HASHMAP *hashmap, void *origin, void *image)
SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP **hashmap, BMS_BLKMEM *blkmem, int mapsize)
SCIP_Bool SCIPhashmapExists(SCIP_HASHMAP *hashmap, void *origin)
SCIP_RETCODE SCIPhashmapRemoveAll(SCIP_HASHMAP *hashmap)
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
SCIP_RETCODE SCIPaddIntParam(SCIP *scip, const char *name, const char *desc, int *valueptr, SCIP_Bool isadvanced, int defaultvalue, int minvalue, int maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
SCIP_RETCODE SCIPaddBoolParam(SCIP *scip, const char *name, const char *desc, SCIP_Bool *valueptr, SCIP_Bool isadvanced, SCIP_Bool defaultvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
SCIP_RETCODE SCIPsetBoolParam(SCIP *scip, const char *name, SCIP_Bool value)
SCIP_RETCODE SCIPgetConsNVars(SCIP *scip, SCIP_CONS *cons, int *nvars, SCIP_Bool *success)
SCIP_RETCODE SCIPgetConsVars(SCIP *scip, SCIP_CONS *cons, SCIP_VAR **vars, int varssize, SCIP_Bool *success)
#define SCIPreallocMemoryArray(scip, ptr, newnum)
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
#define SCIPallocMemoryArray(scip, ptr, num)
#define SCIPallocClearBufferArray(scip, ptr, num)
#define SCIPallocMemory(scip, ptr)
#define SCIPfreeBufferArray(scip, ptr)
#define SCIPfreeMemoryArray(scip, ptr)
#define SCIPfreeMemory(scip, ptr)
SCIP_RETCODE SCIPcreateClock(SCIP *scip, SCIP_CLOCK **clck)
SCIP_RETCODE SCIPresetClock(SCIP *scip, SCIP_CLOCK *clck)
SCIP_RETCODE SCIPstopClock(SCIP *scip, SCIP_CLOCK *clck)
SCIP_RETCODE SCIPfreeClock(SCIP *scip, SCIP_CLOCK **clck)
SCIP_Real SCIPgetClockTime(SCIP *scip, SCIP_CLOCK *clck)
SCIP_RETCODE SCIPstartClock(SCIP *scip, SCIP_CLOCK *clck)
SCIP_Real SCIPround(SCIP *scip, SCIP_Real val)
SCIP_VAR * SCIPvarGetProbvar(SCIP_VAR *var)
int SCIPsnprintf(char *t, int len, const char *s,...)
#define NULL
public methods for working with decomposition structures
#define SCIPdebugMessage
#define SCIPdebugPrintf
various SCIP helper methods
SCIP_Bool blockingassoonaspossible
vector< int > * rowsWithConstrictions
vector< int > * blockedAfterrow
SCIP_Bool dynamicblocking
SCIP_Bool multipledecomps
SCIP_Bool staticblocking
SCIP_HASHMAP * constoblock
Definition: struct_gcg.h:55
SCIP_HASHMAP * indexcons
SCIP_HASHMAP * consindex
SCIP_HASHMAP * varindex
SCIP_HASHMAP * indexvar
interface data structure for the detector calling methods
gcg::PARTIALDECOMP ** newpartialdecs
structure information for decomposition information in GCG projects
SCIP_VERBLEVEL_NORMAL
SCIP_DIDNOTRUN
SCIP_DIDNOTFIND
SCIP_SUCCESS
enum SCIP_Result SCIP_RESULT
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE