From f3527d2130ea5eb7b941dba150583d1b897700d0 Mon Sep 17 00:00:00 2001 From: s475275 Date: Mon, 27 Mar 2023 01:00:35 +0200 Subject: [PATCH] =?UTF-8?q?Domy=20i=20sk=C5=82adowiska=20na=20grafie,=20wy?= =?UTF-8?q?b=C3=B3r=20ruchu=20przez=20obiekt=20agenta,=20pobieranie=20info?= =?UTF-8?q?rmacji?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- res/map.txt | 2 +- src/agent.py | 64 +++++++++++++++++++++++++++++++++++++++++++---- src/main.py | 3 +-- src/simulation.py | 59 +++++++++++++++++++++++++------------------ 4 files changed, 95 insertions(+), 33 deletions(-) diff --git a/res/map.txt b/res/map.txt index 232d116..375db8b 100644 --- a/res/map.txt +++ b/res/map.txt @@ -1,4 +1,4 @@ -MP------------------------ +MP--H--------------------- ORRRRRRRRRRRRRRRRRR---R--- SZ--R------R------R---RRR- ----R------R------R---R--- diff --git a/src/agent.py b/src/agent.py index 6d03eec..a63f7d0 100644 --- a/src/agent.py +++ b/src/agent.py @@ -1,9 +1,31 @@ -class Agent: - def __init__(self): - self.graph = {} - self.poi = {} +from random import randint +import pygame as pg - def inform(self, roads_pos, entities): + +def vector_to_tuple(vector): + tup = (int(vector.x), int(vector.y)) + return tup + + +class HousePOI: + def __init__(self): + self.bins = None + + def discover_bins(self, bins): + self.bins = bins + + +class Agent: + def __init__(self, simulation): + self.simulation = simulation + self.current_pos = vector_to_tuple(self.simulation.state.truck_origin) + self.graph = {} + self.houses = {} + self.dumps = {} + self.fullness = None + + # utworzenie grafu dróg + roads_pos = [vector_to_tuple(pos) for pos in self.simulation.state.roads_pos] for index, pos in enumerate(roads_pos): for another_pos in roads_pos[index:]: if pos == another_pos: @@ -15,3 +37,35 @@ class Agent: self.graph[another_pos] = set() self.graph[pos].add(another_pos) self.graph[another_pos].add(pos) + + # dołączenie domów i składowisk do grafu dróg + entities = self.simulation.state.entities + for entity in entities: + entity_pos = vector_to_tuple(entity.position) + for neighbour_pos in [(entity_pos[0]-1, entity_pos[1]), + (entity_pos[0]+1, entity_pos[1]), + (entity_pos[0], entity_pos[1]-1), + (entity_pos[0], entity_pos[1]+1)]: + if neighbour_pos in roads_pos: + if entity_pos not in self.graph.keys(): + self.graph[entity_pos] = set() + self.graph[entity_pos].add(neighbour_pos) + self.graph[neighbour_pos].add(entity_pos) + if entity.entity_type == 'dump': + self.dumps[entity.trash_type] = vector_to_tuple(entity.position) + if entity.entity_type == 'house': + self.houses[vector_to_tuple(entity.position)] = HousePOI() + + def update(self): + entities = self.simulation.state.entities + for entity in entities: + if entity.entity_type == 'truck': + self.current_pos = vector_to_tuple(entity.position) + self.fullness = entity.fullness + + def decide_move(self): + possible_positions = self.graph[self.current_pos] + possible_moves = [pg.Vector2(pos[0] - self.current_pos[0], pos[1] - self.current_pos[1]) for pos in possible_positions] + + move = possible_moves[randint(0, len(possible_moves)-1)] + return move diff --git a/src/main.py b/src/main.py index 7956d3b..cc63f25 100644 --- a/src/main.py +++ b/src/main.py @@ -2,6 +2,5 @@ from simulation import * from agent import * if __name__ == "__main__": - agent = Agent() - simulation = Interface(agent) + simulation = Interface() simulation.loop() diff --git a/src/simulation.py b/src/simulation.py index 1c72fa9..b6494f7 100644 --- a/src/simulation.py +++ b/src/simulation.py @@ -1,7 +1,9 @@ from pathlib import Path -import pygame as pg from random import randint +import pygame as pg +from agent import Agent + ROAD_SPRITE = pg.Vector2(0, 0) HOUSE_WITHOUT_TRASH_SPRITE = pg.Vector2(1, 0) @@ -20,6 +22,8 @@ DUMP_SPRITES = {'paper': PAPER_DUMP_SPRITE, 'plastic': PLASTIC_DUMP_SPRITE, 'mixed': MIXED_DUMP_SPRITE} +TRASH_TYPES = ['paper', 'plastic', 'glass', 'mixed'] + class Entity: def __init__(self, state, position): @@ -31,6 +35,11 @@ class TruckEntity(Entity): def __init__(self, state, position): super().__init__(state, position) self.tile = TRUCK_SPRITE + self.entity_type = 'truck' + self.fullness = {'paper': 0, + 'glass': 0, + 'plastic': 0, + 'mixed': 0} def move(self, move_vector): proposed_pos = self.position + move_vector @@ -53,19 +62,23 @@ class Bin: def __init__(self, size, trash_type): self.size = size self.trash_type = trash_type + self.fullness = randint(0, 100) class HouseEntity(Entity): def __init__(self, state, position): super().__init__(state, position) self.tile = HOUSE_SPRITES[0] - self.bins = [] + self.entity_type = 'house' + self.bins = [Bin(randint(1, 2), trash_type) for trash_type in TRASH_TYPES] class DumpEntity(Entity): def __init__(self, state, position, trash_type): super().__init__(state, position) self.tile = DUMP_SPRITES[trash_type] + self.entity_type = 'dump' + self.trash_type = trash_type class SimulationState: @@ -115,7 +128,7 @@ class SimulationState: def update(self, move_agent): for entity in self.entities: - if entity.__class__ is TruckEntity: + if entity.entity_type is 'truck': entity.move(move_agent) @@ -166,29 +179,27 @@ class StructureLayer(Layer): class Interface: - def __init__(self, agent): + def __init__(self): pg.init() - # autonomiczny agent - self.agent = agent - # stan symulacji - self.simulation_state = SimulationState() - self.move_agent = pg.Vector2(0, 0) + self.state = SimulationState() + self.move_truck = pg.Vector2(0, 0) + + # autonomiczny agent + self.agent = Agent(self) # rendering self.cell_size = pg.Vector2(64, 64) texture_file = Path("../res/tiles.png") - self.layers = [StructureLayer(self, texture_file, self.simulation_state, - self.simulation_state.roads_pos, ROAD_SPRITE), - EntityLayer(self, texture_file, self.simulation_state, - self.simulation_state.entities)] + self.layers = [StructureLayer(self, texture_file, self.state, self.state.roads_pos, ROAD_SPRITE), + EntityLayer(self, texture_file, self.state, self.state.entities)] # okno pg.display.set_caption("Inteligentna śmieciarka") - window_size = self.simulation_state.world_limits.elementwise() * self.cell_size + window_size = self.state.world_limits.elementwise() * self.cell_size self.window = pg.display.set_mode((int(window_size.x), int(window_size.y))) - self.simulation_state.world_limits.elementwise() * self.cell_size + self.state.world_limits.elementwise() * self.cell_size # dla pętli self.clock = pg.time.Clock() @@ -197,7 +208,7 @@ class Interface: self.debug_mode = False def processUserInput(self): - self.move_agent = pg.Vector2(0, 0) + self.move_truck = pg.Vector2(0, 0) for event in pg.event.get(): if event.type == pg.QUIT: @@ -212,21 +223,20 @@ class Interface: if self.debug_mode: if event.key == pg.K_RIGHT: - self.move_agent.x = 1 + self.move_truck.x = 1 if event.key == pg.K_LEFT: - self.move_agent.x = -1 + self.move_truck.x = -1 if event.key == pg.K_DOWN: - self.move_agent.y = 1 + self.move_truck.y = 1 if event.key == pg.K_UP: - self.move_agent.y = -1 + self.move_truck.y = -1 def processAgentInput(self): - moves = [pg.Vector2(-1, 0), pg.Vector2(1, 0), pg.Vector2(0, -1), pg.Vector2(0, 1)] - - self.move_agent = moves[randint(0, 3)] + self.move_truck = self.agent.decide_move() def update(self): - self.simulation_state.update(self.move_agent) + self.state.update(self.move_truck) + self.agent.update() def render(self): if not self.debug_mode: @@ -240,7 +250,6 @@ class Interface: pg.display.update() def loop(self): - self.agent.inform([(int(pos.x), int(pos.y)) for pos in self.simulation_state.roads_pos], self.simulation_state.entities) while self.run_simulation: self.processUserInput() if not self.debug_mode: