나는 시스템을 최적화하기 위해 PuLP를 사용하여 비용을 최소화하려고합니다. 나는 여러 개의 If를 사용하고 있는데 문제는 항상 첫 번째 조건을 충족한다는 것입니다. 여기 내 코드가있다. 나는이 언어에 대해 배우기 시작한 누군가가 나를 도울 수 있기를 바랍니다.PYTHON, if 문이 첫 번째 조건 만 충족합니다. PuLP
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가 true로 작동합니다.
미리 감사드립니다.
코드를 먼저 들여 쓰기해야합니다. –
죄송합니다. 복사 붙여 넣기 문제였습니다. 파이썬에 게시 한 게시물을 편집했습니다. 관찰을 해주셔서 감사합니다 –
문서를 간략하게 살펴보면 각각 'CWV'에 대해 'CPV'를 'CPV = LpVariable ("PvCapacity", lowBound = 0, cat ='Integer ') 및 동일한 것으로 변경해보십시오. 그것은 작동합니다. –