From 47ba41d2ba7a7098cd59fd150ba9df958bde13da Mon Sep 17 00:00:00 2001 From: Vadzim Valchkovich Date: Thu, 15 Jun 2023 21:22:07 +0200 Subject: [PATCH] genetic algorithm preparation --- agent.py | 9 +++++++++ src/Engine.py | 18 ++++++++++++++++-- src/controller/UserController.py | 1 + src/obj/Kitchen.py | 10 +++++----- src/obj/Table.py | 6 +++--- src/obj/Waiter.py | 9 +++++---- 6 files changed, 39 insertions(+), 14 deletions(-) diff --git a/agent.py b/agent.py index 80c1b5a..b667c9a 100644 --- a/agent.py +++ b/agent.py @@ -15,4 +15,13 @@ kitchen = Kitchen([0, 0], 0, SQUARE_SIZE, SCREEN_SIZE, store) engine = Engine(SCREEN_SIZE, SQUARE_SIZE, kitchen, waiter, ACTION_DURATION) layout = LayoutController(engine, store).create_and_subscribe(COUNT_OF_OBJECTS) + engine.loop() + +''' +def example_stop(action_clock: int) -> bool: + return action_clock < 1000 + + +print(engine.train_loop(example_stop)) +''' diff --git a/src/Engine.py b/src/Engine.py index 73d663b..b41f6ad 100644 --- a/src/Engine.py +++ b/src/Engine.py @@ -26,6 +26,7 @@ class Engine: pygame.display.set_caption('Waiter Agent') self.action_clock = 0 + self.serviced_tables = 0 self.action_duration = action_duration self.tree = TreeController() @@ -75,6 +76,16 @@ class Engine: self.action() 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): self.user_c.handler(engine=self) @@ -121,9 +132,9 @@ class Engine: if o.is_neighbor_of(self.waiter): o.updateState(self.action_clock, self.predictor_c) 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 @@ -164,6 +175,9 @@ class Engine: def clock_increment(self, action_time): self.action_clock += action_time + def serviced_tables_increment(self): + self.serviced_tables += 1 + def redraw(self): # STEP 1. Clean up screen self.screen.fill((255, 255, 255)) diff --git a/src/controller/UserController.py b/src/controller/UserController.py index c0a5e88..77a5627 100644 --- a/src/controller/UserController.py +++ b/src/controller/UserController.py @@ -20,6 +20,7 @@ class UserController: engine.paused = not engine.paused if engine.paused: print(colored("Paused", "red")) + print(engine.serviced_tables) else: print(colored("Continued", "green")) elif event.key == pygame.K_p: diff --git a/src/obj/Kitchen.py b/src/obj/Kitchen.py index 7b0ba0e..7944f76 100644 --- a/src/obj/Kitchen.py +++ b/src/obj/Kitchen.py @@ -29,11 +29,11 @@ class Kitchen(Object): if self.mark: self.mark.blit(screen) - def action(self, waiter, current_time): - self.cook_dishes(current_time) - if waiter.on(self.position): - waiter.combine_orders(current_time, self) - waiter.recharge() + def action(self, engine): + self.cook_dishes(engine.action_clock) + if engine.waiter.on(self.position): + engine.waiter.combine_orders(engine.action_clock, self) + engine.waiter.recharge() def cook_dishes(self, current_time): for table in self.cooking: diff --git a/src/obj/Table.py b/src/obj/Table.py index b673f22..430f8c3 100644 --- a/src/obj/Table.py +++ b/src/obj/Table.py @@ -106,8 +106,8 @@ class Table(Object): diff = current_time - self.waiting_time 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(): - waiter.collect_order(self, current_time) + engine.waiter.collect_order(self, engine) elif self.is_done(): - waiter.deliver_dish(self, current_time) + engine.waiter.deliver_dish(self, engine) diff --git a/src/obj/Waiter.py b/src/obj/Waiter.py index e30695a..10201e7 100644 --- a/src/obj/Waiter.py +++ b/src/obj/Waiter.py @@ -50,11 +50,12 @@ class Waiter(Object): self.basket.append(dish) self.basket_capacity -= 1 - def deliver_dish(self, table: Table, current_time): + def deliver_dish(self, table: Table, engine): if table in self.basket: - table.reset_role(current_time) + table.reset_role(engine.action_clock) self.basket.remove(table) self.basket_capacity += 1 + engine.serviced_tables_increment() def order_in_memory(self, table) -> bool: return table in self.memory @@ -65,11 +66,11 @@ class Waiter(Object): def memory_is_full(self) -> bool: return self.memory_capacity == 0 - def collect_order(self, table, current_time): + def collect_order(self, table: Table, engine): if self.memory_is_full(): return if table.agent_role == "order": - table.set_wait(current_time) + table.set_wait(engine.action_clock) self.memory.append(table) self.memory_capacity -= 1