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

Fix numpy arrays on LinearSystem(s) not being properly updated #18

Draft
wants to merge 15 commits into
base: dev
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[ADD] update option (if matrix is already available)
skapur committed Mar 15, 2021
commit 98fb38f7f6896462d4262c91e7923570a1dde98c
36 changes: 19 additions & 17 deletions src/cobamp/core/linear_systems.py
Original file line number Diff line number Diff line change
@@ -328,20 +328,20 @@ def populate_model_from_matrix(self, S, var_types, lb, ub, b_lb, b_ub, var_names
only_nonzero:
indicator_rows:
"""
self.S = sprs.csc_matrix(np.zeros([0,0]))
self.add_variables_to_model(var_names, lb, ub, var_types)
self.add_rows_to_model(S, b_lb, b_ub, only_nonzero, indicator_rows)
self.S = sprs.csc_matrix(S)
self.add_variables_to_model(var_names, lb, ub, var_types, update=False)
self.add_rows_to_model(S, b_lb, b_ub, only_nonzero, indicator_rows, update=False)

def populate_constraints_from_matrix(self, S, constraints, vars, only_nonzero=False):
def populate_constraints_from_matrix(self, S, constraints, vars, only_nonzero=False, update=True):
if S.shape[0] > MAX_DIM_CONST:
runs = np.array_split(np.array(range(S.shape[0])), max(S.shape[0] // MAX_DIM_CONST, 1))
for run in runs:
subs, subc, subv = S[run,:], [constraints[k] for k in run], vars
self._populate_constraints_from_matrix(subs, subc, subv)
self._populate_constraints_from_matrix(subs, subc, subv, update)
else:
self._populate_constraints_from_matrix(S, constraints, vars, only_nonzero)
self._populate_constraints_from_matrix(S, constraints, vars, only_nonzero, update)

def _populate_constraints_from_matrix(self, S, constraints, vars, only_nonzero=False):
def _populate_constraints_from_matrix(self, S, constraints, vars, only_nonzero=False, update=True):
"""
Args:
S: Two-dimensional np.ndarray instance
@@ -368,12 +368,13 @@ def _populate_constraints_from_matrix(self, S, constraints, vars, only_nonzero=F
for container, nmap in zip([vars,constraints],[var_name_map,cns_name_map])]

self.model.update()
if update:
if len(c_ids) > 0 and len(v_ids) > 0:
self.S[c_ids[:,None],v_ids] = S

if len(c_ids) > 0 and len(v_ids) > 0:
self.S[c_ids[:,None],v_ids] = S


def add_rows_to_model(self, S_new, b_lb, b_ub, only_nonzero=False, indicator_rows=None, vars=None, names=None):
def add_rows_to_model(self, S_new, b_lb, b_ub, only_nonzero=False,
indicator_rows=None, vars=None, names=None, update=True):
"""
Args:
S_new:
@@ -402,7 +403,8 @@ def add_rows_to_model(self, S_new, b_lb, b_ub, only_nonzero=False, indicator_row
self.model.add(constraints, sloppy=True)

self.model.update()
self.S = sprs.vstack([self.S, sprs.csc_matrix(np.zeros([S_new.shape[0], len(self.model.variables)]))]).tocsc()
if update:
self.S = sprs.vstack([self.S, sprs.csc_matrix(np.zeros([S_new.shape[0], len(self.model.variables)]))]).tocsc()

self.populate_constraints_from_matrix(S_new, constraints, vars, only_nonzero)

@@ -434,7 +436,7 @@ def remove_from_model(self, index, what):

self.model.update()

def add_columns_to_model(self, S_new, var_names, lb, ub, var_types, only_nonzero=False):
def add_columns_to_model(self, S_new, var_names, lb, ub, var_types, only_nonzero=False, update=True):
"""
Args:
S_new:
@@ -443,11 +445,11 @@ def add_columns_to_model(self, S_new, var_names, lb, ub, var_types, only_nonzero
ub:
var_types:
"""
vars = self.add_variables_to_model(var_names, lb, ub, var_types)
vars = self.add_variables_to_model(var_names, lb, ub, var_types, update=update)

self.populate_constraints_from_matrix(S_new, self.model.constraints, vars, only_nonzero=only_nonzero)

def add_variables_to_model(self, var_names, lb, ub, var_types):
def add_variables_to_model(self, var_names, lb, ub, var_types, update=True):

"""
Args:
@@ -463,8 +465,8 @@ def add_variables_to_model(self, var_names, lb, ub, var_types):
zip(var_names, lb, ub, var_types)]
self.model.add(vars)
self.model.update()

self.S = sprs.hstack([self.S, sprs.csc_matrix(np.zeros([len(self.model.constraints), len(vars)]))])
if update:
self.S = sprs.hstack([self.S, sprs.csc_matrix(np.zeros([len(self.model.constraints), len(vars)]))])

return vars