|
1 | | -package aima.gui.swing.demo.agent; |
2 | | - |
3 | | -import aima.core.agent.impl.AbstractAgent; |
4 | | -import aima.core.environment.vacuum.FullyObservableVacuumEnvironmentPerceptToStateFunction; |
5 | | -import aima.core.environment.vacuum.ModelBasedReflexVacuumAgent; |
6 | | -import aima.core.environment.vacuum.NondeterministicVacuumAgent; |
7 | | -import aima.core.environment.vacuum.NondeterministicVacuumEnvironment; |
8 | | -import aima.core.environment.vacuum.ReflexVacuumAgent; |
9 | | -import aima.core.environment.vacuum.SimpleReflexVacuumAgent; |
10 | | -import aima.core.environment.vacuum.TableDrivenVacuumAgent; |
11 | | -import aima.core.environment.vacuum.VacuumEnvironment; |
12 | | -import aima.core.environment.vacuum.VacuumWorldActions; |
13 | | -import aima.core.environment.vacuum.VacuumWorldGoalTest; |
14 | | -import aima.core.environment.vacuum.VacuumWorldResults; |
15 | | -import aima.core.search.framework.problem.DefaultStepCostFunction; |
16 | | -import aima.core.search.nondeterministic.NondeterministicProblem; |
17 | | -import aima.gui.swing.framework.AgentAppController; |
18 | | -import aima.gui.swing.framework.AgentAppFrame; |
19 | | -import aima.gui.swing.framework.MessageLogger; |
20 | | -import aima.gui.swing.framework.SimulationThread; |
21 | | - |
22 | | -/** |
23 | | - * Defines how to react on user button events. |
24 | | - * |
25 | | - * @author Ruediger Lunde |
26 | | - */ |
27 | | -public class VacuumController extends AgentAppController { |
28 | | - |
29 | | - protected VacuumEnvironment env = null; |
30 | | - protected AbstractAgent agent = null; |
31 | | - protected boolean isPrepared = false; |
32 | | - |
33 | | - /** Prepares next simulation if that makes sense. */ |
34 | | - @Override |
35 | | - public void clear() { |
36 | | - if (!isPrepared()) |
37 | | - prepare(null); |
38 | | - } |
39 | | - |
40 | | - /** |
41 | | - * Creates a vacuum environment and a corresponding agent based on the |
42 | | - * state of the selectors and finally passes the environment to the viewer. |
43 | | - */ |
44 | | - @Override |
45 | | - public void prepare(String changedSelector) { |
46 | | - AgentAppFrame.SelectionState selState = frame.getSelection(); |
47 | | - env = null; |
48 | | - switch (selState.getIndex(VacuumFrame.ENV_SEL)) { |
49 | | - case 0: |
50 | | - env = new VacuumEnvironment(); |
51 | | - break; |
52 | | - case 1: |
53 | | - env = new NondeterministicVacuumEnvironment(); |
54 | | - break; |
55 | | - } |
56 | | - agent = null; |
57 | | - switch (selState.getIndex(VacuumFrame.AGENT_SEL)) { |
58 | | - case 0: |
59 | | - agent = new TableDrivenVacuumAgent(); |
60 | | - break; |
61 | | - case 1: |
62 | | - agent = new ReflexVacuumAgent(); |
63 | | - break; |
64 | | - case 2: |
65 | | - agent = new SimpleReflexVacuumAgent(); |
66 | | - break; |
67 | | - case 3: |
68 | | - agent = new ModelBasedReflexVacuumAgent(); |
69 | | - break; |
70 | | - case 4: |
71 | | - agent = createNondeterministicVacuumAgent(); |
72 | | - break; |
73 | | - } |
74 | | - if (env != null && agent != null) { |
75 | | - frame.getEnvView().setEnvironment(env); |
76 | | - env.addAgent(agent); |
77 | | - if (agent instanceof NondeterministicVacuumAgent) { |
78 | | - // Set the problem now for this kind of agent |
79 | | - // set the problem and agent |
80 | | - ((NondeterministicVacuumAgent)agent).setProblem(createNondeterministicProblem()); |
81 | | - } |
82 | | - isPrepared = true; |
83 | | - } |
84 | | - } |
85 | | - |
86 | | - /** Checks whether simulation can be started. */ |
87 | | - @Override |
88 | | - public boolean isPrepared() { |
89 | | - return isPrepared && !env.isDone(); |
90 | | - } |
91 | | - |
92 | | - /** Starts simulation. */ |
93 | | - @Override |
94 | | - public void run(MessageLogger logger) { |
95 | | - logger.log("<simulation-log>"); |
96 | | - try { |
97 | | - while (!env.isDone() && !frame.simulationPaused()) { |
98 | | - Thread.sleep(500); |
99 | | - env.step(); |
100 | | - } |
101 | | - } catch (InterruptedException e) {} |
102 | | - logger.log("Performance: " |
103 | | - + env.getPerformanceMeasure(agent)); |
104 | | - logger.log("</simulation-log>\n"); |
105 | | - } |
106 | | - |
107 | | - /** Executes one simulation step. */ |
108 | | - @Override |
109 | | - public void step(MessageLogger logger) { |
110 | | - env.step(); |
111 | | - } |
112 | | - |
113 | | - /** Updates the status of the frame after simulation has finished. */ |
114 | | - public void update(SimulationThread simulationThread) { |
115 | | - if (simulationThread.isCanceled()) { |
116 | | - frame.setStatus("Task canceled."); |
117 | | - isPrepared = false; |
118 | | - } else if (frame.simulationPaused()){ |
119 | | - frame.setStatus("Task paused."); |
120 | | - } else { |
121 | | - frame.setStatus("Task completed."); |
122 | | - } |
123 | | - } |
124 | | - |
125 | | - // |
126 | | - // PRIVATE METHODS |
127 | | - // |
128 | | - private NondeterministicVacuumAgent createNondeterministicVacuumAgent() { |
129 | | - NondeterministicVacuumAgent agent = new NondeterministicVacuumAgent( |
130 | | - new FullyObservableVacuumEnvironmentPerceptToStateFunction()); |
131 | | - |
132 | | - return agent; |
133 | | - } |
134 | | - |
135 | | - private NondeterministicProblem createNondeterministicProblem() { |
136 | | - // create problem |
137 | | - NondeterministicProblem problem = new NondeterministicProblem( |
138 | | - env.getCurrentState(), |
139 | | - new VacuumWorldActions(), |
140 | | - new VacuumWorldResults(agent), |
141 | | - new VacuumWorldGoalTest(agent), |
142 | | - new DefaultStepCostFunction()); |
143 | | - |
144 | | - return problem; |
145 | | - } |
146 | | -} |
147 | | - |
| 1 | +package aima.gui.swing.demo.agent; |
| 2 | + |
| 3 | +import aima.core.agent.impl.AbstractAgent; |
| 4 | +import aima.core.environment.vacuum.FullyObservableVacuumEnvironmentPerceptToStateFunction; |
| 5 | +import aima.core.environment.vacuum.ModelBasedReflexVacuumAgent; |
| 6 | +import aima.core.environment.vacuum.NondeterministicVacuumAgent; |
| 7 | +import aima.core.environment.vacuum.NondeterministicVacuumEnvironment; |
| 8 | +import aima.core.environment.vacuum.ReflexVacuumAgent; |
| 9 | +import aima.core.environment.vacuum.SimpleReflexVacuumAgent; |
| 10 | +import aima.core.environment.vacuum.TableDrivenVacuumAgent; |
| 11 | +import aima.core.environment.vacuum.VacuumEnvironment; |
| 12 | +import aima.core.environment.vacuum.VacuumWorldActions; |
| 13 | +import aima.core.environment.vacuum.VacuumWorldGoalTest; |
| 14 | +import aima.core.environment.vacuum.VacuumWorldResults; |
| 15 | +import aima.core.search.framework.problem.DefaultStepCostFunction; |
| 16 | +import aima.core.search.nondeterministic.NondeterministicProblem; |
| 17 | +import aima.gui.swing.framework.AgentAppController; |
| 18 | +import aima.gui.swing.framework.AgentAppFrame; |
| 19 | +import aima.gui.swing.framework.MessageLogger; |
| 20 | +import aima.gui.swing.framework.SimulationThread; |
| 21 | + |
| 22 | +/** |
| 23 | + * Defines how to react on user button events. |
| 24 | + * |
| 25 | + * @author Ruediger Lunde |
| 26 | + */ |
| 27 | +public class VacuumController extends AgentAppController { |
| 28 | + |
| 29 | + protected VacuumEnvironment env = null; |
| 30 | + protected AbstractAgent agent = null; |
| 31 | + protected boolean isPrepared = false; |
| 32 | + |
| 33 | + /** Prepares next simulation if that makes sense. */ |
| 34 | + @Override |
| 35 | + public void clear() { |
| 36 | + if (!isPrepared()) |
| 37 | + prepare(null); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates a vacuum environment and a corresponding agent based on the |
| 42 | + * state of the selectors and finally passes the environment to the viewer. |
| 43 | + */ |
| 44 | + @Override |
| 45 | + public void prepare(String changedSelector) { |
| 46 | + AgentAppFrame.SelectionState selState = frame.getSelection(); |
| 47 | + env = null; |
| 48 | + switch (selState.getIndex(VacuumFrame.ENV_SEL)) { |
| 49 | + case 0: |
| 50 | + env = new VacuumEnvironment(); |
| 51 | + break; |
| 52 | + case 1: |
| 53 | + env = new NondeterministicVacuumEnvironment(); |
| 54 | + break; |
| 55 | + } |
| 56 | + agent = null; |
| 57 | + switch (selState.getIndex(VacuumFrame.AGENT_SEL)) { |
| 58 | + case 0: |
| 59 | + agent = new TableDrivenVacuumAgent(); |
| 60 | + break; |
| 61 | + case 1: |
| 62 | + agent = new ReflexVacuumAgent(); |
| 63 | + break; |
| 64 | + case 2: |
| 65 | + agent = new SimpleReflexVacuumAgent(); |
| 66 | + break; |
| 67 | + case 3: |
| 68 | + agent = new ModelBasedReflexVacuumAgent(); |
| 69 | + break; |
| 70 | + case 4: |
| 71 | + agent = createNondeterministicVacuumAgent(); |
| 72 | + break; |
| 73 | + } |
| 74 | + if (env != null && agent != null) { |
| 75 | + frame.getEnvView().setEnvironment(env); |
| 76 | + env.addAgent(agent); |
| 77 | + if (agent instanceof NondeterministicVacuumAgent) { |
| 78 | + // Set the problem now for this kind of agent |
| 79 | + // set the problem and agent |
| 80 | + ((NondeterministicVacuumAgent)agent).setProblem(createNondeterministicProblem()); |
| 81 | + } |
| 82 | + isPrepared = true; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** Checks whether simulation can be started. */ |
| 87 | + @Override |
| 88 | + public boolean isPrepared() { |
| 89 | + return isPrepared && !env.isDone(); |
| 90 | + } |
| 91 | + |
| 92 | + /** Starts simulation. */ |
| 93 | + @Override |
| 94 | + public void run(MessageLogger logger) { |
| 95 | + logger.log("<simulation-log>"); |
| 96 | + try { |
| 97 | + while (!env.isDone() && !frame.simulationPaused()) { |
| 98 | + Thread.sleep(500); |
| 99 | + env.step(); |
| 100 | + } |
| 101 | + } catch (InterruptedException e) {} |
| 102 | + logger.log("Performance: " |
| 103 | + + env.getPerformanceMeasure(agent)); |
| 104 | + logger.log("</simulation-log>\n"); |
| 105 | + } |
| 106 | + |
| 107 | + /** Executes one simulation step. */ |
| 108 | + @Override |
| 109 | + public void step(MessageLogger logger) { |
| 110 | + env.step(); |
| 111 | + } |
| 112 | + |
| 113 | + /** Updates the status of the frame after simulation has finished. */ |
| 114 | + public void update(SimulationThread simulationThread) { |
| 115 | + if (simulationThread.isCanceled()) { |
| 116 | + frame.setStatus("Task canceled."); |
| 117 | + isPrepared = false; |
| 118 | + } else if (frame.simulationPaused()){ |
| 119 | + frame.setStatus("Task paused."); |
| 120 | + } else { |
| 121 | + frame.setStatus("Task completed."); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // |
| 126 | + // PRIVATE METHODS |
| 127 | + // |
| 128 | + private NondeterministicVacuumAgent createNondeterministicVacuumAgent() { |
| 129 | + NondeterministicVacuumAgent agent = new NondeterministicVacuumAgent( |
| 130 | + new FullyObservableVacuumEnvironmentPerceptToStateFunction()); |
| 131 | + |
| 132 | + return agent; |
| 133 | + } |
| 134 | + |
| 135 | + private NondeterministicProblem createNondeterministicProblem() { |
| 136 | + // create problem |
| 137 | + NondeterministicProblem problem = new NondeterministicProblem( |
| 138 | + env.getCurrentState(), |
| 139 | + new VacuumWorldActions(), |
| 140 | + new VacuumWorldResults(agent), |
| 141 | + new VacuumWorldGoalTest(agent), |
| 142 | + new DefaultStepCostFunction()); |
| 143 | + |
| 144 | + return problem; |
| 145 | + } |
| 146 | +} |
| 147 | +
|
0 commit comments