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

allow removing multiple constraints at once #211

Merged
merged 7 commits into from
Dec 15, 2023
Merged
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
17 changes: 12 additions & 5 deletions linopy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,20 +651,27 @@ def remove_variables(self, name):

def remove_constraints(self, name):
"""
Remove all constraints stored under reference name `name` from the
Remove all constraints stored under reference name 'name' from the
model.

Parameters
----------
name : str
Reference name of the constraints which to remove, same as used in
`model.add_constraints`.
name : str or list of str
Reference name(s) of the constraints to remove. If a single name is
provided, only that constraint will be removed. If a list of names
is provided, all constraints with those names will be removed.

Returns
-------
None.
"""
self.constraints.remove(name)
if isinstance(name, list):
for n in name:
logger.debug(f"Removed constraint: {n}")
self.constraints.remove(n)
else:
logger.debug(f"Removed constraint: {name}")
self.constraints.remove(name)

def remove_objective(self):
"""
Expand Down
13 changes: 13 additions & 0 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ def test_remove_constraint():
assert not len(m.constraints.labels)


def test_remove_constraints_with_list():
m = Model()

x = m.add_variables()
y = m.add_variables()
m.add_constraints(x, EQUAL, 0, name="constraint_x")
m.add_constraints(y, EQUAL, 0, name="constraint_y")
m.remove_constraints(["constraint_x", "constraint_y"])
assert "constraint_x" not in m.constraints.labels
assert "constraint_y" not in m.constraints.labels
assert not len(m.constraints.labels)


def test_remove_objective():
m = Model()

Expand Down
Loading