From 2fe8737cfee1cf926ff7e0be73dc7ac45da01c26 Mon Sep 17 00:00:00 2001 From: Michael Winter Date: Sat, 14 Mar 2026 03:05:23 +0100 Subject: [PATCH] Improve target range factor with relative distance - Replace harsh 1/(1+distance) with bounded relative scoring - Factor > 1.0 when moving toward target, < 1.0 when moving away - Minimum factor 0.1 to avoid zero weights - Test with max_path=150 shows reaching ~90% of 2-octave target --- src/graph.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) 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)."""