|
| 1 | +module OptimizationSymbolicAnalysisExt |
| 2 | + |
| 3 | +using OptimizationBase, SciMLBase, SymbolicAnalysis, SymbolicAnalysis.Symbolics |
| 4 | +using SymbolicAnalysis: AnalysisResult |
| 5 | +import Symbolics: variable, Equation, Inequality, unwrap, @variables |
| 6 | + |
| 7 | +function OptimizationBase.symify_cache( |
| 8 | + f::OptimizationFunction{iip, AD, F, G, FG, H, FGH, HV, C, CJ, CJV, CVJ, CH, HP, |
| 9 | + CJP, CHP, O, EX, CEX, SYS, LH, LHP, HCV, CJCV, CHCV, LHCV}, |
| 10 | + prob) where {iip, AD, F, G, FG, H, FGH, HV, C, CJ, CJV, CVJ, CH, HP, CJP, CHP, O, |
| 11 | + EX <: Nothing, CEX <: Nothing, SYS, LH, LHP, HCV, CJCV, CHCV, LHCV} |
| 12 | + try |
| 13 | + vars = if prob.u0 isa Matrix |
| 14 | + @variables X[1:size(prob.u0, 1), 1:size(prob.u0, 2)] |
| 15 | + else |
| 16 | + ArrayInterface.restructure( |
| 17 | + prob.u0, [variable(:x, i) for i in eachindex(prob.u0)]) |
| 18 | + end |
| 19 | + params = if prob.p isa SciMLBase.NullParameters |
| 20 | + [] |
| 21 | + elseif prob.p isa MTK.MTKParameters |
| 22 | + [variable(:α, i) for i in eachindex(vcat(p...))] |
| 23 | + else |
| 24 | + ArrayInterface.restructure(p, [variable(:α, i) for i in eachindex(p)]) |
| 25 | + end |
| 26 | + |
| 27 | + if prob.u0 isa Matrix |
| 28 | + vars = vars[1] |
| 29 | + end |
| 30 | + |
| 31 | + obj_expr = f.f(vars, params) |
| 32 | + |
| 33 | + if SciMLBase.isinplace(prob) && !isnothing(prob.f.cons) |
| 34 | + lhs = Array{Symbolics.Num}(undef, num_cons) |
| 35 | + f.cons(lhs, vars) |
| 36 | + cons = Union{Equation, Inequality}[] |
| 37 | + |
| 38 | + if !isnothing(prob.lcons) |
| 39 | + for i in 1:num_cons |
| 40 | + if !isinf(prob.lcons[i]) |
| 41 | + if prob.lcons[i] != prob.ucons[i] |
| 42 | + push!(cons, prob.lcons[i] ≲ lhs[i]) |
| 43 | + else |
| 44 | + push!(cons, lhs[i] ~ prob.ucons[i]) |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + if !isnothing(prob.ucons) |
| 51 | + for i in 1:num_cons |
| 52 | + if !isinf(prob.ucons[i]) && prob.lcons[i] != prob.ucons[i] |
| 53 | + push!(cons, lhs[i] ≲ prob.ucons[i]) |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + if (isnothing(prob.lcons) || all(isinf, prob.lcons)) && |
| 58 | + (isnothing(prob.ucons) || all(isinf, prob.ucons)) |
| 59 | + throw(ArgumentError("Constraints passed have no proper bounds defined. |
| 60 | + Ensure you pass equal bounds (the scalar that the constraint should evaluate to) for equality constraints |
| 61 | + or pass the lower and upper bounds for inequality constraints.")) |
| 62 | + end |
| 63 | + cons_expr = lhs |
| 64 | + elseif !isnothing(prob.f.cons) |
| 65 | + cons_expr = f.cons(vars, params) |
| 66 | + else |
| 67 | + cons_expr = nothing |
| 68 | + end |
| 69 | + catch err |
| 70 | + throw(ArgumentError("Automatic symbolic expression generation with failed with error: $err. |
| 71 | + Try by setting `structural_analysis = false` instead if the solver doesn't require symbolic expressions.")) |
| 72 | + end |
| 73 | + return obj_expr, cons_expr |
| 74 | +end |
| 75 | + |
| 76 | +function analysis(obj_expr, cons_expr) |
| 77 | + if obj_expr !== nothing |
| 78 | + obj_expr = obj_expr |> Symbolics.unwrap |
| 79 | + if manifold === nothing |
| 80 | + obj_res = analyze(obj_expr) |
| 81 | + else |
| 82 | + obj_res = analyze(obj_expr, manifold) |
| 83 | + end |
| 84 | + @info "Objective Euclidean curvature: $(obj_res.curvature)" |
| 85 | + if obj_res.gcurvature !== nothing |
| 86 | + @info "Objective Geodesic curvature: $(obj_res.gcurvature)" |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + if cons_expr !== nothing |
| 91 | + cons_expr = cons_expr .|> Symbolics.unwrap |
| 92 | + if manifold === nothing |
| 93 | + cons_res = analyze.(cons_expr) |
| 94 | + else |
| 95 | + cons_res = analyze.(cons_expr, Ref(manifold)) |
| 96 | + end |
| 97 | + for i in 1:num_cons |
| 98 | + @info "Constraints Euclidean curvature: $(cons_res[i].curvature)" |
| 99 | + |
| 100 | + if cons_res[i].gcurvature !== nothing |
| 101 | + @info "Constraints Geodesic curvature: $(cons_res[i].gcurvature)" |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + return obj_res, cons_res |
| 107 | +end |
| 108 | + |
| 109 | +end |
0 commit comments