纸浆优化错误LPVariable对象不支持索引

2024-06-18 11:40:42 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试添加第31行的约束时,总是出现错误,model+=([MAHL[I][j]for(I,j)in yearlyhours])<;=40

我不确定如何正确设置此约束。我想说的是,在I和j的每一个索引处,特定值必须小于40,并且对所有I,j对都这样做。你知道吗

我是一个非常新的纸浆和试图得到一个基本的模式和运行。输入数据只是一组365行长、24列宽的随机值。你知道吗

from pulp import *
import pandas as pd
import numpy as np
import xlrd

model = pulp.LpProblem("Basic Model", pulp.LpMaximize)

YPER = 365
HE = 24

yearlyhours = [(i,j) for i in range(YPER) for j in range(HE)]

xlsx = pd.ExcelFile('IvA.xlsx')
df1 = pd.read_excel(xlsx, 'Sheet5')
df2 = pd.read_excel(xlsx, 'Sheet3')
df3 = pd.read_excel(xlsx, 'Sheet2')

MAHL = pulp.LpVariable('MAHL', (YPER, HE), cat='Integer')
MALL = pulp.LpVariable('MALL', cat='Integer')
DAHL = pulp.LpVariable('DAHL', cat='Integer')
DALL = pulp.LpVariable('DALL', cat='Integer')

book = xlrd.open_workbook('IvA.xlsx')
sheet10 = book.sheet_by_name('Sheet10')
sheet11 = book.sheet_by_name('Sheet11')

DAPRICE = [[sheet10.cell_value(r, c) for c in range(sheet10.ncols)] for r in range(sheet10.nrows)]
LOAD = [[sheet11.cell_value(r, c) for c in range(sheet11.ncols)] for r in range(sheet11.nrows)]

#model += (MAHL[i][j] for i in range(YPER) for j in range(HE)) <= 40
model += ([MAHL[i][j] for (i,j) in yearlyhours]) <= 40

model += (pulp.lpSum([DAPRICE[i][j] * LOAD[i][j] for i in range(YPER) for j in range(HE)]))

model.solve()
pulp.LpStatus[model.status]
print("Status:", LpStatus[model.status])

obj = value(model.objective)
print(obj)

我尝试了几个解决方案,另外一个被注释掉了。你知道吗

Traceback (most recent call last):
  File "bs.py", line 31, in <module>
    model += ([MAHL[i][j] for (i,j) in yearlyhours]) <= 40
  File "bs.py", line 31, in <listcomp>
    model += ([MAHL[i][j] for (i,j) in yearlyhours]) <= 40
TypeError: 'LpVariable' object does not support indexing

Tags: inimportformodelrangeintegerxlsxpulp
1条回答
网友
1楼 · 发布于 2024-06-18 11:40:42

您已经将MAHL定义为pulp.LpVariable,它(作为错误状态)不支持索引,因为它为LP变量建模。你知道吗

您可能需要使用pulp.LpVariable.dicts来定义它。你知道吗

示例:

MAHL = pulp.LpVariable.dicts('MAHL', yearlyhours, cat=pulp.LpInteger)

并称之为

model += pulp.lpSum([MAHL[(i,j)] for (i,j) in yearlyhours]) <= 40

相关问题 更多 >