-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneralSearch.java
More file actions
161 lines (137 loc) · 4.64 KB
/
GeneralSearch.java
File metadata and controls
161 lines (137 loc) · 4.64 KB
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
package searchAlgorithms;
import java.util.ArrayList;
import java.util.HashMap;
import heuristic.Heuristic;
import loopHandling.GraphLoopHandler;
import loopHandling.LoopHandler;
import pathEvaluation.PathEvaluator;
import searchStrategy.SearchStrategy;
import stateSpace.StateSpace;
/**
* the general search algorithm from the lecture
* @param <Node> the node type that the StateSpace uses
*/
public class GeneralSearch<Node> {
private StateSpace<Node> space;
private SearchStrategy<Node> strategy;
private PathEvaluator<Node> pathEvaluator;
private Heuristic<Node> heuristic;
private LoopHandler<Node> loopHandler = new GraphLoopHandler<>();
/**
* @param space the StateSpace that the algorithm searches in
* @param strategy the search strategy that the algorithm uses
* @param pathEvaluator the path evaluation that the algorithm uses
* @param heuristic the heuristic that the algorithm uses
*/
public GeneralSearch(StateSpace<Node> space, SearchStrategy<Node> strategy, PathEvaluator<Node> pathEvaluator, Heuristic<Node> heuristic) {
this.space = space;
this.strategy = strategy;
this.pathEvaluator = pathEvaluator;
this.heuristic = heuristic;
}
/**
* initializes the search and iterates until a path is found or no further iterations can be made
* @return the first path that is being found that leads to a goal state, null if no path is being found
*/
public ArrayList<Node> search() {
initializeSearch();
while(!strategy.isEmpty()) {
ArrayList<Node> pathToGoal = iterateSearch();
if(pathToGoal != null) return pathToGoal;
}
return null;
}
public HashMap<Node, Double> minCostToNode = new HashMap<>();
public HashMap<Node, Double> minCostToVisitedNode = new HashMap<>();
private ArrayList<Node> startPath = new ArrayList<>();
/**
* initializes the search
*/
public void initializeSearch() {
strategy.clear();
minCostToNode.clear();
minCostToVisitedNode.clear();
minCostToNode.put(space.getStart(), 0.0);
startPath.clear();
startPath.add(space.getStart());
strategy.add(startPath, rate(startPath));
}
/**
* executes one iteration of the search
* @return the first path that is being found that leads to a goal state, null if no path is being found
*/
public ArrayList<Node> iterateSearch() {
ArrayList<Node> path = strategy.get();
minCostToVisitedNode.put(path.getLast(), pathEvaluator.pastCost(space, path));
if(space.isGoal(path.getLast())) return path;
for(Node neighbor : space.getNeighbors(path.getLast())) {
ArrayList<Node> newPath = new ArrayList<Node>(path);
newPath.add(neighbor);
double pathCost = pathEvaluator.pastCost(space, newPath);
if(loopHandler.shouldVisitNode(neighbor, newPath, pathCost, minCostToNode)) {
minCostToNode.put(neighbor, pathCost);
strategy.add(newPath, rate(newPath));
}
}
return null;
}
/**
* combines the past and future cost
* @param path the overall cost of the path
* @return the overall cost of the path
*/
public double rate(ArrayList<Node> path) {
return pathEvaluator.pastCost(space, path) + heuristic.futureCost(space, path.getLast());
}
/**
* if the heuristic is not admissible, this function is guaranteed to return false, if the heuristic is admissible and the StateSpace is infinite, this function will not terminate
* @return whether or not the heuristic that the algorithm uses is admissible
*/
public boolean isHeuristicAdmissible() {
Node initialStart = space.getStart();
UniformSearch<Node> search = new UniformSearch<>(space);
ArrayList<Node> path = null;
for(Node node : space.getNodes()) {
double heuristicCost = heuristic.futureCost(space, node);
space.setStart(node);
path = search.search();
double actualCost = 0;
for(int i = 0; i < path.size() - 1; i++) actualCost += space.getCost(path.get(i), path.get(i + 1));
System.out.println(node.toString() + "\t" + heuristicCost + "\t" + actualCost);
if(actualCost < heuristicCost) return false;
}
space.setStart(initialStart);
return true;
}
/**
* @return the StateSpace that the algorithm searches in
*/
public StateSpace<Node> getStateSpace() {
return space;
}
/**
* @return the strategy that the algorithm uses
*/
public SearchStrategy<Node> getStrategy() {
return strategy;
}
/**
* @return the heuristic that the algorithm uses
*/
public Heuristic<Node> getHeuristic() {
return heuristic;
}
/**
* @return the path evaluator that the algorithm uses
*/
public PathEvaluator<Node> getPathEvaluator() {
return pathEvaluator;
}
/**
* the loop handler that the algorithm uses
* @return
*/
public LoopHandler<Node> getLoopHandler() {
return loopHandler;
}
}