Python 每日最大值的大优化问题

Python big optimization problem with daily max

好的,所以我有以下优化问题: 我需要弄清楚一年中的每个小时,一个发电厂是否应该生产,这取决于生产成本和它获得的生产收入。因此,对于一年中的每个小时,我都有一个变量 0 或 1,它正在生产或不生产。问题是费用取决于每日最大消费。

我得到了下面的代码,但是我得到了一个 nan 错误。

谁能帮我找出我做错了什么?

    #Minimize: cost minus income

#cost=5*max(daily_cons)
#daily_cons=sum of hourly consumption per day
#hourly_cons= variable*inst_power
#income=variable*inst_power*prices
#constraint : yearly production has to be equal to 2000
import csv
import numpy as np
from scipy.optimize import minimize
import openpyxl

#define objective function
def objective(x):
    # x2=cost, x1=income
    return [max(sum((x*inst_power)[i * 24:(i + 1) * 24]) for i in range(int(216/24)))]-sum(x*inst_power*prices)
#define constraint:total number of hours of operation is equal to 10
def constraint1(x):
    sum_eq = 2000
    for i in range(len(x0)):
        sum_eq = sum_eq - x[i]
    return sum_eq

f = open('Book2.csv') # use binary mode if on MS windows
d = [i for i in csv.reader(f) ] # use list comprehension to read from file
f.close() # close file

prices=[i[0] for i in d]
prices = np.delete(prices, 0)
prices=[float(i) for i in prices]
inst_power=[i[1] for i in d]
inst_power = np.delete(inst_power, 0)
inst_power=[float(i) for i in inst_power]
eff=[i[2] for i in d]
eff = np.delete(eff, 0)
eff=[float(i) for i in eff]
x0=np.asarray([1.0]*8760)


#bounds of variables
b=(0.0,1.0)
bnds = [(b[0],b[1]) for k in range(len(x0))]
bnds=tuple(bnds)
print(bnds)

#constrain 1 is equality constraint
con1 = {'type': 'eq', 'fun': constraint1}
cons = ([con1])
#solve problem
solution = minimize(objective,x0,method='SLSQP',\
                    bounds=bnds,constraints=cons)
x = solution.x

# show final objective
print('Final Objective: ' + str(objective(x)))

# print solution
print('Solution')
print(*x, sep = "\n")

一切看起来都很好,问题是你电脑上的 ram 容量不足以处理这么大的数组。 google colab 会有所帮助,或者您可以尝试部分处理数据并将其余部分保存在硬盘上的方法,因为您的计算看起来是线性的。