使用Juniper在Julia中使用MINLP

问题描述 投票:0回答:1

我正在尝试用Julia中的二进制变量来解决MINLP。我有一个特定于用户的目标函数,该函数是非线性的,并且具有非线性和线性约束。我尝试使用Juniper解决问题,但总是收到错误消息KeyError: key :myfunc not found,我不明白。

这里是发生错误的示例。

using Ipopt, JuMP, Juniper
N = 5 

optimizer = Juniper.Optimizer
params = Dict{Symbol,Any}()
params[:nl_solver] = with_optimizer(Ipopt.Optimizer, print_level=0) 
m = Model(with_optimizer(optimizer, params))

@variable(m, z[1:N],binary=true)
@NLconstraint(m,  sum(z[i]*z[i+1] for i=1:N-1) <= 20)
@constraint(m, sum(z[i] for i=1:N) <= 10) 
myfunc(z...) =  sum(sin(i)*z[i]^i for i in 1:length(z))
register(m, :myfunc, N, myfunc, autodiff=true)
@NLobjective(m, Max, myfunc(z...))
m

optimize!(m)
println(JuMP.value.(z))
println(JuMP.objective_value(m))
println(JuMP.termination_status(m))

您知道为什么会出现错误KeyError: key :myfunc not found以及如何解决吗?

谢谢!

julia nonlinear-optimization mixed-integer-programming julia-jump
1个回答
0
投票

似乎该目标需要同时在JuMP和Juniper中注册。这是一个MINLP的工作示例,其中i)目标是非线性的,ii)约束既是线性的又是非线性的,并且iii)变量是二进制的。

using Juniper, Ipopt, JuMP, Cbc # <- last package is optional
N = 4

function myfunction(x...)
    return sum(x[i].^4 for i = 1:length(x))
end


m = Model(
    with_optimizer(
        Juniper.Optimizer;
            nl_solver = with_optimizer(Ipopt.Optimizer, print_level = 0),
            mip_solver = with_optimizer(Cbc.Optimizer, logLevel=0), # <- optional
            registered_functions = [
                Juniper.register(:myfunction,  N, myfunction; autodiff = true)
            ]
        )
    )
register(m, :myfunction, N, myfunction; autodiff = true)
@variable(m, x[1:N], Bin)
@NLconstraint(m, sum(sin(x[i]^2) for i=1:N) <= 4)   
@constraint(m, x[1]+x[2]+x[3] <= 2)   
@NLobjective(m, Max, myfunction(x...))
optimize!(m)
println(JuMP.value.(x))
println(JuMP.objective_value(m))
println(JuMP.termination_status(m))

这是结果输出:

nl_solver             : OptimizerFactory(Ipopt.Optimizer, (), Base.Iterators.Pairs(:print_level => 0))
mip_solver            : OptimizerFactory(Cbc.Optimizer, (), Base.Iterators.Pairs(:logLevel => 0))
log_levels            : Symbol[:Options, :Table, :Info]
registered_functions  : Juniper.RegisteredFunction[Juniper.RegisteredFunction(:myfunction, 4, myfunction, nothing, nothing, true)]

#Variables: 4
#IntBinVar: 4
#Constraints: 2
#Linear Constraints: 1
#Quadratic Constraints: 0
#NonLinear Constraints: 1
Obj Sense: Max

Incumbent using start values: 0.0
Status of relaxation: LOCALLY_SOLVED
Time for relaxation: 0.19966506958007812
Relaxation Obj: 1.5925926562762966

       MIPobj              NLPobj       Time 
=============================================
       1.3333               0.0          0.0 

FP: 0.00896906852722168 s
FP: 1 round
FP: Obj: 3.0

 ONodes   CLevel          Incumbent                   BestBound            Gap    Time   Restarts  GainGap  
============================================================================================================
    0       2                3.0                         1.59            46.91%   0.1       0         -     

#branches: 1
Obj: 3.000000119960002
[1.0, 0.0, 1.0, 1.0]
3.000000119960002
LOCALLY_SOLVED
© www.soinside.com 2019 - 2024. All rights reserved.