Skip to content

Commit 1650814

Browse files
committed
fix monte carlo examples
1 parent 92a2e60 commit 1650814

File tree

4 files changed

+39
-37
lines changed

4 files changed

+39
-37
lines changed

examples/hopper/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[deps]
22
ContactImplicitMPC = "842347fd-0767-4ff8-b652-76aad5eb0a37"
3+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
34
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

examples/hopper/monte_carlo.jl

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using ContactImplicitMPC
88
using LinearAlgebra
99
using StaticArrays
10+
using Random
1011

1112
# ## Simulation
1213
s = get_simulation("hopper_2D", "flat_2D_lc", "flat");
@@ -25,7 +26,7 @@ function run_policy(s::Simulation; H_sim::Int = 4000, verbose = verbose,
2526
joinpath(module_dir(), "src/dynamics/hopper_2D/gaits/gait_in_place.jld2"),
2627
load_type = :joint_traj))
2728

28-
H = ref_traj.H
29+
H = ref_traj.H
2930
h = ref_traj.h
3031
N_sample = 5
3132
H_mpc = 10
@@ -52,13 +53,12 @@ function run_policy(s::Simulation; H_sim::Int = 4000, verbose = verbose,
5253
mpc_opts = LinearizedMPCOptions(
5354
),
5455
ip_opts = InteriorPointOptions(
55-
max_iter = 100,
56-
verbose = false,
57-
r_tol = 1.0e-4,
58-
κ_tol = 1.0e-4,
56+
undercut = 5.0,
57+
κ_tol = κ_mpc,
58+
r_tol = 1.0e-8,
5959
diff_sol = true,
6060
solver = :empty_solver,
61-
),
61+
max_time = 1e5),
6262
)
6363

