属性错误:“系列”对象没有属性“is_expression_type”

问题描述 投票:0回答:1

我正在研究一个大型模型,这是一个高度简化的版本:

# -*- coding: utf-8 -*-

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

model = pyo.ConcreteModel()

charge_daily = pd.read_csv("input.csv")

#Range of hour
model.h = pyo.RangeSet(0,23)
#Storage Technologies
model.str = pyo.Set(initialize=["phs"])

# I need these because I do some df computation (not relevant to the issue) on the input data beforehand and a Series afterhand (instead of importing data as Series)
days=charge_daily.iloc[:,0].values
value=charge_daily.iloc[:,1].values
charge_daily = pd.Series(value, index=[days])

# Converting to hourly data. I found that dictionary are faster to build but dataframe faster to use, hence the conversion
charge = {}
for str_tec in model.str:
    for h in model.h:
        day = int(h/24)
        charge[str_tec, h] = charge_daily[day]/24
charge = pd.Series(list(charge.values()),index=pd.MultiIndex.from_tuples(charge.keys()))

print(charge)

# What is stored at each hour
model.stored = pyo.Var(((storage, h) for storage in model.str for h in model.h), within=pyo.NonNegativeReals, initialize=0)
# What is taken from storage at each hour
model.gene = pyo.Var(((tec,h) for tec in model.str for h in model.h), within=pyo.NonNegativeReals, initialize=0)

def objective_rule(model):
    """Get constraint for the final objective function"""

    return (sum(sum(model.gene[tec, h] for h in model.h) for tec in model.str))
model.objective = pyo.Objective(rule=objective_rule)

def storing_constraint_rule(model, h, tec):
    """Get constraint on storing."""

    hPOne = h+1 if h < model.h.last() else 0
    return model.stored[tec, hPOne] == model.stored[tec, h] + charge[tec, h] - model.gene[tec,h]
model.storing_constraint = pyo.Constraint(model.h, model.str, rule=storing_constraint_rule)

opt = SolverFactory('gurobi')
results = opt.solve(model)

我正在研究一种能量存储技术,该技术每小时充电一次并可以产生一些存储的能量。这里的目标很微不足道,我过度简化了整个模型。

输入数据以每日时间步长存储在这样的文件中,并在代码中以每小时时间步长进行转换。

day,value
0,0.39166009138686114

这里有一个问题:模型通过

python3 my_model.py
调用运行得非常好。但是,注释
opt.solve
命令并使用
pyomo solve my_model.py --solver='gurobi'
求解会导致以下错误:

错误:为索引为(0,'phs')的约束 storage_constraint 生成表达式时规则失败:AttributeError:'Series'对象没有属性'is_expression_type'

错误:从数据构造组件“storing_constraint”=无失败:
AttributeError:“系列”对象没有属性“is_expression_type”

第一次收到这个。

知道为什么会发生这种情况以及如何解决它吗?我需要使用

pyomo solve
通话

深入挖掘发现

charge
变量在每种情况下都是不同的:

  • 使用
    python3
    和 opt.solve 行运行:正是我想要的(我有几个技术)
phs  0     0.016319
     1     0.016319
     2     0.016319
     3     0.016319
     4     0.016319
     5     0.016319
     6     0.016319
     7     0.016319
     8     0.016319
     9     0.016319
     10    0.016319
     11    0.016319
     12    0.016319
     13    0.016319
     14    0.016319
     15    0.016319
     16    0.016319
     17    0.016319
     18    0.016319
     19    0.016319
     20    0.016319
     21    0.016319
     22    0.016319
     23    0.016319
dtype: float64
  • running
    pyomo solve
    :奇怪的东西,甚至不再排序了
phs  19    0    0.016319
dtype: float64
     6     0    0.016319
dtype: float64
     15    0    0.016319
dtype: float64
     2     0    0.016319
dtype: float64
     11    0    0.016319
dtype: float64
     20    0    0.016319
dtype: float64
     7     0    0.016319
dtype: float64
     16    0    0.016319
dtype: float64
     3     0    0.016319
dtype: float64
     12    0    0.016319
dtype: float64
     21    0    0.016319
dtype: float64
     8     0    0.016319
dtype: float64
     17    0    0.016319
dtype: float64
     4     0    0.016319
dtype: float64
     13    0    0.016319
dtype: float64
     22    0    0.016319
dtype: float64
     0     0    0.016319
dtype: float64
     9     0    0.016319
dtype: float64
     18    0    0.016319
dtype: float64
     5     0    0.016319
dtype: float64
     14    0    0.016319
dtype: float64
     23    0    0.016319
dtype: float64
     1     0    0.016319
dtype: float64
     10    0    0.016319
dtype: float64
dtype: object
python pandas dataframe pyomo
1个回答
0
投票

我敢打赌

input.csv
一定有问题。是否存在该文件的多个副本和/或您是否从不同的父目录或其他目录执行该文件?

出现这种情况的可能原因(您没有提供足够的数据来重复问题)是在约束构造中,它被简化为一个简单的表达式或无法正确计算的内容。当 h=0 时,你的表达式简化为:

model.gene[tec, h] == charge[tec, h] 

无论你有什么元素

charge
,都行不通。

建议:在创建模型之前以某种格式打印出您从 pandas 中读取的内容,以确保其通过检查。或者,(更好)在这里完全抛弃 pandas,只需用 csv

 读取 
csv_reader
 值,并避免 pandas 系列问题。

© www.soinside.com 2019 - 2024. All rights reserved.