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;
31
32 import java.io.Serializable;
33 import java.math.BigInteger;
34
35 import org.sat4j.core.ReadOnlyVec;
36 import org.sat4j.core.ReadOnlyVecInt;
37 import org.sat4j.specs.ISolver;
38 import org.sat4j.specs.IVec;
39 import org.sat4j.specs.IVecInt;
40
41
42
43
44
45
46
47
48
49 public class ObjectiveFunction implements Serializable {
50
51
52
53
54 private static final long serialVersionUID = 1L;
55
56
57 private final IVec<BigInteger> coeffs;
58
59 private final IVecInt vars;
60
61 private BigInteger correction = BigInteger.ZERO;
62
63 public ObjectiveFunction(IVecInt vars, IVec<BigInteger> coeffs) {
64 this.vars = new ReadOnlyVecInt(vars);
65 this.coeffs = new ReadOnlyVec<BigInteger>(coeffs);
66 }
67
68
69 public BigInteger calculateDegree(ISolver solver) {
70 BigInteger tempDegree = BigInteger.ZERO;
71 for (int i = 0; i < this.vars.size(); i++) {
72 BigInteger coeff = this.coeffs.get(i);
73 if (varInModel(this.vars.get(i), solver)) {
74 tempDegree = tempDegree.add(coeff);
75 } else if (coeff.signum() < 0
76 && !varInModel(-this.vars.get(i), solver)) {
77
78
79 tempDegree = tempDegree.add(coeff);
80 }
81 }
82 return tempDegree;
83 }
84
85
86 public BigInteger calculateDegreeImplicant(ISolver solver) {
87 BigInteger tempDegree = BigInteger.ZERO;
88 solver.primeImplicant();
89 for (int i = 0; i < this.vars.size(); i++) {
90 BigInteger coeff = this.coeffs.get(i);
91 if (solver.primeImplicant(this.vars.get(i))) {
92 tempDegree = tempDegree.add(coeff);
93 } else if (coeff.signum() < 0
94 && !solver.primeImplicant(-this.vars.get(i))) {
95
96
97 tempDegree = tempDegree.add(coeff);
98 }
99 }
100 return tempDegree;
101 }
102
103 private boolean varInModel(int var, ISolver solver) {
104 if (var > 0) {
105 return solver.model(var);
106 }
107 return !solver.model(-var);
108 }
109
110 public IVec<BigInteger> getCoeffs() {
111 return this.coeffs;
112 }
113
114 public IVecInt getVars() {
115 return this.vars;
116 }
117
118 public void setCorrection(BigInteger correction) {
119 this.correction = correction;
120 }
121
122 public BigInteger getCorrection() {
123 return this.correction;
124 }
125
126 @Override
127 public String toString() {
128 StringBuffer stb = new StringBuffer();
129 IVecInt lits = getVars();
130 IVec<BigInteger> coefs = getCoeffs();
131 BigInteger coef;
132 int lit;
133 for (int i = 0; i < lits.size(); i++) {
134 coef = coefs.get(i);
135 lit = lits.get(i);
136 if (lit < 0) {
137 lit = -lit;
138 coef = coef.negate();
139 }
140 stb.append((coef.signum() < 0 ? "" : "+") + coef + " x" + lit + " ");
141 }
142 return stb.toString();
143 }
144
145 public BigInteger minValue() {
146 BigInteger tempDegree = BigInteger.ZERO;
147 for (int i = 0; i < this.vars.size(); i++) {
148 BigInteger coeff = this.coeffs.get(i);
149 if (coeff.signum() < 0) {
150 tempDegree = tempDegree.add(coeff);
151 }
152 }
153 return tempDegree;
154 }
155
156 public BigInteger calculateDegree(int[] model) {
157 BigInteger tempDegree = BigInteger.ZERO;
158
159 for (int i = 0; i < this.vars.size(); i++) {
160 BigInteger coeff = this.coeffs.get(i);
161 if (varInModel(this.vars.get(i), model)) {
162 tempDegree = tempDegree.add(coeff);
163 } else if (coeff.signum() < 0
164 && !varInModel(-this.vars.get(i), model)) {
165 tempDegree = tempDegree.add(coeff);
166 }
167 }
168 return tempDegree;
169 }
170
171 private boolean varInModel(int var, int[] model) {
172 for (int element : model) {
173 if (var == element) {
174 return true;
175 }
176 }
177 return false;
178 }
179
180 }