6464
q1_ref = copy(ref_traj.q[2]) + offset
@@ -67,16 +67,16 @@ function run_policy(s::Simulation; H_sim::Int = 4000, verbose = verbose,
6767
q0_sim = SVector{model.dim.q}(copy(q1_sim - (q1_ref - q0_ref) / N_sample))
6868
@assert norm((q1_sim - q0_sim) / h_sim - (q1_ref - q0_ref) / h) < 1.0e-8
6969

70-
sim = ContactImplicitMPC.simulator(s, q0_sim, q1_sim, h_sim, H_sim,
70+
sim = simulator(s, q0_sim, q1_sim, h_sim, H_sim,
7171
p = p,
72-
ip_opts = ContactImplicitMPC.InteriorPointOptions(
73-
r_tol = 1.0e-8,
74-
κ_init = 1.0e-6,
75-
κ_tol = 2.0e-6,
76-
diff_sol = true),
77-
sim_opts = ContactImplicitMPC.SimulatorOptions(warmstart = true))
78-
79-
status = ContactImplicitMPC.simulate!(sim, verbose = false)
72+
ip_opts = InteriorPointOptions(
73+
γ_reg = 0.0,
74+
undercut = Inf,
75+
r_tol = 1.0e-8,
76+
κ_tol = 1.0e-8,),
77+
sim_opts = SimulatorOptions(warmstart = true))
78+
79+
status = simulate!(sim, verbose = false)
8080
return deepcopy(sim.traj)
8181
end
8282

@@ -97,10 +97,10 @@ function collect_runs(s::Simulation; n::Int = 1, H_sim::Int = 4000, verbose::Boo
9797
end
9898

9999
# ## Visualize runs
100-
function visualize_runs!(vis::Visualizer, model::ContactModel, trajs::AbstractVector;
100+
function visualize_runs!(vis::ContactImplicitMPC.Visualizer, model::ContactModel, trajs::AbstractVector;
101101
sample=max(1, Int(floor(trajs[1].H / 100))), h=trajs[1].h*sample, α=1.0,
102-
anim::MeshCat.Animation=MeshCat.Animation(Int(floor(1/h))),
103-
name::Symbol=model_name(model))
102+
anim::ContactImplicitMPC.MeshCat.Animation=ContactImplicitMPC.MeshCat.Animation(Int(floor(1/h))),
103+
name::Symbol=ContactImplicitMPC.model_name(model))
104104

105105
for (i, traj) in enumerate(trajs)
106106
name_i = Symbol(string(name) * string(i))
@@ -110,16 +110,15 @@ function visualize_runs!(vis::Visualizer, model::ContactModel, trajs::AbstractVe
110110
end
111111

112112
# ## Experiment
113-
H_sim = 5000
114-
trajs = collect_runs(s, n = 100, H_sim = H_sim, verbose = true)
115-
anim = visualize_runs!(vis, s.model, trajs, α=0.1, sample=5)
116-
rep_ref_traj = repeat_ref_traj(ref_traj, Int(ceil(H_sim/N_sample/H)));
113+
H_sim = 1000
114+
trajs = collect_runs(s, n = 100, H_sim = H_sim, verbose = true);
117115

118116
# ## Visualizer
119117
vis = ContactImplicitMPC.Visualizer()
120118
open(vis)
121119

122120
# ## Visualize
123-
anim = visualize_robot!(vis, model, rep_ref_traj, name=:ref, anim=anim, sample=1, α=1.0)
121+
anim = visualize_runs!(vis, s.model, trajs, α=0.1, sample=5)
122+
124123

125124

examples/quadruped/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[deps]
22
ContactImplicitMPC = "842347fd-0767-4ff8-b652-76aad5eb0a37"
3+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
34
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

examples/quadruped/monte_carlo.jl

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using ContactImplicitMPC
88
using LinearAlgebra
99
using StaticArrays
10+
using Random
1011

1112
# ## Simulation
1213
s = get_simulation("quadruped", "flat_2D_lc", "flat")
@@ -67,11 +68,11 @@ function run_policy(s::Simulation; H_sim::Int = 2000, verbose = false,
6768

6869
sim = ContactImplicitMPC.simulator(s, q0_sim, q1_sim, h_sim, H_sim,
6970
p = p,
70-
ip_opts = ContactImplicitMPC.InteriorPointOptions(
71-
r_tol = 1.0e-8,
72-
κ_init = 1.0e-6,
73-
κ_tol = 2.0e-6,
74-
diff_sol = true),
71+
ip_opts = InteriorPointOptions(
72+
γ_reg = 0.0,
73+
undercut = Inf,
74+
r_tol = 1.0e-8,
75+
κ_tol = 1.0e-8,),
7576
sim_opts = ContactImplicitMPC.SimulatorOptions(warmstart = true))
7677

7778
status = ContactImplicitMPC.simulate!(sim, verbose = false)
@@ -122,10 +123,10 @@ function initial_configuration(model::Quadruped, θ0, θ1, θ2, θ3, x, Δz)
122123
end
123124

124125
# ## Visualize runs
125-
function visualize_runs!(vis::Visualizer, model::ContactModel, trajs::AbstractVector;
126+
function visualize_runs!(vis::ContactImplicitMPC.Visualizer, model::ContactModel, trajs::AbstractVector;
126127
sample=max(1, Int(floor(trajs[1].H / 100))), h=trajs[1].h*sample, α=1.0,
127-
anim::MeshCat.Animation=MeshCat.Animation(Int(floor(1/h))),
128-
name::Symbol=model_name(model))
128+
anim::ContactImplicitMPC.MeshCat.Animation=ContactImplicitMPC.MeshCat.Animation(Int(floor(1/h))),
129+
name::Symbol=ContactImplicitMPC.model_name(model))
129130

130131
for (i, traj) in enumerate(trajs)
131132
name_i = Symbol(string(name) * string(i))
@@ -138,12 +139,12 @@ end
138139
H_sim = 1000
139140
trajs = collect_runs(s, n = 100, H_sim = H_sim, verbose = true)
140141

141-
## Visualize
142-
anim = visualize_runs!(vis, s.model, trajs, α=0.3, sample=5)
143-
rep_ref_traj = repeat_ref_traj(ref_traj, Int(ceil(H_sim/N_sample/H)), idx_shift=[1])
144-
anim = visualize_meshrobot!(vis, model, rep_ref_traj, name=:ref, anim=anim, sample=1, α=1.0)
145-
settransform!(vis[:ref], Translation(0.0, -0.4, 0.0))
146-
plot_surface!(vis, s.env, xlims=[-0.5,4], ylims=[-0.4,0.4])
142+
# ## Visualizer
143+
vis = ContactImplicitMPC.Visualizer()
144+
open(vis)
145+
146+
# ## Visualize
147+
anim = visualize_runs!(vis, model, trajs, α=0.3, sample=5)
147148

148149
# ## High drop
149150
conf = [-0.05, 0.6, 0.6, 0.6, 0.0, 0.30]

0 commit comments

Comments
 (0)