2

I have a Gekko model and is currently experimenting with different objective functions. Most of the objective functions is built with .COST and .DCOST constructs on CV and MV variables augmented by some additional Maximize and Minimize statements. However, what happens is that some permutations of these seem to cause the Objective function value for some iterations to become NaN, as shown in the attached picture. I not sure if that means that the Obj function when calculated for some values result division by zero, inf or otherwise undefined. I will appreciate some pointers as to how to overcome this.

enter image description here

JacquesStrydom
  • 319
  • 1
  • 9

1 Answers1

1

Before convergence, the objective function is a weighted combination of the true objective and equation residuals. At convergence, the objective function is equal to the originally defined objective because the equation residuals are zero. The NaN can occur because of a division by zero, a trigonometric function that evaluates to infinity, or because of imaginary numbers such as sqrt(-value). The NaN indicates that the solver is diverging. The solver rarely recovers because the NaN objective also means that some equation gradients are missing. Here are three suggestions:

1. Eliminate divide-by-zero variables in all equations. It is not necessary to eliminate divide-by-zero for constants that are non-zero. Below is an equation where the density ρ is a calculated variable.

m.Equation(v.dt() == (m_in - m_out)/ρ)

Eliminating divide-by-zero is typically a simple multiplication to both sides of the equation.

m.Equation(ρ*v.dt() == m_in - m_out)

2. Add variable bounds. Adding variable bounds helps avoid divide-by-zero, but can also help the solver find a solution. Examples are variables bounds of (0-1) for mole fractions and (0-10000) for temperatures.

ρ = m.Var(1.0,lb=0.01,ub=10)

Relax (widen) the variable bounds if the solver reports an infeasible problem.

3. Identify infeasible constraints and equations. Set m.options.max_iter=10 to terminate early and look at the infeasibilities.txt file report. Here is help on retrieving that file: How to retrieve the 'infeasibilities.txt' from the gekko

John Hedengren
  • 12,068
  • 1
  • 21
  • 25