-
Notifications
You must be signed in to change notification settings - Fork 0
/
zchaff_solver.cpp
5703 lines (5387 loc) · 203 KB
/
zchaff_solver.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
/*********************************************************************
Copyright 2000-2003, Princeton University. All rights reserved.
By using this software the USER indicates that he or she has read,
understood and will comply with the following:
--- Princeton University hereby grants USER nonexclusive permission
to use, copy and/or modify this software for internal, noncommercial,
research purposes only. Any distribution, including commercial sale
or license, of this software, copies of the software, its associated
documentation and/or modifications of either is strictly prohibited
without the prior consent of Princeton University. Title to copyright
to this software and its associated documentation shall at all times
remain with Princeton University. Appropriate copyright notice shall
be placed on all software copies, and a complete copy of this notice
shall be included in all copies of the associated documentation.
No right is granted to use in advertising, publicity or otherwise
any trademark, service mark, or the name of Princeton University.
--- This software and any associated documentation is provided "as is"
PRINCETON UNIVERSITY MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS
OR IMPLIED, INCLUDING THOSE OF MERCHANTABILITY OR FITNESS FOR A
PARTICULAR PURPOSE, OR THAT USE OF THE SOFTWARE, MODIFICATIONS, OR
ASSOCIATED DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS,
TRADEMARKS OR OTHER INTELLECTUAL PROPERTY RIGHTS OF A THIRD PARTY.
Princeton University shall not be liable under any circumstances for
any direct, indirect, special, incidental, or consequential damages
with respect to any claim by USER or any third party on account of
or arising from the use, or inability to use, this software or its
associated documentation, even if Princeton University has been advised
of the possibility of those damages.
*********************************************************************/
#include <algorithm>
#include <fstream>
#include "zchaff_solver.h"
#include "stdio.h"
//typedef double DOUBLE;
// extern "C" DOUBLE PLED_FindMaxFlow(int nvtxs, int nedges,
// int *edge_from, int *edge_to,
// DOUBLE *ewts, int *part);
//#define VERIFY_ON
#ifdef VERIFY_ON
ofstream verify_out("resolve_trace");
#endif
#ifdef DEBUG_OUT
ofstream solution_file("solutions.txt");
#endif
void CSolver::re_init_stats(void)
{
_stats.is_mem_out = false;
_stats.outcome = UNKNOWN;
_stats.next_restart = _params.restart.first_restart;
_stats.restart_incr = _params.restart.backtrack_incr;
_stats.next_cls_deletion = _params.cls_deletion.interval; // 600(new), 5000(old)
_stats.next_var_score_decay = _params.decision.decay_period;
_stats.current_randomness = _params.decision.base_randomness;
_stats.total_bubble_move = 0;
_stats.num_decisions = 0;
#ifdef BIG_NUM
mpz_init(_stats.num_solutions);
mpz_set_ui(_stats.num_solutions, 0);
#else
_stats.num_solutions = 0; // added by sang
#endif
_stats.num_total_components = 0;
_stats.num_split_components = 0;
_stats.num_not_split_components = 0;
_stats.num_trivial_components = 0;
_stats.first_split_level = 100000; // set to max value
_stats.num_changed_components = 0; // added by sang
_stats.num_cross_implications = 0; // added by sang
_stats.num_adjusted_components = 0; // added by sang
_stats.num_unsatisfied_clause = 0; // added by sang
_stats.num_original_clause = 0; // added by sang
_stats.num_unit_clause = 0; // added by sang
_stats.num_active_added_conf_clauses = 0; // added by sang
_stats.num_backtracks = 0;
_stats.max_dlevel = 0;
_stats.num_del_orig_cls = 0; // added in zchaff2004
_stats.num_implications = 0;
_stats.start_cpu_time = get_cpu_time();
_stats.finish_cpu_time = 0;
}
void CSolver::init_stats(void)
{
re_init_stats();
_stats.been_reset = true;
_stats.outcome = UNDETERMINED;
_stats.num_free_variables = 0;
_stats.num_free_branch_vars = 0;
}
void CSolver::init_parameters(void)
{
_params.verbosity = 0;
_params.time_limit = 3600 * 48; //2 days
_params.decision.strategy = 0;
_params.decision.restart_randomness = 0;
_params.decision.base_randomness = 0;
_params.decision.decay_period = 512; // 1024 //0x100;
_params.decision.bubble_init_step = 0x400;
_params.cls_deletion.enable = true ;
// new parameters for clause deletion in zchaff2004
_params.cls_deletion.enable = true ;
_params.cls_deletion.head_activity = 500;//250;//60
_params.cls_deletion.tail_activity = 10;//35;//7
_params.cls_deletion.head_num_lits = 6;//6;//8
_params.cls_deletion.tail_num_lits = 45;//45;//42
_params.cls_deletion.tail_vs_head = 16;
_params.cls_deletion.interval = 1500; //600;
// old parameters for clause deletion
//_params.cls_deletion.interval = 5000;
//_params.cls_deletion.max_unrelevance = 20;
//_params.cls_deletion.min_num_lits = 100;
//_params.cls_deletion.max_conf_cls_size = 5000;
_params.restart.enable = false;
_params.restart.first_restart = 5000;
_params.restart.backtrack_incr = 1500;
_params.restart.backtrack_incr_incr = 200;
}
CSolver::CSolver(void) {
//for (int i = 0; i < LEVEL_MAX; i++)
// level_total_components[i] = level_new_components[i]
// = level_trivial[i] = level_conflicts[i] = level_hits[i] = 0;
init_parameters();
init_stats();
//backtrack_tag = false; // for debug
//set_tag = true; // for debug
//flag2 = true; // for debug
//flag = true; // for debug
original_BN_nodes = 0; // initialized to 0, which means the CNF is not a BN by default
far_back_track_enabled = false;
static_heuristic = false;
cross_enabled = true; // false -> true
adjust_enabled = true; // false -> true
backtrack_factor = 2;
candidates.resize(10, 0);
num_conflicts = 0;
num_sat = 0;
num_no_components = 0;
_dlevel = 0;
_force_terminate = false;
_implication_id = 0;
_num_marked = 0;
_num_in_new_cl = 0;
//num_no_components = 0;
num_far_backtrack = 0;
_outside_constraint_hook = NULL;
_uni_phased_size = 0; // added by sang
bound = 1000; // added by sang
_gid = 0; // added by sang
#ifdef APPROXIMATE_HASHING
#ifndef BIG_NUM
cache_size = 5 * 1024 * 1024; // 5M
oldest_entry = 1024 * 1024; // that's 1M, can be set by -o option
clean_limit = 20 * oldest_entry;
#else
cache_size = 3 * 1024 * 1024; // 3M
oldest_entry = 1024 * 500; // that's 500K, can be set by -o option
clean_limit = 20 * oldest_entry;
#endif
#else
cache_size = 1024 * 1024; // 1M, can be set by -c option
oldest_entry = 50 * 1024; // that's 50K, can be set by -o option
clean_limit = 20 * oldest_entry;
#endif
max_entry = 1024 * 1024 * 1024; // that's 1G
max_distance = MAX_DISTANCE;
//dynamic_heuristic = false; // true -> false
//dynamic_heuristic = SUM_DEGREE; // which is -h 3
dynamic_heuristic = VSADS; // which is -h 5
quiet = false;
//approximation = false;
max_num_learned_clause = 10*1024*1024; // 10M
//hashtable = new CHashTable(cache_size);
srand(time(NULL));
}
CSolver::~CSolver()
{
while (!_assignment_stack.empty()) {
delete _assignment_stack.back();
_assignment_stack.pop_back();
}
}
void CSolver::set_time_limit(float t)
{
_params.time_limit = t;
}
void CSolver::set_BN_node(int BN_nodes)
{
original_BN_nodes = BN_nodes;
}
void CSolver::set_var_weight(vector<double> * weight_pos, vector<double> * weight_neg)
{
vector<double> var_weight_pos = * weight_pos;
vector<double> var_weight_neg = * weight_neg;
for (int i = variables().size()-1; i >= 1; --i)
{
CVariable & var = variable(i);
//if (var_weight[i] > -0.5) // initialized to -1, > -0.5 means set to some positive value
var.set_weight(var_weight_pos[i], var_weight_neg[i]);
//else if (original_BN_nodes == 0) // not a BN CNF, all var weights should be set to 0.5
// var.set_weight(0.5);
if (var.pos_weight + var.neg_weight < 2)
var.internal = false; // add a tag for a component with only interal nodes
else
var.internal = true; // an internal node has a weight sum of 2
}
}
void CSolver::set_cache_size(unsigned size)
{
cache_size = size;
}
void CSolver::set_max_entry(unsigned entry)
{
max_entry = entry;
}
void CSolver::set_oldest_entry(unsigned oldest)
{
if (clean_limit == oldest_entry * 20) // that means -l not set
clean_limit = 20 * oldest;
oldest_entry = oldest;
}
void CSolver::set_clean_limit(unsigned limit)
{
clean_limit = limit;
}
void CSolver::set_max_distance(unsigned dist)
{
max_distance = dist;
}
void CSolver::set_dynamic_heuristic(int dynamic)
{
switch(dynamic)
{
case 0: dynamic_heuristic = DEGREE;
break;
case 1: dynamic_heuristic = DEGREE_SUM;
break;
case 2: dynamic_heuristic = SUM;
break;
case 3: dynamic_heuristic = SUM_DEGREE;
break;
case 4: dynamic_heuristic = VSIDS;
break;
case 5: dynamic_heuristic = VSADS;
break;
case 6: dynamic_heuristic = UNIT_PROP;
break;
case 7: dynamic_heuristic = BERKMIN;
break;
}
}
//void CSolver::set_approximation(bool app)
//{
// approximation = app;
//}
void CSolver::set_max_num_learned_clause(unsigned max_num)
{
max_num_learned_clause = max_num;
}
void CSolver::set_backtrack_factor(double bfactor)
{
backtrack_factor = bfactor;
}
void CSolver::set_vgo(vector< set<int> > variable_group_ordering)
{
vgo = variable_group_ordering;
}
void CSolver::set_static_heuristic(bool stat)
{
static_heuristic = stat;
}
void CSolver::set_far_back_track_flag(bool far)
{
far_back_track_enabled = far;
}
void CSolver::assign_static_score()
{
for (int i = vgo.size() - 1; i >= 0; --i)
{
set <int> & group = vgo[i];
for (set<int>::iterator itr = group.begin(); itr != group.end(); ++itr)
variable(*itr).set_static_score(i); // the smaller, the better
}
cout << "Assigning static scores done" << endl;
//for (int i = 1; i < variables().size(); i++)
// cout << variable(i)._static_score << " ";
}
void CSolver::set_cross_flag(bool cross)
{
cross_enabled = cross;
}
void CSolver::set_adjust_flag(bool adjust)
{
adjust_enabled = adjust;
}
void CSolver::set_quiet_flag(bool q)
{
quiet = q;
//hashtable->quiet = q;
}
double CSolver::elapsed_cpu_time(void)
{
return get_cpu_time() - _stats.start_cpu_time;
}
double CSolver::cpu_run_time() // changed by sang, from float to double
{
return (_stats.finish_cpu_time - _stats.start_cpu_time);
}
void CSolver::set_variable_number(int n)
{
assert (num_variables() == 0);
CDatabase::set_variable_number(n);
_stats.num_free_variables = num_variables();
//_component_infor_stack.resize(n);
while (_assignment_stack.size() <= num_variables())
{
_assignment_stack.push_back(new vector<int>);
#ifdef NON_ACTIVE_STACK
_non_active_free_var_stack.push_back(new vector<int>);
#endif
#ifdef TOCOMPONENTS
//_component_infor_stack.push_back(new <Component_information>);
_num_implication_stack.push_back(0);
//_clause_component_stack.push_back(new vector<unsigned>);
_branch_infor_stack.push_back(new branch_information);
_branch_infor_stack.back()->gid = _branch_infor_stack.back()->num_new_children = 0; // initialize!
_branch_infor_stack.back()->num_children_of_changed = 0;
#else
#ifdef FORMULA_CACHING // added by sang
//_formula_stack.push_back(new Component_value_pair);
//_formula_stack.back()->value = -1; // initialize formula sat probability to -1
_num_implication_stack.push_back(0);
//_hash_index_stack.push_back(new Hashindex);
//_hash_index_stack.back()->index = 0;
#ifdef APPROXIMATE_HASHING
_hash_index_stack.back()->secondary_index = 0;
#endif
#endif
#endif
}
assert (_assignment_stack.size() == num_variables() + 1);
}
int CSolver::add_variable(void)
{
int num = CDatabase::add_variable();
++_stats.num_free_variables;
while (_assignment_stack.size() <= num_variables())
{
_assignment_stack.push_back(new vector<int>);
#ifdef NON_ACTIVE_STACK
_non_active_free_var_stack.push_back(new vector<int>);
#endif
#ifdef TOCOMPONENTS
_num_implication_stack.push_back(0);
//_component_infor_stack.push_back(new vector<Component_information>);
//_clause_component_stack.push_back(new vector<unsigned>);
_branch_infor_stack.push_back(new branch_information);
_branch_infor_stack.back()->gid = _branch_infor_stack.back()->num_new_children = 0; // initialize!
_branch_infor_stack.back()->num_children_of_changed = 0;
#else
#ifdef FORMULA_CACHING // added by sang
_formula_stack.push_back(new Component_value_pair);
_formula_stack.back()->value = -1; // initialize formula sat probability to -1
_num_implication_stack.push_back(0);
_hash_index_stack.push_back(new Hashindex);
_hash_index_stack.back()->index = 0;
#ifdef APPROXIMATE_HASHING
_hash_index_stack.back()->secondary_index = 0;
#endif
#endif // FORMULA_CACHING
#endif // TOCOMPONENTS
}
assert (_assignment_stack.size() == num_variables() + 1);
return num;
}
void CSolver::set_mem_limit(int s)
{
CDatabase::set_mem_limit(s);
}
void CSolver::set_decision_strategy(int s)
{
_params.decision.strategy = s;
}
void CSolver::enable_cls_deletion(bool allow)
{
_params.cls_deletion.enable = allow;
}
void CSolver::set_cls_del_interval(int n)
{
_params.cls_deletion.interval = n;
}
// old functions for clause deletion
/*
void CSolver::set_max_unrelevance(int n )
{
_params.cls_deletion.max_unrelevance = n;
}
void CSolver::set_min_num_clause_lits_for_delete(int n)
{
_params.cls_deletion.min_num_lits = n;
}
void CSolver::set_max_conflict_clause_length(int l)
{
_params.cls_deletion.max_conf_cls_size = l;
}*/
void CSolver::set_randomness(int n)
{
_params.decision.base_randomness = n;
}
void CSolver::set_random_seed(int seed) {
srand (seed);
}
void CSolver::add_hook( HookFunPtrT fun, int interval)
{
pair<HookFunPtrT, int> a(fun, interval);
_hooks.push_back(pair<int, pair<HookFunPtrT, int> > (0, a));
}
void CSolver::run_periodic_functions(void)
{
//a. clause deletion
/*if ( _params.cls_deletion.enable &&
_stats.num_backtracks > _stats.next_cls_deletion) {
_stats.next_cls_deletion = _stats.num_backtracks + _params.cls_deletion.interval;
delete_unrelevant_clauses();
}*/
if (_stats.num_active_added_conf_clauses > _stats.next_cls_deletion)// if there are too many learned clauses, remove some
{
_stats.next_cls_deletion += _params.cls_deletion.interval; // check and delete clauses every 5000 clauses added
/*cout << "before delete clauses: " << endl;
dump_assignment_stack();
dump_implication_queue();
dump_cross_implications();
dump_branchable_component_stack();
dump_branch_infor_stack();
dump_components(_components);*/
delete_unrelevant_clauses();
/*cout << "after delete clauses: " << endl;
dump_assignment_stack();
dump_implication_queue();
dump_cross_implications();
dump_branchable_component_stack();
dump_branch_infor_stack();
dump_components(_components);*/
}
/*
//b. restart
if (_params.restart.enable &&
_stats.num_backtracks > _stats.next_restart) {
_stats.next_restart = _stats.num_backtracks + _stats.restart_incr;
_stats.restart_incr += _params.restart.backtrack_incr_incr;
restart();
}
*/
//c. update var stats for decision
if (_stats.num_decisions > _stats.next_var_score_decay)
{
_stats.next_var_score_decay = _stats.num_decisions + _params.decision.decay_period;
//cout << "before decaying: num_conflicts = " << num_conflicts
// << ", num_sat = " << num_sat << endl;
//num_conflicts = num_conflicts >> 2;
//num_sat = num_sat >> 2;
//cout << "after decaying: num_conflicts = " << num_conflicts
// << ", num_sat = " << num_sat << endl;
if (dynamic_heuristic >= 4) // heuristic 0-3 are sum/degree based, no need to decay scores
decay_variable_score();
}
/*
//d. run hook functions
for (unsigned i=0; i< _hooks.size(); ++i) {
pair<int,pair<HookFunPtrT, int> > & hook = _hooks[i];
if (_stats.num_decisions >= hook.first) {
hook.first += hook.second.second;
hook.second.first((void *) this);
}
}
*/
}
void CSolver::init_solve(void)
{
CDatabase::init_stats();
re_init_stats();
_stats.been_reset = false;
assert (_conflicts.empty());
assert (_conflict_lits.empty());
assert (_num_marked == 0);
assert (_num_in_new_cl == 0);
assert (_dlevel == 0);
//for (unsigned i=0, sz = variables().size(); i< sz; ++i) {
if (dynamic_heuristic == VSIDS)
for (unsigned i=1, sz = variables().size(); i < sz; ++i)
{
variable(i).score(0) = variable(i).lits_count(0);
variable(i).score(1) = variable(i).lits_count(1);
}
else if (dynamic_heuristic == VSADS)
for (unsigned i=1, sz = variables().size(); i < sz; ++i)
variable(i).score(0) = variable(i).score(1) = 0;
//_ordered_vars.resize( num_variables());
//update_var_score();
//DBG2(dump());
}
void CSolver::set_var_value(int v, int value, ClauseIdx ante, int dl)
{
assert (dl == dlevel()); // added by sang
assert (value == 0 || value == 1);
DBG2(dump());
//CHECK(verify_integrity());
DBG1 (cout << "Setting\t" << (value>0?"+":"-") << v
<< " at " << dlevel() << " because " << ante<< endl;);
CVariable & var = _variables[v];
assert (var.value() == UNKNOWN);
//{ // this implication might affect some other unbranched components
/* vector<int> & var_set = * _component_infor_stack[_branch_infor_stack[dlevel()]->gid]->var_set;
if (!binary_search(var_set.begin(), var_set.end(), v))
{
//return; // cross implications turned off
//if (flag)
cout << "current branching component " << _branch_infor_stack[dlevel()]->gid
<< ", cross implication of var " << v
<< ", decisions = " << _stats.num_decisions << endl;
//--_num_implication_stack[dlevel()];
// put the cross-component-implication in the list for later check
cross_implication_list.push_back(new cross_implication);
cross_implication_list.back()->vid = v;
cross_implication_list.back()->level = dlevel();
cross_implication_list.back()->marked = false;
return; // cross implications turned off
}
else
++_num_implication_stack[dlevel()];
*/
//}
//else
// ++_num_implication_stack[dlevel()];
var.set_dlevel(dl);
var.set_value(value);
var.antecedent() = ante;
#ifdef KEEP_LIT_CLAUSES // added by sang, update counter_one for "half" clauses
/*#ifdef DEBUG_OUT
cout << "set var " << v << ": " << value << endl;
cout << "var " << v << ", pos: " << endl << "(";
for (int j=0; j< var.lit_clause(0).size(); ++j)
cout << var.lit_clause(0)[j] << " " ;
cout << ")" << endl;
cout << "var " << v << ", neg: " << endl << "(";
for (int j=0; j< var.lit_clause(1).size(); ++j)
cout << var.lit_clause(1)[j] << " " ;
cout << ")" << endl;
//cout << "before update all counter_one" << endl;
//for (int k=0; k<_stats.num_original_clause; ++k)
// cout << "clause " << k << "'s counter_one: "
// << clause(k).counter_one() << endl;
#endif */
int sz;
if (value == 1)
{
sz = var.lit_clause(0).size();
for (int i=0; i< sz; ++i)
{
//cout << "increment counter_one of clause "
// << var.lit_clause(0)[i] << endl;
(clause(var.lit_clause(0)[i]).counter_one())++;
//if (clause(var.lit_clause(0)[i]).counter_one() == 1)
// _stats.num_unsatisfied_clause--;
}
}
else // value == 0
{
sz = var.lit_clause(1).size();
for (int i=0; i< sz; ++i)
{
//cout << "increment counter_one of clause "
// << var.lit_clause(1)[i] << endl;
(clause(var.lit_clause(1)[i]).counter_one())++;
//if (clause(var.lit_clause(1)[i]).counter_one() == 1)
// _stats.num_unsatisfied_clause--;
}
}
#ifdef DEBUG_OUT
/*cout << "after update all counter_one" << endl;
for (int k=0; k<_stats.num_original_clause; ++k)
cout << "clause " << k << "'s counter_one: "
<< clause(k).counter_one() << endl;*/
#endif
#endif
if (dl == dlevel())
set_var_value_current_dl(v, value);
else
set_var_value_not_current_dl(v, value);
++_stats.num_implications ;
if (var.is_branchable())
--num_free_variables();
}
void CSolver::set_var_value_current_dl(int v, int value)
{
vector<CLitPoolElement *> & watchs = variable(v).watched(value);
for (vector <CLitPoolElement *>::iterator itr = watchs.begin(); itr != watchs.end(); ++itr) {
ClauseIdx cl_idx;
CLitPoolElement * other_watched;
CLitPoolElement * watched = *itr;
//watched = watched + watchs.size() - 1;
//cout << "watched clause index: " << watched->get_clause_index() << endl;
#ifdef DISABLE_CLASUES
if (clause( watched->find_clause_index()).counter_one() > 1) // make the watched lit possible to move to 1
continue; // added by sang, seems not good, because of added conflict clauses
#endif
int dir = watched->direction();
CLitPoolElement * ptr = watched;
//if (v == 329)
// cout << "got before while" << endl;
while(1) {
ptr += dir;
if (ptr->val() <= 0) { //reached one end of the clause
if (dir == 1) //reached the right end, i.e. spacing element is the cl_id
cl_idx = ptr->get_clause_index();
if (dir == watched->direction()) { //we haven't go both directions.
ptr = watched;
dir = -dir; //change direction, go the other way
continue;
}
//otherwise, we have already gone through the whole clause
int the_value = literal_value (*other_watched);
if (the_value == 0) //a conflict
_conflicts.push_back(cl_idx);
else if ( the_value != 1) //i.e. unknown
{
int unit = other_watched->s_var() >> 1;
//vector<int> & var_set = * _component_infor_stack[_branch_infor_stack[dlevel()]->gid]->var_set;
queue_implication(other_watched->s_var(), cl_idx, dlevel());
/*if (binary_search(var_set.begin(), var_set.end(), unit))
queue_implication(other_watched->s_var(), cl_idx, dlevel());
else if (cross_enabled)
{
//if (flag)
//cout << "branching component " << _branch_infor_stack[dlevel()]->gid
// << ", cross implication of var " << (other_watched->s_var() >> 1)
// << ", decisions = " << _stats.num_decisions << endl;
bool found = false;
// check if this cross-implication has already been generated at this level
for (int i = cross_implication_list.size() - 1; i >= 0 ; --i)
{
if (cross_implication_list[i]->level != dlevel())
break;
if (cross_implication_list[i]->vid == unit)
{
found = true;
break;
}
}
if (!found) // don't add the duplicate cross implication!
{
--_num_implication_stack[dlevel()];
++_stats.num_cross_implications;
// put the cross-component-implication in the list for later check
cross_implication_list.push_back(new cross_implication);
cross_implication_list.back()->vid = unit;
cross_implication_list.back()->level = dlevel();
cross_implication_list.back()->marked = false;
queue_implication(other_watched->s_var(), cl_idx, dlevel());
}
}*/
}
break;
}
if (ptr->is_watched()) { //literal is the other watched lit, skip it.
other_watched = ptr;
continue;
}
if (literal_value(*ptr) == 0) // literal value is 0, keep going
continue;
//now the literal's value is either 1 or unknown, watch it instead
int v1 = ptr->var_index();
int sign = ptr->var_sign();
variable(v1).watched(sign).push_back(ptr);
ptr->set_watch(dir);
//remove the original watched literal from watched list
watched->unwatch();
*itr = watchs.back(); //copy the last element in it's place
watchs.pop_back(); //remove the last element
--itr; //do this so with don't skip one during traversal
break;
}
}
}
void CSolver::set_var_value_not_current_dl(int v, int value)
{
//the only difference between this one and the last function
//is that because the assignment var is not at current_dl,
//(that means, < current_dl), it's possible some variable
//have a dlevel higher than the assigned one. So, we need
//to make sure that watched literal has the highest dlevel if
//we can't find other unassigned or value 1 literals
//also, it's possible we need to readjust decision levels of
//some variables implied.
ClauseIdx cl_idx;
CLitPoolElement * watched, * other_watched, * ptr, * max_ptr = NULL;
int dir,max_dl;
vector<CLitPoolElement *> & watchs = variable(v).watched(value);
for (vector <CLitPoolElement *>::iterator itr = watchs.begin();
itr != watchs.end(); ++itr) {
max_dl = -1;
watched = *itr;
#ifdef DISABLE_CLASUES // added by sang
if (clause( watched->find_clause_index()).counter_one())
continue; // added by sang, seems not good, because of added conflict clauses
#endif
dir = watched->direction();
ptr = watched;
while(1) {
ptr += dir;
if (ptr->val() <= 0) {
if (dir == 1)
cl_idx = ptr->get_clause_index();
if (dir == watched->direction()) {
ptr = watched;
dir = -dir;
continue;
}
CLitPoolElement * current_watched = watched;
if (variable(watched->var_index()).dlevel() < max_dl) {
int v1 = max_ptr->var_index();
int sign = max_ptr->var_sign();
variable(v1).watched(sign).push_back(max_ptr);
max_ptr->set_watch(dir);
watched->unwatch();
*itr = watchs.back();
watchs.pop_back();
--itr;
current_watched = max_ptr;
}
int max = variable(current_watched->var_index()).dlevel();
int the_value = literal_value (*other_watched);
if (the_value == 0) {
//it's a conflict, but we will not put into _conflicts
//instead, make it a implication so it will be resolved
//in deduce()
assert (variable(other_watched->var_index()).dlevel() >= max);
// cout << "Queueing Conflict for " << other_watched->var_index() << " orig DL: "
// << variable(other_watched->var_index()).dlevel() << " current DL: "
// << max << endl;
queue_implication(other_watched->s_var(), cl_idx, max);
}
else if ( the_value == 1) {
int v1 = other_watched->var_index();
if (variable(v1).dlevel() > max)
queue_implication(other_watched->s_var(), cl_idx, max);
}
else
queue_implication (other_watched->s_var(), cl_idx, max);
break;
}
if (ptr->is_watched()) {
other_watched = ptr;
continue;
}
if (literal_value(*ptr) == 0) {
//keep track of the 0 literal with max decision level
int ptr_dl = variable(ptr->var_index()).dlevel();
if ( ptr_dl > max_dl) {
max_dl = ptr_dl;
max_ptr = ptr;
}
continue;
}
//now it's value is either 1 or unknown
int v1 = ptr->var_index();
int sign = ptr->var_sign();
variable(v1).watched(sign).push_back(ptr);
watched->unwatch();
ptr->set_watch(dir);
*itr = watchs.back();
watchs.pop_back();
--itr;
break;
}
}
}
void CSolver::unset_var_value(int v)
{
if (v == 0) return;
DBG1(cout <<"Unset var " << (variable(v).value()?"+":"-") << v << endl;);
CVariable & var = variable(v);
#ifdef KEEP_LIT_CLAUSES
int sz;
if (var.value() == 1)
{
sz = var.lit_clause(0).size();
for (int i=0; i< sz; ++i)
{
clause(var.lit_clause(0)[i]).counter_one()--;
//if (clause(var.lit_clause(0)[i]).counter_one() == 0)
#ifdef DISABLE_CLASUES
//if (var.lit_clause(0)[i] < _stats.num_original_clause) // newly added
#endif
// _stats.num_unsatisfied_clause++;
}
}
else // var.value() == 0
{
sz = var.lit_clause(1).size();
for (int i=0; i< sz; ++i)
{
clause(var.lit_clause(1)[i]).counter_one()--;
//if (clause(var.lit_clause(1)[i]).counter_one() == 0)
#ifdef DISABLE_CLASUES
//if (var.lit_clause(1)[i] < _stats.num_original_clause) // newly added
#endif
// _stats.num_unsatisfied_clause++;
}
}
#endif
var.set_value(UNKNOWN);
var.set_antecedent(NULL_CLAUSE);
var.set_dlevel( -1);
var.assgn_stack_pos() = -1;
if (var.is_branchable()) {
++num_free_variables();
if (var.var_score_pos() < _max_score_pos)
_max_score_pos = var.var_score_pos();
}
}
int CSolver::find_max_clause_dlevel(ClauseIdx cl)
{
//if cl has single literal, then the dlevel for it should be 0
//thus, we need to set initial max_level = 0 instead
//of -1 because if clause has single literal, then the
//loop will found no literal with value UNKNOWN
int max_level = 0;
if (cl == NULL_CLAUSE)
return dlevel();
for (unsigned i=0, sz= clause(cl).num_lits(); i<sz; ++i) {
int var_idx =((clause(cl).literals())[i]).var_index();
if (_variables[var_idx].value() != UNKNOWN) {
if (max_level < _variables[var_idx].dlevel())
max_level = _variables[var_idx].dlevel();
}
}
return max_level;
}
void CSolver::dump_assignment_stack(ostream & os ) {
cout << "Assignment Stack: " << endl;
for (int i=0; i<= dlevel(); ++i)
{
cout << "(" <<i << ":";
if (_assignment_stack[i]->size() > 0)
{
cout << ((*_assignment_stack[i])[0]&0x1?"-":"+")
<< ((*_assignment_stack[i])[0] >> 1);
if (variable((*_assignment_stack[i])[0] >> 1).tried_both())
cout << "* ";
else
cout << " ";
}
for (unsigned j=1; j<(_assignment_stack[i])->size(); ++j )
cout << ((*_assignment_stack[i])[j]&0x1?"-":"+")
<< ((*_assignment_stack[i])[j] >> 1) << " ";
cout << ") ";
}
cout << endl;
}
void CSolver::dump_implication_queue(ostream & os)
{
_implication_queue.dump(os);
cout << endl;
}
void CSolver::delete_clause_group (int gid)
{
assert( is_gid_allocated(gid) );
//if (_stats.been_reset==false)
reset(); //if delete some clause, then implication queue are invalidated
for (vector<CClause>::iterator itr1 = clauses().begin();
itr1 != clauses().end(); ++itr1) {
CClause & cl = * itr1;
if (cl.status() != DELETED_CL) {
if (cl.gid(gid) == true) {
mark_clause_deleted(cl);
}
}
}
//delete the index from variables
for (vector<CVariable>::iterator itr = variables().begin();
itr != variables().end(); ++ itr) {
for (int i=0; i<2; ++i) { //for each phase
//delete the lit index from the vars
/*#ifdef KEEP_LIT_CLAUSES
vector<ClauseIdx> & lit_clauses = (*itr).lit_clause(i);
for (vector<ClauseIdx>::iterator itr1 = lit_clauses.begin();
itr1 != lit_clauses.end(); ++ itr1)
if ( clause(*itr1).status() == DELETED_CL ) {
*itr1 = lit_clauses.back();
lit_clauses.pop_back();
-- itr1;
}
#endif*/
//delete the watched index from the vars
vector<CLitPoolElement *> & watched = (*itr).watched(i);
for (vector<CLitPoolElement *>::iterator itr1 = watched.begin();
itr1 != watched.end(); ++itr1)
if ( (*itr1)->val() <= 0) {
*itr1 = watched.back();
watched.pop_back();
--itr1;
}
}
}
free_gid(gid);
//_gid = 0; // reset _gid to 0!
//cout << "reset _gid to 0!" << endl;
}
void CSolver::reset(void)
{
if (_stats.been_reset)
return;
if (num_variables()==0) return;
//back_track(0); commented out by sang
//_conflicts.clear();commented out by sang
while (!_implication_queue.empty())
_implication_queue.pop();
_stats.is_solver_started = false;
_stats.outcome = UNDETERMINED;
_stats.been_reset = true;
}
// old version
/*
void CSolver::delete_unrelevant_clauses(void)