首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ForcedStop约束函数中的Nlopt

ForcedStop约束函数中的Nlopt
EN

Stack Overflow用户
提问于 2019-07-29 12:32:17
回答 1查看 191关注 0票数 0

我将LikelihoodProfiler:https://github.com/insysbio/LikelihoodProfiler.jl从朱莉娅改写为Python。对于非线性约束,我需要写约束函数:Reference/#nonlinear-constraints,就像我们得到一些值一样,我们抛出强制停止异常:Reference/#exc Nlopt必须处理异常并用特殊代码返回结果。

在朱莉娅看来:pass.jl

代码语言:javascript
运行
复制
function constraints_func(x, g)
        loss = loss_func(x)
        if (loss < 0.) && (scan_func(x) > scan_bound)
            throw(ForcedStop("Out of the scan bound but in ll constraint."))
        #elseif isapprox(loss, 0., atol=loss_tol)
        #    @warn "loss_tol reached... but..."
        #    return loss
        else
            return loss
        end
    end

 opt = Opt(:LN_AUGLAG, n_theta)
    ftol_abs!(opt, scan_tol)
    max_objective!(
        opt,
        (x, g) -> scan_func(x)
        )
    lb = [theta_bounds[i][1] for i in 1:n_theta] # minimum.(theta_bounds)
    ub = [theta_bounds[i][2] for i in 1:n_theta] # maximum.(theta_bounds)
    lower_bounds!(opt, lb)
    upper_bounds!(opt, ub)
    local_optimizer!(opt, local_opt)
    maxeval!(opt, max_iter)

    # inequality constraints
    inequality_constraint!(
        opt,
        constraints_func,
        loss_tol
    )

    # start optimization
    (optf, optx, ret) = optimize(opt, theta_init)

接下来,我尝试将其重写为python:

代码语言:javascript
运行
复制
 # Constraints function

    def constraints_func(x, g, opt):
        loss = loss_func(x)
        if (loss < 0) and (scan_func(x) > scan_bound):
            opt.force_stop()
            #raise nlopt.ForcedStop("Out of the scan bound but in ll constraint.")
        else:
            return loss

    # constrain optimizer
    opt = nlopt.opt(nlopt.LN_AUGLAG, n_theta)
    opt.set_ftol_abs(scan_tol)
    opt.set_max_objective(lambda x, g: scan_func(x))

    lb = [theta_bounds[i][0] for i in range(n_theta)]  # minimum.(theta_bounds)
    ub = [theta_bounds[i][1] for i in range(n_theta)]  # maximum.(theta_bounds)
    opt.set_lower_bounds(lb)
    opt.set_upper_bounds(ub)
    opt.set_local_optimizer(local_opt)
    opt.set_maxeval(max_iter)
    # print(max_iter)
    # inequality constraints
    opt.add_inequality_constraint(lambda x, g: constraints_func(x, g, opt), loss_tol)
    # start optimization
    optx = opt.optimize(theta_init)
    optf = opt.last_optimum_value()
    ret = opt.last_optimize_result()

但是当我运行它时,我得到了nlopt无效参数,如果相反

代码语言:javascript
运行
复制
opt.force_stop()

我使用

代码语言:javascript
运行
复制
raise nlopt.ForcedStop("Out of the scan bound but in ll constraint.")

我得到了nlopt.ForcedStop:超出扫描范围,但在11约束

但是我删除了Nlopt处理异常并用特殊代码返回优化结果。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-30 11:23:57

显然,我无法解决这个问题,但我使用了standart python方法

代码语言:javascript
运行
复制
   try:
        optx = opt.optimize(theta_init)
        optf = opt.last_optimum_value()
        ret = opt.last_optimize_result()
    except nlopt.ForcedStop:
        ret = -5
代码语言:javascript
运行
复制
def constraints_func(x, g):
        loss = loss_func(x)
        if (loss < 0) and (scan_func(x) > scan_bound):
            #return opt.force_stop()
            raise nlopt.ForcedStop("Out of the scan bound but in ll constraint.")
        else:
            return loss
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57253961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档