Skip to content

Commit

Permalink
Merge pull request #212 from PyPSA/dev2
Browse files Browse the repository at this point in the history
Allow removing objective expression from the model
  • Loading branch information
FabianHofmann authored Dec 8, 2023
2 parents a97febe + ab97cca commit 930ceae
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions linopy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,16 @@ def remove_constraints(self, name):
"""
self.constraints.remove(name)

def remove_objective(self):
"""
Remove the objective's linear expression from the model.
Returns
-------
None.
"""
self.objective = Objective(LinearExpression(None, self), self)

@property
def continuous(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_objective():
m = Model()

lower = xr.DataArray(np.zeros((2, 2)), coords=[range(2), range(2)])
upper = xr.DataArray(np.ones((2, 2)), coords=[range(2), range(2)])
x = m.add_variables(lower, upper, name="x")
y = m.add_variables(lower, upper, name="y")
obj = (10 * x + 5 * y).sum()
m.add_objective(obj)
m.remove_objective()
assert not len(m.objective.vars)


def test_assert_model_equal():
m = Model()

Expand Down

0 comments on commit 930ceae

Please sign in to comment.