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 | 55 |
1 files changed, 17 insertions, 38 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 index 425e1e01..5f7f61b8 100644 --- 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 | |||
@@ -1,12 +1,8 @@ | |||
1 | /******************************************************************************* | 1 | /* |
2 | * Copyright (c) 2010-2016, Andras Szabolcs Nagy, Zoltan Ujhelyi and Daniel Varro | 2 | * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/> |
3 | * Copyright (c) 2023 The Refinery Authors <https://refinery.tools/> | ||
4 | * This program and the accompanying materials are made available under the | ||
5 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
6 | * http://www.eclipse.org/legal/epl-v20.html. | ||
7 | * | 3 | * |
8 | * SPDX-License-Identifier: EPL-2.0 | 4 | * SPDX-License-Identifier: EPL-2.0 |
9 | *******************************************************************************/ | 5 | */ |
10 | package tools.refinery.store.dse.strategy; | 6 | package tools.refinery.store.dse.strategy; |
11 | 7 | ||
12 | import tools.refinery.store.dse.DesignSpaceExplorationAdapter; | 8 | import tools.refinery.store.dse.DesignSpaceExplorationAdapter; |
@@ -17,38 +13,31 @@ public class DepthFirstStrategy implements Strategy { | |||
17 | 13 | ||
18 | private DesignSpaceExplorationAdapter dseAdapter; | 14 | private DesignSpaceExplorationAdapter dseAdapter; |
19 | 15 | ||
20 | private int maxDepth; | 16 | private int maxDepth = Integer.MAX_VALUE; |
21 | private int maxSolutions; | 17 | private int maxSolutions = Integer.MAX_VALUE; |
22 | private boolean backTrackIfSolution = true; | 18 | private boolean backtrackFromSolution = true; |
23 | 19 | ||
24 | public DepthFirstStrategy() { | 20 | public DepthFirstStrategy withDepthLimit(int maxDepth) { |
25 | this(-1); | 21 | if (maxDepth >= 0) { |
26 | } | ||
27 | |||
28 | public DepthFirstStrategy(int maxDepth) { | ||
29 | this(maxDepth, -1); | ||
30 | } | ||
31 | |||
32 | public DepthFirstStrategy(int maxDepth, int maxSolutions) { | ||
33 | if (maxDepth < 0) { | ||
34 | this.maxDepth = Integer.MAX_VALUE; | ||
35 | } else { | ||
36 | this.maxDepth = maxDepth; | 22 | this.maxDepth = maxDepth; |
37 | } | 23 | } |
38 | if (maxSolutions < 0) { | 24 | return this; |
39 | this.maxSolutions = Integer.MAX_VALUE; | 25 | } |
40 | } else { | 26 | |
27 | public DepthFirstStrategy withSolutionLimit(int maxSolutions) { | ||
28 | if (maxSolutions >= 0) { | ||
41 | this.maxSolutions = maxSolutions; | 29 | this.maxSolutions = maxSolutions; |
42 | } | 30 | } |
31 | return this; | ||
43 | } | 32 | } |
44 | 33 | ||
45 | public DepthFirstStrategy continueIfHardObjectivesFulfilled() { | 34 | public DepthFirstStrategy continueIfHardObjectivesFulfilled() { |
46 | backTrackIfSolution = false; | 35 | backtrackFromSolution = false; |
47 | return this; | 36 | return this; |
48 | } | 37 | } |
49 | 38 | ||
50 | @Override | 39 | @Override |
51 | public void initStrategy(DesignSpaceExplorationAdapter designSpaceExplorationAdapter) { | 40 | public void initialize(DesignSpaceExplorationAdapter designSpaceExplorationAdapter) { |
52 | this.dseAdapter = designSpaceExplorationAdapter; | 41 | this.dseAdapter = designSpaceExplorationAdapter; |
53 | } | 42 | } |
54 | 43 | ||
@@ -59,22 +48,18 @@ public class DepthFirstStrategy implements Strategy { | |||
59 | } | 48 | } |
60 | while (dseAdapter.getSolutions().size() < maxSolutions) { | 49 | while (dseAdapter.getSolutions().size() < maxSolutions) { |
61 | if (!checkAndHandleGlobalConstraints()) { | 50 | if (!checkAndHandleGlobalConstraints()) { |
62 | // Global constraint is not satisfied and cannot backtrack. | ||
63 | return; | 51 | return; |
64 | } | 52 | } |
65 | // Global constraint is not satisfied, backtrack. | ||
66 | 53 | ||
67 | Fitness fitness = dseAdapter.getFitness(); | 54 | Fitness fitness = dseAdapter.getFitness(); |
68 | if (fitness.isSatisfiesHardObjectives()) { | 55 | if (fitness.isSatisfiesHardObjectives()) { |
69 | dseAdapter.newSolution(); | 56 | dseAdapter.newSolution(); |
70 | if (backTrackIfSolution && !dseAdapter.backtrack()) { | 57 | if (backtrackFromSolution && !dseAdapter.backtrack()) { |
71 | // Found a solution but cannot backtrack. | ||
72 | return; | 58 | return; |
73 | } | 59 | } |
74 | } | 60 | } |
75 | 61 | ||
76 | if (!checkAndHandleDepth()) { | 62 | if (!checkAndHandleDepth()) { |
77 | // Reached max depth but cannot backtrack. | ||
78 | return; | 63 | return; |
79 | } | 64 | } |
80 | 65 | ||
@@ -87,24 +72,18 @@ public class DepthFirstStrategy implements Strategy { | |||
87 | } | 72 | } |
88 | 73 | ||
89 | private boolean checkAndHandleGlobalConstraints() { | 74 | private boolean checkAndHandleGlobalConstraints() { |
90 | // Global constraint is not satisfied and cannot backtrack. | ||
91 | return dseAdapter.checkGlobalConstraints() || dseAdapter.backtrack(); | 75 | return dseAdapter.checkGlobalConstraints() || dseAdapter.backtrack(); |
92 | // Global constraint is satisfied or backtrack. | ||
93 | } | 76 | } |
94 | 77 | ||
95 | private boolean checkAndHandleDepth() { | 78 | private boolean checkAndHandleDepth() { |
96 | // Reached max depth but cannot backtrack. | ||
97 | return dseAdapter.getDepth() < maxDepth || dseAdapter.backtrack(); | 79 | return dseAdapter.getDepth() < maxDepth || dseAdapter.backtrack(); |
98 | // Reached max depth or backtrack. | ||
99 | } | 80 | } |
100 | 81 | ||
101 | private boolean backtrackToLastUntraversed() { | 82 | private boolean backtrackToLastUntraversed() { |
102 | while (dseAdapter.getUntraversedActivations().isEmpty()) { | 83 | while (dseAdapter.getUntraversedActivations().isEmpty()) { |
103 | if (!dseAdapter.backtrack()) { | 84 | if (!dseAdapter.backtrack()) { |
104 | // No more transitions from current state and cannot backtrack. | ||
105 | return false; | 85 | return false; |
106 | } | 86 | } |
107 | // No more transitions from current state, backtrack. | ||
108 | } | 87 | } |
109 | return true; | 88 | return true; |
110 | } | 89 | } |