From 95dcef88cf8e76133672856c85643ee2ceb8168d Mon Sep 17 00:00:00 2001 From: Pikusia Date: Sun, 2 Jun 2019 21:06:39 +0200 Subject: [PATCH] poprawione drzewo, wydluzone ttl, funkcja service --- Cucumber.py | 2 +- Tomato.py | 2 +- Tree.py | 110 +++++++++++++++++++++++++++------------------------- tractor.py | 50 +++++++++++++----------- 4 files changed, 86 insertions(+), 78 deletions(-) diff --git a/Cucumber.py b/Cucumber.py index c701454..c888810 100644 --- a/Cucumber.py +++ b/Cucumber.py @@ -2,7 +2,7 @@ from Plant import Plant class Cucumber(Plant): is_alive = True - ttl = 2000 + ttl = 10000 hydration = 40 soil_level = 40 dehydration_ratio = 0.8 diff --git a/Tomato.py b/Tomato.py index fed9d4c..6306f8b 100644 --- a/Tomato.py +++ b/Tomato.py @@ -2,7 +2,7 @@ from Plant import Plant class Tomato(Plant): is_alive = True - ttl = 2000 + ttl = 12000 hydration = 41 soil_level = 41 dehydration_ratio = 0.7 diff --git a/Tree.py b/Tree.py index 8aa9b69..8ff5923 100644 --- a/Tree.py +++ b/Tree.py @@ -2,6 +2,7 @@ from sklearn import tree import collections import subprocess +class TreeClass: #file = open("probne.pl", 'r').read() #text = "" #for line in file: @@ -22,70 +23,73 @@ import subprocess #features.update({"key5": "text"}) #symbol, is_alive, ttl, hydration, soil_level, ready -features = [["Tomato", True, 60, 90, 95], - ["Tomato", True, 60, 84, 88], - ["Tomato", True, 50, 78, 77], - ["Tomato", True, 45, 63, 68], - ["Tomato", True, 37, 54, 59], - ["Tomato", True, 20, 31, 62], - ["Tomato", True, 18, 75, 39], - ["Tomato", True, 19, 24, 74], - ["Tomato", True, 24, 69, 25], - ["Tomato", True, 15, 45, 85], - ["Tomato", True, 23, 85, 48], - ["Tomato", True, 26, 41, 45], - ["Tomato", True, 21, 35, 32], - ["Tomato", True, 49, 24, 28], - ["Tomato", True, 64, 15, 14], - ["Tomato", True, 84, 4, 8], - - ["Cucumber", True, 55, 89, 84], - ["Cucumber", True, 76, 91, 95], - ["Cucumber", True, 45, 72, 71], - ["Cucumber", True, 37, 64, 68], - ["Cucumber", True, 26, 54, 59], - ["Cucumber", True, 58, 42, 46], - ["Cucumber", True, 20, 31, 62], - ["Cucumber", True, 18, 75, 39], - ["Cucumber", True, 19, 24, 74], - ["Cucumber", True, 24, 69, 25], - ["Cucumber", True, 15, 12, 85], - ["Cucumber", True, 23, 85, 18], - ["Cucumber", True, 34, 38, 36], - ["Cucumber", True, 41, 24, 22], - ["Cucumber", True, 34, 38, 36], - ["Cucumber", True, 74, 26, 23], - ["Cucumber", True, 84, 14, 19], - ["Cucumber", True, 95, 7, 4] - ] - -labels = [0,0,0,0,0,1,2,1,2,1,2,3,3,3,3,3,0,0,0,0,0,0,1,2,1,2,1,2,3,3,3,3,3,3] #print(com) + def make_tree(self, plant, pl_stats): + features = [["Tomato", True, 60, 90, 95], + ["Tomato", True, 60, 84, 88], + ["Tomato", True, 50, 78, 77], + ["Tomato", True, 45, 63, 68], + ["Tomato", True, 37, 54, 59], + ["Tomato", True, 20, 31, 62], + ["Tomato", True, 18, 75, 39], + ["Tomato", True, 19, 24, 74], + ["Tomato", True, 24, 69, 25], + ["Tomato", True, 15, 45, 85], + ["Tomato", True, 23, 85, 48], + ["Tomato", True, 26, 41, 45], + ["Tomato", True, 21, 35, 32], + ["Tomato", True, 49, 24, 28], + ["Tomato", True, 64, 15, 14], + ["Tomato", True, 84, 4, 8], + + ["Cucumber", True, 55, 89, 84], + ["Cucumber", True, 76, 91, 95], + ["Cucumber", True, 45, 72, 71], + ["Cucumber", True, 37, 64, 68], + ["Cucumber", True, 26, 54, 59], + ["Cucumber", True, 58, 42, 46], + ["Cucumber", True, 20, 31, 62], + ["Cucumber", True, 18, 75, 39], + ["Cucumber", True, 19, 24, 74], + ["Cucumber", True, 24, 69, 25], + ["Cucumber", True, 15, 12, 85], + ["Cucumber", True, 23, 85, 18], + ["Cucumber", True, 34, 38, 36], + ["Cucumber", True, 41, 24, 22], + ["Cucumber", True, 34, 38, 36], + ["Cucumber", True, 74, 26, 23], + ["Cucumber", True, 84, 14, 19], + ["Cucumber", True, 95, 7, 4] + ] + + labels = [0, 0, 0, 0, 0, 1, 2, 1, 2, 1, 2, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0, 1, 2, 1, 2, 1, 2, 3, 3, 3, 3, 3, 3] + #utwórz drzewo decyzyjne -tree = tree.DecisionTreeClassifier() + t = tree.DecisionTreeClassifier() #znajdź wzór na podstawie danych -tree = tree.fit(features, labels)#jakieś cechy w środku i decyzje jakie ma podjąć + t = tree.fit(features, labels)#jakieś cechy w środku i decyzje jakie ma podjąć #użyj modelu aby dopasować(podjąć decyzję) -p = tree.predict([["Cucumber", True, 38, 26, 51]]) + #p = tree.predict([["Cucumber", True, 38, 26, 51]]) + p = t.predict([pl_stats[0], pl_stats[1]], pl_stats[2], pl_stats[3], pl_stats[4]) -if p == 0: - print("Ready to cut") - Harvest() -elif p == 1: - plant.increase_hydration(plant.max_hydration_lvl - plant.hydration) -elif p == 2: - plant.increase_soillevel(plant.max_soil_lvl - plant.soil_level) -elif p == 3: - plant.increase_hydration(plant.max_hydration_lvl - plant.hydration) - plant.increase_soillevel(plant.max_soil_lvl - plant.soil_level) + if p == 0: + print("Ready to cut") + t.Harvest() + elif p == 1: + plant.increase_hydration(plant.max_hydration_lvl - plant.hydration) + elif p == 2: + plant.increase_soillevel(plant.max_soil_lvl - plant.soil_level) + elif p == 3: + plant.increase_hydration(plant.max_hydration_lvl - plant.hydration) + plant.increase_soillevel(plant.max_soil_lvl - plant.soil_level) #wyświetlamy dopasowaną wartość -print(p) - + print(p) + pass """ is_life = bool type = "POop" diff --git a/tractor.py b/tractor.py index b663613..6def34c 100755 --- a/tractor.py +++ b/tractor.py @@ -4,8 +4,11 @@ import time from Graph import Graph from Cucumber import Cucumber from Tomato import Tomato +from sklearn import tree +from Tree import TreeClass -class Tractor: + +class Tractor(TreeClass): sleep_time = 0.1 graph = Graph() moved_flag = False @@ -21,6 +24,11 @@ class Tractor: (x2, y2) = b return abs(x1 - x2) + abs(y1 - y2) + # def look_at_plats(field, location): + + + + async def rotate(self, direction): reader, writer = await asyncio.open_connection('127.0.0.1', 8888) writer.write(("rotate " + direction + "\n").encode()) @@ -28,12 +36,20 @@ class Tractor: time.sleep(self.sleep_time) writer.close() - async def look_at_plants(self, direction): + async def look_at_plants(self, location): reader, writer = await asyncio.open_connection('127.0.0.1', 8888) - writer.write(("rotate " + direction + "\n").encode()) + # writer.write(("rotate " + direction + "\n").encode()) await reader.readline() time.sleep(self.sleep_time) + wsp = [ + (location[0] + 1, location[1]), + (location[0] - 1, location[1]), + (location[0], location[1] - 1), + (location[0], location[1] + 1) + ] writer.close() + return wsp + async def try_move(self): @@ -189,27 +205,7 @@ class Tractor: await self.move_tractor(start_position) - def hydration(self, plant): - if type(plant) == Tomato: - if plant.hydration < 50: - plant.increase_hydration(plant.max_hydration_lvl - plant.hydration) - elif type(plant) == Cucumber: - if plant.hydration < 40: - plant.increase_hydration(plant.max_hydration_lvl - plant.hydration) - - def fertilization(self, plant): - if type(plant) == Tomato: - if plant.soil_level < 50: - plant.increase_soillevel(plant.max_soil_lvl - plant.soil_level) - - elif type(plant) == Cucumber: - if plant.soil_level < 40: - plant.increase_soillevel(plant.max_soil_lvl - plant.soil_level) - - async def service(self): - "tutaj trzeba zapytac env z jakimi roslinami sie styka traktor i je obsłuzyc" - pass async def get_stats(self, location): reader, writer = await asyncio.open_connection('127.0.0.1', 8888) @@ -221,10 +217,18 @@ class Tractor: return {"ttl": int(parsed_data[0]), "is_alive": int(parsed_data[1]), "hydration": int(parsed_data[2]), "soil_level": int(parsed_data[3]), "ready": int(parsed_data[4])} + async def service(self): + plant_location = self.look_at_plants(self.current_location) + plant_stats = self.get_stats(plant_location) + TreeClass.make_tree(self, plant_location, plant_stats) + "tutaj trzeba zapytac env z jakimi roslinami sie styka traktor i je obsłuzyc" + + pass if __name__ == "__main__": start = (0,0) goal = (10,6) tractor = Tractor((0,0), 'N') # asyncio.run(tractor.move_tractor(start, goal)) asyncio.run(tractor.run()) + asyncio.service(tractor.service())