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
|
/*
* SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
*
* SPDX-License-Identifier: EPL-2.0
*/
package tools.refinery.store.dse.strategy;
import tools.refinery.store.dse.transition.DesignSpaceExplorationAdapter;
import tools.refinery.store.dse.transition.ObjectiveValue;
import tools.refinery.store.dse.transition.VersionWithObjectiveValue;
import tools.refinery.store.dse.transition.statespace.internal.ActivationStoreWorker;
import tools.refinery.store.map.Version;
import tools.refinery.store.model.Model;
import tools.refinery.store.statecoding.StateCoderAdapter;
import java.util.Random;
public class BestFirstWorker {
final BestFirstStoreManager storeManager;
final Model model;
final ActivationStoreWorker activationStoreWorker;
final StateCoderAdapter stateCoderAdapter;
final DesignSpaceExplorationAdapter explorationAdapter;
public BestFirstWorker(BestFirstStoreManager storeManager, Model model) {
this.storeManager = storeManager;
this.model = model;
explorationAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class);
stateCoderAdapter = model.getAdapter(StateCoderAdapter.class);
activationStoreWorker = new ActivationStoreWorker(storeManager.getActivationStore(),
explorationAdapter.getTransformations());
}
private VersionWithObjectiveValue last = null;
//public boolean isIncluded
public SubmitResult submit() {
if (explorationAdapter.checkExclude()) {
last = null;
return new SubmitResult(false, false, null, null);
}
Version version = model.commit();
ObjectiveValue objectiveValue = explorationAdapter.getObjectiveValue();
last = new VersionWithObjectiveValue(version, objectiveValue);
var code = stateCoderAdapter.calculateStateCode();
var accepted = explorationAdapter.checkAccept();
boolean isNew = storeManager.getEquivalenceClassStore().submit(last, code,
activationStoreWorker.calculateEmptyActivationSize(), accepted);
return new SubmitResult(isNew, accepted, objectiveValue, isNew ? last : null);
}
public void restoreToLast() {
if (explorationAdapter.getModel().hasUncommittedChanges()) {
explorationAdapter.getModel().restore(last.version());
}
}
public VersionWithObjectiveValue restoreToBest() {
var bestVersion = storeManager.getObjectiveStore().getBest();
if (bestVersion != null) {
this.model.restore(bestVersion.version());
}
return bestVersion;
}
public VersionWithObjectiveValue restoreToRandom(Random random) {
var randomVersion = storeManager.getObjectiveStore().getRandom(random);
last = randomVersion;
if (randomVersion != null) {
this.model.restore(randomVersion.version());
}
return randomVersion;
}
public int compare(VersionWithObjectiveValue s1, VersionWithObjectiveValue s2) {
return storeManager.getObjectiveStore().getComparator().compare(s1, s2);
}
public boolean stateHasUnvisited() {
if (!model.hasUncommittedChanges()) {
return storeManager.getActivationStore().hasUnmarkedActivation(last);
} else {
throw new IllegalStateException("The model has uncommitted changes!");
}
}
record RandomVisitResult(SubmitResult submitResult, boolean shouldRetry) {
}
public RandomVisitResult visitRandomUnvisited(Random random) {
if (!model.hasUncommittedChanges()) {
var visitResult = activationStoreWorker.fireRandomActivation(this.last, random);
if (visitResult.successfulVisit()) {
return new RandomVisitResult(submit(), visitResult.mayHaveMore());
} else {
return new RandomVisitResult(null, visitResult.mayHaveMore());
}
} else {
throw new IllegalStateException("The model has uncommitted changes!");
}
}
public boolean hasEnoughSolution() {
return storeManager.solutionStore.hasEnoughSolution();
}
}
|