Skip to content

Commit a11804c

Browse files
committed
FX framework and applications improved.
1 parent 4eeeeba commit a11804c

File tree

6 files changed

+457
-370
lines changed

6 files changed

+457
-370
lines changed
Lines changed: 112 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,112 @@
1-
package aima.core.environment.map;
2-
3-
import java.util.ArrayList;
4-
import java.util.List;
5-
import java.util.Set;
6-
7-
import aima.core.agent.Action;
8-
import aima.core.agent.EnvironmentViewNotifier;
9-
import aima.core.agent.Percept;
10-
import aima.core.agent.State;
11-
import aima.core.agent.impl.DynamicPercept;
12-
import aima.core.agent.impl.DynamicState;
13-
import aima.core.search.framework.Search;
14-
import aima.core.search.framework.SimpleProblemSolvingAgent;
15-
import aima.core.search.framework.problem.Problem;
16-
17-
/**
18-
* @author Ciaran O'Reilly
19-
*
20-
*/
21-
public class MapAgent extends SimpleProblemSolvingAgent {
22-
23-
protected Map map = null;
24-
protected DynamicState state = new DynamicState();
25-
26-
// possibly null...
27-
private EnvironmentViewNotifier notifier = null;
28-
private Search search = null;
29-
private String[] goals = null;
30-
private int goalTestPos = 0;
31-
32-
public MapAgent(Map map, EnvironmentViewNotifier notifier, Search search) {
33-
this.map = map;
34-
this.notifier = notifier;
35-
this.search = search;
36-
}
37-
38-
public MapAgent(Map map, EnvironmentViewNotifier notifier, Search search, int maxGoalsToFormulate) {
39-
super(maxGoalsToFormulate);
40-
this.map = map;
41-
this.notifier = notifier;
42-
this.search = search;
43-
}
44-
45-
public MapAgent(Map map, EnvironmentViewNotifier notifier, Search search, String[] goals) {
46-
this(map, search, goals);
47-
this.notifier = notifier;
48-
}
49-
50-
public MapAgent(Map map, Search search, String[] goals) {
51-
super(goals.length);
52-
this.map = map;
53-
this.search = search;
54-
this.goals = new String[goals.length];
55-
System.arraycopy(goals, 0, this.goals, 0, goals.length);
56-
}
57-
58-
//
59-
// PROTECTED METHODS
60-
//
61-
@Override
62-
protected State updateState(Percept p) {
63-
DynamicPercept dp = (DynamicPercept) p;
64-
65-
state.setAttribute(DynAttributeNames.AGENT_LOCATION, dp.getAttribute(DynAttributeNames.PERCEPT_IN));
66-
67-
return state;
68-
}
69-
70-
@Override
71-
protected Object formulateGoal() {
72-
Object goal = null;
73-
if (null == goals) {
74-
goal = map.randomlyGenerateDestination();
75-
} else {
76-
goal = goals[goalTestPos];
77-
goalTestPos++;
78-
}
79-
if (notifier != null)
80-
notifier.notifyViews("CurrentLocation=In(" + state.getAttribute(DynAttributeNames.AGENT_LOCATION)
81-
+ "), Goal=In(" + goal + ")");
82-
83-
return goal;
84-
}
85-
86-
@Override
87-
protected Problem formulateProblem(Object goal) {
88-
return new BidirectionalMapProblem(map, (String) state.getAttribute(DynAttributeNames.AGENT_LOCATION),
89-
(String) goal);
90-
}
91-
92-
@Override
93-
protected List<Action> search(Problem problem) {
94-
List<Action> actions = new ArrayList<Action>();
95-
try {
96-
List<Action> sactions = search.search(problem);
97-
for (Action action : sactions) {
98-
actions.add(action);
99-
}
100-
} catch (Exception ex) {
101-
ex.printStackTrace();
102-
}
103-
return actions;
104-
}
105-
106-
@Override
107-
protected void notifyViewOfMetrics() {
108-
if (notifier != null) {
109-
Set<String> keys = search.getMetrics().keySet();
110-
for (String key : keys) {
111-
notifier.notifyViews("METRIC[" + key + "]=" + search.getMetrics().get(key));
112-
}
113-
}
114-
}
115-
}
1+
package aima.core.environment.map;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Set;
6+
7+
import aima.core.agent.Action;
8+
import aima.core.agent.EnvironmentViewNotifier;
9+
import aima.core.agent.Percept;
10+
import aima.core.agent.State;
11+
import aima.core.agent.impl.DynamicPercept;
12+
import aima.core.agent.impl.DynamicState;
13+
import aima.core.search.framework.Search;
14+
import aima.core.search.framework.SimpleProblemSolvingAgent;
15+
import aima.core.search.framework.problem.Problem;
16+
17+
/**
18+
* @author Ciaran O'Reilly
19+
*
20+
*/
21+
public class MapAgent extends SimpleProblemSolvingAgent {
22+
23+
protected Map map = null;
24+
protected DynamicState state = new DynamicState();
25+
26+
// possibly null...
27+
private EnvironmentViewNotifier notifier = null;
28+
private Search search = null;
29+
private String[] goals = null;
30+
private int goalTestPos = 0;
31+
32+
public MapAgent(Map map, EnvironmentViewNotifier notifier, Search search) {
33+
this.map = map;
34+
this.notifier = notifier;
35+
this.search = search;
36+
}
37+
38+
public MapAgent(Map map, EnvironmentViewNotifier notifier, Search search, int maxGoalsToFormulate) {
39+
super(maxGoalsToFormulate);
40+
this.map = map;
41+
this.notifier = notifier;
42+
this.search = search;
43+
}
44+
45+
public MapAgent(Map map, EnvironmentViewNotifier notifier, Search search, String[] goals) {
46+
this(map, search, goals);
47+
this.notifier = notifier;
48+
}
49+
50+
public MapAgent(Map map, Search search, String[] goals) {
51+
super(goals.length);
52+
this.map = map;
53+
this.search = search;
54+
this.goals = new String[goals.length];
55+
System.arraycopy(goals, 0, this.goals, 0, goals.length);
56+
}
57+
58+
//
59+
// PROTECTED METHODS
60+
//
61+
@Override
62+
protected State updateState(Percept p) {
63+
DynamicPercept dp = (DynamicPercept) p;
64+
65+
state.setAttribute(DynAttributeNames.AGENT_LOCATION, dp.getAttribute(DynAttributeNames.PERCEPT_IN));
66+
67+
return state;
68+
}
69+
70+
@Override
71+
protected Object formulateGoal() {
72+
Object goal = null;
73+
if (null == goals) {
74+
goal = map.randomlyGenerateDestination();
75+
} else {
76+
goal = goals[goalTestPos];
77+
goalTestPos++;
78+
}
79+
if (notifier != null)
80+
notifier.notifyViews("CurrentLocation=In(" + state.getAttribute(DynAttributeNames.AGENT_LOCATION)
81+
+ "), Goal=In(" + goal + ")");
82+
83+
return goal;
84+
}
85+
86+
@Override
87+
protected Problem formulateProblem(Object goal) {
88+
return new BidirectionalMapProblem(map, (String) state.getAttribute(DynAttributeNames.AGENT_LOCATION),
89+
(String) goal);
90+
}
91+
92+
@Override
93+
protected List<Action> search(Problem problem) {
94+
List<Action> result = new ArrayList<Action>();
95+
try {
96+
result.addAll(search.search(problem));
97+
} catch (Exception ex) {
98+
ex.printStackTrace();
99+
}
100+
return result;
101+
}
102+
103+
@Override
104+
protected void notifyViewOfMetrics() {
105+
if (notifier != null) {
106+
Set<String> keys = search.getMetrics().keySet();
107+
for (String key : keys) {
108+
notifier.notifyViews("METRIC[" + key + "]=" + search.getMetrics().get(key));
109+
}
110+
}
111+
}
112+
}

aima-gui/src/main/java/aima/gui/fx/framework/SimulationPaneCtrl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public SimulationPaneCtrl(List<Parameter> params, List<ComboBox<String>> paramCo
6666
setState(State.READY);
6767
}
6868

69+
public Optional<Parameter> getParam(String paramName) {
70+
return Parameter.find(params, paramName);
71+
}
72+
6973
public int getParamValueIndex(String paramName) {
7074
int valIdx = -1;
7175
int paramIdx = Parameter.indexOf(params, paramName);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import aimax.osm.gui.fx.applications.OsmLRTAStarAgentApp;
66
import aimax.osm.gui.fx.applications.OsmRouteFindingAgentApp;
77
import aimax.osm.gui.fx.applications.OsmRoutePlannerApp;
8+
import aimax.osm.gui.fx.applications.SimpleOsmAgentApp;
89
import javafx.application.Application;
910
import javafx.scene.Scene;
1011
import javafx.scene.layout.BorderPane;
@@ -38,6 +39,7 @@ public static void defineContent(IntegratedAppPaneBuilder builder) {
3839
IntegratedAimaFxApp.defineContent(builder);
3940

4041
builder.registerApp(OsmRoutePlannerApp.class);
42+
builder.registerApp(SimpleOsmAgentApp.class);
4143
builder.registerApp(OsmRouteFindingAgentApp.class);
4244
builder.registerApp(OsmLRTAStarAgentApp.class);
4345
}

0 commit comments

Comments
 (0)