Reputation: 1
I am trying to solve an Abstract model in PYOMO and I am having trouble importing the data into the model to create an instance.
I've looked online for documentation/examples importing data and I don't see how mine is different than the examples found online that do work. ''' ## call PYOMO from pyomo.environ import *
## Define Model
model = AbstractModel()
# Define index sets
# I= set if calculation steps where investments are possible
model.I = Set(dimen=1)
model.I.construct()
# D = set of DERs
model.D = Set(dimen=1)
model.D.construct()
## Define Parameters
a = 0.5
model.NFix = Param(model.D, model.I, within=PositiveReals)
model.NVar = Param(model.D, model.I, within=PositiveReals)
model.RFix = Param(model.D, model.I, within=PositiveReals)
model.RVar = Param(model.D, model.I, within=PositiveReals)
model.y = Param(model.I, within=PositiveReals)
## Define variables
model.n = Var(model.D, model.I, within = Binary)
model.r = Var(model.D, model.I, within = Binary)
model.NCap = Var(model.D, model.I, within = PositiveReals)
model.RCap = Var(model.D, model.I, within = PositiveReals)
## Define objective value
model.cost = Objective(sum((sum(model.n[d,i]*model.NFix[d,i]+ model.NCap[d,i]*model.NVar[d,i] +
model.r[d,i]*model.RFix[d,i] +model.RCap[d,i]*model.RVar[d,i] for d in model.D))/(1+a)**y[i]) for i in model.I)
instance = model.create_instance('data.dat')
opt = pyo.SolverFactory('glpk')
opt.solve(instance)
For the data.dat file I have the following:
set I := i1 i2 i3;
set D := d1 d2;
param NFix : i1 i2 i3 :=
d1 1 1 3
d2 1 3 5
;
param NVar : i1 i2 i3 :=
d1 1 1 3
d2 1 3 5
;
param RFix : i1 i2 i3 :=
d1 1 1 3
d2 1 3 5
;
param RVar : i1 i2 i3 :=
d1 1 1 3
d2 1 3 5
;
param y := 1 2 ;
I get the following error message:
ERROR: Constructing component 'NFix' from data={('d1', 'i1'): 1, ('d2', 'i1'): 1, ('d1', 'i2'): 1, ('d2', 'i2'): 3, ('d1', 'i3'): 3, ('d2', 'i3'): 5} failed: RuntimeError: Failed to set value for param=NFix, index=('d1', 'i1'), value=1. source error message="Index '('d1', 'i1')' is not valid for indexed component 'NFix'"
Upvotes: 0
Views: 535
Reputation: 2828
I found a couple minor errors in both your model file and your dat file.
In your model file, you should never manually call the construct
method on a Pyomo component. In your objective function, you had misplaced parenthesis and you need to make sure to use a rule to define the objective function when using an Abstract model. I also noticed that you forgot the model.
before referencing y
. A corrected version of this file is below:
## Define Model
model = AbstractModel()
# Define index sets
# I= set if calculation steps where investments are possible
model.I = Set(dimen=1)
# D = set of DERs
model.D = Set(dimen=1)
## Define Parameters
a = 0.5
model.NFix = Param(model.D, model.I, within=PositiveReals)
model.NVar = Param(model.D, model.I, within=PositiveReals)
model.RFix = Param(model.D, model.I, within=PositiveReals)
model.RVar = Param(model.D, model.I, within=PositiveReals)
model.y = Param(model.I, within=PositiveReals)
## Define variables
model.n = Var(model.D, model.I, within = Binary)
model.r = Var(model.D, model.I, within = Binary)
model.NCap = Var(model.D, model.I, within = PositiveReals)
model.RCap = Var(model.D, model.I, within = PositiveReals)
## Define objective value
def _obj(model):
return sum((sum(model.n[d,i]*model.NFix[d,i]+ model.NCap[d,i]*model.NVar[d,i] + \
model.r[d,i]*model.RFix[d,i] +model.RCap[d,i]*model.RVar[d,i] for d in model.D)) \
/(1+a)**model.y[i] for i in model.I)
model.cost = Objective(rule=_obj)
instance = model.create_instance('abstract.dat')
opt = SolverFactory('glpk')
opt.solve(instance, tee=True)
The only issue in your dat file is that you haven't defined the y
parameter properly. In the model it looks like y
is indexed by I
which means you need to define your parameter something like:
param y :=
i1 1
i2 2
i3 3 ;
Upvotes: 1