How can I write a pyomo constraint/rule with conditions checked against boolean values within the model data?

Question:

I’m attempting to solve a battery problem using Pyomo. I’m attempting to tell the model that the battery can only discharge during certain hours of the year. I’ve implemented this by create a boolean column within my initial dataframe called ‘Dischargeable’ which is then imported into my Pyomo model data.

If I just let the solver optimize for every hour of the year everything works great. However, when trying to implement this rule I receive the exact same solution as without the rule, and I can see in the results data that the ‘Dischargeable’ data is being ignored. This leads me to believe the issue is only within how I’m writing the rule.

Rule implementation below:

    model.p_BAT_discharge = pyo.Var(model.t, within=pyo.NonNegativeReals)

    # whether or not the battery can discharge as per market rules
    model.dischargeable = pyo.Param(model.t,
                                    within = pyo.Boolean,
                                    initialize=dict(df_model['Dischargeable'])
                                    )
    def discharge_time_rule(model, t):
        if model.dischargeable[t] == False:
            return model.p_BAT_discharge[t] == 0
        else:
            return pyo.Constraint.Skip
        model.discharge_time_constraint = pyo.Constraint(model.t,rule=discharge_time_rule)

I’ve also tried something like:

    def discharge_time_rule(model, t):
        if pyo.Value(model.dischargeable[t] == False) == True:
            return model.p_BAT_discharge[t] == 0
        else:
            return pyo.Constraint.Skip
        model.discharge_time_constraint = pyo.Constraint(model.t,rule=discharge_time_rule)

Thank you!

Asked By: masebee

||

Answers:

Do you miss the bad indentation?

def discharge_time_rule(model, t):
    if pyo.Value(model.dischargeable[t] == False) == True:
        return model.p_BAT_discharge[t] == 0
    else:
        return pyo.Constraint.Skip

# The following line should be outside the function
model.discharge_time_constraint = pyo.Constraint(model.t,rule=discharge_time_rule)
Answered By: Corralien