This commit is contained in:
marcinljablonski 2019-04-30 00:08:38 +02:00
parent 4642d1d86e
commit 065bcf1839
6 changed files with 199 additions and 99 deletions

12
Trac.py
View File

@ -27,3 +27,15 @@ class Trac:
def get_rotation(self):
return self.__rotation
def move(self):
x = self.get_position().get_x()
y = self.get_position().get_y()
if self.__rotation == 'N':
self.set_position((x - 1 ,y))
elif rotation == 'S':
self.set_position((x + 1 ,y))
elif rotation == 'W':
self.set_position((x, y - 1))
else:
self.set_position((x, y + 1))

Binary file not shown.

32
env.py
View File

@ -24,19 +24,20 @@ def initialize_field():
for j in range(7):
if i == 0 or i == 10 or j == 0 \
or j == 3 or j == 6:
# row.append((Point((i, j)), None))
row.append(None)
elif j < 3:
# row.append((Point((i, j)), Cucumber()))
row.append(Cucumber())
else:
# row.append((Point((i, j)), Tomato()))
row.append(Tomato())
field.append(row)
return field
def print_field(field, tractor):
if sys.platform == "win32":
os.system("cls")
else:
os.system("clear")
sys.stdout.write(OKBLUE)
sys.stdout.write("$")
sys.stdout.write(" ")
@ -102,26 +103,15 @@ if __name__ == "__main__":
if message[0] == "rotate":
tractor.set_rotation(message[1])
writer.write("OK\n".encode())
elif try_move(field, tractor):
x = tractor.get_position().get_x()
y = tractor.get_position().get_y()
rotation = tractor.get_rotation()
if rotation == 'N':
x -= 1
elif rotation == 'S':
x += 1
elif rotation == 'W':
y -= 1
elif message[0] == "try":
if try_move(field, tractor):
writer.write("OK\n".encode())
else:
y += 1
tractor.set_position((x,y))
writer.write(("OK " + rotation + "\n").encode())
writer.write("FAIL\n".encode())
else:
writer.write("FAIL\n".encode())
if sys.platform == "win32":
os.system("cls")
else:
os.system("clear")
tractor.move()
writer.write(("OK\n").encode())
print_field(field, tractor)
# print(tractor.get_position().get_x())
# print(tractor.get_position().get_y())

115
prezentacja.md Normal file
View File

@ -0,0 +1,115 @@
# O algorytmie
Algorytm A* od wierzchołka początkowego tworzy ścieżkę, za każdym razem wybierając wierzchołek x z
dostępnych w danym kroku niezbadanych wierzchołków tak, by minimalizować funkcję f ( x ) f(x) zdefiniowaną:
f ( x ) = g ( x ) + h ( x ) ,
gdzie:
g ( x ) droga pomiędzy wierzchołkiem początkowym a x.
h ( x ) - przewidywana przez heurystykę droga od x do wierzchołka docelowego.
W każdym kroku algorytm dołącza do ścieżki wierzchołek o najniższym współczynniku f.
Kończy w momencie natrafienia na wierzchołek będący wierzchołkiem docelowym.
\pagebreak
### Inicjalizacja parametrów
```python
async def tractor():
directions = ['N', 'W', 'S', 'E']
current_rotation = 'N'
last_rotation = None
start = (0,0)
current_position = start
goal = (10,6)
frontier = PriorityQueue()
frontier.put(start, 0)
cost_so_far = {}
cost_so_far[start] = 0
start_flag = True
```
```python
while not frontier.empty():
local_graph = []
current = frontier.get()
last_position = current
if not start_flag:
if current[0] == last_position[0] and current[1] > last_position[1]:
current_rotation = 'E'
elif current[0] == last_position[0] and current[1] < last_position[1]:
current_rotation = 'W'
elif current[0] > last_position[0]:
current_rotation = 'S'
elif current[0] < last_position[0]:
current_rotation = 'N'
last_rotation = current_rotation
```
### budawanie "lokalnego drzewa"
```python
for i, direction in enumerate(directions):
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
writer.write(("rotate " + direction + "\n").encode())
data = await reader.readline()
time.sleep(0.7)
writer.write("move\n".encode())
data = await reader.readline()
result = data.decode().split()
time.sleep(0.7)
if result[0] == "OK":
if direction == 'N':
local_graph.append((current[0] - 1, current[1]))
elif direction == 'S':
local_graph.append((current[0] + 1, current[1]))
elif direction == 'W':
local_graph.append((current[0], current[1] - 1 ))
else:
local_graph.append((current[0], current[1] + 1 ))
writer.write(("rotate " + directions[((i + 2) % 4)] + "\n").encode())
data = await reader.readline()
time.sleep(0.7)
writer.write("move\n".encode())
data = await reader.readline()
time.sleep(0.7)
```
### właściwa część algorytmu
```python
writer.write(("rotate " + current_rotation + "\n").encode())
data = await reader.readline()
time.sleep(0.7)
writer.write("move\n".encode())
data = await reader.readline()
time.sleep(0.7)
if current == goal:
break
for next in local_graph:
new_cost = cost_so_far[current] + 1
if next not in cost_so_far or new_cost < cost_so_far[next]:
cost_so_far[next] = new_cost
priority = new_cost + heuristic(goal, next)
frontier.put(next, priority)
start_flag = False
writer.close()
```
### heurestyka
```python
def heuristic(a, b):
(x1, y1) = a
(x2, y2) = b
return abs(x1 - x2) + abs(y1 - y2)
```

