ve1001
ve1001

Reputation: 183

Equation Definition Error (Equation without an equality or inequality) while using GEKKO MHE

I'm currently trying out the GEKKO MHE mode. I have two specified manipulated variables and controlled variables in the model, and one parameter that I'm looking to estimate via MHE. When I currently run the model, I get an equation definition error, saying that

Equation without an equality (=) or inequality (>,<) -267.25544516-267.28925105-267.21324717-267.21191109-264.56454462 STOPPING...

The model was initialized as:

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

n = 17

m = GEKKO(remote=False)

m.time = np.linspace(0,8,n)

c1_in_arr = np.load('c1_in_arr.npy')
c2_in_arr = np.load('c2_in_arr.npy')


V1_measured = np.load('V1_measured.npy')
V2_measured = np.load('V2_measured.npy')


#mmanipulated variables, feeding in the arrays for them
m.C1_in = m.MV(value=c1_in_arr)
m.C2_in = m.MV(value=c2_in_arr)


#estimated variables, feeding in the upper and lower bounds for them
m.C1_eff = m.FV(value = 0.98, lb = 0.95, ub = 1.0)

#controlled variables, feeding in the measurements for them
m.V1 = m.CV(value=V1_measured)
m.V2 = m.CV(value=V2_measured)


mdot_1 = m.Var()
mdot_2 = m.Var()


m.Equation(mdot_1== 1.52*m.C1_eff*m.C1_in)
m.Equation(mdot_2==-0.668*mdot_1 + 1.33*m.C1_eff*m.C2_in)

df_c = pd.read_csv('Values_C.csv',index_col=0)

Hhat_C1 = m.Var()
Hhat_C1 = m.Var()
M_m = 125
mdot_m = 75
mdot_s = 46

m.Equations([Hhat_C1 == -0.606 + 0.0057 * mdot_1,
             Hhat_C2 == -3.933 + 0.00096 * mdot_1])

C1_m = m.Var(value = 200)
C2_m = m.Var(value = 150)

