-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcartpole.jl
114 lines (95 loc) · 2.99 KB
/
cartpole.jl
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
using OptimizationDynamics
const iLQR = OptimizationDynamics.IterativeLQR
using LinearAlgebra
using Random
# ## visualization
vis = Visualizer()
render(vis)
# ## mode
MODE = :friction
MODE = :frictionless
# ## state-space model
h = 0.05
T = 51
if MODE == :friction
im_dyn = ImplicitDynamics(cartpole_friction, h, eval(r_cartpole_friction_func), eval(rz_cartpole_friction_func), eval(rθ_cartpole_friction_func);
r_tol=1.0e-8, κ_eval_tol=1.0e-4, κ_grad_tol=1.0e-3, no_impact=true)
cartpole_friction.friction .= [0.35; 0.35]
## cartpole_friction.friction .= [0.25; 0.25]
## cartpole_friction.friction .= [0.1; 0.1]
## cartpole_friction.friction .= [0.01; 0.01]
else
im_dyn = ImplicitDynamics(cartpole_frictionless, h, eval(r_cartpole_frictionless_func), eval(rz_cartpole_frictionless_func), eval(rθ_cartpole_frictionless_func);
r_tol=1.0e-8, κ_eval_tol=1.0, κ_grad_tol=1.0, no_impact=true, no_friction=true)
end
nx = 2 * cartpole_friction.nq
nu = cartpole_friction.nu
# ## iLQR model
ilqr_dyn = iLQR.Dynamics((d, x, u, w) -> f(d, im_dyn, x, u, w),
(dx, x, u, w) -> fx(dx, im_dyn, x, u, w),
(du, x, u, w) -> fu(du, im_dyn, x, u, w),
nx, nx, nu)
model = [ilqr_dyn for t = 1:T-1];
# ## initial conditions
q0 = [0.0; 0.0]
q1 = [0.0; 0.0]
qT = [0.0; π]
q_ref = [0.0; π]
x1 = [q1; q1]
xT = [qT; qT]
# ## objective
function objt(x, u, w)
J = 0.0
J += transpose(u) * u
return J
end
function objT(x, u, w)
J = 0.0
J += transpose(x - xT) * (x - xT)
return J
end
ct = iLQR.Cost(objt, nx, nu)
cT = iLQR.Cost(objT, nx, 0)
obj = [[ct for t = 1:T-1]..., cT];
# ## constraints
function terminal_con(x, u, w)
[
x - xT; # goal
]
end
cont = iLQR.Constraint()
conT = iLQR.Constraint(terminal_con, nx, 0)
cons = [[cont for t = 1:T-1]..., conT];
# ## rollout
ū = [(t == 1 ? -1.5 : 0.0) * ones(nu) for t = 1:T-1] # set value to -1.0 when friction coefficient = 0.25
x̄ = iLQR.rollout(model, x1, ū)
q̄ = state_to_configuration(x̄)
visualize!(vis, cartpole_friction, q̄, Δt=h);
# ## solver
solver = iLQR.solver(model, obj, cons,
opts=iLQR.Options(linesearch = :armijo,
α_min=1.0e-5,
obj_tol=1.0e-5,
grad_tol=1.0e-3,
max_iter=100,
max_al_iter=20,
con_tol=0.005,
ρ_init=1.0,
ρ_scale=10.0,
verbose=false))
iLQR.initialize_controls!(solver, ū)
iLQR.initialize_states!(solver, x̄);
# ## solve
iLQR.reset!(solver.s_data)
@time iLQR.solve!(solver);
@show iLQR.eval_obj(solver.m_data.obj.costs, solver.m_data.x, solver.m_data.u, solver.m_data.w)
@show solver.s_data.iter[1]
@show norm(terminal_con(solver.m_data.x[T], zeros(0), zeros(0)), Inf)
@show solver.s_data.obj[1] # augmented Lagrangian cost
# ## solution
x_sol, u_sol = iLQR.get_trajectory(solver)
q_sol = state_to_configuration(x_sol)
visualize!(vis, cartpole_friction, q_sol, Δt=h);
# ## benchmark
solver.options.verbose = false
@benchmark iLQR.solve!($solver, x̄, ū) setup=(x̄=deepcopy(x̄), ū=deepcopy(ū));