-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'libprima:main' into main
- Loading branch information
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module PRIMACUTEstExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using NLPModels | ||
using CUTEst | ||
using PRIMA | ||
else | ||
using ..NLPModels | ||
using ..CUTEst | ||
using ..PRIMA | ||
end | ||
|
||
for func in (:uobyqa, :newuoa, :bobyqa, :lincoa, :cobyla, :prima) | ||
@eval function PRIMA.$(Symbol(func,"_CUTEst"))(name::AbstractString; kwds...) | ||
nlp = CUTEstModel(name) | ||
try | ||
return $func(nlp; kwds...) | ||
finally | ||
finalize(nlp) | ||
end | ||
end | ||
end | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
module PRIMANLPModelsExt | ||
|
||
if isdefined(Base, :get_extension) | ||
using NLPModels | ||
using PRIMA | ||
else | ||
using ..NLPModels | ||
using ..PRIMA | ||
end | ||
|
||
# This structure is to wrap a non-linear problem model into a callable object. | ||
struct ObjectiveFunction{F<:AbstractNLPModel} <: Function | ||
nlp::F | ||
end | ||
(f::ObjectiveFunction)(x::AbstractVector) = obj(f.nlp, x) | ||
|
||
function check_variables(nlp::AbstractNLPModel, x0::AbstractVector) | ||
length(x0) == get_nvar(nlp) || error( | ||
"initial variables must have $(get_nvar(nlp)) elements") | ||
return x0 | ||
end | ||
|
||
const Variables = AbstractVector{<:Real} | ||
|
||
for func in (:uobyqa, :newuoa) | ||
@eval function PRIMA.$func(nlp::AbstractNLPModel, x0::Variables = get_x0(nlp); kwds...) | ||
has_bounds(nlp) && error("`$($func)` cannot solve problems with bound constraints") | ||
has_equalities(nlp) && error("`$($func)` cannot solve problems with equality constraints") | ||
has_inequalities(nlp) && error("`$($func)` cannot solve problems with inequality constraints") | ||
return uobyqa(ObjectiveFunction(nlp), check_variables(nlp, x0); kwds...) | ||
end | ||
end | ||
|
||
function PRIMA.bobyqa(nlp::AbstractNLPModel, x0::Variables = get_x0(nlp); kwds...) | ||
has_equalities(nlp) && error("`bobyqa` cannot solve problems with equality constraints") | ||
has_inequalities(nlp) && error("`bobyqa` cannot solve problems with inequality constraints") | ||
return bobyqa(ObjectiveFunction(nlp), check_variables(nlp, x0); kwds..., | ||
xl = get_lvar(nlp), xu = get_uvar(nlp)) | ||
end | ||
|
||
function PRIMA.lincoa(nlp::AbstractNLPModel, x0::Variables = get_x0(nlp); kwds...) | ||
nlin = get_nlin(nlp) # number of linear constraints | ||
nnln = get_nnln(nlp) # number of non-linear constraints | ||
nlin == 0 || error("linear constraints not yet implemented for NLPModels in `lincoa`") | ||
nnln == 0 || error("`lincoa` cannot solve problems with non-linear constraints") | ||
return lincoa(ObjectiveFunction(nlp), check_variables(nlp, x0); kwds..., | ||
xl = get_lvar(nlp), xu = get_uvar(nlp), | ||
linear_eq = nothing, linear_ineq = nothing) | ||
end | ||
|
||
function PRIMA.cobyla(nlp::AbstractNLPModel, x0::Variables = get_x0(nlp); kwds...) | ||
nlin = get_nlin(nlp) # number of linear constraints | ||
nnln = get_nnln(nlp) # number of non-linear constraints | ||
nlin == 0 || error("linear constraints not yet implemented for NLPModels in `cobyla`") | ||
nnln == 0 || error("non-linear constraints not yet implemented for NLPModels in `cobyla`") | ||
return cobyla(ObjectiveFunction(nlp), check_variables(nlp, x0); kwds..., | ||
xl = get_lvar(nlp), xu = get_uvar(nlp), | ||
linear_eq = nothing, linear_ineq = nothing, | ||
nonlinear_eq = nothing, nonlinear_ineq = nothing) | ||
end | ||
|
||
function PRIMA.prima(nlp::AbstractNLPModel, x0::Variables = get_x0(nlp); kwds...) | ||
nlin = get_nlin(nlp) # number of linear constraints | ||
nnln = get_nnln(nlp) # number of non-linear constraints | ||
if nnln > 0 | ||
# Only COBYLA can deal with non-linear constraints. | ||
error("solving problem with non-linear constraints by COBYLA not yet implemented") | ||
return cobyla(nlp, x0; kwds...) | ||
elseif nlin > 0 | ||
# LINCOA can deal with bounds and linear constraints. | ||
error("solving problem with linear constraints by LINCOA not yet implemented") | ||
return lincoa(nlp, x0; kwds...) | ||
elseif has_bounds(nlp) | ||
# BOBYQA can deal with bounds. | ||
return bobyqa(nlp, x0; kwds...) | ||
else | ||
# Use NEWUOA for unconstrained problems. | ||
return newuoa(nlp, x0; kwds...) | ||
end | ||
end | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters