Skip to content

Commit

Permalink
Fix setting params to use id and type instead of string (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Jul 22, 2024
1 parent 26e9477 commit f831f46
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,21 @@ function MOI.set(model::Optimizer, attr::MOI.RawOptimizerAttribute, value)
@_checked KN_load_tuner_file(model.inner, value)
elseif attr.name == "free"
@_checked KN_free(model.inner)
elseif !MOI.supports(model, attr)
end
pId = Ref{Cint}(0)
ret = KN_get_param_id(model.inner, attr.name, pId)
if ret == KN_RC_BAD_PARAMINPUT || pId[] <= 0
throw(MOI.UnsupportedAttribute(attr))
elseif value isa Integer
@_checked KN_set_int_param_by_name(model.inner, attr.name, value)
elseif value isa Cdouble
@_checked KN_set_double_param_by_name(model.inner, attr.name, value)
elseif value isa AbstractString
@_checked KN_set_char_param_by_name(model.inner, attr.name, value)
end
pType = Ref{Cint}()
@_checked KN_get_param_type(model.inner, pId[], pType)
if pType[] == KN_PARAMTYPE_INTEGER
@_checked KN_set_int_param(model.inner, pId[], value)
elseif pType[] == KN_PARAMTYPE_FLOAT
@_checked KN_set_double_param(model.inner, pId[], value)
else
@assert pType[] == KN_PARAMTYPE_STRING
@_checked KN_set_char_param(model.inner, pId[], value)
end
model.options[attr.name] = value
return
Expand Down
25 changes: 25 additions & 0 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,31 @@ function test_get_nlp_block()
return
end

function test_maxtime_cpu()
model = KNITRO.Optimizer()
attr = MOI.RawOptimizerAttribute("mip_maxtimecpu")
@test MOI.supports(model, attr)
MOI.set(model, attr, 30)
p = Ref{Cdouble}(0.0)
KNITRO.KN_get_double_param(model.inner, KNITRO.KN_PARAM_MIP_MAXTIMECPU, p)
@test p[] == 30.0
return
end

function test_outname()
model = KNITRO.Optimizer()
attr = MOI.RawOptimizerAttribute("outname")
@test MOI.supports(model, attr)
MOI.set(model, attr, "new_name.log")
MOI.set(model, MOI.RawOptimizerAttribute("outmode"), 1)
MOI.add_variable(model)
MOI.optimize!(model)
@test isfile("new_name.log")
@test occursin("Artelys", read("new_name.log", String))
rm("new_name.log")
return
end

end

TestMOIWrapper.runtests()

0 comments on commit f831f46

Please sign in to comment.