-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcprop.c
2519 lines (2013 loc) · 69.7 KB
/
cprop.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
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include "cprop.h"
#include "memory.h"
#include "macros.h"
#include "containers.h"
#include "mincut.h"
// large values, but not too large so that their summation produces an overflow
const double oo = 1e23;
#define VEPS 1e-10
#define VEPS_INF 1e-4
#define FIXED( lb, ub ) ( ub-lb <= VEPS )
enum ConstraintType
{
ConstraintBV, // constraint involving only binary variables
ConstraintINNB, // constraint involving only integer, >= 0 bounded variables (< oo)
ConstraintOther
} ;
struct _CProp
{
int cols;
double *lb;
double *ub;
// if fixed at pre-processing
char *fixedAtPP;
int nFixedAtPP;
/* original bounds */
double *olb;
double *oub;
char *integer;
char *binary;
char **cname;
/* temporary area */
int *idx;
double *coef;
int *lidx;
int *lnewidx;
int *btight; // indexes in a constraint for variables
// that are responsible for tighten bounds
double *pv;
double *nv;
/* incidence vector indicating the processing of rows */
Vec_char *ivr;
/* stack of rows to be processed after an update */
Vec_int *stkRows;
ISet *inodes; // stores nodes already processed in some layer
// of the conflict graph
/* all constraints stored in the format
* ax <= b */
Vec_int *vrstart;
Vec_int *vrnz;
Vec_int *vridx;
Vec_double *vrcoef;
Vec_double *vrrhs;
Vec_char *vrtype;
// in which rows a column appears
V2D_int *rowsCol;
/* int *colNz;
int *colStart;
Vec_int *colIdx;*/
Vec_int *vrnamest; // start of each row name
Vec_char *vrnames; // row names
/* undo information */
Vec_int *vnimpl;
Vec_int *vcolimpl;
Vec_double *voldlb;
Vec_double *voldub;
Vec_IntPair *lastArcsIG; // last nodes added to the implication graph
Vec_int *nNewArcsIG; // number of nodes added in the last operation to the implication graph
char feasible;
// Input arcs of implication graph
// positions j=0 .. cols-1 indicate nodes for
// x_j=1, positions j=cols .. 2*cols-1 indicate nodes
// for x_j = 0, finally, node 2*cols indicate infeasibility
ISet **implGIn;
ISet **implGOut;
int nimpl;
Vec_char *msgInf;
CPCuts *cpcuts;
// fractional solution, stored to check if discovered cuts are violated or not
double *x;
// for each variable: 0 if not probed, contains 2 if probed =0 4 if probed =1
char *probe;
// -1 if outside probe method, or else the literal which is being probed
int probing;
char verbose;
};
enum ConstraintType get_constraint_type( const CProp *cprop, int nz, const int idx[] )
{
const char *integer = cprop->integer;
const double *lb = cprop->lb;
const double *ub = cprop->ub;
/* number of binary, integer and unbounded variables in this constraint */
int nBin = 0, nInt = 0, nNeg = 0, nPUnb = 0;
for ( int j=0 ; (j<nz) ; ++j )
{
if (integer[idx[j]])
{
if (lb[idx[j]]>= -1e-10 && ub[idx[j]]<=1.0+1e-10)
{
nBin++;
}
else
{
nInt++;
if (lb[idx[j]]<=-1e-10)
nNeg++;
if (ub[idx[j]]>oo)
nPUnb++;
}
}
} // all non zeros
enum ConstraintType consType = ConstraintOther;
if ( nBin == nz )
consType = ConstraintBV;
else
if ( nInt==nz && nNeg==0 && nPUnb==0 )
consType = ConstraintINNB;
return consType;
}
/* generate cut from nogood, returns violation */
static double cut_no_good( const CProp *cprop, int nLiterals, int *literals, int idx[], double coef[], double *rhs );
static double cut_violation( int cutNz, const int cutIdx[], const double cutCoef[], double cutRHS, const double x[] );
int cprop_process_constraint_binary_variables( CProp *cprop, int irow );
int cprop_process_constraint( CProp *cprop, int irow );
/* reports an infeasible constraint considering the minimum value minLhs on the left-hand-side
* also adds nodes to the implicating graph if there are fixed variables */
void cprop_report_infeasible_constraint( CProp *cprop, int irow, double minLhs );
#define HASH_SIZE_IMPLG 128
/* maximum number of variables printed
* when some information of the row is displayed */
#define MAX_VAR_ROW_PRINT 6
void cprop_add_row_name( CProp *cprop, const char rname[] );
void cprop_add_arc_impl_g( CProp *cprop, enum IGNType ntSource, int colSource, enum IGNType ntDest, int colDest );
void cprop_add_msg_inf( CProp *cprop, const char *msg );
/* trimmed description of constraint to show in messages */
char *cprop_constraint_descr( const CProp *cprop, int irow, char *descr );
/* trimmed description of bounds in variables */
char *cprop_constraint_descr_variable_bounds( const CProp *cprop, int irow, char *descr );
/* tries to generate cuts considering the current implication graph */
void cprop_generate_cuts_impl( CProp *cprop );
// s is the source node in the implication graph, i.e.
void cprop_generate_cuts_inf( CProp *cprop, int s );
char cprop_impl_graph_has_arc( const CProp *cprop, int dest, int source );
int cprop_impl_graph_in_d( const CProp *cprop, int nodeId );
// fills all nodes connected to some other nodes in the implication graph
// returns true if all nodes in dest can be implied by some node in source
// alreadyIn indicates nodes already inserted in some previous layer
char cprop_impl_graph_layer( const CProp *cprop, int nDest, int dest[],
int *nSource, int source[], char alreadyIn[] );
/* stored constraints in the format
* ax <= b */
void cprop_add_row( CProp *cprop, int nz, const int idx[], const double coef[], double rhs, double mult, const char rname[] );
CProp *cprop_create( int cols, const char integer[], const double lb[], const double ub[], const char **name )
{
CProp *cprop;
ALLOCATE( cprop, CProp );
cprop->cname = NULL;
cprop->feasible = 1;
cprop->nimpl = 0;
cprop->x = NULL;
cprop->cols = cols;
ALLOCATE_VECTOR( cprop->lb, double, cols*4 );
cprop->ub = cprop->lb + cols;
cprop->olb = cprop->ub + cols;
cprop->oub = cprop->olb + cols;
ALLOCATE_VECTOR_INI( cprop->fixedAtPP, char, cols );
cprop->nFixedAtPP = 0;
ALLOCATE_VECTOR( cprop->integer, char, cols );
ALLOCATE_VECTOR( cprop->binary, char, cols );
memcpy( cprop->lb, lb, sizeof(double)*cols );
memcpy( cprop->ub, ub, sizeof(double)*cols );
memcpy( cprop->integer, integer, sizeof(char)*cols );
int j;
// preventing under/overflow
for ( j=0 ; (j<cols) ; ++j )
cprop->lb[j] = MAX( cprop->lb[j], -oo );
for ( j=0 ; (j<cols) ; ++j )
cprop->ub[j] = MIN( cprop->ub[j], oo );
for ( j=0 ; (j<cols) ; ++j )
cprop->olb[j] = MAX( cprop->lb[j], -oo );
for ( j=0 ; (j<cols) ; ++j )
cprop->oub[j] = MIN( cprop->ub[j], oo );
for ( j=0 ; (j<cols) ; ++j )
cprop->binary[j] = integer[j] && lb[j] >= -VEPS && ub[j] <= 1.0+VEPS;
if (name)
{
int strspace = 0;
int *charsname;
ALLOCATE_VECTOR_INI( charsname, int, cols );
for ( j=0 ; (j<cols) ; ++j )
{
charsname[j] = strlen( name[j] );
strspace += charsname[j];
}
ALLOCATE_VECTOR( cprop->cname, char *, cols );
ALLOCATE_VECTOR( cprop->cname[0], char, (strspace+cols) );
for ( j=1 ; (j<cols) ; ++j )
cprop->cname[j] = cprop->cname[j-1] + charsname[j-1] + 1;
for ( j=0 ; (j<cols) ; ++j )
strcpy( cprop->cname[j], name[j] );
free( charsname );
}
/* temporary area in cprop */
ALLOCATE_VECTOR( cprop->idx, int, cols );
ALLOCATE_VECTOR( cprop->lidx, int, cols*2+1 );
ALLOCATE_VECTOR( cprop->lnewidx, int, cols*2+1 );
ALLOCATE_VECTOR( cprop->coef, double, cols );
ALLOCATE_VECTOR( cprop->pv, double, cols );
ALLOCATE_VECTOR( cprop->nv, double, cols );
ALLOCATE_VECTOR( cprop->btight, int, cols );
FILL( cprop->pv, 0, cols, 0.0 );
FILL( cprop->nv, 0, cols, 0.0 );
cprop->ivr = vec_char_create();
cprop->stkRows = vec_int_create();
cprop->inodes = iset_create( 4096 );
cprop->vrnames = vec_char_create();
cprop->vrnamest = vec_int_create();
int iniCapNZ = MAX( cols*2, 4096 );
int iniCapRows = MIN( 8192, cols );
/* all constraints stored in the format
* ax <= b */
/* to store constraints */
cprop->vrstart = vec_int_create_cap( iniCapRows );
cprop->vrnz = vec_int_create_cap( iniCapRows );
cprop->vridx = vec_int_create_cap( iniCapNZ );
cprop->vrcoef = vec_double_create_cap( iniCapNZ );
cprop->vrrhs = vec_double_create_cap( iniCapRows );
cprop->vrtype = vec_char_create_cap( iniCapRows );
// in which rows a column appears
cprop->rowsCol = v2d_create( cols );
/* ALLOCATE_VECTOR( cprop->colNz, int, cols );
ALLOCATE_VECTOR( cprop->colStart, int, (cols+1) );
cprop->colIdx = vec_int_create_cap( cols*5 );*/
/* undo information */
cprop->vnimpl = vec_int_create();
cprop->vcolimpl = vec_int_create();
cprop->voldlb = vec_double_create();
cprop->voldub = vec_double_create();
ALLOCATE_VECTOR( cprop->implGIn, ISet *, 2*cols+1 );
ALLOCATE_VECTOR( cprop->implGOut, ISet *, 2*cols+1 );
for ( int j=0 ; (j<2*cols+1) ; ++j )
cprop->implGIn[j] = NULL;
for ( int j=0 ; (j<2*cols+1) ; ++j )
cprop->implGOut[j] = NULL;
cprop->lastArcsIG = vec_IntPair_create();
cprop->nNewArcsIG = vec_int_create();
cprop->verbose = 0;
cprop->msgInf = NULL;
cprop->cpcuts = cpc_create( 4096 );
ALLOCATE_VECTOR_INI( cprop->probe, char, cprop->cols );
cprop->probing = -1;
return cprop;
}
void cprop_add_tightening_arcs_row(
CProp *cprop,
int nz, const int *idx, const double *coef,
int nTightenedCols, const int *tightenedCols,
enum IGNType ntDest, int colDest
)
{
const double *lb = cprop->lb;
for ( int i=0 ; (i<nTightenedCols) ; ++i )
{
int col = idx[tightenedCols[i]];
enum IGNType sourceType = lb[col]>=0.999 ? EOne : EZero;
cprop_add_arc_impl_g( cprop,
sourceType, col,
ntDest, colDest );
}
}
int cprop_process_constraint_binary_variables( CProp *cprop, int irow )
{
int nz = cprop_nz( cprop, irow );
const int *idx = cprop_idx( cprop, irow );
const double *coef = cprop_coef( cprop, irow );
double rhs = cprop_rhs( cprop, irow );
double *ub = cprop->ub;
double *lb = cprop->lb;
#ifdef DEBUG
{
for ( int i=0 ; (i<nz) ; ++i )
{
assert( lb[idx[i]]>=-1e-10 && ub[idx[i]]<=1.0+1e-10 );
assert( cprop->integer[idx[i]] );
}
}
#endif
int nimpl = 0;
// minimum positive value and maximum negative value in the LHS
double minP = 0.0, maxN = 0.0;
double *pv = cprop->pv;
double *nv = cprop->nv;
const char **cname = (const char**) cprop->cname;
Vec_int *vcolimpl = cprop->vcolimpl;
Vec_double *voldlb = cprop->voldlb;
Vec_double *voldub = cprop->voldub;
int nFixed = 0;
/* fixed variables that contribute to tighthen the rhs */
int nFixedTightner = 0;
int *fixedTightner = cprop->btight;
for ( int j=0 ; j<nz ; ++j )
{
pv[j] = nv[j] = 0.0;
if (coef[j]>=VEPS)
{
pv[j] = coef[j]*lb[idx[j]];
minP += pv[j];
if (lb[idx[j]]>=0.999)
fixedTightner[nFixedTightner++] = j;
}
else
{
if (coef[j]<=-VEPS)
{
nv[j] = (-1.0*coef[j])*ub[idx[j]];
maxN += nv[j];
if (ub[idx[j]]<=0.00000001)
fixedTightner[nFixedTightner++] = j;
}
else
{
fprintf( stderr, "constraint %s with coefficient too close to zero: %g\n", cprop_row_name(cprop, irow), coef[j] );
abort();
}
}
minP = MIN( oo, minP );
maxN = MIN( oo, maxN );
} // all non-zeros
for ( int j=0 ; j<nz ; ++j )
pv[j] = MIN( pv[j], oo );
for ( int j=0 ; j<nz ; ++j )
nv[j] = MIN( nv[j], oo );
for ( int j=0 ; j<nz ; ++j )
if (FIXED(lb[idx[j]], ub[idx[j]]))
++nFixed;
/* checking if constraint if unfeasible */
if ( minP-maxN >= rhs+VEPS_INF )
{
cprop_report_infeasible_constraint( cprop, irow, minP-maxN );
return -1;
}
// computing upper bound of each var in this constraint
for ( int j=0 ; j<nz ; ++j )
{
if (FIXED( lb[idx[j]], ub[idx[j]] ))
continue;
const double uij = rhs - minP + pv[j] + maxN - nv[j];
// N+ set
if ( coef[j] > VEPS )
{
if ( uij <= -VEPS_INF )
{
char msg[1024], strConstr[512], strFixed[512] = "";
cprop_constraint_descr( cprop, irow, strConstr );
sprintf( msg, "No value for variable %s can satisfy constraint %s.\n", cname[idx[j]], strConstr );
cprop_add_msg_inf( cprop, msg );
if (nFixed)
{
char strComment[64] = "";
cprop_constraint_descr_variable_bounds( cprop, irow, strFixed );
if (strstr(strFixed,"*"))
strcpy( strComment, " (* implied bound)");
sprintf( msg, "Fixed variables: %s %s\n", strFixed, strComment );
cprop_add_msg_inf( cprop, msg );
if (cprop->verbose)
printf("%s", msg );
}
// including variables reponsible for tightening bounds in this constrain
cprop_add_tightening_arcs_row( cprop, nz, idx, coef, nFixedTightner, fixedTightner, Infeasible, 0 );
return -1;
}
else
{
if ( coef[j] >= uij+VEPS_INF )
{
vec_int_push_back( vcolimpl, idx[j] );
vec_double_push_back( voldlb, lb[idx[j]] );
vec_double_push_back( voldub, ub[idx[j]] );
ub[idx[j]] = 0.0;
if (cprop->verbose)
printf(" constr %s impl -> %s=0\n", cprop_row_name( cprop, irow ), cname[idx[j]] );
cprop_add_tightening_arcs_row( cprop, nz, idx, coef, nFixedTightner, fixedTightner, EZero, idx[j] );
++nimpl;
}
}
}
else
{
// set N-
if ( coef[j] <= -VEPS )
{
if ( coef[j] >= uij+VEPS_INF )
{
cprop_report_infeasible_constraint( cprop, irow, 0.0 );
cprop_add_tightening_arcs_row( cprop, nz, idx, coef, nFixedTightner, fixedTightner, Infeasible, 0 );
return -1;
}
else
{
if ( uij <= -VEPS_INF )
{
vec_int_push_back( vcolimpl, idx[j] );
vec_double_push_back( voldlb, lb[idx[j]] );
vec_double_push_back( voldub, ub[idx[j]] );
lb[idx[j]] = 1.0;
if (cprop->verbose)
printf(" constr %s impl %s=1\n", cprop_row_name( cprop, irow ), cname ? cname[idx[j]] : "" );
cprop_add_tightening_arcs_row( cprop, nz, idx, coef, nFixedTightner, fixedTightner, EOne, idx[j] );
++nimpl;
}
}
} // negative
} // not positive
}
return nimpl;
}
void cprop_report_infeasible_constraint( CProp *cprop, int irow, double minLhs )
{
char strRDes[512];
cprop_constraint_descr( cprop, irow, strRDes );
char msg[1024];
double rhs = cprop_rhs( cprop, irow );
sprintf( msg, "Constraint %s impossible to satisfy (%g <= %g)\n", strRDes, minLhs, rhs );
if (cprop->verbose)
printf("%s\n", msg );
cprop_add_msg_inf( cprop, msg );
/* checking fixed variables */
int nz = cprop_nz( cprop, irow );
const int *idx = cprop_idx( cprop, irow );
int nFixed = 0;
const double *ub = cprop->ub;
const double *lb = cprop->lb;
for ( int j=0 ; j<nz ; ++j )
if (FIXED(lb[idx[j]], ub[idx[j]]))
++nFixed;
if ( nFixed == 0 )
return;
char strFix[512];
cprop_constraint_descr_variable_bounds( cprop, irow, strFix );
char strComment[256] = "";
if (strstr(strFix,"*"))
strcpy( strComment, " (* implied bound)" );
sprintf( msg, "Fixed variables: %s\n", strFix );
cprop_add_msg_inf( cprop, msg );
if (cprop->verbose)
printf("%s", msg );
for ( int j=0 ; (j<nz) ; ++j )
{
if (FIXED( lb[idx[j]], ub[idx[j]]))
{
cprop_add_arc_impl_g( cprop,
lb[idx[j]]>=0.98 ? EOne : EZero, idx[j],
Infeasible, 0 );
}
}
}
/* considers constraint in for format ax <= b */
int cprop_process_constraint( CProp *cprop, int irow )
{
double rhs = cprop_rhs( cprop, irow );
/* rhs too large */
if ( rhs >= oo )
return 0;
/* all fixed ? */
int nz = cprop_nz( cprop, irow );
const int *idx = cprop_idx( cprop, irow );
const double *coef = cprop_coef( cprop, irow );
double *ub = cprop->ub;
double *lb = cprop->lb;
int nFixed = 0;
for ( int j=0 ; j<nz ; ++j )
if (FIXED(lb[idx[j]], ub[idx[j]]))
++nFixed;
if ( nFixed == nz )
{
double lhs = 0.0;
for ( int j=0 ; j<nz ; ++j )
lhs += ub[idx[j]]*coef[j];
if ( lhs >= rhs+VEPS_INF )
{
cprop_report_infeasible_constraint( cprop, irow, lhs );
return -1;
}
}
switch ( vec_char_get(cprop->vrtype, irow) )
{
case ConstraintBV:
return cprop_process_constraint_binary_variables( cprop, irow );
break;
case ConstraintINNB:
return 0;
}
return 0;
}
int cprop_add_constraint( CProp *cprop, int nz, const int idx[], const double coef[], char sense, double rhs, const char rname[] )
{
int res1 = 0, res2 = 0;
if (!cprop->feasible)
{
fprintf( stderr, "Cannot add more constraints to infeasible program.\n" );
abort();
}
Vec_int *vcolimpl = cprop->vcolimpl;
int implBoundsStart = vec_int_size( vcolimpl );
{
double mult = toupper(sense) == 'G' || toupper(sense) == 'E' ? -1.0 : 1.0;
char rName[256];
sprintf( rName, "%s%s", rname, toupper(sense) == 'E' ? "p1" : "" );
cprop_add_row( cprop, nz, idx, coef, rhs, mult, rName );
res1 = cprop_process_constraint( cprop, cprop_n_rows( cprop )-1 );
// if (cprop->verbose)
// printf("\n");
}
if (toupper(sense)=='E')
{
char rName[256];
sprintf( rName, "%s%s", rname, toupper(sense) == 'E' ? "p2" : "" );
cprop_add_row( cprop, nz, idx, coef, rhs, 1.0, rName );
res2 = cprop_process_constraint( cprop, cprop_n_rows( cprop )-1 );
// if (cprop->verbose)
// printf("\n");
}
cprop->nimpl = vec_int_size( vcolimpl ) - implBoundsStart;
if ( res1==-1 || res2==-1 )
{
cprop->feasible = 0;
return -1;
}
return res1 + res2;
}
void cprop_try_add_stk_rows( int nr, const int rows[], Vec_int *stkr, Vec_char *ivr )
{
for ( int i=0 ; (i<nr) ; ++i )
{
if ( vec_char_get( ivr, rows[i] ) == 0 )
{
vec_int_push_back( stkr, rows[i] );
vec_char_set( ivr, rows[i], 1 );
}
}
}
int cprop_update_bound( CProp *cprop, int j, double l, double u )
{
if (!cprop->feasible)
{
fprintf( stderr, "Cannot change bounds on infeasible program. Use cprop_undo to undo last changes.\n" );
abort();
}
cprop->nimpl = 0;
double *lb = cprop->lb;
double *ub = cprop->ub;
/* stores the number of implications since a bound changes */
Vec_int *vnimpl = cprop->vnimpl;
/* columns with bounds changed and their old bounds */
Vec_int *vcolimpl = cprop->vcolimpl;
Vec_double *voldlb = cprop->voldlb;
Vec_double *voldub = cprop->voldub;
int implBoundsStart = vec_int_size( vcolimpl );
int nArcsIGStart = vec_IntPair_size( cprop->lastArcsIG );
/* storing original bound of this column */
vec_int_push_back( vcolimpl, j );
vec_double_push_back( voldlb, lb[j] );
vec_double_push_back( voldub, ub[j] );
/* rows to be processed after implications */
Vec_int *stkRows = cprop->stkRows;
Vec_char *ivr = cprop->ivr;
lb[j] = l;
ub[j] = u;
if ( vec_char_size( ivr ) < cprop_n_rows( cprop ) )
vec_char_resize( ivr, cprop_n_rows( cprop), 0 );
#ifdef DEBUG
for ( int i=0 ; (i<vec_char_size(ivr)) ; ++i )
assert( vec_char_get(ivr,i) == 0);
#endif
int implBefore = vec_int_size( vcolimpl );
/* processing initial rows of the first column */
{
int nr = cprop_n_rows_col( cprop, j );
const int *rcs = cprop_rows_col( cprop, j );
for (int i=0 ; (i<nr) ; ++i )
{
int newImpl = cprop_process_constraint( cprop, rcs[i] );
if ( newImpl == -1 )
goto RETURN_INFEASIBLE;
}
}
// processing constraints of columns with new implied bounds
int nNewImpl;
while ( (nNewImpl=(vec_int_size( vcolimpl ) - implBefore)) )
{
vec_int_clear( stkRows );
for ( int i=implBefore ; i<implBefore+nNewImpl ; ++i )
{
// checking rows of this new column
j = vec_int_get( vcolimpl, i );
int nr = cprop_n_rows_col( cprop, j );
const int *rcs = cprop_rows_col( cprop, j );
cprop_try_add_stk_rows( nr, rcs, stkRows, ivr );
}
implBefore = vec_int_size( vcolimpl );
int newImpl = 0;
/* computed new set of rows, processing them */
for ( int i=0 ; (i<vec_int_size(stkRows)) ; i++ )
{
newImpl = cprop_process_constraint( cprop, vec_int_get( stkRows, i ) );
if ( newImpl == -1 )
goto DONE_PROCESSING_ROWS; // just break loop, goto clear ivr
}
DONE_PROCESSING_ROWS:
/* clearing incidence vector */
for ( int l=0 ; (l<vec_int_size(stkRows)) ; ++l )
vec_char_set( ivr, vec_int_get( stkRows, l ), 0 );
if ( newImpl == -1 )
goto RETURN_INFEASIBLE;
}
/* return point for feasible operations */
cprop->feasible = 1;
vec_int_push_back( vnimpl, vec_int_size( vcolimpl ) - implBoundsStart );
vec_int_push_back( cprop->nNewArcsIG, vec_IntPair_size(cprop->lastArcsIG)-nArcsIGStart );
cprop->nimpl = vec_int_size( vcolimpl ) - implBoundsStart - 1;
return vec_int_size( vcolimpl ) - implBoundsStart - 1;
RETURN_INFEASIBLE:
cprop->feasible = 0;
cprop->nimpl = vec_int_size( vcolimpl ) - implBoundsStart - 1;
vec_int_push_back( vnimpl, vec_int_size( vcolimpl ) - implBoundsStart );
vec_int_push_back( cprop->nNewArcsIG, vec_IntPair_size(cprop->lastArcsIG)-nArcsIGStart );
/* analyzing infeasibility to generate cuts */
return -1;
}
void cprop_add_row( CProp *cprop, int nz, const int idx[], const double coef[], double rhs, double mult, const char rname[] )
{
if (nz==0)
return;
rhs = MIN( rhs, oo );
rhs = MAX( rhs, -oo );
Vec_int *vrstart = cprop->vrstart;
Vec_int *vrnz = cprop->vrnz;
Vec_int *vridx = cprop->vridx;
Vec_double *vrcoef = cprop->vrcoef;
Vec_double *vrrhs = cprop->vrrhs;
V2D_int *rowsCol = cprop->rowsCol;
const char *integer = cprop->integer;
const double *lb = cprop->lb;
const double *ub = cprop->ub;
int start = vec_int_size( vrstart ) ? vec_int_last( vrstart ) + vec_int_last( vrnz ) : 0;
vec_int_push_back( vrstart, start );
vec_int_push_back( vrnz, nz );
#ifdef DEBUG
for ( int j=0 ; (j<nz) ; ++j )
assert( idx[j] >= 0 && idx[0] < cprop->cols );
#endif
vec_int_push_back_v( vridx, nz, idx );
/* number of binary, integer and unbounded variables in this constraint */
int nBin = 0, nInt = 0, nNeg = 0, nPUnb = 0;
for ( int j=0 ; (j<nz) ; ++j )
{
double v = MIN( coef[j], oo );
v = MAX( -oo, v )*mult;
vec_double_push_back( vrcoef, v );
if (integer[idx[j]])
{
if (lb[idx[j]]>= -1e-10 && ub[idx[j]]<=1.0+1e-10)
{
nBin++;
}
else
{
nInt++;
if (lb[idx[j]]<=-1e-10)
nNeg++;
if (ub[idx[j]]>oo)
nPUnb++;
}
}
} // all non zeros
enum ConstraintType consType = ConstraintOther;
if ( nBin == nz )
consType = ConstraintBV;
else
if ( nInt==nz && nNeg==0 && nPUnb==0 )
consType = ConstraintINNB;
vec_char_push_back( cprop->vrtype, consType );
vec_double_push_back( vrrhs, rhs*mult );
for ( int j=0 ; j<nz ; ++j )
v2d_int_row_push_back( rowsCol, idx[j], vec_int_size(vrnz)-1 );
cprop_add_row_name( cprop, rname );
}
int cprop_nz( const CProp *cprop, int irow )
{
return vec_int_get( cprop->vrnz, irow );
}
const int *cprop_idx( const CProp *cprop, int row )
{
return vec_int_getp( cprop->vridx,
vec_int_get( cprop->vrstart, row ) );
}
const double *cprop_coef( const CProp *cprop, int row )
{
return vec_double_getp( cprop->vrcoef,
vec_int_get( cprop->vrstart, row ) );
}
int cprop_n_rows( const CProp *cprop )
{
return vec_int_size( cprop->vrnz );
}
double cprop_rhs( const CProp *cprop, int row )
{
return vec_double_get( cprop->vrrhs, row );
}
int cprop_n_rows_col( const CProp *cprop, int col )
{
return v2d_int_row_size( cprop->rowsCol, col );
}
int *cprop_rows_col( const CProp *cprop, int col )
{
return v2d_int_row_ptr( cprop->rowsCol, col );
}
void cprop_undo( CProp *cprop )
{
Vec_int *vnimpl = cprop->vnimpl;
Vec_int *vcolimpl = cprop->vcolimpl;
Vec_double *voldlb = cprop->voldlb;
Vec_double *voldub = cprop->voldub;
double *lb = cprop->lb;
double *ub = cprop->ub;
assert( vec_int_size( vnimpl ) );
int n = vec_int_last( vnimpl );
assert( n >= 1);
for ( int i=0 ; (i<n) ; ++i )
{
int j = vec_int_pop_back( vcolimpl );
lb[j] = vec_double_pop_back( voldlb );
ub[j] = vec_double_pop_back( voldub );
}
vec_int_pop_back( vnimpl );
int narcs = vec_int_pop_back( cprop->nNewArcsIG );
for ( int i=0 ; i<narcs ; ++i )
{
const IntPair arc = vec_IntPair_pop_back( cprop->lastArcsIG );
iset_remove( cprop->implGIn[arc.a], arc.b );
iset_remove( cprop->implGOut[arc.b], arc.a );
}
cprop->feasible = 1;
}
void cprop_add_row_name( CProp *cprop, const char rname[] )
{
vec_int_push_back( cprop->vrnamest, vec_char_size(cprop->vrnames) );
int l = strlen( rname );
for ( int i=0 ; (i<l) ; ++i )
vec_char_push_back( cprop->vrnames, rname[i] );
vec_char_push_back( cprop->vrnames, '\0' );
}
const char *cprop_row_name( const CProp *cprop, int i )
{
return vec_char_getp( cprop->vrnames, vec_int_get( cprop->vrnamest, i ) );
}
char cprop_feasible(const CProp* cprop)
{
return cprop->feasible;
}
int cprop_n_implications( const CProp *cprop )
{
return cprop->nimpl;
}
int cprop_implied_var( const CProp *cprop, int i )
{
int pos = vec_int_size( cprop->vcolimpl ) - i - 1;
assert( pos >= 0 && pos < vec_int_size( cprop->vcolimpl ) );
return vec_int_get( cprop->vcolimpl, pos );
}
double cprop_get_lb( const CProp *cprop, int j )
{
assert( j>=0 && j<cprop->cols );
return cprop->lb[j];
}
double cprop_get_ub( const CProp *cprop, int j )
{
assert( j>=0 && j<cprop->cols );
return cprop->ub[j];
}
void cprop_add_arc_impl_g( CProp *cprop, enum IGNType ntSource, int colSource, enum IGNType ntDest, int colDest )
{
int sIndex = cprop_impl_graph_node_id( cprop, ntSource, colSource );
int dIndex = cprop_impl_graph_node_id( cprop, ntDest, colDest );
assert( sIndex != dIndex );
if (cprop->implGIn[dIndex]==NULL)
cprop->implGIn[dIndex] = iset_create( HASH_SIZE_IMPLG );
if (cprop->implGOut[sIndex]==NULL)
cprop->implGOut[sIndex] = iset_create( HASH_SIZE_IMPLG );