diff --git a/AgentBase.py b/AgentBase.py new file mode 100644 index 0000000..e949bc8 --- /dev/null +++ b/AgentBase.py @@ -0,0 +1,14 @@ +from mesa import Agent, Model + +from util.AgentIdFactory import AgentIdFactory + + +class AgentBase(Agent): + + def __init__(self, model: Model): + unique_id = AgentIdFactory.get_next_id() + super().__init__(unique_id, model) + self.creation_log() + + def creation_log(self): + pass diff --git a/ForkliftAgent.py b/ForkliftAgent.py index f884155..cee713c 100644 --- a/ForkliftAgent.py +++ b/ForkliftAgent.py @@ -2,17 +2,18 @@ from typing import Tuple from mesa import Agent +from AgentBase import AgentBase from data.Direction import Direction +from util.AgentIdFactory import AgentIdFactory -class ForkliftAgent(Agent): +class ForkliftAgent(AgentBase): - def __init__(self, unique_id, model): - super().__init__(unique_id, model) + def __init__(self, model): + super().__init__(model) self.movement_queue = [Tuple[int, int]] self.current_position = Tuple[int, int] self.current_rotation = Direction.right - print("Created forklift Agent with ID: {}".format(unique_id)) def assign_new_movement_task(self, movement_list): self.movement_queue = [] @@ -52,3 +53,6 @@ class ForkliftAgent(Agent): def step(self) -> None: print("forklift step") self.move() + + def creation_log(self): + print("Created Forklift Agent [id: {}]".format(self.unique_id)) diff --git a/GameModel.py b/GameModel.py index fbc983f..f1574e9 100644 --- a/GameModel.py +++ b/GameModel.py @@ -1,25 +1,28 @@ -from mesa import Model +from typing import List + +from mesa import Model, Agent from mesa.space import MultiGrid from mesa.time import RandomActivation +from AgentBase import AgentBase from ForkliftAgent import ForkliftAgent from PatchAgent import PatchAgent from PatchType import PatchType -from util.PathDefinitions import inverse_y +from util.PathDefinitions import inverse_y, GridLocation, GridWithWeights from util.PathVisualiser import draw_grid, reconstruct_path from util.Pathfinder import a_star_search class GameModel(Model): - def __init__(self, width, height, graph): + def __init__(self, width, height, graph: GridWithWeights): # self.num_agents = 5 self.running = True self.grid = MultiGrid(height, width, True) self.schedule = RandomActivation(self) - self.agents = [] + self.agents = [AgentBase] - self.forklift_agent = ForkliftAgent(0, self) + self.forklift_agent = ForkliftAgent(self) self.schedule.add(self.forklift_agent) self.agents.append(self.forklift_agent) @@ -29,34 +32,43 @@ class GameModel(Model): self.grid.place_agent(self.forklift_agent, (x, y)) self.forklift_agent.current_position = (x, y) - start, goal = (x, y), (2, 1) - came_from, cost_so_far = a_star_search(graph, start, goal) - draw_grid(graph, point_to=came_from, start=start, goal=goal) + # start, goal = (x, y), (2, 1) + # came_from, cost_so_far = a_star_search(graph, start, goal) + # draw_grid(graph, point_to=came_from, start=start, goal=goal) + # + # path = map(lambda t: (t[0], inverse_y(height, t[1])), + # reconstruct_path(came_from=came_from, start=start, goal=goal)) + # + # print("cam from: {}".format(came_from)) + # print("costerino: {}".format(cost_so_far)) + # draw_grid(graph, path=reconstruct_path(came_from, start=start, goal=goal)) + # self.forklift_agent.assign_new_movement_task(path) - path = map(lambda t: (t[0], inverse_y(height, t[1])), - reconstruct_path(came_from=came_from, start=start, goal=goal)) + self.place_patch_agents() + self.place_walls_agents(graph.walls) - print("cam from: {}".format(came_from)) - print("costerino: {}".format(cost_so_far)) - draw_grid(graph, path=reconstruct_path(came_from, start=start, goal=goal)) - - self.forklift_agent.assign_new_movement_task(path) - - agent = PatchAgent(1, self, PatchType.pickUp) - # self.schedule.add(agent) + def place_patch_agents(self): + agent = PatchAgent(self, PatchType.pickUp) + self.schedule.add(agent) self.grid.place_agent(agent, (self.grid.width - 1, self.grid.height - 1)) self.agents.append(agent) - agent = PatchAgent(2, self, PatchType.dropOff) + agent = PatchAgent(self, PatchType.dropOff) # self.schedule.add(agent) self.grid.place_agent(agent, (0, self.grid.height - 1)) self.agents.append(agent) for i in range(3): - a = PatchAgent(i + 3, self, PatchType.item) + a = PatchAgent(self, PatchType.item) self.agents.append(a) self.grid.place_agent(a, (i, 0)) + def place_walls_agents(self, walls: List[GridLocation]): + for w in walls: + agent = PatchAgent(self, PatchType.wall) + self.agents.append(agent) + self.grid.place_agent(agent, w) + def step(self): self.schedule.step() diff --git a/Generator.py b/Generator.py new file mode 100644 index 0000000..a05e805 --- /dev/null +++ b/Generator.py @@ -0,0 +1,43 @@ +import random +from data.Item import Item +from data.ItemType import ItemType +from data.Order import Order + + +class Generator: + def __init__(self, input_sequence_size: int, output_order_list_size: int): + self.input_sequence = self.generate_input_sequence(input_sequence_size) + self.output_order_list = self.generate_order_list(output_order_list_size) + + def generate_order_list(self, output_order_list_size: int): + order_list = [Order] + for i in range(0, output_order_list_size): + order_list.append(self.generate_order()) + + return order_list + + def generate_order(self) -> Order: + order_size = random.randint(1, 4) + + items = [Item] + for i in range(order_size): + items.append(self.generate_item()) + + time_base = random.randint(8, 20) + final_time = time_base * order_size + + value = sum(i.price for i in items) + + return Order(final_time, items, value) + + def generate_input_sequence(self, input_sequence_size): + sequence = [Item] + for i in range(0, input_sequence_size): + sequence.append(self.generate_item()) + + return sequence + + @staticmethod + def generate_item() -> Item: + randomly_picked_type = random.choice(list(ItemType)) + return Item(randomly_picked_type) diff --git a/PatchAgent.py b/PatchAgent.py index 4ebee70..174461d 100644 --- a/PatchAgent.py +++ b/PatchAgent.py @@ -1,11 +1,14 @@ from mesa import Agent, Model +from AgentBase import AgentBase from PatchType import PatchType -class PatchAgent(Agent): +class PatchAgent(AgentBase): - def __init__(self, unique_id, model, type: PatchType): - super().__init__(unique_id, model) - self.type = type - print("Created Patch Agent with ID: {}".format(unique_id)) \ No newline at end of file + def __init__(self, model, patch_type: PatchType): + self.patch_type = patch_type + super().__init__(model) + + def creation_log(self): + print("Created Patch Agent [id: {} ,type: {}]".format(self.unique_id, self.patch_type)) diff --git a/PatchType.py b/PatchType.py index 49f3bad..3b1bf71 100644 --- a/PatchType.py +++ b/PatchType.py @@ -4,4 +4,5 @@ import enum class PatchType(enum.Enum): dropOff = 1 pickUp = 2 - item = 3 \ No newline at end of file + item = 3 + wall = 4 diff --git a/data/Game.py b/data/Game.py index 502dfd2..92de383 100644 --- a/data/Game.py +++ b/data/Game.py @@ -1,5 +1,5 @@ from data import Direction -from data.CATEGORY import CATEGORY +from data.ItemType import ItemType from data.Item import Item from data.Order import Order from typing import Dict diff --git a/data/Item.py b/data/Item.py index 958f577..9851805 100644 --- a/data/Item.py +++ b/data/Item.py @@ -1,8 +1,16 @@ -from data.CATEGORY import CATEGORY +from itertools import count + +from data.ItemType import ItemType class Item: - def __init__(self, id: int, category: CATEGORY, price: int): - self.id = id - self.category = category - self.price = price \ No newline at end of file + id_counter = count(start=0) + + def __init__(self, type: ItemType): + self.id = next(self.id_counter) + self.category = type + self.price = self.price_factory(type) + + @staticmethod + def price_factory(self, type: ItemType): + return 1 diff --git a/data/CATEGORY.py b/data/ItemType.py similarity index 63% rename from data/CATEGORY.py rename to data/ItemType.py index cf6357d..007b743 100644 --- a/data/CATEGORY.py +++ b/data/ItemType.py @@ -1,7 +1,7 @@ from enum import Enum -class CATEGORY(Enum): +class ItemType(Enum): PIZZA = 1 PASTA = 2 EGG = 3 diff --git a/data/JOB.py b/data/JOB.py deleted file mode 100644 index 3374c0c..0000000 --- a/data/JOB.py +++ /dev/null @@ -1,7 +0,0 @@ -from enum import Enum - - -class JOB(Enum): - FISHERMAN = 1 - FIREFIGHTER = 2 - POLICEMAN = 3 \ No newline at end of file diff --git a/data/Order.py b/data/Order.py index 7407b96..5050cf0 100644 --- a/data/Order.py +++ b/data/Order.py @@ -1,8 +1,13 @@ -from data.JOB import JOB +from itertools import count + +from data.Item import Item class Order: - def __init__(self, id: int, money: int, job: JOB): - self.id = id - self.money = money - self.job = job \ No newline at end of file + id_counter = count(start=0) + + def __init__(self, time: int, items: [Item], value: int): + self.id = next(self.id_counter) + self.time = time + self.items = items + self.value = value diff --git a/data/StockPile.py b/data/StockPile.py index 4581a35..8e6c9db 100644 --- a/data/StockPile.py +++ b/data/StockPile.py @@ -1,9 +1,9 @@ -from data.CATEGORY import CATEGORY +from data.ItemType import ItemType from data.Item import Item class StockPile: - def __init__(self, id: int, category: CATEGORY, itemList: [Item]): + def __init__(self, id: ItemType, pos: (int, int), itemList: [Item], ): self.id = id - self.category = category + self.pos = pos self.itemList = itemList \ No newline at end of file diff --git a/img/brick.webp b/img/brick.webp new file mode 100644 index 0000000..e07db6f Binary files /dev/null and b/img/brick.webp differ diff --git a/main.py b/main.py index 49ccdf4..d5babf9 100644 --- a/main.py +++ b/main.py @@ -32,9 +32,11 @@ def agent_portrayal(agent): if isinstance(agent, PatchAgent): color = colors[0] - if agent.type == PatchType.dropOff: + if agent.patch_type == PatchType.wall: + portrayal = {"Shape": "img/brick.webp", "scale": 1.0, "Layer": 0} + elif agent.patch_type == PatchType.dropOff: portrayal = {"Shape": "img/truck.png", "scale": 1.0, "Layer": 0} - elif agent.type == PatchType.pickUp: + elif agent.patch_type == PatchType.pickUp: portrayal = {"Shape": "img/okB00mer.png", "scale": 1.0, "Layer": 0} else: color = colors[random.randrange(13) + 3] @@ -53,7 +55,7 @@ gridHeight = 10 scale = base / gridWidth diagram4 = GridWithWeights(gridWidth, gridHeight) -diagram4.walls = [] +diagram4.walls = [(5, 5), (5, 6)] grid = CanvasGrid(agent_portrayal, gridWidth, gridHeight, scale * gridWidth, scale * gridHeight) diff --git a/test/nastepnik.py b/test/nastepnik.py index b610361..8c36f6d 100644 --- a/test/nastepnik.py +++ b/test/nastepnik.py @@ -41,11 +41,12 @@ def heuristic(a: Tuple[int, int], b: Tuple[int, int]) -> float: return abs(x1 - x2) + abs(y1 - y2) class PossibleMoves(Enum): - rotateTop = 1 + move = 1 rotateLeft = 2 rotateDown = 3 rotateRight = 4 - move = 5 + rotateTop = 5 + def getRotationEvaluation(direction: Direction): # get evaluationForMoveAfterRotation diff --git a/util/AgentIdFactory.py b/util/AgentIdFactory.py new file mode 100644 index 0000000..0bf988c --- /dev/null +++ b/util/AgentIdFactory.py @@ -0,0 +1,9 @@ +from itertools import count + + +class AgentIdFactory(object): + _id_counter = count(start=0) + + @staticmethod + def get_next_id() -> int: + return next(AgentIdFactory._id_counter) diff --git a/util/TWOJSTARY.py b/util/TWOJSTARY.py new file mode 100644 index 0000000..a0d9d8a --- /dev/null +++ b/util/TWOJSTARY.py @@ -0,0 +1,27 @@ +from data import Game, Order + + +def pickBestOrder(game: Game) -> Order: + maxi = game.orderList[0] + for i in range(len(game.orderList)): + if(game.orderList[i].money > maxi.money): + maxi = game.orderList[i] + + return maxi + +def realizeOrder(game: Game, orderId: int): + return 1 + +def getCorrectStockPile(game: Game) -> (int, int): + return game.stockPilePos[game.carriedItem.category].pos + +def TwojStary(game: Game) -> (int, int): + if len(game.orderList) == 0: + if game.carriedItem is None: + return game.deliveryPos + else: + return getCorrectStockPile(game) + elif game.carriedItem is None: + return realizeOrder(game, pickBestOrder(game)) + else: + return getCorrectStockPile(game) \ No newline at end of file