Skip to content

Commit 98fa26e

Browse files
committed
Edit: All targeted_locomotion fitness functions.
1 parent 7351c69 commit 98fa26e

File tree

1 file changed

+53
-21
lines changed

1 file changed

+53
-21
lines changed
Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,58 @@
11
"""Targeted locomotion."""
2+
import numpy as np
23

4+
def _xy_distance(a: np.ndarray, b: np.ndarray) -> float:
5+
"""Euclidean distance in the horizontal plane (x, y)."""
6+
return float(np.linalg.norm(a[:2] - b[:2]))
37

4-
def distance_to_target(
5-
initial_position: tuple[float, float],
6-
target_position: tuple[float, float],
8+
9+
def distance_to_target(final_position: np.ndarray, target_position: np.ndarray) -> float:
10+
"""Remaining planar distance to target (lower is better for minimization)."""
11+
res = float(np.linalg.norm(final_position[:2] - target_position[:2]))
12+
return res
13+
14+
def fitness_delta_distance(initial_pos: np.ndarray, final_pos: np.ndarray, target_pos: np.ndarray) -> float:
15+
"""Rewards closing distance. Flipped for minimization (lower score is better)."""
16+
initial_dist = _xy_distance(initial_pos, target_pos)
17+
final_dist = _xy_distance(final_pos, target_pos)
18+
19+
return float(final_dist - initial_dist)
20+
21+
def fitness_distance_and_efficiency(initial_pos: np.ndarray, final_pos: np.ndarray, target_pos: np.ndarray, total_control_effort: float) -> float:
22+
delta_dist = fitness_delta_distance(initial_pos, final_pos, target_pos)
23+
24+
# FIXED: Added penalty (Higher score = worse)
25+
effort_penalty = total_control_effort * 0.001
26+
return float(delta_dist + effort_penalty)
27+
28+
def fitness_survival_and_locomotion(initial_pos: np.ndarray, final_pos: np.ndarray, target_pos: np.ndarray, min_z_height: float) -> float:
29+
if min_z_height < 0.05:
30+
return 10.0
31+
32+
return fitness_delta_distance(initial_pos, final_pos, target_pos)
33+
34+
def fitness_direct_path(initial_pos: np.ndarray, final_pos: np.ndarray, target_pos: np.ndarray, total_path_length: float) -> float:
35+
delta_dist = fitness_delta_distance(initial_pos, final_pos, target_pos)
36+
straight_line_displacement = _xy_distance(final_pos, initial_pos)
37+
wasted_movement = total_path_length - straight_line_displacement
38+
39+
path_penalty = wasted_movement * 0.5
40+
return float(delta_dist + path_penalty)
41+
42+
43+
def fitness_speed_to_target(
44+
time_to_target: float | None,
45+
duration: float,
46+
min_distance_to_target: float,
747
) -> float:
48+
"""Reward fast target arrival irrespective of path shape.
49+
50+
Lower score is better (minimization objective):
51+
- Reached target: score in [0, 1], proportional to arrival time.
52+
- Not reached: score > 1, penalized by closest achieved distance.
853
"""
9-
Euclidean distance between the current position and the target position.
10-
11-
Parameters
12-
----------
13-
initial_position : tuple
14-
The current position as (x, y).
15-
target_position : tuple
16-
The target position as (x, y).
17-
18-
Returns
19-
-------
20-
float
21-
The distance between the two positions.
22-
"""
23-
return (
24-
(initial_position[0] - target_position[0]) ** 2
25-
+ (initial_position[1] - target_position[1]) ** 2
26-
) ** 0.5
54+
safe_duration = max(float(duration), 1e-6)
55+
if time_to_target is not None:
56+
return float(np.clip(time_to_target / safe_duration, 0.0, 1.0))
57+
58+
return float(1.0 + min_distance_to_target)

0 commit comments

Comments
 (0)