From 7a14a94b1ed2e6b14f1a193a1d843dd6135e8cbb Mon Sep 17 00:00:00 2001 From: tafit0902 Date: Sat, 11 May 2024 22:05:02 +0200 Subject: [PATCH] traktor porusza sie po polu i decyduje czy podlac na podstawie drzewa decyzyjnego --- App.py | 2 +- Drzewo.py | 2 +- Slot.py | 8 ++++++-- Tractor.py | 40 +++++++++++++++++++++++++++++++++++----- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/App.py b/App.py index e591717..1f0fd48 100644 --- a/App.py +++ b/App.py @@ -115,7 +115,7 @@ def init_demo(): #Demo purpose if(TreeFlag): traktor.move_forward(pole) - traktor.tree_move() + traktor.tree_move(pole) start_flag=False # demo_move() old_info=get_info(old_info) diff --git a/Drzewo.py b/Drzewo.py index e40bf23..706312b 100644 --- a/Drzewo.py +++ b/Drzewo.py @@ -19,7 +19,7 @@ class Drzewo: skltree.plot_tree(self.tree,filled=True,feature_names=atributes) plt.title("Drzewo decyzyjne wytrenowane na przygotowanych danych") plt.savefig('tree.png') - plt.show() + # plt.show() def makeDecision(self,values): action=self.tree.predict([values]) #0- nie podlewac, 1-podlewac if(action==[0]): diff --git a/Slot.py b/Slot.py index 3b07fb6..30ad717 100644 --- a/Slot.py +++ b/Slot.py @@ -22,8 +22,12 @@ class Slot: pygame.draw.rect(self.screen,Colors.BLACK,self.field,BORDER_THICKNESS) #Draw border pygame.display.update() - def redraw_image(self): - self.mark_visited() + def redraw_image(self, destroy = True): + if destroy: + 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): plant,self.plant_image=self.image_loader.return_plant('road') diff --git a/Tractor.py b/Tractor.py index 872d246..a4276fe 100644 --- a/Tractor.py +++ b/Tractor.py @@ -63,14 +63,16 @@ class Tractor: self.current_tractor_image = self.tractor_images[self.direction] self.draw_tractor() - def tree_move(self): + def tree_move(self, pole): drzewo.treeLearn() + print("test") 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) + 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 #condition.cycle() #condition.getCondition() @@ -86,7 +88,7 @@ class Tractor: self.current_tractor_image = self.tractor_images[self.direction] self.draw_tractor() - def move_forward(self, pole): + def move_forward(self, pole, destroy = True): next_slot_coordinates = None if self.direction == Tractor.DIRECTION_EAST: 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] # 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): next_slot = pole.get_slot_from_cord(next_slot_coordinates) - self.slot.redraw_image() + self.slot.redraw_image(destroy) self.slot = next_slot self.draw_tractor() return True @@ -153,6 +156,33 @@ class Tractor: 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): next_slot_coordinates=(x,y) if(self.do_move_if_valid(pole,next_slot_coordinates)):