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 package org.sat4j.opt;
27
28 import java.math.BigInteger;
29
30 import org.sat4j.specs.ContradictionException;
31 import org.sat4j.specs.IOptimizationProblem;
32 import org.sat4j.specs.ISolver;
33 import org.sat4j.specs.TimeoutException;
34 import org.sat4j.tools.SolverDecorator;
35
36
37
38
39
40
41
42 public class PseudoOptDecorator extends SolverDecorator implements
43 IOptimizationProblem {
44
45
46
47
48 private static final long serialVersionUID = 1L;
49
50 private ObjectiveFunction objfct;
51
52 private int[] prevmodel;
53
54 public PseudoOptDecorator(ISolver solver) {
55 super(solver);
56 }
57
58 public void setObjectTiveFunction(ObjectiveFunction objf) {
59 objfct = objf;
60 }
61
62 public boolean admitABetterSolution() throws TimeoutException {
63 boolean result = super.isSatisfiable();
64 if (result)
65 prevmodel = super.model();
66 return result;
67 }
68
69 public boolean hasNoObjectiveFunction() {
70 return objfct == null;
71 }
72
73 public boolean nonOptimalMeansSatisfiable() {
74 return true;
75 }
76
77 public Number calculateObjective() {
78 return objfct.calculateDegree(prevmodel);
79 }
80
81 public void discard() throws ContradictionException {
82 super.addPseudoBoolean(objfct.getVars(), objfct.getCoeffs(), false,
83 objfct.calculateDegree(prevmodel).subtract(BigInteger.ONE));
84 }
85
86 @Override
87 public int[] model() {
88
89 return prevmodel;
90 }
91
92 }