From 0482238eb023866ebbcd2ead783297c1e715ea04 Mon Sep 17 00:00:00 2001 From: Naman-Vasudev Date: Tue, 21 Oct 2025 16:53:09 +0530 Subject: [PATCH 1/2] Create ordinal_representation.py --- genetic_algorithm/ordinal_representation.py | 60 +++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 genetic_algorithm/ordinal_representation.py diff --git a/genetic_algorithm/ordinal_representation.py b/genetic_algorithm/ordinal_representation.py new file mode 100644 index 000000000000..1ccf273af11b --- /dev/null +++ b/genetic_algorithm/ordinal_representation.py @@ -0,0 +1,60 @@ +""" +Ordinal Representation of a Closed Tour +--------------------------------------- + +Converts a path (e.g., a Hamiltonian tour) into its ordinal +representation based on a fixed alphabetical node reference. + +""" + + +def ordinal_representation_closed(path: list[str], nodes: list[str]) -> list[int]: + """ + Generate the ordinal representation for a closed path. + + Each element in the result denotes the position (1-indexed) + of the corresponding city in the current reference list, + which shrinks as cities are removed. + + Parameters + ---------- + path : list[str] + Sequence of cities in the closed tour. + nodes : list[str] + Reference list of all nodes, in fixed order (e.g., A-Z). + + Returns + ------- + list[int] + Ordinal representation of the path. + + Examples + -------- + >>> path = list("GLADBIKEHJFC") + >>> nodes = list("ABCDEFGHIJKL") + >>> ordinal_representation_closed(path, nodes) + [7, 11, 1, 3, 1, 5, 6, 2, 3, 3, 2, 1] + + >>> path = list("ALGC FJHEKIBD".replace(" ", "")) + >>> nodes = sorted(set(path)) + >>> ordinal_representation_closed(path, nodes) + [1, 11, 6, 2, 4, 6, 4, 3, 4, 3, 1, 1] + """ + reference = nodes.copy() + ordinal = [] + + for city in path: + index = reference.index(city) + 1 # 1-based index + ordinal.append(index) + reference.pop(index - 1) + + return ordinal + + +if __name__ == "__main__": + sample_path = list("ODGLAHKMBJFCNIE") + all_nodes = sorted(set(sample_path)) + ordinal_values = ordinal_representation_closed(sample_path, all_nodes) + + print("Ordinal Representation:") + print(" ".join(map(str, ordinal_values))) \ No newline at end of file From e221db3cc35dfe97368c327c039491062f55f314 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 21 Oct 2025 11:27:39 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- genetic_algorithm/ordinal_representation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/genetic_algorithm/ordinal_representation.py b/genetic_algorithm/ordinal_representation.py index 1ccf273af11b..d3688e39e90d 100644 --- a/genetic_algorithm/ordinal_representation.py +++ b/genetic_algorithm/ordinal_representation.py @@ -57,4 +57,4 @@ def ordinal_representation_closed(path: list[str], nodes: list[str]) -> list[int ordinal_values = ordinal_representation_closed(sample_path, all_nodes) print("Ordinal Representation:") - print(" ".join(map(str, ordinal_values))) \ No newline at end of file + print(" ".join(map(str, ordinal_values)))