mapping solver parameters
Closed this issue · 0 comments
hugolarzabal commented
In cornflow client, we now have a mapping of solvers parameters here:
https://github.com/baobabsoluciones/cornflow/blob/master/libs/client/cornflow_client/constants.py
However, we are still missing a function to use this mapping in order to prepare the options to pass to the solver.
In order to have a standard way to do it, I propose to add a method like the following in ExperimentCore:
def get_solver_config(self, config, lib="pyomo", default_solver="cbc"):
"""
Format the configuration to use to solver the problem.
Solver configuration can either be directly in config using cornflow mapping name
or in a config["solver_config"] using the solver names.
Example:
config = {
"solver":"milp.cbc",
"time_limit":60,
"rel_gap":0.1,
"solver_config":{"heur":1, "pumpC":0}
}
:param config: dict config argument of the solver method
:param lib: str library used to create the model (pulp or pyomo)
:param default_solver: str default solver to use if none is present inf config.
:return: the solver name and the config dict.
"""
mapping = PARAMETER_SOLVER_TRANSLATING_MAPPING
solver = config.get("solver", None)
if "." in solver:
solver = solver.split(".")[1]
if solver is None:
message = f"No solver found in config. Default solver {default_solver} will be used"
print(message)
solver = default_solver
conf = {
mapping[k, lib, solver]: v for k, v in config if (k, lib, solver) in mapping
}
conf.update(config.get("solver_config", {}))
return solver, conf