Skip to content

Commit

Permalink
sanitize infinity values: fix multiindex assignment (#371)
Browse files Browse the repository at this point in the history
* constraints: fix multiindex assignment

* remove duplication
  • Loading branch information
FabianHofmann authored Oct 31, 2024
1 parent 3d0275d commit 2fadaf7
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions linopy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,33 +864,32 @@ def sanitize_zeros(self) -> None:
"""
for name in self:
not_zero = abs(self[name].coeffs) > 1e-10
constraint = self[name]
constraint.vars = self[name].vars.where(not_zero, -1)
constraint.coeffs = self[name].coeffs.where(not_zero)
con = self[name]
con.vars = self[name].vars.where(not_zero, -1)
con.coeffs = self[name].coeffs.where(not_zero)

def sanitize_missings(self) -> None:
"""
Set constraints labels to -1 where all variables in the lhs are
missing.
"""
for name in self:
contains_non_missing = (self[name].vars != -1).any(self[name].term_dim)
self[name].data["labels"] = self[name].labels.where(
contains_non_missing, -1
)
con = self[name]
contains_non_missing = (con.vars != -1).any(con.term_dim)
labels = self[name].labels.where(contains_non_missing, -1)
con._data = assign_multiindex_safe(con.data, labels=labels)

def sanitize_infinities(self) -> None:
"""
Replace infinite values in the constraints with a large value.
"""
for name in self:
constraint = self[name]
valid_infinity_values = (
(constraint.sign == LESS_EQUAL) & (constraint.rhs == np.inf)
) | ((constraint.sign == GREATER_EQUAL) & (constraint.rhs == -np.inf))
self[name].data["labels"] = self[name].labels.where(
~valid_infinity_values, -1
con = self[name]
valid_infinity_values = ((con.sign == LESS_EQUAL) & (con.rhs == np.inf)) | (
(con.sign == GREATER_EQUAL) & (con.rhs == -np.inf)
)
labels = con.labels.where(~valid_infinity_values, -1)
con._data = assign_multiindex_safe(con.data, labels=labels)

def get_name_by_label(self, label: Union[int, float]) -> str:
"""
Expand Down

0 comments on commit 2fadaf7

Please sign in to comment.