'Python Optimization for a constrained battery charging schedule
This is related to the following question: Constrained Optimization of battery scheduling in microgrid
I am trying to run a slightly modified version of this program. It works well for the case in the question above, but I start running into issues after my modifications. I have added the code I used below:
import pyomo.environ as pyomo
import numpy as np
import pandas as pd
import csv
# create model
m = pyomo.ConcreteModel()
# Problem DATA
T = 1440
Zmin = 18.2
Zmax = 72.8
Qmin = -30.0
Qmax = 30.0
# Generate prices, solar output and load signals
P = np.genfromtxt('pricing_data.csv',delimiter=',')
S = np.genfromtxt('solar_data.csv',delimiter=',')
L = np.genfromtxt('tues_load.csv',delimiter=',')
# Indexes
times = range(T)
times_plus_1 = range(T+1)
# Decisions variables
m.Q = pyomo.Var(times, domain=pyomo.Reals)
m.Z = pyomo.Var(times_plus_1, domain=pyomo.NonNegativeReals)
# objective
cost = sum(P[t]*(L[t] - S[t] - m.Q[t]) for t in times)
m.cost = pyomo.Objective(expr = cost, sense=pyomo.minimize)
# constraints
m.cons = pyomo.ConstraintList()
m.cons.add(m.Z[0] == 0.5*(Zmin + Zmax))
for t in times:
m.cons.add(pyomo.inequality(Qmin, m.Q[t], Qmax))
m.cons.add(pyomo.inequality(Zmin, m.Z[t], Zmax))
m.cons.add(m.Z[t+1] == m.Z[t] - m.Q[t])
m.cons.add(L[t] - S[t] - m.Q[t] >= 0)
# solve
solver = pyomo.SolverFactory('cbc')
solver.solve(m)
# display results
print("Total cost =", m.cost(), ".")
for v in m.component_objects(pyomo.Var, active=True):
print ("Variable component object",v)
print ("Type of component object: ", str(type(v))[1:-1]) # Stripping <> for nbconvert
varobject = getattr(m, str(v))
print ("Type of object accessed via getattr: ", str(type(varobject))[1:-1])
for index in varobject:
print (" ", index, varobject[index].value)
The error message I get when I try to run it is:
Exception has occurred: ValueError
No value for uninitialized NumericValue object Q[0]
File "C:\Users\shash\Desktop\pyomo optim.py", line 50, in <module>
print("Total cost =", m.cost(), ".")
The data I am using is just basic load//solarprice data, a single column csv with no headers. How could I fix this issue? Additionally, I am actually trying to also obtain times for when the battery should charge/discharge, so how could I modify this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
