python examples/runexp/main.py config.json results/ --jobs 4- Distributed as
- a file in examples/, to copy
- Last updated
- 7 Oct 2026 · 1 commit
- Authors
- ThomSerg
- Shows
- using cpbenchy from another experiment framework, through cpbenchy.backend
- Requires
- runexp (github.com/IgnaceBleukx/Run-Experiments)
- Tags
- runexpintegrationbackend
What it does
runexp runs Python experiments from a JSON config. It
unravels the config into one experiment per combination of values, and keeps one result folder per
experiment. CpbenchyRunner is a runexp Runner that has cpbenchy run each experiment as one solver
run, with cpbenchy’s limits and measurements. The stats go into that experiment’s folder, as runexp
expects.
So you keep runexp’s configs, folders and analysis, and cpbenchy does the measuring. It uses
cpbenchy.backend, the way to use cpbenchy from any experiment framework.
Use it
A config, with lists and ranges that runexp unravels. A glob in instance becomes one experiment per
file:
{
"solver": ["ortools", "exact"],
"instance": "instances/*.opb",
"time_limit": 60,
"seed": {"_from": 1, "_to": 4},
"params": {"num_search_workers": 1}
}python examples/runexp/main.py config.json results/ --jobs 4 --memory-limit 4096Each experiment gets a folder in results/. Running it again only runs the experiments that have no
folder yet.
results/000001/
├── config.json the experiment's config, as runexp writes it
├── status.txt one file per main stat: status, solved, objective, walltime_s, cputime_s, ...
├── stats.json the whole cpbenchy result record
└── solver.log the run's outputLoad them with runexp, as any other results:
from runexp.utils import results_to_df
df = results_to_df("results", fnames=["status.txt", "walltime_s.txt", "solved.txt"])The config
| Key | |
|---|---|
solver |
a CPMpy solver name |
instance |
one instance file; a glob becomes one experiment per file |
time_limit |
seconds |
mem_limit_mib |
optional; else --memory-limit |
params |
optional: arguments for solve() |
seed, cores |
optional |
loader |
optional: a loader reference, as for --loader |
Any key can be a list or a range, as usual in runexp.
Notes
- cpbenchy runs the experiments. There is no experiment function, so pass
func=None. cpbenchy limits and measures each run, in place of runexp’s own memory limit and process pool.--jobsis the number of runs in parallel, each on cores of its own. - In your own runexp script, import
CpbenchyRunnerand use it as any other runner:CpbenchyRunner(func=None, output="results", memory_limit=4096).run_batch(config, parallel=True, num_workers=4). - More stats as files. Add names to
STATSin the file, or plugins to therun(...)call withplugins=[...]. What plugins record ends up instats.json, underextra.
Implementation
The CpbenchyRunner in examples/runexp/cpbenchy_runner.py, lines 25–65 of 65, as of this version of the docs.
class CpbenchyRunner(Runner):
def make_kwargs(self, config): # cpbenchy runs the solver, so there is no experiment function to call
return dict(config)
def run_batch(self, config, parallel=False, num_workers=None, show_progress=True):
"""Measure every unravelled config that has no result yet, `num_workers` at a time if `parallel`."""
configs = self.filter_experiments(unravel_dict(config))
print(f"{len(configs)} experiments to run")
jobs = (num_workers or os.cpu_count() - 1) if parallel else 1
self.measure(configs, jobs)
def run_one(self, config):
self.measure([config], jobs=1)
def measure(self, configs, jobs):
submissions = [Submission(self.spec(config), key=str(i)) for i, config in enumerate(configs)]
# cpbenchy limits and measures each run itself, so runexp's own memory limit and process pool aren't used
run(submissions, jobs=jobs, on_result=lambda item: self.store(configs[int(item.key)], item))
def spec(self, config):
"""What cpbenchy measures for one config."""
default_mem = self.memlimit if self.memlimit > 0 else None
return RunSpec(
Instance.from_path(config["instance"]),
config["solver"],
Limits(time_s=float(config["time_limit"]), mem_mib=config.get("mem_limit_mib", default_mem)),
params=config.get("params", {}),
seed=config.get("seed"),
cores=config.get("cores", 1),
loader=config.get("loader"),
)
def store(self, config, item):
"""Write one run's stats into a new result directory, next to its config."""
record = item.result.to_dict()
artifacts = {name: record[name] for name in STATS if record[name] is not None}
artifacts["solved"] = item.result.solved
artifacts["stats.json"] = json.dumps(record) # runexp writes a key with an extension as it is
if item.log is not None:
artifacts["solver.log"] = item.log
self.save_result(config, artifacts, self.mkdir())