3
3
import math
4
4
5
5
6
- def linear_cost (start , end , cost = 1 ):
6
+ def linear_cost (scale = 1 ):
7
7
"""
8
8
Manhattan distance, only linear movement is allowed
9
9
10
10
Args:
11
11
start (int, int): x and y coordinates of start point
12
12
end (int, int): x and y coordinates of end point
13
- cost (int): cost of one step
13
+ scale (int): scale of one step
14
14
15
15
Returns:
16
- Linear cost between start and end point
16
+ Returns linear cost function between start and end point
17
17
"""
18
18
19
- delta_x = abs (start [0 ] - end [0 ])
20
- delta_y = abs (start [1 ] - end [1 ])
21
- return (delta_x + delta_y ) * cost
19
+ def cost (start , end ):
20
+ delta_x = abs (start [0 ] - end [0 ])
21
+ delta_y = abs (start [1 ] - end [1 ])
22
+ return (delta_x + delta_y ) * scale
22
23
24
+ return cost
23
25
24
- def euclidean_cost (start , end , cost = 1 ):
26
+ def euclidean_cost (scale = 1 ):
25
27
"""
26
28
Euclidean distance, linear and diagonal movement is allowed,
27
29
cost of diagonal movement is calculated using square root method
28
30
29
31
Args:
30
32
start (int, int): x and y coordinates of start point
31
33
end (int, int): x and y coordinates of end point
32
- cost (int): cost of one step
34
+ scale (int): scale of one step
33
35
34
36
Returns:
35
- Euclidean cost between start and end point
37
+ Returns euclidean cost fuction between start and end point
36
38
"""
37
39
38
- delta_x = abs (start [0 ] - end [0 ])
39
- delta_y = abs (start [1 ] - end [1 ])
40
- return math .sqrt (delta_x * delta_x + delta_y * delta_y ) * cost
40
+ def cost (start , end ):
41
+ delta_x = abs (start [0 ] - end [0 ])
42
+ delta_y = abs (start [1 ] - end [1 ])
43
+ return math .sqrt (delta_x * delta_x + delta_y * delta_y ) * scale
41
44
45
+ return cost
42
46
43
- def diagonal_cost (start , end , lin = 1 , diag = 1 ):
47
+
48
+ def diagonal_cost (lin = 1 , diag = 1 ):
44
49
"""
45
50
Diagonal distance, 8 directions.
46
51
Linear and diagonal movement is allowed at same cost
@@ -50,35 +55,39 @@ def diagonal_cost(start, end, lin=1, diag=1):
50
55
Args:
51
56
start (int, int): x and y coordinates of start point
52
57
end (int, int): x and y coordinates of end point
53
- lin int: cost of one linear step
54
- diag int: cost of one diagonal step
58
+ lin int: scale of one linear step
59
+ diag int: scale of one diagonal step
55
60
56
61
Returns:
57
- Diagonal cost between start and end point
62
+ Returns diagonal cost function between start and end point
58
63
"""
59
64
60
- delta_x = abs (start [0 ] - end [0 ])
61
- delta_y = abs (start [1 ] - end [1 ])
62
- return (delta_x + delta_y ) * lin + min (delta_x , delta_y ) * (diag - 2 * lin )
65
+ def cost (start , end ):
66
+ delta_x = abs (start [0 ] - end [0 ])
67
+ delta_y = abs (start [1 ] - end [1 ])
68
+ return (delta_x + delta_y ) * lin + min (delta_x , delta_y ) * (diag - 2 * lin )
69
+
70
+ return cost
63
71
64
72
65
- def scaled_cost (h_func , p_scale , * args ):
73
+ def scaled_cost (h_func , p_scale ):
66
74
"""
67
75
Scales cost function based on given parameter
68
76
69
77
Args:
70
78
h_func: cost function
71
79
p_scale: scales cost function multiple times
72
- *args: arguments passed to cost function
73
80
74
81
Returns:
75
- Scaled value of cost cost
82
+ Scaled cost function
76
83
"""
77
84
78
- return h_func (* args ) * p_scale
85
+ def cost (start , end ):
86
+ return h_func (start , end ) * p_scale
79
87
88
+ return cost
80
89
81
- def randomized_cost (sigma , mu , h_func , * args ):
90
+ def randomized_cost (sigma , mu , h_func ):
82
91
"""
83
92
Generates random number with normal distribution based on given sigma and mu.
84
93
Scales cost function by generated random number. Suggested values are
@@ -88,10 +97,12 @@ def randomized_cost(sigma, mu, h_func, *args):
88
97
sigma: standard deviation in normal distribution
89
98
mu: average value in normal distribution
90
99
h_func: cost function
91
- *args: arguments passed to cost function
92
100
93
101
Returns:
94
- Randomly scaled value of cost cost
102
+ Randomly scaled cost function
95
103
"""
96
104
97
- return h_func (* args ) * random .normalvariate (mu , sigma )
105
+ def cost (start , end ):
106
+ return h_func (start , end ) * random .normalvariate (mu , sigma )
107
+
108
+ return cost
0 commit comments