Skip to content

Commit 66abcfa

Browse files
committed
Port tuples to Point class
1 parent 6ec3d18 commit 66abcfa

9 files changed

+170
-92
lines changed

helper.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import next_moves
2-
31
from itertools import tee
42

3+
from moves import adjacent_octile
4+
55

66
def pairwise(iterable):
77
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
@@ -24,7 +24,7 @@ def get_next_positions(point, moves):
2424

2525
def get_neighbors(point, map_size):
2626
m, n = map_size
27-
octile_moves = next_moves.adjacent_octile()
27+
octile_moves = adjacent_octile()
2828
nbors = get_next_positions(point, octile_moves)
2929

3030
def is_valid(point):
@@ -33,3 +33,21 @@ def is_valid(point):
3333

3434
selected_nbors = [point for point in nbors if is_valid(point)]
3535
return selected_nbors
36+
37+
38+
def get_x_and_y_from_store(store):
39+
x, y = [], []
40+
for key in store.keys():
41+
x.append(key.x)
42+
y.append(key.y)
43+
44+
return x, y
45+
46+
47+
def get_x_and_y_from_path(path):
48+
x, y = [], []
49+
for key in path:
50+
x.append(key.x)
51+
y.append(key.y)
52+
53+
return x, y

main.py

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1+
from timeit import default_timer as timer
2+
13
import map_generator
2-
import potential
3-
import numpy as np
4-
from movement_cost import linear_cost, randomized_cost
4+
from movement_cost import linear_cost
5+
from moves import adjacent_linear
56
from path_finding import PathFinder
6-
from terrain_analysis import TerrainAnalyzer
7-
from next_moves import adjacent_linear
8-
from timeit import default_timer as timer
7+
from point import Point
98

109
map_data = None
1110

1211

1312
def is_valid_pos(cur_pos):
14-
x, y = cur_pos
13+
x, y = cur_pos.x, cur_pos.y
1514
return 0 <= x < 100 and 0 <= y < 100 and map_data[y][x]
1615

1716

1817
if __name__ == "__main__":
1918
map_data = map_generator.generate_map(100, 100, seed=25, obstacle_density=0.35)
20-
start = (59, 45)
21-
mid = (0, 0)
22-
end = (86, 40)
19+
start = Point(59, 45)
20+
mid = Point(0, 0)
21+
end = Point(86, 40)
2322
waypoints = [start, mid, end]
2423
moves = adjacent_linear()
2524
cost_func = linear_cost()
@@ -28,11 +27,11 @@ def is_valid_pos(cur_pos):
2827
path, store = path_finder.find_path_waypoints(moves, waypoints, return_store=True)
2928
print(timer() - start)
3029
map_generator.view_path(map_data, path, store)
31-
start = timer()
32-
potn = potential.manhattan(map_data)
33-
print(timer() - start)
34-
potential.view_potential(potn)
35-
start = timer()
36-
terrainer = TerrainAnalyzer(map_data)
37-
print(timer() - start)
38-
terrainer.view_terrain(True)
30+
# start = timer()
31+
# potn = potential.manhattan(map_data)
32+
# print(timer() - start)
33+
# potential.view_potential(potn)
34+
# start = timer()
35+
# terrainer = TerrainAnalyzer(map_data)
36+
# print(timer() - start)
37+
# terrainer.view_terrain(True)

map_generator.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from helper import get_x_and_y_from_store
2+
from helper import get_x_and_y_from_path
3+
14
import matplotlib.pyplot as plt
25
import numpy as np
36
import perlin
@@ -31,18 +34,18 @@ def view_path(map_data, path, store=None, grid=True, markers=False):
3134
"""
3235
# plot all the points in the store
3336
if store:
34-
x, y = tuple(zip(*store.keys()))
37+
x, y = get_x_and_y_from_store(store)
3538
plt.scatter(x, y, color='orange')
3639
# plot all the points in the path
3740
if path:
3841
start = path[0]
3942
end = path[-1]
40-
x, y = tuple(zip(*path))
43+
x, y = get_x_and_y_from_path(path)
4144
plt.plot(x, y, color='blue')
4245
if markers:
4346
plt.scatter(x, y, color='green')
44-
plt.plot(start[0], start[1], 'gx')
45-
plt.plot(end[0], end[1], 'rx')
47+
plt.plot(start.x, start.y, 'gx')
48+
plt.plot(end.x, end.y, 'rx')
4649
view_map(map_data, grid)
4750
else:
4851
print("Empty path")

movement_cost.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def linear_cost(scale=1):
1616
"""
1717

