Chris
Chris

Reputation: 41

I have a question about Python optmization

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              
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...

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

Object Function - min(SIGMA(TOU∈tt) * F) = min((load[tt]-g[tt,v]) * TOU[tt]) = min((time of load - time of ESS) * time of TOU)

constraints_1 - eq_ES = ESS_S[tt-1,0] + (ESS_c * 0.98 - ESS_d/0.90) = initial of ESS SOC + (ESS_charging * 0.98 - ESS_discharging/0.90)

constraints_2 - eq_total = (c[tt,0] + ESS_d[tt,0]) >= (load[tt] + ESS_c[tt,0]) = (time of Num_EV + time of ESS discharging) >= (time of load + time of ESS charging)

Upvotes: 2

Views: 80

Answers (2)

John Hedengren
John Hedengren

Reputation: 14401

There are a couple issues as @AirSquid correctly identified:

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

There is another problem with the nested for loops because NUM_ESS=1 so the range(1,Num_ESS) is empty with range(1,1) and it doesn't cycle through the second loop.

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

Another problem is with the right-hand side (RHS) of this equation with eq_ESS[tt,0]:

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]

The RHS should not be an equation. I subtituted this with ESS_s[0,0] just as a placeholder, but it should be fixed based on your problem statement. Here is a complete version that solves successfully, although it is likely the incorrect solution until the equation with ESS_s[0,0] is corrected.

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)

Solution report:

 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
 ---------------------------------------------------

This problem looks similar to others.

It looks like a common benchmark problem or else an assignment from a course. I encourage you to get your version of the program working, independent of those other answers.

Upvotes: 1

AirSquid
AirSquid

Reputation: 11938

I'm not too well versed in GEKKO constructs, so take this w/ a grain of salt, but your method of adding equations seems odd and overly complicated. I think in pre-loading one of your lists with zeros you are failing to over-write it with a generated equation.

The error above shows the offending "equation" as [0.]. Meaning that in one of the lists you have an extra zero that GEKKO barfs on when you are making an equation from lists. This is suspect:

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)

You are making a list of zeros t in length and then your loop is t-1 in length as range(1, t) only iterates t-1 times. You might try just appending the list instead of making a zero-loaded shell, or just adding the equations individually to GEKKO model.

Also, I suspect that you need to change your equation addition (if using lists) to m.Equations() with an s.

Upvotes: 2

Related Questions