diff options
Diffstat (limited to 'subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/DepthFirstStrategy.java')
-rw-r--r-- | subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/DepthFirstStrategy.java | 92 |
1 files changed, 0 insertions, 92 deletions
diff --git a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/DepthFirstStrategy.java b/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/DepthFirstStrategy.java deleted file mode 100644 index 0a0caa7e..00000000 --- a/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/DepthFirstStrategy.java +++ /dev/null | |||
@@ -1,92 +0,0 @@ | |||
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.strategy; | ||
7 | |||
8 | import tools.refinery.store.dse.DesignSpaceExplorationAdapter; | ||
9 | import tools.refinery.store.dse.Strategy; | ||
10 | import tools.refinery.store.dse.objectives.Fitness; | ||
11 | |||
12 | public class DepthFirstStrategy implements Strategy { | ||
13 | |||
14 | private DesignSpaceExplorationAdapter dseAdapter; | ||
15 | |||
16 | private int maxDepth = Integer.MAX_VALUE; | ||
17 | private int maxSolutions = Integer.MAX_VALUE; | ||
18 | private boolean backtrackFromSolution = true; | ||
19 | |||
20 | public DepthFirstStrategy withDepthLimit(int maxDepth) { | ||
21 | if (maxDepth >= 0) { | ||
22 | this.maxDepth = maxDepth; | ||
23 | } | ||
24 | return this; | ||
25 | } | ||
26 | |||
27 | public DepthFirstStrategy withSolutionLimit(int maxSolutions) { | ||
28 | if (maxSolutions >= 0) { | ||
29 | this.maxSolutions = maxSolutions; | ||
30 | } | ||
31 | return this; | ||
32 | } | ||
33 | |||
34 | public DepthFirstStrategy continueIfHardObjectivesFulfilled() { | ||
35 | backtrackFromSolution = false; | ||
36 | return this; | ||
37 | } | ||
38 | |||
39 | @Override | ||
40 | public void initialize(DesignSpaceExplorationAdapter designSpaceExplorationAdapter) { | ||
41 | this.dseAdapter = designSpaceExplorationAdapter; | ||
42 | } | ||
43 | |||
44 | @Override | ||
45 | public void explore() { | ||
46 | if (maxSolutions == 0) { | ||
47 | return; | ||
48 | } | ||
49 | while (dseAdapter.getSolutions().size() < maxSolutions) { | ||
50 | if (!checkAndHandleGlobalConstraints()) { | ||
51 | return; | ||
52 | } | ||
53 | |||
54 | Fitness fitness = dseAdapter.getFitness(); | ||
55 | if (fitness.isSatisfiesHardObjectives()) { | ||
56 | dseAdapter.newSolution(); | ||
57 | if (backtrackFromSolution && !dseAdapter.backtrack()) { | ||
58 | return; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | if (!checkAndHandleDepth()) { | ||
63 | return; | ||
64 | } | ||
65 | |||
66 | if (!backtrackToLastUntraversed()) { | ||
67 | return; | ||
68 | } | ||
69 | |||
70 | if (!dseAdapter.fireRandomActivation()) { | ||
71 | return; | ||
72 | } | ||
73 | } | ||
74 | } | ||
75 | |||
76 | private boolean checkAndHandleGlobalConstraints() { | ||
77 | return dseAdapter.checkGlobalConstraints() || dseAdapter.backtrack(); | ||
78 | } | ||
79 | |||
80 | private boolean checkAndHandleDepth() { | ||
81 | return dseAdapter.getDepth() < maxDepth || dseAdapter.backtrack(); | ||
82 | } | ||
83 | |||
84 | private boolean backtrackToLastUntraversed() { | ||
85 | while (dseAdapter.getUntraversedActivations().isEmpty()) { | ||
86 | if (!dseAdapter.backtrack()) { | ||
87 | return false; | ||
88 | } | ||
89 | } | ||
90 | return true; | ||
91 | } | ||
92 | } | ||