2022-05-11 13:07:26 +02:00
|
|
|
import time
|
2022-04-25 17:26:32 +02:00
|
|
|
from Empty import Empty
|
2022-05-11 13:07:26 +02:00
|
|
|
from Finding_fields import Finding_fields
|
2022-04-25 17:26:32 +02:00
|
|
|
from Moving_truck import Moving_truck
|
|
|
|
from Package_types import Package_types
|
2022-05-08 21:20:46 +02:00
|
|
|
from Packages_spawner import Packages_spawner
|
2022-03-24 20:02:21 +01:00
|
|
|
from Shelf import Shelf
|
2022-04-04 20:08:45 +02:00
|
|
|
import pygame
|
2022-04-25 17:26:32 +02:00
|
|
|
import random
|
2022-03-24 20:02:21 +01:00
|
|
|
from Grid import Grid
|
2022-04-25 17:26:32 +02:00
|
|
|
from Truck import Truck
|
|
|
|
from Global_variables import Global_variables as G_var
|
2022-05-12 21:16:05 +02:00
|
|
|
from Sectors_types import Sectors_types
|
|
|
|
from decision_tree.Decision_tree import DecisionTree
|
2022-03-24 20:02:21 +01:00
|
|
|
|
2022-05-08 16:43:43 +02:00
|
|
|
from Astar import Pathfinding, State
|
2022-04-29 02:34:41 +02:00
|
|
|
|
2022-03-24 20:02:21 +01:00
|
|
|
|
|
|
|
class Environment:
|
|
|
|
def __init__(self, window):
|
|
|
|
self.window = window
|
2022-04-25 17:26:32 +02:00
|
|
|
self.grid = Grid(window)
|
|
|
|
self.initialize_eviroment_2d()
|
|
|
|
self.add_shelfs_to_enviroment_2d()
|
|
|
|
# TEST CREATE PACKAGE
|
2022-05-12 21:16:05 +02:00
|
|
|
self.package_spawner = Packages_spawner(window, self.enviroment_2d)
|
|
|
|
self.sector_decision = self.package_spawner.spawn_package()
|
2022-04-25 17:26:32 +02:00
|
|
|
new_truck = Truck(window, 14, 7)
|
|
|
|
self.enviroment_2d[14][7] = new_truck
|
|
|
|
self.truck = new_truck
|
|
|
|
self.moving_truck = Moving_truck(
|
2022-05-08 21:20:46 +02:00
|
|
|
self.window, self.enviroment_2d, self.truck, self.package_spawner)
|
2022-04-29 02:34:41 +02:00
|
|
|
self.astar = Pathfinding(self.enviroment_2d)
|
2022-05-11 13:07:26 +02:00
|
|
|
self.finding_fields = Finding_fields(self.enviroment_2d)
|
2022-05-12 21:16:05 +02:00
|
|
|
self.weekend = random.randint(0, 1)
|
2022-03-24 20:02:21 +01:00
|
|
|
|
2022-04-25 17:26:32 +02:00
|
|
|
def draw_all_elements(self):
|
|
|
|
for row in self.enviroment_2d:
|
|
|
|
for field in row:
|
|
|
|
field.draw()
|
2022-03-24 20:02:21 +01:00
|
|
|
self.grid.draw_grid()
|
2022-05-08 20:57:20 +02:00
|
|
|
# self.astar.draw_path(self.window)
|
2022-04-25 17:26:32 +02:00
|
|
|
pygame.display.flip()
|
2022-05-08 16:43:43 +02:00
|
|
|
|
2022-05-11 13:14:27 +02:00
|
|
|
def update_all_elements(self):
|
2022-05-11 13:07:26 +02:00
|
|
|
self.use_astar() # wywyoływanie za każdym razem astar jest bardzo zasobożerne. Lepiej raz na przejście
|
|
|
|
self.update_truck()
|
|
|
|
time.sleep(0.5)
|
2022-05-12 21:16:05 +02:00
|
|
|
|
|
|
|
# def use_decision_tree(self):
|
|
|
|
# marking = self.package.type
|
|
|
|
# if marking == Package_types.fragile:
|
|
|
|
# marking = 0
|
|
|
|
# elif marking == Package_types.priority:
|
|
|
|
# marking = 1
|
|
|
|
# tree = DecisionTree(marking, self.weekend, self.package.company.popularity,
|
|
|
|
# self.package.company.payment_delay, self.package.payed_upfront,
|
|
|
|
# self.package.company.shipping_type)
|
|
|
|
# decision = tree.decision
|
|
|
|
# return decision
|
|
|
|
|
2022-04-29 02:34:41 +02:00
|
|
|
def use_astar(self):
|
2022-05-11 13:07:26 +02:00
|
|
|
start_state = State(1,self.truck.x,self.truck.y) # sprawić aby paczka i shelf były wyszukiwane raz
|
|
|
|
if self.truck.has_package:
|
2022-05-12 21:16:05 +02:00
|
|
|
end_position = self.finding_fields.find_closest_shelf(self.truck, self.truck.package_type,
|
|
|
|
self.sector_decision)
|
2022-05-11 13:07:26 +02:00
|
|
|
else:
|
|
|
|
end_position = self.finding_fields.find_package()
|
|
|
|
end_state = State(1,end_position.x, end_position.y)
|
2022-04-29 02:34:41 +02:00
|
|
|
self.astar.find_path(start_state,end_state)
|
|
|
|
|
2022-05-11 13:07:26 +02:00
|
|
|
def update_truck(self):
|
|
|
|
next_field_to_move = self.astar.path[0].state
|
|
|
|
next_field_x = next_field_to_move.x - self.truck.x
|
|
|
|
next_field_y = next_field_to_move.y - self.truck.y
|
|
|
|
self.moving_truck.move(next_field_x,next_field_y)
|
2022-04-04 20:08:45 +02:00
|
|
|
|
2022-05-12 21:16:05 +02:00
|
|
|
# def gen_shelf_type(self):
|
|
|
|
# shelve_types = list(Package_types)
|
|
|
|
# while True:
|
|
|
|
# yield random.choice(shelve_types)
|
|
|
|
def gen__sectors(self):
|
|
|
|
sectors = [Sectors_types.normal, Sectors_types.shipping_tomorrow, Sectors_types.shipping_today]
|
2022-04-25 17:26:32 +02:00
|
|
|
while True:
|
2022-05-12 21:16:05 +02:00
|
|
|
yield random.choice(sectors)
|
2022-04-04 20:08:45 +02:00
|
|
|
|
2022-04-25 17:26:32 +02:00
|
|
|
def initialize_eviroment_2d(self):
|
|
|
|
self.enviroment_2d = [[
|
|
|
|
Empty(self.window, j, i)
|
|
|
|
for i in range(G_var().DIMENSION_Y)]
|
|
|
|
for j in range(G_var().DIMENSION_X)
|
|
|
|
]
|
2022-04-04 20:08:45 +02:00
|
|
|
|
2022-04-25 17:26:32 +02:00
|
|
|
def add_shelfs_to_enviroment_2d(self):
|
|
|
|
shelf_2_offset = 9
|
2022-05-12 21:16:05 +02:00
|
|
|
avaiable_sectors = self.gen__sectors()
|
|
|
|
for x in range(8, 22, 3):
|
|
|
|
type_of_new_shelf = Package_types.priority
|
|
|
|
sector_of_new_shelf1 = next(avaiable_sectors)
|
|
|
|
# print(sector_of_new_shelf1)
|
|
|
|
sector_of_new_shelf2 = next(avaiable_sectors)
|
|
|
|
# print(sector_of_new_shelf2)
|
|
|
|
for y in range(0, 6):
|
|
|
|
self.enviroment_2d[x][y] = Shelf(
|
|
|
|
self.window, x, y, type_of_new_shelf, sector_of_new_shelf1
|
|
|
|
)
|
|
|
|
self.enviroment_2d[x][y + shelf_2_offset] = Shelf(
|
|
|
|
self.window, x, (y + shelf_2_offset), type_of_new_shelf, sector_of_new_shelf2
|
|
|
|
)
|
|
|
|
|
|
|
|
for x in range(2, 7, 3):
|
|
|
|
type_of_new_shelf = Package_types.fragile
|
|
|
|
sector_of_new_shelf = Sectors_types.fragile
|
2022-04-25 17:26:32 +02:00
|
|
|
for y in range(0, 6):
|
|
|
|
self.enviroment_2d[x][y] = Shelf(
|
2022-05-12 21:16:05 +02:00
|
|
|
self.window, x, y, type_of_new_shelf, sector_of_new_shelf
|
2022-04-25 17:26:32 +02:00
|
|
|
)
|
|
|
|
self.enviroment_2d[x][y + shelf_2_offset] = Shelf(
|
2022-05-12 21:16:05 +02:00
|
|
|
self.window, x, (y + shelf_2_offset), type_of_new_shelf, sector_of_new_shelf
|
2022-04-25 17:26:32 +02:00
|
|
|
)
|