Skip to content

Commit 4b0c919

Browse files
authored
Add files via upload
1 parent 54ea15d commit 4b0c919

File tree

7 files changed

+961
-961
lines changed

7 files changed

+961
-961
lines changed
Lines changed: 158 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,159 @@
1-
package aima.gui.swing.demo.agent.map;
2-
3-
import java.text.DecimalFormat;
4-
import java.util.List;
5-
6-
import aima.core.agent.Agent;
7-
import aima.core.environment.map.AdaptableHeuristicFunction;
8-
import aima.core.environment.map.MapEnvironment;
9-
import aima.core.environment.map.Scenario;
10-
import aima.gui.swing.framework.AgentAppController;
11-
import aima.gui.swing.framework.MessageLogger;
12-
import aima.gui.swing.framework.SimulationThread;
13-
14-
/**
15-
* Provides a useful base class for agent application controller implementations
16-
* in the context of route finding agent application development. To get it
17-
* ready to work, all you need to do is, to provide implementations for the four
18-
* abstract methods. See {@link RouteFindingAgentApp} for an example.
19-
*
20-
* @author Ruediger Lunde
21-
*/
22-
public abstract class AbstractMapAgentController extends AgentAppController {
23-
/** A scenario. */
24-
protected Scenario scenario;
25-
/**
26-
* Some location names. For route finding problems, only one location
27-
* should be specified.
28-
*/
29-
protected List<String> destinations;
30-
/** Search method to be used. */
31-
protected aima.core.search.framework.Search search;
32-
/** Heuristic function to be used when performing informed search. */
33-
protected AdaptableHeuristicFunction heuristic;
34-
/** Is the scenario up to date? */
35-
protected boolean isPrepared;
36-
/** Sleep time between two steps during simulation in msec. */
37-
protected long sleepTime = 500l;
38-
39-
/** Clears all tracks and prepares simulation if necessary. */
40-
@Override
41-
public void clear() {
42-
((MapAgentView) frame.getEnvView()).clearTracks();
43-
if (!isPrepared())
44-
prepare(null);
45-
}
46-
47-
/**
48-
* Template method, which performs necessary preparations for running the
49-
* agent. The behavior is strongly influenced by the primitive operations
50-
* {@link #selectScenarioAndDest(int, int)}, {@link #prepareView()} and
51-
* {@link #createHeuristic(int)}.
52-
*/
53-
@Override
54-
public void prepare(String changedSelectors) {
55-
MapAgentFrame.SelectionState state = frame.getSelection();
56-
selectScenarioAndDest(state.getIndex(MapAgentFrame.SCENARIO_SEL), state
57-
.getIndex(MapAgentFrame.DESTINATION_SEL));
58-
prepareView();
59-
heuristic = createHeuristic(state.getIndex(MapAgentFrame.HEURISTIC_SEL));
60-
search = SearchFactory.getInstance().createSearch(
61-
state.getIndex(MapAgentFrame.SEARCH_SEL),
62-
state.getIndex(MapAgentFrame.Q_SEARCH_IMPL_SEL), heuristic);
63-
isPrepared = true;
64-
}
65-
66-
/**
67-
* Checks whether the current scenario contains an environment which is
68-
* ready for simulation (no agents or not done).
69-
*/
70-
public boolean isPrepared() {
71-
return isPrepared && (scenario.getEnv().getAgents().isEmpty()
72-
|| !scenario.getEnv().isDone());
73-
}
74-
75-
/**
76-
* Calls {@link #initAgents(MessageLogger)} if necessary and
77-
* then starts simulation until done.
78-
*/
79-
public void run(MessageLogger logger) {
80-
logger.log("<simulation-protocol>");
81-
logger.log("search: " + search.getClass().getName());
82-
if (heuristic != null)
83-
logger.log("heuristic: " + heuristic.getClass().getName());
84-
MapEnvironment env = scenario.getEnv();
85-
if (env.getAgents().isEmpty())
86-
initAgents(logger);
87-
try {
88-
while (!env.isDone() && !frame.simulationPaused()) {
89-
Thread.sleep(sleepTime);
90-
env.step();
91-
}
92-
} catch (InterruptedException e) {}
93-
logger.log("</simulation-protocol>\n");
94-
}
95-
96-
/**
97-
* Calls {@link #initAgents(MessageLogger)} if necessary and
98-
* then executes one simulation step.
99-
*/
100-
@Override
101-
public void step(MessageLogger logger) {
102-
MapEnvironment env = scenario.getEnv();
103-
if (env.getAgents().isEmpty())
104-
initAgents(logger);
105-
env.step();
106-
}
107-
108-
/** Updates the status of the frame. */
109-
public void update(SimulationThread simulationThread) {
110-
if (simulationThread.isCanceled()) {
111-
frame.setStatus("Task canceled.");
112-
isPrepared = false;
113-
} else if (frame.simulationPaused()){
114-
frame.setStatus("Task paused.");
115-
} else {
116-
StringBuffer statusMsg = new StringBuffer();
117-
statusMsg.append("Task completed");
118-
List<Agent> agents = scenario.getEnv().getAgents();
119-
if (agents.size() == 1) {
120-
Double travelDistance = scenario.getEnv().getAgentTravelDistance(
121-
agents.get(0));
122-
if (travelDistance != null) {
123-
DecimalFormat f = new DecimalFormat("#0.0");
124-
statusMsg.append("; travel distance: "
125-
+ f.format(travelDistance));
126-
}
127-
}
128-
statusMsg.append(".");
129-
frame.setStatus(statusMsg.toString());
130-
}
131-
}
132-
133-
/////////////////////////////////////////////////////////////////
134-
// abstract methods
135-
136-
/**
137-
* Primitive operation, responsible for assigning values to attributes
138-
* {@link #scenario} and {@link #destinations}.
139-
*/
140-
abstract protected void selectScenarioAndDest(int scenarioIdx, int destIdx);
141-
142-
/**
143-
* Primitive operation, responsible for preparing the view. Scenario and
144-
* destinations are already selected when this method is called.
145-
*/
146-
abstract protected void prepareView();
147-
148-
/**
149-
* Factory method, responsible for creating a heuristic function.
150-
*/
151-
abstract protected AdaptableHeuristicFunction createHeuristic(int heuIdx);
152-
153-
/**
154-
* Primitive operation, responsible for creating new agents and adding
155-
* them to the current environment.
156-
*/
157-
protected abstract void initAgents(MessageLogger logger);
158-
1+
package aima.gui.swing.demo.agent.map;
2+
3+
import java.text.DecimalFormat;
4+
import java.util.List;
5+
6+
import aima.core.agent.Agent;
7+
import aima.core.environment.map.AdaptableHeuristicFunction;
8+
import aima.core.environment.map.MapEnvironment;
9+
import aima.core.environment.map.Scenario;
10+
import aima.gui.swing.framework.AgentAppController;
11+
import aima.gui.swing.framework.MessageLogger;
12+
import aima.gui.swing.framework.SimulationThread;
13+
14+
/**
15+
* Provides a useful base class for agent application controller implementations
16+
* in the context of route finding agent application development. To get it
17+
* ready to work, all you need to do is, to provide implementations for the four
18+
* abstract methods. See {@link RouteFindingAgentApp} for an example.
19+
*
20+
* @author Ruediger Lunde
21+
*/
22+
public abstract class AbstractMapAgentController extends AgentAppController {
23+
/** A scenario. */
24+
protected Scenario scenario;
25+
/**
26+
* Some location names. For route finding problems, only one location
27+
* should be specified.
28+
*/
29+
protected List<String> destinations;
30+
/** Search method to be used. */
31+
protected aima.core.search.framework.Search search;
32+
/** Heuristic function to be used when performing informed search. */
33+
protected AdaptableHeuristicFunction heuristic;
34+
/** Is the scenario up to date? */
35+
protected boolean isPrepared;
36+
/** Sleep time between two steps during simulation in msec. */
37+
protected long sleepTime = 500l;
38+
39+
/** Clears all tracks and prepares simulation if necessary. */
40+
@Override
41+
public void clear() {
42+
((MapAgentView) frame.getEnvView()).clearTracks();
43+
if (!isPrepared())
44+
prepare(null);
45+
}
46+
47+
/**
48+
* Template method, which performs necessary preparations for running the
49+
* agent. The behavior is strongly influenced by the primitive operations
50+
* {@link #selectScenarioAndDest(int, int)}, {@link #prepareView()} and
51+
* {@link #createHeuristic(int)}.
52+
*/
53+
@Override
54+
public void prepare(String changedSelectors) {
55+
MapAgentFrame.SelectionState state = frame.getSelection();
56+
selectScenarioAndDest(state.getIndex(MapAgentFrame.SCENARIO_SEL), state
57+
.getIndex(MapAgentFrame.DESTINATION_SEL));
58+
prepareView();
59+
heuristic = createHeuristic(state.getIndex(MapAgentFrame.HEURISTIC_SEL));
60+
search = SearchFactory.getInstance().createSearch(
61+
state.getIndex(MapAgentFrame.SEARCH_SEL),
62+
state.getIndex(MapAgentFrame.Q_SEARCH_IMPL_SEL), heuristic);
63+
isPrepared = true;
64+
}
65+
66+
/**
67+
* Checks whether the current scenario contains an environment which is
68+
* ready for simulation (no agents or not done).
69+
*/
70+
public boolean isPrepared() {
71+
return isPrepared && (scenario.getEnv().getAgents().isEmpty()
72+
|| !scenario.getEnv().isDone());
73+
}
74+
75+
/**
76+
* Calls {@link #initAgents(MessageLogger)} if necessary and
77+
* then starts simulation until done.
78+
*/
79+
public void run(MessageLogger logger) {
80+
logger.log("<simulation-protocol>");
81+
logger.log("search: " + search.getClass().getName());
82+
if (heuristic != null)
83+
logger.log("heuristic: " + heuristic.getClass().getName());
84+
MapEnvironment env = scenario.getEnv();
85+
if (env.getAgents().isEmpty())
86+
initAgents(logger);
87+
try {
88+
while (!env.isDone() && !frame.simulationPaused()) {
89+
Thread.sleep(sleepTime);
90+
env.step();
91+
}
92+
} catch (InterruptedException e) {}
93+
logger.log("</simulation-protocol>\n");
94+
}
95+
96+
/**
97+
* Calls {@link #initAgents(MessageLogger)} if necessary and
98+
* then executes one simulation step.
99+
*/
100+
@Override
101+
public void step(MessageLogger logger) {
102+
MapEnvironment env = scenario.getEnv();
103+
if (env.getAgents().isEmpty())
104+
initAgents(logger);
105+
env.step();
106+
}
107+
108+
/** Updates the status of the frame. */
109+
public void update(SimulationThread simulationThread) {
110+
if (simulationThread.isCanceled()) {
111+
frame.setStatus("Task canceled.");
112+
isPrepared = false;
113+
} else if (frame.simulationPaused()){
114+
frame.setStatus("Task paused.");
115+
} else {
116+
StringBuffer statusMsg = new StringBuffer();
117+
statusMsg.append("Task completed");
118+
List<Agent> agents = scenario.getEnv().getAgents();
119+
if (agents.size() == 1) {
120+
Double travelDistance = scenario.getEnv().getAgentTravelDistance(
121+
agents.get(0));
122+
if (travelDistance != null) {
123+
DecimalFormat f = new DecimalFormat("#0.0");
124+
statusMsg.append("; travel distance: "
125+
+ f.format(travelDistance));
126+
}
127+
}
128+
statusMsg.append(".");
129+
frame.setStatus(statusMsg.toString());
130+
}
131+
}
132+
133+
/////////////////////////////////////////////////////////////////
134+
// abstract methods
135+
136+
/**
137+
* Primitive operation, responsible for assigning values to attributes
138+
* {@link #scenario} and {@link #destinations}.
139+
*/
140+
abstract protected void selectScenarioAndDest(int scenarioIdx, int destIdx);
141+
142+
/**
143+
* Primitive operation, responsible for preparing the view. Scenario and
144+
* destinations are already selected when this method is called.
145+
*/
146+
abstract protected void prepareView();
147+
148+
/**
149+
* Factory method, responsible for creating a heuristic function.
150+
*/
151+
abstract protected AdaptableHeuristicFunction createHeuristic(int heuIdx);
152+
153+
/**
154+
* Primitive operation, responsible for creating new agents and adding
155+
* them to the current environment.
156+
*/
157+
protected abstract void initAgents(MessageLogger logger);
158+
159159
}

0 commit comments

Comments
 (0)