Modern Fortran interface for NLopt

I’ve been using the NLopt port of cobyla in my own work.

The design where both the objective function and constraints are evaluated together does make sense also for efficiency. Parts of the computations may be reused for both purposes. I’m not familiar enough with optimization to say, whether the constraints and objective function must always be computed simultaneously.

In a similar manner, the NLopt interface already combines the objective function and its gradient. If the algorithm only wants to update the objective function, but reuse the gradient from the previous iteration, it can do so easily, by passing a null pointer for the gradient callback. This does require the user, to check for the null pointer in his callback:

      if (present(gradient)) then
        gradient(1) = 3.0_wp * a * (a*x(1) + b)**2
        gradient(2) = -1.0_wp
      endif
      f = (a*x(1) + b)**3 - x(2)

In this example a good compiler can reuse the temporary variable a*x(1) + b. This might not be the case, if the user has to provide two callback functions. For a derivative-free optimizer, you just skip the gradient part in the callback completely.

2 Likes