我有一个关于Python优化的问题

jaql4c8m  于 2023-01-14  发布在  Python
关注(0)|答案(2)|浏览(111)

我目前正在使用Python。然而,我正在与一个错误作斗争。这是我到目前为止制作的工具。

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

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

Num_ESS = 1
Num_EV  = 1
TOU     = [76.3,76.3,76.3,76.3,76.3,76.3,76.3,76.3,164.0,251.2,251.2,164.0,251.2,251.2,251.2,251.2,164.0,164.0,164.0,164.0,164.0,164.0,164.0,76.3]
t       = len(TOU)

#setting 
load     = [510,530,516,510,515,544,646,686,741,734,748,760,754,700,686,720,714,761,727,714,618,584,578,544]

c     = m.Array(m.Var, (t,Num_EV))      
EV_s  = m.Array(m.Var, (t,Num_EV))      

for tt in range(t,Num_EV):
    c[tt,0].lower = 0; EV_s[tt,0].lower = 20
    c[tt,0].upper = 7; EV_s[tt,0].upper = 80

g     = m.Array(m.Var, (t,Num_ESS))     
ESS_s = m.Array(m.Var, (t,Num_ESS))    
ESS_c = m.Array(m.Var, (t,Num_ESS))     
ESS_d = m.Array(m.Var, (t,Num_ESS))    

for tt in range(t,Num_ESS):
    ESS_s[tt,0].lower =   0; ESS_c[tt,0].lower =  0; ESS_d[tt,0].lower =  0
    ESS_s[tt,0].upper = 300; ESS_c[tt,0].upper = 50; ESS_d[tt,0].upper = 50

#constraints
ESS_s[0,0] = 300

eq_ESS = np.zeros((t,1))
eq_ESS = list(eq_ESS)

for tt in range(1, t):
    for e in range(1,Num_ESS):
        eq_ESS[tt] = ESS_s[tt-1,0] + (ESS_c[tt,0]*0.98 - ESS_d[tt,0]/0.95) == eq_ESS[tt,0]

m.Equation(eq_ESS)

eq_total = np.zeros((t,1))
eq_total = list(eq_total)

for tt in range(1, t):
    for e in range(1,Num_ESS):
        eq_total[tt] = (g[tt,0] + ESS_d[tt,0]) >= (load[tt] + ESS_c[tt,0])

m.Equation(eq_total)

#Object Function
F = np.zeros((t*Num_ESS))
F = F.tolist()

for tt in range(1,t):
    for v in range(1,Num_ESS):
        F = m.Obj((load[tt]-g[tt])*TOU[tt])

m.solve(disp=True)

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

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

Num_ESS = 1
Num_EV  = 1
TOU     = [76.3,76.3,76.3,76.3,76.3,76.3,76.3,76.3,164.0,251.2,251.2,164.0,251.2,251.2,251.2,251.2,164.0,164.0,164.0,164.0,164.0,164.0,164.0,76.3]
t       = len(TOU)

#setting 
load     = [510,530,516,510,515,544,646,686,741,734,748,760,754,700,686,720,714,761,727,714,618,584,578,544]

c     = m.Array(m.Var, (t,Num_EV))      
EV_s  = m.Array(m.Var, (t,Num_EV))      

for tt in range(t,Num_EV):
    c[tt,0].lower = 0; EV_s[tt,0].lower = 20
    c[tt,0].upper = 7; EV_s[tt,0].upper = 80

g     = m.Array(m.Var, (t,Num_ESS))     
ESS_s = m.Array(m.Var, (t,Num_ESS))    
ESS_c = m.Array(m.Var, (t,Num_ESS))     
ESS_d = m.Array(m.Var, (t,Num_ESS))    

for tt in range(t,Num_ESS):
    ESS_s[tt,0].lower =   0; ESS_c[tt,0].lower =  0; ESS_d[tt,0].lower =  0
    ESS_s[tt,0].upper = 300; ESS_c[tt,0].upper = 50; ESS_d[tt,0].upper = 50

#constraints
ESS_s[0,0] = 300

eq_ESS = np.zeros((t,1))
eq_ESS = list(eq_ESS)

