Gekko and Intermediate variables
Question:
I am using GEKKO to fit a function.
The form of a function is known.
It looks like a sum of similar subfunctions with various parameters,
each subfunction has its own set of parameters to find (optimize)…
I don’t think I understand the use of intermediate variables fully and would love some help with my code.
I am using intermediate variables to minimize the code and make it more readable.
I got an error:
apm 212.160.70.179_gk_model1 <br><pre> ----------------------------------------------------------------
APMonitor, Version 1.0.1
APMonitor Optimization Suite
----------------------------------------------------------------
@error: Intermediate Definition
Error: Intermediate variable with no equality (=) expression
30.124819430.1605366830.1963387730.2322259530.2681985430.30425683
STOPPING...
As I understood I am getting some array as output?
How can I understand which Intermediate variable causes a problem?
Or what am I doing wrong?
Here is the code:
# using GEKKO for preliminary fitting
xData = np.array(df['E'])
yData = np.array(df['exp_trans'])
m = GEKKO()
# parameters
x = m.Param(value = xData) # x-coordinates for fitting
z = m.Param(value = yData) # experimental results to fit to
# constants
A1 = m.Const(A1_c)
gj = m.Const(gj_c)
# variables
El_0 = m.FV(lb = borders_left[0], ub=borders_right[0])
El_1 = m.FV(lb = borders_left[1], ub=borders_right[1])
El_2 = m.FV(lb = borders_left[2], ub=borders_right[2])
G1_0 = m.FV(lb = 0.000001, ub=1)
G1_1 = m.FV(lb = 0.000001, ub=1)
G1_2 = m.FV(lb = 0.000001, ub=1)
# G2 = 0
G2_0 = m.FV(lb = 0.000000, ub=0)
G2_1 = m.FV(lb = 0.000000, ub=0)
G2_2 = m.FV(lb = 0.000000, ub=0)
Gg_0 = m.FV(lb = 0.000001, ub=1)
Gg_1 = m.FV(lb = 0.000001, ub=1)
Gg_2 = m.FV(lb = 0.000001, ub=1)
#Intermediates
k_alfa = m.Intermediate(A1*np.sqrt(x))
ro = m.Intermediate(k_alfa*ac)
G_0 = m.Intermediate(G1_0+G2_0+Gg_0)
G_1 = m.Intermediate(G1_1+G2_1+Gg_1)
G_2 = m.Intermediate(G1_2+G2_2+Gg_2)
d0 = m.Intermediate((El_0-x)**2+(G_0/2)**2)
d1 = m.Intermediate((El_1-x)**2+(G_1/2)**2)
d2 = m.Intermediate((El_2-x)**2+(G_2/2)**2)
phi = m.Intermediate(ro)
f0=m.Intermediate((1-(1-(G_0*G1_0/(2*d0)))*m.cos(2*phi)-((El_0-x)*G1_0/d0)*m.sin(2*phi)))
f1=m.Intermediate((1-(1-(G_1*G1_1/(2*d1)))*m.cos(2*phi)-((El_1-x)*G1_1/d1)*m.sin(2*phi)))
f2=m.Intermediate((1-(1-(G_2*G1_2/(2*d2)))*m.cos(2*phi)-((El_2-x)*G1_2/d2)*m.sin(2*phi)))
sigma_sum = m.Intermediate(2*math.pi*gj/k_alfa * (f0+f1+f2))
# designing an equation for the model
y = m.Var()
m.Equation(y == m.exp(-n*sigma_sum))
m.Minimize(((y-z))**2)
# Options
El_0.STATUS = 1
El_1.STATUS = 1
El_2.STATUS = 1
G1_0.STATUS = 1
G1_1.STATUS = 1
G1_2.STATUS = 1
G2_0.STATUS = 1
G2_1.STATUS = 1
G2_2.STATUS = 1
Gg_0.STATUS = 1
Gg_1.STATUS = 1
Gg_2.STATUS = 1
m.options.IMODE = 2
m.options.SOLVER = 3
m.options.MAX_ITER = 1000
m.solve(disp=1)
Answers:
The problem with the code was connected with the use of various data types representation
e.g.
k_alfa = m.Intermediate(A1*np.sqrt(x))
need to be changed on:
k_alfa = m.Intermediate(A1*m.sqrt(x))
and so on..
Because functions vary in output datatypes.
Always check and be aware of using various data types & structures in your models and variables definitions.
I am using GEKKO to fit a function.
The form of a function is known.
It looks like a sum of similar subfunctions with various parameters,
each subfunction has its own set of parameters to find (optimize)…
I don’t think I understand the use of intermediate variables fully and would love some help with my code.
I am using intermediate variables to minimize the code and make it more readable.
I got an error:
apm 212.160.70.179_gk_model1 <br><pre> ----------------------------------------------------------------
APMonitor, Version 1.0.1
APMonitor Optimization Suite
----------------------------------------------------------------
@error: Intermediate Definition
Error: Intermediate variable with no equality (=) expression
30.124819430.1605366830.1963387730.2322259530.2681985430.30425683
STOPPING...
As I understood I am getting some array as output?
How can I understand which Intermediate variable causes a problem?
Or what am I doing wrong?
Here is the code:
# using GEKKO for preliminary fitting
xData = np.array(df['E'])
yData = np.array(df['exp_trans'])
m = GEKKO()
# parameters
x = m.Param(value = xData) # x-coordinates for fitting
z = m.Param(value = yData) # experimental results to fit to
# constants
A1 = m.Const(A1_c)
gj = m.Const(gj_c)
# variables
El_0 = m.FV(lb = borders_left[0], ub=borders_right[0])
El_1 = m.FV(lb = borders_left[1], ub=borders_right[1])
El_2 = m.FV(lb = borders_left[2], ub=borders_right[2])
G1_0 = m.FV(lb = 0.000001, ub=1)
G1_1 = m.FV(lb = 0.000001, ub=1)
G1_2 = m.FV(lb = 0.000001, ub=1)
# G2 = 0
G2_0 = m.FV(lb = 0.000000, ub=0)
G2_1 = m.FV(lb = 0.000000, ub=0)
G2_2 = m.FV(lb = 0.000000, ub=0)
Gg_0 = m.FV(lb = 0.000001, ub=1)
Gg_1 = m.FV(lb = 0.000001, ub=1)
Gg_2 = m.FV(lb = 0.000001, ub=1)
#Intermediates
k_alfa = m.Intermediate(A1*np.sqrt(x))
ro = m.Intermediate(k_alfa*ac)
G_0 = m.Intermediate(G1_0+G2_0+Gg_0)
G_1 = m.Intermediate(G1_1+G2_1+Gg_1)
G_2 = m.Intermediate(G1_2+G2_2+Gg_2)
d0 = m.Intermediate((El_0-x)**2+(G_0/2)**2)
d1 = m.Intermediate((El_1-x)**2+(G_1/2)**2)
d2 = m.Intermediate((El_2-x)**2+(G_2/2)**2)
phi = m.Intermediate(ro)
f0=m.Intermediate((1-(1-(G_0*G1_0/(2*d0)))*m.cos(2*phi)-((El_0-x)*G1_0/d0)*m.sin(2*phi)))
f1=m.Intermediate((1-(1-(G_1*G1_1/(2*d1)))*m.cos(2*phi)-((El_1-x)*G1_1/d1)*m.sin(2*phi)))
f2=m.Intermediate((1-(1-(G_2*G1_2/(2*d2)))*m.cos(2*phi)-((El_2-x)*G1_2/d2)*m.sin(2*phi)))
sigma_sum = m.Intermediate(2*math.pi*gj/k_alfa * (f0+f1+f2))
# designing an equation for the model
y = m.Var()
m.Equation(y == m.exp(-n*sigma_sum))
m.Minimize(((y-z))**2)
# Options
El_0.STATUS = 1
El_1.STATUS = 1
El_2.STATUS = 1
G1_0.STATUS = 1
G1_1.STATUS = 1
G1_2.STATUS = 1
G2_0.STATUS = 1
G2_1.STATUS = 1
G2_2.STATUS = 1
Gg_0.STATUS = 1
Gg_1.STATUS = 1
Gg_2.STATUS = 1
m.options.IMODE = 2
m.options.SOLVER = 3
m.options.MAX_ITER = 1000
m.solve(disp=1)
The problem with the code was connected with the use of various data types representation
e.g.
k_alfa = m.Intermediate(A1*np.sqrt(x))
need to be changed on:
k_alfa = m.Intermediate(A1*m.sqrt(x))
and so on..
Because functions vary in output datatypes.
Always check and be aware of using various data types & structures in your models and variables definitions.