|
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 | +} |
0 commit comments