From c835bf15da3ce39b856222d352c800f6ebd51acf Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 14 Aug 2025 13:32:19 +0200 Subject: Added attempt to make pause / resume work with python module --- python/examples/pause_resume.py | 35 +++++++++++++++++++++++++++++++++++ python/examples/solve.py | 6 ++---- 2 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 python/examples/pause_resume.py (limited to 'python/examples') diff --git a/python/examples/pause_resume.py b/python/examples/pause_resume.py new file mode 100644 index 0000000..5daebeb --- /dev/null +++ b/python/examples/pause_resume.py @@ -0,0 +1,35 @@ +# This example is meant to test the pause / resume / stop mechanism +# See the solve.py example for more details on how this works + +import sys, os, time + +sys.path.append(os.getcwd()) +sys.path.append(os.getcwd() + os.path.sep + "python") +import nissy + +# Function used to pause, resume and kill the solver +t0 = time.time() +def poll(): + t = time.time() - t0 + if t < 3: + print("[{}s] pausing for the first 3 seconds".format(t)) + return nissy.status_pause + if t < 6: + print("[{}s] Letting the solver run for 3 seconds".format(t)) + return nissy.status_run + print("[{}s] Too much time has passed, giving up".format(t)) + return nissy.status_stop + +solver = "h48h0k4" +datapath = "tables" + os.path.sep + solver +if os.path.exists(datapath): + data = bytearray(open(datapath, "rb").read()) +else: + data = nissy.gendata("h48h0k4") + print("Generated data will NOT be persisted") + +# Scramble is 19 moves optimal, should take a while with the h48h0k4 solver +scramble = "R' U' F D R F2 D L F D2 F2 L' U R' L2 D' R2 F2 R2 D L2 U2 R' U' F" +cube = nissy.applymoves(nissy.solved_cube, scramble); + +nissy.solve(cube, solver, nissy.nissflag_normal, 0, 9, 3, 20, 4, data, poll) diff --git a/python/examples/solve.py b/python/examples/solve.py index 75e7914..c2c992b 100644 --- a/python/examples/solve.py +++ b/python/examples/solve.py @@ -3,12 +3,10 @@ # Run "./build.sh python", then run this from either the top-level directory # of the nissy-core repo or from the python subdirectory. -# Append the directories to the python path so we can load the module +# Append the directories to the python path and import import sys, os sys.path.append(os.getcwd()) sys.path.append(os.getcwd() + os.path.sep + "python") - -# Import with a nicer name import nissy # Choose the solver you prefer @@ -26,7 +24,7 @@ else: cube = nissy.applymoves(nissy.solved_cube, "U F R2"); # Solve! -solutions = nissy.solve(cube, solver, nissy.nissflag_normal, 0, 9, 3, 20, 4, data) +solutions = nissy.solve(cube, solver, nissy.nissflag_normal, 0, 9, 3, 20, 4, data, None) # Print the solutions, one per line print("Found ", len(solutions), " solutions:") -- cgit v1.3