-
Notifications
You must be signed in to change notification settings - Fork 60
/
compute_symmetry_bliss.cpp
1112 lines (923 loc) · 40.1 KB
/
compute_symmetry_bliss.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* This file is part of the program and library */
/* SCIP --- Solving Constraint Integer Programs */
/* */
/* Copyright (C) 2002-2022 Konrad-Zuse-Zentrum */
/* fuer Informationstechnik Berlin */
/* */
/* SCIP is distributed under the terms of the ZIB Academic License. */
/* */
/* You should have received a copy of the ZIB Academic License */
/* along with SCIP; see the file COPYING. If not visit scipopt.org. */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**@file compute_symmetry_bliss.cpp
* @brief interface for symmetry computations to bliss
* @author Marc Pfetsch
* @author Thomas Rehn
* @author Fabian Wegscheider
*/
/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
#include "compute_symmetry.h"
/* include bliss graph */
#include <bliss/defs.hh>
#include <bliss/graph.hh>
#include <string.h>
#include <vector>
#include <list>
#include <math.h>
#include "scip/expr_var.h"
#include "scip/expr_sum.h"
#include "scip/expr_pow.h"
#include "scip/expr.h"
#include "scip/cons_nonlinear.h"
#include "scip/cons_linear.h"
using std::vector;
/** struct for bliss callback */
struct BLISS_Data
{
SCIP* scip; /**< SCIP pointer */
int npermvars; /**< number of variables for permutations */
int nperms; /**< number of permutations */
int** perms; /**< permutation generators as (nperms x npermvars) matrix */
int nmaxperms; /**< maximal number of permutations */
int maxgenerators; /**< maximal number of generators constructed (= 0 if unlimited) */
};
/* ------------------- map for operator types ------------------- */
/** gets the key of the given element */
static
SCIP_DECL_HASHGETKEY(SYMhashGetKeyOptype)
{ /*lint --e{715}*/
return elem;
}
/** returns TRUE iff both keys are equal
*
* Compare the types of two operators according to their name, level and, in case of power, exponent.
*/
static
SCIP_DECL_HASHKEYEQ(SYMhashKeyEQOptype)
{
SYM_OPTYPE* k1;
SYM_OPTYPE* k2;
k1 = (SYM_OPTYPE*) key1;
k2 = (SYM_OPTYPE*) key2;
/* first check operator name */
if ( SCIPexprGetHdlr(k1->expr) != SCIPexprGetHdlr(k2->expr) )
return FALSE;
/* for pow expressions, also check exponent (TODO should that happen for signpow as well?) */
if ( SCIPisExprPower((SCIP*)userptr, k1->expr )
&& SCIPgetExponentExprPow(k1->expr) != SCIPgetExponentExprPow(k2->expr) ) /*lint !e777*/
return FALSE;
/* if still undecided, take level */
if ( k1->level != k2->level )
return FALSE;
return TRUE;
}
/** returns the hash value of the key */
static
SCIP_DECL_HASHKEYVAL(SYMhashKeyValOptype)
{ /*lint --e{715}*/
SYM_OPTYPE* k;
SCIP_Real exponent;
k = (SYM_OPTYPE*) key;
if ( SCIPisExprPower((SCIP*)userptr, k->expr) )
exponent = SCIPgetExponentExprPow(k->expr);
else
exponent = 1.0;
return SCIPhashTwo(SCIPrealHashCode(exponent), k->level),
(uint64_t) SCIPexprhdlrGetName(SCIPexprGetHdlr(k->expr));
}
/* ------------------- map for constant types ------------------- */
/** gets the key of the given element */
static
SCIP_DECL_HASHGETKEY(SYMhashGetKeyConsttype)
{ /*lint --e{715}*/
return elem;
}
/** returns TRUE iff both keys are equal
*
* Compare two constants according to their values.
*/
static
SCIP_DECL_HASHKEYEQ(SYMhashKeyEQConsttype)
{
SYM_CONSTTYPE* k1;
SYM_CONSTTYPE* k2;
k1 = (SYM_CONSTTYPE*) key1;
k2 = (SYM_CONSTTYPE*) key2;
return (SCIP_Bool)(k1->value == k2->value); /*lint !e777*/
}
/** returns the hash value of the key */
static
SCIP_DECL_HASHKEYVAL(SYMhashKeyValConsttype)
{ /*lint --e{715}*/
SYM_CONSTTYPE* k;
k = (SYM_CONSTTYPE*) key;
return SCIPrealHashCode(k->value);
}
/* ------------------- map for constraint side types ------------------- */
/** gets the key of the given element */
static
SCIP_DECL_HASHGETKEY(SYMhashGetKeyRhstype)
{ /*lint --e{715}*/
return elem;
}
/** returns TRUE iff both keys are equal
*
* Compare two constraint sides according to lhs and rhs.
*/
static
SCIP_DECL_HASHKEYEQ(SYMhashKeyEQRhstype)
{
SYM_RHSTYPE* k1;
SYM_RHSTYPE* k2;
k1 = (SYM_RHSTYPE*) key1;
k2 = (SYM_RHSTYPE*) key2;
if ( k1->lhs != k2->lhs ) /*lint !e777*/
return FALSE;
return (SCIP_Bool)(k1->rhs == k2->rhs); /*lint !e777*/
}
/** returns the hash value of the key */
static
SCIP_DECL_HASHKEYVAL(SYMhashKeyValRhstype)
{ /*lint --e{715}*/
SYM_RHSTYPE* k;
k = (SYM_RHSTYPE*) key;
return SCIPhashTwo(SCIPrealHashCode(k->lhs), SCIPrealHashCode(k->rhs));
}
/** callback function for bliss */
static
void blisshook(
void* user_param, /**< parameter supplied at call to bliss */
unsigned int n, /**< size of aut vector */
const unsigned int* aut /**< automorphism */
)
{
assert( aut != NULL );
assert( user_param != NULL );
BLISS_Data* data = static_cast<BLISS_Data*>(user_param);
assert( data->scip != NULL );
assert( data->npermvars < (int) n );
assert( data->maxgenerators >= 0);
/* make sure we do not generate more that maxgenerators many permutations, if the limit in bliss is not available */
if ( data->maxgenerators != 0 && data->nperms >= data->maxgenerators )
return;
/* copy first part of automorphism */
bool isIdentity = true;
int* p = 0;
if ( SCIPallocBlockMemoryArray(data->scip, &p, data->npermvars) != SCIP_OKAY )
return;
for (int j = 0; j < data->npermvars; ++j)
{
/* convert index of variable-level 0-nodes to variable indices */
p[j] = (int) aut[j];
if ( p[j] != j )
isIdentity = false;
}
/* ignore trivial generators, i.e. generators that only permute the constraints */
if ( isIdentity )
{
SCIPfreeBlockMemoryArray(data->scip, &p, data->npermvars);
return;
}
/* check whether we should allocate space for perms */
if ( data->nmaxperms <= 0 )
{
if ( data->maxgenerators == 0 )
data->nmaxperms = 100; /* seems to cover many cases */
else
data->nmaxperms = data->maxgenerators;
if ( SCIPallocBlockMemoryArray(data->scip, &data->perms, data->nmaxperms) != SCIP_OKAY )
return;
}
else if ( data->nperms >= data->nmaxperms ) /* check whether we need to resize */
{
int newsize = SCIPcalcMemGrowSize(data->scip, data->nperms + 1);
assert( newsize >= data->nperms );
assert( data->maxgenerators == 0 );
if ( SCIPreallocBlockMemoryArray(data->scip, &data->perms, data->nmaxperms, newsize) != SCIP_OKAY )
return;
data->nmaxperms = newsize;
}
data->perms[data->nperms++] = p;
}
/** Creates the nodes in the graph that correspond to variables. Each variable type gets a unique color
*
* @pre graph should be empty when this is called
*/
static
SCIP_RETCODE createVariableNodes(
SCIP* scip, /**< SCIP instance */
bliss::Graph* G, /**< Graph to be constructed */
SYM_MATRIXDATA* matrixdata, /**< data for MIP matrix (also contains the relevant variables) */
int& nnodes, /**< buffer to store number of nodes in graph */
const int& nedges, /**< buffer to store number of edges in graph */
int& nusedcolors /**< buffer to store number of used colors */
)
{
assert( scip != NULL );
assert( G != NULL );
assert( nnodes == 0 );
assert( nedges == 0 );
assert( nusedcolors == 0 );
SCIPdebugMsg(scip, "Creating graph with colored nodes for variables.\n");
/* add nodes for variables */
for (int v = 0; v < matrixdata->npermvars; ++v)
{
const int color = matrixdata->permvarcolors[v];
assert( 0 <= color && color < matrixdata->nuniquevars );
#ifndef NDEBUG
int node = (int) G->add_vertex((unsigned) color);
assert( node == v );
#else
(void) G->add_vertex((unsigned) color);
#endif
++nnodes;
}
/* this is not exactly true, since we skip auxvars, but it doesn't matter if some colors are not used at all */
nusedcolors = matrixdata->nuniquevars;
return SCIP_OKAY;
}
/** Construct linear part of colored graph for symmetry computations
*
* Construct graph:
* - Each variable gets a different node.
* - Each constraint gets a different node.
* - Each matrix coefficient gets a different node that is connected to the two nodes
* corresponding to the respective constraint and variable.
*
* Each different variable, rhs, matrix coefficient gets a different color that is attached to the corresponding entries.
*
* @pre This method assumes that the nodes corresponding to permutation variables are already in the graph and that
* their node number is equal to their index.
*/
static
SCIP_RETCODE fillGraphByLinearConss(
SCIP* scip, /**< SCIP instance */
bliss::Graph* G, /**< Graph to be constructed */
SYM_MATRIXDATA* matrixdata, /**< data for MIP matrix */
int& nnodes, /**< buffer to store number of nodes in graph */
int& nedges, /**< buffer to store number of edges in graph */
int& nusedcolors, /**< buffer to store number of used colors */
SCIP_Bool& success /**< whether the construction was successful */
)
{
assert( nnodes == (int) G->get_nof_vertices() );
assert( nusedcolors <= nnodes );
SCIPdebugMsg(scip, "Filling graph with colored coefficient nodes for linear part.\n");
success = TRUE;
/* add nodes for rhs of constraints */
for (int c = 0; c < matrixdata->nrhscoef; ++c)
{
const int color = matrixdata->rhscoefcolors[c];
assert( 0 <= color && color < matrixdata->nuniquerhs );
#ifndef NDEBUG
int node = (int) G->add_vertex((unsigned) (nusedcolors + color));
assert( node == matrixdata->npermvars + c );
#else
(void) G->add_vertex((unsigned) (matrixdata->nuniquevars + color));
#endif
++nnodes;
}
assert( (int) G->get_nof_vertices() == matrixdata->npermvars + matrixdata->nrhscoef );
nusedcolors += matrixdata->nuniquerhs;
/* Grouping of nodes depends on the number of nodes in the bipartite graph class.
* If there are more variables than constraints, we group by constraints.
* That is, given several variable nodes which are incident to one constraint node by the same color,
* we join these variable nodes to the constraint node by only one intermediate node.
*/
const bool groupByConstraints = matrixdata->nrhscoef < matrixdata->npermvars;
if ( groupByConstraints )
SCIPdebugMsg(scip, "Group intermediate nodes by constraints.\n");
else
SCIPdebugMsg(scip, "Group intermediate nodes by variables.\n");
/* "colored" edges based on all matrix coefficients - loop through ordered matrix coefficients */
int ninternodes;
if ( groupByConstraints )
ninternodes = matrixdata->nrhscoef;
else
ninternodes = matrixdata->npermvars;
int* internodes = NULL;
SCIP_CALL( SCIPallocBufferArray(scip, &internodes, ninternodes) ); /*lint !e530*/
for (int l = 0; l < ninternodes; ++l)
internodes[l] = -1;
/* We pass through the matrix coeficients, grouped by color, i.e., different coefficients. If the coeffients appear
* in the same row or column, it suffices to only generate a single node (depending on groupByConstraints). We store
* this node in the array internodes. In order to avoid reinitialization, we store the node number with increasing
* numbers for each color. The smallest number for the current color is stored in firstcolornodenumber. */
int oldcolor = -1;
#ifndef NDEBUG
SCIP_Real oldcoef = SCIP_INVALID;
#endif
int firstcolornodenumber = -1;
for (int j = 0; j < matrixdata->nmatcoef; ++j)
{
int idx = matrixdata->matidx[j];
assert( 0 <= idx && idx < matrixdata->nmatcoef );
/* find color corresponding to matrix coefficient */
const int color = matrixdata->matcoefcolors[idx];
assert( 0 <= color && color < matrixdata->nuniquemat );
assert( 0 <= matrixdata->matrhsidx[idx] && matrixdata->matrhsidx[idx] < matrixdata->nrhscoef );
assert( 0 <= matrixdata->matvaridx[idx] && matrixdata->matvaridx[idx] < matrixdata->npermvars );
const int rhsnode = matrixdata->npermvars + matrixdata->matrhsidx[idx];
const int varnode = matrixdata->matvaridx[idx];
assert( matrixdata->npermvars <= rhsnode && rhsnode < matrixdata->npermvars + matrixdata->nrhscoef );
assert( rhsnode < (int) G->get_nof_vertices() );
assert( varnode < (int) G->get_nof_vertices() );
/* if we have only one color, we do not need intermediate nodes */
if ( matrixdata->nuniquemat == 1 )
{
G->add_edge((unsigned) varnode, (unsigned) rhsnode);
++nedges;
}
else
{
/* if new group of coefficients has been reached */
if ( color != oldcolor )
{
assert( ! SCIPisEQ(scip, oldcoef, matrixdata->matcoef[idx]) );
oldcolor = color;
firstcolornodenumber = nnodes;
#ifndef NDEBUG
oldcoef = matrixdata->matcoef[idx];
#endif
}
else
assert( SCIPisEQ(scip, oldcoef, matrixdata->matcoef[idx]) );
int varrhsidx;
if ( groupByConstraints )
varrhsidx = matrixdata->matrhsidx[idx];
else
varrhsidx = matrixdata->matvaridx[idx];
assert( 0 <= varrhsidx && varrhsidx < ninternodes );
if ( internodes[varrhsidx] < firstcolornodenumber )
{
internodes[varrhsidx] = (int) G->add_vertex((unsigned) (nusedcolors + color));
++nnodes;
}
assert( internodes[varrhsidx] >= matrixdata->npermvars + matrixdata->nrhscoef );
assert( internodes[varrhsidx] >= firstcolornodenumber );
/* determine whether graph would be too large for bliss (can only handle int) */
if ( nnodes >= INT_MAX/2 )
{
success = FALSE;
break;
}
G->add_edge((unsigned) varnode, (unsigned) internodes[varrhsidx]);
G->add_edge((unsigned) rhsnode, (unsigned) internodes[varrhsidx]);
nedges += 2;
}
}
nusedcolors += matrixdata->nuniquemat;
SCIPfreeBufferArray(scip, &internodes);
return SCIP_OKAY;
}
/** Construct non-linear part of colored graph for symmetry computations
*
* Construct graph:
* - Each node of the expression trees gets a different node.
* - Each coefficient of a sum expression gets its own node connected to the node of the corresponding child.
* - Each constraint (with lhs and (!) rhs) gets its own node connected to the corresponding node of the root expression.
*
* @note: In contrast to the linear part, lhs and rhs are treated together here, so that each unique combination of lhs
* and rhs gets its own node. This makes the implementation a lot simpler with the small downside, that different
* formulations of the same constraints would not be detected as equivalent, e.g. for
* 0 <= x1 + x2 <= 1
* 0 <= x3 + x4
* x3 + x4 <= 1
* there would be no symmetry between (x1,x2) and (x3,x4) detected.
*
* Each different constraint (sides), sum-expression coefficient, constant and operator type gets a
* different color that is attached to the corresponding entries.
*
* @pre This method assumes that the nodes corresponding to permutation variables are already in the graph and that
* their node number is equal to their index.
*/
static
SCIP_RETCODE fillGraphByNonlinearConss(
SCIP* scip, /**< SCIP instance */
bliss::Graph* G, /**< Graph to be constructed */
SYM_EXPRDATA* exprdata, /**< data for nonlinear constraints */
int& nnodes, /**< buffer to store number of nodes in graph */
int& nedges, /**< buffer to store number of edges in graph */
int& nusedcolors, /**< number of used colors ind the graph so far */
SCIP_Bool& success /**< whether the construction was successful */
)
{
SCIP_HASHTABLE* optypemap;
SCIP_HASHTABLE* consttypemap;
SCIP_HASHTABLE* sumcoefmap;
SCIP_HASHTABLE* rhstypemap;
SYM_OPTYPE* uniqueoparray = NULL;
SYM_CONSTTYPE* uniqueconstarray = NULL;
SYM_CONSTTYPE* sumcoefarray = NULL;
SYM_RHSTYPE* uniquerhsarray = NULL;
SCIP_CONSHDLR* conshdlr;
SCIP_CONS** conss;
int nconss;
int nuniqueops = 0;
int nuniqueconsts = 0;
int nuniquecoefs = 0;
int nuniquerhs = 0;
int oparraysize = exprdata->nuniqueoperators;
int constarraysize = exprdata->nuniqueconstants;
int coefarraysize = exprdata->nuniquecoefs;
int rhsarraysize;
assert( scip != NULL );
assert( G != NULL );
assert( exprdata != NULL );
assert( nnodes == (int) G->get_nof_vertices() );
assert( nnodes >= nusedcolors );
success = TRUE; /*lint !e838*/
conshdlr = SCIPfindConshdlr(scip, "nonlinear");
nconss = conshdlr != NULL ? SCIPconshdlrGetNConss(conshdlr) : 0;
if ( nconss == 0 )
return SCIP_OKAY;
conss = SCIPconshdlrGetConss(conshdlr);
rhsarraysize = nconss;
SCIPdebugMsg(scip, "Filling graph with colored coefficient nodes for non-linear part.\n");
/* create maps for optypes, constants, sum coefficients and rhs to indices */
SCIP_CALL( SCIPhashtableCreate(&optypemap, SCIPblkmem(scip), oparraysize, SYMhashGetKeyOptype,
SYMhashKeyEQOptype, SYMhashKeyValOptype, (void*) scip) );
SCIP_CALL( SCIPhashtableCreate(&consttypemap, SCIPblkmem(scip), constarraysize, SYMhashGetKeyConsttype,
SYMhashKeyEQConsttype, SYMhashKeyValConsttype, (void*) scip) );
SCIP_CALL( SCIPhashtableCreate(&sumcoefmap, SCIPblkmem(scip), coefarraysize, SYMhashGetKeyConsttype,
SYMhashKeyEQConsttype, SYMhashKeyValConsttype, (void*) scip) );
SCIP_CALL( SCIPhashtableCreate(&rhstypemap, SCIPblkmem(scip), rhsarraysize, SYMhashGetKeyRhstype,
SYMhashKeyEQRhstype, SYMhashKeyValRhstype, (void*) scip) );
assert( optypemap != NULL );
assert( consttypemap != NULL );
assert( sumcoefmap != NULL );
assert( rhstypemap != NULL );
/* allocate space for mappings from optypes, constants, sum coefficients and rhs to colors */
SCIP_CALL( SCIPallocBlockMemoryArray(scip, &uniqueoparray, oparraysize) );
SCIP_CALL( SCIPallocBlockMemoryArray(scip, &uniqueconstarray, constarraysize) );
SCIP_CALL( SCIPallocBlockMemoryArray(scip, &sumcoefarray, coefarraysize) );
SCIP_CALL( SCIPallocBlockMemoryArray(scip, &uniquerhsarray, rhsarraysize) );
SCIP_EXPRITER* it;
SCIP_CALL( SCIPcreateExpriter(scip, &it) );
/* iterate over all expressions and add the corresponding nodes to the graph */
for (int i = 0; i < nconss; ++i)
{
SCIP_EXPR* rootexpr;
vector<int> visitednodes(0);
vector<SCIP_Bool> ischildofsum(0);
int currentlevel = 0;
rootexpr = SCIPgetExprNonlinear(conss[i]);
SCIP_CALL( SCIPexpriterInit(it, rootexpr, SCIP_EXPRITER_DFS, TRUE) );
SCIPexpriterSetStagesDFS(it, SCIP_EXPRITER_ENTEREXPR | SCIP_EXPRITER_LEAVEEXPR);
for (SCIP_EXPR* expr = SCIPexpriterGetCurrent(it); !SCIPexpriterIsEnd(it); expr = SCIPexpriterGetNext(it)) /*lint !e441*/ /*lint !e440*/
{
switch( SCIPexpriterGetStageDFS(it) )
{
/* upon entering an expression, check its type and add nodes and edges if neccessary */
case SCIP_EXPRITER_ENTEREXPR:
{
int node = -1;
int parentnode = -1;
int color = -1;
/* for variable expressions, get the corresponding node that is already in the graph */
if ( SCIPisExprVar(scip, expr) )
{
SCIP_VAR* var = SCIPgetVarExprVar(expr);
/* check whether the variable is active; if not, then replace the inactive variable by its aggregation
* or its fixed value; note that this step is equivalent as representing an inactive variable as sum
* expression
*/
if ( SCIPvarIsActive(var) )
{
node = SCIPvarGetProbindex(var);
assert( node < (int) G->get_nof_vertices() );
}
else
{
SCIP_VAR** vars = NULL;
SCIP_Real* vals = NULL;
SCIP_Real constant = 0;
int varsize = 1;
int requiredsize;
int k;
SCIP_CALL( SCIPallocBufferArray(scip, &vars, varsize) );
SCIP_CALL( SCIPallocBufferArray(scip, &vals, varsize) );
vars[0] = var;
vals[0] = 1.0;
SCIP_CALL( SCIPgetProbvarLinearSum(scip, vars, vals, &varsize, varsize, &constant, &requiredsize, TRUE) );
if ( requiredsize > varsize )
{
SCIP_CALL( SCIPreallocBufferArray(scip, &vars, requiredsize) );
SCIP_CALL( SCIPreallocBufferArray(scip, &vals, requiredsize) );
SCIP_CALL( SCIPgetProbvarLinearSum(scip, vars, vals, &varsize, requiredsize, &constant, &requiredsize, TRUE) );
assert( requiredsize <= varsize );
}
parentnode = visitednodes[visitednodes.size() - 1];
assert( parentnode < (int) G->get_nof_vertices() );
/* create nodes for all aggregation variables and coefficients and connect them to the parent node */
for ( k = 0; k < requiredsize; ++k )
{
SYM_CONSTTYPE* ct;
int internode;
assert( vars[k] != NULL );
assert( vals[k] != 0.0 );
assert( nuniquecoefs < coefarraysize );
ct = &sumcoefarray[nuniquecoefs];
ct->value = vals[k];
if ( !SCIPhashtableExists(sumcoefmap, (void *) ct) )
{
SCIP_CALL( SCIPhashtableInsert(sumcoefmap, (void *) ct) );
ct->color = nusedcolors++;
color = ct->color;
nuniquecoefs++;
}
else
{
color = ((SYM_CONSTTYPE*) SCIPhashtableRetrieve(sumcoefmap, (void *) ct))->color;
}
/* add the intermediate node with the corresponding color */
internode = (int) G->add_vertex((unsigned) color);
++nnodes;
assert( internode < (int) G->get_nof_vertices() );
G->add_edge((unsigned) internode, (unsigned) parentnode);
++nedges;
/* connect the intermediate node to its corresponding variable node */
node = SCIPvarGetProbindex(vars[k]);
assert( node < (int) G->get_nof_vertices() );
G->add_edge((unsigned) node, (unsigned) internode);
++nedges;
}
/* add the node for the constant */
if ( constant != 0.0 )
{
SYM_CONSTTYPE* ct;
/* check whether we have to resize */
if ( nuniqueconsts >= constarraysize )
{
int newsize = SCIPcalcMemGrowSize(scip, nuniqueconsts+1);
assert( newsize >= 0 );
SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &uniqueconstarray, constarraysize, newsize) );
constarraysize = newsize;
}
assert( nuniqueconsts < constarraysize );
ct = &uniqueconstarray[nuniqueconsts];
ct->value = constant;
if ( !SCIPhashtableExists(consttypemap, (void *) ct) )
{
SCIP_CALL( SCIPhashtableInsert(consttypemap, (void *) ct) );
ct->color = nusedcolors++;
color = ct->color;
nuniqueconsts++;
}
else
{
color = ((SYM_CONSTTYPE*) SCIPhashtableRetrieve(consttypemap, (void *) ct))->color;
}
/* add the node with a new color */
node = (int) G->add_vertex((unsigned) color);
++nnodes;
assert( node < (int) G->get_nof_vertices() );
G->add_edge((unsigned) node, (unsigned) parentnode);
++nedges;
}
SCIPfreeBufferArray(scip, &vals);
SCIPfreeBufferArray(scip, &vars);
/* add a filler node since it will be removed in the next iteration anyway */
visitednodes.push_back(nnodes);
ischildofsum.push_back(FALSE);
++currentlevel;
break;
}
}
/* for constant expressions, get the color of its type (value) or assign a new one */
else if ( SCIPisExprValue(scip, expr) )
{
SYM_CONSTTYPE* ct;
assert( nuniqueconsts < constarraysize );
ct = &uniqueconstarray[nuniqueconsts];
ct->value = SCIPgetValueExprValue(expr);
if ( !SCIPhashtableExists(consttypemap, (void *) ct) )
{
SCIP_CALL( SCIPhashtableInsert(consttypemap, (void *) ct) );
ct->color = nusedcolors++;
color = ct->color;
nuniqueconsts++;
}
else
{
color = ((SYM_CONSTTYPE*) SCIPhashtableRetrieve(consttypemap, (void *) ct))->color;
}
}
/* for all other expressions, get the color of its operator type or assign a new one */
else
{
SYM_OPTYPE* ot;
assert( nuniqueops < oparraysize );
ot = &uniqueoparray[nuniqueops];
ot->expr = expr;
ot->level = currentlevel;
if ( !SCIPhashtableExists(optypemap, (void *) ot) )
{
SCIP_CALL( SCIPhashtableInsert(optypemap, (void *) ot) );
ot->color = nusedcolors++;
color = ot->color;
nuniqueops++;
}
else
{
color = ((SYM_OPTYPE*) SCIPhashtableRetrieve(optypemap, (void *) ot))->color;
}
}
/* if this is the root expression, add the constraint side node (will be parent of expression node) */
if ( SCIPexpriterGetParentDFS(it) == NULL )
{
/* add the node corresponding to the constraint */
SYM_RHSTYPE* rt;
int parentcolor;
assert( nuniquerhs < rhsarraysize );
rt = &uniquerhsarray[nuniquerhs];
rt->lhs = SCIPgetLhsNonlinear(conss[i]);
rt->rhs = SCIPgetRhsNonlinear(conss[i]);
if ( !SCIPhashtableExists(rhstypemap, (void *) rt) )
{
SCIP_CALL( SCIPhashtableInsert(rhstypemap, (void *) rt) );
rt->color = nusedcolors++;
parentcolor = rt->color;
nuniquerhs++;
}
else
{
parentcolor = ((SYM_RHSTYPE*) SCIPhashtableRetrieve(rhstypemap, (void *) rt))->color;
}
/* add the constraint side node with the corresponding color */
parentnode = (int) G->add_vertex((unsigned) parentcolor);
++nnodes;
assert( parentnode < (int) G->get_nof_vertices() );
}
/* otherwise, get the parentnode stored in visitednodes */
else
{
parentnode = visitednodes[visitednodes.size() - 1];
assert( parentnode < (int) G->get_nof_vertices() );
}
/* in all cases apart from variable expressions, the new node is added with the corresponding color */
if ( color != -1 )
{
node = (int) G->add_vertex((unsigned) color);
++nnodes;
assert( node < (int) G->get_nof_vertices() );
}
/* store the new node so that it can be used as parentnode later */
assert( node != -1 );
visitednodes.push_back(node);
ischildofsum.push_back(FALSE);
/* connect the current node with its parent */
assert( parentnode != -1 );
G->add_edge((unsigned) node, (unsigned) parentnode);
++nedges;
/* for sum expression, also add intermediate nodes for the coefficients */
if ( SCIPisExprSum(scip, expr) )
{
SCIP_Real* coefs = SCIPgetCoefsExprSum(expr);
int internode;
/* iterate over children from last to first, such that visitednodes array is in correct order */
for (int j = SCIPexprGetNChildren(expr) - 1; j >= 0; --j)
{
SYM_CONSTTYPE* ct;
assert( nuniquecoefs < coefarraysize );
ct = &sumcoefarray[nuniquecoefs];
ct->value = coefs[j];
if ( !SCIPhashtableExists(sumcoefmap, (void *) ct) )
{
SCIP_CALL( SCIPhashtableInsert(sumcoefmap, (void *) ct) );
ct->color = nusedcolors++;
color = ct->color;
nuniquecoefs++;
}
else
{
color = ((SYM_CONSTTYPE*) SCIPhashtableRetrieve(sumcoefmap, (void *) ct))->color;
}
/* add the intermediate node with the corresponding color */
internode = (int) G->add_vertex((unsigned) color);
++nnodes;
visitednodes.push_back(internode);
ischildofsum.push_back(TRUE);
assert( internode < (int) G->get_nof_vertices() );
G->add_edge((unsigned) internode, (unsigned) node);
++nedges;
}
/* add node for the constant term of the sum expression */
SCIP_Real constval = SCIPgetConstantExprSum(expr);
if ( constval != 0.0 )
{
SYM_CONSTTYPE* ct;
/* check whether we have to resize */
if ( nuniqueconsts >= constarraysize )
{
int newsize = SCIPcalcMemGrowSize(scip, nuniqueconsts+1);
assert( newsize >= 0 );
SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &uniqueconstarray, constarraysize, newsize) );
constarraysize = newsize;
}
assert( nuniqueconsts < constarraysize );
ct = &uniqueconstarray[nuniqueconsts];
ct->value = constval;
if ( !SCIPhashtableExists(consttypemap, (void *) ct) )
{
SCIP_CALL( SCIPhashtableInsert(consttypemap, (void *) ct) );
ct->color = nusedcolors++;
color = ct->color;
nuniqueconsts++;
}
else
{
color = ((SYM_CONSTTYPE*) SCIPhashtableRetrieve(consttypemap, (void *) ct))->color;
}
/* add the node with a new color */
internode = (int) G->add_vertex((unsigned) color);
++nnodes;
assert( node < (int) G->get_nof_vertices() );
G->add_edge((unsigned) internode, (unsigned) node);
++nedges;
}
}
currentlevel++;
break;
}
/* when leaving an expression, the nodes that are not needed anymore are erased from the respective arrays */
case SCIP_EXPRITER_LEAVEEXPR:
{
visitednodes.pop_back();
ischildofsum.pop_back();
currentlevel--;
/* When leaving the child of a sum expression, we have to pop again to get rid of the intermediate nodes
* used for the coefficients of summands
*/
if ( !ischildofsum.empty() && ischildofsum[ischildofsum.size() - 1] )
{
visitednodes.pop_back();
ischildofsum.pop_back();
}
break;
}
default:
SCIPABORT(); /* we should never be called in this stage */
break;
}
}
assert( currentlevel == 0 );
assert( visitednodes.empty() );
assert( ischildofsum.empty() );
/* determine whether graph would be too large for bliss (can only handle int) */
if ( nnodes >= INT_MAX/2 )
{
success = FALSE; /*lint !e838*/
break;
}
}
/* free everything */
SCIPfreeExpriter(&it);
SCIPfreeBlockMemoryArrayNull(scip, &uniquerhsarray, rhsarraysize);
SCIPfreeBlockMemoryArrayNull(scip, &sumcoefarray, coefarraysize);
SCIPfreeBlockMemoryArrayNull(scip, &uniqueconstarray, constarraysize);
SCIPfreeBlockMemoryArrayNull(scip, &uniqueoparray, oparraysize);
SCIPhashtableFree(&rhstypemap);
SCIPhashtableFree(&sumcoefmap);
SCIPhashtableFree(&consttypemap);
SCIPhashtableFree(&optypemap);
return SCIP_OKAY;
}
/** return whether symmetry can be computed */
SCIP_Bool SYMcanComputeSymmetry(void)
{
return TRUE;
}
char*
initStaticBlissName( );
static char* blissname = initStaticBlissName();
char*
initStaticBlissName( )
{
blissname = new char[100];
#ifdef BLISS_PATCH_PRESENT
(void) snprintf(blissname, 100, "bliss %sp", bliss::version);
#else
(void) snprintf(blissname, 100, "bliss %s", bliss::version);
#endif
return blissname;
}
/** return name of external program used to compute generators */
const char* SYMsymmetryGetName(void)
{
return blissname;
}
/** return description of external program used to compute generators */
const char* SYMsymmetryGetDesc(void)
{
return "Computing Graph Automorphism Groups by T. Junttila and P. Kaski (www.tcs.hut.fi/Software/bliss/)";
}
/** compute generators of symmetry group */
SCIP_RETCODE SYMcomputeSymmetryGenerators(
SCIP* scip, /**< SCIP pointer */
int maxgenerators, /**< maximal number of generators constructed (= 0 if unlimited) */
SYM_MATRIXDATA* matrixdata, /**< data for MIP matrix */
SYM_EXPRDATA* exprdata, /**< data for nonlinear constraints */
int* nperms, /**< pointer to store number of permutations */
int* nmaxperms, /**< pointer to store maximal number of permutations (needed for freeing storage) */
int*** perms, /**< pointer to store permutation generators as (nperms x npermvars) matrix */
SCIP_Real* log10groupsize /**< pointer to store size of group */
)
{
assert( scip != NULL );
assert( matrixdata != NULL );
assert( exprdata != NULL );
assert( nperms != NULL );