/
SimulatedAnnealingSearch.java
181 lines (161 loc) · 5.71 KB
/
SimulatedAnnealingSearch.java
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package aima.core.search.local;
import aima.core.search.framework.*;
import aima.core.search.framework.problem.Problem;
import aima.core.util.Tasks;
import aima.core.util.Util;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.ToDoubleFunction;
/**
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 4.5, page
* 126.<br>
* <br>
*
* <pre>
* function SIMULATED-ANNEALING(problem, schedule) returns a solution state
*
* current <- MAKE-NODE(problem.INITIAL-STATE)
* for t = 1 to INFINITY do
* T <- schedule(t)
* if T = 0 then return current
* next <- a randomly selected successor of current
* /\E <- next.VALUE - current.value
* if /\E > 0 then current <- next
* else current <- next only with probability eˆ(/\E/T)
* </pre>
*
* Figure 4.5 The simulated annealing search algorithm, a version of stochastic
* hill climbing where some downhill moves are allowed. Downhill moves are
* accepted readily early in the annealing schedule and then less often as time
* goes on. The schedule input determines the value of the temperature T as a
* function of time.
*
* @author Ravi Mohan
* @author Mike Stampone
* @author Ruediger Lunde
*/
public class SimulatedAnnealingSearch<S, A> implements SearchForActions<S, A>, SearchForStates<S, A> {
public static final String METRIC_NODES_EXPANDED = "nodesExpanded";
public static final String METRIC_TEMPERATURE = "temp";
public static final String METRIC_NODE_VALUE = "nodeValue";
private final ToDoubleFunction<Node<S, A>> energyFn;
private final Scheduler scheduler;
private final NodeFactory<S, A> nodeFactory;
private S lastState;
private Metrics metrics = new Metrics();
/**
* Constructs a simulated annealing search for the specified energy
* function and a default scheduler.
*
* @param energyFn
* a function mapping nodes to the energy of their state (the lower the better).
*/
public SimulatedAnnealingSearch(ToDoubleFunction<Node<S, A>> energyFn) {
this(energyFn, new Scheduler());
}
/**
* Constructs a simulated annealing search for the specified energy
* function and scheduler.
*
* @param energyFn
* a function mapping nodes to the energy of their state (the lower the better).
* @param scheduler
* a mapping from time to "temperature"
*/
public SimulatedAnnealingSearch(ToDoubleFunction<Node<S, A>> energyFn, Scheduler scheduler) {
this(energyFn, scheduler, new NodeFactory<>());
}
public SimulatedAnnealingSearch(ToDoubleFunction<Node<S, A>> energyFn, Scheduler scheduler, NodeFactory<S, A> nodeFactory) {
this.energyFn = energyFn;
this.scheduler = scheduler;
this.nodeFactory = nodeFactory;
nodeFactory.addNodeListener((node) -> metrics.incrementInt(METRIC_NODES_EXPANDED));
}
@Override
public Optional<List<A>> findActions(Problem<S, A> p) {
nodeFactory.useParentLinks(true);
return SearchUtils.toActions(findNode(p));
}
@Override
public Optional<S> findState(Problem<S, A> p) {
nodeFactory.useParentLinks(false);
return SearchUtils.toState(findNode(p));
}
/**
* Returns a node corresponding to a goal state or empty. Method {@link #getLastState()}
* provides the last explored state if result is empty. Note that in this version, a minimum
* is searched (two advantages: 1. The physical idea behind the algorithm becomes more visible.
* 2. Heuristic functions can directly be used as energy functions, no need to change the sign
* of the value).
*/
/// function SIMULATED-ANNEALING(problem, schedule) returns a solution state
public Optional<Node<S, A>> findNode(Problem<S, A> p) {
clearMetrics();
/// current <- MAKE-NODE(problem.INITIAL-STATE)
Node<S, A> current = nodeFactory.createNode(p.getInitialState());
/// for t = 1 to INFINITY do
int timeStep = 0;
while (!Tasks.currIsCancelled()) {
/// temperature <- schedule(t)
double temperature = scheduler.getTemp(timeStep);
timeStep++;
lastState = current.getState();
/// if temperature = 0 then return current
if (temperature == 0.0) {
lastState = current.getState();
return Optional.ofNullable(p.testSolution(current) ? current : null);
}
updateMetrics(temperature, getEnergy(current));
List<Node<S, A>> children = nodeFactory.getSuccessors(current, p);
if (children.size() > 0) {
/// next <- a randomly selected successor of current
Node<S, A> next = Util.selectRandomlyFromList(children);
/// dE <- next.VALUE - current.value
double deltaE = getEnergy(next) - getEnergy(current);
// if dE < 0 then current <- next
// else current <- next only with probability eˆ(-dE/T)
if (deltaE < 0.0 || Math.random() <= Math.exp(-deltaE / temperature))
current = next;
}
}
lastState = current.getState();
return Optional.empty();
}
private double getEnergy(Node<S, A> n) {
return energyFn.applyAsDouble(n);
}
/**
* Returns the last explored state.
*/
public S getLastState() {
return lastState;
}
/**
* Returns all the search metrics.
*/
@Override
public Metrics getMetrics() {
return metrics;
}
private void updateMetrics(double temperature, double value) {
metrics.set(METRIC_TEMPERATURE, temperature);
metrics.set(METRIC_NODE_VALUE, value);
}
/**
* Sets all metrics to zero.
*/
private void clearMetrics() {
metrics.set(METRIC_NODES_EXPANDED, 0);
metrics.set(METRIC_TEMPERATURE, 0);
metrics.set(METRIC_NODE_VALUE, 0);
}
@Override
public void addNodeListener(Consumer<Node<S, A>> listener) {
nodeFactory.addNodeListener(listener);
}
@Override
public boolean removeNodeListener(Consumer<Node<S, A>> listener) {
return nodeFactory.removeNodeListener(listener);
}
}