Python PuLP" if"仅符合第一个条件。

时间:2017-11-01 17:58:23

标签: python if-statement pulp

我正在尝试使用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将正常工作。

使用&#34; elif&#34;我有相同的结果。如果我改变条件的顺序(意味着如果xEne&lt; = CPEM,则结果变为仅符合第一个新条件。

提前谢谢!

Priscila Castillo

1 个答案:

答案 0 :(得分:2)

if语句不起作用,因为变量在优化之后才定义了值。

在你设置的lp问题中考虑LpVariables与优化器之间的关系找到与之匹配的解决方案。

在你的情况下,你是以一种不正确的方式混合LpVariables和python整数。

也许看看 http://www.yzuda.org/Useful_Links/optimization/if-then-else-02.html

https://ocw.mit.edu/courses/sloan-school-of-management/15-053-optimization-methods-in-management-science-spring-2013/lecture-notes/MIT15_053S13_lec11.pdf

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.464.6182&rep=rep1&type=pdf

寻求帮助

斯图