1
1
module OptimizationSymbolicAnalysisExt
2
2
3
- using OptimizationBase, SciMLBase, SymbolicAnalysis, SymbolicAnalysis. Symbolics
3
+ using OptimizationBase, SciMLBase, SymbolicAnalysis, SymbolicAnalysis. Symbolics,
4
+ OptimizationBase. ArrayInterface
4
5
using SymbolicAnalysis: AnalysisResult
5
- import Symbolics: variable, Equation, Inequality, unwrap, @variables
6
+ import SymbolicAnalysis . Symbolics: variable, Equation, Inequality, unwrap, @variables
6
7
7
8
function OptimizationBase. symify_cache (
8
9
f:: OptimizationFunction {iip, AD, F, G, FG, H, FGH, HV, C, CJ, CJV, CVJ, CH, HP,
9
10
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,
11
14
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 ))
26
17
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
30
37
31
- obj_expr = f. f (vars, params)
38
+ if obj_expr === nothing
39
+ obj_expr = f. f (vars, params)
40
+ end
32
41
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}[]
37
46
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
45
55
end
46
56
end
47
57
end
48
- end
49
58
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
54
64
end
55
65
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)
56
75
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." ))
68
79
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
80
end
73
- return obj_expr, cons_expr
74
- end
75
81
76
- function analysis (obj_expr, cons_expr)
77
82
if obj_expr != = nothing
78
83
obj_expr = obj_expr |> Symbolics. unwrap
79
84
if manifold === nothing
@@ -85,6 +90,8 @@ function analysis(obj_expr, cons_expr)
85
90
if obj_res. gcurvature != = nothing
86
91
@info " Objective Geodesic curvature: $(obj_res. gcurvature) "
87
92
end
93
+ else
94
+ obj_res = nothing
88
95
end
89
96
90
97
if cons_expr != = nothing
@@ -101,6 +108,8 @@ function analysis(obj_expr, cons_expr)
101
108
@info " Constraints Geodesic curvature: $(cons_res[i]. gcurvature) "
102
109
end
103
110
end
111
+ else
112
+ cons_res = nothing
104
113
end
105
114
106
115
return obj_res, cons_res
0 commit comments