简体   繁体   English

我想知道python gekko优化解决

[英]I want to know python gekko optimization solve

I am currently using Python. However, I am struggling with one error.我目前使用的是 Python。但是,我遇到了一个错误。 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. st 是约束。 First constraint is EV SOC range.第一个约束是 EV SOC 范围。 EV SOC minimum is 30 and Maxmium is 70. EV SOC form is (inital SOC + time by EV SOC). EV SOC 最小值为 30,Maxmium 为 70。EV SOC 形式为(初始 SOC + EV SOC 时间)。 Second constraint is EV Charging range.第二个限制是 EV 充电范围。 EV Charging range is from 0 to 7. Finally, Object function is to minimize the product of tou and charging rate. EV Charging范围从0到7。最后,Object function是为了最小化tou和charging rate的乘积。

There are a few problems with the model that can be observed by opening the model file in the run directory.通过打开运行目录中的 model 文件可以观察到 model 存在一些问题。 Use m.open_folder() and open the gk_model0.apm file with a text editor.使用m.open_folder()并使用文本编辑器打开gk_model0.apm文件。 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: True表达式是因为一个常量在第一个循环中用另一个常量求值:

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.这给出了 Boolean True结果。

The initial EV SOC should either be changed to fixed or else include a simple equation:初始 EV SOC 应更改为fixed的或包含一个简单的方程式:

# 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.索引更改为p_j[tt,0] >= p_j[tt,i]因此p_j[tt,0] >= p_j[tt,0]不作为方程包含在内。 Should the range for time also be adjusted here to start at 1 ?时间范围是否也应在此处调整为从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?这是这个问题的解决方案: 如何使用 Gekko 优化电动汽车充电成本? It looks like you may be working on a similar problem.看起来您可能正在处理类似的问题。

Here are additional similar questions:以下是其他类似问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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