'Pyomo: 'list' object has no attribute error
import pyomo.environ as pyo
import numpy as np
import random
from pyomo.environ import NonNegativeReals
model = pyo.ConcreteModel()
model.nBS = pyo.Param(initialize=2, within=pyo.Reals)
model.BS = pyo.RangeSet(model.nBS)
model.AES = pyo.Set(initialize=[[1, 1], [1, 2], [2, 4]])
model.K = pyo.Set(initialize=[1, 2, 3])
model.AES2 = pyo.Set(model.BS, initialize={1: [1, 2], 2: [4]})
def set_init(m):
return [(i, n) for i in m.BS for n in m.AES2[i]]
model.AES3 = pyo.Set(dimen=2, initialize=set_init)
def e_init(m):
return [random.random()]
model.e = pyo.Param(model.AES3, model.K, initialize=e_init, mutable=True, within=pyo.Any)
model.p = pyo.Param(model.K, initialize=[1, 2, 3], within=pyo.Any)
model.CC = pyo.Param(model.AES, model.K, default=10, mutable=True, within=pyo.Any)
model.nnt = pyo.Param(default=10, within=pyo.Any)
model.ncp = pyo.Param(default=10, within=pyo.Any)
model.b = pyo.Param(model.K, initialize=[1, 2, 3], within=pyo.Any)
model.w = pyo.Param(model.K, initialize=[1, 2, 3], within=pyo.Any)
model.r = pyo.Param(model.BS, initialize=[10, 10], within=pyo.Any)
model.x = pyo.Var(model.AES, model.K, within=pyo.Binary)
model.y = pyo.Var(model.AES, model.K, within=pyo.Binary)
model.z = pyo.Var(model.AES, model.K, within=pyo.Binary)
def obj_expression(m):
return sum(m.e[i, j, k] * m.x[i, j, k] for (i, j, k) in m.AES * m.K)
def task_assignment_cons(m, k):
return sum(m.x[i, j, k] for (i, j) in m.AES) == 1
def max_task_cons(m, i, j):
return sum(m.x[i, j, k] for k in m.K) <= len(m.K)
def max_lat_cons(m, i):
return sum(m.w[k] * m.x[i, j, k] for j in m.AES2[i] for k in m.K) >= 0
model.cons1 = pyo.Constraint(model.K, rule=task_assignment_cons)
model.cons2 = pyo.Constraint(model.AES, rule=max_task_cons)
model.cons4 = pyo.Constraint(model.BS, rule=max_lat_cons)
I am getting the error of AttributeError: 'list' object has no attribute 'is_expression_type'
when I try to run model.cons4 = pyo.Constraint(model.BS, rule=max_lat_cons). I am not experienced in Python forgive me if the question is too basic. I feel like the "sum" function does not return what I think it returns.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
