/
VarStore.java
277 lines (242 loc) · 7.51 KB
/
VarStore.java
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2016 Pierre Talbot (IRCAM)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bonsai.runtime.lattices.choco;
import bonsai.runtime.core.*;
import bonsai.runtime.lattices.LMax;
import org.chocosolver.solver.*;
import org.chocosolver.solver.variables.*;
import org.chocosolver.solver.constraints.*;
import org.chocosolver.util.ESat;
import org.chocosolver.solver.Cause;
import org.chocosolver.solver.exception.ContradictionException;
public class VarStore implements Store, Restorable
{
private Model model;
private Integer depth;
public VarStore bottom() {
return new VarStore();
}
public VarStore top() {
throw new UnsupportedOperationException(
"VarStore does not provide support for the TOP element.");
}
public VarStore() {
this("Bonsai CSP");
}
public VarStore(String csp_name) {
model = new Model(csp_name);
depth = 0;
}
public IntVar[] vars() {
return model.retrieveIntVars(true);
}
public Model model() {
return model;
}
public Integer label() {
return new Integer(depth);
}
// precondition: Restoration strategy of `VarStore` only support depth-first search exploration.
public void restore(Object label) {
Cast.checkNull("label", "VarStore.restore", label);
if (label instanceof Integer) {
Integer newDepth = (Integer) label;
model.getEnvironment().worldPopUntil(newDepth);
model.getEnvironment().worldPush();
depth = newDepth + 1;
}
else {
throw new RuntimeException(
"Label of `VarStore` must be a `Integer` value.");
}
}
public Object alloc(Object value) {
Cast.checkNull("parameter (allocation value)", "VarStore.alloc", value);
if (value instanceof IntDomain) {
IntDomain dom = (IntDomain) value;
return model.intVar(dom.lb, dom.ub, dom.bounded);
}
else {
throw new RuntimeException(
"Allocation in `VarStore.alloc` is only defined for integer domain `IntDomain`.");
}
}
public Object index(Object location) {
Cast.checkNull("parameter (location)", "VarStore.index", location);
if (location instanceof IntVar) {
// Note that the object in the store is actually the same as the location.
return location;
}
else {
throw new RuntimeException(
"Location of `VarStore.index` must be of type `IntVar`");
}
}
public void join_in_place(Object value) {
Cast.checkNull("parameter", "VarStore.join_in_place", value);
if (value instanceof Entry) {
throw new UnsupportedOperationException(
"`join` is currently not defined for `VarStore` because `IntVar` does not provide intersection.");
}
else if (this != value) {
throw new RuntimeException(
"`join` is only defined between `VarStore` and an entry `VarStore.Entry`.");
}
}
public VarStore join(Object value) {
throw new UnsupportedOperationException(
"`join` is currently not defined for `VarStore`.");
}
public void join_eq(IntVar x, Integer v) {
try {
// for(int i=0; i < depth; i++) { System.out.print("."); }
// System.out.println(x + "=" + v);
x.instantiateTo(v, Cause.Null);
}
catch (ContradictionException c) {
throw new RuntimeException("`join_eq` was called with an unsatisfiable constraint x = v.");
}
}
public void join_neq(IntVar x, Integer v) {
try {
// for(int i=0; i < depth; i++) { System.out.print("."); }
// System.out.println(x + "!=" + v);
x.removeValue(v, Cause.Null);
}
catch (ContradictionException c) {
throw new RuntimeException("`join_neq` was called with an unsatisfiable constraint x != v.");
}
}
public void join_le(IntVar x, Integer v) {
try {
x.updateUpperBound(v, Cause.Null);
}
catch (ContradictionException c) {
throw new RuntimeException("`join_le` was called with an unsatisfiable constraint x <= v.");
}
}
public void join_gt(IntVar x, Integer v) {
try {
x.updateLowerBound(v+1, Cause.Null);
}
catch (ContradictionException c) {
throw new RuntimeException("`join_gt` was called with an unsatisfiable constraint x > v.");
}
}
public VarStore meet(Object value) {
throw new UnsupportedOperationException(
"`meet` is currently not defined for `VarStore`.");
}
public void meet_in_place(Object value) {
throw new UnsupportedOperationException(
"`meet_in_place` is currently not defined for `VarStore`.");
}
public LMax countAsn() {
checkOnlyIntVar(model);
LMax asn = new LMax();
for (IntVar v : model.retrieveIntVars(true)) {
if (v.isInstantiated()) {
asn.inc();
}
}
return asn;
}
public LMax countAsnOf(IntVar[] vars) {
checkOnlyIntVar(model);
LMax asn = new LMax();
for (IntVar v : vars) {
if (v.isInstantiated()) {
asn.inc();
}
}
return asn;
}
public Kleene entails(Object value) {
if (value instanceof VarStore) {
VarStore vstore = (VarStore) value;
return entails(vstore);
}
else {
throw new UnsupportedOperationException(
"Entailment is not defined between `VarStore` and `"
+ value.getClass().getName() + "`.");
}
}
private Kleene entails(VarStore vstore) {
checkOnlyIntVar(model);
checkOnlyIntVar(vstore.model);
IntVar[] v1 = model.retrieveIntVars(true);
IntVar[] v2 = vstore.model.retrieveIntVars(true);
if (v1.length < v2.length) {
return Kleene.not(vstore.entails(this));
}
else {
Kleene res = Kleene.TRUE;
for (int i = 0; i < v2.length; i++) {
switch (varEntail(v1[i], v2[i])) {
case FALSE: {
res = Kleene.FALSE;
return res;
}
case UNKNOWN: {
res = Kleene.UNKNOWN;
break;
}
default: break;
}
}
return res;
}
}
private static Kleene varEntail(IntVar v1, IntVar v2) {
if (v1.isInstantiated() && v2.isInstantiated()) {
if (v1.getValue() == v2.getValue()) {
return Kleene.TRUE;
}
else {
return Kleene.FALSE;
}
}
else {
// We could be more precise using the set inclusion between v1 and v2.
return Kleene.UNKNOWN;
}
}
private void checkOnlyIntVar(Model model) {
if (model.retrieveSetVars().length > 0 ||
model.retrieveRealVars().length > 0)
{
// NOTE: It would not be hard to extend the entailment to set and real variables.
throw new RuntimeException(
"Entailment between two `VarStore` only works with integer variables.");
}
}
public static class IntDomain {
public int lb, ub;
public boolean bounded;
public IntDomain(int lb, int ub) {
this(lb, ub, false);
}
public IntDomain(int lb, int ub, boolean bounded) {
this.lb = lb;
this.ub = ub;
this.bounded = bounded;
}
}
public class Entry {
private IntVar location;
private IntDomain value;
public Entry(IntVar location, IntDomain value) {
this.location = location;
this.value = value;
}
}
}