Reputation: 1
I'm trying to solve a MINLP with binary variables in Julia.
I have a user-specific objective function which is non-linear and I have nonlinear and linear constraints. I tried to solve the problem using Juniper, but I always get the error KeyError: key :myfunc not found
which I don't understand.
Here is an example where the error occurs.
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))
Do you have any idea why the error KeyError: key :myfunc not found
occurs and how I can fix it?
Thanks!
Upvotes: 0
Views: 751
Reputation: 1205
It seems that the objective needs to be registered both with JuMP and Juniper. Here is a working example of an MINLP where i) the objective is non-linear, ii) the constraints are both linear and non-linear, and iii) the variables are binary.
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))
Here's the resulting output:
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
Upvotes: 1