Skip to content

Commit

Permalink
Merge pull request #23 from MetExplore/development
Browse files Browse the repository at this point in the history
Prepare v0.9.0-beta.6
  • Loading branch information
pablormier authored Aug 22, 2021
2 parents c8643c9 + aa6a412 commit 9eee113
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
<div align="center">

[![Try It Online](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1JAOEHLlRCW8GziIpBqkFwJL2ha3OEOWJ?usp=sharing)
[![PyPI](https://img.shields.io/pypi/v/miom?style=flat-square&label=PyPI&logo=pypi&logoColor=white)](https://pypi.org/project/miom/)
[![PyPI](https://img.shields.io/pypi/v/miom?label=PyPI&logo=pypi&logoColor=lightgrey)](https://pypi.org/project/miom/)
![Tests](https://github.com/metexplore/miom/actions/workflows/unit-tests.yml/badge.svg)
[![Downloads](https://pepy.tech/badge/miom)](https://pepy.tech/project/miom)

</div>

Expand Down
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<a href="https://metexplore.github.io/miom"><img src="assets/img/miom_v1_fit.png" alt="MIOM" title="MIOM: Mixed Integer Optimization for Metabolism"/></a>

[![Try It Online](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1JAOEHLlRCW8GziIpBqkFwJL2ha3OEOWJ?usp=sharing)
[![PyPI](https://img.shields.io/pypi/v/miom?style=flat-square&label=PyPI&logo=pypi&logoColor=white)](https://pypi.org/project/miom/)
[![PyPI](https://img.shields.io/pypi/v/miom?label=PyPI)](https://pypi.org/project/miom/)
![Tests](https://github.com/metexplore/miom/actions/workflows/unit-tests.yml/badge.svg)
[![Downloads](https://pepy.tech/badge/miom)](https://pepy.tech/project/miom)

# Introduction

Expand Down
2 changes: 1 addition & 1 deletion miom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
from miom.mio import load_gem

__all__ = ["load", "load_gem", "Solvers", "Comparator", "ExtractionMode"]
__version__ = "0.9.0-beta.5"
__version__ = "0.9.0-beta.6"
59 changes: 44 additions & 15 deletions miom/miom.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,25 @@ class Comparator(str, Enum):
("type", _ReactionType)
])

def _get_reversible_vars(weighted_rxn):
def _get_reversible_vars(weighted_rxns):
d = defaultdict(list)
for i, rxn in enumerate(weighted_rxn):
for i, rxn in enumerate(weighted_rxns):
if rxn.type == _ReactionType.RH_POS or rxn.type == _ReactionType.RH_NEG:
d[rxn.index].append(i)
return [l for l in d.values() if len(l) > 1]


def _get_rxn_var_data(weighted_rxns, rxn_type):
idx = [
(i, rd.index, rd.lb, rd.ub)
for i, rd in enumerate(weighted_rxns)
if rd.type is rxn_type
]
if len(idx) > 0:
return [np.array(x) for x in list(zip(*idx))]
return None


def load(network, solver=Solvers.COIN_OR_CBC):
"""
Create a MIOM optimization model for a given solver.
Expand Down Expand Up @@ -857,20 +868,38 @@ def _subset_selection(self, *args, **kwargs):
P = self.problem
# Build the MIP problem
V = self.variables.fluxvars
X = pc.BinaryVariable("X", shape=(len(weighted_rxns), 1))
X = pc.BinaryVariable("X", shape=(len(weighted_rxns),))
C = pc.Constant("C", value=np.array([abs(rxn.cost) for rxn in weighted_rxns]))
for i, rd in enumerate(weighted_rxns):
if rd.type is _ReactionType.RH_POS:
P.add_constraint(V[rd.index] >= X[i] * (eps - rd.lb) + rd.lb)
elif rd.type is _ReactionType.RH_NEG:
P.add_constraint(V[rd.index] <= rd.ub - X[i] * (rd.ub + eps))
elif rd.type is _ReactionType.RL:
P.add_constraint((1 - X[i]) * rd.ub - V[rd.index] >= 0)
P.add_constraint((1 - X[i]) * rd.lb - V[rd.index] <= 0)
else:
raise ValueError("Unknown type of reaction")
for i, j in _get_reversible_vars(weighted_rxns):
self.add_constraint(X[i] + X[j] <= 1)
# NOTE: For some reason, PICOS have issues parsing the priority operators, so constraints
# are split into many parts, otherwise it does not work. The matrix notation is more
# efficient and takes less time and memory than using the add_list_of_constraints method in a loop.
var_data = _get_rxn_var_data(weighted_rxns, _ReactionType.RH_POS)
if var_data is not None:
I, J, LB, UB = var_data
EA = eps - LB
EB = X[I]^EA
expr = V[J] >= EB + LB
P.add_constraint(expr)
var_data = _get_rxn_var_data(weighted_rxns, _ReactionType.RH_NEG)
if var_data is not None:
I, J, LB, UB = var_data
EA = eps + UB
EB = X[I]^EA
expr = V[J] <= UB - EB
P.add_constraint(expr)
var_data = _get_rxn_var_data(weighted_rxns, _ReactionType.RL)
if var_data is not None:
I, J, LB, UB = var_data
EA = 1 - X[I]
EB = EA^UB
EC = EA^LB
P.add_constraint(EB - V[J] >= 0)
P.add_constraint(EC - V[J] <= 0)
# Strengthen the formulation
idx = [np.array(x) for x in list(zip(*_get_reversible_vars(weighted_rxns)))]
if len(idx) > 0:
I, J = idx
P.add_constraint(X[I] + X[J] <= 1)
P.set_objective(kwargs['direction'], C.T * X)
self.variables._indicator_vars = X
return True
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "miom"
version = "0.9.0-beta.5"
version = "0.9.0-beta.6"
description = "Mixed Integer Optimization for Metabolism"
authors = ["Pablo R. Mier <[email protected]>"]
keywords = ["optimization", "LP", "MIP", "metabolism", "metabolic-networks"]
Expand Down

0 comments on commit 9eee113

Please sign in to comment.