-
Notifications
You must be signed in to change notification settings - Fork 3
/
cons_xor.c
4832 lines (4115 loc) · 171 KB
/
cons_xor.c
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-2014 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 email to scip@zib.de. */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**@file cons_xor.c
* @brief Constraint handler for "xor" constraints, \f$rhs = x_1 \oplus x_2 \oplus \dots \oplus x_n\f$
* @author Tobias Achterberg
* @author Stefan Heinz
* @author Marc Pfetsch
* @author Michael Winkler
*
* This constraint handler deals with "xor" constraint. These are constraint of the form:
*
* \f[
* rhs = x_1 \oplus x_2 \oplus \dots \oplus x_n
* \f]
*
* where \f$x_i\f$ is a binary variable for all \f$i\f$ and \f$rhs\f$ is bool. The variables \f$x\f$'s are called
* operators. This constraint is satisfied if \f$rhs\f$ is TRUE and an odd number of the operators are TRUE or if the
* \f$rhs\f$ is FALSE and a even number of operators are TRUE. Hence, if the sum of \f$rhs\f$ and operators is even.
*/
/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
#include <assert.h>
#include <string.h>
#include "scip/pub_misc.h"
#include "scip/cons_xor.h"
#include "scip/cons_setppc.h"
#include "scip/cons_linear.h"
#include "scip/heur_trysol.h"
#include "scip/debug.h"
/* constraint handler properties */
#define CONSHDLR_NAME "xor"
#define CONSHDLR_DESC "constraint handler for xor constraints: r = xor(x1, ..., xn)"
#define CONSHDLR_SEPAPRIORITY +850200 /**< priority of the constraint handler for separation */
#define CONSHDLR_ENFOPRIORITY -850200 /**< priority of the constraint handler for constraint enforcing */
#define CONSHDLR_CHECKPRIORITY -850200 /**< priority of the constraint handler for checking feasibility */
#define CONSHDLR_SEPAFREQ 0 /**< frequency for separating cuts; zero means to separate only in the root node */
#define CONSHDLR_PROPFREQ 1 /**< frequency for propagating domains; zero means only preprocessing propagation */
#define CONSHDLR_EAGERFREQ 100 /**< frequency for using all instead of only the useful constraints in separation,
* propagation and enforcement, -1 for no eager evaluations, 0 for first only */
#define CONSHDLR_MAXPREROUNDS -1 /**< maximal number of presolving rounds the constraint handler participates in (-1: no limit) */
#define CONSHDLR_DELAYSEPA FALSE /**< should separation method be delayed, if other separators found cuts? */
#define CONSHDLR_DELAYPROP FALSE /**< should propagation method be delayed, if other propagators found reductions? */
#define CONSHDLR_DELAYPRESOL FALSE /**< should presolving method be delayed, if other presolvers found reductions? */
#define CONSHDLR_NEEDSCONS TRUE /**< should the constraint handler be skipped, if no constraints are available? */
#define CONSHDLR_PROP_TIMING SCIP_PROPTIMING_BEFORELP
#define EVENTHDLR_NAME "xor"
#define EVENTHDLR_DESC "event handler for xor constraints"
#define DEFAULT_PRESOLPAIRWISE TRUE /**< should pairwise constraint comparison be performed in presolving? */
#define DEFAULT_ADDEXTENDEDFORM FALSE /**< should the extended formulation be added in presolving? */
#define DEFAULT_ADDFLOWEXTENDED FALSE /**< should the extended flow formulation be added (nonsymmetric formulation otherwise)? */
#define DEFAULT_SEPARATEPARITY FALSE /**< should parity inequalities be separated? */
#define DEFAULT_GAUSSPROPFREQ 5 /**< frequency for applying the Gauss propagator */
#define HASHSIZE_XORCONS 131101 /**< minimal size of hash table in logicor constraint tables */
#define DEFAULT_PRESOLUSEHASHING TRUE /**< should hash table be used for detecting redundant constraints in advance */
#define NMINCOMPARISONS 200000 /**< number for minimal pairwise presolving comparisons */
#define MINGAINPERNMINCOMPARISONS 1e-06 /**< minimal gain per minimal pairwise presolving comparisons to repeat pairwise comparison round */
#define MAXXORCONSSSYSTEM 1000 /**< maximal number of active constraints for which checking the system over GF2 is performed */
#define MAXXORVARSSYSTEM 1000 /**< maximal number of variables in xor constraints for which checking the system over GF2 is performed */
#define NROWS 4
/*
* Data structures
*/
/** type used for matrix entries in function checkGauss() */
typedef unsigned short Type;
/** constraint data for xor constraints */
struct SCIP_ConsData
{
SCIP_VAR** vars; /**< variables in the xor operation */
SCIP_VAR* intvar; /**< internal variable for LP relaxation */
SCIP_VAR** extvars; /**< variables in extended (flow|asymmetric) formulation (order for flow formulation: nn, ns, sn, ss) */
SCIP_ROW* rows[NROWS]; /**< rows for linear relaxation of xor constraint */
int nvars; /**< number of variables in xor operation */
int nextvars; /**< number of variables in extended flow formulation */
int varssize; /**< size of vars array */
int extvarssize; /**< size of extvars array */
int watchedvar1; /**< position of first watched operator variable */
int watchedvar2; /**< position of second watched operator variable */
int filterpos1; /**< event filter position of first watched operator variable */
int filterpos2; /**< event filter position of second watched operator variable */
SCIP_Bool rhs; /**< right hand side of the constraint */
unsigned int deleteintvar:1; /**< should artificial variable be deleted */
unsigned int propagated:1; /**< is constraint already preprocessed/propagated? */
unsigned int sorted:1; /**< are the constraint's variables sorted? */
unsigned int changed:1; /**< was constraint changed since last pair preprocessing round? */
};
/** constraint handler data */
struct SCIP_ConshdlrData
{
SCIP_EVENTHDLR* eventhdlr; /**< event handler for events on watched variables */
SCIP_Bool presolpairwise; /**< should pairwise constraint comparison be performed in presolving? */
SCIP_Bool presolusehashing; /**< should hash table be used for detecting redundant constraints in advance? */
SCIP_Bool addextendedform; /**< should the extended formulation be added in presolving? */
SCIP_Bool addflowextended; /**< should the extended flow formulation be added (nonsymmetric formulation otherwise)? */
SCIP_Bool separateparity; /**< should parity inequalities be separated? */
int gausspropfreq; /**< frequency for applying the Gauss propagator */
};
/*
* Propagation rules
*/
enum Proprule
{
PROPRULE_0, /**< all variables are fixed => fix integral variable */
PROPRULE_1, /**< all except one variable fixed => fix remaining variable */
PROPRULE_INTLB, /**< lower bound propagation of integral variable */
PROPRULE_INTUB, /**< upper bound propagation of integral variable */
PROPRULE_INVALID /**< propagation was applied without a specific propagation rule */
};
typedef enum Proprule PROPRULE;
/*
* Local methods
*/
/** installs rounding locks for the given variable in the given xor constraint */
static
SCIP_RETCODE lockRounding(
SCIP* scip, /**< SCIP data structure */
SCIP_CONS* cons, /**< xor constraint */
SCIP_VAR* var /**< variable of constraint entry */
)
{
/* rounding in both directions may violate the constraint */
SCIP_CALL( SCIPlockVarCons(scip, var, cons, TRUE, TRUE) );
return SCIP_OKAY;
}
/** removes rounding locks for the given variable in the given xor constraint */
static
SCIP_RETCODE unlockRounding(
SCIP* scip, /**< SCIP data structure */
SCIP_CONS* cons, /**< xor constraint */
SCIP_VAR* var /**< variable of constraint entry */
)
{
/* rounding in both directions may violate the constraint */
SCIP_CALL( SCIPunlockVarCons(scip, var, cons, TRUE, TRUE) );
return SCIP_OKAY;
}
/** creates constraint handler data */
static
SCIP_RETCODE conshdlrdataCreate(
SCIP* scip, /**< SCIP data structure */
SCIP_CONSHDLRDATA** conshdlrdata, /**< pointer to store the constraint handler data */
SCIP_EVENTHDLR* eventhdlr /**< event handler */
)
{
assert(scip != NULL);
assert(conshdlrdata != NULL);
assert(eventhdlr != NULL);
SCIP_CALL( SCIPallocMemory(scip, conshdlrdata) );
/* set event handler for catching events on watched variables */
(*conshdlrdata)->eventhdlr = eventhdlr;
return SCIP_OKAY;
}
/** frees constraint handler data */
static
SCIP_RETCODE conshdlrdataFree(
SCIP* scip, /**< SCIP data structure */
SCIP_CONSHDLRDATA** conshdlrdata /**< pointer to the constraint handler data */
)
{
assert(conshdlrdata != NULL);
assert(*conshdlrdata != NULL);
SCIPfreeMemory(scip, conshdlrdata);
return SCIP_OKAY;
}
/** stores the given variable numbers as watched variables, and updates the event processing */
static
SCIP_RETCODE consdataSwitchWatchedvars(
SCIP* scip, /**< SCIP data structure */
SCIP_CONSDATA* consdata, /**< xor constraint data */
SCIP_EVENTHDLR* eventhdlr, /**< event handler to call for the event processing */
int watchedvar1, /**< new first watched variable */
int watchedvar2 /**< new second watched variable */
)
{
assert(consdata != NULL);
assert(watchedvar1 == -1 || watchedvar1 != watchedvar2);
assert(watchedvar1 != -1 || watchedvar2 == -1);
assert(watchedvar1 == -1 || (0 <= watchedvar1 && watchedvar1 < consdata->nvars));
assert(watchedvar2 == -1 || (0 <= watchedvar2 && watchedvar2 < consdata->nvars));
/* if one watched variable is equal to the old other watched variable, just switch positions */
if( watchedvar1 == consdata->watchedvar2 || watchedvar2 == consdata->watchedvar1 )
{
int tmp;
tmp = consdata->watchedvar1;
consdata->watchedvar1 = consdata->watchedvar2;
consdata->watchedvar2 = tmp;
tmp = consdata->filterpos1;
consdata->filterpos1 = consdata->filterpos2;
consdata->filterpos2 = tmp;
}
assert(watchedvar1 == -1 || watchedvar1 != consdata->watchedvar2);
assert(watchedvar2 == -1 || watchedvar2 != consdata->watchedvar1);
/* drop events on old watched variables */
if( consdata->watchedvar1 != -1 && consdata->watchedvar1 != watchedvar1 )
{
assert(consdata->filterpos1 != -1);
SCIP_CALL( SCIPdropVarEvent(scip, consdata->vars[consdata->watchedvar1], SCIP_EVENTTYPE_BOUNDCHANGED, eventhdlr,
(SCIP_EVENTDATA*)consdata, consdata->filterpos1) );
}
if( consdata->watchedvar2 != -1 && consdata->watchedvar2 != watchedvar2 )
{
assert(consdata->filterpos2 != -1);
SCIP_CALL( SCIPdropVarEvent(scip, consdata->vars[consdata->watchedvar2], SCIP_EVENTTYPE_BOUNDCHANGED, eventhdlr,
(SCIP_EVENTDATA*)consdata, consdata->filterpos2) );
}
/* catch events on new watched variables */
if( watchedvar1 != -1 && watchedvar1 != consdata->watchedvar1 )
{
SCIP_CALL( SCIPcatchVarEvent(scip, consdata->vars[watchedvar1], SCIP_EVENTTYPE_BOUNDCHANGED, eventhdlr,
(SCIP_EVENTDATA*)consdata, &consdata->filterpos1) );
}
if( watchedvar2 != -1 && watchedvar2 != consdata->watchedvar2 )
{
SCIP_CALL( SCIPcatchVarEvent(scip, consdata->vars[watchedvar2], SCIP_EVENTTYPE_BOUNDCHANGED, eventhdlr,
(SCIP_EVENTDATA*)consdata, &consdata->filterpos2) );
}
/* set the new watched variables */
consdata->watchedvar1 = watchedvar1;
consdata->watchedvar2 = watchedvar2;
return SCIP_OKAY;
}
/** ensures, that the vars array can store at least num entries */
static
SCIP_RETCODE consdataEnsureVarsSize(
SCIP* scip, /**< SCIP data structure */
SCIP_CONSDATA* consdata, /**< linear constraint data */
int num /**< minimum number of entries to store */
)
{
assert(consdata != NULL);
assert(consdata->nvars <= consdata->varssize);
if( num > consdata->varssize )
{
int newsize;
newsize = SCIPcalcMemGrowSize(scip, num);
SCIP_CALL( SCIPreallocBlockMemoryArray(scip, &consdata->vars, consdata->varssize, newsize) );
consdata->varssize = newsize;
}
assert(num <= consdata->varssize);
return SCIP_OKAY;
}
/** creates constraint data for xor constraint */
static
SCIP_RETCODE consdataCreate(
SCIP* scip, /**< SCIP data structure */
SCIP_CONSDATA** consdata, /**< pointer to store the constraint data */
SCIP_Bool rhs, /**< right hand side of the constraint */
int nvars, /**< number of variables in the xor operation */
SCIP_VAR** vars, /**< variables in xor operation */
SCIP_VAR* intvar /**< artificial integer variable for linear relaxation */
)
{
int r;
assert(consdata != NULL);
assert(nvars == 0 || vars != NULL);
SCIP_CALL( SCIPallocBlockMemory(scip, consdata) );
SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*consdata)->vars, vars, nvars) );
(*consdata)->rhs = rhs;
(*consdata)->intvar = intvar;
for( r = 0; r < NROWS; ++r )
(*consdata)->rows[r] = NULL;
(*consdata)->nvars = nvars;
(*consdata)->varssize = nvars;
(*consdata)->watchedvar1 = -1;
(*consdata)->watchedvar2 = -1;
(*consdata)->filterpos1 = -1;
(*consdata)->filterpos2 = -1;
(*consdata)->deleteintvar = (intvar == NULL);
(*consdata)->propagated = FALSE;
(*consdata)->sorted = FALSE;
(*consdata)->changed = TRUE;
(*consdata)->extvars = NULL;
(*consdata)->nextvars = 0;
(*consdata)->extvarssize = 0;
/* get transformed variables, if we are in the transformed problem */
if( SCIPisTransformed(scip) )
{
SCIP_CALL( SCIPgetTransformedVars(scip, (*consdata)->nvars, (*consdata)->vars, (*consdata)->vars) );
if( (*consdata)->intvar != NULL )
{
SCIP_CALL( SCIPgetTransformedVar(scip, (*consdata)->intvar, &((*consdata)->intvar)) );
}
}
if( (*consdata)->intvar != NULL )
{
/* capture artificial variable */
SCIP_CALL( SCIPcaptureVar(scip, (*consdata)->intvar) );
}
return SCIP_OKAY;
}
/** releases LP row of constraint data */
static
SCIP_RETCODE consdataFreeRows(
SCIP* scip, /**< SCIP data structure */
SCIP_CONSDATA* consdata /**< constraint data */
)
{
int r;
assert(consdata != NULL);
for( r = 0; r < NROWS; ++r )
{
if( consdata->rows[r] != NULL )
{
SCIP_CALL( SCIPreleaseRow(scip, &consdata->rows[r]) );
}
}
return SCIP_OKAY;
}
/** frees constraint data for xor constraint */
static
SCIP_RETCODE consdataFree(
SCIP* scip, /**< SCIP data structure */
SCIP_CONSDATA** consdata, /**< pointer to the constraint data */
SCIP_EVENTHDLR* eventhdlr /**< event handler to call for the event processing */
)
{
assert(consdata != NULL);
assert(*consdata != NULL);
if( SCIPisTransformed(scip) )
{
int j;
/* drop events for watched variables */
SCIP_CALL( consdataSwitchWatchedvars(scip, *consdata, eventhdlr, -1, -1) );
/* release flow variables */
if ( (*consdata)->nextvars > 0 )
{
assert( (*consdata)->extvars != NULL );
for (j = 0; j < (*consdata)->extvarssize; ++j)
{
if ( (*consdata)->extvars[j] != NULL )
{
SCIP_CALL( SCIPreleaseVar(scip, &((*consdata)->extvars[j])) );
}
}
SCIPfreeBlockMemoryArray(scip, &((*consdata)->extvars), (*consdata)->extvarssize);
(*consdata)->nextvars = 0;
(*consdata)->extvarssize = 0;
}
}
else
{
assert((*consdata)->watchedvar1 == -1);
assert((*consdata)->watchedvar2 == -1);
}
/* release LP row */
SCIP_CALL( consdataFreeRows(scip, *consdata) );
/* release internal variable */
if( (*consdata)->intvar != NULL )
{
/* if the constraint is deleted and the integral variable is present, it should be fixed */
assert( SCIPisEQ(scip, SCIPvarGetLbGlobal((*consdata)->intvar), SCIPvarGetLbGlobal((*consdata)->intvar)) );
/* We do not delete the integral variable, but leave the handling to SCIP, because it might happen that the
integral variable is stored in some basis information somewhere. */
SCIP_CALL( SCIPreleaseVar(scip, &(*consdata)->intvar) );
}
SCIPfreeBlockMemoryArray(scip, &(*consdata)->vars, (*consdata)->varssize);
SCIPfreeBlockMemory(scip, consdata);
return SCIP_OKAY;
}
/** prints xor constraint to file stream */
static
SCIP_RETCODE consdataPrint(
SCIP* scip, /**< SCIP data structure */
SCIP_CONSDATA* consdata, /**< xor constraint data */
FILE* file, /**< output file (or NULL for standard output) */
SCIP_Bool endline /**< should an endline be set? */
)
{
assert(consdata != NULL);
/* start variable list */
SCIPinfoMessage(scip, file, "xor(");
/* print variable list */
SCIP_CALL( SCIPwriteVarsList(scip, file, consdata->vars, consdata->nvars, TRUE, ',') );
/* close variable list and write right hand side */
SCIPinfoMessage(scip, file, ") = %d", consdata->rhs);
if( endline )
SCIPinfoMessage(scip, file, "\n");
return SCIP_OKAY;
}
/** adds coefficient to xor constraint */
static
SCIP_RETCODE addCoef(
SCIP* scip, /**< SCIP data structure */
SCIP_CONS* cons, /**< linear constraint */
SCIP_EVENTHDLR* eventhdlr, /**< event handler to call for the event processing */
SCIP_VAR* var /**< variable to add to the constraint */
)
{ /*lint --e{715}*/
SCIP_CONSDATA* consdata;
SCIP_Bool transformed;
assert(var != NULL);
consdata = SCIPconsGetData(cons);
assert(consdata != NULL);
assert(consdata->rows[0] == NULL);
/* are we in the transformed problem? */
transformed = SCIPconsIsTransformed(cons);
/* always use transformed variables in transformed constraints */
if( transformed )
{
SCIP_CALL( SCIPgetTransformedVar(scip, var, &var) );
}
assert(var != NULL);
assert(transformed == SCIPvarIsTransformed(var));
SCIP_CALL( consdataEnsureVarsSize(scip, consdata, consdata->nvars+1) );
consdata->vars[consdata->nvars] = var;
consdata->nvars++;
consdata->sorted = (consdata->nvars == 1);
consdata->changed = TRUE;
/* install the rounding locks for the new variable */
SCIP_CALL( lockRounding(scip, cons, var) );
/**@todo update LP rows */
if( consdata->rows[0] != NULL )
{
SCIPerrorMessage("cannot add coefficients to xor constraint after LP relaxation was created\n");
return SCIP_INVALIDCALL;
}
return SCIP_OKAY;
}
/** deletes coefficient at given position from xor constraint data */
static
SCIP_RETCODE delCoefPos(
SCIP* scip, /**< SCIP data structure */
SCIP_CONS* cons, /**< xor constraint */
SCIP_EVENTHDLR* eventhdlr, /**< event handler to call for the event processing */
int pos /**< position of coefficient to delete */
)
{
SCIP_CONSDATA* consdata;
assert(eventhdlr != NULL);
consdata = SCIPconsGetData(cons);
assert(consdata != NULL);
assert(0 <= pos && pos < consdata->nvars);
assert(SCIPconsIsTransformed(cons) == SCIPvarIsTransformed(consdata->vars[pos]));
/* remove the rounding locks of the deleted variable */
SCIP_CALL( unlockRounding(scip, cons, consdata->vars[pos]) );
if( SCIPconsIsTransformed(cons) )
{
/* if the position is watched, stop watching the position */
if( consdata->watchedvar1 == pos )
{
SCIP_CALL( consdataSwitchWatchedvars(scip, consdata, eventhdlr, consdata->watchedvar2, -1) );
}
if( consdata->watchedvar2 == pos )
{
SCIP_CALL( consdataSwitchWatchedvars(scip, consdata, eventhdlr, consdata->watchedvar1, -1) );
}
}
assert(pos != consdata->watchedvar1);
assert(pos != consdata->watchedvar2);
/* move the last variable to the free slot */
consdata->vars[pos] = consdata->vars[consdata->nvars-1];
consdata->nvars--;
/* if the last variable (that moved) was watched, update the watched position */
if( consdata->watchedvar1 == consdata->nvars )
consdata->watchedvar1 = pos;
if( consdata->watchedvar2 == consdata->nvars )
consdata->watchedvar2 = pos;
consdata->propagated = FALSE;
consdata->sorted = FALSE;
consdata->changed = TRUE;
return SCIP_OKAY;
}
/** sorts and constraint's variables by non-decreasing variable index */
static
void consdataSort(
SCIP_CONSDATA* consdata /**< constraint data */
)
{
assert(consdata != NULL);
if( !consdata->sorted )
{
if( consdata->nvars <= 1 )
consdata->sorted = TRUE;
else
{
SCIP_VAR* var1 = NULL;
SCIP_VAR* var2 = NULL;
/* remember watch variables */
if( consdata->watchedvar1 != -1 )
{
var1 = consdata->vars[consdata->watchedvar1];
assert(var1 != NULL);
consdata->watchedvar1 = -1;
if( consdata->watchedvar2 != -1 )
{
var2 = consdata->vars[consdata->watchedvar2];
assert(var2 != NULL);
consdata->watchedvar2 = -1;
}
}
assert(consdata->watchedvar1 == -1);
assert(consdata->watchedvar2 == -1);
assert(var1 != NULL || var2 == NULL);
/* sort variables after index */
SCIPsortPtr((void**)consdata->vars, SCIPvarCompActiveAndNegated, consdata->nvars);
consdata->sorted = TRUE;
/* correct watched variables */
if( var1 != NULL )
{
int pos;
#ifndef NDEBUG
SCIP_Bool found;
found = SCIPsortedvecFindPtr((void**)consdata->vars, SCIPvarCompActiveAndNegated, (void*)var1, consdata->nvars, &pos);
assert(found);
#else
SCIPsortedvecFindPtr((void**)consdata->vars, SCIPvarCompActiveAndNegated, (void*)var1, consdata->nvars, &pos);
#endif
assert(pos >= 0 && pos < consdata->nvars);
consdata->watchedvar1 = pos;
if( var2 != NULL )
{
#ifndef NDEBUG
found = SCIPsortedvecFindPtr((void**)consdata->vars, SCIPvarCompActiveAndNegated, (void*)var2, consdata->nvars, &pos);
assert(found);
#else
SCIPsortedvecFindPtr((void**)consdata->vars, SCIPvarCompActiveAndNegated, (void*)var2, consdata->nvars, &pos);
#endif
assert(pos >= 0 && pos < consdata->nvars);
consdata->watchedvar2 = pos;
}
}
}
}
#ifdef SCIP_DEBUG
/* check sorting */
{
int v;
for( v = 0; v < consdata->nvars; ++v )
{
assert(v == consdata->nvars-1 || SCIPvarCompareActiveAndNegated(consdata->vars[v], consdata->vars[v+1]) <= 0);
}
}
#endif
}
/** gets the key of the given element */
static
SCIP_DECL_HASHGETKEY(hashGetKeyXorcons)
{ /*lint --e{715}*/
/* the key is the element itself */
return elem;
}
/** returns TRUE iff both keys are equal; two constraints are equal if they have the same variables */
static
SCIP_DECL_HASHKEYEQ(hashKeyEqXorcons)
{
SCIP_CONSDATA* consdata1;
SCIP_CONSDATA* consdata2;
int i;
#ifndef NDEBUG
SCIP* scip;
scip = (SCIP*)userptr;
assert(scip != NULL);
#endif
consdata1 = SCIPconsGetData((SCIP_CONS*)key1);
consdata2 = SCIPconsGetData((SCIP_CONS*)key2);
/* checks trivial case */
if( consdata1->nvars != consdata2->nvars )
return FALSE;
/* sorts the constraints */
consdataSort(consdata1);
consdataSort(consdata2);
assert(consdata1->sorted);
assert(consdata2->sorted);
for( i = 0; i < consdata1->nvars ; ++i )
{
/* tests if variables are equal */
if( consdata1->vars[i] != consdata2->vars[i] )
{
assert(SCIPvarCompare(consdata1->vars[i], consdata2->vars[i]) == 1 ||
SCIPvarCompare(consdata1->vars[i], consdata2->vars[i]) == -1);
return FALSE;
}
assert(SCIPvarCompareActiveAndNegated(consdata1->vars[i], consdata2->vars[i]) == 0);
}
return TRUE;
}
/** returns the hash value of the key */
static
SCIP_DECL_HASHKEYVAL(hashKeyValXorcons)
{ /*lint --e{715}*/
SCIP_CONSDATA* consdata;
unsigned int hashval;
int minidx;
int mididx;
int maxidx;
consdata = SCIPconsGetData((SCIP_CONS*)key);
assert(consdata != NULL);
assert(consdata->sorted);
assert(consdata->nvars > 0);
/* only active, fixed or negated variables are allowed */
assert(consdata->vars[0] != NULL);
assert(consdata->vars[consdata->nvars / 2] != NULL);
assert(consdata->vars[consdata->nvars - 1] != NULL);
assert(SCIPvarIsActive(consdata->vars[0]) || SCIPvarGetStatus(consdata->vars[0]) == SCIP_VARSTATUS_NEGATED || SCIPvarGetStatus(consdata->vars[0]) == SCIP_VARSTATUS_FIXED);
assert(SCIPvarIsActive(consdata->vars[consdata->nvars / 2]) || SCIPvarGetStatus(consdata->vars[consdata->nvars / 2]) == SCIP_VARSTATUS_NEGATED || SCIPvarGetStatus(consdata->vars[consdata->nvars / 2]) == SCIP_VARSTATUS_FIXED);
assert(SCIPvarIsActive(consdata->vars[consdata->nvars - 1]) || SCIPvarGetStatus(consdata->vars[consdata->nvars - 1]) == SCIP_VARSTATUS_NEGATED || SCIPvarGetStatus(consdata->vars[consdata->nvars - 1]) == SCIP_VARSTATUS_FIXED);
minidx = SCIPvarGetIndex(consdata->vars[0]);
mididx = SCIPvarGetIndex(consdata->vars[consdata->nvars / 2]);
maxidx = SCIPvarGetIndex(consdata->vars[consdata->nvars - 1]);
/* note that for all indices it does not hold that they are sorted, because variables are sorted with
* SCIPvarCompareActiveAndNegated (see var.c)
*/
hashval = (consdata->nvars << 29) + (minidx << 22) + (mididx << 11) + maxidx; /*lint !e701*/
return hashval;
}
/** deletes all fixed variables and all pairs equal variables variables */
static
SCIP_RETCODE applyFixings(
SCIP* scip, /**< SCIP data structure */
SCIP_CONS* cons, /**< xor constraint */
SCIP_EVENTHDLR* eventhdlr, /**< event handler to call for the event processing */
int* nchgcoefs /**< pointer to add up the number of changed coefficients */
)
{
SCIP_CONSDATA* consdata;
int v;
consdata = SCIPconsGetData(cons);
assert(consdata != NULL);
assert(consdata->nvars == 0 || consdata->vars != NULL);
assert(nchgcoefs != NULL);
SCIPdebugMessage("before fixings: ");
SCIPdebug( SCIP_CALL(consdataPrint(scip, consdata, NULL, TRUE)) );
v = 0;
while( v < consdata->nvars )
{
SCIP_VAR* var;
var = consdata->vars[v];
assert(SCIPvarIsBinary(var));
if( SCIPvarGetUbGlobal(var) < 0.5 )
{
assert(SCIPisEQ(scip, SCIPvarGetLbGlobal(var), 0.0));
SCIP_CALL( delCoefPos(scip, cons, eventhdlr, v) );
(*nchgcoefs)++;
}
else if( SCIPvarGetLbGlobal(var) > 0.5 )
{
assert(SCIPisEQ(scip, SCIPvarGetUbGlobal(var), 1.0));
SCIP_CALL( delCoefPos(scip, cons, eventhdlr, v) );
consdata->rhs = !consdata->rhs;
(*nchgcoefs)++;
}
else
{
SCIP_VAR* repvar;
SCIP_Bool negated;
/* get binary representative of variable */
SCIP_CALL( SCIPgetBinvarRepresentative(scip, var, &repvar, &negated) );
/* remove all negations by replacing them with the active variable
* it holds that xor(x1, ~x2) = 0 <=> xor(x1, x2) = 1
*/
if( negated )
{
assert(SCIPvarIsNegated(repvar));
repvar = SCIPvarGetNegationVar(repvar);
consdata->rhs = !consdata->rhs;
}
/* check, if the variable should be replaced with the representative */
if( repvar != var )
{
/* delete old (aggregated) variable */
SCIP_CALL( delCoefPos(scip, cons, eventhdlr, v) );
/* add representative instead */
SCIP_CALL( addCoef(scip, cons, eventhdlr, repvar) );
}
else
++v;
}
}
/* sort the variables in the constraint */
consdataSort(consdata);
assert(consdata->sorted);
SCIPdebugMessage("after sort : ");
SCIPdebug( SCIP_CALL(consdataPrint(scip, consdata, NULL, TRUE)) );
/* delete pairs of equal or negated variables; scan from back to front because deletion doesn't affect the
* order of the front variables
*/
v = consdata->nvars-2;
while ( v >= 0 )
{
if( consdata->vars[v] == consdata->vars[v+1] )
{
/* delete both variables */
SCIPdebugMessage("xor constraint <%s>: deleting pair of equal variables <%s>\n",
SCIPconsGetName(cons), SCIPvarGetName(consdata->vars[v]));
SCIP_CALL( delCoefPos(scip, cons, eventhdlr, v+1) );
SCIP_CALL( delCoefPos(scip, cons, eventhdlr, v) );
(*nchgcoefs) += 2;
v = MIN(v, consdata->nvars-1);
}
else if( consdata->vars[v] == SCIPvarGetNegatedVar(consdata->vars[v+1]) )
{
/* delete both variables and negate the rhs */
SCIPdebugMessage("xor constraint <%s>: deleting pair of negated variables <%s> and <%s>\n",
SCIPconsGetName(cons), SCIPvarGetName(consdata->vars[v]), SCIPvarGetName(consdata->vars[v+1]));
SCIP_CALL( delCoefPos(scip, cons, eventhdlr, v+1) );
SCIP_CALL( delCoefPos(scip, cons, eventhdlr, v) );
(*nchgcoefs) += 2;
consdata->rhs = !consdata->rhs;
v = MIN(v, consdata->nvars-1);
}
else
assert(SCIPvarGetProbvar(consdata->vars[v]) != SCIPvarGetProbvar(consdata->vars[v+1]));
--v;
}
SCIPdebugMessage("after fixings : ");
SCIPdebug( SCIP_CALL(consdataPrint(scip, consdata, NULL, TRUE)) );
return SCIP_OKAY;
}
/** adds extended flow formulation
*
* The extended flow formulation is built as follows: Let \f$x_1, \dots, x_k\f$ be the variables contained in the given
* XOR constraint. We construct a two layered flow network. The upper layer is called the north layer and the lower is
* called the south layer. For each \f$x_i,\; i = 2, \ldots, k-1\f$, we add arcs that stay in the north and south layer
* (denoted by 'nn' and 'ss', respectively), as well as arcs that change the layers (denoted by 'ns' and 'sn'). For
* \f$x_1\f$, we only add two arcs from the source to the two layers. The source is located on the north layer. For
* \f$x_k\f$, we add two arcs connecting the two layers to the sink. Depending on the rhs of the constraint the sink is
* located on the north or south layer. A change in the layers corresponds to a parity change, i.e., the corresponding
* variable \f$x_i\f$ is 1 (and 0 otherwise).
*/
static
SCIP_RETCODE addExtendedFlowFormulation(
SCIP* scip, /**< SCIP data structure */
SCIP_CONS* cons, /**< constraint to check */
int* naddedconss /**< number of added constraints */
)
{
char name[SCIP_MAXSTRLEN];
SCIP_CONSDATA* consdata;
SCIP_VAR* varprevnn = NULL;
SCIP_VAR* varprevns = NULL;
SCIP_VAR* varprevsn = NULL;
SCIP_VAR* varprevss = NULL;
SCIP_VAR* vars[4];
SCIP_Real vals[4];
int i;
assert( scip != NULL );
assert( cons != NULL );
assert( naddedconss != NULL );
*naddedconss = 0;
/* exit if contraints is modifiable */
if ( SCIPconsIsModifiable(cons) )
return SCIP_OKAY;
consdata = SCIPconsGetData(cons);
assert( consdata != NULL );
/* exit if extended formulation has been added already */
if ( consdata->extvars != NULL )
return SCIP_OKAY;
/* xor constraints with at most 3 variables are handled directly through rows for the convex hull */
if ( consdata->nvars <= 3 )
return SCIP_OKAY;
SCIPdebugMessage("Add extended formulation for xor constraint <%s> ...\n", SCIPconsGetName(cons));
assert( consdata->extvars == NULL );
assert( consdata->nextvars == 0 );
assert( consdata->extvarssize == 0 );
/* get storage for auxiliary variables */
consdata->extvarssize = 4 * (consdata->nvars);
SCIP_CALL( SCIPallocBlockMemoryArray(scip, &(consdata->extvars), consdata->extvarssize) );
/* pass through components */
for (i = 0; i < consdata->nvars; ++i)
{
/* variables: n - north, s - south */
SCIP_VAR* varnn = NULL;
SCIP_VAR* varns = NULL;
SCIP_VAR* varsn = NULL;
SCIP_VAR* varss = NULL;
SCIP_CONS* newcons;
SCIP_Real rhs = 0.0;
SCIP_Bool infeasible = FALSE;
SCIP_Bool redundant = FALSE;
SCIP_Bool aggregated = FALSE;
int cnt = 0;
/* create variables */
if ( i == 0 )
{
(void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_%d_nn", SCIPconsGetName(cons), i);
SCIP_CALL( SCIPcreateVar(scip, &varnn, name, 0.0, 1.0, 0.0, SCIP_VARTYPE_IMPLINT, SCIPconsIsInitial(cons), SCIPconsIsRemovable(cons), NULL, NULL, NULL, NULL, NULL) );
SCIP_CALL( SCIPaddVar(scip, varnn) );
(void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_%d_ns", SCIPconsGetName(cons), i);
SCIP_CALL( SCIPcreateVar(scip, &varns, name, 0.0, 1.0, 0.0, SCIP_VARTYPE_IMPLINT, SCIPconsIsInitial(cons), SCIPconsIsRemovable(cons), NULL, NULL, NULL, NULL, NULL) );
SCIP_CALL( SCIPaddVar(scip, varns) );
/* need to lock variables, because we aggregate them */
SCIP_CALL( SCIPlockVarCons(scip, varnn, cons, TRUE, TRUE) );
SCIP_CALL( SCIPlockVarCons(scip, varns, cons, TRUE, TRUE) );
/* aggregate ns variable with original variable */
SCIP_CALL( SCIPaggregateVars(scip, varns, consdata->vars[0], 1.0, -1.0, 0.0, &infeasible, &redundant, &aggregated) );
assert( ! infeasible );
assert( redundant );
assert( aggregated );
}
else
{
if ( i == consdata->nvars-1 )
{
if ( consdata->rhs )
{
/* if the rhs is 1 (true) the flow goes to the bottom level */
(void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_%d_ns", SCIPconsGetName(cons), i);
SCIP_CALL( SCIPcreateVar(scip, &varns, name, 0.0, 1.0, 0.0, SCIP_VARTYPE_IMPLINT, SCIPconsIsInitial(cons), SCIPconsIsRemovable(cons), NULL, NULL, NULL, NULL, NULL) );
SCIP_CALL( SCIPaddVar(scip, varns) );
(void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_%d_ss", SCIPconsGetName(cons), i);
SCIP_CALL( SCIPcreateVar(scip, &varss, name, 0.0, 1.0, 0.0, SCIP_VARTYPE_IMPLINT, SCIPconsIsInitial(cons), SCIPconsIsRemovable(cons), NULL, NULL, NULL, NULL, NULL) );
SCIP_CALL( SCIPaddVar(scip, varss) );
/* need to lock variables, because we aggregate them */
SCIP_CALL( SCIPlockVarCons(scip, varns, cons, TRUE, TRUE) );
SCIP_CALL( SCIPlockVarCons(scip, varss, cons, TRUE, TRUE) );
/* aggregate ns variable with original variable */
SCIP_CALL( SCIPaggregateVars(scip, varns, consdata->vars[i], 1.0, -1.0, 0.0, &infeasible, &redundant, &aggregated) );
assert( ! infeasible );
assert( redundant );
assert( aggregated );
}
else
{
/* if the rhs is 0 (false) the flow stays on the top level */
(void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_%d_nn", SCIPconsGetName(cons), i);
SCIP_CALL( SCIPcreateVar(scip, &varnn, name, 0.0, 1.0, 0.0, SCIP_VARTYPE_IMPLINT, SCIPconsIsInitial(cons), SCIPconsIsRemovable(cons), NULL, NULL, NULL, NULL, NULL) );
SCIP_CALL( SCIPaddVar(scip, varnn) );
(void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_%d_sn", SCIPconsGetName(cons), i);
SCIP_CALL( SCIPcreateVar(scip, &varsn, name, 0.0, 1.0, 0.0, SCIP_VARTYPE_IMPLINT, SCIPconsIsInitial(cons), SCIPconsIsRemovable(cons), NULL, NULL, NULL, NULL, NULL) );
SCIP_CALL( SCIPaddVar(scip, varsn) );
/* need to lock variables, because we aggregate them */
SCIP_CALL( SCIPlockVarCons(scip, varnn, cons, TRUE, TRUE) );
SCIP_CALL( SCIPlockVarCons(scip, varsn, cons, TRUE, TRUE) );
/* aggregate sn variable with original variable */
SCIP_CALL( SCIPaggregateVars(scip, varsn, consdata->vars[i], 1.0, -1.0, 0.0, &infeasible, &redundant, &aggregated) );
assert( ! infeasible );
assert( redundant );
assert( aggregated );
}
}
else
{
/* add the four flow variables */
(void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_%d_nn", SCIPconsGetName(cons), i);
SCIP_CALL( SCIPcreateVar(scip, &varnn, name, 0.0, 1.0, 0.0, SCIP_VARTYPE_IMPLINT, SCIPconsIsInitial(cons), SCIPconsIsRemovable(cons), NULL, NULL, NULL, NULL, NULL) );
SCIP_CALL( SCIPaddVar(scip, varnn) );
(void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_%d_ns", SCIPconsGetName(cons), i);
SCIP_CALL( SCIPcreateVar(scip, &varns, name, 0.0, 1.0, 0.0, SCIP_VARTYPE_IMPLINT, SCIPconsIsInitial(cons), SCIPconsIsRemovable(cons), NULL, NULL, NULL, NULL, NULL) );
SCIP_CALL( SCIPaddVar(scip, varns) );
(void) SCIPsnprintf(name, SCIP_MAXSTRLEN, "%s_%d_sn", SCIPconsGetName(cons), i);
SCIP_CALL( SCIPcreateVar(scip, &varsn, name, 0.0, 1.0, 0.0, SCIP_VARTYPE_IMPLINT, SCIPconsIsInitial(cons), SCIPconsIsRemovable(cons), NULL, NULL, NULL, NULL, NULL) );