agents structure improvements

This commit is contained in:
Aleksander Szamałek 2022-04-16 14:55:25 +02:00
parent ab11d2725e
commit 11f73635d6
17 changed files with 179 additions and 57 deletions

14
AgentBase.py Normal file
View File

@ -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

View File

@ -2,17 +2,18 @@ from typing import Tuple
from mesa import Agent from mesa import Agent
from AgentBase import AgentBase
from data.Direction import Direction from data.Direction import Direction
from util.AgentIdFactory import AgentIdFactory
class ForkliftAgent(Agent): class ForkliftAgent(AgentBase):
def __init__(self, unique_id, model): def __init__(self, model):
super().__init__(unique_id, model) super().__init__(model)
self.movement_queue = [Tuple[int, int]] self.movement_queue = [Tuple[int, int]]
self.current_position = Tuple[int, int] self.current_position = Tuple[int, int]
self.current_rotation = Direction.right self.current_rotation = Direction.right
print("Created forklift Agent with ID: {}".format(unique_id))
def assign_new_movement_task(self, movement_list): def assign_new_movement_task(self, movement_list):
self.movement_queue = [] self.movement_queue = []
@ -52,3 +53,6 @@ class ForkliftAgent(Agent):
def step(self) -> None: def step(self) -> None:
print("forklift step") print("forklift step")
self.move() self.move()
def creation_log(self):
print("Created Forklift Agent [id: {}]".format(self.unique_id))

View File

@ -1,25 +1,28 @@
from mesa import Model from typing import List
from mesa import Model, Agent
from mesa.space import MultiGrid from mesa.space import MultiGrid
from mesa.time import RandomActivation from mesa.time import RandomActivation
from AgentBase import AgentBase
from ForkliftAgent import ForkliftAgent from ForkliftAgent import ForkliftAgent
from PatchAgent import PatchAgent from PatchAgent import PatchAgent
from PatchType import PatchType 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.PathVisualiser import draw_grid, reconstruct_path
from util.Pathfinder import a_star_search from util.Pathfinder import a_star_search
class GameModel(Model): class GameModel(Model):
def __init__(self, width, height, graph): def __init__(self, width, height, graph: GridWithWeights):
# self.num_agents = 5 # self.num_agents = 5
self.running = True self.running = True
self.grid = MultiGrid(height, width, True) self.grid = MultiGrid(height, width, True)
self.schedule = RandomActivation(self) 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.schedule.add(self.forklift_agent)
self.agents.append(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.grid.place_agent(self.forklift_agent, (x, y))
self.forklift_agent.current_position = (x, y) self.forklift_agent.current_position = (x, y)
start, goal = (x, y), (2, 1) # start, goal = (x, y), (2, 1)
came_from, cost_so_far = a_star_search(graph, start, goal) # came_from, cost_so_far = a_star_search(graph, start, goal)
draw_grid(graph, point_to=came_from, start=start, goal=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])), self.place_patch_agents()
reconstruct_path(came_from=came_from, start=start, goal=goal)) self.place_walls_agents(graph.walls)
print("cam from: {}".format(came_from)) def place_patch_agents(self):
print("costerino: {}".format(cost_so_far)) agent = PatchAgent(self, PatchType.pickUp)
draw_grid(graph, path=reconstruct_path(came_from, start=start, goal=goal)) self.schedule.add(agent)
self.forklift_agent.assign_new_movement_task(path)
agent = PatchAgent(1, self, PatchType.pickUp)
# self.schedule.add(agent)
self.grid.place_agent(agent, (self.grid.width - 1, self.grid.height - 1)) self.grid.place_agent(agent, (self.grid.width - 1, self.grid.height - 1))
self.agents.append(agent) self.agents.append(agent)
agent = PatchAgent(2, self, PatchType.dropOff) agent = PatchAgent(self, PatchType.dropOff)
# self.schedule.add(agent) # self.schedule.add(agent)
self.grid.place_agent(agent, (0, self.grid.height - 1)) self.grid.place_agent(agent, (0, self.grid.height - 1))
self.agents.append(agent) self.agents.append(agent)
for i in range(3): for i in range(3):
a = PatchAgent(i + 3, self, PatchType.item) a = PatchAgent(self, PatchType.item)
self.agents.append(a) self.agents.append(a)
self.grid.place_agent(a, (i, 0)) 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): def step(self):
self.schedule.step() self.schedule.step()

43
Generator.py Normal file
View File

@ -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)

View File

