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