AmplNLWriter.AbstractSolverCommand
— TypeAbstractSolverCommand
An abstract type that allows over-riding the call behavior of the solver.
See also: call_solver
.
AmplNLWriter.Optimizer
— TypeOptimizer(
solver_command::Union{String,Function},
solver_args::Vector{String};
stdin::Any = stdin,
stdout:Any = stdout,
directory::String = "",
)
Create a new Optimizer object.
Arguments
solver_command
: one of two things:- A
String
of the full path of an AMPL-compatible executable - A function that takes takes a function as input, initializes any environment as needed, calls the input function with a path to the initialized executable, and then destructs the environment.
- A
solver_args
: a vector ofString
arguments passed solver executable. However, prefer passingkey=value
options viaMOI.RawOptimizerAttribute
.stdin
andstdio
: arguments passed toBase.pipeline
to redirect IO. See the Julia documentation for more details by typing? pipeline
at the Julia REPL.directory
: the directory in which to write themodel.nl
andmodel.sol
files. If left empty, this defaults to a temporary directory. This argument may be useful when debugging.
Examples
A string to an executable:
Optimizer("/path/to/ipopt.exe")
A function or string provided by a package:
Optimizer(Ipopt.amplexe)
# or
Optimizer(Ipopt_jll.amplexe)
A custom function
function solver_command(f::Function)
# Create environment ...
ret = f("/path/to/ipopt")
# Destruct environment ...
return ret
end
Optimizer(solver_command)
The following two calls are equivalent:
# Okay:
model = Optimizer(Ipopt_jll.amplexe, ["print_level=0"])
# Better:
model = Optimizer(Ipopt_jll.amplexe)
MOI.set(model, MOI.RawOptimizerAttribute("print_level"), 0
AmplNLWriter._solver_command
— Method_solver_command(x::Union{Function,String})
Functionify the solver command so it can be called as follows:
foo = _solver_command(x)
foo() do path
run(`$(path) args...`)
end
AmplNLWriter.call_solver
— Functioncall_solver(
solver::AbstractSolverCommand,
nl_filename::String,
options::Vector{String},
stdin::IO,
stdout::IO,
)::String
Execute the solver
given the NL file at nl_filename
, a vector of options
, and stdin
and stdout
. Return the filename of the resulting .sol
file.
You can assume nl_filename
ends in model.nl
, and that you can write a .sol
file to replace(nl_filename, "model.nl" => "model.sol")
.
If anything goes wrong, throw a descriptive error.