我正在尝试使用PuLP来优化系统,从而最大限度地降低系统成本。我正在使用多个If,问题是它始终满足第一个条件。这是我的代码。我希望有人可以帮助我,因为我刚开始学习这门语言。
import numpy as np
import pandas as pd
from pulp import *
idx = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
d = {
'day': pd.Series(['01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14'], index=idx),
'hour':pd.Series(['00:00:00', '01:00:00', '02:00:00', '03:00:00', '04:00:00', '05:00:00', '06:00:00', '07:00:00', '08:00:00', '09:00:00', '10:00:00', '11:00:00', '12:00:00', '13:00:00', '14:00:00', '15:00:00', '16:00:00', '17:00:00', '18:00:00', '19:00:00', '20:00:00', '21:00:00', '22:00:00', '23:00:00'], index=idx),
'output':pd.Series([0,0,0,0.087,0.309,0.552,0.682,0.757,0.783,0.771,0.715,0.616,0.466,0.255,0.022,0,0,0,0,0,0,0,0,0], index=idx)}
cfPV = pd.DataFrame(d)
idx = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
d1 = {
'day': pd.Series(['01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14', '01/01/14'], index=idx),
'hour':pd.Series(['00:00:00', '01:00:00', '02:00:00', '03:00:00', '04:00:00', '05:00:00', '06:00:00', '07:00:00', '08:00:00', '09:00:00', '10:00:00', '11:00:00', '12:00:00', '13:00:00', '14:00:00', '15:00:00', '16:00:00', '17:00:00', '18:00:00', '19:00:00', '20:00:00', '21:00:00', '22:00:00', '23:00:00'], index=idx),
'output':pd.Series([0.528,0.512,0.51,0.448,0.62,0.649,0.601,0.564,0.541,0.515,0.502,0.522,0.57,0.638,0.66,0.629,0.589,0.544,0.506,0.471,0.448,0.438,0.443,0.451], index=idx)}
cfWT = pd.DataFrame(d1)
prob = LpProblem ("System", LpMinimize)
CPV = LpVariable ("PVCapacity",0) #PV Capacity in kW
CWT = LpVariable ("WTurCapacity",0) #WT Capacity in kW
CBA = LpVariable ("BatteryCapacity",0) #Battery Capacity kW
prob+= 63.128*CPV + 88.167*CWT + 200*CBA, "TotalCostSystem"
xEne = 0
xREin = 0
xBin = 0
xBout = 0
SOCB = 0
xPEMin = 0
xOvEn = 0
xSum = 0
CPEM = 230
for i in idx:
xEne = (CPV*cfPV['output'][i]+CWT*cfWT['output'][i])
#Low limit for Variables
prob += (CPV*cfPV['output'][i]+CWT*cfWT['output'][i]) >= 0
prob += xREin >= 0
prob += xBin >= 0
prob += xBout >= 0
prob += SOCB >= 0
prob += xPEMin >= 0
prob += xOvEn >= 0
prob += xSum >= 0
prob += CBA >= SOCB
prob += xBin <= (CBA - SOCB)
prob += xBout <= SOCB
#Cases
#Case 1 xEne > CPEM
if xEne >= CPEM:
xREin = CPEM
xBout = 0
xOvEn = xEne - CPEM
#Case 1.1 xOvEn < CBA - SOCB
if (value(xOvEn) <= (CBA - value(SOCB))):
xBin = xOvEn
#Case 1.2 xOvEn > CBA -SOCB
else:
xBin = CBA - SOCB
#Case 2 xEne < CPEM
else:
xREin = xEne
xBin = 0
xOvEn = 0
#Case 2.1 SOCB > CPEM - xREin
if (value(SOCB) >= (CPEM - value(xREin))):
xBout = (CPEM - xREin)
#Case 2.2 SOCB < CPEM - xREin
else:
xBout = SOCB
SOCB = SOCB + xBin - xBout
xPEMin = xREin + xBout
xSum += xPEMin
prob += xSum >= 5000
prob.writeLP("PVWTBattSyste.lp")
prob.solve()
给出的解决方案始终满足第一个条件。此外,当不满足条件时(例如,将CPEM更改为50000000000000),if将按原样运行。
提前谢谢!
答案 0 :(得分:0)
您应该使用elif
语句来区分一个if elif else
。这也具有其他语言中使用的开关盒的效果。
但另一方面,你是否使用缩进?像seriuosly一样,Python就是缩进。
此外,您还有一个else
语句没有if statemt
#Case 1.2 xOvEn > CBA -SOCB
else:
xBin = CBA - SOCB
#Case 2 xEne < CPEM
else:
xREin = xEne
xBin = 0
xOvEn = 0
换句话说,如果没有缩进,您的程序只会将空if
跳到下一行。