Navigation Menu

Skip to content

Commit

Permalink
fixed typo errors and removed unnecessary brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
dmeoli committed Jun 17, 2019
1 parent 9e0fa55 commit f743146
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 64 deletions.
11 changes: 5 additions & 6 deletions logic.py
Expand Up @@ -911,7 +911,7 @@ def new_disjunction(sentences):

class WumpusKB(PropKB):
"""
Create a Knowledge Base that contains the atemporal "Wumpus physics" and temporal rules with time zero.
Create a Knowledge Base that contains the a temporal "Wumpus physics" and temporal rules with time zero.
"""

def __init__(self, dimrow):
Expand Down Expand Up @@ -1120,8 +1120,7 @@ def set_orientation(self, orientation):
self.orientation = orientation

def __eq__(self, other):
if other.get_location() == self.get_location() and \
other.get_orientation() == self.get_orientation():
if other.get_location() == self.get_location() and other.get_orientation() == self.get_orientation():
return True
else:
return False
Expand Down Expand Up @@ -1558,8 +1557,8 @@ def fol_bc_and(KB, goals, theta):

P11, P12, P21, P22, P31, B11, B21 = expr('P11, P12, P21, P22, P31, B11, B21')
wumpus_kb.tell(~P11)
wumpus_kb.tell(B11 | '<=>' | ((P12 | P21)))
wumpus_kb.tell(B21 | '<=>' | ((P11 | P22 | P31)))
wumpus_kb.tell(B11 | '<=>' | (P12 | P21))
wumpus_kb.tell(B21 | '<=>' | (P11 | P22 | P31))
wumpus_kb.tell(~B11)
wumpus_kb.tell(B21)

Expand Down Expand Up @@ -1620,7 +1619,7 @@ def diff(y, x):
elif op == '/':
return (v * diff(u, x) - u * diff(v, x)) / (v * v)
elif op == '**' and isnumber(x.op):
return (v * u ** (v - 1) * diff(u, x))
return v * u ** (v - 1) * diff(u, x)
elif op == '**':
return (v * u ** (v - 1) * diff(u, x) +
u ** v * Expr('log')(u) * diff(v, x))
Expand Down
122 changes: 64 additions & 58 deletions tests/test_agents.py
@@ -1,12 +1,12 @@
import random
from agents import Direction

from agents import Agent
from agents import ReflexVacuumAgent, ModelBasedVacuumAgent, TrivialVacuumEnvironment, compare_agents,\
RandomVacuumAgent, TableDrivenVacuumAgent, TableDrivenAgentProgram, RandomAgentProgram, \
SimpleReflexAgentProgram, ModelBasedReflexAgentProgram, rule_match
from agents import Direction
from agents import ReflexVacuumAgent, ModelBasedVacuumAgent, TrivialVacuumEnvironment, compare_agents, \
RandomVacuumAgent, TableDrivenVacuumAgent, TableDrivenAgentProgram, RandomAgentProgram, \
SimpleReflexAgentProgram, ModelBasedReflexAgentProgram
from agents import Wall, Gold, Explorer, Thing, Bump, Glitter, WumpusEnvironment, Pit, \
VacuumEnvironment, Dirt

VacuumEnvironment, Dirt

random.seed("aima-python")

Expand Down Expand Up @@ -58,12 +58,12 @@ def test_add():
assert l2.direction == Direction.D


def test_RandomAgentProgram() :
#create a list of all the actions a vacuum cleaner can perform
def test_RandomAgentProgram():
# create a list of all the actions a vacuum cleaner can perform
list = ['Right', 'Left', 'Suck', 'NoOp']
# create a program and then an object of the RandomAgentProgram
program = RandomAgentProgram(list)

agent = Agent(program)
# create an object of TrivialVacuumEnvironment
environment = TrivialVacuumEnvironment()
Expand All @@ -72,10 +72,10 @@ def test_RandomAgentProgram() :
# run the environment
environment.run()
# check final status of the environment
assert environment.status == {(1, 0): 'Clean' , (0, 0): 'Clean'}
assert environment.status == {(1, 0): 'Clean', (0, 0): 'Clean'}


def test_RandomVacuumAgent() :
def test_RandomVacuumAgent():
# create an object of the RandomVacuumAgent
agent = RandomVacuumAgent()
# create an object of TrivialVacuumEnvironment
Expand All @@ -85,7 +85,7 @@ def test_RandomVacuumAgent() :
# run the environment
environment.run()
# check final status of the environment
assert environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
assert environment.status == {(1, 0): 'Clean', (0, 0): 'Clean'}


