I have a linear problem where I want to maximize my revenue, by minimizing the objective function.
The constraints are linear thus linear programming is suitable for my problem. I am new in python and linear programming, so I have formulated the following code based on what I've read online.
The inputs (energy, spot, fcr, gridE) are vectors with length = 8760.
My code is not running properly but it doesn't give me a specific error.
I think the problem might be the way I've set the variables. I would like my variables (Pcap, Pcharge) to be continuous values (within "0-10"), (Pdis within "-10 - 0") and (SOC within "12 - 36") so that they don't violate the constraints I've defined.
Any advice would be more than welcome.
Code:
The constraints are linear thus linear programming is suitable for my problem. I am new in python and linear programming, so I have formulated the following code based on what I've read online.
The inputs (energy, spot, fcr, gridE) are vectors with length = 8760.
My code is not running properly but it doesn't give me a specific error.
I think the problem might be the way I've set the variables. I would like my variables (Pcap, Pcharge) to be continuous values (within "0-10"), (Pdis within "-10 - 0") and (SOC within "12 - 36") so that they don't violate the constraints I've defined.
Any advice would be more than welcome.
Code:
import numpy as np
import time
import pandas
from pulp import *
import pulp as plp
opt_model = plp.LpProblem(name="MIP Model")
tic=time.time()
year="2015"
ResultFolderName = "results/"
energyData= "data"+year+"/energyy.csv"
spotData= "data"+year+"/spot.csv"
fcrData= "data"+year+"/fcr_2017.csv"
gridEData= "data"+year+"/gridE.csv"
chunk_size = 4
energyData = np.array(pandas.read_csv(energyData, sep=',', header=None).values)
energy_366 = energyData.reshape(-1, chunk_size, energyData.shape[1]).sum(1)
energy_365 = energy_366[:8760]
# Define inputs
energy = list(energy_365)
spot = list(pandas.read_csv(spotData, sep=',', header=None).values)
fcr = list(pandas.read_csv(fcrData, sep=',', header=None).values)
gridE = list(pandas.read_csv(gridEData, sep=',', header=None).values)
h = 0.9
Pmax = 10
Q = 40
#Define variables
Pcap = [plp.LpVariable("Pcap",i,0,cat='Continuous') for i in range(0, 8760)]
Pcharge = [plp.LpVariable("Pcharge",i,0,cat='Continuous') for i in range(0, 8760)]
Pdis = [plp.LpVariable("Pdis",i,0,cat='Continuous') for i in range(0, 8760)]
SOC = [plp.LpVariable("SOC",i,0,cat='Continuous') for i in range(0, 8760)]
SOC.insert(0, 20)
#define constraints
opt_model += lpSum([Pcap[i] for i in range(0, 8760)]) >= 0, "Pcap"
opt_model += lpSum([Pcharge[i] for i in range(0, 8760)]) >= 0, "Pcharge"
opt_model += lpSum([Pdis[i] for i in range(0, 8760)]) >= 0, "Pdis"
opt_model += 12 <= lpSum([SOC[i] for i in range(0, 8760)]) <= 36, "SOC"
opt_model += Pmax >= lpSum(Pcap[i]+Pcharge[i]+Pdis[i] for i in range(0, 8760)), "Pmax"
for i in range(0, 8759):
SOC[i + 1] = SOC[i] + (gridE[i] + Pcap[i] * energy[i] + Pcharge[i] * h - Pdis[i] * 1 / h) * 1 / Q
objective = plp.lpSum(Pcharge[i] * spot[i] - Pdis[i] * spot[i] - Pcap[i] * fcr[i])
# for minimization
opt_model.sense = plp.LpMinimize
opt_model.setObjective(objective)
# solving with CBC
opt_model.solve()
