basic self-replication

main
Nekkowe! 2023-09-30 19:39:26 +02:00
parent c8a4efc402
commit 6111f69c48
2 changed files with 80 additions and 13 deletions

View File

@ -16,15 +16,10 @@ first_agent = CAMSReverseAndSidestepAgent(
initial_direction=Direction.NORTH 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") im = ax.imshow(env.render(), aspect="equal", origin="lower")
def update(frame_number): def update(frame_number):
print(frame_number)
env.step() env.step()
im.set_data(env.render()) im.set_data(env.render())
return im, return im,

86
sim.py
View File

@ -14,8 +14,8 @@ class CellState(Enum):
class Colours(Enum): class Colours(Enum):
WALL = [30,30,30] WALL = [30,30,30]
STOCKED = [200,100,0] STOCKED = [200,150,0]
DEPLETED = [0,50,50] DEPLETED = [100,40,0]
UNDEFINED = [255,0,255] UNDEFINED = [255,0,255]
AGENT = [0,255,0] AGENT = [0,255,0]
@ -121,23 +121,95 @@ class DirectionalAgent(Agent):
position_in_front = self.position + self.direction.value position_in_front = self.position + self.direction.value
return self.environment.is_wall(position_in_front) 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): 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): 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): def reverse_direction(self):
new_vector = np.array(self.direction.value) * -1 new_vector = np.array(self.direction.value) * -1
self.direction = Direction(tuple(new_vector)) self.direction = Direction(tuple(new_vector))
class CAMSReverseAndSidestepAgent(DirectionalAgent): 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) super().__init__(environment, position, initial_direction)
self.resources = 0 self.resources = 0
self.required_resources = required_resources self.required_resources = required_resources
self.number_of_turns = 0
def step(self): def step(self):
self.eat()
if self.wall_in_front(): 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.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