def test_TableDrivenAgent():
Expand All @@ -109,22 +109,22 @@ def test_TableDrivenAgent():
# create an object of TrivialVacuumEnvironment
environment = TrivialVacuumEnvironment()
# initializing some environment status
environment.status = {loc_A:'Dirty', loc_B:'Dirty'}
environment.status = {loc_A: 'Dirty', loc_B: 'Dirty'}
# add agent to the environment
environment.add_thing(agent)

# run the environment by single step everytime to check how environment evolves using TableDrivenAgentProgram
environment.run(steps = 1)
assert environment.status == {(1,0): 'Clean', (0,0): 'Dirty'}
environment.run(steps=1)
assert environment.status == {(1, 0): 'Clean', (0, 0): 'Dirty'}

environment.run(steps = 1)
assert environment.status == {(1,0): 'Clean', (0,0): 'Dirty'}
environment.run(steps=1)
assert environment.status == {(1, 0): 'Clean', (0, 0): 'Dirty'}

environment.run(steps = 1)
assert environment.status == {(1,0): 'Clean', (0,0): 'Clean'}
environment.run(steps=1)
assert environment.status == {(1, 0): 'Clean', (0, 0): 'Clean'}


def test_ReflexVacuumAgent() :
def test_ReflexVacuumAgent():
# create an object of the ReflexVacuumAgent
agent = ReflexVacuumAgent()
# create an object of TrivialVacuumEnvironment
Expand All @@ -134,31 +134,31 @@ def test_ReflexVacuumAgent() :
# run the environment
environment.run()
# check final status of the environment
assert environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
assert environment.status == {(1, 0): 'Clean', (0, 0): 'Clean'}


def test_SimpleReflexAgentProgram():
class Rule:

def __init__(self, state, action):
self.__state = state
self.action = action

def matches(self, state):
return self.__state == state

loc_A = (0, 0)
loc_B = (1, 0)

# create rules for a two state Vacuum Environment
rules = [Rule((loc_A, "Dirty"), "Suck"), Rule((loc_A, "Clean"), "Right"),
Rule((loc_B, "Dirty"), "Suck"), Rule((loc_B, "Clean"), "Left")]
Rule((loc_B, "Dirty"), "Suck"), Rule((loc_B, "Clean"), "Left")]

def interpret_input(state):
return state

# create a program and then an object of the SimpleReflexAgentProgram
program = SimpleReflexAgentProgram(rules, interpret_input)
program = SimpleReflexAgentProgram(rules, interpret_input)
agent = Agent(program)
# create an object of TrivialVacuumEnvironment
environment = TrivialVacuumEnvironment()
Expand All @@ -167,7 +167,7 @@ def interpret_input(state):
# run the environment
environment.run()
# check final status of the environment
assert environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
assert environment.status == {(1, 0): 'Clean', (0, 0): 'Clean'}


def test_ModelBasedReflexAgentProgram():
Expand All @@ -185,7 +185,7 @@ def matches(self, state):

# create rules for a two-state vacuum environment
rules = [Rule((loc_A, "Dirty"), "Suck"), Rule((loc_A, "Clean"), "Right"),
Rule((loc_B, "Dirty"), "Suck"), Rule((loc_B, "Clean"), "Left")]
Rule((loc_B, "Dirty"), "Suck"), Rule((loc_B, "Clean"), "Left")]

def update_state(state, action, percept, model):
return percept
Expand All @@ -203,7 +203,7 @@ def update_state(state, action, percept, model):
assert environment.status == {(1, 0): 'Clean', (0, 0): 'Clean'}


def test_ModelBasedVacuumAgent() :
def test_ModelBasedVacuumAgent():
# create an object of the ModelBasedVacuumAgent
agent = ModelBasedVacuumAgent()
# create an object of TrivialVacuumEnvironment
Expand All @@ -213,10 +213,10 @@ def test_ModelBasedVacuumAgent() :
# run the environment
environment.run()
# check final status of the environment
assert environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
assert environment.status == {(1, 0): 'Clean', (0, 0): 'Clean'}


def test_TableDrivenVacuumAgent() :
def test_TableDrivenVacuumAgent():
# create an object of the TableDrivenVacuumAgent
agent = TableDrivenVacuumAgent()
# create an object of the TrivialVacuumEnvironment
Expand All @@ -226,10 +226,10 @@ def test_TableDrivenVacuumAgent() :
# run the environment
environment.run()
# check final status of the environment
assert environment.status == {(1, 0):'Clean', (0, 0):'Clean'}
assert environment.status == {(1, 0): 'Clean', (0, 0): 'Clean'}


