This: -renamed tree.py to Drzewo.py -added dataTree.csv -added basic function connected to decision tree -added required packages to readme.txt

This commit is contained in:
jakzar 2024-05-10 19:41:13 +02:00
parent e6c86e7856
commit f8a3156678
5 changed files with 40 additions and 9 deletions

13
App.py
View File

@ -11,6 +11,7 @@ import BFS
import AStar
import random
import Condition
import Drzewo
bfs1_flag=False
bfs2_flag=False #Change this lines to show different bfs implementation
@ -19,7 +20,7 @@ Astar = False
Astar2 = False
if bfs3_flag or Astar or Astar2:
Pole.stoneFlag = True
TreeFlag=True
pygame.init()
show_console=True
@ -36,7 +37,7 @@ ui=Ui.Ui(screen)
traktor_slot = pole.get_slot_from_cord((0, 0))
traktor = Tractor.Tractor(traktor_slot, screen, Osprzet.opryskiwacz,clock,bfs2_flag)
condition=Condition.Condition()
drzewo=Drzewo.Drzewo()
def init_demo(): #Demo purpose
old_info=""
@ -115,10 +116,14 @@ def init_demo(): #Demo purpose
else:
print_to_console("Nie można znaleźć ścieżki A*") # Wyświetl komunikat, jeśli nie znaleziono ścieżki
if(TreeFlag):
drzewo.treeLearn()
drzewo.plotTree()
print("Decyzja to: ",drzewo.makeDecision([[10,60,0,1,0,20,1,20]]))
#do moves and condtion cycles
start_flag=False
# demo_move()
condition.cycle()
condition.cycle() #powinno zostac wrzucone razem z getCondition do ruchu traktora. Aktualnie tutaj by zobaczyc czy dziala
condition.getCondition()
old_info=get_info(old_info)
for event in pygame.event.get():

3
Data/dataTree.csv Normal file
View File

@ -0,0 +1,3 @@
plant_water_lever,tractor_water_lever,weather,season,current_time,growth,disease,fertility,action
80,60,0,1,0,20,1,20,0
20,60,0,1,0,20,1,20,1
1 plant_water_lever tractor_water_lever weather season current_time growth disease fertility action
2 80 60 0 1 0 20 1 20 0
3 20 60 0 1 0 20 1 20 1

25
Drzewo.py Normal file
View File

@ -0,0 +1,25 @@
from sklearn import tree as skltree
import pandas,os
import matplotlib.pyplot as plt
atributes=['plant_water_lever','tractor_water_lever','weather','season','current_time','growth','disease','fertility'] #Columns in CSV file should be in the same order
class Drzewo:
def __init__(self):
self.tree=self.treeLearn()
def treeLearn(self):
csvdata=pandas.read_csv('Data/dataTree.csv')
x=csvdata[atributes]
decision=csvdata['action']
self.tree=skltree.DecisionTreeClassifier()
self.tree=self.tree.fit(x,decision)
def plotTree(self):
plt.figure()
skltree.plot_tree(self.tree,filled=True,feature_names=atributes)
plt.title("Drzewo decyzyjne wytrenowane na przygotowanych danych")
plt.show()
def makeDecision(self,values):
action=self.tree.predict(values) #0- nie podlewac, 1-podlewac
return action

View File

@ -1,3 +0,0 @@
class Tree:
def __init__(self) -> None:
pass

View File

@ -1,6 +1,7 @@
Required packages:
pygame,matplotlib,sklearn
pygame,matplotlib,sklearn,pandas
How to install:
pip install pygame
pip install matplotlib
pip install scikit-learn
pip install pandas