Reputation: 23
I am trying to use gekko for optimization, but I am getting error - 13 (If I use the commented equation (see below) and an error code 2 if I use different version of that equation). Kindly guide how to solve this. I am unable to get a solution. I tried to add a lower bound, but still it is not working. The two equations are same, one is with log y and other one is with y value.
import numpy as np
from gekko import GEKKO
import pandas as pd
import matplotlib.pyplot as plt
import math
# Data points
xm = np.array([0.122, 0.276, 0.303, 0.356, 0.384, 0.444, 0.662 , 0.779, 0.867, 0.964 ,1.09, 1.17, 1.3, 1.33 ,1.41])
ym = np.array([0.00515, 0.00297, 0.00271, 0.00249, 0.00228, 0.00216, 0.0016, 0.00139, 0.0013, 0.00116, 0.00105, 0.00101, 0.000888, 0.000904, 0.000854])
# Define GEKKO model
# Parameters and variable
m = GEKKO(remote=False)
a= m.FV(lb=-4000.0,ub=-4000.0)
b= m.FV(lb=-4000.0,ub=-4000.0)
lower = 1e-4
x =m.Param(value=xm)
y_measure = m.Param(value =ym)
y_predict=m.Var(lb = lower)
# Parameters and variable options
# 1 = avaiable to optimizer to minimize objective
a.status = 1
b.status = 1
# Equation
m.Equation(m.log(y_predict) == a + b * m.log (x))
#m.Equation(y_predict == m.exp(a + b * m.log (x)))
# Objective
m.Minimize(((y_predict-y_measure)/y_measure)**2)
# application options
m.options.IMODE = 2 # regression mode
# m.options.SOLVER=1
# solve
m.solve() # remote=False for local solve
# show final objective
print('Final SSE Objective: ' + str(m.options.objfcnval))
# print solution
print('Solution')
print('a = ' + str(a.value[0]))
print('b = ' + str(b.value[0]))
Upvotes: 2
Views: 94
Reputation: 176
it looks like you accidentally made the upper bounds for the a and b coefficients -4000. By changing them to 4000, the solver now works for either prediction function.
import numpy as np
from gekko import GEKKO
import pandas as pd
import matplotlib.pyplot as plt
import math
# Data points
xm = np.array([0.122, 0.276, 0.303, 0.356, 0.384, 0.444, 0.662 , 0.779, 0.867, 0.964 ,1.09, 1.17, 1.3, 1.33 ,1.41])
ym = np.array([0.00515, 0.00297, 0.00271, 0.00249, 0.00228, 0.00216, 0.0016, 0.00139, 0.0013, 0.00116, 0.00105, 0.00101, 0.000888, 0.000904, 0.000854])
# Define GEKKO model
# Parameters and variable
m = GEKKO(remote=False)
a= m.FV(lb=-4000.0,ub=4000.0)
b= m.FV(lb=-4000.0,ub=4000.0)
lower = 1e-4
x =m.Param(value=xm)
y_measure = m.Param(value =ym)
y_predict=m.Var(lb = lower)
# Parameters and variable options
# 1 = avaiable to optimizer to minimize objective
a.status = 1
b.status = 1
# Equation
m.Equation(m.log(y_predict) == a + b * m.log (x))
#m.Equation(y_predict == m.exp(a + b * m.log (x)))
# Objective
m.Minimize(((y_predict-y_measure)/y_measure)**2)
# application options
m.options.IMODE = 2 # regression mode
# m.options.SOLVER=1
# solve
m.solve() # remote=False for local solve
# show final objective
print('Final SSE Objective: ' + str(m.options.objfcnval))
# print solution
print('Solution')
print('a = ' + str(a.value[0]))
print('b = ' + str(b.value[0]))
----------------------------------------------------------------
APMonitor, Version 1.0.0
APMonitor Optimization Suite
----------------------------------------------------------------
--------- APM Model Size ------------
Each time step contains
Objects : 0
Constants : 0
Variables : 5
Intermediates: 0
Connections : 0
Equations : 2
Residuals : 2
Number of state variables: 17
Number of total equations: - 15
Number of slack variables: - 0
---------------------------------------
Degrees of freedom : 2
**********************************************
Model Parameter 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...: 45
Number of nonzeros in inequality constraint Jacobian.: 0
Number of nonzeros in Lagrangian Hessian.............: 18
Total number of variables............................: 17
variables with only lower bounds: 15
variables with lower and upper bounds: 2
variables with only upper bounds: 0
Total number of equality constraints.................: 15
Total number of inequality constraints...............: 0
inequality constraints with only lower bounds: 0
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 7.0504855e+002 9.90e-001 8.09e+002 0.0 0.00e+000 - 0.00e+000 0.00e+000 0
1 3.9209717e+001 4.16e-001 2.77e+002 -3.1 9.98e-001 - 9.90e-001 8.74e-001f 1
2 3.6268661e+001 3.79e-001 2.52e+002 -5.1 9.95e-001 - 9.90e-001 9.31e-002h 1
3 1.2792383e+001 1.39e-001 9.28e+001 -2.3 9.95e-001 - 1.00e+000 1.00e+000f 1
4 1.2870911e+001 5.13e-002 3.41e+001 -4.2 9.86e-001 - 1.00e+000 1.00e+000h 1
5 1.2255837e+001 1.89e-002 1.25e+001 -5.6 9.63e-001 - 1.00e+000 1.00e+000h 1
6 1.0837548e+001 6.92e-003 4.58e+000 -6.9 9.08e-001 - 1.00e+000 1.00e+000h 1
7 8.4929354e+000 2.52e-003 1.63e+000 -7.9 7.96e-001 - 1.00e+000 1.00e+000h 1
8 6.2935389e+000 9.00e-004 3.10e+001 -11.0 6.27e-001 - 2.60e-001 1.00e+000h 1
9 2.8727265e+000 1.70e-004 8.26e+001 -5.1 3.67e-001 - 1.32e-003 1.00e+000f 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
10 8.3643158e-001 1.24e-003 2.17e-001 -5.8 6.36e-001 - 1.00e+000 1.00e+000f 1
11 2.2427664e-001 1.82e-003 2.02e-001 -5.8 4.50e-001 - 1.00e+000 1.00e+000f 1
12 1.8136589e-002 3.88e-004 3.61e-002 -7.0 1.40e-001 - 1.00e+000 1.00e+000h 1
13 1.0934780e-002 3.04e-005 2.25e-003 -8.8 4.72e-002 - 1.00e+000 1.00e+000h 1
14 1.1280858e-002 1.81e-007 1.13e-005 -11.0 3.75e-003 - 1.00e+000 1.00e+000h 1
15 1.1283202e-002 5.56e-012 3.14e-010 -11.0 2.07e-005 - 1.00e+000 1.00e+000h 1
Number of Iterations....: 15
(scaled) (unscaled)
Objective...............: 5.4569096085891645e-004 1.1283201917622072e-002
Dual infeasibility......: 3.1361952979191013e-010 6.4846822354946330e-009
Constraint violation....: 5.5613083582706224e-012 5.5613083582706224e-012
Complementarity.........: 1.0000596947430242e-011 2.0678142529061207e-010
Overall NLP error.......: 3.1361952979191013e-010 6.4846822354946330e-009
Number of objective function evaluations = 16
Number of objective gradient evaluations = 16
Number of equality constraint evaluations = 16
Number of inequality constraint evaluations = 0
Number of equality constraint Jacobian evaluations = 16
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations = 15
Total CPU secs in IPOPT (w/o function evaluations) = 0.019
Total CPU secs in NLP function evaluations = 0.003
EXIT: Optimal Solution Found.
The solution was found.
The final value of the objective function is 0.011283201917622072
---------------------------------------------------
Solver : IPOPT (v3.12)
Solution time : 0.022600000000000002 sec
Objective : 0.011283201917622073
Successful solution
---------------------------------------------------
Final SSE Objective: 0.011283201918
Solution
a = -6.7849216408
b = -0.74185760092
```
Upvotes: 2