1818
def cost(start, end):
19-
delta_x = abs(start[0] - end[0])
20-
delta_y = abs(start[1] - end[1])
19+
delta_x = abs(start.x - end.x)
20+
delta_y = abs(start.y - end.y)
2121
return (delta_x + delta_y) * scale
2222

2323
return cost
@@ -38,8 +38,8 @@ def euclidean_cost(scale=1):
3838
"""
3939

4040
def cost(start, end):
41-
delta_x = abs(start[0] - end[0])
42-
delta_y = abs(start[1] - end[1])
41+
delta_x = abs(start.x - end.x)
42+
delta_y = abs(start.y - end.y)
4343
return math.sqrt(delta_x * delta_x + delta_y * delta_y) * scale
4444

4545
return cost
@@ -63,8 +63,8 @@ def diagonal_cost(lin=1, diag=1):
6363
"""
6464

6565
def cost(start, end):
66-
delta_x = abs(start[0] - end[0])
67-
delta_y = abs(start[1] - end[1])
66+
delta_x = abs(start.x - end.x)
67+
delta_y = abs(start.y - end.y)
6868
return (delta_x + delta_y) * lin + min(delta_x, delta_y) * (diag - 2 * lin)
6969

7070
return cost
@@ -107,3 +107,4 @@ def cost(start, end):
107107
return h_func(start, end) * random.normalvariate(mu, sigma)
108108

109109
return cost
110+

next_moves.py renamed to moves.py

+62-59
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from point import Point
2+
13
import random
24

35

@@ -15,14 +17,14 @@ def adjacent_linear(randomize=False):
1517
or not
1618
1719
Returns:
18-
List (int, int): List of x and y offsets from
20+
List (Point): List of point offsets from
1921
current position
2022
"""
2123
moves = [
22-
(0, 1),
23-
(1, 0),
24-
(0, -1),
25-
(-1, 0)
24+
Point(0, 1),
25+
Point(1, 0),
26+
Point(0, -1),
27+
Point(-1, 0)
2628
]
2729

2830
if randomize:
@@ -45,18 +47,18 @@ def adjacent_octile(randomize=False):
4547
or not
4648
4749
Returns:
48-
List (int, int): List of x and y offsets from
50+
List (Point): List of point offsets from
4951
current position
5052
"""
5153
moves = [
52-
(0, 1),
53-
(1, 0),
54-
(0, -1),
55-
(-1, 0),
56-
(1, 1),
57-
(1, -1),
58-
(-1, 1),
59-
(-1, -1)
54+
Point(0, 1),
55+
Point(1, 0),
56+
Point(0, -1),
57+
Point(-1, 0),
58+
Point(1, 1),
59+
Point(1, -1),
60+
Point(-1, 1),
61+
Point(-1, -1)
6062
]
6163

