0
$\begingroup$

I would like to crate an constraint-equation that depends on 4 sets. For that I use the following code:

#Equations for calculating the total generation from renewable energy sources (RES)
def RESgenerationTotalRule (model, index_BT1, index_BT2, index_BT3, t):
    return model.variable_RESGenerationTotal [t] == sum (model.param_pvGenerationNominal_BT1 [index_BT1, t] * SetUpScenarios.pvPeaksOfBuildings[index_BT1] + model.param_windAssignedNominal_BT1 [index_BT1, t] * SetUpScenarios.maximalPowerOfWindTurbine for index_BT1 in model.set_buildings_BT1) + sum (model.param_pvGenerationNominal_BT2 [index_BT2, t] * SetUpScenarios.pvPeaksOfBuildings[index_BT2] + model.param_windAssignedNominal_BT2 [index_BT2, t] * SetUpScenarios.maximalPowerOfWindTurbine for index_BT2 in model.set_buildings_BT2) + sum (model.param_pvGenerationNominal_BT3 [index_BT3, t] * SetUpScenarios.pvPeaksOfBuildings[index_BT3] + model.param_windAssignedNominal_BT3 [index_BT3, t] * SetUpScenarios.maximalPowerOfWindTurbine for index_BT3 in model.set_buildings_BT3)
    
model.constraint_RESgenerationTotal = pyo.Constraint(model.set_buildings_BT1, model.set_buildings_BT2, model.set_buildings_BT3,model.set_timeslots, rule = RESgenerationTotalRule)

model.constraint_RESgenerationTotal.pprint()

Basically I have the 4 sets model.set_buildings_BT1, model.set_buildings_BT2, model.set_buildings_BT3,model.set_timeslots. When all of those sets are non-empty, the equation is defined. Howeever, if one of the 3 sets model.set_buildings_BT1, model.set_buildings_BT2, model.set_buildings_BT3 is empty, I get and equations Size of 0 and the equation is not defined. How can I implement in Pyomo that - even if some sets are empty - the equation should be defined because it still makes sense to define it. The equation consists of 3 sums; one for each of the 3 sets model.set_buildings_BT1, model.set_buildings_BT2, model.set_buildings_BT3. If one out of those 3 sets in empty, the sum for this set should just not be considered (or set to 0). Any idea how I can do that? I appreciate every comment.

Update: I tried another solution

#Equations for calculating the total generation from renewable energy sources (RES)
def RESgenerationTotalRule (model, t):
    return model.variable_RESGenerationTotal [t] == sum (model.param_pvGenerationNominal_BT1 [index_BT1, t] * SetUpScenarios.pvPeaksOfBuildings[index_BT1] + model.param_windAssignedNominal_BT1 [index_BT1, t] * SetUpScenarios.maximalPowerOfWindTurbine for index_BT1 in model.set_buildings_BT1) + sum (model.param_pvGenerationNominal_BT2 [index_BT2, t] * SetUpScenarios.pvPeaksOfBuildings[index_BT2] + model.param_windAssignedNominal_BT2 [index_BT2, t] * SetUpScenarios.maximalPowerOfWindTurbine for index_BT2 in model.set_buildings_BT2) + sum (model.param_pvGenerationNominal_BT3 [index_BT3, t] * SetUpScenarios.pvPeaksOfBuildings[index_BT3] + model.param_windAssignedNominal_BT3 [index_BT3, t] * SetUpScenarios.maximalPowerOfWindTurbine for index_BT3 in model.set_buildings_BT3)
    
model.constraint_RESgenerationTotal = pyo.Constraint(model.set_timeslots, rule = RESgenerationTotalRule)

model.constraint_RESgenerationTotal.pprint()

Here it is also the case that when the 3 sets are non-empty, the equation is defined and when one of those sets is not empty, I get an error message (opposed to the size 0 equation from the other approach) stating "IndexError: index 3 is out of bounds for axis 0 with size 3"

Here is the equation that I want to implement in Pyomo with the 3 independant sets BT1, BT2 and BT3. If one set is empty (or even 2) the equation should also be createdenter image description here

$\endgroup$
3
$\begingroup$

Let me give you an example

model.N =Param(mutable=True, initialize=10) 
model.i = RangeSet(1,model.N)
model.x = Var(model.i,initialize=0,domain=Binary)

def rue_c1(model):
    return sum(model.x[i] for i in model.i)==5
model.C1 =Constraint(rule=rue_c1)

in the above case, if you initialize your set with N (and define it as a mutable parameter) then it will do the job for you. you can then change it before the solve statement

model.N=20
instance = model.create_instance()

then

opt = SolverFactory('glpk')
results=opt.solve(instance)
$\endgroup$
4
  • $\begingroup$ Thanks for your answer Optimization team. I tried your suggested approach but it did not change anything. I still get the same error. I assume that something is wrong with my equation but I still have not figured out what exactly is the problem. Both both the error message and the values of the constraints (when there is no error message) did not change at all using your approach. It is exactly the same as before. $\endgroup$ – PeterBe Jun 7 at 15:13
  • $\begingroup$ Okay I found the mistake. For the arrays the counting begins with 0 and for the Pyomo.sets the counting begins with 1. This caused the problem. But from the structure itself my equations seem to be correct. $\endgroup$ – PeterBe Jun 7 at 16:19
  • $\begingroup$ So if my answer is correct you can confirm it. $\endgroup$ – Optimization team Jun 8 at 15:27
  • $\begingroup$ Okay, I did that. I upvoted and accepted your answer. $\endgroup$ – PeterBe Jun 8 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.