|
1 | | -package aimax.osm.applications; |
2 | | - |
3 | | -import java.util.HashSet; |
4 | | -import java.util.List; |
5 | | - |
6 | | -import aima.core.agent.Agent; |
7 | | -import aima.core.environment.map.BidirectionalMapProblem; |
8 | | -import aima.core.environment.map.MapAgent; |
9 | | -import aima.core.environment.map.MapFunctionFactory; |
10 | | -import aima.core.search.framework.Node; |
11 | | -import aima.core.search.framework.NodeExpander.NodeListener; |
12 | | -import aima.core.search.framework.problem.Problem; |
13 | | -import aima.core.search.online.LRTAStarAgent; |
14 | | -import aima.core.search.online.OnlineSearchProblem; |
15 | | -import aima.core.util.datastructure.Point2D; |
16 | | -import aima.gui.swing.demo.agent.map.MapAgentFrame; |
17 | | -import aima.gui.swing.framework.AgentAppController; |
18 | | -import aima.gui.swing.framework.AgentAppFrame; |
19 | | -import aima.gui.swing.framework.MessageLogger; |
20 | | -import aimax.osm.data.DataResource; |
21 | | -import aimax.osm.data.entities.MapNode; |
22 | | -import aimax.osm.data.entities.MapWay; |
23 | | -import aimax.osm.routing.agent.MapAdapter; |
24 | | -import aimax.osm.routing.agent.OsmAgentController; |
25 | | -import aimax.osm.routing.agent.OsmAgentFrame; |
26 | | -import aimax.osm.routing.agent.OsmAgentView; |
27 | | -import aimax.osm.viewer.DefaultEntityRenderer; |
28 | | -import aimax.osm.viewer.DefaultEntityViewInfo; |
29 | | -import aimax.osm.viewer.MapStyleFactory; |
30 | | -import aimax.osm.viewer.UColor; |
31 | | - |
32 | | -/** |
33 | | - * This application demonstrates, how different search strategies explore the |
34 | | - * state space. All locations which correspond to expanded nodes are highlighted |
35 | | - * in the map. If you don't see any expanded node, just zoom in! |
36 | | - * |
37 | | - * @author Ruediger Lunde |
38 | | - */ |
39 | | -public class SearchDemoOsmAgentApp extends OsmAgentApp { |
40 | | - |
41 | | - /** |
42 | | - * Stores those states (Strings with map node ids), whose corresponding |
43 | | - * search nodes have been expanded during the last search. Quick and dirty |
44 | | - * solution... |
45 | | - */ |
46 | | - static final HashSet<Object> visitedStates = new HashSet<Object>(); |
47 | | - |
48 | | - /** Creates an <code>OsmAgentView</code>. */ |
49 | | - @Override |
50 | | - public OsmAgentView createEnvironmentView() { |
51 | | - OsmAgentView result = super.createEnvironmentView(); |
52 | | - result.getMapViewPane().setRenderer(new SDMapEntityRenderer()); |
53 | | - return result; |
54 | | - } |
55 | | - |
56 | | - @Override |
57 | | - public AgentAppFrame createFrame() { |
58 | | - return new SDFrame(); |
59 | | - } |
60 | | - |
61 | | - @Override |
62 | | - public AgentAppController createController() { |
63 | | - return new SDController(map); |
64 | | - } |
65 | | - |
66 | | - // /////////////////////////////////////////////////////////////// |
67 | | - // inner classes |
68 | | - |
69 | | - /** Variant of the <code>OsmAgentFrame</code>. */ |
70 | | - private class SDFrame extends OsmAgentFrame { |
71 | | - |
72 | | - private static final long serialVersionUID = 1L; |
73 | | - |
74 | | - SDFrame() { |
75 | | - setTitle("OSDA - the OSM Search Demo Agent Application"); |
76 | | - // this.setSelectorItems(AGENT_SEL, new String[]{}, -1); |
77 | | - } |
78 | | - } |
79 | | - |
80 | | - /** |
81 | | - * Variant of the <code>OsmAgentController</code> which starts an agent with |
82 | | - * a slightly modified goal test function. |
83 | | - */ |
84 | | - private static class SDController extends OsmAgentController { |
85 | | - SDController(MapAdapter map) { |
86 | | - super(map); |
87 | | - } |
88 | | - |
89 | | - @Override |
90 | | - public void prepare(String changedSelector) { |
91 | | - super.prepare(changedSelector); |
92 | | - visitedStates.clear(); |
93 | | - search.getNodeExpander().addNodeListener(new NodeListener() { |
94 | | - @Override |
95 | | - public void onNodeExpanded(Node node) { |
96 | | - visitedStates.add(node.getState()); |
97 | | - } |
98 | | - }); |
99 | | - } |
100 | | - |
101 | | - /** Creates new agents and adds them to the current environment. */ |
102 | | - protected void initAgents(MessageLogger logger) { |
103 | | - List<MapNode> markers = map.getOsmMap().getMarkers(); |
104 | | - if (markers.size() < 2) { |
105 | | - logger.log("Error: Please set two markers with mouse-left."); |
106 | | - return; |
107 | | - } |
108 | | - visitedStates.clear(); |
109 | | - String[] locs = new String[markers.size()]; |
110 | | - for (int i = 0; i < markers.size(); i++) { |
111 | | - MapNode node = markers.get(i); |
112 | | - Point2D pt = new Point2D(node.getLon(), node.getLat()); |
113 | | - locs[i] = map.getNearestLocation(pt); |
114 | | - } |
115 | | - heuristic.adaptToGoal(locs[1], map); |
116 | | - Agent agent = null; |
117 | | - MapAgentFrame.SelectionState state = frame.getSelection(); |
118 | | - switch (state.getIndex(MapAgentFrame.AGENT_SEL)) { |
119 | | - case 0: |
120 | | - agent = new MapAgent(map, env, search, new String[] { locs[1] }); |
121 | | - break; |
122 | | - case 1: |
123 | | - Problem p = new BidirectionalMapProblem(map, null, locs[1]); |
124 | | - OnlineSearchProblem osp = new OnlineSearchProblem(p.getActionsFunction(), p.getGoalTest(), |
125 | | - p.getStepCostFunction()); |
126 | | - agent = new LRTAStarAgent(osp, MapFunctionFactory.getPerceptToStateFunction(), heuristic); |
127 | | - break; |
128 | | - } |
129 | | - env.addAgent(agent, locs[0]); |
130 | | - } |
131 | | - } |
132 | | - |
133 | | - /** |
134 | | - * Variant of the <code>DefaultMapEntityRenderer</code> which highlights way |
135 | | - * nodes mentioned in {@link SearchDemoOsmAgentApp#visitedStates}. |
136 | | - */ |
137 | | - private static class SDMapEntityRenderer extends DefaultEntityRenderer { |
138 | | - DefaultEntityViewInfo highlightProp = new MapStyleFactory().createPoiInfo(0, 0, 5, UColor.GREEN, |
139 | | - MapStyleFactory.createRectangle(4, UColor.GREEN), false); |
140 | | - |
141 | | - @Override |
142 | | - public void printWay(MapWay way, DefaultEntityViewInfo eprop, boolean asArea) { |
143 | | - super.printWay(way, eprop, asArea); |
144 | | - if (scale >= highlightProp.minVisibleScale * displayFactor) { |
145 | | - for (MapNode node : getWayNodes(way)) |
146 | | - if (visitedStates.contains(Long.toString(node.getId()))) |
147 | | - printPoint(imageBdr, node, highlightProp, null); |
148 | | - // highlight(node); |
149 | | - } |
150 | | - } |
151 | | - |
152 | | - // private void highlight(MapNode node) { |
153 | | - // int offs = (int) displayFactor; |
154 | | - // int x = transformer.x(node.getLon()) - offs; |
155 | | - // int y = transformer.y(node.getLat()) - offs; |
156 | | - // g2.setColor(Color.YELLOW); |
157 | | - // g2.setStroke(new BasicStroke(1f)); |
158 | | - // g2.fillRect(x, y, 2*offs, 2*offs); |
159 | | - // } |
160 | | - |
161 | | - } |
162 | | - |
163 | | - // /////////////////////////////////////////////////////////////// |
164 | | - // starter method |
165 | | - |
166 | | - /** Application starter. */ |
167 | | - public static void main(String args[]) { |
168 | | - // Logger.getLogger("aimax.osm").setLevel(Level.FINEST); |
169 | | - // Logger.getLogger("").getHandlers()[0].setLevel(Level.FINE); |
170 | | - |
171 | | - SearchDemoOsmAgentApp demo = new SearchDemoOsmAgentApp(); |
172 | | - demo.readMap(DataResource.getULMFileResource()); |
173 | | - demo.startApplication(); |
174 | | - } |
175 | | -} |
| 1 | +package aimax.osm.applications; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import aima.core.agent.Agent; |
| 7 | +import aima.core.environment.map.BidirectionalMapProblem; |
| 8 | +import aima.core.environment.map.MapAgent; |
| 9 | +import aima.core.environment.map.MapFunctionFactory; |
| 10 | +import aima.core.search.framework.Node; |
| 11 | +import aima.core.search.framework.NodeExpander.NodeListener; |
| 12 | +import aima.core.search.framework.problem.Problem; |
| 13 | +import aima.core.search.online.LRTAStarAgent; |
| 14 | +import aima.core.search.online.OnlineSearchProblem; |
| 15 | +import aima.core.util.math.geom.shapes.Point2D; |
| 16 | +import aima.gui.swing.demo.agent.map.MapAgentFrame; |
| 17 | +import aima.gui.swing.framework.AgentAppController; |
| 18 | +import aima.gui.swing.framework.AgentAppFrame; |
| 19 | +import aima.gui.swing.framework.MessageLogger; |
| 20 | +import aimax.osm.data.DataResource; |
| 21 | +import aimax.osm.data.entities.MapNode; |
| 22 | +import aimax.osm.data.entities.MapWay; |
| 23 | +import aimax.osm.routing.agent.MapAdapter; |
| 24 | +import aimax.osm.routing.agent.OsmAgentController; |
| 25 | +import aimax.osm.routing.agent.OsmAgentFrame; |
| 26 | +import aimax.osm.routing.agent.OsmAgentView; |
| 27 | +import aimax.osm.viewer.DefaultEntityRenderer; |
| 28 | +import aimax.osm.viewer.DefaultEntityViewInfo; |
| 29 | +import aimax.osm.viewer.MapStyleFactory; |
| 30 | +import aimax.osm.viewer.UColor; |
| 31 | + |
| 32 | +/** |
| 33 | + * This application demonstrates, how different search strategies explore the |
| 34 | + * state space. All locations which correspond to expanded nodes are highlighted |
| 35 | + * in the map. If you don't see any expanded node, just zoom in! |
| 36 | + * |
| 37 | + * @author Ruediger Lunde |
| 38 | + */ |
| 39 | +public class SearchDemoOsmAgentApp extends OsmAgentApp { |
| 40 | + |
| 41 | + /** |
| 42 | + * Stores those states (Strings with map node ids), whose corresponding |
| 43 | + * search nodes have been expanded during the last search. Quick and dirty |
| 44 | + * solution... |
| 45 | + */ |
| 46 | + static final HashSet<Object> visitedStates = new HashSet<Object>(); |
| 47 | + |
| 48 | + /** Creates an <code>OsmAgentView</code>. */ |
| 49 | + @Override |
| 50 | + public OsmAgentView createEnvironmentView() { |
| 51 | + OsmAgentView result = super.createEnvironmentView(); |
| 52 | + result.getMapViewPane().setRenderer(new SDMapEntityRenderer()); |
| 53 | + return result; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public AgentAppFrame createFrame() { |
| 58 | + return new SDFrame(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public AgentAppController createController() { |
| 63 | + return new SDController(map); |
| 64 | + } |
| 65 | + |
| 66 | + // /////////////////////////////////////////////////////////////// |
| 67 | + // inner classes |
| 68 | + |
| 69 | + /** Variant of the <code>OsmAgentFrame</code>. */ |
| 70 | + private class SDFrame extends OsmAgentFrame { |
| 71 | + |
| 72 | + private static final long serialVersionUID = 1L; |
| 73 | + |
| 74 | + SDFrame() { |
| 75 | + setTitle("OSDA - the OSM Search Demo Agent Application"); |
| 76 | + // this.setSelectorItems(AGENT_SEL, new String[]{}, -1); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Variant of the <code>OsmAgentController</code> which starts an agent with |
| 82 | + * a slightly modified goal test function. |
| 83 | + */ |
| 84 | + private static class SDController extends OsmAgentController { |
| 85 | + SDController(MapAdapter map) { |
| 86 | + super(map); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void prepare(String changedSelector) { |
| 91 | + super.prepare(changedSelector); |
| 92 | + visitedStates.clear(); |
| 93 | + search.getNodeExpander().addNodeListener(new NodeListener() { |
| 94 | + @Override |
| 95 | + public void onNodeExpanded(Node node) { |
| 96 | + visitedStates.add(node.getState()); |
| 97 | + } |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + /** Creates new agents and adds them to the current environment. */ |
| 102 | + protected void initAgents(MessageLogger logger) { |
| 103 | + List<MapNode> markers = map.getOsmMap().getMarkers(); |
| 104 | + if (markers.size() < 2) { |
| 105 | + logger.log("Error: Please set two markers with mouse-left."); |
| 106 | + return; |
| 107 | + } |
| 108 | + visitedStates.clear(); |
| 109 | + String[] locs = new String[markers.size()]; |
| 110 | + for (int i = 0; i < markers.size(); i++) { |
| 111 | + MapNode node = markers.get(i); |
| 112 | + Point2D pt = new Point2D(node.getLon(), node.getLat()); |
| 113 | + locs[i] = map.getNearestLocation(pt); |
| 114 | + } |
| 115 | + heuristic.adaptToGoal(locs[1], map); |
| 116 | + Agent agent = null; |
| 117 | + MapAgentFrame.SelectionState state = frame.getSelection(); |
| 118 | + switch (state.getIndex(MapAgentFrame.AGENT_SEL)) { |
| 119 | + case 0: |
| 120 | + agent = new MapAgent(map, env, search, new String[] { locs[1] }); |
| 121 | + break; |
| 122 | + case 1: |
| 123 | + Problem p = new BidirectionalMapProblem(map, null, locs[1]); |
| 124 | + OnlineSearchProblem osp = new OnlineSearchProblem(p.getActionsFunction(), p.getGoalTest(), |
| 125 | + p.getStepCostFunction()); |
| 126 | + agent = new LRTAStarAgent(osp, MapFunctionFactory.getPerceptToStateFunction(), heuristic); |
| 127 | + break; |
| 128 | + } |
| 129 | + env.addAgent(agent, locs[0]); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Variant of the <code>DefaultMapEntityRenderer</code> which highlights way |
| 135 | + * nodes mentioned in {@link SearchDemoOsmAgentApp#visitedStates}. |
| 136 | + */ |
| 137 | + private static class SDMapEntityRenderer extends DefaultEntityRenderer { |
| 138 | + DefaultEntityViewInfo highlightProp = new MapStyleFactory().createPoiInfo(0, 0, 5, UColor.GREEN, |
| 139 | + MapStyleFactory.createRectangle(4, UColor.GREEN), false); |
| 140 | + |
| 141 | + @Override |
| 142 | + public void printWay(MapWay way, DefaultEntityViewInfo eprop, boolean asArea) { |
| 143 | + super.printWay(way, eprop, asArea); |
| 144 | + if (scale >= highlightProp.minVisibleScale * displayFactor) { |
| 145 | + for (MapNode node : getWayNodes(way)) |
| 146 | + if (visitedStates.contains(Long.toString(node.getId()))) |
| 147 | + printPoint(imageBdr, node, highlightProp, null); |
| 148 | + // highlight(node); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // private void highlight(MapNode node) { |
| 153 | + // int offs = (int) displayFactor; |
| 154 | + // int x = transformer.x(node.getLon()) - offs; |
| 155 | + // int y = transformer.y(node.getLat()) - offs; |
| 156 | + // g2.setColor(Color.YELLOW); |
| 157 | + // g2.setStroke(new BasicStroke(1f)); |
| 158 | + // g2.fillRect(x, y, 2*offs, 2*offs); |
| 159 | + // } |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + // /////////////////////////////////////////////////////////////// |
| 164 | + // starter method |
| 165 | + |
| 166 | + /** Application starter. */ |
| 167 | + public static void main(String args[]) { |
| 168 | + // Logger.getLogger("aimax.osm").setLevel(Level.FINEST); |
| 169 | + // Logger.getLogger("").getHandlers()[0].setLevel(Level.FINE); |
| 170 | + |
| 171 | + SearchDemoOsmAgentApp demo = new SearchDemoOsmAgentApp(); |
| 172 | + demo.readMap(DataResource.getULMFileResource()); |
| 173 | + demo.startApplication(); |
| 174 | + } |
| 175 | +} |
0 commit comments