Python PuLP Beginner Constraint lists

孤者浪人 提交于 2019-12-11 04:46:51

问题


I am a beginner in Python.

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

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.

Any help would be very well appreciated.

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

Thank you in advance.


回答1:


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 -

  • Are your units consistent? (I can't tell without knowing where your data has come from)
  • Do you need to consider battery charge/discharge losses?
  • Do you want to leave the battery with the same amount of energy with which it starts? (important if the data is for a 'typical' day)

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


来源:https://stackoverflow.com/questions/47441934/python-pulp-beginner-constraint-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!