m.Equations([C1_m.dt() == mdot_1 - C1_m/M_m*mdot_m,
             C2_m.dt() == mdot_2 - C2_m/M_s*mdot_s)

m.Equation(m.V1==0.8*C1_m/M_m)


m.Equation(m.V2 == 0.78*C1_m/C2_m)


m.options.IMODE = 5
#setting the solver settings to MHE

m.options.EV_TYPE = 1
#setting the solver for the MHE to calculate the parameters based on the sum of absolute errors

m.C1_in.STATUS = 0
m.C2_in.STATUS = 0
m.SiO2_in.STATUS = 0

m.C1_eff.STATUS = 1
m.V1.STATUS = 1
m.V2.STATUS = 1

m.C1_in.FSTATUS = 1
m.C2_in.FSTATUS = 1


m.C1_eff.FSTATUS = 0
m.V1.FSTATUS = 1
m.V2.FSTATUS = 1

m.C1_eff.DMAX = 1.0

m.V1.MEAS_GAP = 0.001
m.V2.MEAS_GAP = 0.001


m.open_folder() 
m.solve(disp = False)
   

When I open the GEKKO folder prior to solving, the infeasibilities file is also not present in the folder.

The model is able to run without error when the MVs and CVs are initialized as just the first variable of the "measurement" array

ex. m.C1_in = m.MV(value=c1_in_arr[0])

however, the provided parameter estimate is then incorrect.

I think that this error may be due to the way my MVs and CVs are being treated within the model. Is there a way to pinpoint which equation is causing this error, or if it's due to the MV/CV initialization?

Thank you!

Upvotes: 2

Views: 550

Answers (1)

John Hedengren
John Hedengren

Reputation: 14321

The problem is likely with using a Numpy array or Pandas dataframe in a Gekko equation such as:

# incorrect
df_c = pd.read_csv('Values_C.csv',index_col=0)
m.Equation(m.C1_in==df_c)

You can resolve this error by instead creating an input Parameter such as:

# correct
df_c = pd.read_csv('Values_C.csv',index_col=0)
df_c = m.Param(df_c)
m.Equation(m.C1_in==df_c)

I don't have your .npy files so I can't reproduce your error. However, I did replace those with random array inputs of length n to get a successful solution. There were also parameters such as M_s that are undefined so I included some sample values. Your definition of MVs and CVs is good. The error is likely due to other input parameters that need to be converted to Gekko type Parameters before using them in an equation.

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

n = 17

m = GEKKO(remote=False)

m.time = np.linspace(0,8,n)

c1_in_arr = np.random.rand(n)
c2_in_arr = np.random.rand(n)

V1_measured = np.random.rand(n)
V2_measured = np.random.rand(n)

#mmanipulated variables, feeding in the arrays for them
m.C1_in = m.MV(value=c1_in_arr)
m.C2_in = m.MV(value=c2_in_arr)


#estimated variables, feeding in the upper and lower bounds for them
m.C1_eff = m.FV(value = 0.98, lb = 0.95, ub = 1.0)

#controlled variables, feeding in the measurements for them
m.V1 = m.CV(value=V1_measured)
m.V2 = m.CV(value=V2_measured)

mdot_1 = m.Var()
mdot_2 = m.Var()

m.Equation(mdot_1== 1.52*m.C1_eff*m.C1_in)
m.Equation(mdot_2==-0.668*mdot_1 + 1.33*m.C1_eff*m.C2_in)

Hhat_C1 = m.Var()
Hhat_C2 = m.Var()
M_m = 125
M_s = 125
mdot_m = 75
mdot_s = 46

m.Equations([Hhat_C1 == -0.606 + 0.0057 * mdot_1,
             Hhat_C2 == -3.933 + 0.00096 * mdot_1])

C1_m = m.Var(value = 200)
C2_m = m.Var(value = 150)

m.Equations([C1_m.dt() == mdot_1 - C1_m/M_m*mdot_m,
             C2_m.dt() == mdot_2 - C2_m/M_s*mdot_s])

m.Equation(m.V1==0.8*C1_m/M_m)


m.Equation(m.V2 == 0.78*C1_m/C2_m)


m.options.IMODE = 5
#setting the solver settings to MHE

m.options.EV_TYPE = 1
#setting the solver for the MHE to calculate the
            #parameters based on the sum of absolute errors

m.C1_in.STATUS = 0
m.C2_in.STATUS = 0

m.C1_eff.STATUS = 1
m.V1.STATUS = 1
m.V2.STATUS = 1

m.C1_in.FSTATUS = 1
m.C2_in.FSTATUS = 1

m.C1_eff.FSTATUS = 0
m.V1.FSTATUS = 1
m.V2.FSTATUS = 1

m.C1_eff.DMAX = 1.0

m.V1.MEAS_GAP = 0.001
m.V2.MEAS_GAP = 0.001

m.open_folder() 
m.solve(disp = True)

The file infeasibilities.txt won't be created if there is a model error that prevents the solver from running or if there is a successful solution. With the random input values, there is a successful solution.

----------------------------------------------------------------
 APMonitor, Version 0.9.2
 APMonitor Optimization Suite
 ----------------------------------------------------------------
 
 
 --------- APM Model Size ------------
 Each time step contains
   Objects      :  0
   Constants    :  0
   Variables    :  11
   Intermediates:  0
   Connections  :  0
   Equations    :  8
   Residuals    :  8
 
 Warning: CV( 1 ) on at cycle  1 with no MVs on
 Warning: CV( 2 ) on at cycle  1 with no MVs on
 Number of state variables:    417
 Number of total equations: -  416
 Number of slack variables: -  0
 ---------------------------------------
 Degrees of freedom       :    1
 
 **********************************************
 Dynamic Estimation with Interior Point Solver
 **********************************************
  
  
 Info: Exact Hessian

******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
 Ipopt is released as open source code under the Eclipse Public License (EPL).
         For more information visit http://projects.coin-or.org/Ipopt
******************************************************************************

This is Ipopt version 3.10.2, running with linear solver mumps.

Number of nonzeros in equality constraint Jacobian...:      510
Number of nonzeros in inequality constraint Jacobian.:      384
Number of nonzeros in Lagrangian Hessian.............:       32

Total number of variables............................:      417
                     variables with only lower bounds:      192
                variables with lower and upper bounds:       33
                     variables with only upper bounds:        0
Total number of equality constraints.................:      224
Total number of inequality constraints...............:      192
        inequality constraints with only lower bounds:      192
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
   0 1.4079997e+001 1.20e+002 9.00e+000   0.0 0.00e+000    -  0.00e+000 0.00e+000   0
   1 2.2358394e+002 1.20e+002 9.95e+000  11.0 1.03e+011    -  1.05e-010 1.45e-012f  1
   2 3.0439837e+004 1.20e+002 2.27e+005  12.1 1.14e+012    -  2.78e-013 1.89e-011f  1
   3 3.0621616e+006 1.20e+002 2.44e+006  11.4 4.33e+010    -  1.00e+000 4.98e-008f  1
   4 3.0621722e+006 6.66e-001 2.49e+004   4.6 3.93e+000    -  9.90e-001 1.00e+000f  1
   5 3.0609833e+006 1.78e-014 2.55e+002   2.6 1.28e+000    -  9.90e-001 1.00e+000f  1
   6 2.9460876e+006 1.42e-014 2.55e+000   0.6 8.89e+001    -  9.90e-001 1.00e+000f  1
   7 7.0161757e+005 2.84e-014 2.55e-002  -1.3 1.69e+003    -  9.90e-001 1.00e+000f  1
   8 1.1625511e+004 2.84e-014 2.57e-004  -2.7 1.48e+003    -  9.90e-001 9.91e-001f  1
   9 1.0176844e+003 6.17e-009 5.69e-002  -0.1 3.45e+003    -  1.00e+000 9.32e-001f  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  10 4.7702371e+002 1.77e-008 3.10e+000  -0.8 8.48e+003    -  1.00e+000 6.38e-001f  1
  11 2.8271410e+002 2.82e-007 1.44e+000  -1.0 5.55e+003    -  1.00e+000 7.81e-001f  1
  12 2.3676481e+002 8.50e-008 1.67e+000  -1.9 1.06e+004    -  1.00e+000 7.37e-001f  1
  13 2.2794093e+002 1.98e-007 1.82e+000  -3.0 6.91e+003    -  9.98e-001 7.00e-001f  1
  14 2.2582143e+002 7.63e-008 9.56e-001  -3.2 2.35e+003    -  1.00e+000 7.47e-001f  1
  15 2.2529511e+002 2.39e-008 2.72e-001  -4.0 7.00e+002    -  1.00e+000 7.20e-001f  1
  16 2.2508076e+002 5.09e-010 2.51e-004  -4.6 2.10e+002    -  1.00e+000 1.00e+000f  1
  17 2.2507517e+002 7.65e-011 2.25e-004  -6.7 5.16e+000    -  1.00e+000 8.51e-001f  1
  18 2.2507454e+002 1.43e-011 3.79e-005  -6.2 6.63e-001    -  1.00e+000 8.13e-001f  1
  19 2.2507438e+002 3.69e-012 1.75e-005  -7.0 1.68e-001    -  1.00e+000 7.43e-001f  1
iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
  20 2.2507434e+002 9.33e-013 5.66e-006  -8.2 4.29e-002    -  1.00e+000 7.47e-001f  1
  21 2.2507433e+002 2.36e-013 1.64e-006  -8.6 1.09e-002    -  1.00e+000 7.47e-001f  1
  22 2.2507432e+002 6.01e-014 1.33e-006  -9.3 2.23e-003    -  1.00e+000 7.45e-001f  1
  23 2.2507432e+002 1.42e-014 9.06e-014 -11.0 2.74e-004    -  1.00e+000 1.00e+000h  1

Number of Iterations....: 23

                                   (scaled)                 (unscaled)
Objective...............:  2.2507432359796402e+002   2.2507432359796402e+002
Dual infeasibility......:  9.0594198809412774e-014   9.0594198809412774e-014
Constraint violation....:  9.4739031434680035e-015   1.4210854715202004e-014
Complementarity.........:  1.2089838737827345e-011   1.2089838737827345e-011
Overall NLP error.......:  1.2089838737827345e-011   1.2089838737827345e-011


Number of objective function evaluations             = 24
Number of objective gradient evaluations             = 24
Number of equality constraint evaluations            = 24
Number of inequality constraint evaluations          = 24
Number of equality constraint Jacobian evaluations   = 24
Number of inequality constraint Jacobian evaluations = 24
Number of Lagrangian Hessian evaluations             = 23
Total CPU secs in IPOPT (w/o function evaluations)   =      0.163
Total CPU secs in NLP function evaluations           =      0.083

EXIT: Optimal Solution Found.

 The solution was found.

 The final value of the objective function is  225.07432359796402
 
 ---------------------------------------------------
 Solver         :  IPOPT (v3.12)
 Solution time  :  0.2523 sec
 Objective      :  225.07433063732404
 Successful solution
 ---------------------------------------------------

Upvotes: 1

Related Questions