本文介绍了Python PuLP初学者约束列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的初学者.

I am a beginner in Python.

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

I am using PuLP to solve a minimization problem for a Renewable Energy System (PV + Wind + Battery).

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

My system uses timesteps (24h, per hour). My question is how can I create a list in the constraints that collects the state of charge of the battery, as well as the inlet and outlet of it (variables that change value each timestep). In mathlab I have seen people use "eye", but here in PuLP I am not sure how can be done.

任何帮助将不胜感激.

这是我拥有的代码的一部分(我知道这是不正确的).

Here is part of the code I have (which I know, is not correct).

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

先谢谢您.

推荐答案

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

Below is a version of your code which I think solves the problem you are trying to formulate in your code above. It prints the following out:

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

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

A few things you'll probably want to think about if you haven't already -

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

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

这篇关于Python PuLP初学者约束列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 22:14