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

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.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()

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

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
from enum import Enum
class CATEGORY(Enum):
class ItemType(Enum):
PIZZA = 1
PASTA = 2
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:
def __init__(self, id: int, money: int, job: JOB):
self.id = id
self.money = money
self.job = job
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

View File

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

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

View File

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

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)