diff --git a/main.py b/main.py index 52aaa5c..7cd10e6 100644 --- a/main.py +++ b/main.py @@ -16,15 +16,10 @@ first_agent = CAMSReverseAndSidestepAgent( initial_direction=Direction.NORTH ) -second_agent = CAMSReverseAndSidestepAgent( - environment=env, - position=(11,13), - initial_direction=Direction.EAST -) - im = ax.imshow(env.render(), aspect="equal", origin="lower") def update(frame_number): + print(frame_number) env.step() im.set_data(env.render()) return im, diff --git a/sim.py b/sim.py index d78b618..da4f98e 100644 --- a/sim.py +++ b/sim.py @@ -14,8 +14,8 @@ class CellState(Enum): class Colours(Enum): WALL = [30,30,30] - STOCKED = [200,100,0] - DEPLETED = [0,50,50] + STOCKED = [200,150,0] + DEPLETED = [100,40,0] UNDEFINED = [255,0,255] AGENT = [0,255,0] @@ -121,23 +121,95 @@ class DirectionalAgent(Agent): position_in_front = self.position + self.direction.value return self.environment.is_wall(position_in_front) + def agent_in_front(self): + return False + + def move_forward(self): + return self.move(self.direction) + + NORTH = (0, 1) + SOUTH = (0, -1) + WEST = (-1, 0) + EAST = (1, 0) + def turn_left(self): - raise NotImplementedError + if self.direction is Direction.NORTH: + self.direction = Direction.WEST + elif self.direction is Direction.EAST: + self.direction = Direction.NORTH + elif self.direction is Direction.SOUTH: + self.direction = Direction.EAST + elif self.direction is Direction.WEST: + self.direction = Direction.SOUTH + else: + raise ValueError def turn_right(self): - raise NotImplementedError + if self.direction is Direction.NORTH: + self.direction = Direction.EAST + elif self.direction is Direction.EAST: + self.direction = Direction.SOUTH + elif self.direction is Direction.SOUTH: + self.direction = Direction.WEST + elif self.direction is Direction.WEST: + self.direction = Direction.NORTH + else: + raise ValueError def reverse_direction(self): new_vector = np.array(self.direction.value) * -1 self.direction = Direction(tuple(new_vector)) class CAMSReverseAndSidestepAgent(DirectionalAgent): - def __init__(self, environment, position, initial_direction, required_resources = 100): + def __init__( + self, + environment, + position, + initial_direction, + required_resources = 500 + ): super().__init__(environment, position, initial_direction) self.resources = 0 self.required_resources = required_resources - + self.number_of_turns = 0 + def step(self): + self.eat() + if self.wall_in_front(): + # self.die() + #elif self.agent_in_front(): + if self.number_of_turns == 0: + self.reverse_direction() + self.move_forward() + self.number_of_turns += 1 + elif self.number_of_turns > 0: + self.turn_right() + self.move_forward() + self.turn_right() + self.number_of_turns == 0 + elif self.resources >= self.required_resources: + self.resources -= self.required_resources + new_agent = CAMSReverseAndSidestepAgent( + environment=self.environment, + position=(self.position + self.direction.value), + initial_direction=self.direction, + required_resources=self.required_resources + ) self.reverse_direction() - self.move(self.direction) + else: + self.move_forward() + + def eat(self): + cell = self.environment.cell(self.position) + resources = cell.resources + + if resources > 0: + self.resources += resources + cell.resources = 0 + return True + else: + return False + + +