diff --git a/linopy/model.py b/linopy/model.py index edeea72f..f6aad0ac 100644 --- a/linopy/model.py +++ b/linopy/model.py @@ -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): """ diff --git a/test/test_model.py b/test/test_model.py index 1ba6e772..ac5f3fbc 100644 --- a/test/test_model.py +++ b/test/test_model.py @@ -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()