BIN
prezentacja.pdf Normal file

Binary file not shown.

View File

@ -8,109 +8,87 @@ def heuristic(a, b):
(x2, y2) = b
return abs(x1 - x2) + abs(y1 - y2)
def addnode(local_graph, direction):
if direction == 'N':
local_graph.append((current[0] - 1, current[1]))
elif direction == 'S':
local_graph.append((current[0] + 1, current[1]))
elif direction == 'W':
local_graph.append((current[0], current[1] - 1 ))
else:
local_graph.append((current[0], current[1] + 1 ))
async def tractor():
return local_graph
def getrotation(a, b):
y = b[0] - a[0]
x = b[1] - a[1]
if y > 0:
return 'S'
elif y < 0:
return 'N'
elif x > 0:
return 'E'
else:
return 'W'
async def move_tractor(start_position, goal):
directions = ['N', 'W', 'S', 'E']
current_rotation = 'N'
last_rotation = None
start = (0,0)
current_position = start
goal = (10,6)
last_node = None
current_position = start_position
frontier = PriorityQueue()
frontier.put(start, 0)
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
start_flag = True
while not frontier.empty():
local_graph = []
# is_already_moved = False
current = frontier.get()
print(start_flag)
last_position = current
if not start_flag:
if current[0] == last_position[0] and current[1] > last_position[1]:
current_rotation = 'E'
elif current[0] == last_position[0] and current[1] < last_position[1]:
current_rotation = 'W'
elif current[0] > last_position[0]:
current_rotation = 'S'
elif current[0] < last_position[0]:
current_rotation = 'N'
last_rotation = current_rotation
for i, direction in enumerate(directions):
if direction == last_rotation:
for direction in directions:
if direction == last_node:
continue
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
# current_rotation = directions[((i - 1) % 4)]
writer.write(("rotate " + direction + "\n").encode())
data = await reader.readline()
writer.close()
await reader.readline()
time.sleep(0.7)
writer.close()
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
writer.write("move\n".encode())
writer.write("try\n".encode())
data = await reader.readline()
result = data.decode().split()
writer.close()
result = data.decode()
time.sleep(0.7)
if result[0] == "OK":
print("Jade na "+result[1])
# print("ok"+result[1])
if direction == 'N':
local_graph.append((current_position[0] - 1, current_position[1]))
elif direction == 'S':
local_graph.append((current_position[0] + 1, current_position[1]))
elif direction == 'W':
local_graph.append((current_position[0], current_position[1] - 1 ))
else:
local_graph.append((current_position[0], current_position[1] + 1 ))
print(local_graph)
writer.close()
# print("nawrót")
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
writer.write(("rotate " + directions[((i + 2) % 4)] + "\n").encode())
data = await reader.readline()
# print(current_rotation)
writer.close()
time.sleep(0.7)
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
writer.write("move\n".encode())
print("hop -nawrót")
data = await reader.readline()
writer.close()
time.sleep(0.7)
# reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
# current_rotation = directions[((i - 1) % 4)]
# writer.write(("rotate " + current_rotation + "\n").encode())
# data = await reader.readline()
# writer.close()
# time.sleep(0.7)
# else:
# reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
# current_rotation = directions[((i + 1) % 4)]
# writer.write(("rotate " + current_rotation + "\n").encode())
# data = await reader.readline()
# writer.close()
# time.sleep(0.7)
# print(":(")
if result == "OK":
local_graph = addnode(local_graph, direction)
current_rotation = directions[(directions.index(current_rotation) - 1) % 4]
current = frontier.get()
if not start_flag:
print("jaaazda")
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
writer.write(("rotate " + current_rotation + "\n").encode())
data = await reader.readline()
writer.close()
time.sleep(0.7)
rotation = getrotation(current_position, current)
if rotation != current_rotation:
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
writer.write(("rotate " + rotation + "\n").encode())
await reader.readline()
current_rotation = rotation
time.sleep(0.7)
writer.close()
last_node = directions[(directions.index(rotation) - 2) % 4]
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
writer.write("move\n".encode())
data = await reader.readline()
writer.close()
await reader.readline()
current_position = current
time.sleep(0.7)
writer.close()
if current == goal:
break
@ -121,7 +99,12 @@ async def tractor():
cost_so_far[next] = new_cost
priority = new_cost + heuristic(goal, next)
frontier.put(next, priority)
came_from[next] = current
print(next)
start_flag = False
asyncio.run(tractor())
writer.close()
if __name__ == "__main__":
start = (0,0)
goal = (10,6)
asyncio.run(move_tractor(start, goal))