for tt in range(1, t):
    for e in range(1,Num_ESS):
        eq_ESS[tt] = ESS_s[tt-1,0] + (ESS_c[tt,0]*0.98 - ESS_d[tt,0]/0.95) == eq_ESS[tt,0]

m.Equation(eq_ESS)

eq_total = np.zeros((t,1))
eq_total = list(eq_total)

for tt in range(1, t):
    for e in range(1,Num_ESS):
        eq_total[tt] = (g[tt,0] + ESS_d[tt,0]) >= (load[tt] + ESS_c[tt,0])

m.Equation(eq_total)

#Object Function
F = np.zeros((t*Num_ESS))
F = F.tolist()

for tt in range(1,t):
    for v in range(1,Num_ESS):
        F = m.Obj((load[tt]-g[tt])*TOU[tt])

m.solve(disp=True)
The error codes for this program are:

apm 211.220.231.231_gk_model3 <br><pre> ----------------------------------------------------------------
 APMonitor, Version 1.0.1
 APMonitor Optimization Suite
 ----------------------------------------------------------------
 
 
 Warning: there is insufficient data in CSV file 211.220.231.231_gk_model3.csv
 @error: Equation Definition
 Equation without an equality (=) or inequality (>,<)
 [0.]
 STOPPING...
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
Cell In[16], line 66
     63     for v in range(1,Num_ESS):
     64         F = m.Obj((load[tt]-g[tt])*TOU[tt])
---> 66 m.solve(disp=True)

File c:\Users\6408-01\AppData\Local\Programs\Python\Python310\lib\site-packages\gekko\gekko.py:2185, in GEKKO.solve(self, disp, debug, GUI, **kwargs)
   2183 #print APM error message and die
   2184 if (debug >= 1) and ('@error' in response):
-> 2185     raise Exception(response)
   2187 #load results
   2188 def byte2str(byte):

Exception:  @error: Equation Definition
 Equation without an equality (=) or inequality (>,<)
 [0.]
 STOPPING...

我想知道这个问题。下面是对约束和目标函数的描述。
目标函数-min(∑(TOU ∈ tt)* F)= min((负载[tt]-g [tt,v])* TOU [tt])= min((负载时间-ESS时间)* TOU时间)
约束条件_1-当量_ES = ESS_S [tt-1,0]+(ESS_c * 0.98-ESS_d/0.90)= ESS荷电状态的初始值+(ESS_充电 * 0.98-ESS_放电/0.90)
约束_2-eq_total =(c [tt,0]+ ESS_d [tt,0])〉=(负载[tt]+ ESS_c [tt,0])=(Num_EV的时间+ESS放电的时间)〉=(负载的时间+ESS充电的时间)

km0tfn4u

km0tfn4u1#

我对GEKKO的构造不是很精通,所以对此持保留态度,但是你添加方程的方法看起来很奇怪,而且过于复杂。我认为在用零预加载一个列表时,你没有用生成的方程覆盖它。
上面的错误显示了令人不快的“等式”[0.]。这意味着在其中一个列表中,当你从列表中生成等式时,GEKKO会吐出一个额外的零。这是可疑的:

ESS_s[0,0] = 300

eq_ESS = np.zeros((t,1))
eq_ESS = list(eq_ESS)

for tt in range(1, t):
    for e in range(1,Num_ESS):
        eq_ESS[tt] = ESS_s[tt-1,0] + (ESS_c[tt,0]*0.98 - ESS_d[tt,0]/0.95) == eq_ESS[tt,0]

m.Equation(eq_ESS)

您正在创建一个长度为t的零列表,然后您的循环长度为t-1,因为range(1, t)只迭代t-1次。您可以尝试仅追加列表而不是创建零加载 shell ,或者仅将方程单独添加到GEKKO模型中。
此外,我怀疑您需要将等式加法(如果使用列表)更改为带有sm.Equations()

0yycz8jy

0yycz8jy2#

有几个问题@AirSquid正确识别:

#ESS_s[0,0] = 300  # use equation, ESS_s[0,0] is a variable
m.Equation(ESS_s[0,0] == 300)

