From 59f7b4a086f78080bea08471aaf9cd66c76dde95 Mon Sep 17 00:00:00 2001 From: "Pablo R. Mier" Date: Tue, 22 Mar 2022 13:02:15 +0100 Subject: [PATCH] Improve python-mip performance building models --- miom/miom.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/miom/miom.py b/miom/miom.py index 456ca0a..ec79b99 100644 --- a/miom/miom.py +++ b/miom/miom.py @@ -1175,10 +1175,11 @@ def _add_constraint(self, constraint, **kwargs): def _steady_state(self, **kwargs): V = [self.problem.add_var(lb=rxn['lb'], ub=rxn['ub'], name=f"V_{i}") for i, rxn in enumerate(self.network.R)] - # (Python-MIP does not allow matrix operations like CyLP or CXVOPT) for i in range(self.network.S.shape[0]): - self.problem += mip.xsum(self.network.S[i, j] * V[j] - for j in range(self.network.R.shape[0]) if self.network.S[i, j] != 0) == 0 + non_zero_cols = np.flatnonzero(self.network.S[i, :] != 0) + if len(non_zero_cols) > 0: + self.problem += mip.xsum(self.network.S[i, j] * V[j] + for j in non_zero_cols) == 0 self.variables._flux_vars = V return True