Fix Hamiltonian to track untransposed graph nodes

The Hamiltonian weight was comparing transposed output chords
against untransposed graph nodes, so they never matched.
Now tracks graph_path separately for correct Hamiltonian check.
This commit is contained in:
Michael Winter 2026-03-13 05:07:24 +01:00
parent 40a8996b4e
commit dd9df2ad33

View file

@ -762,6 +762,9 @@ class PathFinder:
path = [output_chord]
last_graph_nodes = (graph_node,)
# Track graph nodes separately for Hamiltonian check (untransposed)
graph_path = [graph_node]
# Track cumulative transposition across all steps
# Start with identity (zero transposition)
dims = output_chord.dims
@ -789,6 +792,7 @@ class PathFinder:
last_graph_nodes,
weights_config,
tuple(voice_stay_count),
graph_path,
)
# Select edge stochastically
@ -831,6 +835,9 @@ class PathFinder:
# Move to next graph node
graph_node = next_graph_node
# Track graph nodes for Hamiltonian check
graph_path.append(graph_node)
path.append(output_chord)
last_graph_nodes = last_graph_nodes + (graph_node,)
if len(last_graph_nodes) > 2:
@ -898,6 +905,7 @@ class PathFinder:
last_chords: tuple[Chord, ...],
config: dict,
voice_stay_count: tuple[int, ...] | None = None,
graph_path: list[Chord] | None = None,
) -> list[float]:
"""Calculate weights for edges based on configuration."""
weights = []
@ -961,7 +969,8 @@ class PathFinder:
# Hamiltonian weight - favor unvisited nodes
if config.get("hamiltonian", False):
destination = edge[1]
if destination in path:
# Use graph_path (untransposed) for Hamiltonian check
if graph_path and destination in graph_path:
w *= 0.1 # Penalize revisiting nodes
else:
w *= 10 # Boost for unvisited nodes