Python PuLP初学者约束列表

时间:2017-11-22 18:50:34

标签: python python-3.x list constraints pulp

我是Python的初学者。

我正在使用PuLP来解决可再生能源系统(PV +风+电池)的最小化问题。

我的系统使用时间步长(24小时,每小时)。我的问题是如何在收集电池充电状态的约束条件中创建一个列表,以及它的入口和出口(每次改变值的变量)。在mathlab中,我看到人们使用" eye",但是在PuLP中我不知道怎么做。

非常感谢任何帮助。

以下是我所拥有的代码的一部分(我知道,这是不正确的。)

import numpy as np
import pandas as pd
from pulp import *

#cfPV Values 'Renewable Ninja values'
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)

#cfWT Values 'Renewable Ninja values'
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+= 63.128*CPV + 88.167*CWT + 126.97 * CBatt, "TotalCostSystem"

    xBin = np.array([])
    xBout = np.array([])
    SOCB = np.array([])

for i in idx:
    for x in enumerate(cfPV):
        SOCB[x] += + xBin[x] - xBout[x] 
        prob += SOCB[x] <= CBatt
        prob += SOCB >= 0
        prob += xBout >= 0 
        prob += xBin >= 0
        prob += CPV*cfPV['output'][i] + CWT*cfWT ['output'][i] + xBout[x] - xBin[x] >= 0
        prob += (CPV*cfPV['output'][i] + CWT*cfWT ['output'][i]) + xBout[x] - xBin[x] <= 250



prob += lpSum(CPV*cfPV['output'][i] + CWT*cfWT ['output'][i] + xBout[x] - xBin[x] for i in idx) >= 5000

提前谢谢你。

1 个答案:

答案 0 :(得分:0)

下面是您的代码的一个版本,我认为可以解决您试图在上面的代码中提出的问题。它打印出以下内容:

Status: Optimal
Total Cost is =  34757.8679575668
Capacity PV =  7.9625771
Capacity WT =  383.65144
Capacity Batt =  3.3851294
Total export =  5000.0

如果您还没有考虑,可能要考虑一些事情-

  • 您的单位是否一致? (我不知道您的数据来自何方)
  • 您需要考虑电池的充放电损失吗?
  • 您想让电池保持与开始时相同的电量吗? (如果数据是“典型”的一天,则很重要)

import numpy as np

import pandas as pd

from pulp import *

#cfPV Values 'Renewable Ninja values'
idx = range(0, 24)
idx_plus_1 = range(0, 25)

d = {'day': pd.Series(['01/01/14']*24, 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)

#cfWT Values 'Renewable Ninja values'
d1 = {
    'day': pd.Series(['01/01/14']*24, 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)

# Initialise porblem object
prob = LpProblem("Renewable Optimisation", LpMinimize)

# Define the capacity variables
CPV = LpVariable("CPV", 0, None, LpContinuous)
CWT = LpVariable("CWT", 0, None, LpContinuous)
CBatt = LpVariable("CBatt", 0, None, LpContinuous)

# Total Capital cost of system is cost of each item x it's capacity
prob+= 63.128*CPV + 88.167*CWT + 126.97 * CBatt, "TotalCostSystem"

# Variable for battery charging, xBin, battery discharging xBout, and battery SoC
# NB: SoC has an additional index - to track SoC at end of final interval
xBin = LpVariable.dicts("xBin", idx, 0)
xBout = LpVariable.dicts("xBout", idx, 0)
SOCB = LpVariable.dicts("SOCB", idx_plus_1, 0)

# Need to define starting SoC
SOCB[0] == 50.0

for i in idx:
    prob += SOCB[i+1] == SOCB[i] + xBin[i] - xBout[i] 
    prob += SOCB[i] <= CBatt
    prob += SOCB[i] >= 0
    prob += xBout[i] >= 0 
    prob += xBin[i] >= 0
    prob += CPV*cfPV['output'][i] + CWT*cfWT['output'][i] + xBout[i] - xBin[i] >= 0
    prob += (CPV*cfPV['output'][i] + CWT*cfWT['output'][i]) + xBout[i] - xBin[i] <= 250

export = LpVariable.dicts("export", idx, 0)
for i in idx:
    prob += export[i] == CPV*cfPV['output'][i] + CWT*cfWT['output'][i] + xBout[i] - xBin[i]  

total_export = LpVariable("total_export", 0, None, LpContinuous)
prob += total_export == lpSum([export[i] for i in idx])
prob += total_export >= 5000

# Solve problem
prob.solve()

# And print some output
print (("Status:"), LpStatus[prob.status])  #-----------------------
print ("Total Cost is = ", value(prob.objective))
print ("Capacity PV = ", value(CPV))
print ("Capacity WT = ", value(CWT))
print ("Capacity Batt = ", value(CBatt))
print ("Total export = ", value(total_export))