Skip to content

Commit 56ba881

Browse files
Merge pull request #732 from ParasPuneetSingh/master
Added struct and constructors, updated OptimizationProblem for MOO
2 parents c7df225 + 739fe11 commit 56ba881

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

src/SciMLBase.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ export ODEFunction, DiscreteFunction, ImplicitDiscreteFunction, SplitFunction, D
811811
IncrementingODEFunction, NonlinearFunction, IntervalNonlinearFunction, BVPFunction,
812812
DynamicalBVPFunction, IntegralFunction, BatchIntegralFunction
813813

814-
export OptimizationFunction
814+
export OptimizationFunction, MultiObjectiveOptimizationFunction
815815

816816
export EnsembleThreads, EnsembleDistributed, EnsembleSplitThreads, EnsembleSerial
817817

src/problems/optimization_problems.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ struct OptimizationProblem{iip, F, uType, P, LB, UB, I, LC, UC, S, K} <:
103103
ucons::UC
104104
sense::S
105105
kwargs::K
106-
@add_kwonly function OptimizationProblem{iip}(f::OptimizationFunction{iip}, u0,
106+
@add_kwonly function OptimizationProblem{iip}(f::Union{OptimizationFunction{iip}, MultiObjectiveOptimizationFunction{iip}}, u0,
107107
p = NullParameters();
108108
lb = nothing, ub = nothing, int = nothing,
109109
lcons = nothing, ucons = nothing,
@@ -119,7 +119,7 @@ struct OptimizationProblem{iip, F, uType, P, LB, UB, I, LC, UC, S, K} <:
119119
end
120120
end
121121

122-
function OptimizationProblem(f::OptimizationFunction, args...; kwargs...)
122+
function OptimizationProblem(f::Union{OptimizationFunction, MultiObjectiveOptimizationFunction}, args...; kwargs...)
123123
OptimizationProblem{isinplace(f)}(f, args...; kwargs...)
124124
end
125125
function OptimizationProblem(f, args...; kwargs...)

src/scimlfunctions.jl

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,6 +1925,38 @@ struct OptimizationFunction{iip, AD, F, G, H, HV, C, CJ, CJV, CVJ, CH, HP, CJP,
19251925
lag_hess_colorvec::LHCV
19261926
end
19271927

1928+
"""
1929+
$(TYPEDEF)
1930+
"""
1931+
1932+
struct MultiObjectiveOptimizationFunction{iip, AD, F, J, H, HV, C, CJ, CJV, CVJ, CH, HP, CJP, CHP, O,
1933+
EX, CEX, SYS, LH, LHP, HCV, CJCV, CHCV, LHCV} <:
1934+
AbstractOptimizationFunction{iip}
1935+
f::F
1936+
adtype::AD
1937+
jac::J # Replacing grad with jac for the Jacobian
1938+
hess::Vector{H} # Hess will be a vector of type H
1939+
hv::HV
1940+
cons::C
1941+
cons_j::CJ
1942+
cons_jvp::CJV
1943+
cons_vjp::CVJ
1944+
cons_h::CH
1945+
hess_prototype::HP
1946+
cons_jac_prototype::CJP
1947+
cons_hess_prototype::CHP
1948+
observed::O
1949+
expr::EX
1950+
cons_expr::CEX
1951+
sys::SYS
1952+
lag_h::LH
1953+
lag_hess_prototype::LHP
1954+
hess_colorvec::HCV
1955+
cons_jac_colorvec::CJCV
1956+
cons_hess_colorvec::CHCV
1957+
lag_hess_colorvec::LHCV
1958+
end
1959+
19281960
"""
19291961
$(TYPEDEF)
19301962
"""
@@ -3819,6 +3851,56 @@ function OptimizationFunction{iip}(f, adtype::AbstractADType = NoAD();
38193851
cons_hess_colorvec, lag_hess_colorvec)
38203852
end
38213853

3854+
# Function call operator for MultiObjectiveOptimizationFunction
3855+
(f::MultiObjectiveOptimizationFunction)(args...) = f.f(args...)
3856+
3857+
# Convenience constructor
3858+
MultiObjectiveOptimizationFunction(args...; kwargs...) = MultiObjectiveOptimizationFunction{true}(args...; kwargs...)
3859+
3860+
# Constructor with keyword arguments
3861+
function MultiObjectiveOptimizationFunction{iip}(f, adtype::AbstractADType = NoAD();
3862+
jac = nothing, hess = Vector{Nothing}(undef, 0), hv = nothing,
3863+
cons = nothing, cons_j = nothing, cons_jvp = nothing,
3864+
cons_vjp = nothing, cons_h = nothing,
3865+
hess_prototype = nothing,
3866+
cons_jac_prototype = __has_jac_prototype(f) ?
3867+
f.jac_prototype : nothing,
3868+
cons_hess_prototype = nothing,
3869+
syms = nothing,
3870+
paramsyms = nothing,
3871+
observed = __has_observed(f) ? f.observed :
3872+
DEFAULT_OBSERVED_NO_TIME,
3873+
expr = nothing, cons_expr = nothing,
3874+
sys = __has_sys(f) ? f.sys : nothing,
3875+
lag_h = nothing, lag_hess_prototype = nothing,
3876+
hess_colorvec = __has_colorvec(f) ? f.colorvec : nothing,
3877+
cons_jac_colorvec = __has_colorvec(f) ? f.colorvec :
3878+
nothing,
3879+
cons_hess_colorvec = __has_colorvec(f) ? f.colorvec :
3880+
nothing,
3881+
lag_hess_colorvec = nothing) where {iip}
3882+
isinplace(f, 2; has_two_dispatches = false, isoptimization = true)
3883+
sys = sys_or_symbolcache(sys, syms, paramsyms)
3884+
MultiObjectiveOptimizationFunction{iip, typeof(adtype), typeof(f), typeof(jac), typeof(hess),
3885+
typeof(hv),
3886+
typeof(cons), typeof(cons_j), typeof(cons_jvp),
3887+
typeof(cons_vjp), typeof(cons_h),
3888+
typeof(hess_prototype),
3889+
typeof(cons_jac_prototype), typeof(cons_hess_prototype),
3890+
typeof(observed),
3891+
typeof(expr), typeof(cons_expr), typeof(sys), typeof(lag_h),
3892+
typeof(lag_hess_prototype), typeof(hess_colorvec),
3893+
typeof(cons_jac_colorvec), typeof(cons_hess_colorvec),
3894+
typeof(lag_hess_colorvec)
3895+
}(f, adtype, jac, hess,
3896+
hv, cons, cons_j, cons_jvp,
3897+
cons_vjp, cons_h,
3898+
hess_prototype, cons_jac_prototype,
3899+
cons_hess_prototype, observed, expr, cons_expr, sys,
3900+
lag_h, lag_hess_prototype, hess_colorvec, cons_jac_colorvec,
3901+
cons_hess_colorvec, lag_hess_colorvec)
3902+
end
3903+
38223904
function BVPFunction{iip, specialize, twopoint}(f, bc;
38233905
mass_matrix = __has_mass_matrix(f) ? f.mass_matrix : I,
38243906
analytic = __has_analytic(f) ? f.analytic : nothing,

src/solve.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ function _check_opt_alg(prob::OptimizationProblem, alg; kwargs...)
116116
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) requires constraints, pass them with the `cons` kwarg in `OptimizationFunction`."))
117117
!allowscallback(alg) && haskey(kwargs, :callback) &&
118118
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) does not support callbacks, remove the `callback` keyword argument from the `solve` call."))
119-
!requiresgradient(alg) && !(prob.f isa OptimizationFunction) &&
119+
requiresgradient(alg) && !(prob.f isa AbstractOptimizationFunction) &&
120120
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) requires gradients, hence use `OptimizationFunction` to generate them with an automatic differentiation backend e.g. `OptimizationFunction(f, AutoForwardDiff())` or pass it in with `grad` kwarg."))
121-
!requireshessian(alg) && !(prob.f isa OptimizationFunction) &&
121+
requireshessian(alg) && !(prob.f isa AbstractOptimizationFunction) &&
122122
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) requires hessians, hence use `OptimizationFunction` to generate them with an automatic differentiation backend e.g. `OptimizationFunction(f, AutoFiniteDiff(); kwargs...)` or pass them in with `hess` kwarg."))
123-
!requiresconsjac(alg) && !(prob.f isa OptimizationFunction) &&
123+
requiresconsjac(alg) && !(prob.f isa AbstractOptimizationFunction) &&
124124
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) requires constraint jacobians, hence use `OptimizationFunction` to generate them with an automatic differentiation backend e.g. `OptimizationFunction(f, AutoFiniteDiff(); kwargs...)` or pass them in with `cons` kwarg."))
125-
!requiresconshess(alg) && !(prob.f isa OptimizationFunction) &&
125+
requiresconshess(alg) && !(prob.f isa AbstractOptimizationFunction) &&
126126
throw(IncompatibleOptimizerError("The algorithm $(typeof(alg)) requires constraint hessians, hence use `OptimizationFunction` to generate them with an automatic differentiation backend e.g. `OptimizationFunction(f, AutoFiniteDiff(), AutoFiniteDiff(hess=true); kwargs...)` or pass them in with `cons` kwarg."))
127127
return
128128
end

0 commit comments

Comments
 (0)