From 6b7a2d3da51b9e5fd559faafad6047f5eaef24a5 Mon Sep 17 00:00:00 2001 From: Harald Holtmann Date: Sat, 6 Sep 2025 21:02:47 +0200 Subject: [PATCH] visualization --- harald/explore.py | 30 +++++++++++++++++++----------- harald/graph.py | 19 +++++++++++++++++++ harald/requirements.txt | 1 + 3 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 harald/graph.py diff --git a/harald/explore.py b/harald/explore.py index afaa558..8e12fe6 100755 --- a/harald/explore.py +++ b/harald/explore.py @@ -1,6 +1,5 @@ #!/usr/bin/env python3 -import api import functools import json import os.path @@ -8,6 +7,9 @@ import sys import random from collections import defaultdict +import api +import graph + class ExploreError(Exception): pass @@ -261,21 +263,24 @@ class Explore: ) def guess(self): + aedi = graph.Aedificium(self.problem) + ids = {} - for i, rid in enumerate(self.room_ids.keys()): - ids[rid] = i + for i, p in enumerate(self.rooms.keys()): + ids[p] = i + aedi.add_node(str(p)) connected = set() connections = [] for path, ns in self.neighbors.items(): - src_id = self.rooms[path] - for src_door, (trg_path, trg_id) in enumerate(ns): - src = (src_id, src_door) + src_path = path + for src_door, (trg_path, _) in enumerate(ns): + src = (src_path, src_door) trg_door = next( ( j for j, (p, rid) in enumerate(self.neighbors[trg_path]) - if rid == src_id + if p == src_path ), None, ) @@ -283,7 +288,7 @@ class Explore: raise ExploreError( f"backlink not found: {(src, trg_path, self.neighbors[trg_path])}" ) - trg = (trg_id, trg_door) + trg = (trg_path, trg_door) if (src, trg) in connected or (trg, src) in connected: continue @@ -295,18 +300,21 @@ class Explore: "to": {"room": ids[trg[0]], "door": trg[1]}, } ) + aedi.add_edge(src[0], src[1], trg[0], trg[1]) layout = { - "rooms": [int(rid[0]) for rid in ids.keys()], - "startingRoom": ids[self.rooms[Path()]], + "rooms": [int(self.rooms[p][0]) for p in ids.keys()], + "startingRoom": ids[Path()], "connections": connections, } # print(layout) + + aedi.render() return api.guess(layout) def room_solve(problem, nrooms, plen): - ex = Explore(problem, [d+d+d+d+d+d for d in DOORS[:plen]]) + ex = Explore(problem, [d + d + d + d + d + d for d in DOORS[:plen]]) print(ex.probes) api.select(ex.problem) res = ex.explore(Path()) diff --git a/harald/graph.py b/harald/graph.py new file mode 100644 index 0000000..4c3658c --- /dev/null +++ b/harald/graph.py @@ -0,0 +1,19 @@ +import pydot + + +class Aedificium: + PORTS = ["n", "ne", "se", "s", "sw", "nw"] + + def __init__(self, problem): + self.graph = pydot.Dot(problem, graph_type="graph") + + def add_node(self, name): + self.graph.add_node(pydot.Node(name, label=name, shape="hexagon")) + + def add_edge(self, scr, src_d, trg, trg_d): + self.graph.add_edge( + pydot.Edge(scr, trg) #, headport=self.PORTS[src_d], tailport=self.PORTS[trg_d]) + ) + + def render(self): + self.graph.write_png("aedificium.png") diff --git a/harald/requirements.txt b/harald/requirements.txt index f229360..bc34167 100644 --- a/harald/requirements.txt +++ b/harald/requirements.txt @@ -1 +1,2 @@ requests +pydot