Skip to content

Commit

Permalink
ENH: Moved linearization tools from etfl to here
Browse files Browse the repository at this point in the history
  • Loading branch information
psalvy committed Feb 10, 2020
1 parent f2acd48 commit 9de062e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pytfa/optim/constraints.py
Expand Up @@ -349,3 +349,20 @@ def __init__(self, model, expr, id_, **kwargs):
**kwargs)

prefix = 'FP_'


class LinearizationConstraint(ModelConstraint):
"""
Class to represent a variable attached to a reaction
"""
@staticmethod
def from_constraints(cons, model):
return LinearizationConstraint(
name = cons.name,
expr = cons.expr,
model = model,
ub = cons.ub,
lb = cons.lb,
)

prefix = 'LC_'
39 changes: 39 additions & 0 deletions pytfa/optim/reformulation.py
Expand Up @@ -12,6 +12,8 @@
import sympy
# import optlang
from collections import namedtuple
from .variables import LinearizationVariable
from .constraints import LinearizationConstraint

# Faster than optlang Constraint object
ConstraintTuple = namedtuple('ConstraintTuple',['name','expression','ub','lb'])
Expand Down Expand Up @@ -161,5 +163,42 @@ def petersen_linearization(b, x, z = None, M=1000):
return z, [cons1,cons2,cons3]


def linearize_product(model, b, x, queue=False):
"""
:param model:
:param b: the binary variable
:param x: the continuous variable
:param queue: whether to queue the variables and constraints made
:return:
"""

# Linearization step for ga_i * [E]
z_name = '__MUL__'.join([b.name, x.name])
# Add the variables
model_z_u = model.add_variable(kind=LinearizationVariable,
hook=model,
id_=z_name,
lb=0,
ub=x.ub,
queue=False)

big_m = x.ub

z_u, new_constraints = petersen_linearization(b=b, x=x, M=big_m,
z=model_z_u)

# Add the constraints:
for cons in new_constraints:
model.add_constraint(kind=LinearizationConstraint,
hook=model,
id_=cons.name,
expr=cons.expression,
# expr=new_expression,
ub=cons.ub,
lb=cons.lb,
queue=queue)

model._push_queue()
return model_z_u

7 changes: 7 additions & 0 deletions pytfa/optim/variables.py
Expand Up @@ -555,3 +555,10 @@ class NegSlackLC(MetaboliteVariable):

prefix = 'NegSlackLC_'


class LinearizationVariable(ModelVariable):
"""
Class to represent a product A*B when performing linearization of the
model
"""
prefix = 'LZ_'

0 comments on commit 9de062e

Please sign in to comment.