Skip to content

Commit

Permalink
direction map is working #6
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrKedra committed Dec 3, 2018
1 parent 496cc99 commit 38e48ff
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 34 deletions.
130 changes: 107 additions & 23 deletions src/environment/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,107 @@

from environment.environment_enum import Env
from environment.a_star import astar
from environment.line import Point, Line


def direction_map(environment, exit_points, step_size):
""" Return direction map with adjusted step_size"""
pass
mapped_environment = copy.deepcopy(environment)
for i in range(0, len(mapped_environment)):
for j in range(0, len(mapped_environment[i])):
if mapped_environment[i][j] == 1:
mapped_environment[i][j] = Env.OBSTACLE
else:
mapped_environment[i][j] = None

obstacles = get_obstacle_line_horizon(environment) + get_obstacle_line_vertical(environment)

for y in range(0, len(environment)):
for x in range(0, len(environment[y])):
if mapped_environment[y][x] is None:
fastest_paths = []
for point in exit_points:
fastest_paths.append(astar(environment, (y, x), point))

shortest_path = fastest_paths[0]
distance_of_shortest_path = path_distance(shortest_path)
for path in fastest_paths:
if distance_of_shortest_path > path_distance(path):
shortest_path = path
distance_of_shortest_path = path_distance(path)

for i in range(0, len(shortest_path)):
current_x = shortest_path[i][1]
current_y = shortest_path[i][0]
if i == len(shortest_path)-1:
mapped_environment[current_y][current_x] = Env.EXIT
else:
possible_step = step_size
possible_step_is_correct = True
while possible_step >= 1:
if i+possible_step < len(shortest_path):
point_to_go = Point(shortest_path[i + possible_step][1], shortest_path[i + possible_step][0])
else:
last_index = len(shortest_path)-1
point_to_go = Point(shortest_path[last_index][1], shortest_path[last_index][0])
possible_step = last_index - i

line = Line(Point(current_x, current_y), point_to_go)
for line_obstacle in obstacles:
if line.intersect(line_obstacle):
possible_step -= 1
possible_step_is_correct = False
break
else:
possible_step_is_correct = True
if possible_step_is_correct is True:
x_l = shortest_path[i + possible_step][1]
y_l = shortest_path[i + possible_step][0]
direction_point = Point(x_l, y_l)
mapped_environment[current_y][current_x] = direction_point
break
return mapped_environment


def get_obstacle_line_vertical(environment):
pass
is_line_started = False
lines = []
for i in range(0, len(environment[0])):
line_vertical = Line(None, None)
for j in range(0, len(environment)):
if environment[j][i] == 1 and is_line_started is False:
is_line_started = True
line_vertical.point_start = Point(i, j)
line_vertical.point_end = Point(i, j)
elif environment[j][i] == 1 and is_line_started is True:
line_vertical.point_end = Point(i, j)
elif environment[j][i] == 0 and is_line_started is True:
lines.append(line_vertical)
is_line_started = False
return lines


def get_obstacle_line_horizon(environment):
is_line_started = False
lines = []
for i in range(0, len(environment)):
line_horizon = Line(None, None)
for j in range(0, len(environment[i])):
if environment[i][j] == 1 and is_line_started is False:
is_line_started = True
line_horizon.point_start = Point(j, i)
line_horizon.point_end = Point(j, i)
elif environment[i][j] == 1 and is_line_started is True:
line_horizon.point_end = Point(j, i)
elif environment[i][j] == 0 and is_line_started is True:
lines.append(line_horizon)
is_line_started = False
return lines


def map_environment(environment, exit_points):
""" Return mapped environment, each cord has next cord in 'fastest' path to exit (for step_size = 1)"""

""" its should be deleted because we have direciton map, but i will delete it in next push """
mapped_environment = copy.deepcopy(environment)
for i in range(0, len(mapped_environment)):
for j in range(0, len(mapped_environment[i])):
Expand All @@ -30,26 +113,27 @@ def map_environment(environment, exit_points):

