-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMainSolver.cpp
More file actions
executable file
·1601 lines (1310 loc) · 40.7 KB
/
MainSolver.cpp
File metadata and controls
executable file
·1601 lines (1310 loc) · 40.7 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 "MainSolver.h" // class's header file
// class constructor
CMainSolver::CMainSolver() :
decStack(*this)
{
stopWatch.setTimeBound(CSolverConf::secsTimeBound);
remPoll = 0;
}
// class destructor
CMainSolver::~CMainSolver()
{
toDEBUGOUT("removed. Poll: " << remPoll << endl);
}
bool CMainSolver::performPreProcessing()
{
if (!prepBCP())
{
theRunAn.setExitState(SUCCESS);
stopWatch.markStopTime();
return false;
}
if (CSolverConf::allowPreProcessing)
{
toSTDOUT("BEGIN preprocessing" << endl);
if (!prepFindHiddenBackBoneLits())
{
theRunAn.setExitState(SUCCESS);
toSTDOUT("ERR: UNSAT Formula" << endl);
stopWatch.markStopTime();
return false;
}
toSTDOUT(endl << "END preprocessing" << endl);
}
prep_CleanUpPool();
toSTDOUT("#Vars remaining:" << countAllVars() << endl);
toSTDOUT("#Clauses remaining:" << countAllClauses() << endl);
toSTDOUT("#bin Cls remaining:" << countBinClauses() << endl);
return true;
}
void CMainSolver::solve(const char *lpstrFileName)
{
SOLVER_StateT exSt;
stopWatch.markStartTime();
num_Nodes = 3;
enable_DT_recording = true;
createfromFile(lpstrFileName);
decStack.init(countAllVars());
CStepTime::makeStart();
xFormulaCache.init();
bdg_var_count = originalVarCount;
// Create the initial LIT nodes for quick caching
for (int i = 0; i <= originalVarCount; ++i)
{
//Original:
litNodes.push_back(new DTNode(i, true, num_Nodes++));
litNodes.push_back(new DTNode(-1 * i, true, num_Nodes++));
}
toSTDOUT("#Vars:" << countAllVars() << endl);
toSTDOUT("#Clauses:" << countAllClauses() << endl);
toSTDOUT("#bin Cls:" << countBinClauses() << endl);
bool needToCount = performPreProcessing();
theRunAn.setRemovedClauses(theRunAn.getData().nRemovedClauses
+ theRunAn.getData().nOriginalClauses - getMaxOriginalClIdx() + 1
- countBinClauses());
if (needToCount)
{
// the following call only correct if bin clauses not used for caching
CCacheEntry::adjustPackSize(countAllVars(), getMaxOriginalClIdx());
lastTimeCClDeleted = CStepTime::getTime();
lastCClCleanUp = CStepTime::getTime();
makeCompIdFromActGraph(decStack.TOSRefComp());
bcpImplQueue.clear();
bcpImplQueue.reserve(countAllVars());
// the size of componentSearchStack has to be reserved, otherwise getComp(...) might
// become erroneous due to realloc
componentSearchStack.reserve(countAllVars() + 2);
exSt = countSAT();
theRunAn.setExitState(exSt);
theRunAn.setSatCount(decStack.top().getOverallSols());
}
else
{
theRunAn.setExitState(SUCCESS);
theRunAn.setSatCount(0.0);
}
stopWatch.markStopTime();
theRunAn.setElapsedTime(stopWatch.getElapsedTime());
xFormulaCache.printStatistics(theRunAn);
// There may have been some translation done during the preprocessing
// phase, so we translate the bdg literals back.
decStack.top().getDTNode()->uncheck(1);
translateLiterals(getVarTranslation());
// We also need to add all of the backbones found in preprocessing, but
// we only do this if there are solutions.
if (decStack.top().getOverallSols() != 0)
{
vector<LiteralIdT>::iterator bbit;
for (bbit = backbones.begin(); bbit != backbones.end(); bbit++)
{
// Check to make sure that the node hasn't been altered
if (bbit->toSignedInt() == get_lit_node(bbit->toSignedInt())->getVal())
decStack.top().getCurrentDTNode()->addChild(get_lit_node(bbit->toSignedInt()), true);
else
decStack.top().getCurrentDTNode()->addChild(get_lit_node_full(bbit->toSignedInt()), true);
}
}
// We compress the tree now that the search is finished
decStack.top().getDTNode()->uncheck(2);
std::cout << "Uncompressed Edges: " << decStack.top().getDTNode()->count(true) << endl;
decStack.top().getDTNode()->uncheck(3);
decStack.top().getDTNode()->compressNode();
decStack.top().getDTNode()->uncheck(4);
bdg_edge_count = decStack.top().getDTNode()->count(true);
std::cout << "Compressed Edges: " << bdg_edge_count << endl;
// There may have been some translation done during the file parsing
// phase, so we translate the bdg literals back.
decStack.top().getDTNode()->uncheck(5);
// We may have litnodes that aren't used yet (but will after smoothing).
// Thus, we need to make sure that all litnodes are unchecked.
for (int i = 0; i < litNodes.size(); i++)
litNodes[i]->uncheck(5);
translateLiterals(getOrigTranslation());
if (CSolverConf::smoothNNF) {
// Smooth the d-DNNF (note: this may cause AND-AND parent-children)
set<int> literals;
decStack.top().getDTNode()->uncheck(6);
decStack.top().getDTNode()->smooth(num_Nodes, *this, literals);
// See if we've got every literal in the d-DNNF
if (2 * originalVarCount == literals.size())
return;
// TODO: Fix an AND node to the top
if (DT_NodeType::kDTAnd != decStack.top().getDTNode()->getType())
toSTDOUT("Error: The top node wasn't an AND node.");
// Make sure that every literal appears some place
DTNode* botNode = new DTNode(DT_NodeType::kDTAnd, num_Nodes++);
botNode->botIfy();
for (int i = 1; i <= originalVarCount; ++i) {
// Check if neither exist
if ((literals.find(i) == literals.end()) &&
(literals.find(-1 * i) == literals.end())) {
// Add an arbitrary choice between the two
DTNode* newOr = new DTNode(DT_NodeType::kDTOr, num_Nodes++);
newOr->choiceVar = (unsigned) i;
decStack.top().getDTNode()->addChild(newOr, true);
newOr->addChild(new DTNode(i, true, num_Nodes++), true);
newOr->addChild(new DTNode((-1 * i), true, num_Nodes++), true);
} else if ((literals.find(i) == literals.end()) && CSolverConf::ensureAllLits) {
DTNode* newOr = new DTNode(DT_NodeType::kDTOr, num_Nodes++);
DTNode* newAnd = new DTNode(DT_NodeType::kDTAnd, num_Nodes++);
get_lit_node_full(-1 * i)->sub_parents(newOr);
newOr->addChild(get_lit_node_full(-1 * i), true);
newOr->addChild(newAnd, true);
newOr->choiceVar = (unsigned) i;
newAnd->addChild(new DTNode(i, true, num_Nodes++), true);
newAnd->addChild(botNode, true);
} else if ((literals.find(-1 * i) == literals.end()) && CSolverConf::ensureAllLits) {
DTNode* newOr = new DTNode(DT_NodeType::kDTOr, num_Nodes++);
DTNode* newAnd = new DTNode(DT_NodeType::kDTAnd, num_Nodes++);
get_lit_node_full(i)->sub_parents(newOr);
newOr->addChild(get_lit_node_full(i), true);
newOr->addChild(newAnd, true);
newOr->choiceVar = (unsigned) i;
newAnd->addChild(new DTNode((-1 * i), true, num_Nodes++), true);
newAnd->addChild(botNode, true);
}
}
}
}
SOLVER_StateT CMainSolver::countSAT()
{
retStateT res;
while (true)
{
while (decide())
{
if (stopWatch.timeBoundBroken())
return TIMEOUT;
while (!bcp())
{
res = resolveConflict();
if (res == EXIT)
return SUCCESS;
if (res == PROCESS_COMPONENT)
break; //force calling decide()
}
}
res = backTrack();
if (res == EXIT)
return SUCCESS;
while (res != PROCESS_COMPONENT && !bcp())
{
res = resolveConflict();
if (res == EXIT)
return SUCCESS;
}
}
}
bool CMainSolver::findVSADSDecVar(LiteralIdT &theLit,
const CComponentId & superComp)
{
vector<VarIdT>::const_iterator it;
int score = -1;
int pr_score = -1;
int bo;
PVARV pVV = NULL;
PVARV pr_pVV = NULL;
for (it = superComp.varsBegin(); *it != varsSENTINEL; it++)
if (getVar(*it).isActive())
{
bo = (int) getVar(*it).getVSIDSScore() + getVar(*it).getDLCSScore();
if (bo > score)
{
score = bo;
pVV = &getVar(*it);
}
if (0 != priorityVars.count(getVar(*it).getVarNum()))
{
if (bo > pr_score)
{
pr_score = bo;
pr_pVV = &getVar(*it);
}
}
}
// If we have a priority variable remaining, then take the best one as a decision
if (-1 != pr_score) {
score = pr_score;
pVV = pr_pVV;
}
if (pVV == NULL)
return false;
bool pol = pVV->scoreDLIS[true] + pVV->scoreVSIDS[true]
> pVV->scoreDLIS[false] + pVV->scoreVSIDS[false];
theLit = pVV->getLitIdT(pol);
return true;
}
bool CMainSolver::decide()
{
LiteralIdT theLit(NOT_A_LIT);
CStepTime::stepTime(); // Solver-Zeit
if (CStepTime::getTime() - xFormulaCache.getLastDivTime()
> xFormulaCache.getScoresDivTime())
{
xFormulaCache.divCacheScores();
xFormulaCache.setLastDivTime(CStepTime::getTime());
}
if (!bcpImplQueue.empty())
return true;
if (!decStack.TOS_hasAnyRemComp())
{
recordRemainingComps();
if (decStack.TOS_countRemComps() == 0)
{
handleSolution();
return false;
}
}
if (!findVSADSDecVar(theLit, decStack.TOS_NextComp())
|| decStack.TOS_NextComp().getClauseCount() == 0) // i.e. component satisfied
{
handleSolution();
decStack.TOS_popRemComp();
return false;
}
//checkCachedCompVal:
static CRealNum cacheVal;
//decStack.TOS_NextComp();
if (CSolverConf::allowComponentCaching && xFormulaCache.extract(
decStack.TOS_NextComp(), cacheVal,
decStack.top().getCurrentDTNode()))
{
decStack.top().includeSol(cacheVal);
theRunAn.addValue(SOLUTION, decStack.getDL());
decStack.TOS_popRemComp();
return false;
}
/////////////////////////////
// Create the nodes
DTNode * newNode = new DTNode(DT_NodeType::kDTOr, num_Nodes++);
DTNode * left = new DTNode(DT_NodeType::kDTAnd, num_Nodes++);
DTNode * right = new DTNode(DT_NodeType::kDTAnd, num_Nodes++);
DTNode * leftLit = get_lit_node(theLit.toSignedInt());
DTNode * rightLit = get_lit_node(-1 * theLit.toSignedInt());
newNode->choiceVar = theLit.toVarIdx();
// Set the parents
left->addParent(newNode, true);
leftLit->addParent(left, true);
right->addParent(newNode, true);
rightLit->addParent(right, true);
newNode->addParent(decStack.top().getCurrentDTNode(), true);
decStack.push(newNode);
bcpImplQueue.clear();
bcpImplQueue.push_back(AntAndLit(NOT_A_CLAUSE, theLit));
theRunAn.addValue(DECISION, decStack.getDL());
if (theRunAn.getData().nDecisions % 255 == 0)
{
doVSIDSScoreDiv();
}
return true;
}
void CMainSolver::removeAllCachePollutions()
{
// We are removing the pollutions because a conflict was found, so the
// current decision level must be reset to only contain the literal.
// TODO: Confirm that this is not required for the graph
//decStack.top().getCurrentDTNode()->reset();
// check if one sibling has modelcount 0
// if so, the other siblings may have polluted the cache --> remove pollutions
// cout << decStack.getDL()<<" "<<endl;
for (vector<CComponentId *>::iterator it = decStack.TOSRemComps_begin(); it
!= decStack.getAllCompStack().end(); it++)
// if component *it is cached remove it and all of its descendants
if ((*it)->cachedAs != 0)
{
remPoll += xFormulaCache.removePollutedEntries((*it)->cachedAs);
(*it)->cachedAs = 0;
(*it)->cachedChildren.clear();
}
// it might occur that *it is not yet cached, but has descedants that are cached
// thus we have to delete them
else if (!(*it)->cachedChildren.empty())
{
for (vector<unsigned int>::iterator ct =
(*it)->cachedChildren.begin(); ct
!= (*it)->cachedChildren.end(); ct++)
{
remPoll += xFormulaCache.removePollutedEntries(*ct);
}
}
xFormulaCache.revalidateCacheLinksIn(decStack.getAllCompStack());
}
retStateT CMainSolver::backTrack()
{
unsigned int refTime = theRunAn.getData().nAddedClauses;
LiteralIdT aLit;
if (refTime - lastTimeCClDeleted > 1000 && countCCls() > 3000)
{
deleteConflictCls();
lastTimeCClDeleted = refTime;
}
if (refTime - lastCClCleanUp > 35000)
{
cleanUp_deletedCCls();
toSTDOUT("cleaned:");
printCClstats();
lastCClCleanUp = refTime;
}
//component cache delete old entries
if (CSolverConf::allowComponentCaching)
xFormulaCache.deleteEntries(decStack);
do
{
if (decStack.top().getBranchSols() == 0.0)
{
removeAllCachePollutions();
}
if (decStack.top().anotherCompProcessible())
{
return PROCESS_COMPONENT;
}
if (!decStack.top().isFlipped())
{
aLit = decStack.TOS_decLit();
decStack.flipTOS();
bcpImplQueue.push_back(AntAndLit(NOT_A_CLAUSE, aLit.oppositeLit()));
return RESOLVED;
}
//include the component value of the current component into the Cache
if (CSolverConf::allowComponentCaching)
{
xFormulaCache.include(decStack.TOSRefComp(),
decStack.top().getOverallSols(),
decStack.top().getOrDTNode());
}
} while (decStack.pop());
return EXIT;
}
bool CMainSolver::bcp()
{
bool bSucceeded;
vector<LiteralIdT>::iterator it;
//BEGIN process unit clauses
for (it = theUnitClauses.begin(); it != theUnitClauses.end(); it++)
{
if (getVar(*it).isActive())
bcpImplQueue.push_back(AntAndLit(NOT_A_CLAUSE, *it));
if (isUnitCl(it->oppositeLit()))
{
// dealing with opposing unit clauses is handed over to
// resolveConflict
return false;
}
}
//END process unit clauses
bSucceeded = BCP(bcpImplQueue);
bcpImplQueue.clear();
if (CSolverConf::allowImplicitBCP && bSucceeded)
{
bSucceeded = implicitBCP();
}
theRunAn.addValue(IMPLICATION, decStack.getDL(),
decStack.TOS_countImplLits());
return bSucceeded;
}
retStateT CMainSolver::resolveConflict()
{
int backtrackDecLev;
// Since we have a conflict, we should add a bottom to the current DTNode
DTNode * newBot = new DTNode(DT_NodeType::kDTBottom, num_Nodes++);
newBot->addParent(decStack.top().getCurrentDTNode(), true);
for (vector<LiteralIdT>::iterator it = theUnitClauses.begin(); it
!= theUnitClauses.end(); it++)
{
if (isUnitCl(it->oppositeLit()))
{
toSTDOUT("\nOPPOSING UNIT CLAUSES - INSTANCE UNSAT\n");
return EXIT;
}
}
theRunAn.addValue(CONFLICT, decStack.getDL(), 1);
#ifdef DEBUG
assert(!getConflicted().empty());
#endif
if (!CSolverConf::analyzeConflicts || getConflicted().empty())
return backTrack();
caGetCauses_firstUIP(getConflicted());
backtrackDecLev = getMaxDecLevFromAnalysis();
if (ca_1UIPClause.size() < ca_lastUIPClause.size())
create1UIPCCl();
//BEGIN Backtracking
if (backtrackDecLev < decStack.getDL())
{
if (CSolverConf::doNonChronBackTracking)
while (decStack.getDL() > backtrackDecLev)
{
/// check for polluted cache Entries
removeAllCachePollutions();
decStack.pop();
newBot = new DTNode(DT_NodeType::kDTBottom, num_Nodes++);
newBot->addParent(decStack.top().getCurrentDTNode(), true);
}
return backTrack();
}
// maybe the other branch had some solutions
if (decStack.top().isFlipped())
{
return backTrack();
}
// now: if the other branch has to be visited:
createAntClauseFor(decStack.TOS_decLit().oppositeLit());
LiteralIdT aLit = decStack.TOS_decLit();
AntecedentT ant = getVar(aLit).getAntecedent();
decStack.flipTOS();
bcpImplQueue.push_back(AntAndLit(ant, aLit.oppositeLit()));
//END Backtracking
return RESOLVED;
}
/////////////////////////////////////////////////////
// BEGIN component analysis
/////////////////////////////////////////////////////
bool CMainSolver::recordRemainingComps()
{
// the refComp has to be copied!! because in case that the vector
// containing it has to realloc (which might happen here)
// copying the refcomp is not necessary anymore, as the allCompsStack is a vector
// of pointers - realloc does not invalidate these
CComponentId & refSupComp = decStack.TOSRefComp();
viewStateT lookUpCls[getMaxOriginalClIdx() + 2];
viewStateT lookUpVars[countAllVars() + 2];
memset(lookUpCls, NIL, sizeof(viewStateT) * (getMaxOriginalClIdx() + 2));
memset(lookUpVars, NIL, sizeof(viewStateT) * (countAllVars() + 2));
vector<VarIdT>::const_iterator vt;
vector<ClauseIdT>::const_iterator itCl;
for (itCl = refSupComp.clsBegin(); *itCl != clsSENTINEL; itCl++)
{
lookUpCls[*itCl] = IN_SUP_COMP;
}
for (vt = refSupComp.varsBegin(); *vt != varsSENTINEL; vt++)
{
if (getVar(*vt).isActive())
{
lookUpVars[*vt] = IN_SUP_COMP;
getVar(*vt).scoreDLIS[true] = 0;
getVar(*vt).scoreDLIS[false] = 0;
}
}
vector<LiteralIdT>::const_iterator itL;
componentSearchStack.clear();
if (CSolverConf::disableDynamicDecomp)
{
decStack.TOS_addRemComp();
decStack.lastComp().setTrueClauseCount(0);
for (vt = refSupComp.varsBegin(); *vt != varsSENTINEL; vt++)
if (lookUpVars[*vt] == IN_SUP_COMP)
{
lookUpVars[*vt] = SEEN;
componentSearchStack.push_back(*vt);
getComp(*vt, decStack.TOSRefComp(), lookUpCls, lookUpVars);
}
decStack.lastComp().addVar(varsSENTINEL);
decStack.lastComp().addCl(clsSENTINEL);
}
else
{
for (vt = refSupComp.varsBegin(); *vt != varsSENTINEL; vt++)
if (lookUpVars[*vt] == IN_SUP_COMP)
{
decStack.TOS_addRemComp();
decStack.lastComp().setTrueClauseCount(0);
lookUpVars[*vt] = SEEN;
componentSearchStack.push_back(*vt);
getComp(*vt, decStack.TOSRefComp(), lookUpCls, lookUpVars);
decStack.lastComp().addVar(varsSENTINEL);
decStack.lastComp().addCl(clsSENTINEL);
}
}
decStack.TOS_sortRemComps();
return true;
}
bool CMainSolver::getComp(const VarIdT &theVar, const CComponentId &superComp,
viewStateT lookUpCls[], viewStateT lookUpVars[])
{
/* Moved to above */
//componentSearchStack.clear();
//lookUpVars[theVar] = SEEN;
//componentSearchStack.push_back(theVar);
vector<VarIdT>::const_iterator vt, itVEnd;
vector<LiteralIdT>::const_iterator itL;
vector<ClauseIdT>::const_iterator itCl;
CVariableVertex * pActVar;
unsigned int nClausesSeen = 0, nBinClsSeen = 0;
for (vt = componentSearchStack.begin(); vt != componentSearchStack.end(); vt++)
// the for-loop is applicable here because componentSearchStack.capacity() == countAllVars()
{
pActVar = &getVar(*vt);
//BEGIN traverse binary clauses
for (itL = pActVar->getBinLinks(true).begin(); *itL != SENTINEL_LIT; itL++)
if (lookUpVars[itL->toVarIdx()] == IN_SUP_COMP)
{
nBinClsSeen++;
lookUpVars[itL->toVarIdx()] = SEEN;
componentSearchStack.push_back(itL->toVarIdx());
getVar(*itL).scoreDLIS[itL->polarity()]++;
pActVar->scoreDLIS[true]++;
}
for (itL = pActVar->getBinLinks(false).begin(); *itL != SENTINEL_LIT; itL++)
if (lookUpVars[itL->toVarIdx()] == IN_SUP_COMP)
{
nBinClsSeen++;
lookUpVars[itL->toVarIdx()] = SEEN;
componentSearchStack.push_back(itL->toVarIdx());
getVar(*itL).scoreDLIS[itL->polarity()]++;
pActVar->scoreDLIS[false]++;
}
//END traverse binary clauses
for (itCl = var_InClsBegin(*pActVar); *itCl != SENTINEL_CL; itCl++)
if (lookUpCls[*itCl] == IN_SUP_COMP)
{
itVEnd = componentSearchStack.end();
for (itL = begin(getClause(*itCl)); *itL != ClauseEnd(); itL++)
if (lookUpVars[itL->toVarIdx()] == NIL) //i.e. the var is not active
{
if (!isSatisfied(*itL))
continue;
//BEGIN accidentally entered a satisfied clause: undo the search process
while (componentSearchStack.end() != itVEnd)
{
lookUpVars[componentSearchStack.back()]
= IN_SUP_COMP;
componentSearchStack.pop_back();
}
lookUpCls[*itCl] = NIL;
for (vector<LiteralIdT>::iterator itX = begin(
getClause(*itCl)); itX != itL; itX++)
{
if (getVar(*itX).scoreDLIS[itX->polarity()] > 0)
getVar(*itX).scoreDLIS[itX->polarity()]--;
}
//END accidentally entered a satisfied clause: undo the search process
break;
}
else
{
getVar(*itL).scoreDLIS[itL->polarity()]++;
if (lookUpVars[itL->toVarIdx()] == IN_SUP_COMP)
{
lookUpVars[itL->toVarIdx()] = SEEN;
componentSearchStack.push_back(itL->toVarIdx());
}
}
if (lookUpCls[*itCl] == NIL)
continue;
nClausesSeen++;
lookUpCls[*itCl] = SEEN;
}
}
/////////////////////////////////////////////////
// BEGIN store variables in resComp
/////////////////////////////////////////////////
decStack.lastComp().reserveSpace(componentSearchStack.size(), nClausesSeen);
for (vt = superComp.varsBegin(); *vt != varsSENTINEL; vt++)
if (lookUpVars[*vt] == SEEN) //we have to put a var into our component
{
decStack.lastComp().addVar(*vt);
lookUpVars[*vt] = IN_OTHER_COMP;
}
//decStack.lastComp().addVar(varsSENTINEL); // Moved to above
/////////////////////////////////////////////////
// END store variables in resComp
/////////////////////////////////////////////////
for (itCl = superComp.clsBegin(); *itCl != clsSENTINEL; itCl++)
if (lookUpCls[*itCl] == SEEN)
{
decStack.lastComp().addCl(*itCl);
lookUpCls[*itCl] = IN_OTHER_COMP;
}
//decStack.lastComp().addCl(clsSENTINEL); // Moved to above
decStack.lastComp().addTrueClauseCount(nClausesSeen + nBinClsSeen);
return true;
}
/////////////////////////////////////////////////////
// END component analysis
/////////////////////////////////////////////////////
void CMainSolver::makeCompIdFromActGraph(CComponentId& rComp)
{
DepositOfClauses::iterator it;
DepositOfVars::iterator jt;
rComp.clear();
unsigned int nBinClauses = 0;
unsigned int idx = 1;
for (it = beginOfClauses(); it != endOfClauses(); it++, idx++)
{
#ifdef DEBUG
assert(!it->isDeleted()); // deleted Cls should be cleaned by prepCleanPool
#endif
rComp.addCl(ClauseIdT(idx));
}
rComp.addCl(clsSENTINEL);
idx = 1;
for (jt = beginOfVars(); jt != endOfVars(); jt++, idx++)
{
#ifdef DEBUG
assert(jt->isActive()); // deleted Vars should be cleaned by prepCleanPool
#endif
rComp.addVar(idx);
nBinClauses += countActiveBinLinks(idx);
}
rComp.addVar(varsSENTINEL);
nBinClauses >>= 1;
rComp.setTrueClauseCount(nBinClauses + rComp.countCls());
}
void CMainSolver::printComponent(const CComponentId& rComp)
{
vector<ClauseIdT>::const_iterator it;
for (it = rComp.clsBegin(); *it != clsSENTINEL; it++)
{
printActiveClause(*it);
}
}
///////////////////////////////////////////////
bool CMainSolver::BCP(vector<AntAndLit> &thePairsOfImpl)
{
extd_vector<ClauseIdT>* pUnWatchCls;
extd_vector<ClauseIdT>::iterator it;
bool retV;
ClauseIdT actCl;
PCLV pCl;
vector<LiteralIdT>::const_iterator bt;
vector<LiteralIdT>::iterator itL;
LiteralIdT satLit, unLit;
getConflicted().clear();
for (unsigned int i = 0; i < thePairsOfImpl.size(); i++)
if (assignVal(thePairsOfImpl[i].getLit(), thePairsOfImpl[i].getAnt()))
{
satLit = thePairsOfImpl[i].getLit();
unLit = thePairsOfImpl[i].getLit().oppositeLit();
decStack.TOS_addImpliedLit(satLit);
#ifdef FULL_DDNNF
if (enable_DT_recording)
{
DTNode * satLitDTNode = get_lit_node(satLit.toSignedInt());
satLitDTNode->addParent(decStack.top().getCurrentDTNode(), true);
dirtyLitNodes.push_back(pair<DTNode *, DTNode *> (
decStack.top().getCurrentDTNode(), satLitDTNode));
}
#endif
pUnWatchCls = &getVar(unLit).getWatchClauses(unLit.polarity());
//BEGIN Propagate Bin Clauses
/* First propagate the original binary clauses */
for (bt = getVar(unLit).getBinLinks(unLit.polarity()).begin(); *bt
!= SENTINEL_LIT; bt++)
{
if (getVar(*bt).isActive())
{
thePairsOfImpl.push_back(AntAndLit(unLit, *bt));
#ifdef FULL_DDNNF
if (enable_DT_recording)
{
DTNode * ccLit = get_lit_node((*bt).toSignedInt());
ccLit->addParent(decStack.top().getCurrentDTNode(), true);
dirtyLitNodes.push_back(pair<DTNode *, DTNode*> (
decStack.top().getCurrentDTNode(), ccLit));
}
#endif
}
else if (!isSatisfied(*bt))
{
getConflicted().push_back(unLit);
getConflicted().push_back(*bt);
return false;
}
}
/* Next propagate the conflict generated binary clauses */
for (bt = bt + 1; *bt != SENTINEL_LIT; bt++)
{
if (getVar(*bt).isActive())
{
thePairsOfImpl.push_back(AntAndLit(unLit, *bt));
if (enable_DT_recording)
{
// Add the implied literal due to a conflict clause
DTNode * ccLit = get_lit_node((*bt).toSignedInt());
ccLit->addParent(decStack.top().getCurrentDTNode(), true);
dirtyLitNodes.push_back(pair<DTNode *, DTNode*> (
decStack.top().getCurrentDTNode(), ccLit));
}
}
else if (!isSatisfied(*bt))
{
getConflicted().push_back(unLit);
getConflicted().push_back(*bt);
return false;
}
}
//END Propagate Bin Clauses
for (it = pUnWatchCls->end() - 1; *it != SENTINEL_CL; it--)
{
actCl = *it;
pCl = &getClause(actCl);
// all clauses treated as conflict clauses, because no subsumption
// is being performed
if (isSatisfied(pCl->idLitA()) || isSatisfied(pCl->idLitB()))
continue;
///////////////////////////////////////
//BEGIN update watched Lits
///////////////////////////////////////
retV = false;
for (itL = begin(*pCl); *itL != ClauseEnd(); itL++)
if (getVar(*itL).isActive() || isSatisfied(*itL))
{
if (*itL == pCl->idLitA() || *itL == pCl->idLitB())
continue;
retV = isSatisfied(*itL);
getVar(*itL).addWatchClause(actCl, itL->polarity());
if (pCl->idLitA() == unLit)
pCl->setLitA(*itL);
else
pCl->setLitB(*itL);
pUnWatchCls->quickErase(it);
break;
}
if (retV)
continue; // i.e. clause satisfied
///////////////////////////////////////
//END update watched Lits
///////////////////////////////////////
if (getVar(pCl->idLitA()).isActive())
{
if (!getVar(pCl->idLitB()).isActive()) //IMPLIES_LITA;
{
thePairsOfImpl.push_back(
AntAndLit(actCl, pCl->idLitA()));
pCl->setTouched();
// Add the implied literal due to a conflict clause
#ifndef FULL_DDNNF
if (pCl->isCC())
{
#endif
if (enable_DT_recording)
{
DTNode * ccLit = get_lit_node(
pCl->idLitA().toSignedInt());
ccLit->addParent(decStack.top().getCurrentDTNode(),
true);
dirtyLitNodes.push_back(pair<DTNode *, DTNode*> (
decStack.top().getCurrentDTNode(), ccLit));
}
#ifndef FULL_DDNNF
}
#endif
}
}
else
{
pCl->setTouched();
if (getVar(pCl->idLitB()).isActive())
{ //IMPLIES_LITB;
thePairsOfImpl.push_back(
AntAndLit(actCl, pCl->idLitB()));
// Add the implied literal due to a conflict clause
#ifndef FULL_DDNNF
if (pCl->isCC())
{
#endif
if (enable_DT_recording)
{
DTNode * ccLit = get_lit_node(
pCl->idLitB().toSignedInt());
ccLit->addParent(decStack.top().getCurrentDTNode(),
true);
dirtyLitNodes.push_back(pair<DTNode *, DTNode*> (
decStack.top().getCurrentDTNode(), ccLit));
}
#ifndef FULL_DDNNF
}
#endif
}
else //provided that SATISFIED has been tested
{
getConflicted().push_back(actCl);
return false;