aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-dse/src/main/java/tools/refinery/store/dse/strategy/BestFirstExplorer.java
blob: 72bbbc552bec71731139b0d994b4a159a20cd91f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.dse.strategy;

import tools.refinery.store.dse.transition.ObjectiveValue;
import tools.refinery.store.dse.transition.VersionWithObjectiveValue;
import tools.refinery.store.model.Model;

import java.util.Random;

public class BestFirstExplorer extends BestFirstWorker {
	final int id;
	Random random;
	public BestFirstExplorer(BestFirstStoreManager storeManager, Model model, int id) {
		super(storeManager, model);
		this.id = id;
		this.random = new Random(id);
	}

	private boolean interrupted = false;
	public void interrupt() {
		this.interrupted = true;
	}

	private boolean shouldRun() {
		return !interrupted && !hasEnoughSolution();
	}

	public void explore() {
		VersionWithObjectiveValue lastVisited = submit().newVersion();

		mainLoop: while (shouldRun()) {

			if (lastVisited == null) {
				var restored = this.restoreToBest();
				if(restored != null) {
					lastVisited = restored;
				} else {
					return;
				}
			}

			boolean tryActivation = true;
			while(tryActivation && shouldRun()) {
				RandomVisitResult randomVisitResult = this.visitRandomUnvisited(random);

				tryActivation &= randomVisitResult.shouldRetry();
				var newSubmit = randomVisitResult.submitResult();
				if(newSubmit != null) {
					if(!newSubmit.include()) {
						restoreToLast();
					} else {
						var newVisit = newSubmit.newVersion();
						int compareResult = compare(lastVisited,newVisit);
						if(compareResult >= 0) {
							lastVisited = newVisit;
							continue mainLoop;
						}
					}
				}
			}

		//final ObjectiveComparatorHelper objectiveComparatorHelper = dseAdapter.getObjectiveComparatorHelper();

		/*boolean globalConstraintsAreSatisfied = dseAdapter.checkGlobalConstraints();
		if (!globalConstraintsAreSatisfied) {
			// Global constraint is not satisfied in the first state. Terminate.
			return;
		}

		final Fitness firstFitness = dseAdapter.getFitness();
		if (firstFitness.isSatisfiesHardObjectives()) {
			dseAdapter.newSolution();
			// First state is a solution. Terminate.
			if (backTrackIfSolution) {
				return;
			}
		}

		if (maxDepth == 0) {
			return;
		}*/

		/*
		var firstTrajectoryWithFitness = new TrajectoryWithFitness(dseAdapter.getTrajectory(), firstFitness);
		trajectoriesToExplore.add(firstTrajectoryWithFitness);
		TrajectoryWithFitness currentTrajectoryWithFitness = null;
		*/
/*
			Collection<Activation> activations = dseAdapter.getUntraversedActivations();
			Iterator<Activation> iterator = activations.iterator();

			while (iterator.hasNext()) {
				final Activation nextActivation = iterator.next();
				if (!iterator.hasNext()) {
					// Last untraversed activation of the state.
					trajectoriesToExplore.remove(currentTrajectoryWithFitness);
				}

				// Executing new activation
				dseAdapter.fireActivation(nextActivation);
				if (dseAdapter.isCurrentStateAlreadyTraversed()) {
					// The new state is already visited.
					dseAdapter.backtrack();
				} else if (!dseAdapter.checkGlobalConstraints()) {
					// Global constraint is not satisfied.
					dseAdapter.backtrack();
				} else {
					final Fitness nextFitness = dseAdapter.getFitness();
					if (nextFitness.isSatisfiesHardObjectives()) {
						dseAdapter.newSolution();
						var solutions = dseAdapter.getSolutions().size();
						if (solutions >= maxSolutions) {
							return;
						}
						// Found a solution.
						if (backTrackIfSolution) {
							dseAdapter.backtrack();
							continue;
						}
					}
					if (dseAdapter.getDepth() >= maxDepth) {
						// Reached max depth.
						dseAdapter.backtrack();
						continue;
					}

					TrajectoryWithFitness nextTrajectoryWithFitness = new TrajectoryWithFitness(
							dseAdapter.getTrajectory(), nextFitness);
					trajectoriesToExplore.add(nextTrajectoryWithFitness);

					int compare = objectiveComparatorHelper.compare(currentTrajectoryWithFitness.fitness,
							nextTrajectoryWithFitness.fitness);
					if (compare < 0) {
						// Better fitness, moving on
						currentTrajectoryWithFitness = nextTrajectoryWithFitness;
						continue mainLoop;
					} else if (compare == 0) {
						if (onlyBetterFirst) {
							// Equally good fitness, backtrack
							dseAdapter.backtrack();
						} else {
							// Equally good fitness, moving on
							currentTrajectoryWithFitness = nextTrajectoryWithFitness;
							continue mainLoop;
						}
					} else {
						//"Worse fitness
						currentTrajectoryWithFitness = null;
						continue mainLoop;
					}
				}
			}

			// State is fully traversed.
			currentTrajectoryWithFitness = null;
*/
		}
		// Interrupted.
	}
}