3
from gekko import GEKKO
import numpy as np              
import matplotlib.pyplot as plt 


m = GEKKO()         
m.options.SOLVER = 1 
m.options.IMODE = 3

Num_car = 1
TOU = [64.9,64.9,64.9,64.9,64.9,64.9,64.9,64.9,152.6,239.8,239.8,152.6,239.8,239.8,239.8,239.8,152.6,152.6,152.6,152.6,152.6,152.6,152.6,64.9]
n=len(TOU)
p_i = m.Array(m.Var,(n,Num_car))

input = m.Array(m.Var, (n), value = 0.0, lb = 0.0, ub = 7.0, integer = True)

SOC_t = m.Array(m.Var,(n, Num_car))

for tt in range(0,n):
    for i in range(0,Num_car):
            SOC_t[tt,i].lower = 30  
            SOC_t[tt,i].upper = 70  

SOC_t[0,0] = 30

eq_car_bat = np.zeros((n))
eq_car_bat = list(eq_car_bat)

for tt in range(0,n):
        eq_car_bat[tt] = SOC_t[tt] + input[tt] == p_i[tt]

m.Equation(eq_car_bat)

SOC_Max = 90
SOC_Min = 30
sum_soc = sum(SOC_t[tt])

eq_total = np.zeros((n))
eq_total = list(eq_total)

eq_total = sum_soc == SOC_Max

m.Equation(eq_total)

for i in range(n):
    m.Minimize(TOU[i]*p_i[i])

m.options.IMODE = 3
m.options.SOLVER = 1
m.solve(disp=True)

I want to find minimized charging costs when the EV charging. My code is shown below but, I received the following error "x must be a python list of GEKKO parameters, variables, or expressions" that I don't know how to solve.

tanjavaner
  • 40
  • 6
LAP1040400
  • 31
  • 3

1 Answers1

1

The intention appears to be to decide when to charge a battery over a 24 hour time period given a specific Time of Use (TOU). Here is a modified version of the script that charges (integer values 0-7) from 30 to 70 on the SOC. It has the capability to add additional vehicles but there is currently only one.

SOC

from gekko import GEKKO
import numpy as np              
import matplotlib.pyplot as plt 

m = GEKKO()         
m.options.SOLVER = 1 
m.options.IMODE = 3

Num_car = 1
TOU = [64.9,64.9,64.9,64.9,64.9,64.9,64.9,64.9,152.6,239.8,
       239.8,152.6,239.8,239.8,239.8,239.8,152.6,152.6,
       152.6,152.6,152.6,152.6,152.6,64.9]
n=len(TOU)
inp = m.Array(m.Var, (n), value = 0.0, 
                lb = 0.0, ub = 7.0, integer = True)

SOC_Min = 30; SOC_Max = 90
# set bounds 30-90
SOC_t = m.Array(m.Var,(n, Num_car),lb=SOC_Min,ub=SOC_Max)

# set new bounds 30-70
for tt in range(0,n):
    for j in range(Num_car):
        SOC_t[tt,j].lower = 30  
        SOC_t[tt,j].upper = 70  

for j in range(Num_car):
    # initial SOC
    m.Equation(SOC_t[0,j]==30)   # initial charge at start
    m.Equation(SOC_t[n-1,j]==70) # desired charge at end
    for tt in range(1,n):
        m.Equation(SOC_t[tt,j] == SOC_t[tt-1,j] + inp[tt])

for tt in range(n):
    m.Minimize(TOU[tt]*inp[tt])

m.options.IMODE = 3
m.options.SOLVER = 1
m.solve(disp=True)

plt.figure(figsize=(8,5))
plt.subplot(3,1,1)
for j in range(Num_car):
    p = np.empty(n)
    for tt in range(n):
        p[tt] = SOC_t[tt,j].value[0]
    plt.plot(p,'r.-',label='vehicle '+str(j+1))
plt.legend(); plt.ylabel('SOC'); plt.grid()

plt.subplot(3,1,2)
p = np.empty(n)
for tt in range(n):
    p[tt] = inp[tt].value[0]
plt.plot(p,'ko-',label='charge rate')
plt.legend(); plt.ylabel('charge'); plt.grid()

plt.subplot(3,1,3)
plt.plot(TOU,'bs-',label='electricity price')
plt.ylabel('price'); plt.grid()
plt.legend(); plt.xlabel('Time (hr)')
plt.tight_layout()
plt.savefig('soc_results.png',dpi=300)
plt.show()

This problem is related to some of the other problems in the Energy Benchmarks. Gekko has the capability to manage the time aspect of the problem when using IMODE=6 instead of indexing time explicitly. Using IMODE=3 (default) gives more control over the problem structure. IMODE=6 is better if there are differential equations.

John Hedengren
  • 12,068
  • 1
  • 21
  • 25