1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.sat4j.pb.tools;
21
22 import java.math.BigInteger;
23 import java.util.Iterator;
24
25 import org.sat4j.core.Vec;
26 import org.sat4j.pb.IPBSolver;
27 import org.sat4j.pb.ObjectiveFunction;
28 import org.sat4j.specs.ContradictionException;
29 import org.sat4j.specs.IConstr;
30 import org.sat4j.specs.IVec;
31 import org.sat4j.specs.IVecInt;
32 import org.sat4j.tools.xplain.Xplain;
33
34 public class XplainPB extends Xplain<IPBSolver> implements IPBSolver {
35
36
37
38
39 private static final long serialVersionUID = 1L;
40
41 public XplainPB(IPBSolver solver) {
42 super(solver);
43 }
44
45 @Override
46 public IConstr addAtLeast(IVecInt literals, int degree)
47 throws ContradictionException {
48 IVec<BigInteger> coeffs = new Vec<BigInteger>();
49 coeffs.growTo(literals.size(), BigInteger.ONE);
50 int newvar = createNewVar(literals);
51 literals.push(newvar);
52 BigInteger coef = BigInteger.valueOf(coeffs.size() - degree);
53 coeffs.push(coef);
54 IConstr constr = decorated().addPseudoBoolean(literals, coeffs, true,
55 BigInteger.valueOf(degree));
56 if (constr == null) {
57
58 discardLastestVar();
59
60 } else {
61 constrs.put(newvar, constr);
62 }
63 return constr;
64 }
65
66 @Override
67 public IConstr addAtMost(IVecInt literals, int degree)
68 throws ContradictionException {
69 IVec<BigInteger> coeffs = new Vec<BigInteger>();
70 coeffs.growTo(literals.size(), BigInteger.ONE);
71 int newvar = createNewVar(literals);
72 literals.push(newvar);
73 BigInteger coef = BigInteger.valueOf(degree - coeffs.size());
74 coeffs.push(coef);
75 IConstr constr = decorated().addPseudoBoolean(literals, coeffs, false,
76 BigInteger.valueOf(degree));
77 if (constr == null) {
78
79 discardLastestVar();
80
81 } else {
82 constrs.put(newvar, constr);
83 }
84 return constr;
85 }
86
87 public IConstr addPseudoBoolean(IVecInt lits, IVec<BigInteger> coeffs,
88 boolean moreThan, BigInteger d) throws ContradictionException {
89 int newvar = createNewVar(lits);
90 lits.push(newvar);
91 if (moreThan && d.signum() >= 0) {
92 coeffs.push(d);
93 } else {
94 BigInteger sum = BigInteger.ZERO;
95 for (Iterator<BigInteger> ite = coeffs.iterator(); ite.hasNext();)
96 sum = sum.add(ite.next());
97 sum = sum.subtract(d);
98 coeffs.push(sum.negate());
99 }
100 IConstr constr = decorated()
101 .addPseudoBoolean(lits, coeffs, moreThan, d);
102 if (constr == null) {
103
104 discardLastestVar();
105
106 } else {
107 constrs.put(newvar, constr);
108 }
109 return constr;
110 }
111
112 public void setObjectiveFunction(ObjectiveFunction obj) {
113 decorated().setObjectiveFunction(obj);
114 }
115
116 public ObjectiveFunction getObjectiveFunction() {
117 return decorated().getObjectiveFunction();
118 }
119 }