decision tree
This commit is contained in:
parent
a33806e32a
commit
9e95c77404
1
Astar.py
1
Astar.py
@ -83,7 +83,6 @@ class Pathfinding:
|
||||
if(neighbour_x >= 0 and neighbour_x < G_var().DIMENSION_X and neighbour_y >= 0 and neighbour_y < G_var().DIMENSION_Y):
|
||||
neighbours.append(self.grid[neighbour_x][neighbour_y])
|
||||
return neighbours
|
||||
|
||||
|
||||
def find_path(self, starting_state, target_state): # algorytm wyszukiwania trasy
|
||||
start_node = self.grid[starting_state.x][starting_state.y]
|
||||
|
9
CompanyFactory.py
Normal file
9
CompanyFactory.py
Normal file
@ -0,0 +1,9 @@
|
||||
import random
|
||||
|
||||
|
||||
class CompanyFactory:
|
||||
def __init__(self):
|
||||
self.popularity = random.randint(0,5)
|
||||
self.payment_delay = random.randint(0,5)
|
||||
self.shipping_type = random.randint(0,1)
|
||||
|
@ -2,7 +2,6 @@ import time
|
||||
from Empty import Empty
|
||||
from Finding_fields import Finding_fields
|
||||
from Moving_truck import Moving_truck
|
||||
from Package import Package
|
||||
from Package_types import Package_types
|
||||
from Packages_spawner import Packages_spawner
|
||||
from Shelf import Shelf
|
||||
@ -11,7 +10,8 @@ import random
|
||||
from Grid import Grid
|
||||
from Truck import Truck
|
||||
from Global_variables import Global_variables as G_var
|
||||
from pygame.constants import *
|
||||
from Sectors_types import Sectors_types
|
||||
from decision_tree.Decision_tree import DecisionTree
|
||||
|
||||
from Astar import Pathfinding, State
|
||||
|
||||
@ -23,8 +23,8 @@ class Environment:
|
||||
self.initialize_eviroment_2d()
|
||||
self.add_shelfs_to_enviroment_2d()
|
||||
# TEST CREATE PACKAGE
|
||||
self.package_spawner = Packages_spawner(window,self.enviroment_2d)
|
||||
self.package_spawner.spawn_package()
|
||||
self.package_spawner = Packages_spawner(window, self.enviroment_2d)
|
||||
self.sector_decision = self.package_spawner.spawn_package()
|
||||
new_truck = Truck(window, 14, 7)
|
||||
self.enviroment_2d[14][7] = new_truck
|
||||
self.truck = new_truck
|
||||
@ -32,6 +32,7 @@ class Environment:
|
||||
self.window, self.enviroment_2d, self.truck, self.package_spawner)
|
||||
self.astar = Pathfinding(self.enviroment_2d)
|
||||
self.finding_fields = Finding_fields(self.enviroment_2d)
|
||||
self.weekend = random.randint(0, 1)
|
||||
|
||||
def draw_all_elements(self):
|
||||
for row in self.enviroment_2d:
|
||||
@ -45,12 +46,24 @@ class Environment:
|
||||
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)
|
||||
|
||||
|
||||
|
||||
# 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
|
||||
|
||||
def use_astar(self):
|
||||
start_state = State(1,self.truck.x,self.truck.y) # sprawić aby paczka i shelf były wyszukiwane raz
|
||||
if self.truck.has_package:
|
||||
end_position = self.finding_fields.find_closest_shelf(self.truck,self.truck.package_type)
|
||||
end_position = self.finding_fields.find_closest_shelf(self.truck, self.truck.package_type,
|
||||
self.sector_decision)
|
||||
else:
|
||||
end_position = self.finding_fields.find_package()
|
||||
end_state = State(1,end_position.x, end_position.y)
|
||||
@ -62,10 +75,14 @@ class Environment:
|
||||
next_field_y = next_field_to_move.y - self.truck.y
|
||||
self.moving_truck.move(next_field_x,next_field_y)
|
||||
|
||||
def gen_shelf_type(self):
|
||||
shelve_types = list(Package_types)
|
||||
# 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]
|
||||
while True:
|
||||
yield random.choice(shelve_types)
|
||||
yield random.choice(sectors)
|
||||
|
||||
def initialize_eviroment_2d(self):
|
||||
self.enviroment_2d = [[
|
||||
@ -76,14 +93,28 @@ class Environment:
|
||||
|
||||
def add_shelfs_to_enviroment_2d(self):
|
||||
shelf_2_offset = 9
|
||||
avaiable_types = self.gen_shelf_type()
|
||||
for x in range(2, 22, 3):
|
||||
type_of_new_shelf_1 = next(avaiable_types)
|
||||
type_of_new_shelf_2 = next(avaiable_types)
|
||||
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_1
|
||||
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_2
|
||||
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
|
||||
for y in range(0, 6):
|
||||
self.enviroment_2d[x][y] = Shelf(
|
||||
self.window, x, y, type_of_new_shelf, sector_of_new_shelf
|
||||
)
|
||||
self.enviroment_2d[x][y + shelf_2_offset] = Shelf(
|
||||
self.window, x, (y + shelf_2_offset), type_of_new_shelf, sector_of_new_shelf
|
||||
)
|
||||
|
@ -8,7 +8,7 @@ class Finding_fields:
|
||||
def __init__(self, enviroment_2d):
|
||||
self.enviroment_2d = enviroment_2d
|
||||
|
||||
def find_closest_shelf(self, start_field, type):
|
||||
def find_closest_shelf(self, start_field, type, sector):
|
||||
shelves = []
|
||||
for row in self.enviroment_2d:
|
||||
for field in row:
|
||||
@ -17,7 +17,7 @@ class Finding_fields:
|
||||
min_distance = math.inf
|
||||
closest_shelf = None
|
||||
for shelf in shelves:
|
||||
if shelf.type == type:
|
||||
if shelf.type == type and shelf.sector == sector:
|
||||
distance = abs(start_field.x - shelf.x) + abs(start_field.y - shelf.y)
|
||||
if distance < min_distance:
|
||||
min_distance = distance
|
||||
|
@ -6,6 +6,7 @@ from Global_variables import Global_variables as G_var
|
||||
from Types_colors import Types_colors
|
||||
from Package_types import Package_types
|
||||
import math
|
||||
from CompanyFactory import CompanyFactory
|
||||
|
||||
|
||||
class Package(Field):
|
||||
@ -13,13 +14,14 @@ class Package(Field):
|
||||
Field.__init__(self, window, x, y)
|
||||
self.mark_image = self.get_marking_photo()
|
||||
self.type = type
|
||||
self.company = CompanyFactory()
|
||||
self.payed_upfront = random.randint(0,1)
|
||||
self.is_placed = False
|
||||
|
||||
def get_marking_photo(self):
|
||||
file_path_type = ["resources/package_markings/*.jpg"]
|
||||
images = glob2.glob(random.choice(file_path_type))
|
||||
random_image = random.choice(images)
|
||||
print(random_image)
|
||||
return random_image
|
||||
|
||||
def draw(self):
|
||||
|
@ -2,6 +2,7 @@ import random
|
||||
from Global_variables import Global_variables as G_var
|
||||
from Package import Package
|
||||
from Package_types import Package_types
|
||||
from decision_tree.Decision_tree import DecisionTree
|
||||
|
||||
|
||||
class Packages_spawner:
|
||||
@ -10,8 +11,23 @@ class Packages_spawner:
|
||||
self.enviroment_2d = enviroment_2d
|
||||
|
||||
def spawn_package(self):
|
||||
package_x = random.randrange(22,26)
|
||||
package_y = random.randrange(1,13)
|
||||
package_x = random.randrange(22, 26)
|
||||
package_y = random.randrange(1, 13)
|
||||
weekend = random.randint(0,1)
|
||||
package_type = random.choice(list(Package_types))
|
||||
new_package = Package(self.window,package_x,package_y,package_type)
|
||||
self.enviroment_2d[package_x][package_y] = new_package
|
||||
new_package = Package(self.window, package_x, package_y, package_type)
|
||||
self.enviroment_2d[package_x][package_y] = new_package
|
||||
sector_type = self.use_decision_tree(new_package, weekend)
|
||||
return sector_type
|
||||
|
||||
def use_decision_tree(self, package, weekend):
|
||||
marking = package.type
|
||||
if marking == Package_types.fragile:
|
||||
marking = 0
|
||||
elif marking == Package_types.priority:
|
||||
marking = 1
|
||||
tree = DecisionTree(marking, weekend, package.company.popularity,
|
||||
package.company.payment_delay, package.payed_upfront,
|
||||
package.company.shipping_type)
|
||||
decision = tree.decision
|
||||
return decision
|
||||
|
8
Sectors_types.py
Normal file
8
Sectors_types.py
Normal file
@ -0,0 +1,8 @@
|
||||
import enum
|
||||
|
||||
|
||||
class Sectors_types(enum.Enum):
|
||||
normal = 1
|
||||
shipping_tomorrow = 2
|
||||
shipping_today = 3
|
||||
fragile = 4
|
3
Shelf.py
3
Shelf.py
@ -6,10 +6,11 @@ from Types_colors import Types_colors
|
||||
|
||||
|
||||
class Shelf(Field):
|
||||
def __init__(self, window, x, y, type):
|
||||
def __init__(self, window, x, y, type, sector):
|
||||
Field.__init__(self, window, x, y)
|
||||
self.type = type
|
||||
self.color = Types_colors.get_shelf_color(type)
|
||||
self.sector = sector
|
||||
self.rect = pygame.Rect(self.x * G_var().RECT_SIZE, self.y *
|
||||
G_var().RECT_SIZE, G_var().RECT_SIZE, G_var().RECT_SIZE)
|
||||
|
||||
|
65
decision_tree/Decision_tree.py
Normal file
65
decision_tree/Decision_tree.py
Normal file
@ -0,0 +1,65 @@
|
||||
import joblib
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas
|
||||
from sklearn.tree import DecisionTreeClassifier, export_text, plot_tree
|
||||
from Sectors_types import Sectors_types
|
||||
|
||||
|
||||
decisions = ["decision"]
|
||||
attributes = ["marking", "weekend", "c_popularity", "payment_delay", "payed", "shipping_method"]
|
||||
|
||||
|
||||
class DecisionTree:
|
||||
|
||||
def __init__(self, marking, weekend, c_popularity, payment_delay, payed, shipping_method):
|
||||
self.decision = self.decision(marking, weekend, c_popularity, payment_delay, payed, shipping_method)
|
||||
|
||||
def tree(self):
|
||||
dataset = pandas.read_csv('./decision_tree/csv_file.csv')
|
||||
x = dataset[attributes]
|
||||
y = dataset[decisions]
|
||||
decision_tree = DecisionTreeClassifier()
|
||||
decision_tree = decision_tree.fit(x.values, y)
|
||||
|
||||
return decision_tree
|
||||
|
||||
# return decision made from tree and attributes
|
||||
def decision(self, marking, weekend, c_popularity, payment_delay, payed, shipping_method):
|
||||
decision_tree = self.tree()
|
||||
decision = decision_tree.predict(
|
||||
[[marking, weekend, c_popularity, payment_delay, payed, shipping_method]])
|
||||
if decision == 1:
|
||||
decision = Sectors_types.fragile
|
||||
elif decision == 2:
|
||||
decision = Sectors_types.normal
|
||||
elif decision == 3:
|
||||
decision = Sectors_types.shipping_tomorrow
|
||||
elif decision == 4:
|
||||
decision = Sectors_types.shipping_today
|
||||
# print(decision)
|
||||
return decision
|
||||
|
||||
def tree_as_txt(self, decision_tree):
|
||||
with open('tree.txt', "w") as file:
|
||||
file.write(export_text(decision_tree))
|
||||
|
||||
def tree_to_png(self, decision_tree):
|
||||
fig = plt.figure()
|
||||
plot_tree(decision_tree, feature_names=attributes, filled=True)
|
||||
plt.title("Decision tree")
|
||||
# plt.show()
|
||||
fig.savefig('tree.png')
|
||||
|
||||
# def tree_to_structure(self,decision_tree):
|
||||
# joblib.dump(decision_tree, 'tree_model')
|
||||
#
|
||||
# def tree_from_structure(self, file):
|
||||
# return joblib.load(file)
|
||||
#
|
||||
#
|
||||
#
|
||||
# drzewo = tree()
|
||||
# # tree_as_txt(drzewo)
|
||||
# tree_to_png(drzewo)
|
||||
# # tree_to_structure(drzewo)
|
||||
#
|
0
decision_tree/__init__.py
Normal file
0
decision_tree/__init__.py
Normal file
159
decision_tree/csv_file.csv
Normal file
159
decision_tree/csv_file.csv
Normal file
@ -0,0 +1,159 @@
|
||||
marking,weekend,c_popularity,payment_delay,payed,shipping_method,decision
|
||||
0,1,1,1,1,1,1
|
||||
0,1,5,0,1,0,1
|
||||
0,1,3,0,1,0,1
|
||||
0,1,0,3,0,1,1
|
||||
0,0,5,1,1,1,1
|
||||
0,0,1,4,1,0,1
|
||||
0,1,3,1,0,1,1
|
||||
0,1,4,5,0,0,1
|
||||
0,0,2,1,0,1,1
|
||||
0,1,5,4,0,0,1
|
||||
0,1,3,1,1,0,1
|
||||
0,1,1,0,0,1,1
|
||||
0,1,0,5,0,1,1
|
||||
0,1,2,0,0,0,1
|
||||
0,1,0,2,0,1,1
|
||||
0,0,3,3,1,1,1
|
||||
0,0,5,4,1,0,1
|
||||
0,0,3,5,0,1,1
|
||||
0,1,5,0,0,0,1
|
||||
1,1,0,0,1,0,2
|
||||
1,1,3,4,1,1,2
|
||||
1,1,4,3,1,0,2
|
||||
1,1,2,3,0,1,2
|
||||
1,1,3,2,0,1,2
|
||||
1,1,2,4,0,1,2
|
||||
1,1,0,1,1,1,2
|
||||
1,1,1,2,1,0,2
|
||||
1,1,1,1,0,0,2
|
||||
1,1,0,4,0,1,2
|
||||
1,1,0,5,1,0,2
|
||||
1,1,0,3,1,1,2
|
||||
1,1,4,2,1,1,2
|
||||
1,1,5,3,0,1,2
|
||||
1,1,5,2,0,1,2
|
||||
1,1,1,5,0,0,2
|
||||
1,1,1,5,1,0,2
|
||||
1,1,4,4,1,0,2
|
||||
1,1,5,4,0,1,2
|
||||
1,0,1,2,0,1,2
|
||||
1,0,0,0,0,0,2
|
||||
1,0,0,4,1,1,2
|
||||
1,0,0,2,0,1,2
|
||||
1,0,0,5,1,0,2
|
||||
1,0,0,2,1,0,2
|
||||
1,0,0,2,1,0,2
|
||||
1,0,2,3,0,0,2
|
||||
1,0,1,2,0,1,2
|
||||
1,0,0,2,0,1,2
|
||||
1,0,2,1,0,0,2
|
||||
1,0,0,1,1,0,2
|
||||
1,0,0,0,1,1,2
|
||||
1,0,1,4,0,1,2
|
||||
1,0,2,4,1,0,2
|
||||
1,0,0,4,0,1,2
|
||||
1,0,2,4,1,1,2
|
||||
1,0,0,5,0,0,2
|
||||
1,0,2,1,0,0,2
|
||||
1,0,2,2,1,0,2
|
||||
1,0,2,4,0,1,2
|
||||
1,0,1,2,1,0,2
|
||||
1,0,1,2,0,0,2
|
||||
1,0,2,4,0,1,2
|
||||
1,0,4,5,1,1,2
|
||||
1,0,5,5,1,1,2
|
||||
1,0,5,5,0,0,2
|
||||
1,0,4,5,0,1,2
|
||||
1,0,5,5,0,0,2
|
||||
1,0,4,5,1,0,2
|
||||
1,0,5,5,1,0,2
|
||||
1,0,3,5,1,0,2
|
||||
1,0,4,5,1,1,2
|
||||
1,0,3,5,0,0,2
|
||||
1,0,5,5,0,1,2
|
||||
1,0,5,5,1,1,2
|
||||
1,0,5,5,1,0,2
|
||||
1,0,5,5,0,1,2
|
||||
1,0,4,5,0,0,2
|
||||
1,0,5,5,0,1,2
|
||||
1,0,4,5,1,1,2
|
||||
1,0,3,5,1,1,2
|
||||
1,0,4,5,1,0,2
|
||||
1,0,5,5,1,1,2
|
||||
1,0,5,5,0,1,2
|
||||
1,0,4,5,1,0,2
|
||||
1,0,4,5,0,1,2
|
||||
1,0,4,5,0,1,2
|
||||
1,0,3,0,0,0,3
|
||||
1,0,3,3,0,1,3
|
||||
1,0,4,4,0,1,3
|
||||
1,0,3,3,0,0,3
|
||||
1,0,4,4,0,0,3
|
||||
1,0,5,4,0,1,3
|
||||
1,0,3,3,0,1,3
|
||||
1,0,5,4,0,1,3
|
||||
1,0,5,4,0,1,3
|
||||
1,0,4,4,0,0,3
|
||||
1,0,5,1,0,1,3
|
||||
1,0,3,1,0,0,3
|
||||
1,0,5,1,0,0,3
|
||||
1,0,5,4,0,1,3
|
||||
1,0,4,4,0,0,3
|
||||
1,0,3,3,0,1,3
|
||||
1,0,5,2,0,0,3
|
||||
1,0,4,1,0,0,3
|
||||
1,0,4,1,0,1,3
|
||||
1,0,5,1,0,1,3
|
||||
1,0,4,0,0,0,3
|
||||
1,0,5,1,0,1,3
|
||||
1,0,3,3,0,0,3
|
||||
1,0,4,3,0,1,3
|
||||
1,0,4,4,1,0,4
|
||||
1,0,3,0,1,0,4
|
||||
1,0,4,1,1,0,4
|
||||
1,0,3,4,1,0,4
|
||||
1,0,5,3,1,0,4
|
||||
1,0,4,3,1,0,4
|
||||
1,0,5,0,1,0,4
|
||||
1,0,3,0,1,0,4
|
||||
1,0,4,1,1,0,4
|
||||
1,0,4,0,1,0,4
|
||||
1,0,5,1,1,0,4
|
||||
1,0,4,3,1,0,4
|
||||
1,0,3,4,1,0,4
|
||||
1,0,3,4,1,0,4
|
||||
1,0,4,4,1,0,4
|
||||
1,0,3,2,1,0,4
|
||||
1,0,4,3,1,0,4
|
||||
1,0,4,2,1,0,4
|
||||
1,0,5,2,1,0,4
|
||||
1,0,4,3,1,0,4
|
||||
1,0,3,2,1,0,4
|
||||
1,0,5,1,1,0,4
|
||||
1,0,3,2,1,0,4
|
||||
1,0,5,2,1,0,4
|
||||
1,0,4,3,1,0,4
|
||||
1,0,3,4,1,0,4
|
||||
1,0,3,4,1,0,4
|
||||
1,0,3,0,1,0,4
|
||||
1,0,3,0,1,0,4
|
||||
1,0,5,4,1,1,3
|
||||
1,0,3,4,1,1,3
|
||||
1,0,4,0,1,1,3
|
||||
1,0,4,0,1,1,3
|
||||
1,0,3,0,1,1,3
|
||||
1,0,4,3,1,1,3
|
||||
1,0,4,4,1,1,3
|
||||
1,0,5,0,1,1,3
|
||||
1,0,5,4,1,1,3
|
||||
1,0,4,4,1,1,3
|
||||
1,0,4,1,1,1,3
|
||||
1,0,4,0,1,1,3
|
||||
1,0,5,4,1,1,3
|
||||
1,0,3,1,1,1,3
|
||||
1,0,5,2,1,1,3
|
||||
1,0,5,3,1,1,3
|
||||
1,0,4,4,1,1,3
|
||||
1,0,5,4,1,1,3
|
||||
1,0,3,0,1,1,3
|
|
BIN
decision_tree/tree.png
Normal file
BIN
decision_tree/tree.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 57 KiB |
22
decision_tree/tree.txt
Normal file
22
decision_tree/tree.txt
Normal file
@ -0,0 +1,22 @@
|
||||
|--- feature_0 <= 0.50
|
||||
| |--- class: 1
|
||||
|--- feature_0 > 0.50
|
||||
| |--- feature_2 <= 2.50
|
||||
| | |--- class: 2
|
||||
| |--- feature_2 > 2.50
|
||||
| | |--- feature_3 <= 4.50
|
||||
| | | |--- feature_5 <= 0.50
|
||||
| | | | |--- feature_4 <= 0.50
|
||||
| | | | | |--- class: 3
|
||||
| | | | |--- feature_4 > 0.50
|
||||
| | | | | |--- feature_1 <= 0.50
|
||||
| | | | | | |--- class: 4
|
||||
| | | | | |--- feature_1 > 0.50
|
||||
| | | | | | |--- class: 2
|
||||
| | | |--- feature_5 > 0.50
|
||||
| | | | |--- feature_1 <= 0.50
|
||||
| | | | | |--- class: 3
|
||||
| | | | |--- feature_1 > 0.50
|
||||
| | | | | |--- class: 2
|
||||
| | |--- feature_3 > 4.50
|
||||
| | | |--- class: 2
|
BIN
decision_tree/tree_model
Normal file
BIN
decision_tree/tree_model
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user