diff options
author | 2023-09-07 17:34:37 +0200 | |
---|---|---|
committer | 2023-09-07 17:52:34 +0200 | |
commit | 741ff77e0e841cdf3fd893e1fb3d1ccccc6a83e4 (patch) | |
tree | 5670015d5e4e02ab1453fad2cf8888ac9d1a9a2d /subprojects/store-dse/src/main | |
parent | Merge remote-tracking branch 'oszkar/datastructure' into datastructure (diff) | |
download | refinery-741ff77e0e841cdf3fd893e1fb3d1ccccc6a83e4.tar.gz refinery-741ff77e0e841cdf3fd893e1fb3d1ccccc6a83e4.tar.zst refinery-741ff77e0e841cdf3fd893e1fb3d1ccccc6a83e4.zip |
fix issues with DSE framework
Diffstat (limited to 'subprojects/store-dse/src/main')
8 files changed, 69 insertions, 39 deletions
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 index 9a03b0b8..a2b6268f 100644 --- 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 | |||
@@ -36,7 +36,6 @@ public class BestFirstExplorer extends BestFirstWorker { | |||
36 | if (lastVisited == null) { | 36 | if (lastVisited == null) { |
37 | lastVisited = this.restoreToBest(); | 37 | lastVisited = this.restoreToBest(); |
38 | if(lastVisited == null) { | 38 | if(lastVisited == null) { |
39 | visualizerAdapter.visualize(); | ||
40 | return; | 39 | return; |
41 | } | 40 | } |
42 | } | 41 | } |
@@ -162,7 +161,6 @@ public class BestFirstExplorer extends BestFirstWorker { | |||
162 | */ | 161 | */ |
163 | } | 162 | } |
164 | // Interrupted. | 163 | // Interrupted. |
165 | visualizerAdapter.visualize(); | ||
166 | 164 | ||
167 | } | 165 | } |
168 | } | 166 | } |
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 index 3795daa4..0b9aae9c 100644 --- 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 | |||
@@ -18,14 +18,20 @@ import tools.refinery.store.dse.transition.statespace.internal.ObjectivePriority | |||
18 | import tools.refinery.store.dse.transition.statespace.internal.SolutionStoreImpl; | 18 | import tools.refinery.store.dse.transition.statespace.internal.SolutionStoreImpl; |
19 | import tools.refinery.store.map.Version; | 19 | import tools.refinery.store.map.Version; |
20 | import tools.refinery.store.model.ModelStore; | 20 | import tools.refinery.store.model.ModelStore; |
21 | import tools.refinery.store.statecoding.StateCoderResult; | ||
21 | import tools.refinery.store.statecoding.StateCoderStoreAdapter; | 22 | import tools.refinery.store.statecoding.StateCoderStoreAdapter; |
23 | import tools.refinery.visualization.ModelVisualizerStoreAdapter; | ||
24 | import tools.refinery.visualization.statespace.VisualizationStore; | ||
25 | import tools.refinery.visualization.statespace.internal.VisualizationStoreImpl; | ||
22 | 26 | ||
23 | public class BestFirstStoreManager { | 27 | public class BestFirstStoreManager { |
28 | |||
24 | ModelStore modelStore; | 29 | ModelStore modelStore; |
25 | ObjectivePriorityQueue objectiveStore; | 30 | ObjectivePriorityQueue objectiveStore; |
26 | ActivationStore activationStore; | 31 | ActivationStore activationStore; |
27 | SolutionStore solutionStore; | 32 | SolutionStore solutionStore; |
28 | EquivalenceClassStore equivalenceClassStore; | 33 | EquivalenceClassStore equivalenceClassStore; |
34 | VisualizationStore visualizationStore; | ||
29 | 35 | ||
30 | public BestFirstStoreManager(ModelStore modelStore) { | 36 | public BestFirstStoreManager(ModelStore modelStore) { |
31 | this.modelStore = modelStore; | 37 | this.modelStore = modelStore; |
@@ -35,17 +41,17 @@ public class BestFirstStoreManager { | |||
35 | objectiveStore = new ObjectivePriorityQueueImpl(storeAdapter.getObjectives()); | 41 | objectiveStore = new ObjectivePriorityQueueImpl(storeAdapter.getObjectives()); |
36 | Procedure<VersionWithObjectiveValue> whenAllActivationsVisited = x -> objectiveStore.remove(x); | 42 | Procedure<VersionWithObjectiveValue> whenAllActivationsVisited = x -> objectiveStore.remove(x); |
37 | activationStore = new ActivationStoreImpl(storeAdapter.getTransformations().size(), whenAllActivationsVisited); | 43 | activationStore = new ActivationStoreImpl(storeAdapter.getTransformations().size(), whenAllActivationsVisited); |
38 | solutionStore = new SolutionStoreImpl(10); | 44 | solutionStore = new SolutionStoreImpl(50); |
39 | equivalenceClassStore = new FastEquivalenceClassStore(modelStore.getAdapter(StateCoderStoreAdapter.class)) { | 45 | equivalenceClassStore = new FastEquivalenceClassStore(modelStore.getAdapter(StateCoderStoreAdapter.class)) { |
40 | @Override | 46 | @Override |
41 | protected void delegate(VersionWithObjectiveValue version, int[] emptyActivations, boolean accept) { | 47 | protected void delegate(VersionWithObjectiveValue version, int[] emptyActivations, boolean accept) { |
42 | objectiveStore.submit(version); | 48 | throw new UnsupportedOperationException("This equivalence storage is not prepared to resolve symmetries!"); |
43 | activationStore.markNewAsVisited(version, emptyActivations); | ||
44 | if(accept) { | ||
45 | solutionStore.submit(version); | ||
46 | } | ||
47 | } | 49 | } |
48 | }; | 50 | }; |
51 | visualizationStore = new VisualizationStoreImpl(); | ||
52 | } | ||
53 | public ModelStore getModelStore() { | ||
54 | return modelStore; | ||
49 | } | 55 | } |
50 | 56 | ||
51 | ObjectivePriorityQueue getObjectiveStore() { | 57 | ObjectivePriorityQueue getObjectiveStore() { |
@@ -64,6 +70,10 @@ public class BestFirstStoreManager { | |||
64 | return equivalenceClassStore; | 70 | return equivalenceClassStore; |
65 | } | 71 | } |
66 | 72 | ||
73 | public VisualizationStore getVisualizationStore() { | ||
74 | return visualizationStore; | ||
75 | } | ||
76 | |||
67 | public void startExploration(Version initial) { | 77 | public void startExploration(Version initial) { |
68 | BestFirstExplorer bestFirstExplorer = new BestFirstExplorer(this, modelStore.createModelForState(initial), 1); | 78 | BestFirstExplorer bestFirstExplorer = new BestFirstExplorer(this, modelStore.createModelForState(initial), 1); |
69 | bestFirstExplorer.explore(); | 79 | bestFirstExplorer.explore(); |
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 index 0f1702ea..f1bec14f 100644 --- 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 | |||
@@ -13,7 +13,7 @@ import tools.refinery.store.map.Version; | |||
13 | import tools.refinery.store.model.Model; | 13 | import tools.refinery.store.model.Model; |
14 | import tools.refinery.store.query.ModelQueryAdapter; | 14 | import tools.refinery.store.query.ModelQueryAdapter; |
15 | import tools.refinery.store.statecoding.StateCoderAdapter; | 15 | import tools.refinery.store.statecoding.StateCoderAdapter; |
16 | import tools.refinery.visualization.ModelVisualizerAdapter; | 16 | import tools.refinery.visualization.statespace.VisualizationStore; |
17 | 17 | ||
18 | import java.util.Random; | 18 | import java.util.Random; |
19 | 19 | ||
@@ -24,7 +24,7 @@ public class BestFirstWorker { | |||
24 | final StateCoderAdapter stateCoderAdapter; | 24 | final StateCoderAdapter stateCoderAdapter; |
25 | final DesignSpaceExplorationAdapter explorationAdapter; | 25 | final DesignSpaceExplorationAdapter explorationAdapter; |
26 | final ModelQueryAdapter queryAdapter; | 26 | final ModelQueryAdapter queryAdapter; |
27 | final ModelVisualizerAdapter visualizerAdapter; | 27 | final VisualizationStore visualizationStore; |
28 | final boolean isVisualizationEnabled; | 28 | final boolean isVisualizationEnabled; |
29 | 29 | ||
30 | public BestFirstWorker(BestFirstStoreManager storeManager, Model model) { | 30 | public BestFirstWorker(BestFirstStoreManager storeManager, Model model) { |
@@ -36,9 +36,8 @@ public class BestFirstWorker { | |||
36 | queryAdapter = model.getAdapter(ModelQueryAdapter.class); | 36 | queryAdapter = model.getAdapter(ModelQueryAdapter.class); |
37 | activationStoreWorker = new ActivationStoreWorker(storeManager.getActivationStore(), | 37 | activationStoreWorker = new ActivationStoreWorker(storeManager.getActivationStore(), |
38 | explorationAdapter.getTransformations()); | 38 | explorationAdapter.getTransformations()); |
39 | visualizerAdapter = model.getAdapter(ModelVisualizerAdapter.class); | 39 | visualizationStore = storeManager.getVisualizationStore(); |
40 | System.out.println("visualizerAdapter = " + visualizerAdapter); | 40 | isVisualizationEnabled = visualizationStore != null; |
41 | isVisualizationEnabled = visualizerAdapter != null; | ||
42 | } | 41 | } |
43 | 42 | ||
44 | private VersionWithObjectiveValue last = null; | 43 | private VersionWithObjectiveValue last = null; |
@@ -47,28 +46,41 @@ public class BestFirstWorker { | |||
47 | 46 | ||
48 | public SubmitResult submit() { | 47 | public SubmitResult submit() { |
49 | if (explorationAdapter.checkExclude()) { | 48 | if (explorationAdapter.checkExclude()) { |
50 | last = null; | ||
51 | return new SubmitResult(false, false, null, null); | 49 | return new SubmitResult(false, false, null, null); |
52 | } | 50 | } |
53 | 51 | ||
54 | Version version = model.commit(); | ||
55 | queryAdapter.flushChanges(); | ||
56 | ObjectiveValue objectiveValue = explorationAdapter.getObjectiveValue(); | ||
57 | last = new VersionWithObjectiveValue(version, objectiveValue); | ||
58 | var code = stateCoderAdapter.calculateStateCode(); | 52 | var code = stateCoderAdapter.calculateStateCode(); |
59 | var accepted = explorationAdapter.checkAccept(); | 53 | |
60 | boolean isNew = storeManager.getEquivalenceClassStore().submit(last, code, | 54 | boolean isNew = storeManager.getEquivalenceClassStore().submit(code); |
61 | activationStoreWorker.calculateEmptyActivationSize(), accepted); | 55 | if (isNew) { |
62 | return new SubmitResult(isNew, accepted, objectiveValue, isNew ? last : null); | 56 | Version version = model.commit(); |
57 | ObjectiveValue objectiveValue = explorationAdapter.getObjectiveValue(); | ||
58 | var versionWithObjectiveValue = new VersionWithObjectiveValue(version, objectiveValue); | ||
59 | last = versionWithObjectiveValue; | ||
60 | var accepted = explorationAdapter.checkAccept(); | ||
61 | |||
62 | storeManager.getObjectiveStore().submit(versionWithObjectiveValue); | ||
63 | storeManager.getActivationStore().markNewAsVisited(versionWithObjectiveValue, activationStoreWorker.calculateEmptyActivationSize()); | ||
64 | if(accepted) { | ||
65 | storeManager.solutionStore.submit(versionWithObjectiveValue); | ||
66 | } | ||
67 | |||
68 | if (isVisualizationEnabled) { | ||
69 | visualizationStore.addState(last.version(), last.objectiveValue().toString()); | ||
70 | if (accepted) { | ||
71 | visualizationStore.addSolution(last.version()); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | return new SubmitResult(true, accepted, objectiveValue, last); | ||
76 | } | ||
77 | |||
78 | return new SubmitResult(false, false, null, null); | ||
63 | } | 79 | } |
64 | 80 | ||
65 | public void restoreToLast() { | 81 | public void restoreToLast() { |
66 | if (explorationAdapter.getModel().hasUncommittedChanges()) { | 82 | if (explorationAdapter.getModel().hasUncommittedChanges()) { |
67 | var oldVersion = model.getState(); | ||
68 | explorationAdapter.getModel().restore(last.version()); | 83 | explorationAdapter.getModel().restore(last.version()); |
69 | if (isVisualizationEnabled) { | ||
70 | visualizerAdapter.addTransition(oldVersion, last.version(), ""); | ||
71 | } | ||
72 | } | 84 | } |
73 | } | 85 | } |
74 | 86 | ||
@@ -78,7 +90,7 @@ public class BestFirstWorker { | |||
78 | var oldVersion = model.getState(); | 90 | var oldVersion = model.getState(); |
79 | this.model.restore(bestVersion.version()); | 91 | this.model.restore(bestVersion.version()); |
80 | if (isVisualizationEnabled) { | 92 | if (isVisualizationEnabled) { |
81 | visualizerAdapter.addTransition(oldVersion, last.version(), ""); | 93 | visualizationStore.addTransition(oldVersion, last.version(), ""); |
82 | } | 94 | } |
83 | } | 95 | } |
84 | return bestVersion; | 96 | return bestVersion; |
@@ -105,7 +117,7 @@ public class BestFirstWorker { | |||
105 | } | 117 | } |
106 | } | 118 | } |
107 | 119 | ||
108 | record RandomVisitResult(SubmitResult submitResult, boolean shouldRetry) { | 120 | public record RandomVisitResult(SubmitResult submitResult, boolean shouldRetry) { |
109 | } | 121 | } |
110 | 122 | ||
111 | public RandomVisitResult visitRandomUnvisited(Random random) { | 123 | public RandomVisitResult visitRandomUnvisited(Random random) { |
@@ -119,15 +131,9 @@ public class BestFirstWorker { | |||
119 | oldVersion = last.version(); | 131 | oldVersion = last.version(); |
120 | } | 132 | } |
121 | var submitResult = submit(); | 133 | var submitResult = submit(); |
122 | if (isVisualizationEnabled) { | 134 | if (isVisualizationEnabled && submitResult.newVersion() != null) { |
123 | 135 | var newVersion = submitResult.newVersion().version(); | |
124 | Version newVersion = null; | 136 | visualizationStore.addTransition(oldVersion, newVersion, ""); |
125 | if (submitResult.newVersion() != null) { | ||
126 | newVersion = submitResult.newVersion().version(); | ||
127 | visualizerAdapter.addState(newVersion, submitResult.newVersion().objectiveValue().toString()); | ||
128 | visualizerAdapter.addSolution(newVersion); | ||
129 | } | ||
130 | visualizerAdapter.addTransition(oldVersion, newVersion, ""); | ||
131 | } | 137 | } |
132 | return new RandomVisitResult(submitResult, visitResult.mayHaveMore()); | 138 | return new RandomVisitResult(submitResult, visitResult.mayHaveMore()); |
133 | } else { | 139 | } else { |
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 index bbe26fe5..28d1488b 100644 --- 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 | |||
@@ -10,6 +10,7 @@ import tools.refinery.store.statecoding.StateCoderResult; | |||
10 | 10 | ||
11 | public interface EquivalenceClassStore { | 11 | public interface EquivalenceClassStore { |
12 | boolean submit(VersionWithObjectiveValue version, StateCoderResult stateCoderResult, int[] emptyActivations, boolean accept); | 12 | boolean submit(VersionWithObjectiveValue version, StateCoderResult stateCoderResult, int[] emptyActivations, boolean accept); |
13 | boolean submit(StateCoderResult stateCoderResult); | ||
13 | boolean hasUnresolvedSymmetry(); | 14 | boolean hasUnresolvedSymmetry(); |
14 | void resolveOneSymmetry(); | 15 | void resolveOneSymmetry(); |
15 | int getNumberOfUnresolvedSymmetries(); | 16 | int getNumberOfUnresolvedSymmetries(); |
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 index 8466a0f3..b5087e86 100644 --- 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 | |||
@@ -22,6 +22,13 @@ public abstract class AbstractEquivalenceClassStore implements EquivalenceClassS | |||
22 | protected abstract boolean tryToAdd(StateCoderResult stateCoderResult, VersionWithObjectiveValue newVersion, | 22 | protected abstract boolean tryToAdd(StateCoderResult stateCoderResult, VersionWithObjectiveValue newVersion, |
23 | int[] emptyActivations, boolean accept); | 23 | int[] emptyActivations, boolean accept); |
24 | 24 | ||
25 | public abstract boolean tryToAdd(StateCoderResult stateCoderResult); | ||
26 | |||
27 | @Override | ||
28 | public boolean submit(StateCoderResult stateCoderResult) { | ||
29 | return tryToAdd(stateCoderResult); | ||
30 | } | ||
31 | |||
25 | @Override | 32 | @Override |
26 | public synchronized boolean submit(VersionWithObjectiveValue version, StateCoderResult stateCoderResult, | 33 | public synchronized boolean submit(VersionWithObjectiveValue version, StateCoderResult stateCoderResult, |
27 | int[] emptyActivations, boolean accept) { | 34 | int[] emptyActivations, boolean accept) { |
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 index fbd5b17f..9186741f 100644 --- 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 | |||
@@ -69,7 +69,7 @@ public class ActivationStoreImpl implements ActivationStore { | |||
69 | if(!hasMoreInActivation) { | 69 | if(!hasMoreInActivation) { |
70 | boolean hasMoreInOtherTransformation = false; | 70 | boolean hasMoreInOtherTransformation = false; |
71 | for (var e : entries) { | 71 | for (var e : entries) { |
72 | if (e != entry && e.getNumberOfVisitedActivations() > 0) { | 72 | if (e != entry && e.getNumberOfUnvisitedActivations() > 0) { |
73 | hasMoreInOtherTransformation = true; | 73 | hasMoreInOtherTransformation = true; |
74 | break; | 74 | break; |
75 | } | 75 | } |
@@ -108,6 +108,11 @@ public class ActivationStoreImpl implements ActivationStore { | |||
108 | sum1 += entry.getNumberOfUnvisitedActivations(); | 108 | sum1 += entry.getNumberOfUnvisitedActivations(); |
109 | } | 109 | } |
110 | 110 | ||
111 | if(sum1 == 0) { | ||
112 | this.actionWhenAllActivationVisited.accept(version); | ||
113 | return new VisitResult(false, false, -1, -1); | ||
114 | } | ||
115 | |||
111 | int selected = random.nextInt(sum1); | 116 | int selected = random.nextInt(sum1); |
112 | int sum2 = 0; | 117 | int sum2 = 0; |
113 | int transformation = 0; | 118 | int transformation = 0; |
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 index e05f5122..881b133c 100644 --- 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 | |||
@@ -8,7 +8,6 @@ package tools.refinery.store.dse.transition.statespace.internal; | |||
8 | import tools.refinery.store.dse.transition.Transformation; | 8 | import tools.refinery.store.dse.transition.Transformation; |
9 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; | 9 | import tools.refinery.store.dse.transition.VersionWithObjectiveValue; |
10 | import tools.refinery.store.dse.transition.statespace.ActivationStore; | 10 | import tools.refinery.store.dse.transition.statespace.ActivationStore; |
11 | import tools.refinery.store.map.Version; | ||
12 | 11 | ||
13 | import java.util.List; | 12 | import java.util.List; |
14 | import java.util.Random; | 13 | import java.util.Random; |
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 index faeedba5..6eba87d4 100644 --- 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 | |||
@@ -26,6 +26,10 @@ public abstract class FastEquivalenceClassStore extends AbstractEquivalenceClass | |||
26 | return this.codes.add(stateCoderResult.modelCode()); | 26 | return this.codes.add(stateCoderResult.modelCode()); |
27 | } | 27 | } |
28 | 28 | ||
29 | public synchronized boolean tryToAdd(StateCoderResult stateCoderResult) { | ||
30 | return this.codes.add(stateCoderResult.modelCode()); | ||
31 | } | ||
32 | |||
29 | @Override | 33 | @Override |
30 | public void resolveOneSymmetry() { | 34 | public void resolveOneSymmetry() { |
31 | throw new IllegalArgumentException("This equivalence storage is not prepared to resolve symmetries!"); | 35 | throw new IllegalArgumentException("This equivalence storage is not prepared to resolve symmetries!"); |