user20010813
user20010813

Reputation: 51

NLopt minimum working example fails with "RoundoffLimited"

I was experimenting with NLopt and created the following minimum working example, which consistently fails with RoundoffLimited: NLopt roundoff-limited:

import numpy as np
import nlopt

dim = 1

def obj_func(x, grad):
    return float( (x[0]-0.5)**2 )


opt = nlopt.opt(nlopt.LN_COBYLA, dim)
opt.set_min_objective(obj_func)
lb = np.zeros(dim)
ub = np.ones(dim)
opt.set_lower_bounds(lb)
opt.set_upper_bounds(ub)
opt.set_ftol_rel(1e-6)
x0 = np.random.uniform(low=lb, high=ub)

xopt = opt.optimize(x0)

I have absolutely no idea what I am doing wrong here, since basically every other MWE looks similar.

Upvotes: 5

Views: 412

Answers (1)

Saullo G. P. Castro
Saullo G. P. Castro

Reputation: 58985

You need to provide information regarding the absolute convergence criterion, which in your case could be:

opt.set_ftol_abs(1e-10)

the final code would look like:

import numpy as np
import nlopt

dim = 1

def obj_func(x, grad):
    return (x[0] - 0.5)**2

opt = nlopt.opt(nlopt.LN_COBYLA, dim)
opt.set_min_objective(obj_func)
lb = np.zeros(dim)
ub = np.ones(dim)
opt.set_lower_bounds(lb)
opt.set_upper_bounds(ub)
opt.set_ftol_abs(1e-10)
x0 = np.random.uniform(low=lb, high=ub)

xopt = opt.optimize(x0)
print(xopt)
#[0.50000118]

Upvotes: 0

Related Questions