Skip to content

Commit 218d7bd

Browse files
committed
RouteFindingAgentApp extended for several goals.
1 parent f593a51 commit 218d7bd

File tree

3 files changed

+54
-23
lines changed

3 files changed

+54
-23
lines changed

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,39 @@
3131
*/
3232
public class MapAgent extends ProblemSolvingAgent {
3333

34-
protected Map map = null;
35-
protected DynamicState state = new DynamicState();
34+
protected final Map map;
35+
protected final DynamicState state = new DynamicState();
36+
protected final List<String> goals = new ArrayList<String>();
37+
protected int currGoalIdx = -1;
3638

3739
// possibly null...
38-
private EnvironmentViewNotifier notifier = null;
40+
protected EnvironmentViewNotifier notifier = null;
3941
private Search search = null;
4042
private HeuristicFunctionFactory hfFactory;
41-
protected String[] goals = null;
42-
protected int currGoalIdx = 0;
4343

4444
public MapAgent(Map map, Search search, String goal) {
4545
this.map = map;
4646
this.search = search;
47-
this.goals = new String[] { goal };
47+
goals.add(goal);
4848
}
4949

5050
public MapAgent(Map map, Search search, String goal, EnvironmentViewNotifier notifier) {
5151
this(map, search, goal);
5252
this.notifier = notifier;
5353
}
5454

55-
public MapAgent(Map map, Search search, String[] goals) {
55+
public MapAgent(Map map, Search search, List<String> goals) {
5656
this.map = map;
5757
this.search = search;
58-
this.goals = new String[goals.length];
59-
System.arraycopy(goals, 0, this.goals, 0, goals.length);
58+
this.goals.addAll(goals);
6059
}
6160

62-
public MapAgent(Map map, Search search, String[] goals, EnvironmentViewNotifier notifier) {
61+
public MapAgent(Map map, Search search, List<String> goals, EnvironmentViewNotifier notifier) {
6362
this(map, search, goals);
6463
this.notifier = notifier;
6564
}
6665

67-
public MapAgent(Map map, Search search, String[] goals, EnvironmentViewNotifier notifier,
66+
public MapAgent(Map map, Search search, List<String> goals, EnvironmentViewNotifier notifier,
6867
HeuristicFunctionFactory hfFactory) {
6968
this(map, search, goals, notifier);
7069
this.hfFactory = hfFactory;
@@ -76,17 +75,15 @@ public MapAgent(Map map, Search search, String[] goals, EnvironmentViewNotifier
7675
@Override
7776
protected State updateState(Percept p) {
7877
DynamicPercept dp = (DynamicPercept) p;
79-
8078
state.setAttribute(DynAttributeNames.AGENT_LOCATION, dp.getAttribute(DynAttributeNames.PERCEPT_IN));
81-
8279
return state;
8380
}
8481

8582
@Override
8683
protected Object formulateGoal() {
8784
Object goal = null;
88-
if (currGoalIdx < goals.length) {
89-
goal = goals[currGoalIdx++];
85+
if (currGoalIdx < goals.size() - 1) {
86+
goal = goals.get(++currGoalIdx);
9087
if (notifier != null)
9188
notifier.notifyViews("CurrentLocation=In(" + state.getAttribute(DynAttributeNames.AGENT_LOCATION)
9289
+ "), Goal=In(" + goal + ")");
@@ -129,9 +126,8 @@ private void modifyHeuristicFunction(Object goal) {
129126
ef = ((BestFirstSearch) search).getEvaluationFunction();
130127
else if (search instanceof RecursiveBestFirstSearch)
131128
ef = ((RecursiveBestFirstSearch) search).getEvaluationFunction();
132-
if (ef instanceof HeuristicEvaluationFunction) {
129+
if (ef instanceof HeuristicEvaluationFunction)
133130
((HeuristicEvaluationFunction) ef).setHeuristicFunction(hfFactory.createHeuristicFunction(goal));
134-
}
135131
}
136132
}
137133
}

aimax-osm/src/main/java/aimax/osm/gui/fx/applications/OsmAgentBaseApp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public void initialize() {
176176
public void simulate() {
177177
List<MapNode> markers = map.getOsmMap().getMarkers();
178178
if (markers.size() < 2) {
179-
simPaneCtrl.setStatus("Error: Please set two markers with mouse-left.");
179+
simPaneCtrl.setStatus("Error: Please set at least two markers with mouse-left.");
180180
} else {
181181
List<String> locations = new ArrayList<String>(markers.size());
182182
for (int i = 0; i < markers.size(); i++) {

aimax-osm/src/main/java/aimax/osm/gui/fx/applications/OsmRouteFindingAgentApp.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
import java.util.HashSet;
44
import java.util.List;
55

6+
import aima.core.agent.Agent;
7+
import aima.core.environment.map.MapAgent;
8+
import aima.core.environment.map.MapFunctionFactory;
9+
import aima.core.search.framework.HeuristicFunctionFactory;
610
import aima.core.search.framework.Node;
711
import aima.core.search.framework.NodeExpander;
812
import aima.core.search.framework.SearchForActions;
13+
import aima.gui.util.SearchFactory;
914
import aimax.osm.data.EntityClassifier;
1015
import aimax.osm.data.entities.EntityViewInfo;
1116
import aimax.osm.data.entities.MapNode;
@@ -19,8 +24,10 @@
1924

2025
/**
2126
* Integrable application which demonstrates how different kinds of search
22-
* algorithms perform an a route finding scenario based on a real OSM map. Map
23-
* locations corresponding to expanded nodes are highlighted in green.
27+
* algorithms perform in a route finding scenario based on a real OSM map. This
28+
* implementation extends <code>OsmAgentBaseApp</code> by two aspects: Map
29+
* locations corresponding to expanded nodes are highlighted in green. The user
30+
* can define several goals by placing more then two markers on the map.
2431
*
2532
* @author Ruediger Lunde
2633
*
@@ -56,14 +63,28 @@ public Pane createRootPane() {
5663
return root;
5764
}
5865

66+
/**
67+
* The method is called after each parameter selection change. This
68+
* implementation clears visited states of the last simulation run, prepares
69+
* the map for different kinds of vehicles and clears the currently
70+
* displayed track.
71+
*/
72+
@Override
73+
public void initialize() {
74+
visitedStates.clear();
75+
super.initialize();
76+
}
77+
5978
/**
6079
* Factory method which creates a search strategy based on the current
61-
* parameter settings. Here, a node expander is added to visualize
62-
* progress during search.
80+
* parameter settings. Here, a listener is added to the node expander to
81+
* visualize progress during search. A dummy heuristic function is used
82+
* because the agent will replace it anyway.
6383
*/
6484
@Override
6585
protected SearchForActions createSearch(List<String> locations) {
66-
SearchForActions result = super.createSearch(locations);
86+
SearchForActions result = SearchFactory.getInstance().createSearch(simPaneCtrl.getParamValueIndex(PARAM_SEARCH),
87+
simPaneCtrl.getParamValueIndex(PARAM_Q_SEARCH_IMPL), MapFunctionFactory.getZeroHeuristicFunction());
6788
result.getNodeExpander().addNodeListener(new NodeExpander.NodeListener() {
6889
@Override
6990
public void onNodeExpanded(Node node) {
@@ -74,6 +95,20 @@ public void onNodeExpanded(Node node) {
7495
return result;
7596
}
7697

98+
/**
99+
* Factory method which creates a new agent based on the current parameter
100+
* settings. The agent is provided with a heuristic function factory to
101+
* adapt to different goals.
102+
*/
103+
protected Agent createAgent(SearchForActions search, List<String> locations) {
104+
HeuristicFunctionFactory hfFactory;
105+
if (simPaneCtrl.getParamValueIndex(PARAM_HEURISTIC) == 0)
106+
hfFactory = (goal) -> MapFunctionFactory.getZeroHeuristicFunction();
107+
else
108+
hfFactory = (goal) -> MapFunctionFactory.getSLDHeuristicFunction(goal, map);
109+
return new MapAgent(map, search, locations.subList(1, locations.size()), envViewCtrl::notify, hfFactory);
110+
}
111+
77112
// helper classes...
78113

79114
/**

0 commit comments

Comments
 (0)