Dodanie drzewa decyzjnego v1
This commit is contained in:
parent
0f090f9a85
commit
8e331a8803
66
decision_tree/decisionTree.py
Normal file
66
decision_tree/decisionTree.py
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import joblib
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import pandas
|
||||||
|
from sklearn.tree import DecisionTreeClassifier, export_text, plot_tree
|
||||||
|
|
||||||
|
'''
|
||||||
|
atrybuty w pliku csv muszą być integerami, wstępnie ustaliłem:
|
||||||
|
season = {"wiosna": 1, "lato": 2, "jesien":3, "zima":4}
|
||||||
|
enough_space_in_trashmaster = { "no": 1, "yes":2}
|
||||||
|
time_since_flush = [1,2,3,4,5,6,7,8,9,10]
|
||||||
|
type_of_trash = {"bio":1, "szklo":2, "plastik":3, "papier":4, "mieszane":5}
|
||||||
|
access_to_bin = { "no":1, "yes":2}
|
||||||
|
distance = [1,2,3,4,5,6,7,8,9,10]
|
||||||
|
decision = [1,2,3,4,5]
|
||||||
|
'''
|
||||||
|
decisions = ["decision"]
|
||||||
|
attributes = ["season", "enough_space_in_trashmaster", "time_since_flush", "type_of_trash", "access_to_bin", "distance",
|
||||||
|
"decision"]
|
||||||
|
|
||||||
|
|
||||||
|
# return tree made from attributes
|
||||||
|
def tree():
|
||||||
|
dataset = pandas.read_csv('./decision_tree/drzewo_decyzyjne.csv')
|
||||||
|
|
||||||
|
x = dataset[attributes]
|
||||||
|
y = dataset[decisions]
|
||||||
|
decision_tree = DecisionTreeClassifier()
|
||||||
|
decision_tree = decision_tree.fit(x, y)
|
||||||
|
|
||||||
|
return decision_tree
|
||||||
|
|
||||||
|
|
||||||
|
# return decision made from tree and attributes
|
||||||
|
def decision(decision_tree, season, enough_space_in_trashmaster, time_since_flush, type_of_trash, access_to_bin,
|
||||||
|
distance):
|
||||||
|
decision = decision_tree.predict(
|
||||||
|
[[season, enough_space_in_trashmaster, time_since_flush, type_of_trash, access_to_bin, distance]])
|
||||||
|
|
||||||
|
return decision
|
||||||
|
|
||||||
|
|
||||||
|
'''
|
||||||
|
we shall save output of our decision tree. It is possible for a few ways:
|
||||||
|
txt, png or structure
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
def tree_as_txt(decision_tree):
|
||||||
|
with open('./decision_tree/tree_as_txt.txt', "w") as file:
|
||||||
|
file.write(export_text(decision_tree))
|
||||||
|
|
||||||
|
|
||||||
|
def tree_to_png(decision_tree):
|
||||||
|
plt.figure()
|
||||||
|
plot_tree(decision_tree, feature_names=attributes, filled=True)
|
||||||
|
plt.title("Decision tree")
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
def tree_to_structure(decision_tree):
|
||||||
|
joblib.dump(decision_tree, './decision_tree/tree_model')
|
||||||
|
|
||||||
|
#drzewo = tree()
|
||||||
|
#tree_as_txt(drzewo)
|
||||||
|
#tree_to_png(drzewo)
|
||||||
|
#tree_to_structure(drzewo)
|
101
decision_tree/drzewo_decyzyjne.csv
Normal file
101
decision_tree/drzewo_decyzyjne.csv
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
season,enough_space_in_trashmaster,time_since_flush,type_of_trash,access_to_bin,distance,decision
|
||||||
|
2,2,1,1,2,10,1
|
||||||
|
2,2,2,2,2,8,1
|
||||||
|
2,2,3,3,2,6,1
|
||||||
|
2,2,4,4,2,4,2
|
||||||
|
2,2,5,5,2,2,3
|
||||||
|
2,2,6,1,2,1,5
|
||||||
|
2,2,7,2,2,3,4
|
||||||
|
2,2,8,3,2,5,4
|
||||||
|
2,2,9,4,2,7,3
|
||||||
|
2,2,10,5,2,9,5
|
||||||
|
2,2,1,1,2,2,1
|
||||||
|
2,2,2,2,2,1,1
|
||||||
|
2,2,3,3,2,3,2
|
||||||
|
2,2,4,4,2,4,2
|
||||||
|
2,2,5,5,2,5,3
|
||||||
|
2,2,6,1,2,6,3
|
||||||
|
2,2,7,2,2,7,2
|
||||||
|
2,2,8,3,2,8,2
|
||||||
|
2,2,9,4,2,9,4
|
||||||
|
2,2,10,5,2,10,4
|
||||||
|
2,2,1,1,2,7,1
|
||||||
|
2,2,2,2,2,6,2
|
||||||
|
2,2,3,3,2,5,3
|
||||||
|
2,2,4,4,1,4,0
|
||||||
|
2,2,5,5,1,3,0
|
||||||
|
2,2,6,1,1,2,0
|
||||||
|
2,2,7,2,1,1,0
|
||||||
|
2,2,8,3,1,9,0
|
||||||
|
2,2,9,4,1,8,0
|
||||||
|
2,2,10,5,1,7,0
|
||||||
|
2,2,1,1,1,3,0
|
||||||
|
2,2,2,2,1,2,0
|
||||||
|
2,2,3,3,1,1,0
|
||||||
|
2,2,4,4,1,4,0
|
||||||
|
2,1,5,5,1,5,0
|
||||||
|
2,1,6,1,1,6,0
|
||||||
|
2,1,7,2,1,10,0
|
||||||
|
2,1,8,3,1,9,0
|
||||||
|
2,1,9,4,1,8,0
|
||||||
|
2,1,10,5,1,7,0
|
||||||
|
2,1,1,1,1,2,0
|
||||||
|
2,1,2,2,1,4,0
|
||||||
|
2,1,3,3,1,6,0
|
||||||
|
2,1,4,4,1,8,0
|
||||||
|
2,1,5,5,2,10,0
|
||||||
|
2,1,6,1,2,1,0
|
||||||
|
2,1,7,2,2,3,0
|
||||||
|
2,1,8,3,2,5,0
|
||||||
|
2,1,9,4,2,7,0
|
||||||
|
2,1,10,5,2,9,0
|
||||||
|
3,2,1,1,2,2,1
|
||||||
|
3,2,2,2,2,1,1
|
||||||
|
3,2,3,3,2,4,2
|
||||||
|
3,2,4,4,2,3,3
|
||||||
|
3,2,5,5,2,6,4
|
||||||
|
3,2,6,1,2,5,4
|
||||||
|
3,2,7,2,2,8,3
|
||||||
|
3,2,8,3,2,7,3
|
||||||
|
3,2,9,4,2,9,4
|
||||||
|
3,2,10,5,2,10,5
|
||||||
|
3,2,1,1,2,7,1
|
||||||
|
3,2,2,2,2,6,1
|
||||||
|
3,2,3,3,2,4,3
|
||||||
|
3,2,4,4,2,1,3
|
||||||
|
3,2,5,5,2,2,4
|
||||||
|
3,2,6,1,2,3,4
|
||||||
|
3,2,7,2,2,9,3
|
||||||
|
3,2,8,3,2,8,3
|
||||||
|
3,2,9,4,2,5,5
|
||||||
|
3,2,10,5,2,4,5
|
||||||
|
3,2,1,1,2,1,1
|
||||||
|
3,2,2,2,1,7,0
|
||||||
|
3,2,3,3,1,9,0
|
||||||
|
3,2,4,4,1,10,0
|
||||||
|
3,2,5,5,1,3,0
|
||||||
|
3,2,6,1,1,2,0
|
||||||
|
3,2,7,2,1,5,0
|
||||||
|
3,2,8,3,1,6,0
|
||||||
|
3,2,9,4,1,8,0
|
||||||
|
3,1,10,5,1,3,0
|
||||||
|
3,1,1,1,1,1,0
|
||||||
|
3,1,2,2,1,2,0
|
||||||
|
3,1,3,3,1,6,0
|
||||||
|
3,1,4,4,1,9,0
|
||||||
|
3,1,5,5,1,7,0
|
||||||
|
3,1,6,1,1,4,0
|
||||||
|
3,1,7,2,1,3,0
|
||||||
|
3,1,8,3,1,5,0
|
||||||
|
3,1,9,4,1,10,0
|
||||||
|
3,1,10,5,1,8,0
|
||||||
|
3,1,1,1,2,2,0
|
||||||
|
3,1,2,2,2,4,0
|
||||||
|
3,1,3,3,2,6,0
|
||||||
|
3,1,4,4,2,7,0
|
||||||
|
3,1,5,5,2,1,0
|
||||||
|
3,1,6,1,2,9,0
|
||||||
|
3,1,7,2,2,3,0
|
||||||
|
3,1,8,3,2,9,0
|
||||||
|
3,1,9,4,2,9,0
|
||||||
|
3,1,10,5,2,1,0
|
|
16
decision_tree/tree_as_txt.txt
Normal file
16
decision_tree/tree_as_txt.txt
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
|--- feature_6 <= 0.50
|
||||||
|
| |--- class: 0
|
||||||
|
|--- feature_6 > 0.50
|
||||||
|
| |--- feature_6 <= 1.50
|
||||||
|
| | |--- class: 1
|
||||||
|
| |--- feature_6 > 1.50
|
||||||
|
| | |--- feature_6 <= 3.50
|
||||||
|
| | | |--- feature_6 <= 2.50
|
||||||
|
| | | | |--- class: 2
|
||||||
|
| | | |--- feature_6 > 2.50
|
||||||
|
| | | | |--- class: 3
|
||||||
|
| | |--- feature_6 > 3.50
|
||||||
|
| | | |--- feature_6 <= 4.50
|
||||||
|
| | | | |--- class: 4
|
||||||
|
| | | |--- feature_6 > 4.50
|
||||||
|
| | | | |--- class: 5
|
BIN
decision_tree/tree_model
Normal file
BIN
decision_tree/tree_model
Normal file
Binary file not shown.
@ -19,6 +19,7 @@ class Player(pg.sprite.Sprite):
|
|||||||
self.pos = vec(x, y)
|
self.pos = vec(x, y)
|
||||||
self.rot = 0
|
self.rot = 0
|
||||||
self.__rotation = a_star_utils.Rotation.RIGHT
|
self.__rotation = a_star_utils.Rotation.RIGHT
|
||||||
|
self.mass = 0
|
||||||
|
|
||||||
def rotation(self) -> a_star_utils.Rotation:
|
def rotation(self) -> a_star_utils.Rotation:
|
||||||
return self.__rotation
|
return self.__rotation
|
||||||
|
37
main.py
37
main.py
@ -10,9 +10,18 @@ from map import map
|
|||||||
from map import map_utils
|
from map import map_utils
|
||||||
from path_search_algorthms import bfs
|
from path_search_algorthms import bfs
|
||||||
from path_search_algorthms import a_star, a_star_utils
|
from path_search_algorthms import a_star, a_star_utils
|
||||||
|
from decision_tree import decisionTree
|
||||||
|
|
||||||
from game_objects import aiPlayer
|
from game_objects import aiPlayer
|
||||||
|
|
||||||
|
|
||||||
|
def printTree():
|
||||||
|
tree = decisionTree.tree()
|
||||||
|
decisionTree.tree_as_txt(tree)
|
||||||
|
decisionTree.tree_to_png(tree)
|
||||||
|
decisionTree.tree_to_structure(tree)
|
||||||
|
|
||||||
|
|
||||||
class Game():
|
class Game():
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -52,7 +61,7 @@ class Game():
|
|||||||
# print(path)
|
# print(path)
|
||||||
realPath = []
|
realPath = []
|
||||||
nextNode = target_node
|
nextNode = target_node
|
||||||
for i in range(len(path)-1, 0, -1):
|
for i in range(len(path) - 1, 0, -1):
|
||||||
node = path[i]
|
node = path[i]
|
||||||
if node[0] == nextNode:
|
if node[0] == nextNode:
|
||||||
realPath.insert(0, node[0])
|
realPath.insert(0, node[0])
|
||||||
@ -72,15 +81,15 @@ class Game():
|
|||||||
game_folder = path.dirname(__file__)
|
game_folder = path.dirname(__file__)
|
||||||
img_folder = path.join(game_folder, 'resources/textures')
|
img_folder = path.join(game_folder, 'resources/textures')
|
||||||
|
|
||||||
self.player_img = pg.image.load(path.join(img_folder,PLAYER_IMG)).convert_alpha()
|
self.player_img = pg.image.load(path.join(img_folder, PLAYER_IMG)).convert_alpha()
|
||||||
self.player_img = pg.transform.scale(self.player_img, (PLAYER_WIDTH,PLAYER_HEIGHT) )
|
self.player_img = pg.transform.scale(self.player_img, (PLAYER_WIDTH, PLAYER_HEIGHT))
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
# game loop - set self.playing = False to end the game
|
# game loop - set self.playing = False to end the game
|
||||||
self.playing = True
|
self.playing = True
|
||||||
|
|
||||||
while self.playing:
|
while self.playing:
|
||||||
self.dt = self.clock.tick(FPS) / 1000.0
|
self.dt = self.clock.tick(FPS) / 1000.0
|
||||||
self.events()
|
self.events()
|
||||||
self.update()
|
self.update()
|
||||||
self.draw()
|
self.draw()
|
||||||
@ -96,20 +105,20 @@ class Game():
|
|||||||
# pygame.display.update()
|
# pygame.display.update()
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
#display fps as window title
|
# display fps as window title
|
||||||
pg.display.set_caption("{:.2f}".format(self.clock.get_fps()))
|
pg.display.set_caption("{:.2f}".format(self.clock.get_fps()))
|
||||||
|
|
||||||
#rerender map
|
# rerender map
|
||||||
map.render_tiles(self.roadTiles, self.screen, self.camera)
|
map.render_tiles(self.roadTiles, self.screen, self.camera)
|
||||||
map.render_tiles(self.wallTiles, self.screen, self.camera, self.debug_mode)
|
map.render_tiles(self.wallTiles, self.screen, self.camera, self.debug_mode)
|
||||||
|
|
||||||
#rerender additional sprites
|
# rerender additional sprites
|
||||||
for sprite in self.agentSprites:
|
for sprite in self.agentSprites:
|
||||||
self.screen.blit(sprite.image, self.camera.apply(sprite))
|
self.screen.blit(sprite.image, self.camera.apply(sprite))
|
||||||
if self.debug_mode:
|
if self.debug_mode:
|
||||||
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(sprite.hit_rect), 1)
|
pg.draw.rect(self.screen, CYAN, self.camera.apply_rect(sprite.hit_rect), 1)
|
||||||
|
|
||||||
#finally update screen
|
# finally update screen
|
||||||
pg.display.flip()
|
pg.display.flip()
|
||||||
|
|
||||||
def events(self):
|
def events(self):
|
||||||
@ -125,7 +134,9 @@ class Game():
|
|||||||
pos = pg.mouse.get_pos()
|
pos = pg.mouse.get_pos()
|
||||||
offset_x, offset_y = self.camera.offset()
|
offset_x, offset_y = self.camera.offset()
|
||||||
clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y]
|
clicked_coords = [math.floor(pos[0] / TILESIZE) - offset_x, math.floor(pos[1] / TILESIZE) - offset_y]
|
||||||
actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE), math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(), clicked_coords[0], clicked_coords[1], self.mapArray)
|
actions = a_star.search_path(math.floor(self.player.pos[0] / TILESIZE),
|
||||||
|
math.floor(self.player.pos[1] / TILESIZE), self.player.rotation(),
|
||||||
|
clicked_coords[0], clicked_coords[1], self.mapArray)
|
||||||
print(actions)
|
print(actions)
|
||||||
if (actions != None):
|
if (actions != None):
|
||||||
t = aiPlayer.aiPlayer(self.player, game=self)
|
t = aiPlayer.aiPlayer(self.player, game=self)
|
||||||
@ -137,11 +148,13 @@ class Game():
|
|||||||
def show_go_screen(self):
|
def show_go_screen(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
# create the game object
|
# create the game object
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
g = Game()
|
g = Game()
|
||||||
g.show_start_screen()
|
g.show_start_screen()
|
||||||
|
printTree()
|
||||||
|
|
||||||
g.run()
|
g.run()
|
||||||
g.show_go_screen()
|
g.show_go_screen()
|
@ -15,3 +15,7 @@ toml==0.10.2
|
|||||||
tomli==2.0.1
|
tomli==2.0.1
|
||||||
typing_extensions==4.1.1
|
typing_extensions==4.1.1
|
||||||
wrapt==1.14.0
|
wrapt==1.14.0
|
||||||
|
joblib~=1.1.0
|
||||||
|
matplotlib~=3.5.1
|
||||||
|
pandas~=1.1.1
|
||||||
|
scikit-learn~=1.0.2
|
Loading…
Reference in New Issue
Block a user