for y in range(0, len(environment)):
for x in range(0, len(environment[y])):
if environment[y][x] != 1: # 1 mean there is obstacle
if mapped_environment[y][x] is None:
fastest_paths = []
for point in exit_points:
fastest_paths.append(astar(environment, (y, x), point))

shortest_path = fastest_paths[0]
distance_of_shortest_path = path_distance(shortest_path)
for path in fastest_paths:
if distance_of_shortest_path > path_distance(path):
shortest_path = path
distance_of_shortest_path = path_distance(path)

for i in range(0, len(shortest_path)):
current_x = shortest_path[i][1]
current_y = shortest_path[i][0]
if i == len(shortest_path)-1:
mapped_environment[current_y][current_x] = Env.EXIT
else:
mapped_environment[current_y][current_x] = shortest_path[i+1]
if mapped_environment[y][x] is None:
fastest_paths = []
print("befor astar")
for point in exit_points:
fastest_paths.append(astar(environment, (y, x), point))

print("after astar")
shortest_path = fastest_paths[0]
distance_of_shortest_path = path_distance(shortest_path)
for path in fastest_paths:
if distance_of_shortest_path > path_distance(path):
shortest_path = path
distance_of_shortest_path = path_distance(path)

for i in range(0, len(shortest_path)):
current_x = shortest_path[i][1]
current_y = shortest_path[i][0]
if i == len(shortest_path)-1:
mapped_environment[current_y][current_x] = Env.EXIT
else:
mapped_environment[current_y][current_x] = shortest_path[i+1]

return mapped_environment

Expand Down
15 changes: 11 additions & 4 deletions src/environment/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@


class Line:

def __init__(self, point_s, point_e):
self.point_start = point_s
self.point_end = point_e

def __repr__(self):
return "(" + str(self.point_start) + ", " + str(self.point_end) + ")"

def intersect(self, line):
""" returns true if two line intersect"""
# general case
Expand All @@ -20,13 +24,13 @@ def intersect(self, line):
return True

# special case
if o1 == 0 and is_between(self.point_start, line.point_start, self.point_end):
if o1 == Intersect.COLLINEAR and is_between(self.point_start, line.point_start, self.point_end):
return True
elif o2 == 0 and is_between(self.point_start, line.point_end, self.point_end):
elif o2 == Intersect.COLLINEAR and is_between(self.point_start, line.point_end, self.point_end):
return True
elif o3 == 0 and is_between(line.point_start, self.point_start, line.point_end):
elif o3 == Intersect.COLLINEAR and is_between(line.point_start, self.point_start, line.point_end):
return True
elif o4 == 0 and is_between(line.point_start, self.point_end, line.point_end):
elif o4 == Intersect.COLLINEAR and is_between(line.point_start, self.point_end, line.point_end):
return True
else:
return False
Expand Down Expand Up @@ -61,3 +65,6 @@ class Point:
def __init__(self, x, y):
self.x = x
self.y = y

def __repr__(self):
return "(" + str(self.x) + ", " + str(self.y) + ")"
21 changes: 14 additions & 7 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from environment.a_star import astar
from environment.environment import map_environment

from environment.environment import map_environment, get_obstacle_line_horizon, get_obstacle_line_vertical, \
direction_map
from environment.line import Point, Line

maze = [[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
Expand All @@ -14,13 +15,19 @@
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]


print(get_obstacle_line_horizon(maze))
print(get_obstacle_line_vertical(maze))

for ele in map_environment(maze, [(9, 1),(9,2)]):
print(ele)



p1 = Point(0, 0)
p2 = Point(6, 0)

p3 = Point(4, 0)

l1 = Line(p1, p2)
l2 = Line(p3, p3)

for ele in map_environment(maze, [(9, 1)]):
print(ele)

for ele in direction_map(maze, [(9, 1)], 3):
print(ele)

0 comments on commit 38e48ff

Please sign in to comment.