-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsos_solver.jl
More file actions
157 lines (122 loc) · 4.54 KB
/
Copy pathsos_solver.jl
File metadata and controls
157 lines (122 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using SumOfSquares
using DynamicPolynomials
using Mosek
using MosekTools
using JuMP
include("utils.jl")
using .utilsModule: string2poly, str2expr
using .taylorApproxModule: taylor_cos, taylor_sin
function sos_solver(; h_exp, ds, du)
"""
Args:
; (For kayword argumants syntax)
h_exp: Julia math expression for safe region (h > 0)
ds: Degree of auxiliary polynomials
du: Degree of control polynomials
"""
# Define polynomial variables
@polyvar x y
vars = [x, y]
h = string2poly(h_exp, x, y)
# Parameters
xi0 = 1e-8
# Create monomials for control inputs
monos_ux = monomials(vars, 0:du)
# Initialise SOS program
model = SOSModel(Mosek.Optimizer)
# Define control polynomial variables
@variable(model, u1, Poly(monos_ux))
@variable(model, u2, Poly(monos_ux))
@variable(model, lambda)
@variable(model, delta)
# Lie derivative of h along the contorl vector field
Lhu = differentiate(h, vars[1])*u1 + differentiate(h, vars[2])*u2
# Auxiliary polynomials
monos_s = monomials(vars, 0:ds)
@variable(model, s0, Poly(monos_s))
@variable(model, s1, Poly(monos_s))
# TAG SOS constraints
# @constraint(model, Lhu - lambda*h + delta - s0*h - s1*g >= 0)
@constraint(model, Lhu - lambda*h + delta - s0*h >= 0)
@constraint(model, delta >= 0)
@constraint(model, lambda - xi0 >= 0)
@constraint(model, s0 >= 0)
@constraint(model, s1 >= 0)
# Set objective function (maximise delta)
@objective(model, Min, delta)
# Solve the optimisation problem
optimize!(model)
u1_poly = string(value(u1))
u2_poly = string(value(u2))
return u1_poly, u2_poly
end
function create_poly(expr_str, x, mu)
# Parse the expression
expr = Meta.parse(expr_str)
# Evaluate in a local scope with defined variables (mu is a JuMP variable, do not define it here)
return eval(quote
let x = $x, mu = $mu, taylor_cos = taylor_cos, taylor_sin = taylor_sin
$expr
end
end)
end
function sos_solver2(; psi_gamma_mu, psi, ku1_num, ku2_num, ku_den, u1_bound, u2_bound, dmu, ds, )
@polyvar x[1:4]
model = SOSModel(Mosek.Optimizer)
monos_mux = monomials(x, 0:dmu)
monos_ms = monomials(x, 0:ds)
@variable(model, mu, Poly(monos_mux))
@variable(model, delta)
@variable(model, s11, Poly(monos_ms))
@variable(model, s12, Poly(monos_ms))
psi_gamma_mu = create_poly(psi_gamma_mu, x, mu)
psi = create_poly(psi, x, mu)
ku1_num = create_poly(ku1_num, x, mu)
ku2_num = create_poly(ku2_num, x, mu)
ku_den = create_poly(ku_den, x, mu)
# TAG add constraints
@constraint(model, mu >= 0)
@constraint(model, psi_gamma_mu + delta >= 0)
@constraint(model, u1_bound * ku_den^2 - ku1_num * ku_den - s11 * psi >= 0)
@constraint(model, u1_bound * ku_den^2 + ku1_num * ku_den - s12 * psi >= 0)
@constraint(model, s11 >= 0)
@constraint(model, s12 >= 0)
# @constraint(model, u2_bound * ku_den^2 - ku2_num * ku_den - 0 * psi_gamma >= 0)
# @constraint(model, u2_bound * ku_den^2 + ku2_num * ku_den - 0 * psi_gamma >= 0)
@objective(model, Min, delta)
optimize!(model)
mu_poly = string(value(mu))
# println(mu_poly)
result = replace(mu_poly, r"x\[(\d+)\]" => s"x\1")
return(result)
end
function sos_solver3(; psi_gamma_mu, psi, ku1_num, ku2_num, ku_den, u1_bound, u2_bound, dmu, ds, )
@polyvar x[1:4]
model = SOSModel(Mosek.Optimizer)
monos_mux = monomials(x, 0:dmu)
monos_ms = monomials(x, 0:ds)
@variable(model, mu)
@variable(model, delta)
@variable(model, s11, Poly(monos_ms))
@variable(model, s12, Poly(monos_ms))
psi_gamma_mu = create_poly(psi_gamma_mu, x, mu)
psi = create_poly(psi, x, mu)
ku1_num = create_poly(ku1_num, x, mu)
ku2_num = create_poly(ku2_num, x, mu)
ku_den = create_poly(ku_den, x, mu)
# TAG add constraints
@constraint(model, mu >= 0)
@constraint(model, psi_gamma_mu + delta >= 0)
@constraint(model, u1_bound * ku_den^2 - ku1_num * ku_den - s11 * psi >= 0)
@constraint(model, u1_bound * ku_den^2 + ku1_num * ku_den - s12 * psi >= 0)
@constraint(model, s11 >= 0)
@constraint(model, s12 >= 0)
# @constraint(model, u2_bound * ku_den^2 - ku2_num * ku_den - 0 * psi_gamma >= 0)
# @constraint(model, u2_bound * ku_den^2 + ku2_num * ku_den - 0 * psi_gamma >= 0)
@objective(model, Min, delta)
optimize!(model)
mu_poly = string(value(mu))
# println(mu_poly)
# result = replace(mu_poly, r"x\[(\d+)\]" => s"x\1")
return(value(mu))
end