|
| 1 | +module OptimizationPRIMA |
| 2 | + |
| 3 | +using PRIMA, Optimization, Optimization.SciMLBase |
| 4 | + |
| 5 | +abstract type PRIMASolvers end |
| 6 | + |
| 7 | +struct UOBYQA <: PRIMASolvers end |
| 8 | +struct NEWUOA <: PRIMASolvers end |
| 9 | +struct BOBYQA <: PRIMASolvers end |
| 10 | +struct LINCOA <: PRIMASolvers end |
| 11 | +struct COBYLA <: PRIMASolvers end |
| 12 | + |
| 13 | +SciMLBase.supports_opt_cache_interface(::PRIMASolvers) = true |
| 14 | +SciMLBase.allowsconstraints(::Union{LINCOA, COBYLA}) = true |
| 15 | +SciMLBase.allowsbounds(opt::Union{BOBYQA, LINCOA, COBYLA}) = true |
| 16 | +SciMLBase.requiresconstraints(opt::COBYLA) = true |
| 17 | + |
| 18 | +function SciMLBase.__init(prob::SciMLBase.OptimizationProblem, opt::PRIMASolvers, |
| 19 | + data = Optimization.DEFAULT_DATA; |
| 20 | + callback = (args...) -> (false), |
| 21 | + progress = false, kwargs...) |
| 22 | + return OptimizationCache(prob, opt, data; callback, progress, |
| 23 | + kwargs...) |
| 24 | +end |
| 25 | + |
| 26 | +function get_solve_func(opt::PRIMASolvers) |
| 27 | + if opt isa UOBYQA |
| 28 | + return PRIMA.uobyqa |
| 29 | + elseif opt isa NEWUOA |
| 30 | + return PRIMA.newuoa |
| 31 | + elseif opt isa BOBYQA |
| 32 | + return PRIMA.bobyqa |
| 33 | + elseif opt isa LINCOA |
| 34 | + return PRIMA.lincoa |
| 35 | + elseif opt isa COBYLA |
| 36 | + return PRIMA.cobyla |
| 37 | + end |
| 38 | +end |
| 39 | + |
| 40 | +function __map_optimizer_args!(cache::OptimizationCache, opt::PRIMASolvers; |
| 41 | + callback = nothing, |
| 42 | + maxiters::Union{Number, Nothing} = nothing, |
| 43 | + maxtime::Union{Number, Nothing} = nothing, |
| 44 | + abstol::Union{Number, Nothing} = nothing, |
| 45 | + reltol::Union{Number, Nothing} = nothing, |
| 46 | + kwargs...) |
| 47 | + kws = (; kwargs...) |
| 48 | + |
| 49 | + if !isnothing(maxiters) |
| 50 | + kws = (; kws..., maxfun = maxiters) |
| 51 | + end |
| 52 | + |
| 53 | + if cache.ub !== nothing |
| 54 | + kws = (; kws..., xu = cache.ub, xl = cache.lb) |
| 55 | + end |
| 56 | + |
| 57 | + if !isnothing(maxtime) || !isnothing(abstol) || !isnothing(reltol) |
| 58 | + error("maxtime, abstol and reltol kwargs not supported in $opt") |
| 59 | + end |
| 60 | + |
| 61 | + return kws |
| 62 | +end |
| 63 | + |
| 64 | +function sciml_prima_retcode(rc::AbstractString) |
| 65 | + if rc in ["SMALL_TR_RADIUS", "TRSUBP_FAILED","NAN_INF_X" |
| 66 | + ,"NAN_INF_F" |
| 67 | + ,"NAN_INF_MODEL" |
| 68 | + ,"DAMAGING_ROUNDING" |
| 69 | + ,"ZERO_LINEAR_CONSTRAINT" |
| 70 | + ,"INVALID_INPUT" |
| 71 | + ,"ASSERTION_FAILS" |
| 72 | + ,"VALIDATION_FAILS" |
| 73 | + ,"MEMORY_ALLOCATION_FAILS"] |
| 74 | + return ReturnCode.Failure |
| 75 | + else rc in [ |
| 76 | + "FTARGET_ACHIEVED" |
| 77 | + "MAXFUN_REACHED" |
| 78 | + "MAXTR_REACHED" |
| 79 | + "NO_SPACE_BETWEEN_BOUNDS" |
| 80 | + ] |
| 81 | + return ReturnCode.Success |
| 82 | + end |
| 83 | +end |
| 84 | + |
| 85 | +function SciMLBase.__solve(cache::OptimizationCache{ |
| 86 | + F, |
| 87 | + RC, |
| 88 | + LB, |
| 89 | + UB, |
| 90 | + LC, |
| 91 | + UC, |
| 92 | + S, |
| 93 | + O, |
| 94 | + D, |
| 95 | + P, |
| 96 | + C, |
| 97 | +}) where { |
| 98 | + F, |
| 99 | + RC, |
| 100 | + LB, |
| 101 | + UB, |
| 102 | + LC, |
| 103 | + UC, |
| 104 | + S, |
| 105 | + O <: PRIMASolvers, |
| 106 | + D, |
| 107 | + P, |
| 108 | + C, |
| 109 | +} |
| 110 | + _loss = function (θ) |
| 111 | + x = cache.f(θ, cache.p) |
| 112 | + if cache.callback(θ, x...) |
| 113 | + error("Optimization halted by callback.") |
| 114 | + end |
| 115 | + return x[1] |
| 116 | + end |
| 117 | + |
| 118 | + optfunc = get_solve_func(cache.opt) |
| 119 | + |
| 120 | + |
| 121 | + maxiters = Optimization._check_and_convert_maxiters(cache.solver_args.maxiters) |
| 122 | + maxtime = Optimization._check_and_convert_maxtime(cache.solver_args.maxtime) |
| 123 | + |
| 124 | + kws = __map_optimizer_args!(cache, cache.opt; callback = cache.callback, maxiters = maxiters, |
| 125 | + maxtime = maxtime, |
| 126 | + cache.solver_args...) |
| 127 | + |
| 128 | + t0 = time() |
| 129 | + if cache.opt isa COBYLA |
| 130 | + function fwcons(θ, res) |
| 131 | + cache.f.cons(res, θ, cache.p) |
| 132 | + return _loss(θ) |
| 133 | + end |
| 134 | + (minx, minf, nf, rc, cstrv) = optfunc(fwcons, cache.u0; kws...) |
| 135 | + elseif cache.opt isa LINCOA |
| 136 | + (minx, minf, nf, rc, cstrv) = optfunc(_loss, cache.u0; kws...) |
| 137 | + else |
| 138 | + (minx, minf, nf, rc) = optfunc(_loss, cache.u0; kws...) |
| 139 | + end |
| 140 | + t1 = time() |
| 141 | + |
| 142 | + retcode = sciml_prima_retcode(PRIMA.reason(rc)) |
| 143 | + |
| 144 | + SciMLBase.build_solution(cache, cache.opt, minx, |
| 145 | + minf; retcode = retcode, |
| 146 | + solve_time = t1 - t0) |
| 147 | +end |
| 148 | + |
| 149 | +export UOBYQA, NEWUOA, BOBYQA, LINCOA, COBYLA |
| 150 | +end |
0 commit comments