This commit is contained in:
Paulina J 2019-06-03 11:56:25 +02:00
parent 95dcef88cf
commit 66fe83073f
3 changed files with 69 additions and 60 deletions

75
Tree.py
View File

@ -27,41 +27,41 @@ class TreeClass:
#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],
features = [[0, True, 60, 90, 95],
[0, True, 60, 84, 88],
[0, True, 50, 78, 77],
[0, True, 45, 63, 68],
[0, True, 37, 54, 59],
[0, True, 20, 31, 62],
[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],
["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]
[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, 18, 75, 39],
[1, True, 19, 24, 74],
[1, True, 24, 69, 25],
[1, True, 15, 12, 85],
[1, True, 23, 85, 18],
[1, True, 34, 38, 36],
[1, True, 41, 24, 22],
[1, True, 34, 38, 36],
[1, True, 74, 26, 23],
[1, True, 84, 14, 19],
[1, 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]
@ -70,7 +70,7 @@ class TreeClass:
t = tree.DecisionTreeClassifier()
#znajdź wzór na podstawie danych
t = tree.fit(features, labels)#jakieś cechy w środku i decyzje jakie ma podjąć
t = t.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]])
@ -89,7 +89,10 @@ class TreeClass:
#wyświetlamy dopasowaną wartość
print(p)
pass
T = TreeClass()
#pass
"""
is_life = bool
type = "POop"

18
env.py Executable file → Normal file
View File

@ -99,8 +99,11 @@ def look_at_plats(field, location):
(location[0], location[1] - 1),
(location[0], location[1] + 1)
]
return wsp
plants = []
for i in wsp:
if field[i[0]][i[1]]:
plants.append(i)
return plants
def send_stats(field, location):
@ -132,9 +135,12 @@ if __name__ == "__main__":
elif message[0] == "move":
tractor.move()
writer.write(("OK\n").encode())
elif message[0] == "look":
tractor.move()
writer.write(("OK\n").encode())
elif message[0] == "look_at_plants":
plants = look_at_plats(field, (int(message[1]), int(message[2])))
plants_str = ""
for i in plants:
plants_str += (" " + str(i[0]) + " " + str(i[1]))
writer.write((plants_str + "\n").encode())
elif message[0] == "get_stats":
x = message[1]
y = message[2]
@ -153,7 +159,7 @@ if __name__ == "__main__":
async def main():
server = await asyncio.start_server(
handle_echo, '127.0.0.1', 8888)
handle_echo, '127.0.0.1', 8887)
addr = server.sockets[0].getsockname()

36
tractor.py Executable file → Normal file
View File

@ -30,30 +30,30 @@ class Tractor(TreeClass):
async def rotate(self, direction):
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
reader, writer = await asyncio.open_connection('127.0.0.1', 8887)
writer.write(("rotate " + direction + "\n").encode())
await reader.readline()
time.sleep(self.sleep_time)
writer.close()
async def look_at_plants(self, location):
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
# writer.write(("rotate " + direction + "\n").encode())
await reader.readline()
reader, writer = await asyncio.open_connection('127.0.0.1', 8887)
writer.write(("look_at_plants " + str(location[0]) + " " + str(location[1]) + "\n").encode())
xd = await reader.readline()
xd = xd.split()
l = len(xd)
xd2 = []
for i in range(l):
xd2.append((int(xd[2*i]), int(xd[2*i+1])))
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
return xd2
async def try_move(self):
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
reader, writer = await asyncio.open_connection('127.0.0.1', 8887)
writer.write("try\n".encode())
data = await reader.readline()
result = data.decode()
@ -62,7 +62,7 @@ class Tractor(TreeClass):
return result
async def move(self):
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
reader, writer = await asyncio.open_connection('127.0.0.1', 8887)
writer.write("move\n".encode())
await reader.readline()
time.sleep(self.sleep_time)
@ -208,7 +208,7 @@ class Tractor(TreeClass):
async def get_stats(self, location):
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
reader, writer = await asyncio.open_connection('127.0.0.1', 8887)
writer.write(("stats " + str(location[0]) + " " + str(location[1]) + "\n").encode())
data = await reader.readline()
parsed_data = data.decode().split()
@ -218,9 +218,10 @@ class Tractor(TreeClass):
"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)
plant_location = await self.look_at_plants(self.current_location)
for i in plant_location:
plant_stats = await self.get_stats(i)
TreeClass.make_tree(self, i, plant_stats)
"tutaj trzeba zapytac env z jakimi roslinami sie styka traktor i je obsłuzyc"
@ -231,4 +232,3 @@ if __name__ == "__main__":
tractor = Tractor((0,0), 'N')
# asyncio.run(tractor.move_tractor(start, goal))
asyncio.run(tractor.run())
asyncio.service(tractor.service())