From 4d9e871f27930d974533b585e9a2a76722634b92 Mon Sep 17 00:00:00 2001 From: Irieo Date: Thu, 7 Dec 2023 16:22:05 +0100 Subject: [PATCH 1/6] allow removing multiple constraints at once --- linopy/model.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/linopy/model.py b/linopy/model.py index eace2cd5..673efcca 100644 --- a/linopy/model.py +++ b/linopy/model.py @@ -651,19 +651,28 @@ 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. """ + + if isinstance(name, list): + for n in name: + logger.info(f"Removed constraint: {name}") + self.remove_constraints(n) + return + + logger.info(f"Removed constraint: {name}") self.constraints.remove(name) @property From a6245fe5e740ee1197f586faddb01b1a0b16d62e Mon Sep 17 00:00:00 2001 From: Irieo Date: Fri, 8 Dec 2023 13:02:48 +0100 Subject: [PATCH 2/6] add test for remove_constraints() to include list case --- test/test_model.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test_model.py b/test/test_model.py index ab3ee3b9..efdcb31e 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_assert_model_equal(): m = Model() From 36a393f6b48f0de79592b708006fca4cd6d643a5 Mon Sep 17 00:00:00 2001 From: Irieo Date: Fri, 8 Dec 2023 13:18:45 +0100 Subject: [PATCH 3/6] avoid additional call in remove_constraint() --- linopy/model.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/linopy/model.py b/linopy/model.py index 673efcca..4b9a84a9 100644 --- a/linopy/model.py +++ b/linopy/model.py @@ -665,15 +665,13 @@ def remove_constraints(self, name): ------- None. """ - if isinstance(name, list): for n in name: - logger.info(f"Removed constraint: {name}") - self.remove_constraints(n) - return - - logger.info(f"Removed constraint: {name}") - self.constraints.remove(name) + logger.info(f"Removed constraint: {n}") + self.constraints.remove(n) + else: + logger.info(f"Removed constraint: {name}") + self.constraints.remove(name) @property def continuous(self): From 62bb5ce0d4583b1ee48b7da79a6c0c05ed86c3bd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 8 Dec 2023 12:42:19 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- test/test_model.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/test_model.py b/test/test_model.py index 840c0d51..ac5f3fbc 100644 --- a/test/test_model.py +++ b/test/test_model.py @@ -126,6 +126,7 @@ def test_remove_constraints_with_list(): assert "constraint_y" not in m.constraints.labels assert not len(m.constraints.labels) + def test_remove_objective(): m = Model() @@ -138,6 +139,7 @@ def test_remove_objective(): m.remove_objective() assert not len(m.objective.vars) + def test_assert_model_equal(): m = Model() From 2b5949133dfd037c2ff2708767c8a77ac9691a06 Mon Sep 17 00:00:00 2001 From: Fabian Hofmann Date: Fri, 15 Dec 2023 15:34:14 +0100 Subject: [PATCH 5/6] Update linopy/model.py --- linopy/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linopy/model.py b/linopy/model.py index ca41f9e6..9862a446 100644 --- a/linopy/model.py +++ b/linopy/model.py @@ -667,7 +667,7 @@ def remove_constraints(self, name): """ if isinstance(name, list): for n in name: - logger.info(f"Removed constraint: {n}") + logger.debug(f"Removed constraint: {n}") self.constraints.remove(n) else: logger.info(f"Removed constraint: {name}") From 148a5458da1fc150da7119ef5f75379c8c1ca545 Mon Sep 17 00:00:00 2001 From: Fabian Hofmann Date: Fri, 15 Dec 2023 15:34:19 +0100 Subject: [PATCH 6/6] Update linopy/model.py --- linopy/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linopy/model.py b/linopy/model.py index 9862a446..f6aad0ac 100644 --- a/linopy/model.py +++ b/linopy/model.py @@ -670,7 +670,7 @@ def remove_constraints(self, name): logger.debug(f"Removed constraint: {n}") self.constraints.remove(n) else: - logger.info(f"Removed constraint: {name}") + logger.debug(f"Removed constraint: {name}") self.constraints.remove(name) def remove_objective(self):