Skip to content

Commit aae885b

Browse files
finozzifadanielelerede-oetpre-commit-ci[bot]
authored
Runner: run_solver.py update with knitro (#410)
The goal of this pull request is add support for the `knitro` solver in the `run_solver.py` script. The interface between `linopy` and the `knitro` Python API is introduced in this [pull request.](PyPSA/linopy#532) ## Changes ### `run_solver.py` I have added a `knitro` specification for the following functions: - `def get_solver` - `def def is_mip_problem` - `def get_reported_runtime` - `def get_duality_gap` ## Test I tested the integration between knitro and linopy and the changes to run_solver.py on the benchmark [pypsa-eur-elec-trex-3-12h.lp](https://storage.googleapis.com/solver-benchmarks/pypsa-eur-elec-trex-3-12h.lp). I get: `{"runtime": 4.476548100999992, "reported_runtime": 3.8160810470581055, "status": "ok", "condition": "optimal", "objective": 7398198432.386027, "duality_gap": null, "max_integrality_violation": null}` The value of the objective function is compatible to those found [here](https://openenergybenchmark.org/dashboard/benchmark-set/pypsa-eur-elec-trex) --------- Co-authored-by: Daniele Lerede <daniele.lerede@openenergytransition.org> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5492470 commit aae885b

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

runner/run_solver.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ def get_solver(solver_name):
7676
"randomseed": 0,
7777
"mip.tolerances.mipgap": mip_gap,
7878
},
79+
"knitro": { # TODO check seed option for knitro
80+
"KN_PARAM_MS_SEED": 1066,
81+
},
7982
}
8083

8184
return solver_class(**seed_options.get(solver_name, {}))
@@ -103,6 +106,9 @@ def is_mip_problem(solver_model, solver_name):
103106
# so MIP problem detection is not possible.
104107
# TODO preprocess benchmarks and add this info to metadata
105108
return False
109+
elif solver_name == "knitro":
110+
# Knitro is not designed for MILP problems
111+
return False
106112
else:
107113
raise NotImplementedError(f"The solver '{solver_name}' is not supported.")
108114

@@ -135,6 +141,9 @@ def get_duality_gap(solver_model, solver_name: str):
135141
return None
136142
elif solver_name == "cplex":
137143
return solver_model.solution.MIP.get_mip_relative_gap()
144+
elif solver_name == "knitro":
145+
# Knitro duality gap retrieval not implemented yet
146+
return None
138147
else:
139148
raise NotImplementedError(f"The solver '{solver_name}' is not supported.")
140149

@@ -180,6 +189,8 @@ def get_reported_runtime(solver_name, solver_model) -> float | None:
180189
return solver_model.Runtime
181190
case "cplex":
182191
return None
192+
case "knitro":
193+
return solver_model.reported_runtime
183194
case _:
184195
print(f"WARNING: cannot obtain reported runtime for {solver_name}")
185196
return None

0 commit comments

Comments
 (0)