我正在将NLopt AUGLAG与COBYLA一起用作本地最小化函数。为什么会出现“舍入受限”错误?如何解决此错误?
这是我尝试过的事情: 删除所有约束,但它仍然返回RoundoffLimited错误。查看优化程序的值和出错之前的目标值,但是对于最小化问题,目标函数值是无穷大。我也尝试过相对的容忍度,但仍然没有变化。
因为我使用的是AUGLAG,所以我认为不需要定义梯度,因为该算法是无梯度的。这是我的代码的一般结构:
# setting global variables to keep track of optimizers in case or error
previous_args = None
current_args = None
def myfunc(x, grad):
global previous_args
global current_args
previous_args = current_args
current_args = x
occ = 0.5/x
return sqrt(np.sum((target - occ)**2)/len(x))
def myconstraint(x, grad, a, b):
return b - np.sum(x*a)
No = number of optimization variables I have
opt = nlopt.opt(nlopt.AUGLAG, No)
# Set local optimizer for AUGLAG
local_opt = nlopt.opt(nlopt.LN_COBYLA,No)
opt.set_local_optimizer(local_opt)
# Lower bounds for the variables
opt.set_lower_bounds(np.zeros((No)))
# Set objective fn
opt.set_min_objective(myfunc)
# Add constraints, tot_rev is a number
opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,2/x,tot_rev), 1e-8)
try:
x = opt.optimize(np.ones((No)))
except nlopt.RoundoffLimited:
x = previous_args
# The exact error I get on x = opt.optimize is
# RoundoffLimited: NLopt roundoff-limited if I don't include the exception
# Set relative tolerance on optimization parameters
opt.set_xtol_rel(1e-3)