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
278
279
|
package tools.refinery.store.query.dse.internal;
import tools.refinery.store.model.Interpretation;
import tools.refinery.store.model.Model;
import tools.refinery.store.query.ModelQueryAdapter;
import tools.refinery.store.query.dnf.RelationalQuery;
import tools.refinery.store.query.dse.DesignSpaceExplorationAdapter;
import tools.refinery.store.query.dse.DesignSpaceExplorationStoreAdapter;
import tools.refinery.store.query.dse.Strategy;
import tools.refinery.store.query.dse.objectives.Fitness;
import tools.refinery.store.query.dse.objectives.Objective;
import tools.refinery.store.query.dse.objectives.ObjectiveComparatorHelper;
import tools.refinery.store.query.resultset.ResultSet;
import tools.refinery.store.representation.Symbol;
import tools.refinery.store.tuple.Tuple;
import tools.refinery.store.tuple.Tuple1;
import tools.refinery.visualization.ModelVisualizerAdapter;
import java.util.*;
public class DesignSpaceExplorationAdapterImpl implements DesignSpaceExplorationAdapter {
static final Symbol<Integer> NODE_COUNT_SYMBOL = Symbol.of("MODEL_SIZE", 0, Integer.class, 0);
private final Model model;
private final ModelQueryAdapter queryEngine;
private final DesignSpaceExplorationStoreAdapterImpl storeAdapter;
private final LinkedHashSet<TransformationRule> transformationRules;
private final LinkedHashSet<RelationalQuery> globalConstraints;
private final List<Objective> objectives;
private final LinkedHashSet<ResultSet<Boolean>> globalConstraintResultSets = new LinkedHashSet<>();
private final Interpretation<Integer> sizeInterpretation;
private final Strategy strategy;
private ObjectiveComparatorHelper objectiveComparatorHelper;
private List<Long> trajectory = new LinkedList<>();
private Fitness lastFitness;
private final LinkedHashSet<Long> solutions = new LinkedHashSet<>();
private Map<Long, LinkedHashSet<Activation>> statesAndUntraversedActivations;
private Map<Long, LinkedHashSet<Activation>> statesAndTraversedActivations;
private Random random = new Random();
private boolean isNewState = false;
private final boolean isVisualizationEnabled;
private final ModelVisualizerAdapter modelVisualizerAdapter;
public List<Long> getTrajectory() {
return new LinkedList<>(trajectory);
}
public DesignSpaceExplorationAdapterImpl(Model model, DesignSpaceExplorationStoreAdapterImpl storeAdapter) {
this.model = model;
this.storeAdapter = storeAdapter;
this.sizeInterpretation = model.getInterpretation(NODE_COUNT_SYMBOL);
queryEngine = model.getAdapter(ModelQueryAdapter.class);
globalConstraints = storeAdapter.getGlobalConstraints();
for (var constraint : globalConstraints) {
globalConstraintResultSets.add(queryEngine.getResultSet(constraint));
}
transformationRules = storeAdapter.getTransformationSpecifications();
for (var rule : transformationRules) {
rule.prepare(model, queryEngine);
}
objectives = storeAdapter.getObjectives();
statesAndUntraversedActivations = new HashMap<>();
statesAndTraversedActivations = new HashMap<>();
strategy = storeAdapter.getStrategy();
modelVisualizerAdapter = model.tryGetAdapter(ModelVisualizerAdapter.class).orElse(null);
isVisualizationEnabled = modelVisualizerAdapter != null;
}
@Override
public Model getModel() {
return model;
}
@Override
public DesignSpaceExplorationStoreAdapter getStoreAdapter() {
return storeAdapter;
}
@Override
public LinkedHashSet<Long> explore() {
var state = model.commit();
trajectory.add(state);
statesAndUntraversedActivations.put(state, getAllActivations());
statesAndTraversedActivations.put(state, new LinkedHashSet<>());
strategy.initStrategy(this);
strategy.explore();
return solutions;
}
@Override
public int getModelSize() {
return sizeInterpretation.get(Tuple.of());
}
@Override
public Tuple1 createObject() {
var newNodeId = getModelSize();
sizeInterpretation.put(Tuple.of(), newNodeId + 1);
return Tuple.of(newNodeId);
}
@Override
public Tuple deleteObject(Tuple tuple) {
if (tuple.getSize() != 1) {
throw new IllegalArgumentException("Tuple size must be 1");
}
// TODO: implement more efficient deletion
// if (tuple.get(0) == getModelSize() - 1) {
// sizeInterpretation.put(Tuple.of(), getModelSize() - 1);
// }
return tuple;
}
@Override
public boolean checkGlobalConstraints() {
for (var resultSet : globalConstraintResultSets) {
if (resultSet.size() > 0) {
return false;
}
}
return true;
}
@Override
public boolean backtrack() {
if (trajectory.size() < 2) {
return false;
}
if (isVisualizationEnabled) {
modelVisualizerAdapter.addTransition(trajectory.get(trajectory.size() - 1),
trajectory.get(trajectory.size() - 2), "backtrack");
}
model.restore(trajectory.get(trajectory.size() - 2));
trajectory.remove(trajectory.size() - 1);
return true;
}
@Override
public void restoreTrajectory(List<Long> trajectory) {
model.restore(trajectory.get(trajectory.size() - 1));
// if (isVisualizationEnabled) {
// modelVisualizerAdapter.addTransition(this.trajectory.get(trajectory.size() - 1),
// trajectory.get(trajectory.size() - 1), "restore");
// }
this.trajectory = trajectory;
}
@Override
public void setRandom(Random random) {
this.random = random;
}
@Override
public void setRandom(long seed) {
this.random = new Random(seed);
}
@Override
public Fitness calculateFitness() {
Fitness result = new Fitness();
boolean satisfiesHardObjectives = true;
for (Objective objective : objectives) {
var fitness = objective.getFitness(this);
result.put(objective.getName(), fitness);
if (objective.isHardObjective() && !objective.satisfiesHardObjective(fitness)) {
satisfiesHardObjectives = false;
}
}
result.setSatisfiesHardObjectives(satisfiesHardObjectives);
lastFitness = result;
return result;
}
@Override
public void newSolution() {
var state = model.getState();
solutions.add(state);
if (isVisualizationEnabled) {
modelVisualizerAdapter.addSolution(state);
}
}
@Override
public int getDepth() {
return trajectory.size() - 1;
}
public LinkedHashSet<Activation> getUntraversedActivations() {
// return statesAndUntraversedActivations.get(model.getState());
LinkedHashSet<Activation> untraversedActivations = new LinkedHashSet<>();
for (Activation activation : getAllActivations()) {
if (!statesAndTraversedActivations.get(model.getState()).contains(activation)) {
untraversedActivations.add(activation);
}
}
return untraversedActivations;
}
@Override
public boolean fireActivation(Activation activation) {
if (activation == null) {
return false;
}
long previousState = model.getState();
if (!statesAndUntraversedActivations.get(previousState).contains(activation)) {
// TODO: throw exception?
return false;
}
if (!activation.fire()) {
return false;
}
statesAndUntraversedActivations.get(previousState).remove(activation);
statesAndTraversedActivations.get(previousState).add(activation);
long newState = model.commit();
trajectory.add(newState);
isNewState = !statesAndUntraversedActivations.containsKey(newState);
statesAndUntraversedActivations.put(newState, getAllActivations());
statesAndTraversedActivations.put(newState, new LinkedHashSet<>());
if (isVisualizationEnabled) {
modelVisualizerAdapter.addTransition(trajectory.get(trajectory.size() - 2),
trajectory.get(trajectory.size() - 1), activation.transformationRule().getName(),
activation.activation());
}
return true;
}
@Override
public void fireRandomActivation() {
var activations = getUntraversedActivations();
if (activations.isEmpty()) {
// TODO: throw exception
return;
}
int index = random.nextInt(activations.size());
var iterator = activations.iterator();
while (index-- > 0) {
iterator.next();
}
var activationId = iterator.next();
fireActivation(activationId);
}
@Override
public boolean isCurrentInTrajectory() {
return trajectory.contains(model.getState());
}
public LinkedHashSet<Activation> getAllActivations() {
LinkedHashSet<Activation> result = new LinkedHashSet<>();
for (var rule : transformationRules) {
result.addAll(rule.getAllActivations());
}
return result;
}
public boolean isCurrentStateAlreadyTraversed() {
// TODO: check isomorphism?
return !isNewState;
}
public Fitness getLastFitness() {
return lastFitness;
}
public ObjectiveComparatorHelper getObjectiveComparatorHelper() {
if (objectiveComparatorHelper == null) {
objectiveComparatorHelper = new ObjectiveComparatorHelper(objectives);
}
return objectiveComparatorHelper;
}
}
|