pythonnumpynlopt

NLOPT Invalid argument Python


When I run the following simple NLOPT example in python :

import numpy as np
import nlopt 

n = 2
localopt_feval_max = 10
lb = np.array([-1, -1])
ub = np.array([1, 1])


def myfunc(x, grad):
    return -1

opt = nlopt.opt(nlopt.LN_NELDERMEAD, n)

opt.set_lower_bounds(lb)
opt.set_upper_bounds(ub)
opt.set_maxeval(localopt_feval_max)

opt.set_min_objective(myfunc)
opt.set_xtol_rel(1e-8)
x0 = np.array([0,0])

x = opt.optimize(x0)

I get an error:

"ValueError: nlopt invalid argument"

The only suggestion given by the reference here:

http://ab-initio.mit.edu/wiki/index.php/NLopt_Python_Reference

is that the lower bounds might be bigger than the upper bounds, or there is an unknown algorithm (neither of which is the case here). I am running the following versions of Python, NLOPT, and NumPy

>>> sys.version
'3.4.0 (default, Apr 11 2014, 13:05:11) \n[GCC 4.8.2]'
>>> nlopt.__version__
'2.4.2'
>>> np.__version__
'1.8.2'

Solution

  • By changing the function declaration to

    def myfunc(x, grad):
        return -1.0
    

    everything works. So NLopt can not handle objectives that return python integer instead of float

    I feel like NLopt should be able to cast integer objective function values as float. If not this, then at least a TypeError should be raised instead of a ValueError: nlopt invalid argument.