嵌套的for循环还有另一个问题,因为NUM_ESS=1,所以range(1,Num_ESS)是空的,range(1,1),它不会循环通过第二个循环。

for tt in range(1, t):
    #for e in range(1,Num_ESS):
    for e in range(Num_ESS):

另一个问题是关于eq_ESS[tt,0]的方程的右手边(RHS):

for tt in range(1, t):
    for e in range(Num_ESS):
        eq_ESS[tt] = ESS_s[tt-1,e] + (ESS_c[tt,e]*0.98 \
                                      - ESS_d[tt,e]/0.95) \
                     ESS_s[0,0]
                     #== eq_ESS[tt,0]

RHS不应该是一个方程。我用ESS_s[0,0]代替了它,只是作为一个占位符,但是它应该根据你的问题陈述来修复。这里是一个成功求解的完整版本,尽管在ESS_s[0,0]的方程被纠正之前,它可能是不正确的解。

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

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

Num_ESS = 1
Num_EV  = 1
TOU     = [76.3,76.3,76.3,76.3,76.3,76.3,76.3,76.3,\
           164.0,251.2,251.2,164.0,251.2,251.2,251.2,251.2,\
           164.0,164.0,164.0,164.0,164.0,164.0,164.0,76.3]
t       = len(TOU)

#setting 
load     = [510,530,516,510,515,544,646,686,741,734,748,760,\
            754,700,686,720,714,761,727,714,618,584,578,544]

c     = m.Array(m.Var, (t,Num_EV))      
EV_s  = m.Array(m.Var, (t,Num_EV))      

for tt in range(t,Num_EV):
    c[tt,0].lower = 0; EV_s[tt,0].lower = 20
    c[tt,0].upper = 7; EV_s[tt,0].upper = 80

g     = m.Array(m.Var, (t,Num_ESS))     
ESS_s = m.Array(m.Var, (t,Num_ESS))
ESS_c = m.Array(m.Var, (t,Num_ESS))     
ESS_d = m.Array(m.Var, (t,Num_ESS))    

for tt in range(t,Num_ESS):
    ESS_s[tt,0].lower =  0; ESS_s[tt,0].upper = 300
    ESS_c[tt,0].lower =  0; ESS_c[tt,0].upper = 50
    ESS_d[tt,0].lower =  0; ESS_d[tt,0].upper = 50

#constraints
m.Equation(ESS_s[0,0] == 300)

eq_ESS = np.zeros((t,1))
eq_ESS = list(eq_ESS)

for tt in range(1, t):
    for e in range(Num_ESS):
        eq_ESS[tt] = ESS_s[tt-1,e] + (ESS_c[tt,e]*0.98 \
                                      - ESS_d[tt,e]/0.95) \
                     == ESS_s[0,0]

m.Equations(eq_ESS[1:])

eq_total = np.zeros((t,1))
eq_total = list(eq_total)

for tt in range(1, t):
    for e in range(Num_ESS):
        eq_total[tt] = (g[tt,0] + ESS_d[tt,0]) \
                        >= (load[tt] + ESS_c[tt,0])

m.Equations(eq_total[1:])

#Object Function
F = np.zeros((t*Num_ESS))
F = F.tolist()

for tt in range(1,t):
    for v in range(1,Num_ESS):
        F = m.Obj((load[tt]-g[tt])*TOU[tt])

m.solve(disp=True)

解决方案报告:

Number of state variables:    167
 Number of total equations: -  47
 Number of slack variables: -  23
 ---------------------------------------
 Degrees of freedom       :    97
 
 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------
 
 Iter    Objective  Convergence
    0  1.61811E-09  7.61000E+02
    1  0.00000E+00  2.22045E-16
    2  0.00000E+00  2.22045E-16
 Successful solution
 
 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :  0.015699999999999992 sec
 Objective      :  0.
 Successful solution
 ---------------------------------------------------

这个问题看起来与其他问题相似。

它看起来像是一个常见的基准问题,或者是一门课程的作业。我鼓励你让你的程序版本工作,独立于那些其他的答案。

相关问题