-1

I am new to pyomo. I am trying to run a simple maximization problem, but i keep getting this error message: Error retrieving component Pd[1]: The component has not been constructed. . Only the last 5 constraints give me this problem, the first three constraints work fine. I'm using this command on the IPython console to run it !pyomo --solver-manager=neos --solver=cbc battpyomo.py battpyomo.dat

On the data file, I only define the set T and parameter p.

set T := 1 2 3 4 5 6 7 8 9;

param: p :=
1 51.12
2 48.79
3 39.56
4 36.27
5 36.16
6 34.90
7 33.33
8 21.16
9 24.42;

Here's the code for battbyomo.py:

from pyomo.environ import *

model = AbstractModel()

########Sets#########
#Hours
model.T = Set()

#########Parameters##########
#Price
model.p = Param(model.T,within=PositiveReals)
#Variable OM cost Discharge
model.VOMd = Param(initialize=15)
#Varaible OM cost Charge
model.VOMc = Param(initialize=10)
#State of Charge min
model.SOCmin = Param(initialize=5)
#State of charge max
model.SOCmax = Param(initialize=25)
#ESS efficiency
model.m = Param(initialize=0.99)
#Max discharge rate
model.Pdmax = Param(initialize=20)
#Max charge rate
model.Pcmax = Param(initialize=20)
#Initial State of Charge
model.SOCini = Param(initialize=25)

###########Variables##########
#Power discharged
model.Pd = Var(model.T, within=NonNegativeIntegers)
#Power charged
model.Pc= Var(model.T, within=NonNegativeIntegers)
#Charging Status
model.Uc = Var(model.T, within=NonNegativeIntegers)
#Discharging status
model.Ud = Var(model.T, within=NonNegativeIntegers)
#State of Charge
model.SOC = Var(model.T, within=NonNegativeIntegers)

#######Objective##########
def profit_rule(model):
    return sum(model.Pd[i]*model.p[i]-model.Pd[i]*model.VOMd-model.Pc[i]*model.p[i]-model.Pc[i]*model.VOMc for i in model.T)
model.profit = Objective(rule=profit_rule, sense = maximize)    

#######Constraints##########

def status_rule(model,i):
    return (model.Ud[i] + model.Uc[i] <= 1)
model.status = Constraint(model.T,rule=status_rule)

def Cmax_rule(model,i):
    return model.Pc[i] <= model.Pcmax*model.Uc[i]
model.Cmax = Constraint(model.T,rule=Cmax_rule)

def Dmax_rule(model,i):
    return model.Pd[i] <= model.Pdmax*model.Ud[i]
model.Dmax = Constraint(model.T,rule=Dmax_rule)

def Dlim_rule(module,i):
    return model.Pd[i] <= model.SOC[i] - model.SOCmin
model.Dlim = Constraint(model.T,rule=Dlim_rule)

def Clim_rule(module,i):
    return model.Pc[i] <= model.SOCmax-model.SOC[i]
model.Clim = Constraint(model.T,rule=Clim_rule)

def Smin_rule(module,i):
    return model.SOC[i] >= model.SOCmin
model.Smin = Constraint(model.T,rule=Smin_rule)

def Smax_rule(module,i):
    return model.SOC[i] <= model.SOCmax
model.Smax = Constraint(model.T,rule=Smax_rule)

def E_rule(module,i):
    if i == 1:
        return model.SOC[i] == model.SOCini + model.Pc[i]*model.m -model.Pd[i]
    else:
        return model.SOC[i] == model.SOC[i-1] + model.Pc[i]*model.m - model.Pd[i]
model.E = Constraint(model.T,rule=E_rule)

2 Answers2

3

In some of the constraints listed above, the argument in the rule is "module", when "model" is used in the expression e.g.,

def Dlim_rule(module,i):
    return model.Pd[i] <= model.SOC[i] - model.SOCmin
model.Dlim = Constraint(model.T,rule=Dlim_rule)

The definition of the rule and the constraint should be,

def Dlim_rule(model,i):
    return model.Pd[i] <= model.SOC[i] - model.SOCmin
model.Dlim = Constraint(model.T,rule=Dlim_rule)

Incidentally, the first argument (for the model object) can be called anything you like. However, the name of the argument must match the use of the model within the rule. For example, this is also valid,

def Dlim_rule(m,i):
    return m.Pd[i] <= m.SOC[i] - m.SOCmin
model.Dlim = Constraint(model.T,rule=Dlim_rule)
Carl Laird
  • 56
  • 2
0

You shouldn't need the very first "import pyomo" line. The only import line you should need is the "from pyomo.environ import *". If this doesn't solve your problem then you should post the data file you're using (or a simplified version of it). Seems like the data isn't being loaded into the Pyomo model correctly.

Bethany Nicholson
  • 2,723
  • 1
  • 11
  • 18
  • Thank you @Bethany Nicholson , I removed the "import pyomo" line and the problem is still there, I added the information on the data file to the description. I hope you can help me. – Juan Arteaga Oct 04 '16 at 14:48