aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-dse/src/main/java/tools/refinery/store/dse/internal/DesignSpaceExplorationAdapterImpl.java
blob: 1ae09916a464eeab2b4444d7b0d969271b91ad0c (plain) (blame)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*******************************************************************************
 * Copyright (c) 2010-2014, Miklos Foldenyi, Andras Szabolcs Nagy, Abel Hegedus, Akos Horvath, Zoltan Ujhelyi and Daniel Varro
 * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/>
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package tools.refinery.store.dse.internal;

import tools.refinery.store.map.Version;
import tools.refinery.store.model.Interpretation;
import tools.refinery.store.model.Model;
import tools.refinery.store.query.ModelQueryAdapter;
import tools.refinery.store.query.dnf.Query;
import tools.refinery.store.query.dnf.RelationalQuery;
import tools.refinery.store.dse.DesignSpaceExplorationAdapter;
import tools.refinery.store.dse.DesignSpaceExplorationStoreAdapter;
import tools.refinery.store.dse.Strategy;
import tools.refinery.store.dse.objectives.Fitness;
import tools.refinery.store.dse.objectives.Objective;
import tools.refinery.store.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 Set<TransformationRule> transformationRules;
	private final Set<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<Version> trajectory = new ArrayList<>();
	private Map<Version, Version> parents = new HashMap<>();
	private final List<Version> solutions = new ArrayList<>();
	private Map<Version, List<Activation>> statesAndTraversedActivations;
	@SuppressWarnings("squid:S2245")
	private Random random = new Random();
	private boolean isNewState = false;
	private final boolean isVisualizationEnabled;
	private final ModelVisualizerAdapter modelVisualizerAdapter;

	private final Map<Version, Fitness> fitnessCache = new HashMap<>();

	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();
		statesAndTraversedActivations = new HashMap<>();
		strategy = storeAdapter.getStrategy();
		strategy.initialize(this);
		modelVisualizerAdapter = model.tryGetAdapter(ModelVisualizerAdapter.class).orElse(null);
		isVisualizationEnabled = modelVisualizerAdapter != null;

	}

	@Override
	public void addTransformationRule(TransformationRule rule) {
		transformationRules.add(rule);
		rule.prepare(model, queryEngine);
	}

	public List<Version> getTrajectory() {
		return new ArrayList<>(trajectory);
	}

	@Override
	public Model getModel() {
		return model;
	}

	@Override
	public DesignSpaceExplorationStoreAdapter getStoreAdapter() {
		return storeAdapter;
	}

	@Override
	public List<Version> explore() {
		var state = model.commit();
		trajectory.add(state);
		strategy.explore();
		if (isVisualizationEnabled) {
			modelVisualizerAdapter.visualize();
		}
		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() {
		return backtrack("");
	}
	@Override
	public boolean backtrack(String reason) {
		if (trajectory.size() < 2) {
			return false;
		}
		var currentState = model.getState();
		if (!parents.containsKey(currentState)) {
			return false;
		}
		if (isVisualizationEnabled) {
			modelVisualizerAdapter.addTransition(trajectory.get(trajectory.size() - 1),
					trajectory.get(trajectory.size() - 2), "backtrack(" + reason + ")");
		}
		model.restore(parents.get(model.getState()));
		trajectory.remove(trajectory.size() - 1);
		return true;
	}

	@Override
	public void restoreTrajectory(List<Version> 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 = new ArrayList<>(trajectory);

	}

	@Override
	public void setRandom(Random random) {
		this.random = random;
	}

	@Override
	@SuppressWarnings("squid:S2245")
	public void setRandom(long seed) {
		this.random = new Random(seed);
	}

	@Override
	public List<Version> getSolutions() {
		return solutions;
	}

	@Override
	public Fitness getFitness() {
        return fitnessCache.computeIfAbsent(model.getState(), s -> calculateFitness());
	}

	private 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);

		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() {
		var traversedActivations = statesAndTraversedActivations.get(model.getState());
		if (traversedActivations == null) {
			return new LinkedHashSet<>(getAllActivations());
		}
		else {
			LinkedHashSet<Activation> untraversedActivations = new LinkedHashSet<>();
			for (Activation activation : getAllActivations()) {
				if (!traversedActivations.contains(activation)) {
					untraversedActivations.add(activation);
				}
			}
			return untraversedActivations;
		}
	}

	@Override
	public boolean fireActivation(Activation activation) {
		if (activation == null) {
			return false;
		}
		var previousState = model.getState();
		if (!activation.fire()) {
			return false;
		}
		statesAndTraversedActivations.computeIfAbsent(previousState, s -> new ArrayList<>()).add(activation);
		var newState = model.commit();
		trajectory.add(newState);
		parents.put(newState, previousState);
		isNewState = !statesAndTraversedActivations.containsKey(newState);
		if (isVisualizationEnabled) {
			if (isNewState) {
				modelVisualizerAdapter.addState(newState, getFitness().values());
			}
			modelVisualizerAdapter.addTransition(previousState, newState, activation.transformationRule().getName(),
					activation.activation());
		}
		return true;
	}

	@Override
	public boolean fireRandomActivation() {
		var activations = getUntraversedActivations();
		if (activations.isEmpty()) {
			return false;
		}
		int index = random.nextInt(activations.size());
		var iterator = activations.iterator();
		while (index-- > 0) {
			iterator.next();
		}
		var activationId = iterator.next();
		return fireActivation(activationId);
	}

	public List<Activation> getAllActivations() {
		List<Activation> result = new LinkedList<>();
		for (var rule : transformationRules) {
			result.addAll(rule.getAllActivationsAsList());
		}
		return result;
	}

	public boolean isCurrentStateAlreadyTraversed() {
		return !isNewState;
	}

	public ObjectiveComparatorHelper getObjectiveComparatorHelper() {
		if (objectiveComparatorHelper == null) {
			objectiveComparatorHelper = new ObjectiveComparatorHelper(objectives);
		}
		return objectiveComparatorHelper;
	}
}