def test_compare_agents() :
def test_compare_agents():
environment = TrivialVacuumEnvironment
agents = [ModelBasedVacuumAgent, ReflexVacuumAgent]

Expand Down Expand Up @@ -263,90 +263,96 @@ def test_TableDrivenAgentProgram():
def test_Agent():
def constant_prog(percept):
return percept

agent = Agent(constant_prog)
result = agent.program(5)
assert result == 5


def test_VacuumEnvironment():
# Initialize Vacuum Environment
v = VacuumEnvironment(6,6)
#Get an agent
v = VacuumEnvironment(6, 6)
# Get an agent
agent = ModelBasedVacuumAgent()
agent.direction = Direction(Direction.R)
v.add_thing(agent)
v.add_thing(Dirt(), location=(2,1))
v.add_thing(Dirt(), location=(2, 1))

# Check if things are added properly
assert len([x for x in v.things if isinstance(x, Wall)]) == 20
assert len([x for x in v.things if isinstance(x, Dirt)]) == 1

#Let the action begin!
# Let the action begin!
assert v.percept(agent) == ("Clean", "None")
v.execute_action(agent, "Forward")
assert v.percept(agent) == ("Dirty", "None")
v.execute_action(agent, "TurnLeft")
v.execute_action(agent, "Forward")
assert v.percept(agent) == ("Dirty", "Bump")
v.execute_action(agent, "Suck")
assert v.percept(agent) == ("Clean", "None")
assert v.percept(agent) == ("Clean", "None")
old_performance = agent.performance
v.execute_action(agent, "NoOp")
assert old_performance == agent.performance


def test_WumpusEnvironment():
def constant_prog(percept):
return percept

# Initialize Wumpus Environment
w = WumpusEnvironment(constant_prog)

#Check if things are added properly
# Check if things are added properly
assert len([x for x in w.things if isinstance(x, Wall)]) == 20
assert any(map(lambda x: isinstance(x, Gold), w.things))
assert any(map(lambda x: isinstance(x, Explorer), w.things))
assert not any(map(lambda x: not isinstance(x,Thing), w.things))
assert not any(map(lambda x: not isinstance(x, Thing), w.things))

#Check that gold and wumpus are not present on (1,1)
assert not any(map(lambda x: isinstance(x, Gold) or isinstance(x,WumpusEnvironment),
w.list_things_at((1, 1))))
# Check that gold and wumpus are not present on (1,1)
assert not any(map(lambda x: isinstance(x, Gold) or isinstance(x, WumpusEnvironment),
w.list_things_at((1, 1))))

#Check if w.get_world() segments objects correctly
# Check if w.get_world() segments objects correctly
assert len(w.get_world()) == 6
for row in w.get_world():
assert len(row) == 6

#Start the game!
# Start the game!
agent = [x for x in w.things if isinstance(x, Explorer)][0]
gold = [x for x in w.things if isinstance(x, Gold)][0]
pit = [x for x in w.things if isinstance(x, Pit)][0]

assert w.is_done()==False
assert w.is_done() == False

#Check Walls
# Check Walls
agent.location = (1, 2)
percepts = w.percept(agent)
assert len(percepts) == 5
assert any(map(lambda x: isinstance(x,Bump), percepts[0]))
assert any(map(lambda x: isinstance(x, Bump), percepts[0]))

#Check Gold
# Check Gold
agent.location = gold.location
percepts = w.percept(agent)
assert any(map(lambda x: isinstance(x,Glitter), percepts[4]))
agent.location = (gold.location[0], gold.location[1]+1)
assert any(map(lambda x: isinstance(x, Glitter), percepts[4]))
agent.location = (gold.location[0], gold.location[1] + 1)
percepts = w.percept(agent)
assert not any(map(lambda x: isinstance(x,Glitter), percepts[4]))
assert not any(map(lambda x: isinstance(x, Glitter), percepts[4]))

#Check agent death
# Check agent death
agent.location = pit.location
assert w.in_danger(agent) == True
assert agent.alive == False
assert agent.killed_by == Pit.__name__
assert agent.performance == -1000

assert w.is_done()==True
assert w.is_done() == True


def test_WumpusEnvironmentActions():
def constant_prog(percept):
return percept

# Initialize Wumpus Environment
w = WumpusEnvironment(constant_prog)

Expand All @@ -371,4 +377,4 @@ def constant_prog(percept):
w.execute_action(agent, 'Climb')
assert not any(map(lambda x: isinstance(x, Explorer), w.things))

assert w.is_done()==True
assert w.is_done() == True

0 comments on commit f743146

Please sign in to comment.