From 54e09e46443f9b94b57c42930b711bf1175a77ae Mon Sep 17 00:00:00 2001 From: ulfw Date: Mon, 29 Apr 2024 14:59:05 +0200 Subject: [PATCH 1/2] add support for integer/binary vars for MOSEK --- linopy/io.py | 13 ++++++++++++- linopy/solvers.py | 3 +-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/linopy/io.py b/linopy/io.py index d7b9454c..2a65894d 100644 --- a/linopy/io.py +++ b/linopy/io.py @@ -364,6 +364,17 @@ def to_mosekpy(model, task=None): bux = [b if b < np.inf else 0.0 for b in M.ub] task.putvarboundslice(0, model.nvars, bkx, blx, bux) + if len(model.binaries.labels) + len(model.integers.labels) > 0: + j = [i for (i, v) in enumerate(M.vtypes) if v in ["B", "I"]] + task.putvartypelist(j, [mosek.variabletype.type_int] * len(j)) + if len(model.binaries.labels) > 0: + task.putvarboundlistconst( + [i for (i, v) in enumerate(M.vtypes) if v == "B"], + mosek.boundkey.ra, + 0.0, + 1.0, + ) + ## Constraints if len(model.constraints) > 0: @@ -396,7 +407,7 @@ def to_mosekpy(model, task=None): if model.is_quadratic: Q = (0.5 * tril(M.Q + M.Q.transpose())).tocoo() task.putqobj(Q.row, Q.col, Q.data) - task.putclist(np.arange(model.nvars), M.c) + task.putclist(list(np.arange(model.nvars)), M.c) if model.objective.sense == "max": task.putobjsense(mosek.objsense.maximize) diff --git a/linopy/solvers.py b/linopy/solvers.py index e5aa00c0..87fff110 100644 --- a/linopy/solvers.py +++ b/linopy/solvers.py @@ -892,9 +892,8 @@ def run_mosek( "Keyword argument `io_api` has to be one of `lp`, `mps` or None" ) - problem_fn = model.to_file(problem_fn) - if io_api != "direct" and io_api is not None: + problem_fn = model.to_file(problem_fn) problem_fn = maybe_convert_path(problem_fn) log_fn = maybe_convert_path(log_fn) warmstart_fn = maybe_convert_path(warmstart_fn) From ac58deffa0e9fd615eb66553b44e8c8ff6404cc4 Mon Sep 17 00:00:00 2001 From: Fabian Date: Fri, 3 May 2024 15:13:22 +0200 Subject: [PATCH 2/2] io: apply stylistic change --- linopy/io.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/linopy/io.py b/linopy/io.py index 51d1d2f6..a6c73dfa 100644 --- a/linopy/io.py +++ b/linopy/io.py @@ -365,15 +365,11 @@ def to_mosek(model, task=None): task.putvarboundslice(0, model.nvars, bkx, blx, bux) if len(model.binaries.labels) + len(model.integers.labels) > 0: - j = [i for (i, v) in enumerate(M.vtypes) if v in ["B", "I"]] - task.putvartypelist(j, [mosek.variabletype.type_int] * len(j)) + idx = [i for (i, v) in enumerate(M.vtypes) if v in ["B", "I"]] + task.putvartypelist(idx, [mosek.variabletype.type_int] * len(idx)) if len(model.binaries.labels) > 0: - task.putvarboundlistconst( - [i for (i, v) in enumerate(M.vtypes) if v == "B"], - mosek.boundkey.ra, - 0.0, - 1.0, - ) + bidx = [i for (i, v) in enumerate(M.vtypes) if v == "B"] + task.putvarboundlistconst(bidx, mosek.boundkey.ra, 0.0, 1.0) ## Constraints