Compare commits

...

2 Commits

Author SHA1 Message Date
Harald Holtmann 7019f11e50 execution script 2025-09-07 16:28:31 +02:00
Harald Holtmann a880df0181 aggregate explore requests 2025-09-07 16:28:10 +02:00
2 changed files with 44 additions and 74 deletions

@ -1,5 +1,6 @@
#!/usr/bin/env python3
import itertools
import functools
import json
import os.path
@ -87,22 +88,24 @@ class Explore:
return path
def explore(self, path=Path(), probes=None, mark=Path()):
def explore(self, paths, probes=None, mark=Path()):
probes = probes or self.probes
path = self._path(path)
paths = [mark + path + probe for probe in probes]
num_marks = mark.path.count("[")
prefix_len = len(mark) - 2 * num_marks + len(path)
expaths = []
for path in paths:
path = self._path(path)
expaths.extend(mark + path + probe for probe in probes)
print("explore", paths)
response = api.explore([p.path for p in paths])
response = api.explore([p.path for p in expaths])
results = response["results"]
self.score = response["queryCount"]
print("id", path, results)
return [res[prefix_len:] for res in results]
for path, presults in zip(paths, itertools.batched(results, len(probes))):
prefix_len = len(mark) - 2 * num_marks + len(path)
print("id", path, results)
self.update(path, [res[prefix_len:] for res in presults])
def _add_room(self, path, results):
label = results[0][0]
@ -241,10 +244,13 @@ class Explore:
break
def unexplored(self):
unexplored = []
for path, ns in self.neighbors.items():
for d, (p, rid) in enumerate(ns):
if not rid:
yield d, path
unexplored.append((d, path))
if unexplored:
yield unexplored
def is_explored(self):
return next(self.unexplored(), None) is None and all(len(p) == 1 for p in self.room_ids.values())
@ -367,19 +373,19 @@ def mark_solve(problem, nrooms):
ex = Explore(problem, probes)
exs.append(ex)
res = ex.explore(Path(), mark=mark_loop)
ex.update(Path(), res)
ex.explore([Path()], mark=mark_loop)
ex.dump()
while True:
door, unexplored = next(ex.unexplored(), (None, None))
unexplored = next(ex.unexplored(), None)
if unexplored is None:
break
print("explore", door, unexplored)
path = unexplored + Path([door])
res = ex.explore(path, mark=mark_loop)
ex.update(path, res)
paths = []
for door, path in unexplored:
paths.append(path + Path([door]))
print("explore", unexplored)
ex.explore(paths, mark=mark_loop)
ex.unify_all()
ex.dump()
@ -424,7 +430,7 @@ def mark_solve(problem, nrooms):
if t == Path():
start = s
print("use as start", start)
assert start
assert start is not None
layout = ex.layout(orig=orig, start=starts[0])
guess_ok = api.guess(layout)
@ -433,60 +439,6 @@ def mark_solve(problem, nrooms):
return guess_ok
def path_solve(problem, nrooms, plen):
maxlen = 18 * nrooms
probe = Path([random.randrange(6) for _ in range(plen)])
print("probe path", probe)
ex = Explore(problem, probe)
api.select(ex.problem)
def extendpath(path, maxlen):
while len(path) < maxlen - plen - 1:
path = path + Path([random.randrange(6)]) + probe
return path
def target(door, path):
print("target", door, path)
probe0 = extendpath(probe, maxlen - len(path))
probe1 = extendpath(Path([door]) + probe, maxlen - len(path))
res0, res1 = ex.explore(path, [probe0, probe1])
ex.update_path(path, door, res0[: plen + 1], res1[: plen + 2])
ex.dump()
ex.unify_all()
ex.dump()
i = plen + 1
while i + plen < len(res0):
ex.update(path + probe0[:i], [res0[i : i + 1], res0[i : i + plen + 1]])
ex.unify_all()
i += plen + 1
i = plen + 2
while i + plen < len(res1):
ex.update(path + probe1[:i], [res1[i : i + 1], res1[i : i + plen + 1]])
ex.unify_all()
i += plen + 1
target(0, Path())
ex.dump()
while True:
door, unexplored = next(ex.unexplored(), (None, None))
print("unexplored", door, unexplored)
if unexplored is None:
break
target(door, unexplored)
ex.dump()
print("explored", ex.is_explored())
if ex.is_explored():
print("guess", ex.guess())
print("score", ex.score)
if __name__ == "__main__":
problem = sys.argv[1]
@ -504,9 +456,9 @@ if __name__ == "__main__":
api.clean_explore_cache()
except ExploreError as exc:
api.clean_explore_cache()
print(exc)
raise exc
except Exception as exc:
api.write_explore_cache()
print(exc)
raise exc
sys.exit(0 if ok else 1)

@ -0,0 +1,18 @@
#! /bin/bash
PROBLEMS="$*"
if [ -z "$PROBLEMS" ] ; then
PROBLEMS=$(jq -r '.[]|.problem' ../problems.json)
fi
TRIES=${TRIES:-10}
for problem in $PROBLEMS; do
for t in $(seq $TRIES); do
./explore.py $problem
if [ $? -eq 0 ]; then
break
fi
done
done