Skip to content

Commit 83f1ce6

Browse files
committed
Code polished.
1 parent 120357f commit 83f1ce6

8 files changed

Lines changed: 121 additions & 210 deletions

File tree

aima-core/src/main/java/aima/core/environment/map/MapFunctionFactory.java

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import aima.core.search.framework.PerceptToStateFunction;
1111
import aima.core.search.framework.problem.ActionsFunction;
1212
import aima.core.search.framework.problem.ResultFunction;
13+
import aima.core.util.math.geom.shapes.Point2D;
1314

1415
/**
1516
* @author Ciaran O'Reilly
17+
* @author Ruediger Lunde
1618
*
1719
*/
1820
public class MapFunctionFactory {
@@ -34,6 +36,23 @@ public static ResultFunction getResultFunction() {
3436
return resultFunction;
3537
}
3638

39+
/** Returns an adaptable heuristic based on straight line distance computation. Don't forget to call
40+
* {@link AdaptableHeuristicFunction#adaptToGoal(Object, Map)} before use! */
41+
public static AdaptableHeuristicFunction getSLDHeuristicFunction() {
42+
return new StraightLineDistanceHeuristicFunction();
43+
}
44+
45+
/** Returns an adaptable heuristic based on straight line distance computation. */
46+
public static AdaptableHeuristicFunction getSLDHeuristicFunction(Object goal, Map map) {
47+
return new StraightLineDistanceHeuristicFunction(goal, map);
48+
}
49+
50+
/** Returns a heuristic which always returns zero. More optimism is not possible. */
51+
public static AdaptableHeuristicFunction getZeroHeuristicFunction() {
52+
return new ZeroHeuristicFunction();
53+
}
54+
55+
3756
private static class MapActionsFunction implements ActionsFunction {
3857
private Map map = null;
3958
private boolean reverseMode;
@@ -52,11 +71,11 @@ public Set<Action> actions(Object state) {
5271
for (String linkLoc : linkedLocations) {
5372
actions.add(new MoveToAction(linkLoc));
5473
}
55-
5674
return actions;
5775
}
5876
}
5977

78+
6079
public static PerceptToStateFunction getPerceptToStateFunction() {
6180
if (null == perceptToStateFunction) {
6281
perceptToStateFunction = new MapPerceptToStateFunction();
@@ -69,22 +88,51 @@ public MapResultFunction() {
6988
}
7089

7190
public Object result(Object s, Action a) {
72-
7391
if (a instanceof MoveToAction) {
7492
MoveToAction mta = (MoveToAction) a;
75-
7693
return mta.getToLocation();
7794
}
78-
7995
// The Action is not understood or is a NoOp
8096
// the result will be the current state.
8197
return s;
8298
}
8399
}
84100

101+
85102
private static class MapPerceptToStateFunction implements PerceptToStateFunction {
86103
public Object getState(Percept p) {
87104
return ((DynamicPercept) p).getAttribute(DynAttributeNames.PERCEPT_IN);
88105
}
89106
}
107+
108+
109+
private static class StraightLineDistanceHeuristicFunction extends
110+
AdaptableHeuristicFunction {
111+
112+
public StraightLineDistanceHeuristicFunction() {
113+
}
114+
115+
public StraightLineDistanceHeuristicFunction(Object goal, Map map) {
116+
this.goal = goal;
117+
this.map = map;
118+
}
119+
120+
public double h(Object state) {
121+
double result = 0.0;
122+
Point2D pt1 = map.getPosition((String) state);
123+
Point2D pt2 = map.getPosition((String) goal);
124+
if (pt1 != null && pt2 != null) {
125+
result = pt1.distance(pt2);
126+
}
127+
return result;
128+
}
129+
}
130+
131+
private static class ZeroHeuristicFunction extends
132+
AdaptableHeuristicFunction {
133+
134+
public double h(Object state) {
135+
return 0.0;
136+
}
137+
}
90138
}

aima-core/src/main/java/aima/core/environment/map/StraightLineDistanceHeuristicFunction.java

Lines changed: 0 additions & 25 deletions
This file was deleted.

aima-core/src/test/java/aima/test/core/unit/search/informed/AStarSearchTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import aima.core.environment.map.MapFunctionFactory;
1616
import aima.core.environment.map.MapStepCostFunction;
1717
import aima.core.environment.map.SimplifiedRoadMapOfPartOfRomania;
18-
import aima.core.environment.map.StraightLineDistanceHeuristicFunction;
1918
import aima.core.search.framework.HeuristicFunction;
2019
import aima.core.search.framework.Search;
2120
import aima.core.search.framework.SearchAgent;
@@ -70,7 +69,7 @@ public void testAIMA3eFigure3_15() throws Exception {
7069
new MapStepCostFunction(romaniaMap));
7170

7271
Search search = new AStarSearch(new GraphSearch(),
73-
new StraightLineDistanceHeuristicFunction(
72+
MapFunctionFactory.getSLDHeuristicFunction(
7473
SimplifiedRoadMapOfPartOfRomania.BUCHAREST, romaniaMap));
7574
SearchAgent agent = new SearchAgent(problem, search);
7675

@@ -93,7 +92,7 @@ public void testAIMA3eFigure3_24() throws Exception {
9392
new MapStepCostFunction(romaniaMap));
9493

9594
Search search = new AStarSearch(new TreeSearch(),
96-
new StraightLineDistanceHeuristicFunction(
95+
MapFunctionFactory.getSLDHeuristicFunction(
9796
SimplifiedRoadMapOfPartOfRomania.BUCHAREST, romaniaMap));
9897
SearchAgent agent = new SearchAgent(problem, search);
9998
Assert.assertEquals(
@@ -118,7 +117,7 @@ public void testAIMA3eFigure3_24_using_GraphSearch() throws Exception {
118117
new MapStepCostFunction(romaniaMap));
119118

120119
Search search = new AStarSearch(new GraphSearch(),
121-
new StraightLineDistanceHeuristicFunction(
120+
MapFunctionFactory.getSLDHeuristicFunction(
122121
SimplifiedRoadMapOfPartOfRomania.BUCHAREST, romaniaMap));
123122
SearchAgent agent = new SearchAgent(problem, search);
124123
Assert.assertEquals(

aima-core/src/test/java/aima/test/core/unit/search/informed/GreedyBestFirstSearchTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import aima.core.environment.map.MapFunctionFactory;
1212
import aima.core.environment.map.MapStepCostFunction;
1313
import aima.core.environment.map.SimplifiedRoadMapOfPartOfRomania;
14-
import aima.core.environment.map.StraightLineDistanceHeuristicFunction;
1514
import aima.core.search.framework.PrioritySearch;
1615
import aima.core.search.framework.Search;
1716
import aima.core.search.framework.SearchAgent;
@@ -84,7 +83,7 @@ public void testAIMA3eFigure3_23() throws Exception {
8483
new DefaultGoalTest(SimplifiedRoadMapOfPartOfRomania.BUCHAREST), new MapStepCostFunction(romaniaMap));
8584

8685
Search search = new GreedyBestFirstSearch(new TreeSearch(),
87-
new StraightLineDistanceHeuristicFunction(SimplifiedRoadMapOfPartOfRomania.BUCHAREST, romaniaMap));
86+
MapFunctionFactory.getSLDHeuristicFunction(SimplifiedRoadMapOfPartOfRomania.BUCHAREST, romaniaMap));
8887
SearchAgent agent = new SearchAgent(problem, search);
8988
Assert.assertEquals(
9089
"[Action[name==moveTo, location==Sibiu], Action[name==moveTo, location==Fagaras], Action[name==moveTo, location==Bucharest]]",
@@ -103,7 +102,7 @@ public void testAIMA3eFigure3_23_using_GraphSearch() throws Exception {
103102
new DefaultGoalTest(SimplifiedRoadMapOfPartOfRomania.BUCHAREST), new MapStepCostFunction(romaniaMap));
104103

105104
Search search = new GreedyBestFirstSearch(new GraphSearch(),
106-
new StraightLineDistanceHeuristicFunction(SimplifiedRoadMapOfPartOfRomania.BUCHAREST, romaniaMap));
105+
MapFunctionFactory.getSLDHeuristicFunction(SimplifiedRoadMapOfPartOfRomania.BUCHAREST, romaniaMap));
107106
SearchAgent agent = new SearchAgent(problem, search);
108107
Assert.assertEquals(
109108
"[Action[name==moveTo, location==Sibiu], Action[name==moveTo, location==Fagaras], Action[name==moveTo, location==Bucharest]]",

aima-gui/src/main/java/aima/gui/fx/applications/agent/RouteFindingAgentApp.java

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@
44
import java.util.List;
55

66
import aima.core.agent.Agent;
7-
import aima.core.environment.map.AdaptableHeuristicFunction;
8-
import aima.core.environment.map.ExtendableMap;
9-
import aima.core.environment.map.MapAgent;
10-
import aima.core.environment.map.MapEnvironment;
11-
import aima.core.environment.map.Scenario;
12-
import aima.core.environment.map.SimplifiedRoadMapOfAustralia;
13-
import aima.core.environment.map.SimplifiedRoadMapOfPartOfRomania;
7+
import aima.core.environment.map.*;
148
import aima.core.util.CancelableThread;
159
import aima.core.util.math.geom.shapes.Point2D;
1610
import aima.gui.fx.framework.IntegrableApplication;
@@ -107,11 +101,12 @@ protected Parameter[] createParameters() {
107101
Parameter p3 = new Parameter(PARAM_SEARCH, (Object[]) SearchFactory.getInstance().getSearchStrategyNames());
108102
p3.setDefaultValueIndex(5);
109103
Parameter p4 = new Parameter(PARAM_Q_SEARCH_IMPL, (Object[]) SearchFactory.getInstance().getQSearchImplNames());
104+
p4.setDefaultValueIndex(1);
110105
p4.setDependency(PARAM_SEARCH, "Depth First", "Breadth First", "Uniform Cost", "Greedy Best First", "A*");
111106
Parameter p5 = new Parameter(PARAM_HEURISTIC, "0", "SLD");
107+
p5.setDefaultValueIndex(1);
112108
p5.setDependency(PARAM_SEARCH, "Greedy Best First", "A*", "Recursive Best First",
113109
"Recursive Best First No Loops", "Hill Climbing");
114-
p5.setDefaultValueIndex(1);
115110
return new Parameter[] { p1, p2r, p2a, p3, p4, p5 };
116111
}
117112

@@ -180,12 +175,11 @@ public void initialize() {
180175

181176
switch (simPaneCtrl.getParamValueIndex(PARAM_HEURISTIC)) {
182177
case 0:
183-
heuristic = new H1();
178+
heuristic = MapFunctionFactory.getZeroHeuristicFunction();
184179
break;
185180
default:
186-
heuristic = new H2();
181+
heuristic = MapFunctionFactory.getSLDHeuristicFunction(destinations.get(0), scenario.getAgentMap());
187182
}
188-
heuristic.adaptToGoal(destinations.get(0), scenario.getAgentMap());
189183

190184
search = SearchFactory.getInstance().createSearch(simPaneCtrl.getParamValueIndex(PARAM_SEARCH),
191185
simPaneCtrl.getParamValueIndex(PARAM_Q_SEARCH_IMPL), heuristic);
@@ -204,41 +198,12 @@ public void simulate() {
204198
env.step();
205199
simPaneCtrl.waitAfterStep();
206200
}
207-
simPaneCtrl.setStatus("Search metrics: " + search.getMetrics());
201+
simPaneCtrl.setStatus(search.getMetrics().toString());
208202
envViewCtrl.notify("pathCost=" + search.getMetrics().get("pathCost"));
209203
}
210204

211205
@Override
212206
public void finalize() {
213207
simPaneCtrl.cancelSimulation();
214208
}
215-
216-
// helper classes...
217-
218-
/**
219-
* Returns always the heuristic value 0.
220-
*/
221-
static class H1 extends AdaptableHeuristicFunction {
222-
223-
public double h(Object state) {
224-
return 0.0;
225-
}
226-
}
227-
228-
/**
229-
* A simple heuristic which interprets <code>state</code> and {@link #goal}
230-
* as location names and uses the straight-line distance between them as
231-
* heuristic value.
232-
*/
233-
static class H2 extends AdaptableHeuristicFunction {
234-
235-
public double h(Object state) {
236-
double result = 0.0;
237-
Point2D pt1 = map.getPosition((String) state);
238-
Point2D pt2 = map.getPosition((String) goal);
239-
if (pt1 != null && pt2 != null)
240-
result = pt1.distance(pt2);
241-
return result;
242-
}
243-
}
244209
}

aimax-osm/src/main/java/aimax/osm/gui/fx/IntegratedAimaOsmFxApp.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,4 @@ public static void defineContent(IntegratedAppPaneBuilder builder) {
4141
builder.registerApp(OsmRouteFindingAgentApp.class);
4242
builder.registerApp(OsmLRTAStarAgentApp.class);
4343
}
44-
4544
}

0 commit comments

Comments
 (0)