visualization

main
Harald Holtmann 2025-09-06 21:02:47 +02:00
parent 5d2d39586e
commit 6b7a2d3da5
3 changed files with 39 additions and 11 deletions

@ -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())

@ -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")

@ -1 +1,2 @@
requests
pydot