Skip to content

Commit 06b412e

Browse files
Ruediger.LundeRuediger.Lunde
authored andcommitted
Documentation improved.
1 parent 8b0689c commit 06b412e

File tree

2 files changed

+222
-193
lines changed

2 files changed

+222
-193
lines changed

src/aima/gui/applications/search/map/RoutePlanningAgentAppDemo.java

Lines changed: 94 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,92 +7,109 @@
77
import aima.gui.framework.AgentAppModel;
88
import aima.gui.framework.SimpleAgentAppDemo;
99
import aima.search.framework.SearchFactory;
10-
import aima.search.map.*;
10+
import aima.search.map.AdaptableHeuristicFunction;
11+
import aima.search.map.MapAgent;
12+
import aima.search.map.MapEnvironment;
13+
import aima.search.map.MapWithSLD;
14+
import aima.search.map.Scenario;
15+
import aima.search.map.SimplifiedRoadMapOfAustralia;
16+
import aima.search.map.SimplifiedRoadMapOfRomania;
1117

1218
/**
13-
* Demo example of a route planning agent application with GUI. The main
14-
* method starts a map agent frame and supports runtime experiments.
15-
* This implementation is based on the {@link aima.search.map.MapAgent}
16-
* and the {@link aima.search.map.MapEnvironment} from the aima library.
17-
* It can be used as a code template for creating new applications with different
18-
* specialized kinds of agents and environments.
19+
* Demo example of a route planning agent application with GUI. The main method
20+
* starts a map agent frame and supports runtime experiments. This
21+
* implementation is based on the {@link aima.search.map.MapAgent} and the
22+
* {@link aima.search.map.MapEnvironment} from the aima library. It can be used
23+
* as a code template for creating new applications with different specialized
24+
* kinds of agents and environments.
25+
*
1926
* @author R. Lunde
2027
*/
2128
public class RoutePlanningAgentAppDemo extends SimpleAgentAppDemo {
2229

2330
/** Creates a <code>MapAgentModel</code>. */
31+
@Override
2432
public AgentAppModel createModel() {
2533
return new MapAgentModel();
2634
}
27-
35+
2836
/** Creates and configures a <code>RoutePlanningAgentFrame</code>. */
37+
@Override
2938
public AgentAppFrame createFrame() {
3039
return new RoutePlanningAgentFrame();
3140
}
3241

3342
/** Creates a <code>RoutePlanningAgentController</code>. */
43+
@Override
3444
public AgentAppController createController() {
3545
return new RoutePlanningAgentController();
3646
}
37-
38-
39-
////////////////////////////////////////////////////////////
47+
48+
// //////////////////////////////////////////////////////////
4049
// local classes
41-
50+
4251
/** Frame for a graphical route planning agent application. */
4352
protected static class RoutePlanningAgentFrame extends MapAgentFrame {
44-
public static enum MapType { ROMANIA, AUSTRALIA };
53+
public static enum MapType {
54+
ROMANIA, AUSTRALIA
55+
};
56+
4557
private MapType usedMap = null;
46-
private static String[] ROMANIA_DESTS = new String[]{
47-
"D1 (to Bucharest)", "D2 (to Eforie)", "D3 (to Neamt)",
48-
"D4 (to random)"};
49-
private static String[] AUSTRALIA_DESTS = new String[]{
50-
"D1 (to Port Hedland)", "D2 (to Albany)", "D3 (to Melbourne)",
51-
"D4 (to random)"};
52-
58+
private static String[] ROMANIA_DESTS = new String[] {
59+
"D1 (to Bucharest)", "D2 (to Eforie)", "D3 (to Neamt)",
60+
"D4 (to random)" };
61+
private static String[] AUSTRALIA_DESTS = new String[] {
62+
"D1 (to Port Hedland)", "D2 (to Albany)", "D3 (to Melbourne)",
63+
"D4 (to random)" };
64+
5365
/** Creates a new frame. */
5466
public RoutePlanningAgentFrame() {
55-
setTitle("RPA - the Routing Planning Agent");
56-
setSelectorItems(SCENARIO_SEL, new String[]{
67+
setTitle("RPA - the Route Planning Agent");
68+
setSelectorItems(SCENARIO_SEL, new String[] {
5769
"S1 (Romania, from Arad)", "S2 (Romania, from Lugoj)",
5870
"S3 (Romania, from Fagaras)",
59-
"S4 (Australia, from Sydney)", "S4 (Australia, from Random)"}, 0);
60-
setSelectorItems(SEARCH_MODE_SEL,
61-
SearchFactory.getInstance().getSearchModeNames(), 1); // change the default!
62-
setSelectorItems(HEURISTIC_SEL, new String[]{
63-
"H1 (=0)", "H2 (sld to goal)"}, 1);
71+
"S4 (Australia, from Sydney)",
72+
"S4 (Australia, from Random)" }, 0);
73+
setSelectorItems(SEARCH_MODE_SEL, SearchFactory.getInstance()
74+
.getSearchModeNames(), 1); // change the default!
75+
setSelectorItems(HEURISTIC_SEL, new String[] { "H1 (=0)",
76+
"H2 (sld to goal)" }, 1);
6477
}
65-
78+
6679
/**
67-
* Changes the destination selector items depending on the
68-
* scenario selection if necessary, and calls the super
69-
* class implementation afterwards.
80+
* Changes the destination selector items depending on the scenario
81+
* selection if necessary, and calls the super class implementation
82+
* afterwards.
7083
*/
84+
@Override
7185
protected void selectionChanged() {
7286
SelectionState state = getSelection();
7387
int scenarioIdx = state.getValue(MapAgentFrame.SCENARIO_SEL);
74-
RoutePlanningAgentFrame.MapType mtype = (scenarioIdx < 3)
75-
? MapType.ROMANIA : MapType.AUSTRALIA;
88+
RoutePlanningAgentFrame.MapType mtype = (scenarioIdx < 3) ? MapType.ROMANIA
89+
: MapType.AUSTRALIA;
7690
if (mtype != usedMap) {
7791
usedMap = mtype;
7892
String[] items = null;
7993
switch (mtype) {
8094
case ROMANIA:
81-
items = ROMANIA_DESTS; break;
95+
items = ROMANIA_DESTS;
96+
break;
8297
case AUSTRALIA:
83-
items = AUSTRALIA_DESTS; break;
98+
items = AUSTRALIA_DESTS;
99+
break;
84100
}
85101
setSelectorItems(DESTINATION_SEL, items, 0);
86102
}
87103
super.selectionChanged();
88104
}
89105
}
90-
106+
91107
/** Controller for a graphical route planning agent application. */
92-
protected static class RoutePlanningAgentController extends AbstractMapAgentController {
108+
protected static class RoutePlanningAgentController extends
109+
AbstractMapAgentController {
93110
/**
94-
* Configures a scenario and a list of destinations. Note that
95-
* for route planning problems, the size of the list needs to be 1.
111+
* Configures a scenario and a list of destinations. Note that for route
112+
* planning problems, the size of the list needs to be 1.
96113
*/
97114
@Override
98115
protected void selectScenarioAndDest(int scenarioIdx, int destIdx) {
@@ -102,22 +119,27 @@ protected void selectScenarioAndDest(int scenarioIdx, int destIdx) {
102119
switch (scenarioIdx) {
103120
case 0:
104121
SimplifiedRoadMapOfRomania.initMap(map);
105-
agentLoc = SimplifiedRoadMapOfRomania.ARAD; break;
122+
agentLoc = SimplifiedRoadMapOfRomania.ARAD;
123+
break;
106124
case 1:
107125
SimplifiedRoadMapOfRomania.initMap(map);
108-
agentLoc = SimplifiedRoadMapOfRomania.LUGOJ; break;
126+
agentLoc = SimplifiedRoadMapOfRomania.LUGOJ;
127+
break;
109128
case 2:
110129
SimplifiedRoadMapOfRomania.initMap(map);
111-
agentLoc = SimplifiedRoadMapOfRomania.FAGARAS; break;
130+
agentLoc = SimplifiedRoadMapOfRomania.FAGARAS;
131+
break;
112132
case 3:
113133
SimplifiedRoadMapOfAustralia.initMap(map);
114-
agentLoc = SimplifiedRoadMapOfAustralia.SYDNEY; break;
134+
agentLoc = SimplifiedRoadMapOfAustralia.SYDNEY;
135+
break;
115136
case 4:
116137
SimplifiedRoadMapOfAustralia.initMap(map);
117-
agentLoc = map.randomlyGenerateDestination(); break;
138+
agentLoc = map.randomlyGenerateDestination();
139+
break;
118140
}
119141
scenario = new Scenario(env, map, agentLoc);
120-
142+
121143
destinations = new ArrayList<String>();
122144
if (scenarioIdx < 3) {
123145
switch (destIdx) {
@@ -151,43 +173,52 @@ protected void selectScenarioAndDest(int scenarioIdx, int destIdx) {
151173
}
152174
}
153175
}
154-
155-
/** Prepares the model for the previously specified scenario and destinations. */
176+
177+
/**
178+
* Prepares the model for the previously specified scenario and
179+
* destinations.
180+
*/
156181
@Override
157182
protected void prepareModel() {
158183
((MapAgentModel) model).prepare(scenario, destinations);
159184
}
160-
185+
161186
/**
162-
* Returns the trivial zero function or a simple heuristic
163-
* which is based on straight-line distance computation.
187+
* Returns the trivial zero function or a simple heuristic which is
188+
* based on straight-line distance computation.
164189
*/
165190
@Override
166191
protected AdaptableHeuristicFunction createHeuristic(int heuIdx) {
167192
switch (heuIdx) {
168-
case 0: return new H1();
169-
default: return new H2();
193+
case 0:
194+
return new H1();
195+
default:
196+
return new H2();
170197
}
171198
}
172-
199+
173200
/**
174201
* Creates environment and agent, starts the agent and initiates some
175202
* text outputs describing the state of the agent.
176203
*/
177204
@Override
178205
protected void startAgent() {
179206
if (destinations.size() != 1) {
180-
frame.logMessage("Error: This agent requires exact one destination.");
207+
frame
208+
.logMessage("Error: This agent requires exact one destination.");
181209
return;
182210
}
183211
frame.logMessage("<route-planning-simulation-protocol>");
184212
frame.logMessage("search: " + search.getClass().getName());
185213
MapEnvironment env = scenario.getEnv();
186214
String goal = destinations.get(0);
187-
MapAgent agent = new MapAgent(env, search, new String[]{goal});
215+
MapAgent agent = new MapAgent(env, search, new String[] { goal });
188216
if (heuristic != null) {
189-
frame.logMessage("heuristic: " + heuristic.getClass().getName());
190-
agent.setHeuristicFunction(heuristic.getAdaptation(goal, scenario.getAgentMap()));
217+
frame
218+
.logMessage("heuristic: "
219+
+ heuristic.getClass().getName());
220+
agent.setHeuristicFunction(heuristic.getAdaptation(goal,
221+
scenario.getAgentMap()));
191222
}
192223
env.addAgent(agent, scenario.getInitAgentLocation());
193224
env.stepUntilNoOp();
@@ -204,23 +235,22 @@ public double getHeuristicValue(Object state) {
204235
return 0.0;
205236
}
206237
}
207-
238+
208239
/**
209240
* A simple heuristic which interprets <code>state</code> and
210-
* {@link #goal} as location names and uses the straight-line
211-
* distance between them as heuristic value.
241+
* {@link #goal} as location names and uses the straight-line distance
242+
* between them as heuristic value.
212243
*/
213244
static class H2 extends AdaptableHeuristicFunction {
214245
@Override
215246
public double getHeuristicValue(Object state) {
216247
return map.getStraightLineDistance((String) state, (String) goal);
217248
}
218249
}
219-
220-
221-
////////////////////////////////////////////////////////////
250+
251+
// //////////////////////////////////////////////////////////
222252
// starter method
223-
253+
224254
/** Application starter. */
225255
public static void main(String args[]) {
226256
new RoutePlanningAgentAppDemo().startApplication();

0 commit comments

Comments
 (0)