You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using pulp with standard solver to plan when to import/export electricity and store in in a battery for later usage or export.
The optimisation should maximise ( export*sellprice - import * buyprice). This works fine as long as all prices are positive but tomorrow there are a number of hours in sequences with slightly negative prices and then the optimisation fails (although it reports still as optimal). The import is done on one of the hours with neagtive price and then on hours with positive price.
If I add tax to the prices the optimisation shifts correctly to the hours with lowest prices. Also if I round prices smaller than 0.001 to 0 it works well.
Here is an example of the output with wrong optimisation. The import of electricity and charging of the batterij should start at 03:00 (the yellow line) instead of 04:00.
here is my code:
`def LPoptimization():
nrIntervals = len(priceList)
# BATTERY PARAMETERS
Effcharge = onewayEff
Effdischarge = onewayEff
# LP PROBLEM
prob = pulp.LpProblem("Battery_Optimization", pulp.LpMaximize)
# VARIABLES
chargekWh = pulp.LpVariable.dicts("charge", range(nrIntervals), lowBound=0, upBound=maxChargeSpeed) # this is indirect charge from PV not connected directly
dischargekWh = pulp.LpVariable.dicts("discharge", range(nrIntervals), lowBound=0, upBound=maxDischargeSpeed)
sockWh = pulp.LpVariable.dicts("soc", range(nrIntervals), lowBound=int(float(minBatterySOCPct/100*ratedBatteryCapacity)), upBound=ratedBatteryCapacity)
importkWh = pulp.LpVariable.dicts("import", range(nrIntervals), lowBound=0)
exportkWh = pulp.LpVariable.dicts("export", range(nrIntervals), lowBound=0)
costsEuro = pulp.LpVariable.dicts("costs", range(nrIntervals))
# Indices for field in priceList
forecastDirectIndex=4
forecastIndirectIndex=5
forecastUsageIndex=6
buyPriceIndex=7
sellPriceIndex=8
# OBJECTIVE
prob += pulp.lpSum(
priceList[t][sellPriceIndex]/1000 * exportkWh[t] - priceList[t][buyPriceIndex]/1000 * importkWh[t] # - dischargekWh[t]*0.052
for t in range(nrIntervals)
)
# CONSTRAINTS
for t in range(nrIntervals):
# Energy balance , note could remove priceList[t][forecastDirectindex] on both sides
prob += (
priceList[t][forecastDirectIndex] + priceList[t][forecastIndirectIndex] + importkWh[t] + dischargekWh[t]
==
priceList[t][forecastUsageIndex] + exportkWh[t] + chargekWh[t] + priceList[t][forecastDirectIndex]
)
# Charge / discharge limits
if hourAvgPlanning:
prob += chargekWh[t] <= maxChargeSpeed
prob += dischargekWh[t] <= maxDischargeSpeed
else:
prob += chargekWh[t] <= maxChargeSpeed/4
prob += dischargekWh[t] <= maxDischargeSpeed/4
# SOC evolution
if t == 0:
prob += sockWh[t] == initialCharge + priceList[t][forecastDirectIndex]+Effcharge * chargekWh[t] - dischargekWh[t] / Effdischarge
else:
prob += sockWh[t] == sockWh[t-1] + priceList[t][forecastDirectIndex]+Effcharge * chargekWh[t] - dischargekWh[t] / Effdischarge
costsEuro[t]=priceList[t][sellPriceIndex]/1000 * exportkWh[t] - priceList[t][buyPriceIndex]/1000 * importkWh[t]
if zeroGridCharge:
prob += importkWh[t]==0
# SOLVE
prob.solve(pulp.PULP_CBC_CMD(msg=False))
optimisationStatus=pulp.LpStatus[prob.status]
# OUTPUT
schedule = []
for t in range(nrIntervals):
schedule.append({
"interval": t,
"charge": int(chargekWh[t].value()),
"discharge": int(dischargekWh[t].value()),
"soc": int(sockWh[t].value()),
"import": int(importkWh[t].value()),
"export": int(exportkWh[t].value()),
"costs" : int(costsEuro[t].value()*10000)/10000
})
return optimisationStatus,schedule
I just got a tip from someone to not divide the price by 1000 in the optimisation objective and indeed, with that it also works fine. Here is the output without the factor 1000 (above) and with the factor 1000.
So I could close the issue but would still be interested in comments as to the reson why.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I am using pulp with standard solver to plan when to import/export electricity and store in in a battery for later usage or export.
The optimisation should maximise ( export*sellprice - import * buyprice). This works fine as long as all prices are positive but tomorrow there are a number of hours in sequences with slightly negative prices and then the optimisation fails (although it reports still as optimal). The import is done on one of the hours with neagtive price and then on hours with positive price.
If I add tax to the prices the optimisation shifts correctly to the hours with lowest prices. Also if I round prices smaller than 0.001 to 0 it works well.
Here is an example of the output with wrong optimisation. The import of electricity and charging of the batterij should start at 03:00 (the yellow line) instead of 04:00.

here is my code:
`def LPoptimization():
`
All reactions