diff --git a/src/graph.py b/src/graph.py index 7c9612b..860a495 100644 --- a/src/graph.py +++ b/src/graph.py @@ -343,11 +343,11 @@ class PathFinder: config: dict, cumulative_trans: "Pitch | None", ) -> float: - """Returns 1.0 if at target, 0.0 if far from target. + """Returns factor based on movement toward target. Target progresses based on position in path. + Factor > 1.0 if moving toward target, < 1.0 if moving away. """ - # Check weight - if 0, return 1.0 (neutral) if config.get("weight_target_range", 1) == 0: return 1.0 @@ -364,15 +364,27 @@ class PathFinder: progress = len(path) / max_path current_target = progress * target_cents + current_cumulative_cents = cumulative_trans.to_cents() + edge_trans = edge_data.get("transposition") new_cumulative = cumulative_trans.transpose(edge_trans) new_cumulative_cents = new_cumulative.to_cents() - # Closeness: 1.0 if at target, 0.0 if far if current_target <= 0: return 1.0 - distance = abs(new_cumulative_cents - current_target) - return 1.0 / (1.0 + distance) + + dist_before = abs(current_cumulative_cents - current_target) + dist_after = abs(new_cumulative_cents - current_target) + + if dist_before == 0: + return 1.0 + + if dist_after < dist_before: + return 1.0 + (dist_before - dist_after) / dist_before + elif dist_after > dist_before: + return max(0.1, 1.0 - (dist_after - dist_before) / dist_before) + else: + return 1.0 def is_hamiltonian(self, path: list["Chord"]) -> bool: """Check if a path is Hamiltonian (visits all nodes exactly once)."""