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 package org.sat4j.pb.core;
29
30 import org.sat4j.core.Vec;
31 import org.sat4j.minisat.core.AssertingClauseGenerator;
32 import org.sat4j.minisat.core.Constr;
33 import org.sat4j.minisat.core.IOrder;
34 import org.sat4j.minisat.core.LearningStrategy;
35 import org.sat4j.minisat.core.Pair;
36 import org.sat4j.minisat.core.RestartStrategy;
37 import org.sat4j.minisat.core.SearchParams;
38 import org.sat4j.minisat.restarts.MiniSATRestarts;
39 import org.sat4j.pb.constraints.pb.ConflictMap;
40 import org.sat4j.pb.constraints.pb.IConflict;
41 import org.sat4j.pb.constraints.pb.PBConstr;
42 import org.sat4j.specs.IVec;
43
44
45
46
47
48 public class PBSolverCP extends PBSolver {
49
50 private static final long serialVersionUID = 1L;
51
52
53
54
55
56
57 public PBSolverCP(AssertingClauseGenerator acg,
58 LearningStrategy<PBDataStructureFactory> learner,
59 PBDataStructureFactory dsf, IOrder order) {
60 super(acg, learner, dsf, new SearchParams(1.5, 100), order,
61 new MiniSATRestarts());
62 }
63
64 public PBSolverCP(AssertingClauseGenerator acg,
65 LearningStrategy<PBDataStructureFactory> learner,
66 PBDataStructureFactory dsf, SearchParams params, IOrder order,
67 RestartStrategy restarter) {
68 super(acg, learner, dsf, params, order, restarter);
69 }
70
71 public PBSolverCP(AssertingClauseGenerator acg,
72 LearningStrategy<PBDataStructureFactory> learner,
73 PBDataStructureFactory dsf, SearchParams params, IOrder order) {
74 super(acg, learner, dsf, params, order, new MiniSATRestarts());
75 }
76
77 @Override
78 public void analyze(Constr myconfl, Pair results) {
79 int litImplied = trail.last();
80 int currentLevel = voc.getLevel(litImplied);
81 IConflict confl = chooseConflict((PBConstr) myconfl, currentLevel);
82 assert confl.slackConflict().signum() < 0;
83 while (!confl.isAssertive(currentLevel)) {
84 PBConstr constraint = (PBConstr) voc.getReason(litImplied);
85
86 confl.resolve(constraint, litImplied, this);
87 assert confl.slackConflict().signum() <= 0;
88
89 if (trail.size() == 1)
90 break;
91 undoOne();
92
93 if (decisionLevel() == 0)
94 break;
95 litImplied = trail.last();
96 if (voc.getLevel(litImplied) != currentLevel) {
97 trailLim.pop();
98 confl.updateSlack(voc.getLevel(litImplied));
99 }
100 assert voc.getLevel(litImplied) <= currentLevel;
101 currentLevel = voc.getLevel(litImplied);
102 assert confl.slackIsCorrect(currentLevel);
103 assert currentLevel == decisionLevel();
104 assert litImplied > 1;
105 }
106 assert confl.isAssertive(currentLevel) || trail.size() == 1
107 || decisionLevel() == 0;
108
109 assert currentLevel == decisionLevel();
110 undoOne();
111
112
113
114 if ((confl.size() == 0)
115 || ((decisionLevel() == 0 || trail.size() == 0) && confl
116 .slackConflict().signum() < 0)) {
117 results.reason = null;
118 results.backtrackLevel = -1;
119 return;
120 }
121
122
123 PBConstr resConstr = (PBConstr) dsfactory
124 .createUnregisteredPseudoBooleanConstraint(confl);
125
126 results.reason = resConstr;
127
128
129
130
131 if (decisionLevel() == 0 || trail.size() == 0)
132 results.backtrackLevel = -1;
133 else
134 results.backtrackLevel = confl.getBacktrackLevel(currentLevel);
135 }
136
137 IConflict chooseConflict(PBConstr myconfl, int level) {
138 return ConflictMap.createConflict(myconfl, level);
139 }
140
141 @Override
142 public String toString(String prefix) {
143 return prefix + "Cutting planes based inference ("
144 + this.getClass().getName() + ")\n" + super.toString(prefix);
145 }
146
147 private final IVec<String> conflictVariables = new Vec<String>();
148 private final IVec<String> conflictConstraints = new Vec<String>();
149
150 void initExplanation() {
151 conflictVariables.clear();
152 conflictConstraints.clear();
153 }
154 }