简体   繁体   中英

I want to know python gekko optimization solve

I am currently using Python. However, I am struggling with one error. This is the tool I have made so far.

from gekko import GEKKO
import numpy as np         

m = GEKKO(remote=False)     
m.options.SOLVER = 1        


hour = 24                  
Num_EV = 1                  

p_i =m.Array(m.Var,(hour,Num_EV)) 

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)

# EV min/Max setting
for tt in range(0,hour):
    p_i[tt,0].lower = 30
    p_i[tt,0].upper = 70

# EV Charger min/Max setting
Num_EV_C = 1
p_j = m.Array(m.Var, (hour, Num_EV_C))

for tt in range(0,hour):
    p_j[tt,0].lower = 0
    p_j[tt,0].upper = 7

# s.t : EV SOC
p_i[0,0] = 30              # inital EV SOC

eq_EV_SOC = np.zeros((hour,1))   
eq_EV_SOC = list(eq_EV_SOC)          

for tt in range(0,hour):     
    for i in range(0,Num_EV):
        eq_EV_SOC[tt] = p_i[tt-1,i] + p_i[tt,i] == p_i[tt,0]

m.Equation(eq_EV_SOC)

# s.t : EV charging rate
p_j[0,0] = 0

eq_EV_C = np.zeros((hour,1))  
eq_EV_C = list(eq_EV_C)          

for tt in range(0,hour):     
    for i in range(0,Num_EV_C):
        eq_EV_C[tt] = p_j[tt,0] >= p_j[tt,i]

m.Equation(eq_EV_C)

# Object Function : sum[i=n]*sum[t=T]()

F = np.zeros((hour*Num_EV)) 
F = F.tolist()              

for tt in range(0,hour):
    for i in range(0,Num_EV):
        F[i+tt*Num_EV] = p_i[tt,i] * p_j[tt,i]

F_Obj = m.sum(F)

m.Minimize(F_Obj)
m.solve(disp=True)
Exception: @error: Equation Definition
Equation without an equality (=) or inequality (>,<) true STOPPING...

I want to know this problem. Below is a description of constraints and objective functions.

st is constraint. First constraint is EV SOC range. EV SOC minimum is 30 and Maxmium is 70. EV SOC form is (inital SOC + time by EV SOC). Second constraint is EV Charging range. EV Charging range is from 0 to 7. Finally, Object function is to minimize the product of tou and charging rate.

There are a few problems with the model that can be observed by opening the model file in the run directory. Use m.open_folder() and open the gk_model0.apm file with a text editor. Here are some of the equations that indicate that there is a problem with the formulation:

True
v50>=v50
v51>=v51
v52>=v52
v53>=v53

The True expression is because a constant is evaluated with another constant in the first cycle of:

for tt in range(0,hour):     
    for i in range(0,Num_EV_C):
        eq_EV_C[tt] = p_j[tt,0] >= p_j[tt,i]

This gives a Boolean True result.

The initial EV SOC should either be changed to fixed or else include a simple equation:

# s.t : EV SOC
m.Equation(p_i[0,0]== 30) # inital EV SOC

# s.t : EV charging rate
m.Equation(p_j[0,0]==0)

It appears that the charging rate should decrease over time with this constraint:

m.Equation(p_j[0,0]==0)
for tt in range(0,hour):     
    for i in range(1,Num_EV_C):
        m.Equation(p_j[tt,0] >= p_j[tt,i])

The index is changed to p_j[tt,0] >= p_j[tt,i] so that p_j[tt,0] >= p_j[tt,0] is not included as an equation. Should the range for time also be adjusted here to start at 1 ?

for tt in range(1,hour):     
    for i in range(0,Num_EV):
        m.Equation(p_i[tt-1,i] + p_i[tt,i] == p_i[tt,0])

The problem is currently infeasible, even with these corrections. Maybe this problem can help:

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 is a solution to this question: How to optimize the electric vehicle charging cost using Gekko? It looks like you may be working on a similar problem.

Here are additional similar questions:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM