From dd9df2ad331d18fb1b0182ad5cbe7b77b261d491 Mon Sep 17 00:00:00 2001 From: Michael Winter Date: Fri, 13 Mar 2026 05:07:24 +0100 Subject: [PATCH] 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. --- compact_sets.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/compact_sets.py b/compact_sets.py index 9d3d785..8a5d0f6 100644 --- a/compact_sets.py +++ b/compact_sets.py @@ -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