Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Oct 17, 2024
1 parent 5c12c3c commit 2c83f27
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
12 changes: 5 additions & 7 deletions src/AmplNLWriter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ function call_solver end

struct _DefaultSolverCommand{F} <: AbstractSolverCommand
f::F
flags::Vector{String}
end

function call_solver(
Expand All @@ -72,7 +71,7 @@ function call_solver(
# the BLAS library via the LBT_DEFAULT_LIBS environment variable.
# Provide a default in case the user doesn't set.
lbt_default_libs = get(ENV, "LBT_DEFAULT_LIBS", _get_blas_loaded_libs())
cmd = `$solver_path $nl_filename $(solver.flags) $options`
cmd = `$(solver_path) $(nl_filename) -AMPL $(options)`
if !isempty(lbt_default_libs)
cmd = addenv(cmd, "LBT_DEFAULT_LIBS" => lbt_default_libs)
end
Expand All @@ -95,9 +94,9 @@ foo() do path
end
```
"""
_solver_command(x::String, flags) = _DefaultSolverCommand(f -> f(x), flags)
_solver_command(x::Function, flags) = _DefaultSolverCommand(x, flags)
_solver_command(x::AbstractSolverCommand, flags) = x
_solver_command(x::String) = _DefaultSolverCommand(f -> f(x))
_solver_command(x::Function) = _DefaultSolverCommand(x)
_solver_command(x::AbstractSolverCommand) = x

mutable struct Optimizer <: MOI.AbstractOptimizer
inner::MOI.FileFormats.NL.Model
Expand Down Expand Up @@ -177,11 +176,10 @@ function Optimizer(
stdin::Any = stdin,
stdout::Any = stdout,
directory::String = "",
flags::Vector{String} = ["-AMPL"],
)
return Optimizer(
MOI.FileFormats.NL.Model(),
_solver_command(solver_command, flags),
_solver_command(solver_command),
Dict{String,String}(opt => "" for opt in solver_args),
stdin,
stdout,
Expand Down
48 changes: 33 additions & 15 deletions test/MINLPTests/run_minlptests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,40 @@ CONFIG["Ipopt"] = Dict(
# )

import Uno_jll

# Uno needs the full uno.options file...
run(
`curl https://raw.githubusercontent.com/cvanaret/Uno/refs/heads/main/uno.options -o uno.options`,
)

struct UnoSolverCommand <: AbstractSolverCommand

function AmplNLWriter.call_solver(
solver::UnoSolverCommand,
nl_filename::String,
options::Vector{String},
stdin::IO,
stdout::IO,
)
Uno_jll.amplexe() do solver_path
lbt_default_libs =
get(ENV, "LBT_DEFAULT_LIBS", AmplNLWriter._get_blas_loaded_libs())
cmd = `$solver_path $options $nl_filename`
if !isempty(lbt_default_libs)
cmd = addenv(cmd, "LBT_DEFAULT_LIBS" => lbt_default_libs)
end
ret = run(pipeline(cmd; stdin = stdin, stdout = stdout))
if ret.exitcode != 0
error("Nonzero exit code: $(ret.exitcode)")
end
end
return replace(nl_filename, "model.nl" => "model.sol")
end

CONFIG["Uno"] = Dict(
"mixed-integer" => false,
"amplexe" => Uno_jll.amplexe,
"options" => String[],
"amplexe" => UnoSolverCommand(),
"options" => String["-preset ipopt"],
"tol" => 1e-5,
"dual_tol" => 1e-5,
"nlp_exclude" => String[],
Expand All @@ -114,22 +144,10 @@ CONFIG["Uno"] = Dict(
"infeasible_point" => AmplNLWriter.MOI.NO_SOLUTION,
)

# Uno needs the full uno.options file...
run(
`curl https://raw.githubusercontent.com/cvanaret/Uno/refs/heads/main/uno.options -o uno.options`,
)

@testset "$(name)" for name in ["Uno", "Ipopt", "Bonmin", "Couenne"]
config = CONFIG[name]
OPTIMIZER = if name == "Uno"
() -> AmplNLWriter.Optimizer(
config["amplexe"],
config["options"];
flags = String[],
)
else
OPTIMIZER =
() -> AmplNLWriter.Optimizer(config["amplexe"], config["options"])
end
PRIMAL_TARGET[MINLPTests.INFEASIBLE_PROBLEM] = config["infeasible_point"]
@testset "NLP" begin
MINLPTests.test_nlp(
Expand Down
2 changes: 1 addition & 1 deletion test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function test_nlpblockdual(path)
end

function test_AbstractSolverCommand(path)
cmd = AmplNLWriter._DefaultSolverCommand(f -> f(path), ["-AMPL"])
cmd = AmplNLWriter._DefaultSolverCommand(f -> f(path))
model = AmplNLWriter.Optimizer(cmd)
@test model.solver_command === cmd
end
Expand Down

0 comments on commit 2c83f27

Please sign in to comment.