-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.cpp
More file actions
1875 lines (1058 loc) · 42.8 KB
/
solver.cpp
File metadata and controls
1875 lines (1058 loc) · 42.8 KB
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 <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <ctime>
#include "solver.h"
#define INF 1000000
using namespace std;
// Class Solver methods -------------------------------------------------------------------------------
// constructors ----------------------------------
Solver::Solver(){ // default: size of variable array : VAR_NUM
for (int i = 0; i < VAR_NUM; i++){
Variable var(i);
variable.push_back(var);
}
StatesDomains = new bool*[VAR_NUM];
for( int i = 0; i < VAR_NUM; i++){
StatesDomains[i] = new bool[VAR_NUM];
for( int j = 0; j < VAR_NUM; j++){
StatesDomains[i][j] = false;
}
}
}
Solver::Solver(vector<Variable> v){
variable = v;
StatesDomains = new bool*[VAR_NUM];
for( int i = 0; i < VAR_NUM; i++){
StatesDomains[i] = new bool[VAR_NUM];
for( int j = 0; j < VAR_NUM; j++){
StatesDomains[i][j] = false;
}
}
}
Solver::Solver(Constraint_Net c, Constraint_Net b){ // default: size of variable array : VAR_NUM
for (int i = 0; i < VAR_NUM; i++){
Variable var(i);
variable.push_back(var);
}
StatesDomains = new bool*[VAR_NUM];
for( int i = 0; i < VAR_NUM; i++){
StatesDomains[i] = new bool[VAR_NUM];
for( int j = 0; j < VAR_NUM; j++){
StatesDomains[i][j] = false;
}
}
set_constraints(c, b);
}
Solver::Solver(vector<Variable> v, Constraint_Net c, Constraint_Net b){
variable = v;
StatesDomains = new bool*[VAR_NUM];
for( int i = 0; i < VAR_NUM; i++){
StatesDomains[i] = new bool[VAR_NUM];
for( int j = 0; j < VAR_NUM; j++){
StatesDomains[i][j] = false;
}
}
set_constraints(c, b);
}
void Solver::initialize(){
for (int i = 0; i < variable.size(); i++){
variable[i].initialize();
}
vars_queue.clear();
used_vars_queue.clear();
bias_rejects.clear();
bias_checked.clear();
bias_remain.clear();
set_constraints(cl, bias);
}
void Solver::initialize(vector<Variable> v){
variable = v;
vars_queue.clear();
used_vars_queue.clear();
bias_rejects.clear();
bias_checked.clear();
bias_remain.clear();
set_constraints(cl, bias);
}
void Solver::initialize(Constraint_Net c, Constraint_Net b){
for (int i = 0; i < variable.size(); i++){
variable[i].initialize();
}
vars_queue.clear();
used_vars_queue.clear();
bias_rejects.clear();
bias_checked.clear();
bias_remain.clear();
set_constraints(c, b);
}
// set -------------------------------------------
void Solver::set_variables(vector<Variable> v){ // set all variables
variable = v;
}
void Solver::set_variable(Variable v, int i){ // set variable i
variable[i] = v;
}
void Solver::set_variable_value(int v, int i){ // set value v to variable i
variable[i].set_value(v);
}
void Solver::set_variable_domain(Domain d, int i){ // set domain d to variable i
variable[i].set_domain(d);
}
void Solver::set_variable_domain_state(int state, int var, int dom){ // set variable's var, domain value dom, to state state
variable[var].set_domain_state(state, dom);
}
void Solver::set_cl(Constraint_Net c){
cl = c;
// for each constraint, push it to its variables related constraints
for (int i = 0; i < cl.get_size(); i++){
for (int j = 0; j < cl.get_con(i).get_scope_size(); j++){
variable[ cl.get_con(i).get_var(j) ].push_con(i);
}
}
}
void Solver::set_bias(Constraint_Net b){
bias = b;
bias_remain.clear();
// for each bias constraint, push it to its variables related constraints
for (int i = 0; i < bias.get_size(); i++){
bias_remain.push_back(0);
vector<short int> scope = bias.get_con(i).get_scope().get_distinct_variables();
for (int j = 0; j < scope.size(); j++){
variable[ scope[j] ].push_bias_con(i);
}
}
}
void Solver::set_constraints(Constraint_Net c, Constraint_Net b){
set_cl(c);
set_bias(b);
}
void Solver::increase_con_weight(int c){
cl.increase_con_weight(c);
}
// get -------------------------------------------
vector<Variable> Solver::get_variables(){ // return all variables
return variable;
}
Variable Solver::get_variable(int i){ // return variable i
return variable[i];
}
int Solver::get_variable_value(int i){ // get variable i value
return variable[i].get_value();
}
Domain Solver::get_variable_domain(int i){ // get value of domain value dom, of variable var
return variable[i].get_domain();
}
int Solver::get_variable_domain_state(int var, int dom){ // get state of domain value dom, of variable var
return variable[var].get_domain_state(dom);
}
int Solver::get_variable_domain_value(int var, int dom){ // get value of domain value dom, of variable var
return variable[var].get_domain_value(dom);
}
Constraint_Net Solver::get_cl(){ // return cl
return cl;
}
Constraint_Net Solver::get_bias(){ // return bias
return bias;
}
int Solver::get_con_weight(int c){ // return con c weight
return cl.get_con_weight(c);
}
// other methods ---------------------------------
void Solver::print_variables(){
cout << endl << "The variables of the problem are: " << endl << endl;
for (int i = 0; i < variable.size(); i++){
cout << "Variable " << i << ": " << endl;
variable[i].print();
}
}
bool Solver::gac(vector<short int> scope){
return gac(-2, scope);
}
bool Solver::gac(int state, vector<short int> scope){ // gac algorithm for arc consistency
// state: For mac algorithm, which var's assignation deleted these domain values
// run for all variables in vector scope
int i,j; // for the loops
int var_size = scope.size();
bool del; // if revise deleted any value
vector<int> vars; // vector vars: the queue of gac
int var; // the variable to operate (extracted from the queue vars)
int var2; // the variable that has constraint with var
int c; // the index of a constraint
Constraint con;
bool flag = true; // if gac failed or not
bool found;
int var_pos; // to store the position of a var in the scope of the constraint
vector<short int> distinct_variables; // distinct variables of a scope
if ( state < 0 ) { // if it is a call outside - before mac
// initialize vector vars with all variables of scope
for (i = 0; i < var_size; i++){
vars.push_back( scope[i] );
variable[ scope[i] ].set_in_queue(true);
}
} else { // if it is a call after the assignation of a value
for (i = 0; i < var_size; i++){
variable[ scope[i] ].set_in_queue(false);
}
vars.push_back(state); // initialize only with the variable that has been assigned a value
variable[state].set_in_queue(true);
}
// gac main loop
while ( vars.size() > 0 && flag ) {
// fifo : take first element of vector and erase it
var = vars[0];
vars.erase(vars.begin());
variable[var].set_in_queue(false);
// for every constraint that var is involved
for (i = 0; i < variable[var].get_con_size() && flag; i++){
var_pos = -1; // until we find the position of the var in the scope of the constraint
c = variable[var].get_con(i);
con = cl.get_con(c);
if ( con.get_type() >= 0 && (con.get_type() > 23 || con.get_type() < 18 )){
found = true;
distinct_variables = con.get_scope().get_distinct_variables();
// find the position of the variable in the scope of the constraint
// Also check if all the variables in the scope of the constraint belong to the scope given
for (j = 0; j < distinct_variables.size() && found; j++){
if ( distinct_variables[j] == var )
var_pos = j;
else {
if ( find(scope.begin(), scope.end(), distinct_variables[j] ) == scope.end() ) {
found = false;
}
}
}
// call revise -------------------------------------------------------------------
if ( found ) { // if all the variables in the scope of the constraint belong to the scope given
for (j = 0; j < distinct_variables.size() && flag; j++){
del = false;
if ( j != var_pos && variable[ distinct_variables[j] ].get_value() == wrong ) { // check all the neighboors of the variable in var_pos
// if it already has been assigned a value then no domain value has to be deleted
del = revise(con, j, state);
var2 = con.get_var(j);
if ( del ){ // if at least one value was deleted from the domain of variable i
if ( variable[var2].get_domain_size() > 0 ){
// if variable i is not already in queue
if ( !variable[var2].get_in_queue() ) {
variable[var2].set_in_queue(true); // put it in the queue
vars.push_back(var2);
}
} else {
flag = false; // if a domain is empty then fail !
cl.increase_con_weight(c); // increase the weight of constraint c if failed
}
}
}
}
}
} else if ( state == -2 ) {
int value, var_domain_size = variable[ var ].get_domain_values().size();
for (int d = 0; d < var_domain_size; d++){ // for each domain value of the variable we check
if ( variable[ var ].get_domain_state(d) == -1 ){ // if it has not already been deleted
value = variable[ var ].get_domain_value(d);
if ( !con.satisfy( value ) ){
variable[ var ].set_domain_state(state, d);
}
}
}
}
}
}
return flag;
} // end of method gac()
bool Solver::revise(Constraint con, int var_pos, int state){ // revise function for gac algorithm
int i, j, v; // for the loops
bool del = false; // if revise deleted any value
bool support; // if a domain value is supported
vector<short int> distinct_variables = con.get_scope().get_distinct_variables();
int scope_size = distinct_variables.size();
// new arrays for the backtracking search for support
int *var = new int[ scope_size ];
int *value = new int[ scope_size ];
int *var_domain_size = new int[ scope_size ];
int *dom_pos = new int[ scope_size - 1 ];
int *vars_q = new int[ scope_size - 1 ]; // the other variables from the scope, except the one we are operating
int identifier = 0;
for (i = 0; i < scope_size; i++){
value[i] = wrong;
var[i] = distinct_variables[i];
if ( variable[ var[i] ].get_value() == wrong )
var_domain_size[i] = variable[ var[i] ].get_domain_values().size();
else
var_domain_size[i] = 1;
if ( i != var_pos ){ // save the other variables from the scope, except the one we are operating
vars_q[identifier] = i;
identifier++;
}
}
for (i = 0; i < var_domain_size[ var_pos ]; i++){ // for each domain value of the variable we check
if ( variable[ var[var_pos] ].get_domain_state(i) == -1 ){ // if it has not already been deleted
// start of backtracking search of support ----------------------------------------------------
support = false; // initialize to false and check
value[ var_pos ] = variable[ var[var_pos] ].get_domain_value(i);
identifier = 0; // begin with vars_q[0]
for (j = 0; j < (scope_size - 1); j++) // begin from domain value 0 for every variable
dom_pos[j] = 0;
// backtracking search until we find a support or searched all the combinations
while ( !support && identifier >= 0 ){
if ( identifier < (scope_size-1) ) { // if we have not selected a value for all the variables
// (scope_size -1) <- vars_q.size()
// if we searched all the domain values of the variable vars_q[identifier]
while ( dom_pos[identifier] == var_domain_size[ vars_q[identifier] ] ) {
// backtrack ------
dom_pos[identifier] = 0;
identifier--;
}
if ( identifier >= 0 ){
// if the variable we are operating is instantiated
if ( variable[ var[ vars_q[identifier]]].get_value() != wrong ){
value[ vars_q[identifier] ] = variable[ var[ vars_q[identifier]]].get_value();
dom_pos[identifier]++;
identifier++;
} else { // if the variable we are operating is uninstantiated
// if the current domain value is not already deleted
if ( variable[ var[ vars_q[identifier]]].get_domain_state( dom_pos[identifier] ) == -1 ){
value[ vars_q[identifier] ] = variable[ var[ vars_q[identifier]]].get_domain_value( dom_pos[identifier] );
dom_pos[identifier]++;
identifier++;
} else {
dom_pos[identifier]++;
}
}
}
} else { // if we have selected a value for all the variables
vector<int> e;
for (int x = 0; x < VAR_NUM; x++)
e.push_back(wrong);
for (int x = 0; x < distinct_variables.size(); x++) // for the distinct variables of the scope
e[distinct_variables[x]] = value[x];
if ( con.satisfy(e) )
support = true;
dom_pos[identifier] = 0;
identifier--;
}
}
if ( !support ){
del = true;
variable[ var[var_pos] ].set_domain_state(state, i);
if ( state >= 0 )
StatesDomains[state][var[var_pos]] = true;
}
}
}
// end of backtracking search of support ------------------------------------------------------
return del;
} // end of function revise() of gac
Query Solver::generate_query(){ // if no arguments then run for all variables
vector<short int> scope;
for (int i = 0; i < VAR_NUM; i++){
scope.push_back(i);
}
return generate_query(scope);
}
Query Solver::generate_query(Query query){ // save the predefined values and try to expand the query given (run for the rest of the variables)
return generate_query(query, time_bound);
}
Query Solver::generate_query(Query query, double bound){ // save the predefined values and try to expand the query given (run for the rest of the variables)
int in;
bool gac_flag;
clock_t start_time, end_time; // to run for 1 sec
vector<short int> scope, scope_arc;
double time;
start_time = clock(); // starting time
for (int i = 0; i < VAR_NUM; i++){
variable[i].set_value(query.get_var(i));
if ( variable[i].get_value() != wrong ){
scope_arc.push_back(i);
}
scope.push_back(i);
}
query.print();
cout << "Expand ..." << endl;
end_time = clock();
time = (double) (end_time - start_time)/CLOCKS_PER_SEC;
query = generate_query(scope, bound - time);
return query;
// return generate_query(scope);
}
Query Solver::generate_query(vector<short int> scope){
if ( !sol_opt ){
// for line 3 of Generate Example
sol_opt = true;
Query query = generate_query(scope, INF);
sol_opt = false;
return query;
} else{
return generate_query(scope, time_bound);
}
}
Query Solver::generate_query(vector<short int> scope, double bound){ // generate query with only with variables in scope in maximum time = bound
int var_index, i, j; // var_index: index for variables
int first_var;
int domain_pos; // index of domain - var.
bool gac_flag ;
bool bias_rej_flag ; // if we can maximize best_bias_rej
clock_t start_time, end_time; // to run for 1 sec
double time;
int bias_rej; // how many constraints of the bias are rejecting the query
int sum_rejects = 0; // counter of bias constraints violated
int bias_to_check = bias.get_size(); // counter of bias constraints left to check ( bias.get_size() - bias.checked() )
int best_bias_rej = 0; // max bias cons violated by a query
int best_vars_inst = 0; // max vars instantiated
Query query, best_query;
int nodes_visited = 1;
start_time = clock(); // starting time
cout << "scope: " << endl;
for (i = 0; i < variable.size(); i++){
query.set_var(variable[i].get_value(), i);
variable[i].set_value(wrong);
}
gac_flag = gac(scope); // gac algorithm in the beginning
// if given with some variables instantiated -----------------
for (i = 0; i < scope.size(); i++){
cout << scope[i] << ", ";
variable[scope[i]].set_value(query.get_var(scope[i]));
if ( query.get_var(scope[i]) != wrong ){ // If a variable is already instantiated
// count the bias rejects and bias checked
gac_flag = gac(scope[i], scope);
if ( gac_flag ) {
bias_rej = count_bias_rejects(scope[i]);
sum_rejects += bias_rej;
bias_to_check -= bias_checked.back();
used_vars_queue.push_back(scope[i]); // push it in used vars
// increase the instantiated variables number for the constraints it is involved
for(int c = 0; c < variable[scope[i]].get_bias_con_size(); c++){
bias.increase_inst_vars(variable[scope[i]].get_bias_con(c));
for (int k = 0; k < bias.get_con( variable[scope[i]].get_bias_con(c) ).get_scope_size(); k++){
if ( bias.get_con( variable[scope[i]].get_bias_con(c) ).get_scope_vars()[k] != scope[i] )
variable[ bias.get_con( variable[scope[i]].get_bias_con(c) ).get_scope_vars()[k] ].increase_bwdeg();
}
}
} else {
variable[scope[i]].set_value(wrong);
restore_domain(scope[i]); // restore previous changes to domains to continue
vars_queue.push_back(scope[i]);
}
} else {
// Initialize Variables queue -----------
vars_queue.push_back(scope[i]);
}
}
cout << endl;
for (i = 0; i < variable.size(); i++)
query.set_var(variable[i].get_value(), i);
best_query = query;
best_bias_rej = sum_rejects;
// -----------------------------------------------------------
// get first variable -------------------
if ( var_heuristic == 2 ) { // if var_heuristic is wdeg or dom / wdeg
for (i = 0; i < vars_queue.size(); i++){ // calculate wdeg for all the variables
calc_wdeg(vars_queue[i]);
}
}
var_index = next_var(); // get next variable with max wdeg
first_var = var_index;
// --------------------------------------
// main loop of mac algorithm -------------------------------------------------------------------------------------
while ( true ){ // until converge, find query or collapse
end_time = clock();
time = (double) (end_time - start_time)/CLOCKS_PER_SEC;
// End after one sec if have found at least one query being rejected by the bias
if ( !sol_opt && (time > cutoff || time > bound) && best_bias_rej > 0 ){
cout << endl << "best_bias_rej = " << best_bias_rej << endl << endl;
cout << "nodes visited = " << nodes_visited << endl;
cout << "here?" << endl;
return best_query;
} else if ( time > bound && (sol_opt || solp_opt || max_b) ){
cout << "2 here?" << endl;
best_query.set_state(5);
return best_query;
}
// if variable var_index has no valid domain value then backtrack to last variable ------------------------
while ( variable[var_index].get_domain_size() == 0 ){
vars_queue.push_back( var_index ); // push this var to vars_queue
variable[var_index].set_value(wrong); // set its value to wrong ( no value )
// decrease the instantiated variables number for the constraints it is involved
for (int i = 0; i < variable[var_index].get_bias_con_size(); i++){
bias.decrease_inst_vars( variable[var_index].get_bias_con(i) );
for (int j = 0; j < bias.get_con( variable[var_index].get_bias_con(i) ).get_scope_size(); j++){
if ( bias.get_con( variable[var_index].get_bias_con(i) ).get_scope_vars()[j] != var_index )
variable[ bias.get_con( variable[var_index].get_bias_con(i) ).get_scope_vars()[j] ].decrease_bwdeg();
}
}
// -----------------------------------------------------------------------------
var_index = prev_var();
if ( var_index >= 0 ) { // If mac has not ended
variable[var_index].set_value(wrong);
restore_domain(var_index); // restore previous changes to domains to continue
// for the bias violating check ------
bias_rej = bias_rejects.back();
sum_rejects -= bias_rej;
bias_to_check += bias_checked.back();
bias_rejects.pop_back();
bias_checked.pop_back();
bias_remain_restore(var_index);
// -----------------------------------
} else if ( var_index == -1 ){ // then end mac algorithm
if ( best_bias_rej == 0 ) // converge ------------
best_query.set_state(3);
else if ( best_bias_rej == bias.get_size() ) // if all constraints of bias are rejecting it
best_query.set_state(4);
return best_query;
}
}
// choose randomely a valid domain value and assign to variable var_index --------------------------------
domain_pos = pick_domain_value(var_index);
variable[var_index].set_value( variable[var_index].get_domain_value(domain_pos) );
// Count Bias rejects by the chosen value and check if it can maximaze best query's bias rejects ----------
bias_rej = count_bias_rejects(var_index);
sum_rejects += bias_rej;
bias_to_check -= bias_checked.back();
// --------------------------------------------------------------------------------------------------------
nodes_visited++;
if ( vars_queue.size() > 0 ){
// Decide if we want to branch or cut
if ( (max_opt || max_b) && (sum_rejects + bias_to_check) > best_bias_rej )
bias_rej_flag = true;
else if( min_opt && (sum_rejects < best_bias_rej || best_bias_rej == 0) && (sum_rejects + bias_to_check) > 0)
bias_rej_flag = true;
else if ( (sol_opt || solp_opt) && (sum_rejects + bias_to_check) > 0)
bias_rej_flag = true;
else
bias_rej_flag = false;
// Run gac ------------------------------------------------------------------------------------------------
if ( var_heuristic == 2 ) { // if var_heuristic is wdeg or dom / wdeg
// calculate wdeg for all the variables in queue
for (i = 0; i < vars_queue.size(); i++){
calc_wdeg(vars_queue[i]);
}
}
gac_flag = gac(var_index, scope);
if ( gac_flag && bias_rej_flag ){
if ( max_b || solp_opt ) { // if partial heuristic (max_B or solp) is used
// check how many bias constraints are violated from this query ----------------------------------------------------
if ( max_b && (best_bias_rej < sum_rejects) ){ //&& (sum_rejects != bias.get_size() || scope.size() == VAR_NUM || bias.get_size() == 1) ) {
best_bias_rej = sum_rejects;
query.initialize();
query.set_state(1);
for (i = 0; i < variable.size(); i++)
query.set_var(variable[i].get_value(), i);
best_query = query;
} else if ( solp_opt && best_vars_inst < used_vars_queue.size()+1 && sum_rejects > 0 ){
best_vars_inst = used_vars_queue.size()+1;
best_bias_rej = sum_rejects;
query.initialize();
query.set_state(1);
for (i = 0; i < variable.size(); i++)
query.set_var(variable[i].get_value(), i);
best_query = query;
}
}
used_vars_queue.push_back(var_index); // push it in used vars
var_index = next_var(); // get next variable
} else {
variable[var_index].set_value(wrong);
restore_domain(var_index); // restore previous changes to domains to continue
// for the bias violating check ------
bias_rej = bias_rejects.back();
sum_rejects -= bias_rej;
bias_to_check += bias_checked.back();
bias_rejects.pop_back();
bias_checked.pop_back();
bias_remain_restore(var_index);
// -----------------------------------
}
} else if ( vars_queue.size() == 0 ){
if ( used_vars_queue.size() < VAR_NUM-1 ) { // To check if theoretically it could lead to a solution
if ( var_heuristic == 2 ) { // if var_heuristic is wdeg or dom / wdeg
// calculate wdeg for all the variables in queue
for (i = 0; i < vars_queue.size(); i++){
calc_wdeg(vars_queue[i]);
}
}
gac_flag = gac(var_index, scope);
} else
gac_flag = true;
if ( gac_flag ) {
// if reached this point there was no collapse, now create query --------------------------------------------------
query.initialize();
query.set_state(1);
for (i = 0; i < variable.size(); i++)
query.set_var(variable[i].get_value(), i);
// check how many bias constraints are violated from the query ----------------------------------------------------
if ( (max_opt || max_b) && (best_bias_rej < sum_rejects)){ // && (sum_rejects != bias.get_size() || scope.size() == VAR_NUM) ) {