This commit is contained in:
Marcin Jabłoński 2019-06-03 14:42:39 +02:00
parent ce28016282
commit d13fa47643
5 changed files with 88 additions and 56 deletions

View File

@ -1,13 +1,13 @@
from Plant import Plant from Plant import Plant
class Cucumber(Plant): class Cucumber(Plant):
name = "Cucumber" name = 0
is_alive = True is_alive = True
ttl = 10000 ttl = 10000
hydration = 40 hydration = 40
soil_level = 40 soil_level = 40
dehydration_ratio = 0.8 dehydration_ratio = 2
desoil_ratio = 0.7 desoil_ratio = 1
max_soil_lvl = 40 max_soil_lvl = 40
optimal_soil_ratio = 2 optimal_soil_ratio = 2
max_hydration_lvl = 40 max_hydration_lvl = 40

View File

@ -1,13 +1,13 @@
from Plant import Plant from Plant import Plant
class Tomato(Plant): class Tomato(Plant):
name = "Tomato" name = 1
is_alive = True is_alive = True
ttl = 12000 ttl = 12000
hydration = 41 hydration = 41
soil_level = 41 soil_level = 41
dehydration_ratio = 0.7 dehydration_ratio = 1
desoil_ratio = 0.8 desoil_ratio = 2
max_soil_lvl = 40 max_soil_lvl = 40
optimal_soil_ratio = 2 optimal_soil_ratio = 2
max_hydration_lvl = 100 max_hydration_lvl = 100

91
Tree.py
View File

@ -1,6 +1,5 @@
from sklearn import tree from sklearn import tree
import collections
import subprocess
class TreeClass: class TreeClass:
#file = open("probne.pl", 'r').read() #file = open("probne.pl", 'r').read()
@ -27,41 +26,42 @@ class TreeClass:
#print(com) #print(com)
def make_tree(self, plant, pl_stats): def make_tree(self, plant, pl_stats):
features = [[0, True, 60, 90, 95], features = [
[0, True, 60, 84, 88], [1, True, 60, 90, 95],
[0, True, 50, 78, 77], [1, True, 60, 84, 88],
[0, True, 45, 63, 68], [1, True, 50, 78, 77],
[0, True, 37, 54, 59], [1, True, 45, 63, 68],
[0, True, 20, 31, 62], [1, True, 37, 54, 59],
[0, True, 18, 75, 39],
[0, True, 19, 24, 74],
[0, True, 24, 69, 25],
[0, True, 15, 45, 85],
[0, True, 23, 85, 48],
[0, True, 26, 41, 45],
[0, True, 21, 35, 32],
[0, True, 49, 24, 28],
[0, True, 64, 15, 14],
[0, True, 84, 4, 8],
[1, True, 55, 89, 84],
[1, True, 76, 91, 95],
[1, True, 45, 72, 71],
[1, True, 37, 64, 68],
[1, True, 26, 54, 59],
[1, True, 58, 42, 46],
[1, True, 20, 31, 62], [1, True, 20, 31, 62],
[1, True, 18, 75, 39], [1, True, 18, 75, 39],
[1, True, 19, 24, 74], [1, True, 19, 24, 74],
[1, True, 24, 69, 25], [1, True, 24, 69, 25],
[1, True, 15, 12, 85], [1, True, 15, 45, 85],
[1, True, 23, 85, 18], [1, True, 23, 85, 48],
[1, True, 34, 38, 36], [1, True, 26, 41, 45],
[1, True, 41, 24, 22], [1, True, 21, 35, 32],
[1, True, 34, 38, 36], [1, True, 49, 24, 28],
[1, True, 74, 26, 23], [1, True, 64, 15, 14],
[1, True, 84, 14, 19], [1, True, 84, 4, 8],
[1, True, 95, 7, 4]
[0, True, 55, 89, 84],
[0, True, 76, 91, 95],
[0, True, 45, 72, 71],
[0, True, 37, 64, 68],
[0, True, 26, 54, 59],
[0, True, 58, 42, 46],
[0, True, 20, 31, 62],
[0, True, 18, 75, 39],
[0, True, 19, 24, 74],
[0, True, 24, 69, 25],
[0, True, 15, 12, 85],
[0, True, 23, 85, 18],
[0, True, 34, 38, 36],
[0, True, 41, 24, 22],
[0, True, 34, 38, 36],
[0, True, 74, 26, 23],
[0, True, 84, 14, 19],
[0, 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] 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]
@ -74,18 +74,21 @@ class TreeClass:
#użyj modelu aby dopasować(podjąć decyzję) #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]) p = t.predict([[pl_stats["name"], pl_stats["is_alive"], pl_stats["ttl"], pl_stats["hydration"], pl_stats["soil_level"]]])
if p == 0: return p
print("Ready to cut")
t.Harvest() # if p == 0:
elif p == 1: # print("Ready to cut")
plant.increase_hydration(plant.max_hydration_lvl - plant.hydration) # return 0
elif p == 2: # # t.Harvest()
plant.increase_soillevel(plant.max_soil_lvl - plant.soil_level) # elif p == 1:
elif p == 3: # plant.increase_hydration(plant.max_hydration_lvl - plant.hydration)
plant.increase_hydration(plant.max_hydration_lvl - plant.hydration) # elif p == 2:
plant.increase_soillevel(plant.max_soil_lvl - plant.soil_level) # 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ść #wyświetlamy dopasowaną wartość
print(p) print(p)

10
env.py
View File

@ -111,7 +111,7 @@ def send_stats(field, location):
y = location[1] y = location[1]
stats = field[x][y].get_stats() stats = field[x][y].get_stats()
str_stats = str(stats["ttl"]) + " " + str(stats["is_alive"]) + " " + str(stats["hydration"]) + " " \ str_stats = str(stats["ttl"]) + " " + str(stats["is_alive"]) + " " + str(stats["hydration"]) + " " \
+ str(stats["soil_level"]) + " " + str(stats["ready"]) + "\n" + str(stats["soil_level"]) + " " + str(stats["ready"]) + " " + str(stats["name"]) + "\n"
return str_stats return str_stats
@ -146,6 +146,14 @@ if __name__ == "__main__":
y = int(message[2]) y = int(message[2])
stats = send_stats(field, (x, y)).encode() stats = send_stats(field, (x, y)).encode()
writer.write(stats) writer.write(stats)
elif message[0] == "inc_soil":
x = int(message[1])
y = int(message[2])
field[x][y].increase_soillevel(1)
elif message[0] == "inc_hydration":
x = int(message[1])
y = int(message[2])
field[x][y].increase_hydration(1)
print_field(field, tractor) print_field(field, tractor)

View File

@ -24,10 +24,19 @@ class Tractor(TreeClass):
(x2, y2) = b (x2, y2) = b
return abs(x1 - x2) + abs(y1 - y2) return abs(x1 - x2) + abs(y1 - y2)
# def look_at_plats(field, location): async def increase_soil_lvl(self, location):
reader, writer = await asyncio.open_connection('127.0.0.1', 8887)
writer.write(("inc_soil " + str(location[0]) + " " + str(location[1]) + "\n").encode())
await reader.readline()
time.sleep(self.sleep_time)
writer.close()
async def increase_hydration(self, location):
reader, writer = await asyncio.open_connection('127.0.0.1', 8887)
writer.write(("inc_hydration " + str(location[0]) + " " + str(location[1]) + "\n").encode())
await reader.readline()
time.sleep(self.sleep_time)
writer.close()
async def rotate(self, direction): async def rotate(self, direction):
reader, writer = await asyncio.open_connection('127.0.0.1', 8887) reader, writer = await asyncio.open_connection('127.0.0.1', 8887)
@ -222,14 +231,26 @@ class Tractor(TreeClass):
hydration = int(parsed_data[2]) hydration = int(parsed_data[2])
soil_level = int(parsed_data[3]) soil_level = int(parsed_data[3])
ready = int(parsed_data[4]) ready = int(parsed_data[4])
name = int(parsed_data[5])
# return {"ttl": int(parsed_data[0]), "is_alive": ((parsed_data[1]=="True")?True:False), "hydration": int(parsed_data[2]), "soil_level": int(parsed_data[3]), "ready": int(parsed_data[4])} # return {"ttl": int(parsed_data[0]), "is_alive": ((parsed_data[1]=="True")?True:False), "hydration": int(parsed_data[2]), "soil_level": int(parsed_data[3]), "ready": int(parsed_data[4])}
return ({"ttl": ttl,"is_alive": is_alive,"hydration": hydration,"soil_level": soil_level,"ready": ready}) return ({"ttl": ttl,"is_alive": is_alive,"hydration": hydration,"soil_level": soil_level,"ready": ready ,"name": name})
async def service(self): async def service(self):
plant_location = await self.look_at_plants(self.current_location) plant_location = await self.look_at_plants(self.current_location)
for i in plant_location: for i in plant_location:
plant_stats = await self.get_stats(i) plant_stats = await self.get_stats(i)
TreeClass.make_tree(self, i, plant_stats) p = TreeClass.make_tree(self, i, plant_stats)
if p == 0:
print("Ready to cut")
# t.Harvest()
elif p == 1:
await self.increase_hydration(i)
elif p == 2:
await self.increase_soil_lvl(i)
elif p == 3:
await self.increase_hydration(i)
await self.increase_soil_lvl(i)
"tutaj trzeba zapytac env z jakimi roslinami sie styka traktor i je obsłuzyc" "tutaj trzeba zapytac env z jakimi roslinami sie styka traktor i je obsłuzyc"