swch_optimiser
A Python wrapper around MiniZinc for solving system reconfiguration problems: given the current mapping of pods (microservice instances) to nodes plus an application-specific rule set, SwchOptimiser finds an optimal next mapping (including scaling nodes/pods up or down) and turns the result into a list of concrete actions (create_ms, destroy_ms, create_node, destroy_node).
Application-specific constants, parameters, constraints, and the optimisation goal are written in MiniZinc and passed in as a self-contained rule string, which must also declare the shared system parameters (sys_*) that every rule set builds on — see Usage.
Requirements
- Python 3.12+
- A working MiniZinc installation with the Gecode solver available on your system (
minizinc --solversshould listGecode)
Installation
To use the published package in your own project, add it to your requirements.txt:
swchoptimiser==0.1.0
then pip install -r requirements.txt. Note that the PyPI distribution name (swchoptimiser) is not the same as the Python import name (swch_optimiser, with an underscore) — you install one, but import the other:
from swch_optimiser import SwchOptimiser
pip also pulls in the minizinc Python package automatically as a dependency — you don't need to list it separately. It does not install MiniZinc/Gecode themselves; see Requirements above.
To work on this repository itself, it uses Poetry:
poetry install
Usage
Every rule string you pass in must be self-contained: SwchOptimiser does not inject any system parameters for you, so the rule needs to declare the reserved sys_* inputs/outputs itself, in addition to your application-specific constants, parameters, constraints, and optimisation goal.
from swch_optimiser import SwchOptimiser
rule = r"""
% --- SYSTEM PARAMETERS: required by every rule ---
int: sys_pod_count_max;
int: sys_node_count_max;
int: sys_node_count_actual;
array[1..sys_pod_count_max] of int: sys_mapping_actual;
var 1..sys_node_count_max: sys_node_count_next :: add_to_output;
array[1..sys_pod_count_max] of var 0..sys_node_count_max: sys_mapping_next :: add_to_output;
var 0..sys_pod_count_max: sys_pod_count_next = sum(p in 1..sys_pod_count_max)(sys_mapping_next[p] > 0);
int: sys_pod_count_actual = sum(p in 1..sys_pod_count_max)(sys_mapping_actual[p] > 0);
% --- APPLICATION CONSTANTS ---
float: threshold_min_node_load;
float: threshold_max_node_load;
% --- APPLICATION PARAMETERS ---
array[1..sys_node_count_actual] of float: node_load;
% keep node count fixed unless your rule explicitly scales it
constraint sys_node_count_next = sys_node_count_actual;
constraint sys_pod_count_next >= 1;
% ... additional constraints ...
var int: loss = 100 * sum(p in 1..sys_pod_count_max)(bool2int(sys_mapping_next[p] != sys_mapping_actual[p]));
solve minimize loss;
"""
optimiser = SwchOptimiser(rule, mslist=["my-service"])
if optimiser.get_error() is not None:
raise SystemExit(optimiser.get_error())
optimiser.add_input_system({
"sys_pod_count_max": 14,
"sys_node_count_max": 10,
"sys_node_count_actual": 3,
"sys_mapping_actual": [1, 1, 2, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0],
})
optimiser.add_input_constants({
"threshold_min_node_load": 40.0,
"threshold_max_node_load": 70.0,
})
optimiser.add_input_metrics({
"node_load": [30.0, 30.0, 30.0],
})
optimiser.validate_inputs()
result = optimiser.solve(time_limit_milliseconds=None)
if result is not None and result.solution is not None:
for action in optimiser.generate_actions():
print(action)
More complete, runnable examples for different use cases are in examples/:
demo_nodescaling.pydemo_fuelics_wastewater.pydemo_innorenew_sound_processing.pydemo_ust_forest_dt.py
Run any of them with:
poetry run python examples/demo_nodescaling.py
API reference
The public API is a single class, SwchOptimiser, exported from swch_optimiser.
Constructor
| Call | Description |
|---|---|
SwchOptimiser(rule: str, mslist: list = None) |
Builds the MiniZinc model from your self-contained rule string (see Usage — it must declare the sys_* system parameters itself) and instantiates it against the Gecode solver. Any MiniZinc build error is captured, not raised — check get_error() afterward. |
Input setup
| Call | Description |
|---|---|
add_input_system(system: dict) |
Sets the sys_* values (pod/node limits, actual mapping, etc.). |
add_input_constants(constants: dict) |
Sets app-specific constants (e.g. thresholds). |
add_input_metrics(metrics: dict) |
Sets app-specific live metrics (e.g. load, temperature). |
validate_inputs() |
Merges system + constants + metrics into all_inputs. Must be called before solve() — nothing validates parameters against the rule yet. |
Solving
| Call | Description |
|---|---|
solve(time_limit_milliseconds=None, verbose=False) |
Assigns all_inputs onto the MiniZinc instance and solves. Returns the minizinc.Result (or None on error — check get_error()). Records elapsed time. |
time_taken() |
Milliseconds the last solve() call took. |
get_error() |
Returns the formatted error string from construction or solving, or None. |
Reading back results / actions
| Call | Description |
|---|---|
generate_actions() |
Diffs actual vs. next mapping (and node count) into a list of action dicts: create_ms, destroy_ms, create_node, destroy_node. Handles both single-microservice (sys_mapping_actual/sys_mapping_next) and multi-microservice (sys_mapping_actual_<name>/sys_mapping_next_<name>) rule patterns. |
generate_actions_for_one_ms(ms="undefined_ms", mi="sys_mapping_actual", mf="sys_mapping_next") |
Lower-level helper generate_actions() calls per microservice; usable directly if you want to diff a specific mapping pair yourself. |
dump_actions(actions: list) |
Pretty-prints an actions list. |
Introspection / debugging helpers
| Call | Description |
|---|---|
query_system_inputs() |
Dict of declared MiniZinc params matching the reserved sys_* input names. |
query_appspec_inputs() |
Dict of declared MiniZinc params that are not system params (i.e. your app's constants/metrics). |
query_outputs() |
Dict of declared MiniZinc output params matching reserved sys_* output names. |
get_system_input_parameter_values() |
Actual values you supplied for system inputs. |
get_appspec_input_parameter_values() |
Actual values you supplied for app-specific inputs. |
get_system_output_parameter_values() |
Solved values for system outputs (reads reconfig_result.solution). |
dump_extracted_parameters() |
Prints system inputs, system outputs, and appspec inputs as declared in the model (pre-solve). |
dump_system_input_parameter_values() / dump_appspec_input_parameter_values() / dump_system_output_parameter_values() |
Print counterparts of the get_* methods above. |
License
MIT — see LICENSE.
Metadata
Release files for swchoptimiser 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| swchoptimiser-0.2.0.tar.gz | 7.8 kB | Without provenance | Details for swchoptimiser-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| swchoptimiser-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for swchoptimiser-0.2.0-py3-none-any.whl |
Total release size: 16.4 kB
Release files / swchoptimiser-0.2.0.tar.gz
| Download URL | swchoptimiser-0.2.0.tar.gz |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
23dc79c20f095b3b9da0c45d91a4b02f56af4cc630cfaaef7bbdc4bd2e509676
|
|
BLAKE2b-256 checksum How to use checksums |
f66dd754707ae54fc98090bc1fb25a621e319a371cbf5c1feb5c40ca24a466ad
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.3 Linux/6.18.54.1-microsoft-standard-WSL2
|
Release files / swchoptimiser-0.2.0-py3-none-any.whl
| Download URL | swchoptimiser-0.2.0-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fbb800d66ebb4c42edc39ff90cf342f9e03f4e0121a764df39e52b3de9546b4a
|
|
BLAKE2b-256 checksum How to use checksums |
97bfa1af933be917b42d1153590fc2e61b286a1fcd48f0e408fe5a1932a7a8dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.12.3 Linux/6.18.54.1-microsoft-standard-WSL2
|
Security for swchoptimiser 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.