genetic algorithm preparation

This commit is contained in:
Vadzim Valchkovich 2023-06-15 21:22:07 +02:00
parent e8ec4cbcb2
commit 47ba41d2ba
6 changed files with 39 additions and 14 deletions

View File

@ -15,4 +15,13 @@ kitchen = Kitchen([0, 0], 0, SQUARE_SIZE, SCREEN_SIZE, store)
engine = Engine(SCREEN_SIZE, SQUARE_SIZE, kitchen, waiter, ACTION_DURATION) engine = Engine(SCREEN_SIZE, SQUARE_SIZE, kitchen, waiter, ACTION_DURATION)
layout = LayoutController(engine, store).create_and_subscribe(COUNT_OF_OBJECTS) layout = LayoutController(engine, store).create_and_subscribe(COUNT_OF_OBJECTS)
engine.loop() engine.loop()
'''
def example_stop(action_clock: int) -> bool:
return action_clock < 1000
print(engine.train_loop(example_stop))
'''

View File

@ -26,6 +26,7 @@ class Engine:
pygame.display.set_caption('Waiter Agent') pygame.display.set_caption('Waiter Agent')
self.action_clock = 0 self.action_clock = 0
self.serviced_tables = 0
self.action_duration = action_duration self.action_duration = action_duration
self.tree = TreeController() self.tree = TreeController()
@ -75,6 +76,16 @@ class Engine:
self.action() self.action()
self.redraw() self.redraw()
def train_loop(self, stop_condition):
print(colored("Simulation started", "green"))
self.action_duration = 0
while stop_condition(self.action_clock):
self.action()
return self.serviced_tables
def user_interaction(self): def user_interaction(self):
self.user_c.handler(engine=self) self.user_c.handler(engine=self)
@ -121,9 +132,9 @@ class Engine:
if o.is_neighbor_of(self.waiter): if o.is_neighbor_of(self.waiter):
o.updateState(self.action_clock, self.predictor_c) o.updateState(self.action_clock, self.predictor_c)
if self.waiter.on(o.position): if self.waiter.on(o.position):
o.action(self.waiter, self.action_clock) o.action(self)
self.kitchen.action(self.waiter, self.action_clock) self.kitchen.action(self)
# STEP 6. Update kitchen state # STEP 6. Update kitchen state
@ -164,6 +175,9 @@ class Engine:
def clock_increment(self, action_time): def clock_increment(self, action_time):
self.action_clock += action_time self.action_clock += action_time
def serviced_tables_increment(self):
self.serviced_tables += 1
def redraw(self): def redraw(self):
# STEP 1. Clean up screen # STEP 1. Clean up screen
self.screen.fill((255, 255, 255)) self.screen.fill((255, 255, 255))

View File

@ -20,6 +20,7 @@ class UserController:
engine.paused = not engine.paused engine.paused = not engine.paused
if engine.paused: if engine.paused:
print(colored("Paused", "red")) print(colored("Paused", "red"))
print(engine.serviced_tables)
else: else:
print(colored("Continued", "green")) print(colored("Continued", "green"))
elif event.key == pygame.K_p: elif event.key == pygame.K_p:

View File

@ -29,11 +29,11 @@ class Kitchen(Object):
if self.mark: if self.mark:
self.mark.blit(screen) self.mark.blit(screen)
def action(self, waiter, current_time): def action(self, engine):
self.cook_dishes(current_time) self.cook_dishes(engine.action_clock)
if waiter.on(self.position): if engine.waiter.on(self.position):
waiter.combine_orders(current_time, self) engine.waiter.combine_orders(engine.action_clock, self)
waiter.recharge() engine.waiter.recharge()
def cook_dishes(self, current_time): def cook_dishes(self, current_time):
for table in self.cooking: for table in self.cooking:

View File

@ -106,8 +106,8 @@ class Table(Object):
diff = current_time - self.waiting_time diff = current_time - self.waiting_time
return 0 if diff >= 1000 else 1 # 0 - bad; 1 - good return 0 if diff >= 1000 else 1 # 0 - bad; 1 - good
def action(self, waiter, current_time): def action(self, engine):
if self.is_order(): if self.is_order():
waiter.collect_order(self, current_time) engine.waiter.collect_order(self, engine)
elif self.is_done(): elif self.is_done():
waiter.deliver_dish(self, current_time) engine.waiter.deliver_dish(self, engine)

View File

@ -50,11 +50,12 @@ class Waiter(Object):
self.basket.append(dish) self.basket.append(dish)
self.basket_capacity -= 1 self.basket_capacity -= 1
def deliver_dish(self, table: Table, current_time): def deliver_dish(self, table: Table, engine):
if table in self.basket: if table in self.basket:
table.reset_role(current_time) table.reset_role(engine.action_clock)
self.basket.remove(table) self.basket.remove(table)
self.basket_capacity += 1 self.basket_capacity += 1
engine.serviced_tables_increment()
def order_in_memory(self, table) -> bool: def order_in_memory(self, table) -> bool:
return table in self.memory return table in self.memory
@ -65,11 +66,11 @@ class Waiter(Object):
def memory_is_full(self) -> bool: def memory_is_full(self) -> bool:
return self.memory_capacity == 0 return self.memory_capacity == 0
def collect_order(self, table, current_time): def collect_order(self, table: Table, engine):
if self.memory_is_full(): if self.memory_is_full():
return return
if table.agent_role == "order": if table.agent_role == "order":
table.set_wait(current_time) table.set_wait(engine.action_clock)
self.memory.append(table) self.memory.append(table)
self.memory_capacity -= 1 self.memory_capacity -= 1