2020-04-02 15:25:15 +02:00
|
|
|
import pygame
|
|
|
|
import warehouse
|
2020-04-04 22:03:15 +02:00
|
|
|
import agent
|
2020-05-11 13:42:10 +02:00
|
|
|
from genetic_algorithm import gen_alg
|
2020-04-04 22:03:15 +02:00
|
|
|
import random
|
2020-04-06 15:18:00 +02:00
|
|
|
import sys
|
2020-05-03 18:01:23 +02:00
|
|
|
from attributes import PackStatus, COLORS, DIRECTION_ANGLES
|
2020-04-02 15:25:15 +02:00
|
|
|
|
2020-04-30 16:36:55 +02:00
|
|
|
WINDOW_SIZE = (640, 640)
|
2020-04-04 22:03:15 +02:00
|
|
|
COLOR_OF_FIELD = {
|
|
|
|
'Floor': 'gray',
|
|
|
|
'Rack': 'white',
|
2020-04-28 22:03:48 +02:00
|
|
|
'Pack': 'yellow',
|
2020-05-03 18:01:23 +02:00
|
|
|
'path': 'orange',
|
|
|
|
'FridgeFloor': 'lightblue',
|
|
|
|
'Fridge': 'iceblue'
|
2020-04-04 22:03:15 +02:00
|
|
|
}
|
2020-04-30 16:36:55 +02:00
|
|
|
TILE_WIDTH = 32
|
|
|
|
TILE_HEIGHT = 32
|
2020-04-04 22:03:15 +02:00
|
|
|
CIRCLE_CENTER_X, CIRCLE_CENTER_Y = int(TILE_WIDTH/2), int(TILE_HEIGHT/2)
|
2020-04-02 15:25:15 +02:00
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
class MainGameFrame:
|
2020-06-15 14:10:33 +02:00
|
|
|
def __init__(self, mutation_prob=0.03, generation_size=40, number_of_generations=100, amount_of_promotion=0):
|
2020-05-03 18:01:23 +02:00
|
|
|
pygame.font.init()
|
2020-04-04 22:03:15 +02:00
|
|
|
self.display = pygame.display.set_mode(WINDOW_SIZE)
|
2020-05-03 18:01:23 +02:00
|
|
|
pygame.display.set_caption("Smart ForkLift")
|
2020-04-06 16:11:57 +02:00
|
|
|
agent_radius = int(TILE_WIDTH/2)
|
2020-04-30 16:36:55 +02:00
|
|
|
self.agent_tex = pygame.image.load('forklift.png')
|
2020-06-15 03:17:57 +02:00
|
|
|
self.agent_tex2 = pygame.image.load('forklift_loaded.png')
|
2020-05-03 18:01:23 +02:00
|
|
|
self.font = pygame.font.Font('freesansbold.ttf', 16)
|
2020-05-11 12:17:58 +02:00
|
|
|
self.warehouse_map = warehouse.Warehouse(20, 20, 150, 10)
|
2020-04-05 15:04:38 +02:00
|
|
|
starting_x, starting_y = self.set_starting_agent_position()
|
2020-04-06 16:11:57 +02:00
|
|
|
self.agent = agent.Agent(starting_x, starting_y, self.warehouse_map, agent_radius)
|
2020-04-07 01:18:04 +02:00
|
|
|
self.clock = pygame.time.Clock()
|
2020-05-11 13:42:10 +02:00
|
|
|
packs_coords = [(pack.lays_on_field.x_position, pack.lays_on_field.y_position) for pack in self.warehouse_map.packages]
|
|
|
|
list_of_racks = self.warehouse_map.get_all_racks(True)
|
|
|
|
racks_coords = [(line.x_position, line.y_position) for line in list_of_racks]
|
2020-06-15 14:10:33 +02:00
|
|
|
packs_sizes = [pack for pack in self.warehouse_map.packages]
|
|
|
|
racks_capacities = list_of_racks
|
2020-05-11 15:13:28 +02:00
|
|
|
# print("koordynaty paczek: ",packs_coords)
|
|
|
|
# print("koordynaty regałów: ",racks_coords)
|
2020-05-11 13:42:10 +02:00
|
|
|
print("wagi paczek: ",packs_sizes)
|
2020-05-11 14:54:33 +02:00
|
|
|
print("pojemności regałów: ",racks_capacities)
|
2020-06-15 11:55:24 +02:00
|
|
|
gen_alg(packs_sizes, racks_capacities, number_of_generations, generation_size, mutation_prob, amount_of_promotion, self.agent.location_classifier)
|
2020-06-15 03:17:57 +02:00
|
|
|
self.agent.create_graph_map()
|
|
|
|
self.agent.trace_route()
|
|
|
|
self.agent.find_path()
|
2020-04-07 18:50:25 +02:00
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
def run(self):
|
|
|
|
while True:
|
2020-04-06 15:18:00 +02:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
pygame.quit()
|
|
|
|
sys.exit()
|
|
|
|
self.draw_floor()
|
|
|
|
self.draw_packages()
|
|
|
|
self.draw_agent()
|
2020-05-03 18:01:23 +02:00
|
|
|
self.draw_nums()
|
2020-04-27 22:14:07 +02:00
|
|
|
self.agent.move()
|
2020-05-11 12:17:58 +02:00
|
|
|
packages_to_go = [p for p in self.warehouse_map.packages if p.status != PackStatus.STORED]
|
|
|
|
if not packages_to_go and not self.agent.transported_package:
|
|
|
|
pygame.quit()
|
|
|
|
sys.exit()
|
2020-04-04 22:03:15 +02:00
|
|
|
pygame.display.update()
|
2020-05-11 12:17:58 +02:00
|
|
|
self.clock.tick(6)
|
2020-04-02 15:25:15 +02:00
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
def draw_floor(self):
|
|
|
|
for x in range(self.warehouse_map.width):
|
|
|
|
for y in range(self.warehouse_map.height):
|
2020-04-05 15:04:38 +02:00
|
|
|
self.draw_field(x, y)
|
2020-04-28 01:18:23 +02:00
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
def draw_field(self, x, y):
|
|
|
|
current_tile = self.warehouse_map.tiles[x][y]
|
|
|
|
color = COLOR_OF_FIELD.get(current_tile.category.name, 'white')
|
|
|
|
color = COLORS[color]
|
2020-04-28 22:03:48 +02:00
|
|
|
if (current_tile.x_position,current_tile.y_position) in [(a.x, a.y) for a in self.agent.path]:
|
|
|
|
color = COLORS.get('orange')
|
2020-04-04 22:03:15 +02:00
|
|
|
pygame.draw.rect(self.display, COLORS['black'],
|
|
|
|
(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT))
|
|
|
|
pygame.draw.rect(self.display, color,
|
|
|
|
((x * TILE_WIDTH) + 1, (y * TILE_HEIGHT) + 1, TILE_WIDTH - 1, TILE_HEIGHT - 1))
|
|
|
|
|
2020-04-05 15:04:38 +02:00
|
|
|
def draw_packages(self):
|
2020-04-05 15:26:28 +02:00
|
|
|
def get_package_color(pack):
|
|
|
|
colors = {
|
|
|
|
PackStatus.LOOSE: COLORS['yellow'],
|
|
|
|
PackStatus.STORED: COLORS['lightgreen'],
|
|
|
|
PackStatus.STORED_BAD_LOCATION: COLORS['red']
|
|
|
|
}
|
|
|
|
return colors[pack.status]
|
|
|
|
|
2020-04-05 15:04:38 +02:00
|
|
|
for pack in self.warehouse_map.packages:
|
|
|
|
pack_x, pack_y = pack.lays_on_field.x_position, pack.lays_on_field.y_position
|
2020-04-05 15:26:28 +02:00
|
|
|
package_color = get_package_color(pack)
|
2020-04-05 15:04:38 +02:00
|
|
|
pygame.draw.rect(self.display, package_color,
|
|
|
|
((pack_x * TILE_WIDTH) + 3, (pack_y * TILE_HEIGHT) + 3, TILE_WIDTH - 5, TILE_HEIGHT - 5))
|
2020-05-03 18:21:25 +02:00
|
|
|
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)
|
2020-04-05 15:04:38 +02:00
|
|
|
|
2020-05-03 18:01:23 +02:00
|
|
|
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))
|
2020-05-07 16:42:19 +02:00
|
|
|
self.display.blit(text_surface, ((cell.x_position * TILE_WIDTH) + 6, (cell.y_position * TILE_HEIGHT) + 6))
|
2020-06-15 03:17:57 +02:00
|
|
|
for package in self.warehouse_map.packages:
|
|
|
|
if package.status == PackStatus.LOOSE:
|
|
|
|
text_surface = self.font.render(str(package.id + 1), True, (0, 0, 0))
|
|
|
|
self.display.blit(text_surface, ((package.lays_on_field.x_position * TILE_WIDTH) + 6, (package.lays_on_field.y_position * TILE_HEIGHT) + 6))
|
2020-04-05 15:04:38 +02:00
|
|
|
|
2020-04-04 22:03:15 +02:00
|
|
|
def draw_agent(self):
|
2020-06-15 03:17:57 +02:00
|
|
|
if self.agent.is_loaded:
|
|
|
|
rotated = pygame.transform.rotate(self.agent_tex2, DIRECTION_ANGLES.get(self.agent.direction))
|
|
|
|
else:
|
|
|
|
rotated = pygame.transform.rotate(self.agent_tex, DIRECTION_ANGLES.get(self.agent.direction))
|
2020-04-30 16:36:55 +02:00
|
|
|
self.display.blit(rotated, (self.agent.x*TILE_WIDTH, self.agent.y*TILE_WIDTH))
|
2020-04-02 15:25:15 +02:00
|
|
|
|
2020-04-05 15:04:38 +02:00
|
|
|
def set_starting_agent_position(self):
|
|
|
|
starting_x, starting_y = random.randrange(self.warehouse_map.width), random.randrange(self.warehouse_map.height)
|
2020-04-05 15:26:28 +02:00
|
|
|
while not isinstance(self.warehouse_map.tiles[starting_x][starting_y], warehouse.Tile) or self.warehouse_map.tiles[starting_x][starting_y].category.name != 'Floor':
|
2020-04-05 15:04:38 +02:00
|
|
|
starting_x, starting_y = random.randrange(self.warehouse_map.width), random.randrange(
|
|
|
|
self.warehouse_map.height)
|
|
|
|
return starting_x, starting_y
|
2020-04-23 19:43:51 +02:00
|
|
|
|
2020-04-02 15:25:15 +02:00
|
|
|
if __name__ == '__main__':
|
2020-04-04 22:03:15 +02:00
|
|
|
maingame = MainGameFrame()
|
2020-04-27 22:14:07 +02:00
|
|
|
maingame.run()
|