This: -added tree saving to .png file -added usage of decision tree -added way to get current status of all conditions -small improvements -switched off road and mud generation -changed order in .csv file to more logical

This commit is contained in:
jakzar 2024-05-11 14:18:40 +02:00
parent 48b159ae02
commit 12d2ba0d81
9 changed files with 43 additions and 20 deletions

15
App.py
View File

@ -9,9 +9,7 @@ import Osprzet
import Ui
import BFS
import AStar
import random
import Condition
import Drzewo
bfs1_flag=False
bfs2_flag=False #Change this lines to show different bfs implementation
@ -36,8 +34,7 @@ ui=Ui.Ui(screen)
#Tractor creation
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=""
@ -117,14 +114,10 @@ def init_demo(): #Demo purpose
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,1,0,20,1,20]]))
#do moves and condtion cycles
traktor.move_forward(pole)
traktor.tree_move()
start_flag=False
# demo_move()
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():
if event.type == pygame.QUIT:

View File

@ -38,6 +38,10 @@ class Condition:
self.temperature=self.setRandomTemperature()
self.clock=self.clock+1
def return_condition(self):
return [self.temperature,self.rain,self.season,self.currentTime]
def getCondition(self):
print(f"Aktualny czas: {Climate.time[self.currentTime]},opady: {Climate.rain[self.rain]},temperatura: {Climate.temperature[self.temperature]}, pora roku: {Climate.seasons[self.season]}")

View File

@ -1,3 +1,3 @@
plant_water_level,tractor_water_level,temperature,rain,season,current_time,growth,disease,fertility,action
80,60,3,2,1,0,20,1,20,0
20,60,3,0,1,0,20,1,20,1
plant_water_level,growth,disease,fertility,tractor_water_level,temperature,rain,season,current_time,action
80,20,0,90,40,2,0,1,0,0
50,20,0,90,80,2,0,1,0,1
1 plant_water_level growth disease fertility tractor_water_level temperature rain season current_time action
2 80 20 1 0 20 90 60 40 3 2 2 0 1 0 0
3 20 50 20 1 0 20 90 60 80 3 2 0 1 0 1

View File

@ -2,7 +2,7 @@ from sklearn import tree as skltree
import pandas,os
import matplotlib.pyplot as plt
atributes=['plant_water_level','tractor_water_level','temperature','rain','season','current_time','growth','disease','fertility'] #Columns in CSV file should be in the same order
atributes=['plant_water_level','growth','disease','fertility','tractor_water_level','temperature','rain','season','current_time'] #Columns in CSV file has to be in the same order
class Drzewo:
def __init__(self):
self.tree=self.treeLearn()
@ -12,14 +12,14 @@ class Drzewo:
x=csvdata[atributes]
decision=csvdata['action']
self.tree=skltree.DecisionTreeClassifier()
self.tree=self.tree.fit(x,decision)
self.tree=self.tree.fit(x.values,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.savefig('tree.png')
plt.show()
def makeDecision(self,values):
action=self.tree.predict(values) #0- nie podlewac, 1-podlewac
action=self.tree.predict([values]) #0- nie podlewac, 1-podlewac
return action

View File

@ -37,7 +37,7 @@ class Image:
self.gasStation_image=pygame.transform.scale(gasStation,(dCon.CUBE_SIZE,dCon.CUBE_SIZE))
def return_random_plant(self):
x=random.randint(0,7)
x=random.randint(0,5) #disabled dirt and mud generation
keys=list(self.plants_image_dict.keys())
plant=keys[x]
return (plant,self.plants_image_dict[plant])

View File

@ -110,6 +110,9 @@ class Roslina:
def return_stan(self):
return self.stan
def return_stan_for_tree(self):
return self.stan.return_stan_for_tree()
def get_hydrate_stats(self):
return self.stan.return_hydrate()

View File

@ -82,4 +82,6 @@ class Slot:
self.plant.stan.nawodnienie=random.randint(61,100)
elif(index==-1):
pass
def return_stan_for_tree(self):
return self.plant.return_stan_for_tree()

View File

@ -57,5 +57,9 @@ class Stan:
return "Zdrowa"
if(self.choroba==1):
return "Chora"
def return_stan_for_tree(self):
return [self.nawodnienie,self.wzrost,self.choroba,self.zyznosc]
def report_all(self):
return f"Nawodnienie: {self.nawodnienie} Zyznosc: {self.zyznosc} Wzrost: {self.wzrost} Choroba: {self.return_disease_as_string()}"

View File

@ -7,6 +7,12 @@ import displayControler as dCon
import Slot
import Osprzet
import Node
import Condition
import Drzewo
condition=Condition.Condition()
drzewo=Drzewo.Drzewo()
tab = [-1, 0, 0, 0, 0, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 0, 1, 0, 1, 1,
@ -57,6 +63,17 @@ class Tractor:
self.current_tractor_image = self.tractor_images[self.direction]
self.draw_tractor()
def tree_move(self):
drzewo.treeLearn()
drzewo.plotTree()
slot_attributes=self.slot.return_stan_for_tree()
climate_attributes=condition.return_condition()
attributes=[]
attributes=attributes+slot_attributes+[self.waterLevel]+climate_attributes
print("Decyzja czy podlac:",drzewo.makeDecision(attributes),"Atrybuty tego stanu to:",attributes)
#TODO SNAKE MOVE AND USING drzewo.makeDecision(attributes)for each slot. Also we need to cycle climate for each slot change by
#condition.cycle()
#condition.getCondition()
def turn_right(self):
# zmiana kierunku w prawo
direction_map = {