Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes on the last update #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions reframed/cobra/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def pFBA(model, objective=None, obj_frac=None, minimize=False, constraints=None,
return pre_solution

if obj_frac is None:
solver.add_constraint('obj', objective, '=', pre_solution.fobj, update=False)
solver.add_constraint('obj', objective, '=', pre_solution.fobj)
else:
solver.add_constraint('obj', objective, '>', obj_frac * pre_solution.fobj, update=False)
solver.add_constraint('obj', objective, '>', obj_frac * pre_solution.fobj)

if not reactions:
reactions = model.reactions.keys()
Expand All @@ -82,15 +82,15 @@ def pFBA(model, objective=None, obj_frac=None, minimize=False, constraints=None,
for r_id in reactions:
if model.reactions[r_id].reversible:
pos, neg = r_id + '_p', r_id + '_n'
solver.add_variable(pos, 0, inf, update=False)
solver.add_variable(neg, 0, inf, update=False)
solver.update()
solver.add_variable(pos, 0, inf)
solver.add_variable(neg, 0, inf)
#solver.update()
for r_id in reactions:
if model.reactions[r_id].reversible:
pos, neg = r_id + '_p', r_id + '_n'
solver.add_constraint('c' + pos, {r_id: -1, pos: 1}, '>', 0, update=False)
solver.add_constraint('c' + neg, {r_id: 1, neg: 1}, '>', 0, update=False)
solver.update()
solver.add_constraint('c' + pos, {r_id: -1, pos: 1}, '>', 0)
solver.add_constraint('c' + neg, {r_id: 1, neg: 1}, '>', 0)
#solver.update()

objective = dict()
for r_id in reactions:
Expand All @@ -100,6 +100,8 @@ def pFBA(model, objective=None, obj_frac=None, minimize=False, constraints=None,
objective[neg] = 1
else:
objective[r_id] = 1

solver.update()

solution = solver.solve(objective, minimize=True, constraints=constraints)
solution.pre_solution = pre_solution
Expand Down Expand Up @@ -195,8 +197,8 @@ def CAFBA(model, objective=None, minimize=False, wc=0, we=8.3e-4, wr=0.169, pmax
if carbon_source in model.reactions[r_id].stoichiometry:
if model.reactions[r_id].reversible:
pos, neg = r_id + '+', r_id + '-'
solver.add_variable(pos, 0, inf, update=False)
solver.add_variable(neg, 0, inf, update=False)
solver.add_variable(pos, 0, inf)
solver.add_variable(neg, 0, inf)
uptake.append(r_id + '+')
uptake.append(r_id + '-')
splits[r_id] = pos, neg
Expand All @@ -206,8 +208,8 @@ def CAFBA(model, objective=None, minimize=False, wc=0, we=8.3e-4, wr=0.169, pmax
for r_id in model.get_reactions_by_type(ReactionType.ENZYMATIC):
if model.reactions[r_id].reversible:
pos, neg = r_id + '+', r_id + '-'
solver.add_variable(pos, 0, inf, update=False)
solver.add_variable(neg, 0, inf, update=False)
solver.add_variable(pos, 0, inf)
solver.add_variable(neg, 0, inf)
enzymatic.append(r_id + '+')
enzymatic.append(r_id + '-')
splits[r_id] = pos, neg
Expand All @@ -217,8 +219,8 @@ def CAFBA(model, objective=None, minimize=False, wc=0, we=8.3e-4, wr=0.169, pmax
solver.update()

for r_id, (pos, neg) in splits.items():
solver.add_constraint('c' + pos, {r_id: -1, pos: 1}, '>', 0, update=False)
solver.add_constraint('c' + neg, {r_id: 1, neg: 1}, '>', 0, update=False)
solver.add_constraint('c' + pos, {r_id: -1, pos: 1}, '>', 0)
solver.add_constraint('c' + neg, {r_id: 1, neg: 1}, '>', 0)
solver.update()

main_constr = {}
Expand All @@ -228,10 +230,11 @@ def CAFBA(model, objective=None, minimize=False, wc=0, we=8.3e-4, wr=0.169, pmax
main_constr[r_id] = wc
main_constr[model.biomass_reaction] = wr

solver.add_constraint('alloc', main_constr, '=', pmax, update=True)

solver.add_constraint('alloc', main_constr, '=', pmax)
solver.update()

solution = solver.solve(objective, minimize=minimize, constraints=constraints)

solver.remove_constraint('alloc')

if cleanup:
Expand Down Expand Up @@ -308,13 +311,13 @@ def lMOMA(model, reference=None, constraints=None, reactions=None, solver=None):
solver.lMOMA_flag = True
for r_id in reactions:
d_pos, d_neg = r_id + '_d+', r_id + '_d-'
solver.add_variable(d_pos, 0, inf, update=False)
solver.add_variable(d_neg, 0, inf, update=False)
solver.add_variable(d_pos, 0, inf)
solver.add_variable(d_neg, 0, inf)
solver.update()
for r_id in reactions:
d_pos, d_neg = r_id + '_d+', r_id + '_d-'
solver.add_constraint('c' + d_pos, {r_id: -1, d_pos: 1}, '>', -reference[r_id], update=False)
solver.add_constraint('c' + d_neg, {r_id: 1, d_neg: 1}, '>', reference[r_id], update=False)
solver.add_constraint('c' + d_pos, {r_id: -1, d_pos: 1}, '>', -reference[r_id])
solver.add_constraint('c' + d_neg, {r_id: 1, d_neg: 1}, '>', reference[r_id])
solver.update()

objective = dict()
Expand Down Expand Up @@ -370,7 +373,7 @@ def ROOM(model, reference=None, constraints=None, wt_constraints=None, reactions

for r_id in reactions:
y_i = 'y_' + r_id
solver.add_variable(y_i, 0, 1, vartype=VarType.BINARY, update=False)
solver.add_variable(y_i, 0, 1, vartype=VarType.BINARY)
objective[y_i] = 1
solver.update()

Expand All @@ -384,8 +387,8 @@ def ROOM(model, reference=None, constraints=None, wt_constraints=None, reactions
w_i_max = reference[r_id]
w_u = w_i_max + delta * abs(w_i_max) + epsilon
w_l = w_i_min - delta * abs(w_i_min) - epsilon
solver.add_constraint('c' + r_id + '_u', {r_id: 1, y_i: (w_u - U)}, '<', w_u, update=False)
solver.add_constraint('c' + r_id + '_l', {r_id: 1, y_i: (w_l - L)}, '>', w_l, update=False)
solver.add_constraint('c' + r_id + '_u', {r_id: 1, y_i: (w_u - U)}, '<', w_u)
solver.add_constraint('c' + r_id + '_l', {r_id: 1, y_i: (w_l - L)}, '>', w_l)
solver.update()

if solutions == 1:
Expand Down
11 changes: 10 additions & 1 deletion reframed/solvers/cplex_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def add_variables(self, var_dict):

def add_constraints(self, constr_dict):


constr_ids = list(constr_dict.keys())
lhs_all = [SparsePair(ind=list(lhs.keys()), val=list(lhs.values())) for (lhs, _, _) in constr_dict.values()]
map_sense = {'=': 'E', '<': 'L', '>': 'G'}
Expand All @@ -87,7 +86,17 @@ def add_constraints(self, constr_dict):
self.problem.linear_constraints.add(lin_expr=lhs_all, senses=sense_all, rhs=rhs_all, names=constr_ids)
self.constraints.extend(constr_ids)

def remove_constraint(self, constr_id):
""" Remove a constraint from the current problem.

Arguments:
constr_id (str): constraint identifier
"""
if constr_id in self.constraints:
self.problem.linear_constraints.delete(constr_id)
self.constraints.remove(constr_id)


def set_objective(self, objective, minimize=True):

if isinstance(objective, str):
Expand Down
11 changes: 11 additions & 0 deletions reframed/solvers/gurobi_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ def add_constraints(self, constr_dict):
self.constraints.extend(constr_dict.keys())
self.problem.update()

def remove_constraint(self, constr_id):
""" Remove a constraint from the current problem.

Arguments:
constr_id (str): constraint identifier
"""

if constr_id in self.constraints:
self.problem.remove(self.problem.getConstrByName(constr_id))
self.constraints.remove(constr_id)


def set_objective(self, objective, minimize=True):

Expand Down
19 changes: 17 additions & 2 deletions reframed/solvers/scip_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def __init__(self, model=None):

def add_variables(self, var_dict):

self.problem.freeTransform()
for var_id, (lb, ub, vartype) in var_dict.items():

lb = None if lb == -inf else lb
Expand All @@ -90,14 +91,28 @@ def add_variables(self, var_dict):
self.variables.extend(var_dict.keys())

def add_constraints(self, constr_dict):


self.problem.freeTransform()
for constr_id, (lhs, sense, rhs) in constr_dict.items():
expr = quicksum(self._vars_dict[var_id] * coeff for var_id, coeff in lhs.items())
constr = sense_mapping[sense](expr,rhs)
self._cons_dict[constr_id] = self.problem.addCons(constr, name=constr_id)

self.constraints.extend(constr_dict.keys())


def remove_constraint(self, constr_id):
""" Remove a constraint from the current problem.

Arguments:
constr_id (str): constraint identifier
"""

if constr_id in self.constraints:
self.problem.freeTransform()
self.problem.delCons(self._cons_dict[constr_id])
del self._cons_dict[constr_id]
self.constraints.remove(constr_id)


def set_objective(self, objective, minimize=True):

Expand Down
6 changes: 5 additions & 1 deletion reframed/solvers/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def add_constraint(self, constr_id, lhs, sense='=', rhs=0):

def add_constraints(self, constr_dict):
""" Solver specific implementation """
pass

def remove_constraint(self, constr_id):
""" Solver specific implementation """
pass

def update(self):
""" Update internal structure. Used for efficient lazy updating. """
Expand All @@ -80,7 +85,6 @@ def update(self):
self.add_constraints(self._cached_constrs)
self._cached_constrs = {}


def build_problem(self, model):
""" Create problem structure for a given model.

Expand Down