6264
if randomize:
@@ -81,22 +83,22 @@ def bc19_4_radius(randomize=False):
8183
or not
8284
8385
Returns:
84-
List (int, int): List of x and y offsets from
86+
List (Point): List of point offsets from
8587
current position
8688
"""
8789
moves = [
88-
(0, 1),
89-
(1, 0),
90-
(0, -1),
91-
(-1, 0),
92-
(1, 1),
93-
(1, -1),
94-
(-1, 1),
95-
(-1, -1),
96-
(2, 0),
97-
(0, 2),
98-
(0, -2),
99-
(-2, 0)
90+
Point(0, 1),
91+
Point(1, 0),
92+
Point(0, -1),
93+
Point(-1, 0),
94+
Point(1, 1),
95+
Point(1, -1),
96+
Point(-1, 1),
97+
Point(-1, -1),
98+
Point(2, 0),
99+
Point(0, 2),
100+
Point(0, -2),
101+
Point(-2, 0)
100102
]
101103

102104
if randomize:
@@ -123,38 +125,38 @@ def bc19_9_radius(randomize=False):
123125
or not
124126
125127
Returns:
126-
List (int, int): List of x and y offsets from
128+
List (Point): List of point offsets from
127129
current position
128130
"""
129131
moves = [
130-
(0, 1),
131-
(1, 0),
132-
(0, -1),
133-
(-1, 0),
134-
(1, 1),
135-
(1, -1),
136-
(-1, 1),
137-
(-1, -1),
138-
(2, 0),
139-
(0, 2),
140-
(0, -2),
141-
(-2, 0),
142-
(0, 3),
143-
(1, 2),
144-
(2, 1),
145-
(3, 0),
146-
(2, -1),
147-
(1, -2),
148-
(0, -3),
149-
(-1, -2),
150-
(-2, -1),
151-
(-3, 0),
152-
(-2, 1),
153-
(-1, 2),
154-
(2, 2),
155-
(2, -2),
156-
(-2, 2),
157-
(-2, -2)
132+
Point(0, 1),
133+
Point(1, 0),
134+
Point(0, -1),
135+
Point(-1, 0),
136+
Point(1, 1),
137+
Point(1, -1),
138+
Point(-1, 1),
139+
Point(-1, -1),
140+
Point(2, 0),
141+
Point(0, 2),
142+
Point(0, -2),
143+
Point(-2, 0),
144+
Point(0, 3),
145+
Point(1, 2),
146+
Point(2, 1),
147+
Point(3, 0),
148+
Point(2, -1),
149+
Point(1, -2),
150+
Point(0, -3),
151+
Point(-1, -2),
152+
Point(-2, -1),
153+
Point(-3, 0),
154+
Point(-2, 1),
155+
Point(-1, 2),
156+
Point(2, 2),
157+
Point(2, -2),
158+
Point(-2, 2),
159+
Point(-2, -2)
158160
]
159161

160162
if randomize:
@@ -177,11 +179,12 @@ def adjacent_linear_directional(up_down=True):
177179
or left right movement
178180
179181
Returns:
180-
List (int, int): List of x and y offsets from
182+
List (Point): List of point offsets from
181183
current position
182184
"""
183185

184186
if up_down:
185-
return [(0, 1), (0, -1)]
187+
return [Point(0, 1), Point(0, -1)]
186188
else:
187-
return [(1, 0), (-1, 0)]
189+
return [Point(1, 0), Point(-1, 0)]
190+

path_finding.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def total_cost(cur_pos, next_pos, end):
182182
if cur_pos == end:
183183
return store # return store on reaching end
184184

185-
next_moves = [helper.add_tuple_elements(cur_pos, move) for move in moves]
185+
next_moves = [cur_pos + move for move in moves]
186186
valid_moves = [move for move in next_moves if self.is_valid_move(move)]
187187
possible_state = [(total_cost(cur_pos, move, end), move) for move in valid_moves]
188188
valid_state = [(next_score, next_pos) for (next_score, next_pos) in possible_state
@@ -193,3 +193,20 @@ def total_cost(cur_pos, next_pos, end):
193193
queue.push((next_score, next_pos))
194194

195195
return {}
196+
197+
def best_potential_step(self, cur_pos, path=None, range=None):
198+
"""
199+
Finds the point with the best potential score for the next step.
200+
If path and range is given, finds the best potential score point
201+
within the range of the next step prescribed by path.
202+
203+
Args:
204+
cur_pos Point: current position
205+
path List(Point): prescribed path from source to destination
206+
range int: cutoff range from points along the path
207+
208+
Return:
209+
Point: best step
210+
"""
211+
return None
212+

0 commit comments

Comments
 (0)