11module OptimizationSymbolicAnalysisExt
22
3- using OptimizationBase, SciMLBase, SymbolicAnalysis, SymbolicAnalysis. Symbolics
3+ using OptimizationBase, SciMLBase, SymbolicAnalysis, SymbolicAnalysis. Symbolics,
4+ OptimizationBase. ArrayInterface
45using SymbolicAnalysis: AnalysisResult
5- import Symbolics: variable, Equation, Inequality, unwrap, @variables
6+ import SymbolicAnalysis . Symbolics: variable, Equation, Inequality, unwrap, @variables
67
78function OptimizationBase. symify_cache (
89 f:: OptimizationFunction {iip, AD, F, G, FG, H, FGH, HV, C, CJ, CJV, CVJ, CH, HP,
910 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+ prob, num_cons,
12+ manifold) where {
13+ iip, AD, F, G, FG, H, FGH, HV, C, CJ, CJV, CVJ, CH, HP, CJP, CHP, O,
1114 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
15+ obj_expr = f. expr
16+ cons_expr = f. cons_expr === nothing ? nothing : getfield .(f. cons_expr, Ref (:lhs ))
2617
27- if prob. u0 isa Matrix
28- vars = vars[1 ]
29- end
18+ if obj_expr === nothing || cons_expr === nothing
19+ try
20+ vars = if prob. u0 isa Matrix
21+ @variables X[1 : size (prob. u0, 1 ), 1 : size (prob. u0, 2 )]
22+ else
23+ ArrayInterface. restructure (
24+ prob. u0, [variable (:x , i) for i in eachindex (prob. u0)])
25+ end
26+ params = if prob. p isa SciMLBase. NullParameters
27+ []
28+ elseif prob. p isa MTK. MTKParameters
29+ [variable (:α , i) for i in eachindex (vcat (p... ))]
30+ else
31+ ArrayInterface. restructure (p, [variable (:α , i) for i in eachindex (p)])
32+ end
33+
34+ if prob. u0 isa Matrix
35+ vars = vars[1 ]
36+ end
3037
31- obj_expr = f. f (vars, params)
38+ if obj_expr === nothing
39+ obj_expr = f. f (vars, params)
40+ end
3241
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}[]
42+ if cons_expr === nothing && SciMLBase. isinplace (prob) && ! isnothing (prob. f. cons)
43+ lhs = Array {Symbolics.Num} (undef, num_cons)
44+ f. cons (lhs, vars)
45+ cons = Union{Equation, Inequality}[]
3746
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])
47+ if ! isnothing (prob. lcons)
48+ for i in 1 : num_cons
49+ if ! isinf (prob. lcons[i])
50+ if prob. lcons[i] != prob. ucons[i]
51+ push! (cons, prob. lcons[i] ≲ lhs[i])
52+ else
53+ push! (cons, lhs[i] ~ prob. ucons[i])
54+ end
4555 end
4656 end
4757 end
48- end
4958
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])
59+ if ! isnothing (prob. ucons)
60+ for i in 1 : num_cons
61+ if ! isinf (prob. ucons[i]) && prob. lcons[i] != prob. ucons[i]
62+ push! (cons, lhs[i] ≲ prob. ucons[i])
63+ end
5464 end
5565 end
66+ if (isnothing (prob. lcons) || all (isinf, prob. lcons)) &&
67+ (isnothing (prob. ucons) || all (isinf, prob. ucons))
68+ throw (ArgumentError (" Constraints passed have no proper bounds defined.
69+ Ensure you pass equal bounds (the scalar that the constraint should evaluate to) for equality constraints
70+ or pass the lower and upper bounds for inequality constraints." ))
71+ end
72+ cons_expr = lhs
73+ elseif cons_expr === nothing && ! isnothing (prob. f. cons)
74+ cons_expr = f. cons (vars, params)
5675 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
76+ catch err
77+ throw (ArgumentError (" Automatic symbolic expression generation with failed with error: $err .
78+ Try by setting `structural_analysis = false` instead if the solver doesn't require symbolic expressions." ))
6879 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." ))
7280 end
73- return obj_expr, cons_expr
74- end
7581
76- function analysis (obj_expr, cons_expr)
7782 if obj_expr != = nothing
7883 obj_expr = obj_expr |> Symbolics. unwrap
7984 if manifold === nothing
@@ -85,6 +90,8 @@ function analysis(obj_expr, cons_expr)
8590 if obj_res. gcurvature != = nothing
8691 @info " Objective Geodesic curvature: $(obj_res. gcurvature) "
8792 end
93+ else
94+ obj_res = nothing
8895 end
8996
9097 if cons_expr != = nothing
@@ -101,6 +108,8 @@ function analysis(obj_expr, cons_expr)
101108 @info " Constraints Geodesic curvature: $(cons_res[i]. gcurvature) "
102109 end
103110 end
111+ else
112+ cons_res = nothing
104113 end
105114
106115 return obj_res, cons_res
0 commit comments