-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathFallbackOptimizationProver.java
More file actions
203 lines (180 loc) · 8 KB
/
FallbackOptimizationProver.java
File metadata and controls
203 lines (180 loc) · 8 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package org.sosy_lab.java_smt.basicimpl;
import org.sosy_lab.common.log.LogManager;
import org.sosy_lab.java_smt.api.BasicProverEnvironment;
import org.sosy_lab.java_smt.api.BooleanFormula;
import org.sosy_lab.java_smt.api.Formula;
import org.sosy_lab.java_smt.api.FormulaManager;
import org.sosy_lab.java_smt.api.IntegerFormulaManager;
import org.sosy_lab.java_smt.api.NumeralFormula;
import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula;
import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula;
import org.sosy_lab.java_smt.api.OptimizationProverEnvironment;
import org.sosy_lab.java_smt.api.SolverContext;
import org.sosy_lab.java_smt.api.SolverException;
import org.sosy_lab.common.rationals.Rational;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
/**
* A fallback implementation of OptimizationProverEnvironment for solvers
* that don't support optimization natively. This implementation uses
* iterative solving to approximate optimization results.
*/
public class FallbackOptimizationProver extends AbstractOptimizationProver {
private final BasicProverEnvironment baseProver;
private final SolverContext context;
private final Map<Integer, Formula> objectives;
private int nextHandle;
public FallbackOptimizationProver(
SolverContext pContext,
LogManager pLogger,
FormulaManager pMgr,
Set<SolverContext.ProverOptions> pOptions) {
super(pLogger, pMgr);
this.context = pContext;
this.baseProver = pContext.newProverEnvironment(pOptions);
this.objectives = new HashMap<>();
this.nextHandle = 0;
}
@Override
public boolean isOptimizationSupported() {
return true; // We always support optimization through fallback
}
@Override
protected String getSolverName() {
return context.getSolverName();
}
@Override
protected int maximizeInternal(Formula objective) {
int handle = nextHandle++;
objectives.put(handle, objective);
return handle;
}
@Override
protected int minimizeInternal(Formula objective) {
// For minimization, we negate the objective and maximize
FormulaManager fmgr = context.getFormulaManager();
if (objective instanceof IntegerFormula) {
IntegerFormulaManager imgr = fmgr.getIntegerFormulaManager();
return maximizeInternal(imgr.negate((IntegerFormula) objective));
} else if (objective instanceof RationalFormula) {
// Handle rational formulas similarly
// Implementation depends on the specific formula manager
throw new UnsupportedOperationException(
"Rational optimization not yet implemented in fallback");
} else {
throw new UnsupportedOperationException(
"Unsupported formula type for optimization: " + objective.getClass());
}
}
@Override
protected Optional<Rational> upperInternal(int handle, Rational epsilon) {
Formula objective = objectives.get(handle);
if (objective == null) {
throw new IllegalArgumentException("Invalid objective handle: " + handle);
}
try {
// Start with a large upper bound
Rational upperBound = Rational.ofLong(1000000);
Rational lowerBound = Rational.ofLong(-1000000);
// Binary search to find the maximum value
while (upperBound.subtract(lowerBound).compareTo(epsilon) > 0) {
Rational mid = upperBound.add(lowerBound).divide(Rational.ofLong(2));
// Create constraint: objective <= mid
BooleanFormula constraint = createUpperBoundConstraint(objective, mid);
baseProver.push();
baseProver.addConstraint(constraint);
try {
if (baseProver.isUnsat()) {
// No solution exists with this upper bound
upperBound = mid;
} else {
// Solution exists, try a higher value
lowerBound = mid;
}
} finally {
baseProver.pop();
}
}
return Optional.of(upperBound);
} catch (SolverException | InterruptedException e) {
logger.log(Level.WARNING, "Error during optimization", e);
return Optional.empty();
}
}
@Override
protected Optional<Rational> lowerInternal(int handle, Rational epsilon) {
Formula objective = objectives.get(handle);
if (objective == null) {
throw new IllegalArgumentException("Invalid objective handle: " + handle);
}
try {
// Start with a small lower bound
Rational lowerBound = Rational.ofLong(-1000000);
Rational upperBound = Rational.ofLong(1000000);
// Binary search to find the minimum value
while (upperBound.subtract(lowerBound).compareTo(epsilon) > 0) {
Rational mid = upperBound.add(lowerBound).divide(Rational.ofLong(2));
// Create constraint: objective >= mid
BooleanFormula constraint = createLowerBoundConstraint(objective, mid);
baseProver.push();
baseProver.addConstraint(constraint);
try {
if (baseProver.isUnsat()) {
// No solution exists with this lower bound
lowerBound = mid;
} else {
// Solution exists, try a lower value
upperBound = mid;
}
} finally {
baseProver.pop();
}
}
return Optional.of(lowerBound);
} catch (SolverException | InterruptedException e) {
logger.log(Level.WARNING, "Error during optimization", e);
return Optional.empty();
}
}
private BooleanFormula createUpperBoundConstraint(Formula objective, Rational bound) {
FormulaManager fmgr = context.getFormulaManager();
if (objective instanceof IntegerFormula) {
IntegerFormulaManager imgr = fmgr.getIntegerFormulaManager();
return imgr.lessOrEquals(
(IntegerFormula) objective,
imgr.makeNumber(bound.longValue()));
} else if (objective instanceof RationalFormula) {
// Handle rational formulas similarly
// Implementation depends on the specific formula manager
throw new UnsupportedOperationException(
"Rational optimization not yet implemented in fallback");
} else {
throw new UnsupportedOperationException(
"Unsupported formula type for optimization: " + objective.getClass());
}
}
private BooleanFormula createLowerBoundConstraint(Formula objective, Rational bound) {
FormulaManager fmgr = context.getFormulaManager();
if (objective instanceof IntegerFormula) {
IntegerFormulaManager imgr = fmgr.getIntegerFormulaManager();
return imgr.greaterOrEquals(
(IntegerFormula) objective,
imgr.makeNumber(bound.longValue()));
} else if (objective instanceof RationalFormula) {
// Handle rational formulas similarly
// Implementation depends on the specific formula manager
throw new UnsupportedOperationException(
"Rational optimization not yet implemented in fallback");
} else {
throw new UnsupportedOperationException(
"Unsupported formula type for optimization: " + objective.getClass());
}
}
@Override
public void close() {
baseProver.close();
}
}