linopy.model.Model.solve

Contents

linopy.model.Model.solve#

Model.solve(solver_name=None, io_api=None, explicit_coordinate_names=False, problem_fn=None, solution_fn=None, log_fn=None, basis_fn=None, warmstart_fn=None, keep_files=False, env=None, sanitize_zeros=True, sanitize_infinities=True, slice_size=2000000, remote=None, progress=None, mock_solve=False, reformulate_sos=False, **solver_options)#

Solve the model with possibly different solvers.

The optimal values of the variables are stored in model.solution. The optimal dual variables are stored in model.dual.

Parameters:
  • solver_name (str, optional) – Name of the solver to use, this must be in linopy.available_solvers. Default to the first entry in linopy.available_solvers.

  • io_api (str, optional) – Api to use for communicating with the solver, must be one of {‘lp’, ‘mps’, ‘direct’}. If set to ‘lp’/’mps’ the problem is written to an LP/MPS file which is then read by the solver. If set to ‘direct’ the problem is communicated to the solver via the solver specific API, e.g. gurobipy. This may lead to faster run times. The default is set to ‘lp’ if available.

  • explicit_coordinate_names (bool, optional) – If the Api to use for communicating with the solver is based on ‘lp’, this option allows to keep the variable and constraint names in the lp file. This may lead to slower run times. The default is set to False.

  • problem_fn (path_like, optional) – Path of the lp file or output file/directory which is written out during the process. The default None results in a temporary file.

  • solution_fn (path_like, optional) – Path of the solution file which is written out during the process. The default None results in a temporary file.

  • log_fn (path_like, optional) – Path of the logging file which is written out during the process. The default None results in the no log file, hence all solver outputs are piped to the python repl.

  • basis_fn (path_like, optional) – Path of the basis file of the solution which is written after solving. The default None results in a temporary file, if the solver/method supports writing out a basis file.

  • warmstart_fn (path_like, optional) – Path of the basis file which should be used to warmstart the solving. The default is None.

  • keep_files (bool, optional) – Whether to keep all temporary files like lp file, solution file. This argument is ignored for the logger file log_fn. The default is False.

  • env (gurobi.Env, optional) – Existing environment passed to the solver (e.g. gurobipy.Env). Currently only in use for Gurobi. The default is None.

  • sanitize_zeros (bool, optional) – Whether to set terms with zero coefficient as missing. This will remove unneeded overhead in the lp file writing. The default is True.

  • sanitize_infinities (bool, optional) – Whether to filter out constraints that are subject to <= inf or >= -inf.

  • slice_size (int, optional) – Size of the slice to use for writing the lp file. The slice size is used to split large variables and constraints into smaller chunks to avoid memory issues. The default is 2_000_000.

  • remote (linopy.remote.RemoteHandler | linopy.oetc.OetcHandler, optional) – Remote handler to use for solving model on a server. Note that when solving on a rSee linopy.remote.RemoteHandler for more details.

  • progress (bool, optional) – Whether to show a progress bar of writing the lp file. The default is None, which means that the progress bar is shown if the model has more than 10000 variables and constraints.

  • mock_solve (bool, optional) – Whether to run a mock solve. This will skip the actual solving. Variables will be set to have dummy values

  • reformulate_sos (bool | Literal[``”auto”``], optional) – Whether to automatically reformulate SOS constraints as binary + linear constraints for solvers that don’t support them natively. If True, always reformulates (warns if solver supports SOS natively). If “auto”, silently reformulates only when the solver lacks SOS support. If False, raises if solver doesn’t support SOS. This uses the Big-M method and requires all SOS variables to have finite bounds. Default is False.

  • **solver_options (kwargs) – Options passed to the solver.

Returns:

status (tuple) – Tuple containing the status and termination condition of the optimization process.