From cf34fe54cc0d784d1a569cdeafbd69471d65a6fa Mon Sep 17 00:00:00 2001 From: Paras Puneet Singh <136245940+ParasPuneetSingh@users.noreply.github.com> Date: Mon, 24 Jun 2024 20:36:15 +0530 Subject: [PATCH 1/3] Updated instantiate_function function.jl I have updated the instantiate_function to handle the MOOFunction. --- src/function.jl | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/function.jl b/src/function.jl index a43a9dd..8e85d70 100644 --- a/src/function.jl +++ b/src/function.jl @@ -113,3 +113,78 @@ function instantiate_function(f, x, adtype::ADTypes.AbstractADType, adpkg = adtypestr[strtind:(open_brkt_ind - 1)] throw(ArgumentError("The passed automatic differentiation backend choice is not available. Please load the corresponding AD package $adpkg.")) end + +function instantiate_function(f::MultiObjectiveOptimizationFunction, x, ::SciMLBase.NoAD, + p, num_cons = 0) + jac = f.jac === nothing ? nothing : (J, x, args...) -> f.jac(J, x, p, args...) + hess = f.hess === nothing ? nothing : [(H, x, args...) -> h(H, x, p, args...) for h in f.hess] + hv = f.hv === nothing ? nothing : (H, x, v, args...) -> f.hv(H, x, v, p, args...) + cons = f.cons === nothing ? nothing : (res, x) -> f.cons(res, x, p) + cons_j = f.cons_j === nothing ? nothing : (res, x) -> f.cons_j(res, x, p) + cons_jvp = f.cons_jvp === nothing ? nothing : (res, x) -> f.cons_jvp(res, x, p) + cons_vjp = f.cons_vjp === nothing ? nothing : (res, x) -> f.cons_vjp(res, x, p) + cons_h = f.cons_h === nothing ? nothing : (res, x) -> f.cons_h(res, x, p) + hess_prototype = f.hess_prototype === nothing ? nothing : + convert.(eltype(x), f.hess_prototype) + cons_jac_prototype = f.cons_jac_prototype === nothing ? nothing : + convert.(eltype(x), f.cons_jac_prototype) + cons_hess_prototype = f.cons_hess_prototype === nothing ? nothing : + [convert.(eltype(x), f.cons_hess_prototype[i]) + for i in 1:num_cons] + expr = symbolify(f.expr) + cons_expr = symbolify.(f.cons_expr) + + return MultiObjectiveOptimizationFunction{true}(f.f, SciMLBase.NoAD(); jac = jac, hess = hess, + hv = hv, + cons = cons, cons_j = cons_j, cons_jvp = cons_jvp, cons_vjp = cons_vjp, cons_h = cons_h, + hess_prototype = hess_prototype, + cons_jac_prototype = cons_jac_prototype, + cons_hess_prototype = cons_hess_prototype, + expr = expr, cons_expr = cons_expr, + sys = f.sys, + observed = f.observed) +end + +function instantiate_function(f::MultiObjectiveOptimizationFunction, x, adtype::ADTypes.AbstractADType, + p, num_cons = 0) + adtypestr = string(adtype) + _strtind = findfirst('.', adtypestr) + strtind = isnothing(_strtind) ? 5 : _strtind + 5 + open_nrmlbrkt_ind = findfirst('(', adtypestr) + open_squigllybrkt_ind = findfirst('{', adtypestr) + open_brkt_ind = isnothing(open_squigllybrkt_ind) ? open_nrmlbrkt_ind : + min(open_nrmlbrkt_ind, open_squigllybrkt_ind) + adpkg = adtypestr[strtind:(open_brkt_ind - 1)] + throw(ArgumentError("The passed automatic differentiation backend choice is not available. Please load the corresponding AD package $adpkg.")) +end + +function instantiate_function(f::MultiObjectiveOptimizationFunction, cache::ReInitCache, ::SciMLBase.NoAD, + num_cons = 0) + jac = f.jac === nothing ? nothing : (J, x, args...) -> f.jac(J, x, cache.p, args...) + hess = f.hess === nothing ? nothing : [(H, x, args...) -> h(H, x, cache.p, args...) for h in f.hess] + hv = f.hv === nothing ? nothing : (H, x, v, args...) -> f.hv(H, x, v, cache.p, args...) + cons = f.cons === nothing ? nothing : (res, x) -> f.cons(res, x, cache.p) + cons_j = f.cons_j === nothing ? nothing : (res, x) -> f.cons_j(res, x, cache.p) + cons_jvp = f.cons_jvp === nothing ? nothing : (res, x) -> f.cons_jvp(res, x, cache.p) + cons_vjp = f.cons_vjp === nothing ? nothing : (res, x) -> f.cons_vjp(res, x, cache.p) + cons_h = f.cons_h === nothing ? nothing : (res, x) -> f.cons_h(res, x, cache.p) + hess_prototype = f.hess_prototype === nothing ? nothing : + convert.(eltype(cache.u0), f.hess_prototype) + cons_jac_prototype = f.cons_jac_prototype === nothing ? nothing : + convert.(eltype(cache.u0), f.cons_jac_prototype) + cons_hess_prototype = f.cons_hess_prototype === nothing ? nothing : + [convert.(eltype(cache.u0), f.cons_hess_prototype[i]) + for i in 1:num_cons] + expr = symbolify(f.expr) + cons_expr = symbolify.(f.cons_expr) + + return MultiObjectiveOptimizationFunction{true}(f.f, SciMLBase.NoAD(); jac = jac, hess = hess, + hv = hv, + cons = cons, cons_j = cons_j, cons_jvp = cons_jvp, cons_vjp = cons_vjp, cons_h = cons_h, + hess_prototype = hess_prototype, + cons_jac_prototype = cons_jac_prototype, + cons_hess_prototype = cons_hess_prototype, + expr = expr, cons_expr = cons_expr, + sys = f.sys, + observed = f.observed) +end From 6bd3b78f3f078e837ea22c40f5e33c2afbebead2 Mon Sep 17 00:00:00 2001 From: Paras Puneet Singh <136245940+ParasPuneetSingh@users.noreply.github.com> Date: Fri, 19 Jul 2024 16:02:19 +0530 Subject: [PATCH 2/3] Update function.jl Changed the position of the MOO function checks in instantiate_function. --- src/function.jl | 103 +++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 49 deletions(-) diff --git a/src/function.jl b/src/function.jl index b7f3878..e6c8b0a 100644 --- a/src/function.jl +++ b/src/function.jl @@ -43,13 +43,17 @@ function that is not defined, an error is thrown. For more information on the use of automatic differentiation, see the documentation of the `AbstractADType` types. """ -function instantiate_function(f, x, ::SciMLBase.NoAD, + + +function instantiate_function(f::MultiObjectiveOptimizationFunction, x, ::SciMLBase.NoAD, p, num_cons = 0) - grad = f.grad === nothing ? nothing : (G, x, args...) -> f.grad(G, x, p, args...) - hess = f.hess === nothing ? nothing : (H, x, args...) -> f.hess(H, x, p, args...) + jac = f.jac === nothing ? nothing : (J, x, args...) -> f.jac(J, x, p, args...) + hess = f.hess === nothing ? nothing : [(H, x, args...) -> h(H, x, p, args...) for h in f.hess] hv = f.hv === nothing ? nothing : (H, x, v, args...) -> f.hv(H, x, v, p, args...) cons = f.cons === nothing ? nothing : (res, x) -> f.cons(res, x, p) cons_j = f.cons_j === nothing ? nothing : (res, x) -> f.cons_j(res, x, p) + cons_jvp = f.cons_jvp === nothing ? nothing : (res, x) -> f.cons_jvp(res, x, p) + cons_vjp = f.cons_vjp === nothing ? nothing : (res, x) -> f.cons_vjp(res, x, p) cons_h = f.cons_h === nothing ? nothing : (res, x) -> f.cons_h(res, x, p) hess_prototype = f.hess_prototype === nothing ? nothing : convert.(eltype(x), f.hess_prototype) @@ -61,9 +65,9 @@ function instantiate_function(f, x, ::SciMLBase.NoAD, expr = symbolify(f.expr) cons_expr = symbolify.(f.cons_expr) - return OptimizationFunction{true}(f.f, SciMLBase.NoAD(); grad = grad, hess = hess, + return MultiObjectiveOptimizationFunction{true}(f.f, SciMLBase.NoAD(); jac = jac, hess = hess, hv = hv, - cons = cons, cons_j = cons_j, cons_h = cons_h, + cons = cons, cons_j = cons_j, cons_jvp = cons_jvp, cons_vjp = cons_vjp, cons_h = cons_h, hess_prototype = hess_prototype, cons_jac_prototype = cons_jac_prototype, cons_hess_prototype = cons_hess_prototype, @@ -72,13 +76,28 @@ function instantiate_function(f, x, ::SciMLBase.NoAD, observed = f.observed) end -function instantiate_function(f, cache::ReInitCache, ::SciMLBase.NoAD, +function instantiate_function(f::MultiObjectiveOptimizationFunction, x, adtype::ADTypes.AbstractADType, + p, num_cons = 0) + adtypestr = string(adtype) + _strtind = findfirst('.', adtypestr) + strtind = isnothing(_strtind) ? 5 : _strtind + 5 + open_nrmlbrkt_ind = findfirst('(', adtypestr) + open_squigllybrkt_ind = findfirst('{', adtypestr) + open_brkt_ind = isnothing(open_squigllybrkt_ind) ? open_nrmlbrkt_ind : + min(open_nrmlbrkt_ind, open_squigllybrkt_ind) + adpkg = adtypestr[strtind:(open_brkt_ind - 1)] + throw(ArgumentError("The passed automatic differentiation backend choice is not available. Please load the corresponding AD package $adpkg.")) +end + +function instantiate_function(f::MultiObjectiveOptimizationFunction, cache::ReInitCache, ::SciMLBase.NoAD, num_cons = 0) - grad = f.grad === nothing ? nothing : (G, x, args...) -> f.grad(G, x, cache.p, args...) - hess = f.hess === nothing ? nothing : (H, x, args...) -> f.hess(H, x, cache.p, args...) + jac = f.jac === nothing ? nothing : (J, x, args...) -> f.jac(J, x, cache.p, args...) + hess = f.hess === nothing ? nothing : [(H, x, args...) -> h(H, x, cache.p, args...) for h in f.hess] hv = f.hv === nothing ? nothing : (H, x, v, args...) -> f.hv(H, x, v, cache.p, args...) cons = f.cons === nothing ? nothing : (res, x) -> f.cons(res, x, cache.p) cons_j = f.cons_j === nothing ? nothing : (res, x) -> f.cons_j(res, x, cache.p) + cons_jvp = f.cons_jvp === nothing ? nothing : (res, x) -> f.cons_jvp(res, x, cache.p) + cons_vjp = f.cons_vjp === nothing ? nothing : (res, x) -> f.cons_vjp(res, x, cache.p) cons_h = f.cons_h === nothing ? nothing : (res, x) -> f.cons_h(res, x, cache.p) hess_prototype = f.hess_prototype === nothing ? nothing : convert.(eltype(cache.u0), f.hess_prototype) @@ -90,9 +109,9 @@ function instantiate_function(f, cache::ReInitCache, ::SciMLBase.NoAD, expr = symbolify(f.expr) cons_expr = symbolify.(f.cons_expr) - return OptimizationFunction{true}(f.f, SciMLBase.NoAD(); grad = grad, hess = hess, + return MultiObjectiveOptimizationFunction{true}(f.f, SciMLBase.NoAD(); jac = jac, hess = hess, hv = hv, - cons = cons, cons_j = cons_j, cons_h = cons_h, + cons = cons, cons_j = cons_j, cons_jvp = cons_jvp, cons_vjp = cons_vjp, cons_h = cons_h, hess_prototype = hess_prototype, cons_jac_prototype = cons_jac_prototype, cons_hess_prototype = cons_hess_prototype, @@ -101,28 +120,14 @@ function instantiate_function(f, cache::ReInitCache, ::SciMLBase.NoAD, observed = f.observed) end -function instantiate_function(f, x, adtype::ADTypes.AbstractADType, - p, num_cons = 0) - adtypestr = string(adtype) - _strtind = findfirst('.', adtypestr) - strtind = isnothing(_strtind) ? 5 : _strtind + 5 - open_nrmlbrkt_ind = findfirst('(', adtypestr) - open_squigllybrkt_ind = findfirst('{', adtypestr) - open_brkt_ind = isnothing(open_squigllybrkt_ind) ? open_nrmlbrkt_ind : - min(open_nrmlbrkt_ind, open_squigllybrkt_ind) - adpkg = adtypestr[strtind:(open_brkt_ind - 1)] - throw(ArgumentError("The passed automatic differentiation backend choice is not available. Please load the corresponding AD package $adpkg.")) -end -function instantiate_function(f::MultiObjectiveOptimizationFunction, x, ::SciMLBase.NoAD, +function instantiate_function(f, x, ::SciMLBase.NoAD, p, num_cons = 0) - jac = f.jac === nothing ? nothing : (J, x, args...) -> f.jac(J, x, p, args...) - hess = f.hess === nothing ? nothing : [(H, x, args...) -> h(H, x, p, args...) for h in f.hess] + grad = f.grad === nothing ? nothing : (G, x, args...) -> f.grad(G, x, p, args...) + hess = f.hess === nothing ? nothing : (H, x, args...) -> f.hess(H, x, p, args...) hv = f.hv === nothing ? nothing : (H, x, v, args...) -> f.hv(H, x, v, p, args...) cons = f.cons === nothing ? nothing : (res, x) -> f.cons(res, x, p) cons_j = f.cons_j === nothing ? nothing : (res, x) -> f.cons_j(res, x, p) - cons_jvp = f.cons_jvp === nothing ? nothing : (res, x) -> f.cons_jvp(res, x, p) - cons_vjp = f.cons_vjp === nothing ? nothing : (res, x) -> f.cons_vjp(res, x, p) cons_h = f.cons_h === nothing ? nothing : (res, x) -> f.cons_h(res, x, p) hess_prototype = f.hess_prototype === nothing ? nothing : convert.(eltype(x), f.hess_prototype) @@ -134,9 +139,9 @@ function instantiate_function(f::MultiObjectiveOptimizationFunction, x, ::SciMLB expr = symbolify(f.expr) cons_expr = symbolify.(f.cons_expr) - return MultiObjectiveOptimizationFunction{true}(f.f, SciMLBase.NoAD(); jac = jac, hess = hess, + return OptimizationFunction{true}(f.f, SciMLBase.NoAD(); grad = grad, hess = hess, hv = hv, - cons = cons, cons_j = cons_j, cons_jvp = cons_jvp, cons_vjp = cons_vjp, cons_h = cons_h, + cons = cons, cons_j = cons_j, cons_h = cons_h, hess_prototype = hess_prototype, cons_jac_prototype = cons_jac_prototype, cons_hess_prototype = cons_hess_prototype, @@ -145,28 +150,13 @@ function instantiate_function(f::MultiObjectiveOptimizationFunction, x, ::SciMLB observed = f.observed) end -function instantiate_function(f::MultiObjectiveOptimizationFunction, x, adtype::ADTypes.AbstractADType, - p, num_cons = 0) - adtypestr = string(adtype) - _strtind = findfirst('.', adtypestr) - strtind = isnothing(_strtind) ? 5 : _strtind + 5 - open_nrmlbrkt_ind = findfirst('(', adtypestr) - open_squigllybrkt_ind = findfirst('{', adtypestr) - open_brkt_ind = isnothing(open_squigllybrkt_ind) ? open_nrmlbrkt_ind : - min(open_nrmlbrkt_ind, open_squigllybrkt_ind) - adpkg = adtypestr[strtind:(open_brkt_ind - 1)] - throw(ArgumentError("The passed automatic differentiation backend choice is not available. Please load the corresponding AD package $adpkg.")) -end - -function instantiate_function(f::MultiObjectiveOptimizationFunction, cache::ReInitCache, ::SciMLBase.NoAD, +function instantiate_function(f, cache::ReInitCache, ::SciMLBase.NoAD, num_cons = 0) - jac = f.jac === nothing ? nothing : (J, x, args...) -> f.jac(J, x, cache.p, args...) - hess = f.hess === nothing ? nothing : [(H, x, args...) -> h(H, x, cache.p, args...) for h in f.hess] + grad = f.grad === nothing ? nothing : (G, x, args...) -> f.grad(G, x, cache.p, args...) + hess = f.hess === nothing ? nothing : (H, x, args...) -> f.hess(H, x, cache.p, args...) hv = f.hv === nothing ? nothing : (H, x, v, args...) -> f.hv(H, x, v, cache.p, args...) cons = f.cons === nothing ? nothing : (res, x) -> f.cons(res, x, cache.p) cons_j = f.cons_j === nothing ? nothing : (res, x) -> f.cons_j(res, x, cache.p) - cons_jvp = f.cons_jvp === nothing ? nothing : (res, x) -> f.cons_jvp(res, x, cache.p) - cons_vjp = f.cons_vjp === nothing ? nothing : (res, x) -> f.cons_vjp(res, x, cache.p) cons_h = f.cons_h === nothing ? nothing : (res, x) -> f.cons_h(res, x, cache.p) hess_prototype = f.hess_prototype === nothing ? nothing : convert.(eltype(cache.u0), f.hess_prototype) @@ -178,9 +168,9 @@ function instantiate_function(f::MultiObjectiveOptimizationFunction, cache::ReIn expr = symbolify(f.expr) cons_expr = symbolify.(f.cons_expr) - return MultiObjectiveOptimizationFunction{true}(f.f, SciMLBase.NoAD(); jac = jac, hess = hess, + return OptimizationFunction{true}(f.f, SciMLBase.NoAD(); grad = grad, hess = hess, hv = hv, - cons = cons, cons_j = cons_j, cons_jvp = cons_jvp, cons_vjp = cons_vjp, cons_h = cons_h, + cons = cons, cons_j = cons_j, cons_h = cons_h, hess_prototype = hess_prototype, cons_jac_prototype = cons_jac_prototype, cons_hess_prototype = cons_hess_prototype, @@ -188,3 +178,18 @@ function instantiate_function(f::MultiObjectiveOptimizationFunction, cache::ReIn sys = f.sys, observed = f.observed) end + +function instantiate_function(f, x, adtype::ADTypes.AbstractADType, + p, num_cons = 0) + adtypestr = string(adtype) + _strtind = findfirst('.', adtypestr) + strtind = isnothing(_strtind) ? 5 : _strtind + 5 + open_nrmlbrkt_ind = findfirst('(', adtypestr) + open_squigllybrkt_ind = findfirst('{', adtypestr) + open_brkt_ind = isnothing(open_squigllybrkt_ind) ? open_nrmlbrkt_ind : + min(open_nrmlbrkt_ind, open_squigllybrkt_ind) + adpkg = adtypestr[strtind:(open_brkt_ind - 1)] + throw(ArgumentError("The passed automatic differentiation backend choice is not available. Please load the corresponding AD package $adpkg.")) +end + + From 32c0a1ac621a82d11be70ffa5a6ce1ba705b3f5b Mon Sep 17 00:00:00 2001 From: Paras Puneet Singh <136245940+ParasPuneetSingh@users.noreply.github.com> Date: Tue, 13 Aug 2024 00:42:02 +0530 Subject: [PATCH 3/3] Removed a dispatch on review function.jl Removed the dispatch as per the review comment. --- src/function.jl | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/function.jl b/src/function.jl index e6c8b0a..bbf41ec 100644 --- a/src/function.jl +++ b/src/function.jl @@ -76,19 +76,6 @@ function instantiate_function(f::MultiObjectiveOptimizationFunction, x, ::SciMLB observed = f.observed) end -function instantiate_function(f::MultiObjectiveOptimizationFunction, x, adtype::ADTypes.AbstractADType, - p, num_cons = 0) - adtypestr = string(adtype) - _strtind = findfirst('.', adtypestr) - strtind = isnothing(_strtind) ? 5 : _strtind + 5 - open_nrmlbrkt_ind = findfirst('(', adtypestr) - open_squigllybrkt_ind = findfirst('{', adtypestr) - open_brkt_ind = isnothing(open_squigllybrkt_ind) ? open_nrmlbrkt_ind : - min(open_nrmlbrkt_ind, open_squigllybrkt_ind) - adpkg = adtypestr[strtind:(open_brkt_ind - 1)] - throw(ArgumentError("The passed automatic differentiation backend choice is not available. Please load the corresponding AD package $adpkg.")) -end - function instantiate_function(f::MultiObjectiveOptimizationFunction, cache::ReInitCache, ::SciMLBase.NoAD, num_cons = 0) jac = f.jac === nothing ? nothing : (J, x, args...) -> f.jac(J, x, cache.p, args...)