|
2 | 2 | from agents import Direction
|
3 | 3 | from agents import Agent
|
4 | 4 | from agents import ReflexVacuumAgent, ModelBasedVacuumAgent, TrivialVacuumEnvironment, compare_agents,\
|
5 |
| - RandomVacuumAgent, TableDrivenVacuumAgent, TableDrivenAgentProgram, RandomAgentProgram |
| 5 | + RandomVacuumAgent, TableDrivenVacuumAgent, TableDrivenAgentProgram, RandomAgentProgram, \ |
| 6 | + SimpleReflexAgentProgram, rule_match |
6 | 7 |
|
7 | 8 |
|
8 | 9 | random.seed("aima-python")
|
@@ -131,6 +132,38 @@ def test_ReflexVacuumAgent() :
|
131 | 132 | # check final status of the environment
|
132 | 133 | assert environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
|
133 | 134 |
|
| 135 | +def test_SimpleReflexAgentProgram(): |
| 136 | + class Rule: |
| 137 | + |
| 138 | + def __init__(self, state, action): |
| 139 | + self.__state = state |
| 140 | + self.action = action |
| 141 | + |
| 142 | + def matches(self, state): |
| 143 | + return self.__state == state |
| 144 | + |
| 145 | + loc_A = (0, 0) |
| 146 | + loc_B = (1, 0) |
| 147 | + |
| 148 | + # create rules for a two state Vacuum Environment |
| 149 | + rules = [Rule((loc_A, "Dirty"), "Suck"), Rule((loc_A, "Clean"), "Right"), |
| 150 | + Rule((loc_B, "Dirty"), "Suck"), Rule((loc_B, "Clean"), "Left")] |
| 151 | + |
| 152 | + def interpret_input(state): |
| 153 | + return state |
| 154 | + |
| 155 | + # create a program and then an object of the SimpleReflexAgentProgram |
| 156 | + program = SimpleReflexAgentProgram(rules, interpret_input) |
| 157 | + agent = Agent(program) |
| 158 | + # create an object of TrivialVacuumEnvironment |
| 159 | + environment = TrivialVacuumEnvironment() |
| 160 | + # add agent to the environment |
| 161 | + environment.add_thing(agent) |
| 162 | + # run the environment |
| 163 | + environment.run() |
| 164 | + # check final status of the environment |
| 165 | + assert environment.status == {(1,0):'Clean' , (0,0) : 'Clean'} |
| 166 | + |
134 | 167 |
|
135 | 168 | def test_ModelBasedVacuumAgent() :
|
136 | 169 | # create an object of the ModelBasedVacuumAgent
|
|
0 commit comments