Removing an existing constraintΒΆ

This notebook outlines the steps to exclude an existing constraint from the model.

[ ]:
from pyehub.energy_hub.ehub_model import EHubModel
from pyehub.energy_hub.utils import constraint_list
from pyehub.outputter import pretty_print

We will inherit the EHubModel class to build a custom model. The idea is that we will exclude a constraint from the new model and the rest of the model will continue to work as it should. + Extend the EHubModel class. + Put the type of constraint decorator you want to be excluded. Here we exclude a constraint of type constraint_list(a list of constraints). To exclude one of type constraint, just use @constraint(). + As an argument to the decorator, pass enabled = False. + Define the constraint to be excluded from the model. + This constraint will no longer be applied when you solve the child model.

[ ]:
class MyModel(EHubModel):
    """
    This is a subclass of the original EHubModel.

    We create a subclass so that we can modify the superclass (or parent class) without modifying the superclass's source code.

    For this tutorial, say we want to remove the capacity bounds constraint(say).
    """

    # We disable the constraint by using `enabled=False` in the function decorator's arguments
    @constraint_list(enabled=False)
    def capacity_bounds(self):
        """Now this constraint won't be used."""
        # We can test this by raising an Exception and then testing if we caught it later on.
        # If this constraint is not disabled, we'll get an Exception due to the execution of the following statement.
        raise Exception

Now we load and run a model.

[ ]:
excel_file = "test_file_all_constraints_work.xlsx"  # name of the excel file. [This must be in the current directory]
my_model = MyModel(
    excel=excel_file
)  # instantiate our model. Nothing is solved at this point.
results = my_model.solve()  # solve the model and get back our results
pretty_print(results)  # print the results to the console
[ ]: