decision tree

This commit is contained in:
Anna Śmigiel 2022-05-12 21:16:05 +02:00
parent a33806e32a
commit 9e95c77404
14 changed files with 337 additions and 25 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

@ -0,0 +1,8 @@
import enum
class Sectors_types(enum.Enum):
normal = 1
shipping_tomorrow = 2
shipping_today = 3
fragile = 4

View File

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

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

View File

159
decision_tree/csv_file.csv Normal file
View 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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1 3
94 1 0 3 3 0 1 3
95 1 0 5 4 0 1 3
96 1 0 5 4 0 1 3
97 1 0 4 4 0 0 3
98 1 0 5 1 0 1 3
99 1 0 3 1 0 0 3
100 1 0 5 1 0 0 3
101 1 0 5 4 0 1 3
102 1 0 4 4 0 0 3
103 1 0 3 3 0 1 3
104 1 0 5 2 0 0 3
105 1 0 4 1 0 0 3
106 1 0 4 1 0 1 3
107 1 0 5 1 0 1 3
108 1 0 4 0 0 0 3
109 1 0 5 1 0 1 3
110 1 0 3 3 0 0 3
111 1 0 4 3 0 1 3
112 1 0 4 4 1 0 4
113 1 0 3 0 1 0 4
114 1 0 4 1 1 0 4
115 1 0 3 4 1 0 4
116 1 0 5 3 1 0 4
117 1 0 4 3 1 0 4
118 1 0 5 0 1 0 4
119 1 0 3 0 1 0 4
120 1 0 4 1 1 0 4
121 1 0 4 0 1 0 4
122 1 0 5 1 1 0 4
123 1 0 4 3 1 0 4
124 1 0 3 4 1 0 4
125 1 0 3 4 1 0 4
126 1 0 4 4 1 0 4
127 1 0 3 2 1 0 4
128 1 0 4 3 1 0 4
129 1 0 4 2 1 0 4
130 1 0 5 2 1 0 4
131 1 0 4 3 1 0 4
132 1 0 3 2 1 0 4
133 1 0 5 1 1 0 4
134 1 0 3 2 1 0 4
135 1 0 5 2 1 0 4
136 1 0 4 3 1 0 4
137 1 0 3 4 1 0 4
138 1 0 3 4 1 0 4
139 1 0 3 0 1 0 4
140 1 0 3 0 1 0 4
141 1 0 5 4 1 1 3
142 1 0 3 4 1 1 3
143 1 0 4 0 1 1 3
144 1 0 4 0 1 1 3
145 1 0 3 0 1 1 3
146 1 0 4 3 1 1 3
147 1 0 4 4 1 1 3
148 1 0 5 0 1 1 3
149 1 0 5 4 1 1 3
150 1 0 4 4 1 1 3
151 1 0 4 1 1 1 3
152 1 0 4 0 1 1 3
153 1 0 5 4 1 1 3
154 1 0 3 1 1 1 3
155 1 0 5 2 1 1 3
156 1 0 5 3 1 1 3
157 1 0 4 4 1 1 3
158 1 0 5 4 1 1 3
159 1 0 3 0 1 1 3

BIN
decision_tree/tree.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

22
decision_tree/tree.txt Normal file
View 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

Binary file not shown.