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.core.Vec; |
31 |
|
import org.sat4j.core.VecInt; |
32 |
|
import org.sat4j.specs.ContradictionException; |
33 |
|
import org.sat4j.specs.IOptimizationProblem; |
34 |
|
import org.sat4j.specs.ISolver; |
35 |
|
import org.sat4j.specs.IVec; |
36 |
|
import org.sat4j.specs.IVecInt; |
37 |
|
import org.sat4j.specs.TimeoutException; |
38 |
|
import org.sat4j.tools.SolverDecorator; |
39 |
|
|
|
|
| 0% |
Uncovered Elements: 46 (46) |
Complexity: 5 |
Complexity Density: 0,62 |
|
40 |
|
public class MinCostDecorator extends SolverDecorator implements |
41 |
|
IOptimizationProblem { |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
private static final long serialVersionUID = 1L; |
47 |
|
|
48 |
|
private int[] costs; |
49 |
|
|
50 |
|
private int[] prevmodel; |
51 |
|
|
52 |
|
private final IVecInt vars = new VecInt(); |
53 |
|
|
54 |
|
private final IVec<BigInteger> coeffs = new Vec<BigInteger>(); |
55 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
56 |
0
|
public MinCostDecorator(ISolver solver) {... |
57 |
0
|
super(solver); |
58 |
|
} |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@see |
64 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
65 |
0
|
@Override... |
66 |
|
public int newVar() { |
67 |
0
|
throw new UnsupportedOperationException(); |
68 |
|
} |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@param |
77 |
|
|
78 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0,29 |
|
79 |
0
|
@Override... |
80 |
|
public int newVar(int howmany) { |
81 |
0
|
costs = new int[howmany + 1]; |
82 |
|
|
83 |
0
|
vars.clear(); |
84 |
0
|
coeffs.clear(); |
85 |
0
|
for (int i = 1; i <= howmany; i++) { |
86 |
0
|
vars.push(i); |
87 |
0
|
coeffs.push(BigInteger.ZERO); |
88 |
|
} |
89 |
|
|
90 |
|
|
91 |
0
|
return super.newVar(howmany); |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@param |
98 |
|
|
99 |
|
@return |
100 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
101 |
0
|
public int costOf(int var) {... |
102 |
0
|
return costs[var]; |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
@param |
109 |
|
|
110 |
|
@param |
111 |
|
|
112 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
113 |
0
|
public void setCost(int var, int cost) {... |
114 |
0
|
costs[var] = cost; |
115 |
0
|
coeffs.set(var - 1, BigInteger.valueOf(cost)); |
116 |
|
} |
117 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0,5 |
|
118 |
0
|
public boolean admitABetterSolution() throws TimeoutException {... |
119 |
0
|
boolean result = super.isSatisfiable(); |
120 |
0
|
if (result) |
121 |
0
|
prevmodel = super.model(); |
122 |
0
|
return result; |
123 |
|
} |
124 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
125 |
0
|
public boolean hasNoObjectiveFunction() {... |
126 |
0
|
return false; |
127 |
|
} |
128 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
129 |
0
|
public boolean nonOptimalMeansSatisfiable() {... |
130 |
0
|
return true; |
131 |
|
} |
132 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
133 |
0
|
public Number calculateObjective() {... |
134 |
0
|
return calculateDegree(prevmodel); |
135 |
|
} |
136 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0,6 |
|
137 |
0
|
private int calculateDegree(int[] prevmodel2) {... |
138 |
0
|
int tmpcost = 0; |
139 |
0
|
for (int i = 1; i < costs.length; i++) { |
140 |
0
|
if (prevmodel2[i - 1] > 0) { |
141 |
0
|
tmpcost += costs[i]; |
142 |
|
} |
143 |
|
} |
144 |
0
|
return tmpcost; |
145 |
|
} |
146 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
147 |
0
|
public void discard() throws ContradictionException {... |
148 |
0
|
super.addPseudoBoolean(vars, coeffs, false, BigInteger |
149 |
|
.valueOf(calculateDegree(prevmodel) - 1)); |
150 |
|
} |
151 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
152 |
0
|
@Override... |
153 |
|
public int[] model() { |
154 |
0
|
return prevmodel; |
155 |
|
} |
156 |
|
|
157 |
|
} |