Home > Mobile >  "Cannot treat the scalar component '' as an indexed component"
"Cannot treat the scalar component '' as an indexed component"

Time:09-19

Here is a few rows of my code for an operations planning optimization.I have 2 dictionaries for the product monthly demand .I have created two parameters with these two dictionaries. As m i have defined the months. After that, i want to multiply some variables with the parameters of demand(the following constraints) but there is an error KeyError: "Cannot treat the scalar component 'tsi' as an indexed component" I don't know what is wrong with that. I have tried a lot of things to fix it but nothing works.

import pyomo.environ as pyo
from pyomo.opt import SolverFactory

def run(): model = pyo.ConcreteModel()

Dict1 = {1:28800,2:26200,3:20900,4:12660,5:25770,6:12350,7:28200,8:26200,9:30000,10:7400,11:24000,12:22200}

Dict2 = {1:5000,2:5700,3:4000,4:1540,5:9000,6:1800,7:7250,8:4000,9:8000,10:3000,11:5000,12:8100}

model.m = pyo.Param(initialize = 12)                                                                                    
model.k = pyo.Param(initialize=2)                                         
model.i = pyo.Param(initialize=3)                                        
model.j = pyo.Param(initialize=3)                                      
model.l = pyo.Param(initialize=2)
model.tsi = pyo.Param(initialize = Dict1)
model.trc = pyo.Param(initialize = Dict2)

model.setm = pyo.RangeSet(1, model.m)
model.setk = pyo.RangeSet(1, model.k)
model.seti = pyo.RangeSet(1, model.i)
model.setj = pyo.RangeSet(1, model.j)
model.setl = pyo.RangeSet(1, model.l)

def sixteenthRule(model, m): return ([model.g[1,m] for m in model.setm]) == ([model.trc[m] for m in range(1,13)])*([model.G[1,m] for m in model.setm]) ([model.tsi[m] for m in range [1,13]])

def seventeenthRule(model, m): return ([model.g[2,m] for m in model.setm]) == ([model.trc[m] for m in range(1,13)])*([1-model.G[1,m] for m in model.setm])

def twentyeightthRule(model, m): return ([model.z[1,1,m] for m in model.setm]) == ([model.z[1,1,m-1] for m in model.setm]) ([model.p[1,1,m] for m in model.setm]) - ([model.tsi[m] for m in range [1,13]])

def thirtythRule(model, m): return ([model.z[1,2,m] for m in model.setm]) == ([model.z[1,2,m-1] for m in model.setm]) ([model.p[2,2,m] for m in model.setm]) ([model.dr[2,m] for m in model.setm]) - ([model.G[1,m] for m in model.setm])*([model.trc[m] for m in range [1,13]]) ([model.p[1,2,m] for m in model.setm])

CodePudding user response:

The parameter model.tsi is a scalar parameter in the way you have defined it i.e. it can only take a single value e.g. 5 or 10.

The Dict1 dictionary has multiple values though. If you need to pass all of them, depending on the exact details of your problem, you will have to first define a Pyomo Set for all the keys of Dict1 like:

model.set_tsi = pyo.RangeSet(1, 12)

Then define the parameter as follows:

model.tsi = model.tsi = pyo.Param(model.set_tsi, initialize = Dict1)

CodePudding user response:

hello and thank you for your answer. I follow your advice but i still have error. As you can see below sets and constraints that contains the parameters :

model.m = pyo.Param(initialize = 12)                                                                                    
model.k = pyo.Param(initialize=2)                                         
model.i = pyo.Param(initialize=3)                                       
model.j = pyo.Param(initialize=3)                                       
model.l = pyo.Param(initialize=2)

model.setm = pyo.RangeSet(1, model.m)
model.setk = pyo.RangeSet(1, model.k)
model.seti = pyo.RangeSet(1, model.i)
model.setj = pyo.RangeSet(1, model.j)
model.setl = pyo.RangeSet(1, model.l)
model.set_tsi = pyo.RangeSet(1, 12)
model.set_trc = pyo.RangeSet(1, 12)

model.tsi = pyo.Param(model.set_tsi, initialize = Dict1)
model.trc = pyo.Param(model.set_trc, initialize = Dict2)

def sixteenthRule(model, m):
return sum([model.g[1,m] for m in model.setm]) == sum([model.trc for trc in range(1,12)])*sum([model.G[1,m] for m in model.setm])/12   sum([model.tsi for tsi in range(1,12)])

def seventeenthRule(model, m): return ([model.g[2,m] for m in model.setm]) == sum([model.trc for trc in range(1,12)])*sum([1-model.G[1,m] for m in model.setm])/12

def twentyeightthRule(model, m): return sum([model.z[1,1,m] for m in model.setm]) == sum([model.z[1,1,m-1] for m in model.setm]) sum([model.p[1,1,m] for m in model.setm]) - sum([model.tsi for m in model.setm])

*return sum([model.g[1,m] for m in model.setm]) == sum([model.trc for trc in range(1,12)])sum([model.G[1,m] for m in model.setm])/12 sum([model.tsi for tsi in range(1,12)]) TypeError: unsupported operand type(s) for : 'int' and 'IndexedParam'

  • Related