From 7606871afb886480c3632d7596bc3840f896126c Mon Sep 17 00:00:00 2001 From: andrzej Date: Fri, 1 May 2020 01:08:08 +0200 Subject: [PATCH 01/10] =?UTF-8?q?Dodanie=20szukania=20najbli=C5=BCszej=20p?= =?UTF-8?q?aczki,=20szukania=20najbli=C5=BCszego=20rega=C5=82u=20pasuj?= =?UTF-8?q?=C4=85cego=20do=20paczki,=20i=20=C5=82adowania=20i=20odk=C5=82a?= =?UTF-8?q?dania=20paczki=20na=20miejsce?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent.py | 88 +++++++++++++++++++++++++++++++++++++++++++++++----- main.py | 2 +- warehouse.py | 2 ++ 3 files changed, 83 insertions(+), 9 deletions(-) diff --git a/agent.py b/agent.py index 087f8cd..0e97dfd 100644 --- a/agent.py +++ b/agent.py @@ -1,13 +1,16 @@ from warehouse import Coordinates, Tile, Pack from queue import PriorityQueue from math import sqrt -from attributes import TURN_LEFT_DIRECTIONS, TURN_RIGHT_DIRECTIONS +from attributes import TURN_LEFT_DIRECTIONS, TURN_RIGHT_DIRECTIONS, PackStatus, PackSize +import pdb class Node: - def __init__(self, coord_x, coord_y): + def __init__(self, coord_x, coord_y, package=None, is_rack=False): self.x = coord_x self.y = coord_y self.parent = None + self.package = package + self.is_rack = is_rack self.g_cost = 0 self.h_cost = 0 def __eq__(self, other): @@ -38,12 +41,20 @@ class Agent: def find_path(self): self.closed = [] + self.path = [] self.open = PriorityQueue() + if self.is_loaded: + rack = self.find_nearest_rack_for(self.transported_package) + self.dest = Node(rack.x_position, rack.y_position, is_rack=True) + else: + package = self.find_nearest_package() + self.dest = Node(package.lays_on_field.x_position, package.lays_on_field.y_position, package=package) + start_node = Node(self.x, self.y) + self.open.put((0, start_node)) while self.open: _, current_node = self.open.get() - print(current_node.x, current_node.y) self.closed.append(current_node) if current_node.x == self.dest.x and current_node.y == self.dest.y: while current_node.x != start_node.x or current_node.y != start_node.y: @@ -98,13 +109,21 @@ class Agent: return neighbours def move(self): + dest_coords = (self.dest.x, self.dest.y) if not self.path: + print("chuj") if not self.find_path(): return else: next = self.path.pop() + if (next.x, next.y) == dest_coords: + if self.dest.package: + self.pick_up_package(self.dest.package) + return + elif self.dest.is_rack: + self.unload_package(self.dest) + return star_dir = self.direction - print(next.x, next.y) if self.x > next.x and not self.direction == 'left': if self.direction == 'down': self.turn_right() @@ -133,17 +152,70 @@ class Agent: self.path.append(next) self.closed = [] - def check_if_can_move(self, next_coords: Coordinates): + def check_if_can_move(self, next_coords: Coordinates, rack_searching=False): tile_on_map = 0 <= next_coords.x < self.warehouse.width and 0 <= next_coords.y < self.warehouse.height + tile_passable = True if not tile_on_map: return False next_tile = self.warehouse.tiles[next_coords.x][next_coords.y] - tile_passable = isinstance(next_tile, Tile) and next_tile.category.passable + if not self.is_loaded: + tile_passable = isinstance(next_tile, Tile) and next_tile.category.passable return tile_passable + def find_nearest_package(self): + packages_costs = [] + start_node = Node(self.x, self.y) + if not self.warehouse.packages: + return + for package in self.warehouse.packages: + if package.status == PackStatus.STORED: + continue + new_node = Node(package.lays_on_field.x_position, package.lays_on_field.y_position) + cost = self.heuristic(start_node, new_node) + if cost > 0: + packages_costs.append((package, cost)) + package = min(packages_costs, key=lambda l: l[1])[0] + return package + + def find_nearest_rack_for(self, package, expand_box=0): + weight = package.size + accepted_weights = [weight, PackSize.ALL] + start_node = Node(self.x, self.y) + quarter_x = int(self.warehouse.width/4) + expand_box + quarter_y = int(self.warehouse.height/4) + expand_box + start_quarter_x = self.x - quarter_x if self.x - quarter_x > 0 else 0 + end_quarter_x = self.x + quarter_x if self.x + quarter_x < self.warehouse.width else self.warehouse.width - 1 + start_quarter_y = self.y - quarter_y if self.y - quarter_y > 0 else 0 + end_quarter_y = self.y + quarter_y if self.y + quarter_y < self.warehouse.height else self.warehouse.height - 1 + quarter = [row[start_quarter_y:end_quarter_y] for row in self.warehouse.tiles[start_quarter_x:end_quarter_x]] + quarter_racks = [[t for t in row if t.category.name == "Rack" and not t.occupied and t.category.pack_size in accepted_weights] for row in quarter] + quarter_racks = [t for row in quarter_racks for t in row] + racks_costs = [] + if not quarter_racks: + self.find_nearest_rack_for(package, expand_box+1) + for rack in quarter_racks: + new_node = Node(rack.x_position, rack.y_position) + cost = self.heuristic(start_node, new_node) + if cost > 0: + racks_costs.append((rack, cost)) + rack = min(racks_costs, key=lambda l: l[1])[0] + return rack + + def pick_up_package(self, pack): - tile = pack.lays_on_field - self.assigned_warehouse.tiles[tile.x_position][tile.y_position] = tile + self.warehouse.packages.remove(pack) self.is_loaded = True + self.warehouse.tiles[pack.lays_on_field.x_position][pack.lays_on_field.y_position].occupied = False + self.dest.package = None pack.lays_on_field = None self.transported_package = pack + + def unload_package(self, rack): + pack = self.transported_package + tile = self.warehouse.tiles[rack.x][rack.y] + self.transported_package = None + self.is_loaded = False + pack.lays_on_field = tile + self.warehouse.tiles[rack.x][rack.y].occupied = True + pack.status = PackStatus.STORED + self.warehouse.packages.append(pack) \ No newline at end of file diff --git a/main.py b/main.py index ee832a2..11337b4 100644 --- a/main.py +++ b/main.py @@ -21,7 +21,7 @@ class MainGameFrame: self.display = pygame.display.set_mode(WINDOW_SIZE) agent_radius = int(TILE_WIDTH/2) self.agent_tex = pygame.image.load('forklift.png') - self.warehouse_map = warehouse.Warehouse(20, 20, 150, 20) + self.warehouse_map = warehouse.Warehouse(20, 20, 150, 25) starting_x, starting_y = self.set_starting_agent_position() self.agent = agent.Agent(starting_x, starting_y, self.warehouse_map, agent_radius) self.clock = pygame.time.Clock() diff --git a/warehouse.py b/warehouse.py index ab1f5ec..4b3ba19 100644 --- a/warehouse.py +++ b/warehouse.py @@ -161,6 +161,7 @@ class Warehouse: for i in range(no_of_packages): pack_x, pack_y = self._set_package_position() package_field = self.tiles[pack_x][pack_y] + self.tiles[pack_x][pack_y].occupied = True new_package = Pack(lays_on_field=package_field) packages.append(new_package) return packages @@ -206,6 +207,7 @@ class Tile: self.category = CATEGORY.get(category, CATEGORY['floor']) self.x_position = x_position self.y_position = y_position + self.occupied = False def __str__(self): From 42f79f165aafdaabf766fc92170b310e6819f072 Mon Sep 17 00:00:00 2001 From: andrzej Date: Fri, 1 May 2020 01:19:23 +0200 Subject: [PATCH 02/10] =?UTF-8?q?Poprawka,=20by=20zamyka=C5=82o=20okno=20g?= =?UTF-8?q?dy=20wszystkie=20paczki=20s=C4=85=20na=20swoich=20miejscach?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent.py | 8 ++++++-- main.py | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/agent.py b/agent.py index 0e97dfd..86ee113 100644 --- a/agent.py +++ b/agent.py @@ -3,6 +3,8 @@ from queue import PriorityQueue from math import sqrt from attributes import TURN_LEFT_DIRECTIONS, TURN_RIGHT_DIRECTIONS, PackStatus, PackSize import pdb +import pygame +import sys class Node: def __init__(self, coord_x, coord_y, package=None, is_rack=False): @@ -111,7 +113,6 @@ class Agent: def move(self): dest_coords = (self.dest.x, self.dest.y) if not self.path: - print("chuj") if not self.find_path(): return else: @@ -166,7 +167,7 @@ class Agent: packages_costs = [] start_node = Node(self.x, self.y) if not self.warehouse.packages: - return + return None for package in self.warehouse.packages: if package.status == PackStatus.STORED: continue @@ -174,6 +175,9 @@ class Agent: cost = self.heuristic(start_node, new_node) if cost > 0: packages_costs.append((package, cost)) + if not packages_costs: + pygame.quit() + sys.exit() package = min(packages_costs, key=lambda l: l[1])[0] return package diff --git a/main.py b/main.py index 11337b4..995ce72 100644 --- a/main.py +++ b/main.py @@ -21,7 +21,7 @@ class MainGameFrame: self.display = pygame.display.set_mode(WINDOW_SIZE) agent_radius = int(TILE_WIDTH/2) self.agent_tex = pygame.image.load('forklift.png') - self.warehouse_map = warehouse.Warehouse(20, 20, 150, 25) + self.warehouse_map = warehouse.Warehouse(20, 20, 150, 2) starting_x, starting_y = self.set_starting_agent_position() self.agent = agent.Agent(starting_x, starting_y, self.warehouse_map, agent_radius) self.clock = pygame.time.Clock() From 98c16b9227c9e0673d7e263cd11a75a736e57ad1 Mon Sep 17 00:00:00 2001 From: andrzej Date: Fri, 1 May 2020 01:20:28 +0200 Subject: [PATCH 03/10] =?UTF-8?q?Zmiana=20ilo=C5=9Bci=20paczek=20w=20magaz?= =?UTF-8?q?ynie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 995ce72..ee832a2 100644 --- a/main.py +++ b/main.py @@ -21,7 +21,7 @@ class MainGameFrame: self.display = pygame.display.set_mode(WINDOW_SIZE) agent_radius = int(TILE_WIDTH/2) self.agent_tex = pygame.image.load('forklift.png') - self.warehouse_map = warehouse.Warehouse(20, 20, 150, 2) + self.warehouse_map = warehouse.Warehouse(20, 20, 150, 20) starting_x, starting_y = self.set_starting_agent_position() self.agent = agent.Agent(starting_x, starting_y, self.warehouse_map, agent_radius) self.clock = pygame.time.Clock() From 06ce652016f5e8af2eaababbb42f7605364358e2 Mon Sep 17 00:00:00 2001 From: andrzej Date: Fri, 1 May 2020 13:28:10 +0200 Subject: [PATCH 04/10] =?UTF-8?q?Poprawka,=20przesuni=C4=99cie=20momentu?= =?UTF-8?q?=20wykonania=20operacji=20za=C5=82adunku/roz=C5=82adunku,=20tak?= =?UTF-8?q?=20aby=20w=C3=B3zek=20wcze=C5=9Bniej=20obr=C3=B3ci=C5=82=20si?= =?UTF-8?q?=C4=99=20we=20w=C5=82a=C5=9Bciwym=20kierunku?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/agent.py b/agent.py index 86ee113..0f174a9 100644 --- a/agent.py +++ b/agent.py @@ -117,13 +117,6 @@ class Agent: return else: next = self.path.pop() - if (next.x, next.y) == dest_coords: - if self.dest.package: - self.pick_up_package(self.dest.package) - return - elif self.dest.is_rack: - self.unload_package(self.dest) - return star_dir = self.direction if self.x > next.x and not self.direction == 'left': if self.direction == 'down': @@ -145,7 +138,15 @@ class Agent: self.turn_right() else: self.turn_left() - + + if (next.x, next.y) == dest_coords: + if self.dest.package: + self.pick_up_package(self.dest.package) + return + elif self.dest.is_rack: + self.unload_package(self.dest) + return + if star_dir == self.direction: self.x = next.x self.y = next.y From e54769abdd290446fba30db1c9cd915e164eab61 Mon Sep 17 00:00:00 2001 From: andrzej Date: Fri, 1 May 2020 13:48:13 +0200 Subject: [PATCH 05/10] =?UTF-8?q?Poprawka,=20aby=20agent=20nie=20przeje?= =?UTF-8?q?=C5=BCd=C5=BCa=C5=82=20przez=20rega=C5=82y=20gdy=20jest=20za?= =?UTF-8?q?=C5=82adowany,=20poprawka=20warunku=20odpalenia=20rekurencyjnie?= =?UTF-8?q?=20find=5Fnearest=5Frack=5Ffor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent.py | 8 +++----- main.py | 4 ++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/agent.py b/agent.py index 0f174a9..a565fe9 100644 --- a/agent.py +++ b/agent.py @@ -2,7 +2,6 @@ from warehouse import Coordinates, Tile, Pack from queue import PriorityQueue from math import sqrt from attributes import TURN_LEFT_DIRECTIONS, TURN_RIGHT_DIRECTIONS, PackStatus, PackSize -import pdb import pygame import sys @@ -160,7 +159,7 @@ class Agent: if not tile_on_map: return False next_tile = self.warehouse.tiles[next_coords.x][next_coords.y] - if not self.is_loaded: + if not self.is_loaded or (next_coords.x, next_coords.y) != (self.dest.x, self.dest.y): tile_passable = isinstance(next_tile, Tile) and next_tile.category.passable return tile_passable @@ -196,14 +195,13 @@ class Agent: quarter_racks = [[t for t in row if t.category.name == "Rack" and not t.occupied and t.category.pack_size in accepted_weights] for row in quarter] quarter_racks = [t for row in quarter_racks for t in row] racks_costs = [] - if not quarter_racks: - self.find_nearest_rack_for(package, expand_box+1) for rack in quarter_racks: new_node = Node(rack.x_position, rack.y_position) cost = self.heuristic(start_node, new_node) if cost > 0: racks_costs.append((rack, cost)) - rack = min(racks_costs, key=lambda l: l[1])[0] + + rack = self.find_nearest_rack_for(package, expand_box + 1) if not racks_costs else min(racks_costs, key=lambda l: l[1])[0] return rack diff --git a/main.py b/main.py index ee832a2..81d67b0 100644 --- a/main.py +++ b/main.py @@ -21,7 +21,7 @@ class MainGameFrame: self.display = pygame.display.set_mode(WINDOW_SIZE) agent_radius = int(TILE_WIDTH/2) self.agent_tex = pygame.image.load('forklift.png') - self.warehouse_map = warehouse.Warehouse(20, 20, 150, 20) + self.warehouse_map = warehouse.Warehouse(20, 20, 150, 40) starting_x, starting_y = self.set_starting_agent_position() self.agent = agent.Agent(starting_x, starting_y, self.warehouse_map, agent_radius) self.clock = pygame.time.Clock() @@ -37,7 +37,7 @@ class MainGameFrame: self.draw_agent() self.agent.move() pygame.display.update() - self.clock.tick(5) + self.clock.tick(8) def draw_floor(self): for x in range(self.warehouse_map.width): From 2cee41217039f7cf55eea93b2cce32ce0e109f4e Mon Sep 17 00:00:00 2001 From: andrzej Date: Sun, 3 May 2020 18:01:23 +0200 Subject: [PATCH 06/10] =?UTF-8?q?Dodanie=20obszaru=20b=C4=99d=C4=85cego=20?= =?UTF-8?q?ch=C5=82odni=C4=85,=20zmiana=20atrybut=C3=B3w=20rega=C5=82?= =?UTF-8?q?=C3=B3w=20i=20paczek,=20maj=C4=85=20teraz=20odpowiednio=20okre?= =?UTF-8?q?=C5=9Blon=C4=85=20pojemno=C5=9B=C4=87(losowan=C4=85=20z=20zakre?= =?UTF-8?q?su),=20oraz=20rozmiar.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- agent.py | 24 +++++++++++++++------- attributes.py | 9 ++------ main.py | 19 +++++++++++++---- warehouse.py | 57 +++++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 80 insertions(+), 29 deletions(-) diff --git a/agent.py b/agent.py index a565fe9..0b812ac 100644 --- a/agent.py +++ b/agent.py @@ -1,7 +1,7 @@ from warehouse import Coordinates, Tile, Pack from queue import PriorityQueue from math import sqrt -from attributes import TURN_LEFT_DIRECTIONS, TURN_RIGHT_DIRECTIONS, PackStatus, PackSize +from attributes import TURN_LEFT_DIRECTIONS, TURN_RIGHT_DIRECTIONS, PackStatus import pygame import sys @@ -89,7 +89,16 @@ class Agent: def heuristic(self, start: Node, goal: Node): diff_x = pow(goal.x - start.x, 2) diff_y = pow(goal.y - start.y, 2) - return round(sqrt(diff_x + diff_y), 3) + additional_cost = 0 + # if diff_x < diff_y: + # column = self.warehouse.tiles[start.x] + # tiles = column[goal.y:start.y] if goal.y < start.y else column[start.y:goal.y] + # additional_cost += len([t for t in tiles if not t.category.passable]) + # elif diff_x > diff_y: + # row = [col[start.x] for col in self.warehouse.tiles] + # tiles = row[goal.x:start.x] if goal.x < start.x else row[start.x:goal.x] + # additional_cost += len([t for t in tiles if not t.category.passable]) + return round(sqrt(diff_x + diff_y), 3) + float(10*additional_cost) def check_if_open(self, node: Node): return (node.x, node.y) in [(n.x, n.y) for (_,n) in self.open.queue] @@ -183,7 +192,6 @@ class Agent: def find_nearest_rack_for(self, package, expand_box=0): weight = package.size - accepted_weights = [weight, PackSize.ALL] start_node = Node(self.x, self.y) quarter_x = int(self.warehouse.width/4) + expand_box quarter_y = int(self.warehouse.height/4) + expand_box @@ -192,7 +200,7 @@ class Agent: start_quarter_y = self.y - quarter_y if self.y - quarter_y > 0 else 0 end_quarter_y = self.y + quarter_y if self.y + quarter_y < self.warehouse.height else self.warehouse.height - 1 quarter = [row[start_quarter_y:end_quarter_y] for row in self.warehouse.tiles[start_quarter_x:end_quarter_x]] - quarter_racks = [[t for t in row if t.category.name == "Rack" and not t.occupied and t.category.pack_size in accepted_weights] for row in quarter] + quarter_racks = [[t for t in row if t.category.name in self.warehouse.storage_types and t.capacity >= weight] for row in quarter] quarter_racks = [t for row in quarter_racks for t in row] racks_costs = [] for rack in quarter_racks: @@ -208,7 +216,9 @@ class Agent: def pick_up_package(self, pack): self.warehouse.packages.remove(pack) self.is_loaded = True - self.warehouse.tiles[pack.lays_on_field.x_position][pack.lays_on_field.y_position].occupied = False + if pack.lays_on_field.category.name in ['Rack', 'Fridge']: + pack.lays_on_field.capacity += pack.size + self.dest.package = None pack.lays_on_field = None self.transported_package = pack @@ -219,6 +229,6 @@ class Agent: self.transported_package = None self.is_loaded = False pack.lays_on_field = tile - self.warehouse.tiles[rack.x][rack.y].occupied = True + pack.lays_on_field.capacity -= pack.size pack.status = PackStatus.STORED - self.warehouse.packages.append(pack) \ No newline at end of file + self.warehouse.packages.append(pack) diff --git a/attributes.py b/attributes.py index 204b476..ad158f5 100644 --- a/attributes.py +++ b/attributes.py @@ -1,11 +1,5 @@ from enum import Enum -class PackSize(Enum): - ALL = 0 - SMALL = 1 - MEDIUM = 2 - LARGE = 3 - HUGE = 4 class PackStatus(Enum): LOOSE = 0 @@ -25,7 +19,8 @@ COLORS = { 'yellow': (235, 235, 0), 'lightgreen': (70, 238, 70), 'red': (255, 0, 0), - 'lightblue': (135, 206, 250), + 'lightblue': (120, 180, 230), + 'iceblue': (186,242,239), 'orange': (255, 165, 0) } diff --git a/main.py b/main.py index 81d67b0..683bfd0 100644 --- a/main.py +++ b/main.py @@ -3,14 +3,16 @@ import warehouse import agent import random import sys -from attributes import PackSize, PackStatus, COLORS, DIRECTION_ANGLES +from attributes import PackStatus, COLORS, DIRECTION_ANGLES WINDOW_SIZE = (640, 640) COLOR_OF_FIELD = { 'Floor': 'gray', 'Rack': 'white', 'Pack': 'yellow', - 'path': 'orange' + 'path': 'orange', + 'FridgeFloor': 'lightblue', + 'Fridge': 'iceblue' } TILE_WIDTH = 32 TILE_HEIGHT = 32 @@ -18,9 +20,12 @@ CIRCLE_CENTER_X, CIRCLE_CENTER_Y = int(TILE_WIDTH/2), int(TILE_HEIGHT/2) class MainGameFrame: def __init__(self): + pygame.font.init() self.display = pygame.display.set_mode(WINDOW_SIZE) + pygame.display.set_caption("Smart ForkLift") agent_radius = int(TILE_WIDTH/2) self.agent_tex = pygame.image.load('forklift.png') + self.font = pygame.font.Font('freesansbold.ttf', 16) self.warehouse_map = warehouse.Warehouse(20, 20, 150, 40) starting_x, starting_y = self.set_starting_agent_position() self.agent = agent.Agent(starting_x, starting_y, self.warehouse_map, agent_radius) @@ -35,6 +40,7 @@ class MainGameFrame: self.draw_floor() self.draw_packages() self.draw_agent() + self.draw_nums() self.agent.move() pygame.display.update() self.clock.tick(8) @@ -52,8 +58,6 @@ class MainGameFrame: def draw_field(self, x, y): current_tile = self.warehouse_map.tiles[x][y] - # if not isinstance(current_tile, warehouse.Tile): - # current_tile = current_tile.lays_on_field if isinstance(current_tile, warehouse.Pack) else None color = COLOR_OF_FIELD.get(current_tile.category.name, 'white') color = COLORS[color] if (current_tile.x_position,current_tile.y_position) in [(a.x, a.y) for a in self.agent.path]: @@ -78,6 +82,13 @@ class MainGameFrame: pygame.draw.rect(self.display, package_color, ((pack_x * TILE_WIDTH) + 3, (pack_y * TILE_HEIGHT) + 3, TILE_WIDTH - 5, TILE_HEIGHT - 5)) + def draw_nums(self): + for row in self.warehouse_map.tiles: + for cell in row: + if cell.category.name in self.warehouse_map.storage_types: + text_surface = self.font.render(str(cell.capacity), True, (0, 0, 0)) + self.display.blit(text_surface, ((cell.x_position * TILE_WIDTH) + 1, (cell.y_position * TILE_HEIGHT) + 6)) + def draw_agent(self): rotated = pygame.transform.rotate(self.agent_tex, DIRECTION_ANGLES.get(self.agent.direction)) self.display.blit(rotated, (self.agent.x*TILE_WIDTH, self.agent.y*TILE_WIDTH)) diff --git a/warehouse.py b/warehouse.py index 4b3ba19..d618d90 100644 --- a/warehouse.py +++ b/warehouse.py @@ -1,4 +1,4 @@ -from attributes import PackSize, PackStatus +from attributes import PackStatus import random import queue from collections import namedtuple @@ -7,18 +7,17 @@ import itertools Coordinates = namedtuple("Coordinates",'x y') class CategoryData: - def __init__(self, name, passable=False, can_store=True, location='general', pack_size=PackSize.ALL): + def __init__(self, name, passable=False, can_store=True, location='general'): self.name = name self.passable = passable self.can_store = can_store self.location = location - self.pack_size = pack_size def __repr__(self): return self.name class Pack: - def __init__(self, size=PackSize.MEDIUM, categ='general', lays_on_field=None): + def __init__(self, size=5, categ='general', lays_on_field=None): self.size = size self.category = categ assert isinstance(lays_on_field, Tile), AssertionError("Attribute lays_on_field must be a Tile object! You know, package cannot lay in void :)") @@ -27,16 +26,17 @@ class Pack: def set_status(self): status = PackStatus.LOOSE - if self.lays_on_field.category.name == 'Floor': + if self.lays_on_field.category.name in ['Floor', 'FridgeFloor']: status = PackStatus.LOOSE - elif self.lays_on_field.category.name == 'Rack': + elif self.lays_on_field.category.name in ['Rack', 'Fridge']: status = PackStatus.STORED return status CATEGORY = { 'floor': CategoryData('Floor', True, False), #lava 'rack': CategoryData('Rack', False, True), - # 'freezer': CategoryData('Freezer', False, True, location='cold_room') + 'fridge_floor': CategoryData('FridgeFloor', True, False, location='cold_room'), + 'fridge': CategoryData('Fridge', False, True, location='cold_room') } @@ -46,9 +46,11 @@ class Warehouse: self.height = length self.tiles = self.generate_map() self.no_of_racks = no_of_racks + self.storage_types = ["Rack", "Fridge"] self.no_of_packages = no_of_packages self.generate_racks() self.open_isolated_areas() + self.create_fridge(10) self.packages = self.place_packages(no_of_packages) self.tiles[1][1] = Tile('floor', 1, 1) def __str__(self): @@ -68,7 +70,7 @@ class Warehouse: node_x, node_y = random.randrange(1, self.width-1), random.randrange(1, self.height-1) node = self.tiles[node_x][node_y] next_node = None - self.tiles[node_x][node_y] = Tile('rack', node_x, node_y) + self.tiles[node_x][node_y] = Tile('rack', node_x, node_y, capacity=random.randrange(16, 20)) q.put(node) while not q.empty(): if next_node is not None: @@ -92,6 +94,37 @@ class Warehouse: node_y = next_node.y_position self.tiles[node_x][node_y] = Tile('rack', node_x, node_y) + def create_fridge(self, size): + x_corner = random.choice(['left', 'right']) + y_corner = random.choice(['top', 'bottom']) + start_x = None + start_y = None + end_x = None + end_y = None + if x_corner == 'left': + start_x = 0 + end_x = size + else: + # import pdb + # pdb.set_trace() + start_x = (self.width-1) - size + end_x = self.width - 1 + + if y_corner == 'top': + start_y = 0 + end_y = size + else: + start_y = (self.height-1) - size + end_y = self.height - 1 + + rows = self.tiles[start_x:end_x].copy() + for num, row in enumerate(rows, start_x): + for index, tile in enumerate(row[start_y:end_y], start_y): + if self.tiles[num][index].category.name == "Floor": + self.tiles[num][index] = Tile('fridge_floor', num, index) + else: + self.tiles[num][index] = Tile('fridge', num, index, capacity=random.randrange(10, 12)) + def get_not_rack_nodes(self, node_x, node_y): adjacent_tiles = self.get_adjacent_tiles(node_x, node_y) possible_nodes = [line for line in adjacent_tiles if line.category.name != "Rack"] @@ -161,8 +194,10 @@ class Warehouse: for i in range(no_of_packages): pack_x, pack_y = self._set_package_position() package_field = self.tiles[pack_x][pack_y] - self.tiles[pack_x][pack_y].occupied = True new_package = Pack(lays_on_field=package_field) + new_package.size = random.randrange(1, 10) + if package_field.category.name in self.storage_types: + package_field.capacity -= new_package.size packages.append(new_package) return packages def _set_package_position(self): @@ -203,11 +238,11 @@ class Warehouse: return wall class Tile: - def __init__(self, category, x_position, y_position): + def __init__(self, category, x_position, y_position, capacity=10): self.category = CATEGORY.get(category, CATEGORY['floor']) self.x_position = x_position self.y_position = y_position - self.occupied = False + self.capacity = capacity def __str__(self): From b9fa16a5a95cd6163942676ff5f87aa1139d185f Mon Sep 17 00:00:00 2001 From: andrzej Date: Sun, 3 May 2020 18:21:25 +0200 Subject: [PATCH 07/10] Poprawka wizualna --- attributes.py | 3 ++- main.py | 4 ++++ warehouse.py | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/attributes.py b/attributes.py index ad158f5..3f959e6 100644 --- a/attributes.py +++ b/attributes.py @@ -20,7 +20,8 @@ COLORS = { 'lightgreen': (70, 238, 70), 'red': (255, 0, 0), 'lightblue': (120, 180, 230), - 'iceblue': (186,242,239), + 'iceblue': (186, 242, 239), + 'blue': (0, 0, 255), 'orange': (255, 165, 0) } diff --git a/main.py b/main.py index 683bfd0..adb376e 100644 --- a/main.py +++ b/main.py @@ -81,6 +81,10 @@ class MainGameFrame: package_color = get_package_color(pack) pygame.draw.rect(self.display, package_color, ((pack_x * TILE_WIDTH) + 3, (pack_y * TILE_HEIGHT) + 3, TILE_WIDTH - 5, TILE_HEIGHT - 5)) + if pack.category == "freezed": + pygame.draw.rect(self.display, COLORS['blue'], + ((pack_x * TILE_WIDTH) + 2, (pack_y * TILE_HEIGHT) + 2, TILE_WIDTH - 4, + TILE_HEIGHT - 4), 3) def draw_nums(self): for row in self.warehouse_map.tiles: diff --git a/warehouse.py b/warehouse.py index d618d90..64d99a5 100644 --- a/warehouse.py +++ b/warehouse.py @@ -195,6 +195,9 @@ class Warehouse: pack_x, pack_y = self._set_package_position() package_field = self.tiles[pack_x][pack_y] new_package = Pack(lays_on_field=package_field) + categ_seed = random.random() + if categ_seed > 0.8: + new_package.category = "freezed" new_package.size = random.randrange(1, 10) if package_field.category.name in self.storage_types: package_field.capacity -= new_package.size From ba69a06e3f80205bdbb1f694cbd311d61536f11a Mon Sep 17 00:00:00 2001 From: andrzej Date: Thu, 7 May 2020 16:42:19 +0200 Subject: [PATCH 08/10] Poprawki --- agent.py | 12 ++++++++---- main.py | 6 +++--- warehouse.py | 6 +++++- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/agent.py b/agent.py index 0b812ac..cb08386 100644 --- a/agent.py +++ b/agent.py @@ -162,7 +162,7 @@ class Agent: self.path.append(next) self.closed = [] - def check_if_can_move(self, next_coords: Coordinates, rack_searching=False): + def check_if_can_move(self, next_coords: Coordinates): tile_on_map = 0 <= next_coords.x < self.warehouse.width and 0 <= next_coords.y < self.warehouse.height tile_passable = True if not tile_on_map: @@ -185,13 +185,17 @@ class Agent: if cost > 0: packages_costs.append((package, cost)) if not packages_costs: - pygame.quit() - sys.exit() + return + # pygame.quit() + # sys.exit() package = min(packages_costs, key=lambda l: l[1])[0] return package def find_nearest_rack_for(self, package, expand_box=0): weight = package.size + storage = "Rack" + if package.category == "freezed": + storage = "Fridge" start_node = Node(self.x, self.y) quarter_x = int(self.warehouse.width/4) + expand_box quarter_y = int(self.warehouse.height/4) + expand_box @@ -200,7 +204,7 @@ class Agent: start_quarter_y = self.y - quarter_y if self.y - quarter_y > 0 else 0 end_quarter_y = self.y + quarter_y if self.y + quarter_y < self.warehouse.height else self.warehouse.height - 1 quarter = [row[start_quarter_y:end_quarter_y] for row in self.warehouse.tiles[start_quarter_x:end_quarter_x]] - quarter_racks = [[t for t in row if t.category.name in self.warehouse.storage_types and t.capacity >= weight] for row in quarter] + quarter_racks = [[t for t in row if t.category.name == storage and t.capacity >= weight] for row in quarter] quarter_racks = [t for row in quarter_racks for t in row] racks_costs = [] for rack in quarter_racks: diff --git a/main.py b/main.py index adb376e..2a8cab4 100644 --- a/main.py +++ b/main.py @@ -26,7 +26,7 @@ class MainGameFrame: agent_radius = int(TILE_WIDTH/2) self.agent_tex = pygame.image.load('forklift.png') self.font = pygame.font.Font('freesansbold.ttf', 16) - self.warehouse_map = warehouse.Warehouse(20, 20, 150, 40) + self.warehouse_map = warehouse.Warehouse(20, 20, 250, 20) starting_x, starting_y = self.set_starting_agent_position() self.agent = agent.Agent(starting_x, starting_y, self.warehouse_map, agent_radius) self.clock = pygame.time.Clock() @@ -43,7 +43,7 @@ class MainGameFrame: self.draw_nums() self.agent.move() pygame.display.update() - self.clock.tick(8) + self.clock.tick(5) def draw_floor(self): for x in range(self.warehouse_map.width): @@ -91,7 +91,7 @@ class MainGameFrame: for cell in row: if cell.category.name in self.warehouse_map.storage_types: text_surface = self.font.render(str(cell.capacity), True, (0, 0, 0)) - self.display.blit(text_surface, ((cell.x_position * TILE_WIDTH) + 1, (cell.y_position * TILE_HEIGHT) + 6)) + self.display.blit(text_surface, ((cell.x_position * TILE_WIDTH) + 6, (cell.y_position * TILE_HEIGHT) + 6)) def draw_agent(self): rotated = pygame.transform.rotate(self.agent_tex, DIRECTION_ANGLES.get(self.agent.direction)) diff --git a/warehouse.py b/warehouse.py index 64d99a5..08843bf 100644 --- a/warehouse.py +++ b/warehouse.py @@ -29,7 +29,10 @@ class Pack: if self.lays_on_field.category.name in ['Floor', 'FridgeFloor']: status = PackStatus.LOOSE elif self.lays_on_field.category.name in ['Rack', 'Fridge']: - status = PackStatus.STORED + if self.category == 'freezed' and self.lays_on_field.category.name != 'Fridge': + status = PackStatus.STORED_BAD_LOCATION + else: + status = PackStatus.STORED return status CATEGORY = { @@ -198,6 +201,7 @@ class Warehouse: categ_seed = random.random() if categ_seed > 0.8: new_package.category = "freezed" + new_package.set_status() new_package.size = random.randrange(1, 10) if package_field.category.name in self.storage_types: package_field.capacity -= new_package.size From 5bf27c46ada0c26da48df6d8cec80cf50a8312d5 Mon Sep 17 00:00:00 2001 From: andrzej Date: Thu, 7 May 2020 17:39:00 +0200 Subject: [PATCH 09/10] =?UTF-8?q?Zmiana=20ilo=C5=9Bci=20paczek?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 995ce72..17888a5 100644 --- a/main.py +++ b/main.py @@ -21,7 +21,7 @@ class MainGameFrame: self.display = pygame.display.set_mode(WINDOW_SIZE) agent_radius = int(TILE_WIDTH/2) self.agent_tex = pygame.image.load('forklift.png') - self.warehouse_map = warehouse.Warehouse(20, 20, 150, 2) + self.warehouse_map = warehouse.Warehouse(20, 20, 150, 10) starting_x, starting_y = self.set_starting_agent_position() self.agent = agent.Agent(starting_x, starting_y, self.warehouse_map, agent_radius) self.clock = pygame.time.Clock() From ace8f744407bf81d99e9c6eed8b5c3ad9a06bd92 Mon Sep 17 00:00:00 2001 From: andrzej Date: Thu, 7 May 2020 21:35:24 +0200 Subject: [PATCH 10/10] Poprawki --- main.py | 1 - warehouse.py | 16 +++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index a476d89..6243e62 100644 --- a/main.py +++ b/main.py @@ -49,7 +49,6 @@ class MainGameFrame: for x in range(self.warehouse_map.width): for y in range(self.warehouse_map.height): self.draw_field(x, y) - self.draw_target(1, 1) def draw_target(self, x, y): target_screen_position = ( diff --git a/warehouse.py b/warehouse.py index 08843bf..e3de507 100644 --- a/warehouse.py +++ b/warehouse.py @@ -191,25 +191,27 @@ class Warehouse: racks.append(y) return racks - - def place_packages(self, no_of_packages): + def place_packages(self, no_of_packages: int): packages = [] for i in range(no_of_packages): - pack_x, pack_y = self._set_package_position() + new_package_size = random.randrange(1, 10) + pack_x, pack_y = self._set_package_position(new_package_size) package_field = self.tiles[pack_x][pack_y] new_package = Pack(lays_on_field=package_field) categ_seed = random.random() if categ_seed > 0.8: new_package.category = "freezed" - new_package.set_status() - new_package.size = random.randrange(1, 10) + # new_package.set_status() + new_package.size = new_package_size if package_field.category.name in self.storage_types: package_field.capacity -= new_package.size packages.append(new_package) return packages - def _set_package_position(self): + + def _set_package_position(self, pack_size: int): starting_x, starting_y = random.randrange(self.width), random.randrange(self.height) - while not isinstance(self.tiles[starting_x][starting_y], Tile): + while not isinstance(self.tiles[starting_x][starting_y], Tile) \ + and self.tiles[starting_x][starting_y].size - pack_size < 0: starting_x, starting_y = random.randrange(self.width), random.randrange( self.height) return starting_x, starting_y