Skip to content

Commit

Permalink
Add methods for custom variables and constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
lumbric committed Mar 11, 2024
1 parent c2247e0 commit 425d823
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
20 changes: 20 additions & 0 deletions syfop/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,26 @@ def _generate_optimization_model(self, nodes, solver_dir):

return model

def add_variables(self, *args, **kwargs):
"""Add custom variables to the linopy optimization model. See
``linopy.Model.add_variables()`` for a documentation of the parameters.
This method must be called before ``Network.optimize()`` is called.
"""
return self.model.add_variables(*args, **kwargs)

def add_constraints(self, *args, **kwargs):
"""Add custom constraints to the linopy optimization model. See
``linopy.Model.add_constraints()`` for a documentation of the parameters.
To create the constraint, variables can be accessed via ``Network.model``
This method must be called before ``Network.optimize()`` is called.
"""
return self.model.add_constraints(*args, **kwargs)

def _check_storage_level_zero(self):
"""This is a basic plausibility check. A storage which is never full or never empty
could be replaced by a smaller storage, which would be advantageous if costs are
Expand Down
44 changes: 44 additions & 0 deletions tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,47 @@ def test_infeasible_network():
)
with pytest.raises(SolverError, match=error_msg):
network.optimize(default_solver)


def test_network_add_constraints():
"""Test adding custom constraints."""
wind = NodeScalableInputProfile(
name="wind",
input_flow=const_time_series(0.5),
costs=1,
output_unit="MW",
)
demand = NodeFixOutputProfile(
name="demand",
inputs=[wind],
input_commodities="electricity",
output_flow=const_time_series(5.0),
costs=0,
output_unit="MW",
)
curtailment = Node(
name="curtailment",
inputs=[wind],
input_commodities="electricity",
costs=0,
output_unit="MW",
)
network = Network([wind, demand, curtailment])

# Let's make Sebastian happy and force the system to have at least 15MW of wind :)
network.add_constraints(wind.size >= 15)
network.optimize()

assert network.model.solution.size_wind == 15.0
np.testing.assert_array_almost_equal(network.model.solution.output_flow_curtailment, 2.5)


def test_network_add_variables():
"""Test adding custom variables."""
network = simple_demand_network()
total_energy = network.add_variables(name="total_energy")
network.add_constraints(total_energy - network.nodes_dict["wind"].input_flows[""].sum() == 0.0)
network.optimize()

# we need 5 MW constant demand
assert network.model.solution.total_energy == DEFAULT_NUM_TIME_STEPS * 5.0

0 comments on commit 425d823

Please sign in to comment.