mardi 31 octobre 2017

PYTHON, if-statement meets only first condition. PuLP

I am trying to use PuLP to optimize a system, minimizing the cost of it. I am using multiple If's and the problem is that it always meets the first condition. Here is my code. I hope someone can help me, as I am just starting to learn about this language.

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()

The solution given always meets first condition. Also, when the condition is not met (changing CPEM to 50000000000000, for example) the if works as it is true.

Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire