Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🐍 Python API

import ramparils

The Python extension provides a single function, specialize, that runs FocusedILS from an initial strategy and returns the best configuration found within the time budget. It is a native Rust extension built with PyO3, so there is no subprocess overhead β€” the ILS loop, parallel evaluation, and SQLite cache all run in-process. All tuning options are passed as fields in the scenario dict, matching the YAML keys documented in the CLI reference.

🐏 specialize

ramparils.specialize(strategy, scenario) -> dict[str, str]

Specialize a strategy on a set of benchmark instances using ILS.

Runs Iterated Local Search starting from strategy, evaluating (configuration, instance) pairs in parallel, and returns the best configuration found within the time budget. Results may be cached in SQLite when cache_db names a file.

Cache entries are keyed only by the active configuration and instance path. Use a separate cache when the algorithm command, cutoff, objective, solver version, wrapper behavior, or random seed changes. Reusing an incompatible cache can silently return stale results.

πŸ“₯ Arguments

strategy β€” dict[str, str]

Initial parameter configuration as {name: value} strings. Must contain every parameter defined in scenario["paramfile"]. Values are strings even for numeric parameters (e.g. "1.189").

scenario β€” dict

Tuning scenario. All keys match the YAML fields in the CLI reference.

Required keys:

KeyTypeDescription
algostrCommand to invoke the target algorithm. Invoked as <algo> <instance> <cutoff_time> -p1 v1 …
paramfilestrPath to the .params file describing the parameter space.
cutoff_timefloatPer-run time limit in seconds, passed to the target algorithm.
tuner_timeoutfloatTotal wall-clock budget for the tuner in seconds.

At least one of the following must be supplied to specify instances:

KeyTypeDescription
instanceslist[str]List of instance paths directly.
instance_filestrPath to a text file with one instance path per line.

If both are present, instances takes precedence. Relative paths are resolved from the Python process’s current working directory, not from the parameter or instance-list file.

Optional keys (all have defaults matching the CLI):

KeyDefaultDescription
run_obj"runtime"Numeric value to minimise: "runtime" or "quality". Convert maximisation objectives to costs in the wrapper.
overall_obj"mean""mean" or "median".
approach"focused""focused", "basic", or "random"; currently random follows the same full-fidelity ILS path as basic.
perturbation_strength4Neighbourhood steps per perturbation.
initial_fidelity1Initial instances per configuration in FocusedILS, capped by the instance count.
fidelity_step1Instances added at each FocusedILS fidelity increase.
bound_multiplier10.0Adaptive capping multiplier.
pruningTrueEnable adaptive capping.
iterative_deepeningFalseEnable iterative deepening.
lambda_n0.5Iterative deepening instance-count factor.
lambda_c0.5Iterative deepening cutoff-time factor.
lambda_t0.5Iterative deepening timeout factor.
cores0Parallel workers; 0 uses all available cores.
num_run0Run index / random seed (reserved).
cache_db":memory:"Path to the SQLite cache. Defaults to in-memory (not persisted). Set to a file path to share results across calls.
debugFalsePrint new incumbents and scores to stderr.
debug_wrapperFalsePrint every solver invocation.
debug_solverFalsePrint every solver result.
debug_logNoneWrite debug output to this file.
error_logNoneWrite failed solver runs to this file.
test_instance_fileNoneReserved for future use.

FocusedILS uses the first N entries from instances or instance_file while fidelity grows. Put a representative ordering in the list; RamParILS does not shuffle it automatically.

πŸ“€ Returns

dict[str, str] β€” The best configuration found. Only active parameters are included (inactive conditional parameters are omitted).

⚠️ Raises

RuntimeError β€” If the scenario is invalid, the instance list is empty, the paramfile cannot be parsed, or the cache cannot be opened.

πŸ§ͺ Example

import ramparils

result = ramparils.specialize(
    strategy={
        "alpha": "1.189",
        "rho":   "0.5",
        "ps":    "0.1",
        "wp":    "0.03",
    },
    scenario={
        # Required
        "algo":          "ruby /path/to/saps_wrapper.rb",
        "paramfile":     "/path/to/saps.params",
        "instances":     [
            "/path/to/instances/inst1.cnf",
            "/path/to/instances/inst2.cnf",
        ],
        "cutoff_time":   5.0,
        "tuner_timeout": 120.0,
        # Optional β€” defaults shown
        "cache_db":      "/tmp/ramparils_cache.db",
        "cores":         0,       # 0 = all available
        "approach":      "focused",
        "debug":         False,
    },
)

print("Best config found:")
for k, v in sorted(result.items()):
    print(f"  {k} = {v}")

See examples/saps_python.py for a runnable example.