diff options
Diffstat (limited to 'subprojects/store-dse/src/main/java')
42 files changed, 1987 insertions, 0 deletions
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/ActionFactory.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/ActionFactory.java new file mode 100644 index 00000000..48a508b4 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/ActionFactory.java | |||
@@ -0,0 +1,15 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse; | ||
7 | |||
8 | import tools.refinery.store.model.Model; | ||
9 | import tools.refinery.store.tuple.Tuple; | ||
10 | |||
11 | import java.util.function.Consumer; | ||
12 | |||
13 | public interface ActionFactory { | ||
14 | Consumer<Tuple> prepare(Model model); | ||
15 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/ModificationAdapter.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/ModificationAdapter.java new file mode 100644 index 00000000..f15c16e0 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/ModificationAdapter.java | |||
@@ -0,0 +1,24 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.modification; | ||
7 | |||
8 | import tools.refinery.store.adapter.ModelAdapter; | ||
9 | import tools.refinery.store.dse.modification.internal.ModificationBuilderImpl; | ||
10 | import tools.refinery.store.tuple.Tuple; | ||
11 | import tools.refinery.store.tuple.Tuple1; | ||
12 | |||
13 | public interface ModificationAdapter extends ModelAdapter { | ||
14 | |||
15 | int getModelSize(); | ||
16 | |||
17 | Tuple1 createObject(); | ||
18 | |||
19 | Tuple deleteObject(Tuple tuple); | ||
20 | |||
21 | static ModificationBuilder builder() { | ||
22 | return new ModificationBuilderImpl(); | ||
23 | } | ||
24 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/ModificationBuilder.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/ModificationBuilder.java new file mode 100644 index 00000000..48c22bdf --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/ModificationBuilder.java | |||
@@ -0,0 +1,11 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.modification; | ||
7 | |||
8 | import tools.refinery.store.adapter.ModelAdapterBuilder; | ||
9 | |||
10 | public interface ModificationBuilder extends ModelAdapterBuilder { | ||
11 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/ModificationStoreAdapter.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/ModificationStoreAdapter.java new file mode 100644 index 00000000..144c4d05 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/ModificationStoreAdapter.java | |||
@@ -0,0 +1,11 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.modification; | ||
7 | |||
8 | import tools.refinery.store.adapter.ModelStoreAdapter; | ||
9 | |||
10 | public interface ModificationStoreAdapter extends ModelStoreAdapter { | ||
11 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/internal/ModificationAdapterImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/internal/ModificationAdapterImpl.java new file mode 100644 index 00000000..b2a80d71 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/internal/ModificationAdapterImpl.java | |||
@@ -0,0 +1,62 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.modification.internal; | ||
7 | |||
8 | import tools.refinery.store.adapter.ModelStoreAdapter; | ||
9 | import tools.refinery.store.dse.modification.ModificationAdapter; | ||
10 | import tools.refinery.store.model.Interpretation; | ||
11 | import tools.refinery.store.model.Model; | ||
12 | import tools.refinery.store.representation.Symbol; | ||
13 | import tools.refinery.store.tuple.Tuple; | ||
14 | import tools.refinery.store.tuple.Tuple1; | ||
15 | |||
16 | public class ModificationAdapterImpl implements ModificationAdapter { | ||
17 | static final Symbol<Integer> NEXT_ID = Symbol.of("NEXT_ID", 0, Integer.class, 0); | ||
18 | |||
19 | final ModelStoreAdapter storeAdapter; | ||
20 | final Model model; | ||
21 | Interpretation<Integer> nodeCountInterpretation; | ||
22 | |||
23 | ModificationAdapterImpl(ModelStoreAdapter storeAdapter, Model model) { | ||
24 | this.storeAdapter = storeAdapter; | ||
25 | this.model = model; | ||
26 | this.nodeCountInterpretation = model.getInterpretation(NEXT_ID); | ||
27 | } | ||
28 | |||
29 | @Override | ||
30 | public Model getModel() { | ||
31 | return model; | ||
32 | } | ||
33 | |||
34 | @Override | ||
35 | public ModelStoreAdapter getStoreAdapter() { | ||
36 | return storeAdapter; | ||
37 | } | ||
38 | |||
39 | @Override | ||
40 | public int getModelSize() { | ||
41 | return nodeCountInterpretation.get(Tuple.of()); | ||
42 | } | ||
43 | |||
44 | @Override | ||
45 | public Tuple1 createObject() { | ||
46 | var newNodeId = getModelSize(); | ||
47 | nodeCountInterpretation.put(Tuple.of(), newNodeId + 1); | ||
48 | return Tuple.of(newNodeId); | ||
49 | } | ||
50 | |||
51 | @Override | ||
52 | public Tuple deleteObject(Tuple tuple) { | ||
53 | if (tuple.getSize() != 1) { | ||
54 | throw new IllegalArgumentException("Tuple size must be 1"); | ||
55 | } | ||
56 | // TODO: implement more efficient deletion | ||
57 | if (tuple.get(0) == getModelSize() - 1) { | ||
58 | nodeCountInterpretation.put(Tuple.of(), getModelSize() - 1); | ||
59 | } | ||
60 | return tuple; | ||
61 | } | ||
62 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/internal/ModificationBuilderImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/internal/ModificationBuilderImpl.java new file mode 100644 index 00000000..c4d38d22 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/internal/ModificationBuilderImpl.java | |||
@@ -0,0 +1,29 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.modification.internal; | ||
7 | |||
8 | import tools.refinery.store.adapter.AbstractModelAdapterBuilder; | ||
9 | import tools.refinery.store.dse.modification.ModificationBuilder; | ||
10 | import tools.refinery.store.dse.modification.ModificationStoreAdapter; | ||
11 | import tools.refinery.store.model.ModelStore; | ||
12 | import tools.refinery.store.model.ModelStoreBuilder; | ||
13 | import tools.refinery.store.statecoding.StateCoderBuilder; | ||
14 | |||
15 | public class ModificationBuilderImpl extends AbstractModelAdapterBuilder<ModificationStoreAdapter> implements ModificationBuilder { | ||
16 | |||
17 | @Override | ||
18 | protected void doConfigure(ModelStoreBuilder storeBuilder) { | ||
19 | storeBuilder.symbols(ModificationAdapterImpl.NEXT_ID); | ||
20 | storeBuilder.tryGetAdapter(StateCoderBuilder.class).ifPresent( | ||
21 | coderBuilder -> coderBuilder.exclude(ModificationAdapterImpl.NEXT_ID)); | ||
22 | super.doConfigure(storeBuilder); | ||
23 | } | ||
24 | |||
25 | @Override | ||
26 | protected ModificationStoreAdapter doBuild(ModelStore store) { | ||
27 | return new ModificationStoreAdapterImpl(store); | ||
28 | } | ||
29 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/internal/ModificationStoreAdapterImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/internal/ModificationStoreAdapterImpl.java new file mode 100644 index 00000000..913cb33f --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/modification/internal/ModificationStoreAdapterImpl.java | |||
@@ -0,0 +1,29 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.modification.internal; | ||
7 | |||
8 | import tools.refinery.store.adapter.ModelAdapter; | ||
9 | import tools.refinery.store.dse.modification.ModificationStoreAdapter; | ||
10 | import tools.refinery.store.model.Model; | ||
11 | import tools.refinery.store.model.ModelStore; | ||
12 | |||
13 | public class ModificationStoreAdapterImpl implements ModificationStoreAdapter { | ||
14 | ModelStore store; | ||
15 | |||
16 | ModificationStoreAdapterImpl(ModelStore store) { | ||
17 | this.store = store; | ||
18 | } | ||
19 | |||
20 | @Override | ||
21 | public ModelStore getStore() { | ||
22 | return store; | ||
23 | } | ||
24 | |||
25 | @Override | ||
26 | public ModelAdapter createModelAdapter(Model model) { | ||
27 | return null; | ||
28 | } | ||
29 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/BestFirstExplorer.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/BestFirstExplorer.java new file mode 100644 index 00000000..72bbbc55 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/BestFirstExplorer.java | |||
@@ -0,0 +1,164 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.strategy; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.ObjectiveValue; | ||
9 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
10 | import tools.refinery.store.model.Model; | ||
11 | |||
12 | import java.util.Random; | ||
13 | |||
14 | public class BestFirstExplorer extends BestFirstWorker { | ||
15 | final int id; | ||
16 | Random random; | ||
17 | public BestFirstExplorer(BestFirstStoreManager storeManager, Model model, int id) { | ||
18 | super(storeManager, model); | ||
19 | this.id = id; | ||
20 | this.random = new Random(id); | ||
21 | } | ||
22 | |||
23 | private boolean interrupted = false; | ||
24 | public void interrupt() { | ||
25 | this.interrupted = true; | ||
26 | } | ||
27 | |||
28 | private boolean shouldRun() { | ||
29 | return !interrupted && !hasEnoughSolution(); | ||
30 | } | ||
31 | |||
32 | public void explore() { | ||
33 | VersionWithObjectiveValue lastVisited = submit().newVersion(); | ||
34 | |||
35 | mainLoop: while (shouldRun()) { | ||
36 | |||
37 | if (lastVisited == null) { | ||
38 | var restored = this.restoreToBest(); | ||
39 | if(restored != null) { | ||
40 | lastVisited = restored; | ||
41 | } else { | ||
42 | return; | ||
43 | } | ||
44 | } | ||
45 | |||
46 | boolean tryActivation = true; | ||
47 | while(tryActivation && shouldRun()) { | ||
48 | RandomVisitResult randomVisitResult = this.visitRandomUnvisited(random); | ||
49 | |||
50 | tryActivation &= randomVisitResult.shouldRetry(); | ||
51 | var newSubmit = randomVisitResult.submitResult(); | ||
52 | if(newSubmit != null) { | ||
53 | if(!newSubmit.include()) { | ||
54 | restoreToLast(); | ||
55 | } else { | ||
56 | var newVisit = newSubmit.newVersion(); | ||
57 | int compareResult = compare(lastVisited,newVisit); | ||
58 | if(compareResult >= 0) { | ||
59 | lastVisited = newVisit; | ||
60 | continue mainLoop; | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | |||
66 | //final ObjectiveComparatorHelper objectiveComparatorHelper = dseAdapter.getObjectiveComparatorHelper(); | ||
67 | |||
68 | /*boolean globalConstraintsAreSatisfied = dseAdapter.checkGlobalConstraints(); | ||
69 | if (!globalConstraintsAreSatisfied) { | ||
70 | // Global constraint is not satisfied in the first state. Terminate. | ||
71 | return; | ||
72 | } | ||
73 | |||
74 | final Fitness firstFitness = dseAdapter.getFitness(); | ||
75 | if (firstFitness.isSatisfiesHardObjectives()) { | ||
76 | dseAdapter.newSolution(); | ||
77 | // First state is a solution. Terminate. | ||
78 | if (backTrackIfSolution) { | ||
79 | return; | ||
80 | } | ||
81 | } | ||
82 | |||
83 | if (maxDepth == 0) { | ||
84 | return; | ||
85 | }*/ | ||
86 | |||
87 | /* | ||
88 | var firstTrajectoryWithFitness = new TrajectoryWithFitness(dseAdapter.getTrajectory(), firstFitness); | ||
89 | trajectoriesToExplore.add(firstTrajectoryWithFitness); | ||
90 | TrajectoryWithFitness currentTrajectoryWithFitness = null; | ||
91 | */ | ||
92 | /* | ||
93 | Collection<Activation> activations = dseAdapter.getUntraversedActivations(); | ||
94 | Iterator<Activation> iterator = activations.iterator(); | ||
95 | |||
96 | while (iterator.hasNext()) { | ||
97 | final Activation nextActivation = iterator.next(); | ||
98 | if (!iterator.hasNext()) { | ||
99 | // Last untraversed activation of the state. | ||
100 | trajectoriesToExplore.remove(currentTrajectoryWithFitness); | ||
101 | } | ||
102 | |||
103 | // Executing new activation | ||
104 | dseAdapter.fireActivation(nextActivation); | ||
105 | if (dseAdapter.isCurrentStateAlreadyTraversed()) { | ||
106 | // The new state is already visited. | ||
107 | dseAdapter.backtrack(); | ||
108 | } else if (!dseAdapter.checkGlobalConstraints()) { | ||
109 | // Global constraint is not satisfied. | ||
110 | dseAdapter.backtrack(); | ||
111 | } else { | ||
112 | final Fitness nextFitness = dseAdapter.getFitness(); | ||
113 | if (nextFitness.isSatisfiesHardObjectives()) { | ||
114 | dseAdapter.newSolution(); | ||
115 | var solutions = dseAdapter.getSolutions().size(); | ||
116 | if (solutions >= maxSolutions) { | ||
117 | return; | ||
118 | } | ||
119 | // Found a solution. | ||
120 | if (backTrackIfSolution) { | ||
121 | dseAdapter.backtrack(); | ||
122 | continue; | ||
123 | } | ||
124 | } | ||
125 | if (dseAdapter.getDepth() >= maxDepth) { | ||
126 | // Reached max depth. | ||
127 | dseAdapter.backtrack(); | ||
128 | continue; | ||
129 | } | ||
130 | |||
131 | TrajectoryWithFitness nextTrajectoryWithFitness = new TrajectoryWithFitness( | ||
132 | dseAdapter.getTrajectory(), nextFitness); | ||
133 | trajectoriesToExplore.add(nextTrajectoryWithFitness); | ||
134 | |||
135 | int compare = objectiveComparatorHelper.compare(currentTrajectoryWithFitness.fitness, | ||
136 | nextTrajectoryWithFitness.fitness); | ||
137 | if (compare < 0) { | ||
138 | // Better fitness, moving on | ||
139 | currentTrajectoryWithFitness = nextTrajectoryWithFitness; | ||
140 | continue mainLoop; | ||
141 | } else if (compare == 0) { | ||
142 | if (onlyBetterFirst) { | ||
143 | // Equally good fitness, backtrack | ||
144 | dseAdapter.backtrack(); | ||
145 | } else { | ||
146 | // Equally good fitness, moving on | ||
147 | currentTrajectoryWithFitness = nextTrajectoryWithFitness; | ||
148 | continue mainLoop; | ||
149 | } | ||
150 | } else { | ||
151 | //"Worse fitness | ||
152 | currentTrajectoryWithFitness = null; | ||
153 | continue mainLoop; | ||
154 | } | ||
155 | } | ||
156 | } | ||
157 | |||
158 | // State is fully traversed. | ||
159 | currentTrajectoryWithFitness = null; | ||
160 | */ | ||
161 | } | ||
162 | // Interrupted. | ||
163 | } | ||
164 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/BestFirstStoreManager.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/BestFirstStoreManager.java new file mode 100644 index 00000000..693b0903 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/BestFirstStoreManager.java | |||
@@ -0,0 +1,72 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.strategy; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.DesignSpaceExplorationStoreAdapter; | ||
9 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
10 | import tools.refinery.store.dse.transition.statespace.ActivationStore; | ||
11 | import tools.refinery.store.dse.transition.statespace.EquivalenceClassStore; | ||
12 | import tools.refinery.store.dse.transition.statespace.ObjectivePriorityQueue; | ||
13 | import tools.refinery.store.dse.transition.statespace.SolutionStore; | ||
14 | import tools.refinery.store.dse.transition.statespace.internal.ActivationStoreImpl; | ||
15 | import tools.refinery.store.dse.transition.statespace.internal.FastEquivalenceClassStore; | ||
16 | import tools.refinery.store.dse.transition.statespace.internal.ObjectivePriorityQueueImpl; | ||
17 | import tools.refinery.store.dse.transition.statespace.internal.SolutionStoreImpl; | ||
18 | import tools.refinery.store.map.Version; | ||
19 | import tools.refinery.store.model.ModelStore; | ||
20 | import tools.refinery.store.statecoding.StateCoderStoreAdapter; | ||
21 | |||
22 | import java.util.function.Consumer; | ||
23 | |||
24 | public class BestFirstStoreManager { | ||
25 | ModelStore modelStore; | ||
26 | ObjectivePriorityQueue objectiveStore; | ||
27 | ActivationStore activationStore; | ||
28 | SolutionStore solutionStore; | ||
29 | EquivalenceClassStore equivalenceClassStore; | ||
30 | |||
31 | public BestFirstStoreManager(ModelStore modelStore) { | ||
32 | this.modelStore = modelStore; | ||
33 | DesignSpaceExplorationStoreAdapter storeAdapter = | ||
34 | modelStore.getAdapter(DesignSpaceExplorationStoreAdapter.class); | ||
35 | |||
36 | objectiveStore = new ObjectivePriorityQueueImpl(storeAdapter.getObjectives()); | ||
37 | Consumer<VersionWithObjectiveValue> whenAllActivationsVisited = x -> objectiveStore.remove(x); | ||
38 | activationStore = new ActivationStoreImpl(storeAdapter.getTransformations().size(), whenAllActivationsVisited); | ||
39 | solutionStore = new SolutionStoreImpl(1); | ||
40 | equivalenceClassStore = new FastEquivalenceClassStore(modelStore.getAdapter(StateCoderStoreAdapter.class)) { | ||
41 | @Override | ||
42 | protected void delegate(VersionWithObjectiveValue version, int[] emptyActivations, boolean accept) { | ||
43 | objectiveStore.submit(version); | ||
44 | activationStore.markNewAsVisited(version, emptyActivations); | ||
45 | if(accept) { | ||
46 | solutionStore.submit(version); | ||
47 | } | ||
48 | } | ||
49 | }; | ||
50 | } | ||
51 | |||
52 | ObjectivePriorityQueue getObjectiveStore() { | ||
53 | return objectiveStore; | ||
54 | } | ||
55 | |||
56 | ActivationStore getActivationStore() { | ||
57 | return activationStore; | ||
58 | } | ||
59 | |||
60 | SolutionStore getSolutionStore() { | ||
61 | return solutionStore; | ||
62 | } | ||
63 | |||
64 | EquivalenceClassStore getEquivalenceClassStore() { | ||
65 | return equivalenceClassStore; | ||
66 | } | ||
67 | |||
68 | public void startExploration(Version initial) { | ||
69 | BestFirstExplorer bestFirstExplorer = new BestFirstExplorer(this, modelStore.createModelForState(initial), 1); | ||
70 | bestFirstExplorer.explore(); | ||
71 | } | ||
72 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/BestFirstWorker.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/BestFirstWorker.java new file mode 100644 index 00000000..ea7fe43f --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/BestFirstWorker.java | |||
@@ -0,0 +1,113 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.strategy; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.DesignSpaceExplorationAdapter; | ||
9 | import tools.refinery.store.dse.transition.ObjectiveValue; | ||
10 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
11 | import tools.refinery.store.dse.transition.statespace.internal.ActivationStoreWorker; | ||
12 | import tools.refinery.store.map.Version; | ||
13 | import tools.refinery.store.model.Model; | ||
14 | import tools.refinery.store.statecoding.StateCoderAdapter; | ||
15 | |||
16 | |||
17 | import java.util.Random; | ||
18 | |||
19 | public class BestFirstWorker { | ||
20 | final BestFirstStoreManager storeManager; | ||
21 | final Model model; | ||
22 | final ActivationStoreWorker activationStoreWorker; | ||
23 | final StateCoderAdapter stateCoderAdapter; | ||
24 | final DesignSpaceExplorationAdapter explorationAdapter; | ||
25 | |||
26 | public BestFirstWorker(BestFirstStoreManager storeManager, Model model) { | ||
27 | this.storeManager = storeManager; | ||
28 | this.model = model; | ||
29 | |||
30 | explorationAdapter = model.getAdapter(DesignSpaceExplorationAdapter.class); | ||
31 | stateCoderAdapter = model.getAdapter(StateCoderAdapter.class); | ||
32 | activationStoreWorker = new ActivationStoreWorker(storeManager.getActivationStore(), | ||
33 | explorationAdapter.getTransformations()); | ||
34 | } | ||
35 | |||
36 | private VersionWithObjectiveValue last = null; | ||
37 | |||
38 | //public boolean isIncluded | ||
39 | |||
40 | public SubmitResult submit() { | ||
41 | if (explorationAdapter.checkExclude()) { | ||
42 | last = null; | ||
43 | return new SubmitResult(false, false, null, null); | ||
44 | } | ||
45 | |||
46 | Version version = model.commit(); | ||
47 | ObjectiveValue objectiveValue = explorationAdapter.getObjectiveValue(); | ||
48 | var res = new VersionWithObjectiveValue(version, objectiveValue); | ||
49 | var code = stateCoderAdapter.calculateStateCode(); | ||
50 | var accepted = explorationAdapter.checkAccept(); | ||
51 | |||
52 | boolean isNew = storeManager.getEquivalenceClassStore().submit(res, code, | ||
53 | activationStoreWorker.calculateEmptyActivationSize(), accepted); | ||
54 | |||
55 | last = new VersionWithObjectiveValue(version, objectiveValue); | ||
56 | return new SubmitResult(isNew, accepted, objectiveValue, last); | ||
57 | } | ||
58 | |||
59 | public void restoreToLast() { | ||
60 | if (explorationAdapter.getModel().hasUncommittedChanges()) { | ||
61 | explorationAdapter.getModel().restore(last.version()); | ||
62 | } | ||
63 | } | ||
64 | |||
65 | public VersionWithObjectiveValue restoreToBest() { | ||
66 | var bestVersion = storeManager.getObjectiveStore().getBest(); | ||
67 | if (bestVersion != null) { | ||
68 | this.model.restore(bestVersion.version()); | ||
69 | } | ||
70 | return bestVersion; | ||
71 | } | ||
72 | |||
73 | public VersionWithObjectiveValue restoreToRandom(Random random) { | ||
74 | var randomVersion = storeManager.getObjectiveStore().getRandom(random); | ||
75 | last = randomVersion; | ||
76 | if (randomVersion != null) { | ||
77 | this.model.restore(randomVersion.version()); | ||
78 | } | ||
79 | return randomVersion; | ||
80 | } | ||
81 | |||
82 | public int compare(VersionWithObjectiveValue s1, VersionWithObjectiveValue s2) { | ||
83 | return storeManager.getObjectiveStore().getComparator().compare(s1, s2); | ||
84 | } | ||
85 | |||
86 | public boolean stateHasUnvisited() { | ||
87 | if (!model.hasUncommittedChanges()) { | ||
88 | return storeManager.getActivationStore().hasUnmarkedActivation(last); | ||
89 | } else { | ||
90 | throw new IllegalStateException("The model has uncommitted changes!"); | ||
91 | } | ||
92 | } | ||
93 | |||
94 | record RandomVisitResult(SubmitResult submitResult, boolean shouldRetry) { | ||
95 | } | ||
96 | |||
97 | public RandomVisitResult visitRandomUnvisited(Random random) { | ||
98 | if (!model.hasUncommittedChanges()) { | ||
99 | var visitResult = activationStoreWorker.fireRandomActivation(this.last, random); | ||
100 | if (visitResult.successfulVisit()) { | ||
101 | return new RandomVisitResult(submit(), visitResult.mayHaveMore()); | ||
102 | } else { | ||
103 | return new RandomVisitResult(null, visitResult.mayHaveMore()); | ||
104 | } | ||
105 | } else { | ||
106 | throw new IllegalStateException("The model has uncommitted changes!"); | ||
107 | } | ||
108 | } | ||
109 | |||
110 | public boolean hasEnoughSolution() { | ||
111 | return storeManager.solutionStore.hasEnoughSolution(); | ||
112 | } | ||
113 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/SubmitResult.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/SubmitResult.java new file mode 100644 index 00000000..37d548d7 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/SubmitResult.java | |||
@@ -0,0 +1,14 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.strategy; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.ObjectiveValue; | ||
9 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
10 | import tools.refinery.store.map.Version; | ||
11 | |||
12 | public record SubmitResult(boolean include, boolean accepted, ObjectiveValue objective, VersionWithObjectiveValue newVersion) { | ||
13 | |||
14 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationAdapter.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationAdapter.java new file mode 100644 index 00000000..37448309 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationAdapter.java | |||
@@ -0,0 +1,31 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition; | ||
7 | |||
8 | import tools.refinery.store.adapter.ModelAdapter; | ||
9 | import tools.refinery.store.dse.transition.internal.DesignSpaceExplorationBuilderImpl; | ||
10 | import tools.refinery.store.map.Version; | ||
11 | import tools.refinery.store.tuple.Tuple; | ||
12 | import tools.refinery.store.tuple.Tuple1; | ||
13 | |||
14 | import java.util.Collection; | ||
15 | import java.util.List; | ||
16 | |||
17 | public interface DesignSpaceExplorationAdapter extends ModelAdapter { | ||
18 | |||
19 | |||
20 | |||
21 | @Override | ||
22 | DesignSpaceExplorationStoreAdapter getStoreAdapter(); | ||
23 | |||
24 | static DesignSpaceExplorationBuilder builder() { | ||
25 | return new DesignSpaceExplorationBuilderImpl(); | ||
26 | } | ||
27 | List<Transformation> getTransformations(); | ||
28 | boolean checkAccept(); | ||
29 | boolean checkExclude(); | ||
30 | ObjectiveValue getObjectiveValue(); | ||
31 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationBuilder.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationBuilder.java new file mode 100644 index 00000000..3855a20a --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationBuilder.java | |||
@@ -0,0 +1,59 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition; | ||
7 | |||
8 | import tools.refinery.store.adapter.ModelAdapterBuilder; | ||
9 | import tools.refinery.store.dse.transition.objectives.Criterion; | ||
10 | import tools.refinery.store.dse.transition.objectives.Objective; | ||
11 | |||
12 | import java.util.Collection; | ||
13 | import java.util.List; | ||
14 | |||
15 | public interface DesignSpaceExplorationBuilder extends ModelAdapterBuilder { | ||
16 | |||
17 | DesignSpaceExplorationBuilder transformation(TransformationRule transformationRuleDefinition); | ||
18 | default DesignSpaceExplorationBuilder transformations(TransformationRule... transformationRuleDefinitions) { | ||
19 | return transformations(List.of(transformationRuleDefinitions)); | ||
20 | } | ||
21 | |||
22 | default DesignSpaceExplorationBuilder transformations(Collection<? extends TransformationRule> transformationRules) { | ||
23 | transformationRules.forEach(this::transformation); | ||
24 | return this; | ||
25 | } | ||
26 | |||
27 | DesignSpaceExplorationBuilder accept(Criterion criteria); | ||
28 | |||
29 | default DesignSpaceExplorationBuilder accept(Criterion... criteria) { | ||
30 | return accept(List.of(criteria)); | ||
31 | } | ||
32 | |||
33 | default DesignSpaceExplorationBuilder accept(Collection<Criterion> criteria) { | ||
34 | criteria.forEach(this::accept); | ||
35 | return this; | ||
36 | } | ||
37 | |||
38 | DesignSpaceExplorationBuilder exclude(Criterion criteria); | ||
39 | |||
40 | default DesignSpaceExplorationBuilder exclude(Criterion... criteria) { | ||
41 | return exclude(List.of(criteria)); | ||
42 | } | ||
43 | |||
44 | default DesignSpaceExplorationBuilder exclude(Collection<Criterion> criteria) { | ||
45 | criteria.forEach(this::exclude); | ||
46 | return this; | ||
47 | } | ||
48 | |||
49 | DesignSpaceExplorationBuilder objective(Objective objective); | ||
50 | |||
51 | default DesignSpaceExplorationBuilder objectives(Objective... objectives) { | ||
52 | return objectives(List.of(objectives)); | ||
53 | } | ||
54 | |||
55 | default DesignSpaceExplorationBuilder objectives(Collection<? extends Objective> objectives) { | ||
56 | objectives.forEach(this::objective); | ||
57 | return this; | ||
58 | } | ||
59 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationStoreAdapter.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationStoreAdapter.java new file mode 100644 index 00000000..5c8c7a4d --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/DesignSpaceExplorationStoreAdapter.java | |||
@@ -0,0 +1,27 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition; | ||
7 | |||
8 | import tools.refinery.store.adapter.ModelStoreAdapter; | ||
9 | import tools.refinery.store.dse.transition.objectives.Criterion; | ||
10 | import tools.refinery.store.dse.transition.objectives.Objective; | ||
11 | import tools.refinery.store.model.Model; | ||
12 | |||
13 | import java.util.List; | ||
14 | |||
15 | public interface DesignSpaceExplorationStoreAdapter extends ModelStoreAdapter | ||
16 | { | ||
17 | @Override | ||
18 | DesignSpaceExplorationAdapter createModelAdapter(Model model); | ||
19 | |||
20 | List<TransformationRule> getTransformations(); | ||
21 | |||
22 | List<Criterion> getAccepts(); | ||
23 | |||
24 | List<Criterion> getExcludes(); | ||
25 | |||
26 | List<Objective> getObjectives(); | ||
27 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/ObjectiveValue.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/ObjectiveValue.java new file mode 100644 index 00000000..89ee61c8 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/ObjectiveValue.java | |||
@@ -0,0 +1,24 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition; | ||
7 | |||
8 | public interface ObjectiveValue { | ||
9 | double get(int index); | ||
10 | int getSize(); | ||
11 | |||
12 | static ObjectiveValue of(double v1) { | ||
13 | return new ObjectiveValues.ObjectiveValue1(v1); | ||
14 | } | ||
15 | |||
16 | static ObjectiveValue of(double v1, double v2) { | ||
17 | return new ObjectiveValues.ObjectiveValue2(v1,v2); | ||
18 | } | ||
19 | |||
20 | static ObjectiveValue of(double[] v) { | ||
21 | return new ObjectiveValues.ObjectiveValueN(v); | ||
22 | } | ||
23 | |||
24 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/ObjectiveValues.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/ObjectiveValues.java new file mode 100644 index 00000000..60913ff3 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/ObjectiveValues.java | |||
@@ -0,0 +1,69 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition; | ||
7 | |||
8 | import java.util.Arrays; | ||
9 | |||
10 | public interface ObjectiveValues { | ||
11 | public record ObjectiveValue1(double value0) implements ObjectiveValue { | ||
12 | @Override | ||
13 | public double get(int index) { | ||
14 | if(index == 0) return value0; | ||
15 | else throw new IllegalArgumentException("No value at " + index); | ||
16 | } | ||
17 | |||
18 | @Override | ||
19 | public int getSize() { | ||
20 | return 1; | ||
21 | } | ||
22 | } | ||
23 | public record ObjectiveValue2(double value0, double value1) implements ObjectiveValue { | ||
24 | @Override | ||
25 | public double get(int index) { | ||
26 | if(index == 0) return value0; | ||
27 | else if(index == 1) return value1; | ||
28 | else throw new IllegalArgumentException("No value at " + index); | ||
29 | } | ||
30 | |||
31 | @Override | ||
32 | public int getSize() { | ||
33 | return 2; | ||
34 | } | ||
35 | } | ||
36 | public record ObjectiveValueN(double[] values) implements ObjectiveValue { | ||
37 | @Override | ||
38 | public double get(int index) { | ||
39 | return values[index]; | ||
40 | } | ||
41 | |||
42 | @Override | ||
43 | public int getSize() { | ||
44 | return values().length; | ||
45 | } | ||
46 | |||
47 | @Override | ||
48 | public boolean equals(Object o) { | ||
49 | if (this == o) return true; | ||
50 | if (o == null || getClass() != o.getClass()) return false; | ||
51 | |||
52 | ObjectiveValueN that = (ObjectiveValueN) o; | ||
53 | |||
54 | return Arrays.equals(values, that.values); | ||
55 | } | ||
56 | |||
57 | @Override | ||
58 | public int hashCode() { | ||
59 | return Arrays.hashCode(values); | ||
60 | } | ||
61 | |||
62 | @Override | ||
63 | public String toString() { | ||
64 | return "ObjectiveValueN{" + | ||
65 | "values=" + Arrays.toString(values) + | ||
66 | '}'; | ||
67 | } | ||
68 | } | ||
69 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/Transformation.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/Transformation.java new file mode 100644 index 00000000..ab9fda3e --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/Transformation.java | |||
@@ -0,0 +1,44 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition; | ||
7 | |||
8 | import tools.refinery.store.query.resultset.OrderedResultSet; | ||
9 | import tools.refinery.store.query.resultset.ResultSet; | ||
10 | import tools.refinery.store.tuple.Tuple; | ||
11 | |||
12 | import java.util.function.Consumer; | ||
13 | |||
14 | public class Transformation { | ||
15 | private final TransformationRule definition; | ||
16 | |||
17 | private final OrderedResultSet<Boolean> activations; | ||
18 | |||
19 | private final Consumer<Tuple> action; | ||
20 | |||
21 | public Transformation(TransformationRule definition, OrderedResultSet<Boolean> activations, Consumer<Tuple> action) { | ||
22 | this.definition = definition; | ||
23 | this.activations = activations; | ||
24 | this.action = action; | ||
25 | } | ||
26 | |||
27 | public TransformationRule getDefinition() { | ||
28 | return definition; | ||
29 | } | ||
30 | |||
31 | public ResultSet<Boolean> getAllActivationsAsResultSet() { | ||
32 | return activations; | ||
33 | } | ||
34 | |||
35 | public Tuple getActivation(int index) { | ||
36 | return activations.getKey(index); | ||
37 | } | ||
38 | |||
39 | public boolean fireActivation(Tuple activation) { | ||
40 | action.accept(activation); | ||
41 | //queryEngine.flushChanges(); | ||
42 | return true; | ||
43 | } | ||
44 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/TransformationRule.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/TransformationRule.java new file mode 100644 index 00000000..d64a3db1 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/TransformationRule.java | |||
@@ -0,0 +1,63 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition; | ||
7 | |||
8 | import tools.refinery.store.model.Model; | ||
9 | import tools.refinery.store.model.ModelStoreBuilder; | ||
10 | import tools.refinery.store.query.ModelQueryAdapter; | ||
11 | import tools.refinery.store.query.ModelQueryBuilder; | ||
12 | import tools.refinery.store.query.dnf.RelationalQuery; | ||
13 | import tools.refinery.store.dse.ActionFactory; | ||
14 | import tools.refinery.store.query.resultset.OrderedResultSet; | ||
15 | import tools.refinery.store.query.resultset.ResultSet; | ||
16 | import tools.refinery.store.tuple.Tuple; | ||
17 | |||
18 | import java.util.*; | ||
19 | |||
20 | public class TransformationRule { | ||
21 | |||
22 | private final String name; | ||
23 | private final RelationalQuery precondition; | ||
24 | private final ActionFactory actionFactory; | ||
25 | |||
26 | private Random random; | ||
27 | private ModelQueryAdapter queryEngine; | ||
28 | |||
29 | public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory) { | ||
30 | this(name, precondition, actionFactory, new Random()); | ||
31 | } | ||
32 | |||
33 | public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory, long seed) { | ||
34 | this(name, precondition, actionFactory, new Random(seed)); | ||
35 | } | ||
36 | |||
37 | public TransformationRule(String name, RelationalQuery precondition, ActionFactory actionFactory, Random random) { | ||
38 | this.name = name; | ||
39 | this.precondition = precondition; | ||
40 | this.actionFactory = actionFactory; | ||
41 | this.random = random; | ||
42 | } | ||
43 | public void doConfigure(ModelStoreBuilder storeBuilder) { | ||
44 | var queryBuilder = storeBuilder.getAdapter(ModelQueryBuilder.class); | ||
45 | queryBuilder.query(this.precondition); | ||
46 | } | ||
47 | |||
48 | public Transformation prepare(Model model) { | ||
49 | var queryEngine = model.getAdapter(ModelQueryAdapter.class); | ||
50 | var activations = new OrderedResultSet<>(queryEngine.getResultSet(precondition)); | ||
51 | var action = actionFactory.prepare(model); | ||
52 | return new Transformation(this,activations,action); | ||
53 | } | ||
54 | |||
55 | public String getName() { | ||
56 | return name; | ||
57 | } | ||
58 | |||
59 | public RelationalQuery getPrecondition() { | ||
60 | return precondition; | ||
61 | } | ||
62 | |||
63 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/VersionWithObjectiveValue.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/VersionWithObjectiveValue.java new file mode 100644 index 00000000..ca28e27f --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/VersionWithObjectiveValue.java | |||
@@ -0,0 +1,11 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition; | ||
7 | |||
8 | import tools.refinery.store.map.Version; | ||
9 | |||
10 | public record VersionWithObjectiveValue(Version version, ObjectiveValue objectiveValue) { | ||
11 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationAdapterImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationAdapterImpl.java new file mode 100644 index 00000000..e1a29d40 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationAdapterImpl.java | |||
@@ -0,0 +1,90 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.internal; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.DesignSpaceExplorationAdapter; | ||
9 | import tools.refinery.store.dse.transition.DesignSpaceExplorationStoreAdapter; | ||
10 | import tools.refinery.store.dse.transition.ObjectiveValue; | ||
11 | import tools.refinery.store.dse.transition.Transformation; | ||
12 | import tools.refinery.store.dse.transition.objectives.CriterionCalculator; | ||
13 | import tools.refinery.store.dse.transition.objectives.ObjectiveCalculator; | ||
14 | import tools.refinery.store.model.Model; | ||
15 | |||
16 | import java.util.List; | ||
17 | |||
18 | public class DesignSpaceExplorationAdapterImpl implements DesignSpaceExplorationAdapter { | ||
19 | final Model model; | ||
20 | final DesignSpaceExplorationStoreAdapter designSpaceExplorationStoreAdapter; | ||
21 | |||
22 | final List<Transformation> transformations; | ||
23 | final List<CriterionCalculator> accepts; | ||
24 | final List<CriterionCalculator> excludes; | ||
25 | final List<ObjectiveCalculator> objectives; | ||
26 | |||
27 | public DesignSpaceExplorationAdapterImpl(Model model, | ||
28 | DesignSpaceExplorationStoreAdapter designSpaceExplorationStoreAdapter, | ||
29 | List<Transformation> transformations, | ||
30 | List<CriterionCalculator> accepts, | ||
31 | List<CriterionCalculator> excludes, | ||
32 | List<ObjectiveCalculator> objectives) { | ||
33 | this.model = model; | ||
34 | this.designSpaceExplorationStoreAdapter = designSpaceExplorationStoreAdapter; | ||
35 | |||
36 | this.transformations = transformations; | ||
37 | this.accepts = accepts; | ||
38 | this.excludes = excludes; | ||
39 | this.objectives = objectives; | ||
40 | } | ||
41 | |||
42 | @Override | ||
43 | public Model getModel() { | ||
44 | return model; | ||
45 | } | ||
46 | |||
47 | @Override | ||
48 | public DesignSpaceExplorationStoreAdapter getStoreAdapter() { | ||
49 | return designSpaceExplorationStoreAdapter; | ||
50 | } | ||
51 | |||
52 | public List<Transformation> getTransformations() { | ||
53 | return transformations; | ||
54 | } | ||
55 | |||
56 | @Override | ||
57 | public boolean checkAccept() { | ||
58 | for (var accept : this.accepts) { | ||
59 | if (!accept.isSatisfied()) { | ||
60 | return false; | ||
61 | } | ||
62 | } | ||
63 | return true; | ||
64 | } | ||
65 | |||
66 | @Override | ||
67 | public boolean checkExclude() { | ||
68 | for (var exclude : this.excludes) { | ||
69 | if (exclude.isSatisfied()) { | ||
70 | return true; | ||
71 | } | ||
72 | } | ||
73 | return false; | ||
74 | } | ||
75 | |||
76 | @Override | ||
77 | public ObjectiveValue getObjectiveValue() { | ||
78 | if (objectives.size() == 1) { | ||
79 | return ObjectiveValue.of(objectives.get(0).getValue()); | ||
80 | } else if (objectives.size() == 2) { | ||
81 | return ObjectiveValue.of(objectives.get(0).getValue(), objectives.get(1).getValue()); | ||
82 | } else { | ||
83 | double[] res = new double[objectives.size()]; | ||
84 | for (int i = 0; i < objectives.size(); i++) { | ||
85 | res[i] = objectives.get(i).getValue(); | ||
86 | } | ||
87 | return ObjectiveValue.of(res); | ||
88 | } | ||
89 | } | ||
90 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationBuilderImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationBuilderImpl.java new file mode 100644 index 00000000..4371cc03 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationBuilderImpl.java | |||
@@ -0,0 +1,75 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.internal; | ||
7 | |||
8 | import tools.refinery.store.adapter.AbstractModelAdapterBuilder; | ||
9 | import tools.refinery.store.dse.transition.DesignSpaceExplorationBuilder; | ||
10 | import tools.refinery.store.dse.transition.TransformationRule; | ||
11 | import tools.refinery.store.dse.transition.objectives.Criterion; | ||
12 | import tools.refinery.store.dse.transition.objectives.Objective; | ||
13 | import tools.refinery.store.model.ModelStore; | ||
14 | import tools.refinery.store.model.ModelStoreBuilder; | ||
15 | |||
16 | import java.util.ArrayList; | ||
17 | import java.util.LinkedHashSet; | ||
18 | import java.util.List; | ||
19 | |||
20 | public class DesignSpaceExplorationBuilderImpl | ||
21 | extends AbstractModelAdapterBuilder<DesignSpaceExplorationStoreAdapterImpl> | ||
22 | implements DesignSpaceExplorationBuilder { | ||
23 | |||
24 | LinkedHashSet<TransformationRule> transformationRuleDefinitions = new LinkedHashSet<>(); | ||
25 | LinkedHashSet<Criterion> accepts = new LinkedHashSet<>(); | ||
26 | LinkedHashSet<Criterion> excludes = new LinkedHashSet<>(); | ||
27 | LinkedHashSet<Objective> objectives = new LinkedHashSet<>(); | ||
28 | |||
29 | @Override | ||
30 | public DesignSpaceExplorationBuilder transformation(TransformationRule transformationRuleDefinition) { | ||
31 | transformationRuleDefinitions.add(transformationRuleDefinition); | ||
32 | return this; | ||
33 | } | ||
34 | |||
35 | @Override | ||
36 | public DesignSpaceExplorationBuilder accept(Criterion criteria) { | ||
37 | accepts.add(criteria); | ||
38 | return this; | ||
39 | } | ||
40 | |||
41 | @Override | ||
42 | public DesignSpaceExplorationBuilder exclude(Criterion criteria) { | ||
43 | excludes.add(criteria); | ||
44 | return this; | ||
45 | } | ||
46 | |||
47 | |||
48 | @Override | ||
49 | public DesignSpaceExplorationBuilder objective(Objective objective) { | ||
50 | objectives.add(objective); | ||
51 | return this; | ||
52 | } | ||
53 | |||
54 | @Override | ||
55 | protected void doConfigure(ModelStoreBuilder storeBuilder) { | ||
56 | transformationRuleDefinitions.forEach(x -> x.doConfigure(storeBuilder)); | ||
57 | accepts.forEach(x -> x.doConfigure(storeBuilder)); | ||
58 | excludes.forEach(x -> x.doConfigure(storeBuilder)); | ||
59 | objectives.forEach(x -> x.doConfigure(storeBuilder)); | ||
60 | |||
61 | super.doConfigure(storeBuilder); | ||
62 | } | ||
63 | |||
64 | @Override | ||
65 | protected DesignSpaceExplorationStoreAdapterImpl doBuild(ModelStore store) { | ||
66 | List<TransformationRule> transformationRuleDefinitiions1 = new ArrayList<>(transformationRuleDefinitions); | ||
67 | List<Criterion> accepts1 = new ArrayList<>(accepts); | ||
68 | List<Criterion> excludes1 = new ArrayList<>(excludes); | ||
69 | List<Objective> objectives1 = new ArrayList<>(objectives); | ||
70 | |||
71 | return new DesignSpaceExplorationStoreAdapterImpl(store, | ||
72 | transformationRuleDefinitiions1, accepts1, | ||
73 | excludes1, objectives1); | ||
74 | } | ||
75 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationStoreAdapterImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationStoreAdapterImpl.java new file mode 100644 index 00000000..3319e148 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/internal/DesignSpaceExplorationStoreAdapterImpl.java | |||
@@ -0,0 +1,70 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.internal; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.DesignSpaceExplorationStoreAdapter; | ||
9 | import tools.refinery.store.dse.transition.Transformation; | ||
10 | import tools.refinery.store.dse.transition.TransformationRule; | ||
11 | import tools.refinery.store.dse.transition.objectives.Criterion; | ||
12 | import tools.refinery.store.dse.transition.objectives.CriterionCalculator; | ||
13 | import tools.refinery.store.dse.transition.objectives.Objective; | ||
14 | import tools.refinery.store.dse.transition.objectives.ObjectiveCalculator; | ||
15 | import tools.refinery.store.model.Model; | ||
16 | import tools.refinery.store.model.ModelStore; | ||
17 | |||
18 | import java.util.List; | ||
19 | |||
20 | public class DesignSpaceExplorationStoreAdapterImpl implements DesignSpaceExplorationStoreAdapter { | ||
21 | protected final ModelStore store; | ||
22 | |||
23 | protected final List<TransformationRule> transformationRuleDefinitions; | ||
24 | protected final List<Criterion> accepts; | ||
25 | protected final List<Criterion> excludes; | ||
26 | protected final List<Objective> objectives; | ||
27 | |||
28 | public DesignSpaceExplorationStoreAdapterImpl(ModelStore store, | ||
29 | List<TransformationRule> transformationRuleDefinitions, | ||
30 | List<Criterion> accepts, List<Criterion> excludes, | ||
31 | List<Objective> objectives) { | ||
32 | this.store = store; | ||
33 | |||
34 | this.transformationRuleDefinitions = transformationRuleDefinitions; | ||
35 | this.accepts = accepts; | ||
36 | this.excludes = excludes; | ||
37 | this.objectives = objectives; | ||
38 | } | ||
39 | |||
40 | @Override | ||
41 | public ModelStore getStore() { | ||
42 | return store; | ||
43 | } | ||
44 | |||
45 | @Override | ||
46 | public DesignSpaceExplorationAdapterImpl createModelAdapter(Model model) { | ||
47 | final List<Transformation> t = this.transformationRuleDefinitions.stream().map(x->x.prepare(model)).toList(); | ||
48 | final List<CriterionCalculator> a = this.accepts.stream().map(x->x.createCalculator(model)).toList(); | ||
49 | final List<CriterionCalculator> e = this.excludes.stream().map(x->x.createCalculator(model)).toList(); | ||
50 | final List<ObjectiveCalculator> o = this.objectives.stream().map(x->x.createCalculator(model)).toList(); | ||
51 | |||
52 | return new DesignSpaceExplorationAdapterImpl(model, this, t, a, e, o); | ||
53 | } | ||
54 | @Override | ||
55 | public List<TransformationRule> getTransformations() { | ||
56 | return transformationRuleDefinitions; | ||
57 | } | ||
58 | @Override | ||
59 | public List<Criterion> getAccepts() { | ||
60 | return accepts; | ||
61 | } | ||
62 | @Override | ||
63 | public List<Criterion> getExcludes() { | ||
64 | return excludes; | ||
65 | } | ||
66 | @Override | ||
67 | public List<Objective> getObjectives() { | ||
68 | return objectives; | ||
69 | } | ||
70 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/Criterion.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/Criterion.java new file mode 100644 index 00000000..66ca6f5e --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/Criterion.java | |||
@@ -0,0 +1,15 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.objectives; | ||
7 | |||
8 | import tools.refinery.store.model.Model; | ||
9 | import tools.refinery.store.model.ModelStoreBuilder; | ||
10 | |||
11 | public interface Criterion { | ||
12 | default void doConfigure(ModelStoreBuilder storeBuilder) { | ||
13 | } | ||
14 | CriterionCalculator createCalculator(Model model); | ||
15 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/CriterionCalculator.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/CriterionCalculator.java new file mode 100644 index 00000000..944ffed6 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/CriterionCalculator.java | |||
@@ -0,0 +1,10 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.objectives; | ||
7 | |||
8 | public interface CriterionCalculator { | ||
9 | boolean isSatisfied(); | ||
10 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/Objective.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/Objective.java new file mode 100644 index 00000000..b5924455 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/Objective.java | |||
@@ -0,0 +1,15 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.objectives; | ||
7 | |||
8 | import tools.refinery.store.model.Model; | ||
9 | import tools.refinery.store.model.ModelStoreBuilder; | ||
10 | |||
11 | public interface Objective { | ||
12 | default void doConfigure(ModelStoreBuilder storeBuilder) { | ||
13 | } | ||
14 | ObjectiveCalculator createCalculator(Model model); | ||
15 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/ObjectiveCalculator.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/ObjectiveCalculator.java new file mode 100644 index 00000000..f01b8de9 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/ObjectiveCalculator.java | |||
@@ -0,0 +1,10 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.objectives; | ||
7 | |||
8 | public interface ObjectiveCalculator { | ||
9 | double getValue(); | ||
10 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryCriteria.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryCriteria.java new file mode 100644 index 00000000..e2260cca --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryCriteria.java | |||
@@ -0,0 +1,44 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.objectives; | ||
7 | |||
8 | import tools.refinery.store.model.Model; | ||
9 | import tools.refinery.store.model.ModelStoreBuilder; | ||
10 | import tools.refinery.store.query.ModelQueryAdapter; | ||
11 | import tools.refinery.store.query.ModelQueryBuilder; | ||
12 | import tools.refinery.store.query.dnf.AnyQuery; | ||
13 | |||
14 | public class QueryCriteria implements Criterion { | ||
15 | protected final boolean acceptIfHasMatch; | ||
16 | protected final AnyQuery query; | ||
17 | |||
18 | /** | ||
19 | * Criteria based on the existence of matches evaluated on the model. | ||
20 | * @param query The query evaluated on the model. | ||
21 | * @param acceptIfHasMatch If true, the criteria satisfied if the query has any match on the model. Otherwise, | ||
22 | * the criteria satisfied if the query has no match on the model. | ||
23 | */ | ||
24 | public QueryCriteria(AnyQuery query, boolean acceptIfHasMatch) { | ||
25 | this.query = query; | ||
26 | this.acceptIfHasMatch = acceptIfHasMatch; | ||
27 | } | ||
28 | |||
29 | @Override | ||
30 | public CriterionCalculator createCalculator(Model model) { | ||
31 | var resultSet = model.getAdapter(ModelQueryAdapter.class).getResultSet(query); | ||
32 | if(acceptIfHasMatch) { | ||
33 | return () -> resultSet.size() > 0; | ||
34 | } else { | ||
35 | return () -> resultSet.size() == 0; | ||
36 | } | ||
37 | } | ||
38 | |||
39 | @Override | ||
40 | public void doConfigure(ModelStoreBuilder storeBuilder) { | ||
41 | Criterion.super.doConfigure(storeBuilder); | ||
42 | storeBuilder.getAdapter(ModelQueryBuilder.class).query(query); | ||
43 | } | ||
44 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryObjective.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryObjective.java new file mode 100644 index 00000000..dfddccfc --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/objectives/QueryObjective.java | |||
@@ -0,0 +1,44 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.objectives; | ||
7 | |||
8 | import tools.refinery.store.model.Model; | ||
9 | import tools.refinery.store.model.ModelStoreBuilder; | ||
10 | import tools.refinery.store.query.ModelQueryAdapter; | ||
11 | import tools.refinery.store.query.ModelQueryBuilder; | ||
12 | import tools.refinery.store.query.dnf.FunctionalQuery; | ||
13 | |||
14 | public class QueryObjective implements Objective { | ||
15 | protected final FunctionalQuery<? extends Number> objectiveFunction; | ||
16 | |||
17 | public QueryObjective(FunctionalQuery<? extends Number> objectiveFunction) { | ||
18 | this.objectiveFunction = objectiveFunction; | ||
19 | } | ||
20 | |||
21 | @Override | ||
22 | public ObjectiveCalculator createCalculator(Model model) { | ||
23 | var resultSet = model.getAdapter(ModelQueryAdapter.class).getResultSet(objectiveFunction); | ||
24 | return () -> { | ||
25 | var cursor = resultSet.getAll(); | ||
26 | boolean hasElement = cursor.move(); | ||
27 | if(hasElement) { | ||
28 | double result = cursor.getValue().doubleValue(); | ||
29 | if(cursor.move()) { | ||
30 | throw new IllegalStateException("Query providing the objective function has multiple values!"); | ||
31 | } | ||
32 | return result; | ||
33 | } else { | ||
34 | throw new IllegalStateException("Query providing the objective function has no values!"); | ||
35 | } | ||
36 | }; | ||
37 | } | ||
38 | |||
39 | @Override | ||
40 | public void doConfigure(ModelStoreBuilder storeBuilder) { | ||
41 | Objective.super.doConfigure(storeBuilder); | ||
42 | storeBuilder.getAdapter(ModelQueryBuilder.class).query(objectiveFunction); | ||
43 | } | ||
44 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/ActivationStore.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/ActivationStore.java new file mode 100644 index 00000000..52e0611d --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/ActivationStore.java | |||
@@ -0,0 +1,18 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
9 | import tools.refinery.store.map.Version; | ||
10 | |||
11 | import java.util.Random; | ||
12 | |||
13 | public interface ActivationStore { | ||
14 | record VisitResult(boolean successfulVisit, boolean mayHaveMore, int transformation, int activation) { } | ||
15 | VisitResult markNewAsVisited(VersionWithObjectiveValue to, int[] emptyEntrySizes); | ||
16 | boolean hasUnmarkedActivation(VersionWithObjectiveValue version); | ||
17 | VisitResult getRandomAndMarkAsVisited(VersionWithObjectiveValue version, Random random); | ||
18 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/EquivalenceClassStore.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/EquivalenceClassStore.java new file mode 100644 index 00000000..bbe26fe5 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/EquivalenceClassStore.java | |||
@@ -0,0 +1,16 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
9 | import tools.refinery.store.statecoding.StateCoderResult; | ||
10 | |||
11 | public interface EquivalenceClassStore { | ||
12 | boolean submit(VersionWithObjectiveValue version, StateCoderResult stateCoderResult, int[] emptyActivations, boolean accept); | ||
13 | boolean hasUnresolvedSymmetry(); | ||
14 | void resolveOneSymmetry(); | ||
15 | int getNumberOfUnresolvedSymmetries(); | ||
16 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/ObjectivePriorityQueue.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/ObjectivePriorityQueue.java new file mode 100644 index 00000000..df72c343 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/ObjectivePriorityQueue.java | |||
@@ -0,0 +1,21 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
9 | import tools.refinery.store.map.Version; | ||
10 | |||
11 | import java.util.Comparator; | ||
12 | import java.util.Random; | ||
13 | |||
14 | public interface ObjectivePriorityQueue { | ||
15 | Comparator<VersionWithObjectiveValue> getComparator(); | ||
16 | void submit(VersionWithObjectiveValue versionWithObjectiveValue); | ||
17 | void remove(VersionWithObjectiveValue versionWithObjectiveValue); | ||
18 | int getSize(); | ||
19 | VersionWithObjectiveValue getBest(); | ||
20 | VersionWithObjectiveValue getRandom(Random random); | ||
21 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/SolutionStore.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/SolutionStore.java new file mode 100644 index 00000000..d1bfaa79 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/SolutionStore.java | |||
@@ -0,0 +1,17 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
9 | |||
10 | import java.util.List; | ||
11 | import java.util.concurrent.Future; | ||
12 | |||
13 | public interface SolutionStore { | ||
14 | boolean submit(VersionWithObjectiveValue version); | ||
15 | List<VersionWithObjectiveValue> getSolutions(); | ||
16 | boolean hasEnoughSolution(); | ||
17 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/AbstractEquivalenceClassStore.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/AbstractEquivalenceClassStore.java new file mode 100644 index 00000000..8466a0f3 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/AbstractEquivalenceClassStore.java | |||
@@ -0,0 +1,47 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace.internal; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
9 | import tools.refinery.store.dse.transition.statespace.EquivalenceClassStore; | ||
10 | import tools.refinery.store.statecoding.StateCoderResult; | ||
11 | import tools.refinery.store.statecoding.StateCoderStoreAdapter; | ||
12 | |||
13 | public abstract class AbstractEquivalenceClassStore implements EquivalenceClassStore { | ||
14 | protected final StateCoderStoreAdapter stateCoderStoreAdapter; | ||
15 | AbstractEquivalenceClassStore(StateCoderStoreAdapter stateCoderStoreAdapter) { | ||
16 | this.stateCoderStoreAdapter = stateCoderStoreAdapter; | ||
17 | } | ||
18 | |||
19 | protected int numberOfUnresolvedSymmetries = 0; | ||
20 | |||
21 | protected abstract void delegate(VersionWithObjectiveValue version, int[] emptyActivations, boolean accept); | ||
22 | protected abstract boolean tryToAdd(StateCoderResult stateCoderResult, VersionWithObjectiveValue newVersion, | ||
23 | int[] emptyActivations, boolean accept); | ||
24 | |||
25 | @Override | ||
26 | public synchronized boolean submit(VersionWithObjectiveValue version, StateCoderResult stateCoderResult, | ||
27 | int[] emptyActivations, boolean accept) { | ||
28 | boolean hasNewVersion = tryToAdd(stateCoderResult, version, emptyActivations, accept); | ||
29 | if (hasNewVersion) { | ||
30 | delegate(version, emptyActivations, accept); | ||
31 | return true; | ||
32 | } else { | ||
33 | numberOfUnresolvedSymmetries++; | ||
34 | return false; | ||
35 | } | ||
36 | } | ||
37 | |||
38 | @Override | ||
39 | public boolean hasUnresolvedSymmetry() { | ||
40 | return numberOfUnresolvedSymmetries > 0; | ||
41 | } | ||
42 | |||
43 | @Override | ||
44 | public int getNumberOfUnresolvedSymmetries() { | ||
45 | return numberOfUnresolvedSymmetries; | ||
46 | } | ||
47 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreBitVectorEntry.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreBitVectorEntry.java new file mode 100644 index 00000000..ba243d7d --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreBitVectorEntry.java | |||
@@ -0,0 +1,46 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace.internal; | ||
7 | |||
8 | public class ActivationStoreBitVectorEntry extends ActivationStoreEntry { | ||
9 | final int[] selected; | ||
10 | |||
11 | ActivationStoreBitVectorEntry(int numberOfActivations) { | ||
12 | super(numberOfActivations); | ||
13 | this.selected = new int[(numberOfActivations / Integer.SIZE) + 1]; | ||
14 | } | ||
15 | |||
16 | @Override | ||
17 | public int getNumberOfUnvisitedActivations() { | ||
18 | int visited = 0; | ||
19 | for (int i : selected) { | ||
20 | visited += Integer.bitCount(i); | ||
21 | } | ||
22 | return visited; | ||
23 | } | ||
24 | |||
25 | private static final int ELEMENT_POSITION = 5; // size of Integer.SIZE | ||
26 | private static final int ELEMENT_BITMASK = (1<<ELEMENT_POSITION)-1; | ||
27 | @Override | ||
28 | public int getAndAddActivationAfter(int index) { | ||
29 | int position = index; | ||
30 | do { | ||
31 | final int selectedElement = position >> ELEMENT_POSITION; | ||
32 | final int selectedBit = position & ELEMENT_BITMASK; | ||
33 | if((selected[selectedElement] & selectedBit) == 0) { | ||
34 | selected[selectedElement] |= selectedBit; | ||
35 | return position; | ||
36 | } else { | ||
37 | if(position < this.numberOfActivations) { | ||
38 | position++; | ||
39 | } else { | ||
40 | position = 0; | ||
41 | } | ||
42 | } | ||
43 | } while(position != index); | ||
44 | throw new IllegalArgumentException("There is are no unvisited activations!"); | ||
45 | } | ||
46 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreEntry.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreEntry.java new file mode 100644 index 00000000..f69b234c --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreEntry.java | |||
@@ -0,0 +1,32 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace.internal; | ||
7 | |||
8 | public abstract class ActivationStoreEntry { | ||
9 | protected final int numberOfActivations; | ||
10 | |||
11 | ActivationStoreEntry(int numberOfActivations) { | ||
12 | this.numberOfActivations = numberOfActivations; | ||
13 | } | ||
14 | |||
15 | public int getNumberOfVisitedActivations() { | ||
16 | return numberOfActivations; | ||
17 | } | ||
18 | |||
19 | public abstract int getNumberOfUnvisitedActivations(); | ||
20 | public abstract int getAndAddActivationAfter(int index); | ||
21 | |||
22 | // public abstract boolean contains(int activation) | ||
23 | // public abstract boolean add(int activation) | ||
24 | |||
25 | public static ActivationStoreEntry create(int size) { | ||
26 | if(size <= Integer.SIZE*6) { | ||
27 | return new ActivationStoreBitVectorEntry(size); | ||
28 | } else { | ||
29 | return new ActivationStoreListEntry(size); | ||
30 | } | ||
31 | } | ||
32 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreImpl.java new file mode 100644 index 00000000..3e59b21a --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreImpl.java | |||
@@ -0,0 +1,131 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace.internal; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
9 | import tools.refinery.store.dse.transition.statespace.ActivationStore; | ||
10 | |||
11 | import java.util.*; | ||
12 | import java.util.function.Consumer; | ||
13 | |||
14 | public class ActivationStoreImpl implements ActivationStore { | ||
15 | final int numberOfTransformations; | ||
16 | final Consumer<VersionWithObjectiveValue> actionWhenAllActivationVisited; | ||
17 | final Map<VersionWithObjectiveValue, List<ActivationStoreEntry>> versionToActivations; | ||
18 | |||
19 | public ActivationStoreImpl(final int numberOfTransformations, | ||
20 | Consumer<VersionWithObjectiveValue> actionWhenAllActivationVisited) { | ||
21 | this.numberOfTransformations = numberOfTransformations; | ||
22 | this.actionWhenAllActivationVisited = actionWhenAllActivationVisited; | ||
23 | versionToActivations = new HashMap<>(); | ||
24 | } | ||
25 | |||
26 | public synchronized VisitResult markNewAsVisited(VersionWithObjectiveValue to, int[] emptyEntrySizes) { | ||
27 | boolean[] successful = new boolean[]{false}; | ||
28 | var entries = versionToActivations.computeIfAbsent(to, x -> { | ||
29 | successful[0] = true; | ||
30 | List<ActivationStoreEntry> result = new ArrayList<>(emptyEntrySizes.length); | ||
31 | for(int emptyEntrySize : emptyEntrySizes) { | ||
32 | result.add(ActivationStoreListEntry.create(emptyEntrySize)); | ||
33 | } | ||
34 | return result; | ||
35 | }); | ||
36 | boolean hasMore = false; | ||
37 | for (var entry : entries) { | ||
38 | if (entry.getNumberOfUnvisitedActivations() > 0) { | ||
39 | hasMore = true; | ||
40 | break; | ||
41 | } | ||
42 | } | ||
43 | if(!hasMore) { | ||
44 | actionWhenAllActivationVisited.accept(to); | ||
45 | } | ||
46 | return new VisitResult(successful[0], hasMore, -1, -1); | ||
47 | } | ||
48 | |||
49 | public synchronized VisitResult visitActivation(VersionWithObjectiveValue from, int transformationIndex, int activationIndex) { | ||
50 | var entries = versionToActivations.get(from); | ||
51 | var entry = entries.get(transformationIndex); | ||
52 | final int unvisited = entry.getNumberOfUnvisitedActivations(); | ||
53 | |||
54 | final boolean successfulVisit = unvisited > 0; | ||
55 | final boolean hasMoreInActivation = unvisited > 1; | ||
56 | final boolean hasMore; | ||
57 | final int transformation; | ||
58 | final int activation; | ||
59 | |||
60 | if (successfulVisit) { | ||
61 | transformation = transformationIndex; | ||
62 | activation = entry.getAndAddActivationAfter(activationIndex); | ||
63 | |||
64 | } else { | ||
65 | transformation = -1; | ||
66 | activation = -1; | ||
67 | } | ||
68 | |||
69 | if(hasMoreInActivation) { | ||
70 | boolean hasMoreInOtherTransformation = false; | ||
71 | for (var e : entries) { | ||
72 | if (e != entry && e.getNumberOfVisitedActivations() > 0) { | ||
73 | hasMoreInOtherTransformation = true; | ||
74 | break; | ||
75 | } | ||
76 | } | ||
77 | hasMore = hasMoreInOtherTransformation; | ||
78 | } else { | ||
79 | hasMore = true; | ||
80 | } | ||
81 | |||
82 | if(!hasMore) { | ||
83 | actionWhenAllActivationVisited.accept(from); | ||
84 | } | ||
85 | |||
86 | return new VisitResult(false, hasMore, transformation, activation); | ||
87 | } | ||
88 | |||
89 | @Override | ||
90 | public synchronized boolean hasUnmarkedActivation(VersionWithObjectiveValue version) { | ||
91 | var entries = versionToActivations.get(version); | ||
92 | boolean hasMore = false; | ||
93 | for (var entry : entries) { | ||
94 | if (entry.getNumberOfUnvisitedActivations() > 0) { | ||
95 | hasMore = true; | ||
96 | break; | ||
97 | } | ||
98 | } | ||
99 | return hasMore; | ||
100 | } | ||
101 | |||
102 | @Override | ||
103 | public synchronized VisitResult getRandomAndMarkAsVisited(VersionWithObjectiveValue version, Random random) { | ||
104 | var entries = versionToActivations.get(version); | ||
105 | |||
106 | int sum1 = 0; | ||
107 | for (var entry : entries) { | ||
108 | sum1 += entry.getNumberOfUnvisitedActivations(); | ||
109 | } | ||
110 | |||
111 | int selected = random.nextInt(sum1); | ||
112 | int sum2 = 0; | ||
113 | int transformation = 0; | ||
114 | int activation = -1; | ||
115 | for (; transformation < entries.size(); transformation++) { | ||
116 | var entry = entries.get(transformation); | ||
117 | int unvisited = entry.getNumberOfUnvisitedActivations(); | ||
118 | if (selected < sum2 + unvisited) { | ||
119 | activation = sum2 + unvisited - selected; | ||
120 | break; | ||
121 | } else { | ||
122 | sum2 += unvisited; | ||
123 | } | ||
124 | } | ||
125 | if (activation == -1) { | ||
126 | throw new IllegalArgumentException("no unvisited"); | ||
127 | } | ||
128 | |||
129 | return this.visitActivation(version, transformation, activation); | ||
130 | } | ||
131 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreListEntry.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreListEntry.java new file mode 100644 index 00000000..e560f8c5 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreListEntry.java | |||
@@ -0,0 +1,95 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace.internal; | ||
7 | |||
8 | import org.eclipse.collections.api.factory.primitive.IntLists; | ||
9 | import org.eclipse.collections.api.list.primitive.MutableIntList; | ||
10 | |||
11 | public class ActivationStoreListEntry extends ActivationStoreEntry { | ||
12 | private final MutableIntList visitedActivations = IntLists.mutable.empty(); | ||
13 | |||
14 | ActivationStoreListEntry(int numberOfActivations) { | ||
15 | super(numberOfActivations); | ||
16 | } | ||
17 | |||
18 | @Override | ||
19 | public int getNumberOfUnvisitedActivations() { | ||
20 | return this.numberOfActivations - visitedActivations.size(); | ||
21 | } | ||
22 | |||
23 | @Override | ||
24 | public int getAndAddActivationAfter(int index) { | ||
25 | // If it is empty, just add it. | ||
26 | if(this.visitedActivations.isEmpty()) { | ||
27 | this.visitedActivations.add(index); | ||
28 | return index; | ||
29 | } | ||
30 | |||
31 | int position = getPosition(index); | ||
32 | |||
33 | // If the index is not in the position, one can insert it | ||
34 | if(this.visitedActivations.get(position) != index) { | ||
35 | this.visitedActivations.addAtIndex(position,index); | ||
36 | return index; | ||
37 | } | ||
38 | |||
39 | // Otherwise, get the next empty space between two elements | ||
40 | while(position + 2 < this.visitedActivations.size()) { | ||
41 | position++; | ||
42 | if(this.visitedActivations.get(position+1)-this.visitedActivations.get(position) > 1) { | ||
43 | this.visitedActivations.addAtIndex(position+1, this.visitedActivations.get(position+1)+1); | ||
44 | } | ||
45 | } | ||
46 | |||
47 | // Otherwise, try to add to the last space | ||
48 | int last = this.visitedActivations.get(this.visitedActivations.size()-1); | ||
49 | if(last<this.numberOfActivations) { | ||
50 | this.visitedActivations.add(last+1); | ||
51 | return last+1; | ||
52 | } | ||
53 | |||
54 | // Otherwise, try to put to the beginning | ||
55 | if(this.visitedActivations.get(0) > 0) { | ||
56 | this.visitedActivations.add(0); | ||
57 | return 0; | ||
58 | } | ||
59 | |||
60 | // Otherwise, get the next empty space between two elements | ||
61 | position = 0; | ||
62 | while(position + 2 < this.visitedActivations.size()) { | ||
63 | position++; | ||
64 | if(this.visitedActivations.get(position+1)-this.visitedActivations.get(position) > 1) { | ||
65 | this.visitedActivations.addAtIndex(position+1, this.visitedActivations.get(position+1)+1); | ||
66 | } | ||
67 | } | ||
68 | |||
69 | throw new IllegalArgumentException("There is are no unvisited activations!"); | ||
70 | } | ||
71 | |||
72 | /** | ||
73 | * Returns the position of an index in the {@code visitedActivations}. If the collection contains the index, in | ||
74 | * returns its position, otherwise, it returns the position where the index need to be put. | ||
75 | * | ||
76 | * @param index Index of an activation. | ||
77 | * @return The position of the index. | ||
78 | */ | ||
79 | private int getPosition(int index) { | ||
80 | int left = 0; | ||
81 | int right = this.visitedActivations.size() - 1; | ||
82 | while (left <= right) { | ||
83 | final int middle = (right - left) / 2 + left; | ||
84 | final int middleElement = visitedActivations.get(middle); | ||
85 | if(middleElement == index) { | ||
86 | return middle; | ||
87 | } else if(middleElement < index) { | ||
88 | left = middle +1; | ||
89 | } else{ | ||
90 | right = middle-1; | ||
91 | } | ||
92 | } | ||
93 | return right+1; | ||
94 | } | ||
95 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreWorker.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreWorker.java new file mode 100644 index 00000000..e05f5122 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ActivationStoreWorker.java | |||
@@ -0,0 +1,56 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace.internal; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.Transformation; | ||
9 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
10 | import tools.refinery.store.dse.transition.statespace.ActivationStore; | ||
11 | import tools.refinery.store.map.Version; | ||
12 | |||
13 | import java.util.List; | ||
14 | import java.util.Random; | ||
15 | |||
16 | public class ActivationStoreWorker { | ||
17 | final ActivationStore store; | ||
18 | final List<Transformation> transformations; | ||
19 | |||
20 | public ActivationStoreWorker(ActivationStore store, List<Transformation> transformations) { | ||
21 | this.store = store; | ||
22 | this.transformations = transformations; | ||
23 | } | ||
24 | |||
25 | public int[] calculateEmptyActivationSize() { | ||
26 | int[] result = new int[transformations.size()]; | ||
27 | for (int i = 0; i < result.length; i++) { | ||
28 | result[i] = transformations.get(i).getAllActivationsAsResultSet().size(); | ||
29 | } | ||
30 | return result; | ||
31 | } | ||
32 | |||
33 | |||
34 | public ActivationStore.VisitResult fireRandomActivation(VersionWithObjectiveValue thisVersion, Random random) { | ||
35 | var result = store.getRandomAndMarkAsVisited(thisVersion, random); | ||
36 | if(result.successfulVisit()) { | ||
37 | int selectedTransformation = result.transformation(); | ||
38 | int selectedActivation = result.activation(); | ||
39 | |||
40 | Transformation transformation = transformations.get(selectedTransformation); | ||
41 | var tuple = transformation.getActivation(selectedActivation); | ||
42 | |||
43 | boolean success = transformation.fireActivation(tuple); | ||
44 | if(success) { | ||
45 | return result; | ||
46 | } else { | ||
47 | return new ActivationStore.VisitResult( | ||
48 | false, | ||
49 | result.mayHaveMore(), | ||
50 | selectedActivation, | ||
51 | selectedActivation); | ||
52 | } | ||
53 | } | ||
54 | return result; | ||
55 | } | ||
56 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/CompleteEquivalenceClassStore.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/CompleteEquivalenceClassStore.java new file mode 100644 index 00000000..20a026b6 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/CompleteEquivalenceClassStore.java | |||
@@ -0,0 +1,104 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace.internal; | ||
7 | |||
8 | import org.eclipse.collections.api.factory.primitive.IntObjectMaps; | ||
9 | import org.eclipse.collections.api.map.primitive.MutableIntObjectMap; | ||
10 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
11 | import tools.refinery.store.dse.transition.statespace.EquivalenceClassStore; | ||
12 | import tools.refinery.store.statecoding.StateCoderResult; | ||
13 | import tools.refinery.store.statecoding.StateCoderStoreAdapter; | ||
14 | import tools.refinery.store.statecoding.StateEquivalenceChecker; | ||
15 | |||
16 | import java.util.ArrayList; | ||
17 | |||
18 | public abstract class CompleteEquivalenceClassStore extends AbstractEquivalenceClassStore implements EquivalenceClassStore { | ||
19 | |||
20 | static class SymmetryStoreArray extends ArrayList<VersionWithObjectiveValue> { | ||
21 | final int[] activationSizes; | ||
22 | final boolean accept; | ||
23 | |||
24 | SymmetryStoreArray(int[] activationSizes, boolean accept) { | ||
25 | super(); | ||
26 | this.activationSizes = activationSizes; | ||
27 | this.accept = accept; | ||
28 | } | ||
29 | } | ||
30 | |||
31 | private final MutableIntObjectMap<Object> modelCode2Versions = IntObjectMaps.mutable.empty(); | ||
32 | |||
33 | protected CompleteEquivalenceClassStore(StateCoderStoreAdapter stateCoderStoreAdapter) { | ||
34 | super(stateCoderStoreAdapter); | ||
35 | } | ||
36 | |||
37 | @Override | ||
38 | protected boolean tryToAdd(StateCoderResult stateCoderResult, VersionWithObjectiveValue newVersion, | ||
39 | int[] emptyActivations, boolean accept) { | ||
40 | int modelCode = stateCoderResult.modelCode(); | ||
41 | Object old = modelCode2Versions.updateValue( | ||
42 | modelCode, | ||
43 | () -> newVersion, | ||
44 | x -> { | ||
45 | if (x instanceof SymmetryStoreArray array) { | ||
46 | if(array.accept != accept || array.activationSizes != emptyActivations) { | ||
47 | this.delegate(newVersion,emptyActivations,accept); | ||
48 | return x; | ||
49 | } | ||
50 | array.add(newVersion); | ||
51 | return array; | ||
52 | } else { | ||
53 | SymmetryStoreArray result = new SymmetryStoreArray(emptyActivations, accept); | ||
54 | result.add((VersionWithObjectiveValue) x); | ||
55 | result.add(newVersion); | ||
56 | return result; | ||
57 | } | ||
58 | }); | ||
59 | return old == null; | ||
60 | } | ||
61 | |||
62 | @Override | ||
63 | public void resolveOneSymmetry() { | ||
64 | var unresolvedSimilarity = getOneUnresolvedSymmetry(); | ||
65 | if (unresolvedSimilarity == null) { | ||
66 | return; | ||
67 | } | ||
68 | var outcome = this.stateCoderStoreAdapter.checkEquivalence(unresolvedSimilarity.get(0).version(), | ||
69 | unresolvedSimilarity.get(1).version()); | ||
70 | if (outcome != StateEquivalenceChecker.EquivalenceResult.ISOMORPHIC) { | ||
71 | delegate(unresolvedSimilarity.get(1), unresolvedSimilarity.activationSizes, unresolvedSimilarity.accept); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | //record UnresolvedSymmetryResult | ||
76 | |||
77 | private synchronized SymmetryStoreArray getOneUnresolvedSymmetry() { | ||
78 | if (numberOfUnresolvedSymmetries <= 0) { | ||
79 | return null; | ||
80 | } | ||
81 | |||
82 | for (var entry : modelCode2Versions.keyValuesView()) { | ||
83 | int hash = entry.getOne(); | ||
84 | var value = entry.getTwo(); | ||
85 | if (value instanceof SymmetryStoreArray array) { | ||
86 | int size = array.size(); | ||
87 | var representative = array.get(0); | ||
88 | var similar = array.get(size - 1); | ||
89 | array.remove(size - 1); | ||
90 | |||
91 | if (size <= 2) { | ||
92 | modelCode2Versions.put(hash, representative); | ||
93 | } | ||
94 | |||
95 | var result = new SymmetryStoreArray(array.activationSizes, array.accept); | ||
96 | result.add(representative); | ||
97 | result.add(similar); | ||
98 | return result; | ||
99 | } | ||
100 | } | ||
101 | |||
102 | return null; | ||
103 | } | ||
104 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/FastEquivalenceClassStore.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/FastEquivalenceClassStore.java new file mode 100644 index 00000000..6ee8e196 --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/FastEquivalenceClassStore.java | |||
@@ -0,0 +1,32 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace.internal; | ||
7 | |||
8 | import org.eclipse.collections.api.factory.primitive.IntSets; | ||
9 | import org.eclipse.collections.api.set.primitive.MutableIntSet; | ||
10 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
11 | import tools.refinery.store.dse.transition.statespace.EquivalenceClassStore; | ||
12 | import tools.refinery.store.statecoding.StateCoderResult; | ||
13 | import tools.refinery.store.statecoding.StateCoderStoreAdapter; | ||
14 | |||
15 | public abstract class FastEquivalenceClassStore extends AbstractEquivalenceClassStore implements EquivalenceClassStore { | ||
16 | |||
17 | private final MutableIntSet codes = IntSets.mutable.empty(); | ||
18 | |||
19 | public FastEquivalenceClassStore(StateCoderStoreAdapter stateCoderStoreAdapter) { | ||
20 | super(stateCoderStoreAdapter); | ||
21 | } | ||
22 | |||
23 | @Override | ||
24 | protected boolean tryToAdd(StateCoderResult stateCoderResult, VersionWithObjectiveValue newVersion, int[] emptyActivations, boolean accept) { | ||
25 | return this.codes.add(stateCoderResult.modelCode()); | ||
26 | } | ||
27 | |||
28 | @Override | ||
29 | public void resolveOneSymmetry() { | ||
30 | throw new IllegalArgumentException("This equivalence storage is not prepared to resolve symmetries!"); | ||
31 | } | ||
32 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ObjectivePriorityQueueImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ObjectivePriorityQueueImpl.java new file mode 100644 index 00000000..249b22da --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/ObjectivePriorityQueueImpl.java | |||
@@ -0,0 +1,74 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace.internal; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.ObjectiveValues; | ||
9 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
10 | import tools.refinery.store.dse.transition.objectives.Objective; | ||
11 | import tools.refinery.store.dse.transition.statespace.ObjectivePriorityQueue; | ||
12 | import tools.refinery.store.map.Version; | ||
13 | |||
14 | import java.util.Comparator; | ||
15 | import java.util.List; | ||
16 | import java.util.PriorityQueue; | ||
17 | import java.util.Random; | ||
18 | |||
19 | public class ObjectivePriorityQueueImpl implements ObjectivePriorityQueue { | ||
20 | public static final Comparator<VersionWithObjectiveValue> c1 = (o1, o2) -> Double.compare( | ||
21 | ((ObjectiveValues.ObjectiveValue1) o1.objectiveValue()).value0(), | ||
22 | ((ObjectiveValues.ObjectiveValue1) o2.objectiveValue()).value0()); | ||
23 | // TODO: support multi objective! | ||
24 | final PriorityQueue<VersionWithObjectiveValue> priorityQueue; | ||
25 | |||
26 | public ObjectivePriorityQueueImpl(List<Objective> objectives) { | ||
27 | |||
28 | if(objectives.size() == 1) { | ||
29 | this.priorityQueue = new PriorityQueue<>(c1); | ||
30 | } else { | ||
31 | throw new UnsupportedOperationException("Only single objective comparator is implemented currently!"); | ||
32 | } | ||
33 | } | ||
34 | @Override | ||
35 | public Comparator<VersionWithObjectiveValue> getComparator() { | ||
36 | return c1; | ||
37 | } | ||
38 | |||
39 | @Override | ||
40 | public void submit(VersionWithObjectiveValue versionWithObjectiveValue) { | ||
41 | priorityQueue.add(versionWithObjectiveValue); | ||
42 | } | ||
43 | |||
44 | @Override | ||
45 | public void remove(VersionWithObjectiveValue versionWithObjectiveValue) { | ||
46 | priorityQueue.remove(versionWithObjectiveValue); | ||
47 | } | ||
48 | |||
49 | @Override | ||
50 | public int getSize() { | ||
51 | return priorityQueue.size(); | ||
52 | } | ||
53 | |||
54 | @Override | ||
55 | public VersionWithObjectiveValue getBest() { | ||
56 | var best = priorityQueue.peek(); | ||
57 | if (best != null) { | ||
58 | return best; | ||
59 | } else { | ||
60 | throw new IllegalArgumentException("The objective store is empty!"); | ||
61 | } | ||
62 | } | ||
63 | |||
64 | @Override | ||
65 | public VersionWithObjectiveValue getRandom(Random random) { | ||
66 | int randomPosition = random.nextInt(getSize()); | ||
67 | for (VersionWithObjectiveValue entry : this.priorityQueue) { | ||
68 | if (randomPosition-- == 0) { | ||
69 | return entry; | ||
70 | } | ||
71 | } | ||
72 | throw new IllegalStateException("The priority queue is inconsistent!"); | ||
73 | } | ||
74 | } | ||
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/SolutionStoreImpl.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/SolutionStoreImpl.java new file mode 100644 index 00000000..cc48864f --- /dev/null +++ b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/transition/statespace/internal/SolutionStoreImpl.java | |||
@@ -0,0 +1,53 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.dse.transition.statespace.internal; | ||
7 | |||
8 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | ||
9 | import tools.refinery.store.dse.transition.statespace.SolutionStore; | ||
10 | |||
11 | import java.util.ArrayList; | ||
12 | import java.util.List; | ||
13 | import java.util.SortedSet; | ||
14 | import java.util.TreeSet; | ||
15 | |||
16 | public class SolutionStoreImpl implements SolutionStore { | ||
17 | final int maxNumberSolutions; | ||
18 | public static final int UNLIMITED = -1; | ||
19 | final SortedSet<VersionWithObjectiveValue> solutions; | ||
20 | |||
21 | public SolutionStoreImpl(int maxNumberSolutions) { | ||
22 | this.maxNumberSolutions = maxNumberSolutions; | ||
23 | solutions = new TreeSet<>(ObjectivePriorityQueueImpl.c1); | ||
24 | } | ||
25 | |||
26 | |||
27 | @Override | ||
28 | public synchronized boolean submit(VersionWithObjectiveValue version) { | ||
29 | boolean removeLast = hasEnoughSolution(); | ||
30 | solutions.add(version); | ||
31 | if(removeLast) { | ||
32 | var last = solutions.last(); | ||
33 | solutions.remove(last); | ||
34 | return last != version; | ||
35 | } else { | ||
36 | return true; | ||
37 | } | ||
38 | } | ||
39 | |||
40 | @Override | ||
41 | public List<VersionWithObjectiveValue> getSolutions() { | ||
42 | return new ArrayList<>(solutions); | ||
43 | } | ||
44 | |||
45 | @Override | ||
46 | public boolean hasEnoughSolution() { | ||
47 | if (maxNumberSolutions == UNLIMITED) { | ||
48 | return false; | ||
49 | } else { | ||
50 | return solutions.size() >= maxNumberSolutions; | ||
51 | } | ||
52 | } | ||
53 | } | ||