@ -1,11 +1,14 @@
from mesa import Agent, Model from mesa import Agent, Model
from AgentBase import AgentBase
from PatchType import PatchType from PatchType import PatchType
class PatchAgent(Agent): class PatchAgent(AgentBase):
def __init__(self, unique_id, model, type: PatchType): def __init__(self, model, patch_type: PatchType):
super().__init__(unique_id, model) self.patch_type = patch_type
self.type = type super().__init__(model)
print("Created Patch Agent with ID: {}".format(unique_id))
def creation_log(self):
print("Created Patch Agent [id: {} ,type: {}]".format(self.unique_id, self.patch_type))

View File

@ -5,3 +5,4 @@ class PatchType(enum.Enum):
dropOff = 1 dropOff = 1
pickUp = 2 pickUp = 2
item = 3 item = 3
wall = 4

View File

@ -1,5 +1,5 @@
from data import Direction from data import Direction
from data.CATEGORY import CATEGORY from data.ItemType import ItemType
from data.Item import Item from data.Item import Item
from data.Order import Order from data.Order import Order
from typing import Dict from typing import Dict

View File

@ -1,8 +1,16 @@
from data.CATEGORY import CATEGORY from itertools import count
from data.ItemType import ItemType
class Item: class Item:
def __init__(self, id: int, category: CATEGORY, price: int): id_counter = count(start=0)
self.id = id
self.category = category def __init__(self, type: ItemType):
self.price = price self.id = next(self.id_counter)
self.category = type
self.price = self.price_factory(type)
@staticmethod
def price_factory(self, type: ItemType):
return 1

View File

@ -1,7 +1,7 @@
from enum import Enum from enum import Enum
class CATEGORY(Enum): class ItemType(Enum):
PIZZA = 1 PIZZA = 1
PASTA = 2 PASTA = 2
EGG = 3 EGG = 3

View File

@ -1,7 +0,0 @@
from enum import Enum
class JOB(Enum):
FISHERMAN = 1
FIREFIGHTER = 2
POLICEMAN = 3

View File

@ -1,8 +1,13 @@
from data.JOB import JOB from itertools import count
from data.Item import Item
class Order: class Order:
def __init__(self, id: int, money: int, job: JOB): id_counter = count(start=0)
self.id = id
self.money = money def __init__(self, time: int, items: [Item], value: int):
self.job = job self.id = next(self.id_counter)
self.time = time
self.items = items
self.value = value

View File

@ -1,9 +1,9 @@
from data.CATEGORY import CATEGORY from data.ItemType import ItemType
from data.Item import Item from data.Item import Item
class StockPile: 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.id = id
self.category = category self.pos = pos
self.itemList = itemList self.itemList = itemList

BIN
img/brick.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -32,9 +32,11 @@ def agent_portrayal(agent):
if isinstance(agent, PatchAgent): if isinstance(agent, PatchAgent):
color = colors[0] 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} 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} portrayal = {"Shape": "img/okB00mer.png", "scale": 1.0, "Layer": 0}
else: else:
color = colors[random.randrange(13) + 3] color = colors[random.randrange(13) + 3]
@ -53,7 +55,7 @@ gridHeight = 10
scale = base / gridWidth scale = base / gridWidth
diagram4 = GridWithWeights(gridWidth, gridHeight) diagram4 = GridWithWeights(gridWidth, gridHeight)
diagram4.walls = [] diagram4.walls = [(5, 5), (5, 6)]
grid = CanvasGrid(agent_portrayal, gridWidth, gridHeight, scale * gridWidth, scale * gridHeight) grid = CanvasGrid(agent_portrayal, gridWidth, gridHeight, scale * gridWidth, scale * gridHeight)

View File

@ -41,11 +41,12 @@ def heuristic(a: Tuple[int, int], b: Tuple[int, int]) -> float:
return abs(x1 - x2) + abs(y1 - y2) return abs(x1 - x2) + abs(y1 - y2)
class PossibleMoves(Enum): class PossibleMoves(Enum):
rotateTop = 1 move = 1
rotateLeft = 2 rotateLeft = 2
rotateDown = 3 rotateDown = 3
rotateRight = 4 rotateRight = 4
move = 5 rotateTop = 5
def getRotationEvaluation(direction: Direction): def getRotationEvaluation(direction: Direction):
# get evaluationForMoveAfterRotation # get evaluationForMoveAfterRotation

9
util/AgentIdFactory.py Normal file
View File

@ -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)

27
util/TWOJSTARY.py Normal file
View File

@ -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)