traktor porusza sie po polu i decyduje czy podlac na podstawie drzewa decyzyjnego

This commit is contained in:
tafit0902 2024-05-11 22:05:02 +02:00
parent 7c7a485d6b
commit 7a14a94b1e
4 changed files with 43 additions and 9 deletions

2
App.py
View File

@ -115,7 +115,7 @@ def init_demo(): #Demo purpose
if(TreeFlag): if(TreeFlag):
traktor.move_forward(pole) traktor.move_forward(pole)
traktor.tree_move() traktor.tree_move(pole)
start_flag=False start_flag=False
# demo_move() # demo_move()
old_info=get_info(old_info) old_info=get_info(old_info)

View File

@ -19,7 +19,7 @@ class Drzewo:
skltree.plot_tree(self.tree,filled=True,feature_names=atributes) skltree.plot_tree(self.tree,filled=True,feature_names=atributes)
plt.title("Drzewo decyzyjne wytrenowane na przygotowanych danych") plt.title("Drzewo decyzyjne wytrenowane na przygotowanych danych")
plt.savefig('tree.png') plt.savefig('tree.png')
plt.show() # plt.show()
def makeDecision(self,values): def makeDecision(self,values):
action=self.tree.predict([values]) #0- nie podlewac, 1-podlewac action=self.tree.predict([values]) #0- nie podlewac, 1-podlewac
if(action==[0]): if(action==[0]):

View File

@ -22,8 +22,12 @@ class Slot:
pygame.draw.rect(self.screen,Colors.BLACK,self.field,BORDER_THICKNESS) #Draw border pygame.draw.rect(self.screen,Colors.BLACK,self.field,BORDER_THICKNESS) #Draw border
pygame.display.update() pygame.display.update()
def redraw_image(self): def redraw_image(self, destroy = True):
if destroy:
self.mark_visited() self.mark_visited()
else:
self.screen.blit(self.plant_image, (self.x_axis * dCon.CUBE_SIZE, self.y_axis * dCon.CUBE_SIZE))
pygame.draw.rect(self.screen, Colors.BLACK, self.field, BORDER_THICKNESS)
def mark_visited(self): def mark_visited(self):
plant,self.plant_image=self.image_loader.return_plant('road') plant,self.plant_image=self.image_loader.return_plant('road')

View File

@ -63,14 +63,16 @@ class Tractor:
self.current_tractor_image = self.tractor_images[self.direction] self.current_tractor_image = self.tractor_images[self.direction]
self.draw_tractor() self.draw_tractor()
def tree_move(self): def tree_move(self, pole):
drzewo.treeLearn() drzewo.treeLearn()
print("test")
drzewo.plotTree() drzewo.plotTree()
slot_attributes=self.slot.return_stan_for_tree() slot_attributes=self.slot.return_stan_for_tree()
climate_attributes=condition.return_condition() climate_attributes=condition.return_condition()
attributes=[] attributes=[]
attributes=attributes+slot_attributes+[self.waterLevel]+climate_attributes attributes=attributes+slot_attributes+[self.waterLevel]+climate_attributes
print("Decyzja czy podlac:",drzewo.makeDecision(attributes),"Atrybuty tego stanu to:",attributes) print("Decyzja czy podlac:",drzewo.makeDecision(attributes),"Atrybuty tego stanu to:",attributes)
self.snake_move_irrigation(pole, drzewo)
#TODO SNAKE MOVE AND USING drzewo.makeDecision(attributes)for each slot. Also we need to cycle climate for each slot change #TODO SNAKE MOVE AND USING drzewo.makeDecision(attributes)for each slot. Also we need to cycle climate for each slot change
#condition.cycle() #condition.cycle()
#condition.getCondition() #condition.getCondition()
@ -86,7 +88,7 @@ class Tractor:
self.current_tractor_image = self.tractor_images[self.direction] self.current_tractor_image = self.tractor_images[self.direction]
self.draw_tractor() self.draw_tractor()
def move_forward(self, pole): def move_forward(self, pole, destroy = True):
next_slot_coordinates = None next_slot_coordinates = None
if self.direction == Tractor.DIRECTION_EAST: if self.direction == Tractor.DIRECTION_EAST:
next_slot_coordinates = (self.slot.x_axis + 1, self.slot.y_axis) next_slot_coordinates = (self.slot.x_axis + 1, self.slot.y_axis)
@ -102,12 +104,13 @@ class Tractor:
self.current_tractor_image = self.tractor_images[self.direction] self.current_tractor_image = self.tractor_images[self.direction]
# sprawdzenie czy następny slot jest dobry # sprawdzenie czy następny slot jest dobry
self.do_move_if_valid(pole,next_slot_coordinates) self.do_move_if_valid(pole,next_slot_coordinates, destroy)
self.clock.tick(10)
def do_move_if_valid(self,pole, next_slot_coordinates): def do_move_if_valid(self,pole, next_slot_coordinates, destroy = True):
if next_slot_coordinates and pole.is_valid_move(next_slot_coordinates): if next_slot_coordinates and pole.is_valid_move(next_slot_coordinates):
next_slot = pole.get_slot_from_cord(next_slot_coordinates) next_slot = pole.get_slot_from_cord(next_slot_coordinates)
self.slot.redraw_image() self.slot.redraw_image(destroy)
self.slot = next_slot self.slot = next_slot
self.draw_tractor() self.draw_tractor()
return True return True
@ -153,6 +156,33 @@ class Tractor:
self.snake_move(pole,x,y) self.snake_move(pole,x,y)
def snake_move_irrigation(self, pole, drzewo):
initPos = (self.slot.x_axis, self.slot.y_axis)
counter = 0
for i in range(initPos[1], dCon.NUM_Y):
for j in range(initPos[0], dCon.NUM_X):
slot_attributes=self.slot.return_stan_for_tree()
climate_attributes=condition.return_condition()
attributes=[]
attributes=attributes+slot_attributes+[self.waterLevel]+climate_attributes
decision = drzewo.makeDecision(attributes)
print("Slot:", str("({:02d}, {:02d})").format(self.slot.x_axis, self.slot.y_axis),"Decyzja czy podlac:",decision,"Atrybuty slotu:",attributes)
if decision == "Podlewac":
self.slot.irrigatePlant()
counter += 1
condition.cycle()
#condition.getCondition()
self.move_forward(pole, False)
if i % 2 == 0 and i != dCon.NUM_Y - 1:
self.turn_right()
self.move_forward(pole, False)
self.turn_right()
elif i != dCon.NUM_Y - 1:
self.turn_left()
self.move_forward(pole, False)
self.turn_left()
print("podlanych slotów: ", str(counter))
def snake_move(self,pole,x,y): def snake_move(self,pole,x,y):
next_slot_coordinates=(x,y) next_slot_coordinates=(x,y)
if(self.do_move_if_valid(pole,next_slot_coordinates)): if(self.do_move_if_valid(pole,next_slot_coordinates)):