Skip to content Skip to sidebar Skip to footer

Python Pulp Beginner Constraint Lists

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 quest

Solution 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 Costis=34757.8679575668CapacityPV=7.9625771CapacityWT=383.65144CapacityBatt=3.3851294Totalexport=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.0for 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 outputprint (("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))

Post a Comment for "Python Pulp Beginner Constraint Lists"