Skip to content

Commit e96c658

Browse files
authored
Merge pull request #12 from rejuvyesh/jkg/envtest
Add some basic tests for env interface
2 parents 0d54f76 + ab86d79 commit e96c658

File tree

19 files changed

+68
-58
lines changed

19 files changed

+68
-58
lines changed

environments/ant/methods/env.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function ant(;
4141
nx = maximal_dimension(mechanism)
4242
end
4343
nu = 8
44-
no = nx
44+
no = nx + length(mechanism.contacts)
4545

4646
aspace = BoxSpace(nu,
4747
low=(-ones(nu)),

environments/box/methods/env.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ function block(;
2222
opts_grad=SolverOptions(rtol=3.0e-4, btol=3.0e-4, undercut=1.5),
2323
T=Float64)
2424

25-
mechanism = get_box(
25+
mechanism = get_block(
2626
timestep=timestep,
2727
gravity=gravity,
2828
friction_coefficient=friction_coefficient,
2929
side=side,
3030
contact=contact,
3131
contact_type=contact_type)
3232

33-
initialize_box!(mechanism)
33+
initialize_block!(mechanism)
3434

3535
if representation == :minimal
3636
nx = minimal_dimension(mechanism)

environments/box/methods/initialize.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function get_box(;
1+
function get_block(;
22
timestep=0.01,
33
gravity=[0.0; 0.0; -9.81],
44
friction_coefficient=0.8,
@@ -58,7 +58,7 @@ function get_box(;
5858
return mech
5959
end
6060

61-
function initialize_box!(mechanism::Mechanism{T};
61+
function initialize_block!(mechanism::Mechanism{T};
6262
x=[0.0, 0.0, 1.0],
6363
q=Quaternion(1.0, 0.0, 0.0, 0.0),
6464
v=[1.0, 0.3, 0.2],

environments/hopper/methods/env.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function hopper(;
3939
nx = maximal_dimension(mechanism)
4040
end
4141
nu = 3
42-
no = nx
42+
no = nx - 1 # full_state is not being returned by default
4343

4444
# values taken from Mujoco's model, combining the control range -1, 1 and the motor gears.
4545
aspace = BoxSpace(nu,
@@ -140,7 +140,7 @@ function cost(env::Environment{Hopper}, x, u;
140140
return c
141141
end
142142

143-
function is_done(::Environment{Hopper}, x)
143+
function is_done(env::Environment{Hopper}, x)
144144
nx = minimal_dimension(env.mechanism)
145145
if env.representation == :minimal
146146
x0 = x

environments/rexhopper/methods/env.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function Base.reset(env::Environment{RexHopper};
9393
else
9494
# initialize above the ground to make sure that with random initialization we do not violate the ground constraint.
9595
initialize!(env.mechanism, :rexhopper)
96-
x0 = get_minimal_state(env.mechanism)
96+
x = get_minimal_state(env.mechanism)
9797
nx = minimal_dimension(env.mechanism)
9898
z = minimal_to_maximal(env.mechanism, x)
9999
set_maximal_state!(env.mechanism, z)

environments/walker/methods/env.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function walker(;
3939
nx = maximal_dimension(mechanism)
4040
end
4141
nu = 6
42-
no = nx
42+
no = nx-1 # full_state is false by default
4343

4444
# values taken from Mujoco's model, combining the control range -1, 1 and the motor gears.
4545
aspace = BoxSpace(nu,
@@ -136,7 +136,7 @@ function cost(env::Environment{Walker}, x, u;
136136
return c
137137
end
138138

139-
function is_done(::Environment{Walker}, x)
139+
function is_done(env::Environment{Walker}, x)
140140
nx = minimal_dimension(env.mechanism)
141141
if env.representation == :minimal
142142
x0 = x

examples/simulation/cone_compare.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ opts = SolverOptions(rtol=1.0e-6, btol=1.0e-6)
3232

3333
# ## Linear cone
3434
color_lc = orange;
35-
mech_lc = get_mechanism(:box,
35+
mech_lc = get_mechanism(:block,
3636
timestep=timestep,
3737
gravity=gravity,
3838
friction_coefficient=friction_coefficient,
@@ -41,7 +41,7 @@ mech_lc = get_mechanism(:box,
4141
color=color_lc);
4242

4343
# ## Simulate
44-
initialize!(mech_lc, :box,
44+
initialize!(mech_lc, :block,
4545
x=x0,
4646
q=one(Quaternion),
4747
v=v0,
@@ -59,7 +59,7 @@ via, anim = visualize(mech_lc, storage_lc,
5959

6060
# ## Nonlinear cone
6161
color_nc = cyan;
62-
mech_nc = get_mechanism(:box,
62+
mech_nc = get_mechanism(:block,
6363
timestep=timestep,
6464
gravity=gravity,
6565
friction_coefficient=friction_coefficient,
@@ -68,7 +68,7 @@ mech_nc = get_mechanism(:box,
6868
color=color_nc);
6969

7070
# ## Simulate
71-
initialize!(mech_nc, :box,
71+
initialize!(mech_nc, :block,
7272
x=x0,
7373
q=one(Quaternion),
7474
v=v0,
@@ -91,15 +91,15 @@ vis, anim = visualize(mech_nc, storage_nc,
9191
# ## MuJoCo pyramidal cone
9292
color_mjlc = magenta;
9393

94-
mech_mjlc = get_mechanism(:box,
94+
mech_mjlc = get_mechanism(:block,
9595
timestep=timestep,
9696
gravity=gravity,
9797
friction_coefficient=friction_coefficient,
9898
contact_type=:linear,
9999
mode=:box, color=color_mjlc);
100100

101101
# ## Load
102-
initialize!(mech_mjlc, :box,
102+
initialize!(mech_mjlc, :block,
103103
x=x0,
104104
q=one(Quaternion),
105105
v=v0,
@@ -125,15 +125,15 @@ setobject!(vis[:path_mjlc], MeshCat.Line(points_mjlc, line_mat_mjlc))
125125
# ## MuJoCo elliptic cone
126126
color_mjnc = RGBA(0,0,0);
127127

128-
mech_mjnc = get_mechanism(:box,
128+
mech_mjnc = get_mechanism(:block,
129129
timestep=timestep,
130130
gravity=gravity,
131131
friction_coefficient=friction_coefficient,
132132
contact_type=:linear,
133133
mode=:box, color=color_mjnc);
134134

135135
# ## Load
136-
initialize!(mech_mjnc, :box,
136+
initialize!(mech_mjnc, :block,
137137
x=x0,
138138
q=one(Quaternion),
139139
v=v0,

examples/system_identification/data/data.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function generate_hardware_dataset(;N::Int=10,
6464
timestep= 1/148 * S
6565
gscaled = -9.81*20
6666

67-
mechanism = get_mechanism(:box, timestep=timestep, gravity=gravityscaled);
67+
mechanism = get_mechanism(:block, timestep=timestep, gravity=gravityscaled);
6868
trajs = []
6969
pairs = []
7070
for i = 1:N

examples/system_identification/hardware_examples/visualization.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function d2data(d)
7272
]
7373
return data
7474
end
75-
mech = get_mechanism(:box, timestep=timestep/S, gravity=gravityscaled, friction_coefficient=Dsol[end][1], radius=0.00, side=2.0, mode=:box);
75+
mech = get_mechanism(:block, timestep=timestep/S, gravity=gravityscaled, friction_coefficient=Dsol[end][1], radius=0.00, side=2.0, mode=:box);
7676
set_simulator_data!(mech, d2data(Dsol[end]))
7777
id = 7#4,6,7,8
7878
traj_truth = trajs1[id]
@@ -81,7 +81,7 @@ v15 = traj_truth.v[1][1]
8181
q2 = traj_truth.q[1][1]
8282
ϕ15 = traj_truth.ω[1][1]
8383

84-
initialize!(mech, :box, x=x2, v=v15, q=q2, ω=ϕ15)
84+
initialize!(mech, :block, x=x2, v=v15, q=q2, ω=ϕ15)
8585
traj_sim = simulate!(mech, 0.80, record=true,
8686
opts=SolverOptions(btol=1e-6, rtol=1e-6, verbose=false))
8787

examples/system_identification/learning.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ open(vis)
2222
S = 7
2323
timestep= 1/148 * S
2424
gscaled = -9.81*20
25-
mech = get_mechanism(:box,
25+
mech = get_mechanism(:block,
2626
timestep=timestep,
2727
gravity=gravityscaled,
2828
friction_coefficient=0.2,
@@ -31,7 +31,7 @@ mech = get_mechanism(:box,
3131
mode=:box);
3232

3333
# ## Simualate
34-
initialize!(mech, :box,
34+
initialize!(mech, :block,
3535
x=[0.0, -1.0, 1.0],
3636
v=[0.0, 2.0, 1.0],
3737
ω=[2.0, 5.0, 10.0])
@@ -61,18 +61,18 @@ data0 = params0[:data]
6161
################################################################################
6262
## Optimization Objective: Evaluation & Gradient
6363
################################################################################
64-
clean_loss(:box, pairs0, data0,
64+
clean_loss(:block, pairs0, data0,
6565
n_sample=250,
6666
opts=SolverOptions(btol=3e-4, rtol=3e-4),
6767
timestep=timestep,
6868
gravity=gravityscaled)
6969

70-
[clean_loss(:box, pairs0, data0 + [i;zeros(55)],
70+
[clean_loss(:block, pairs0, data0 + [i;zeros(55)],
7171
opts=SolverOptions(btol=3e-4, rtol=3e-4),
7272
timestep=timestep,
7373
gravity=gravityscaled)
7474
for i in Vector(-0.10:0.01:0.1)]
75-
[clean_loss(:box, pairs0, data0 + [0;i;zeros(54)],
75+
[clean_loss(:block, pairs0, data0 + [0;i;zeros(54)],
7676
opts=SolverOptions(btol=3e-4, rtol=3e-4),
7777
timestep=timestep,
7878
gravity=gravityscaled)
@@ -108,7 +108,7 @@ lower = [0.00, 0.05]
108108
upper = [0.80, 1.50]
109109

110110
function f0(d; rot=0)
111-
return clean_loss(:box, pairs0, d2data(d),
111+
return clean_loss(:block, pairs0, d2data(d),
112112
n_sample=200,
113113
timestep=timestep,
114114
gravity=gravityscaled,
@@ -118,7 +118,7 @@ end
118118

119119
function fgH0(d;
120120
rot=0)
121-
f, g, H = clean_loss(:box, pairs0, d2data(d),
121+
f, g, H = clean_loss(:block, pairs0, d2data(d),
122122
n_sample=200,
123123
timestep=timestep,
124124
gravity=gravityscaled,
@@ -151,7 +151,7 @@ dsol #[0.171, 0.933]
151151
using Plots; pyplot()
152152
x=range(0.00,stop=0.80,length=10)
153153
y=range(0.10,stop=1.50,length=10)
154-
f(x,y) = log(10, clean_loss(:box, pairs0, d2data([x,y]), n_sample=50, timestep=timestep, gravity=gravityscaled,
154+
f(x,y) = log(10, clean_loss(:block, pairs0, d2data([x,y]), n_sample=50, timestep=timestep, gravity=gravityscaled,
155155
opts=SolverOptions(btol=3e-4, rtol=3e-4))[1])
156156
plot(x,y,f,st=:surface,camera=(25,75))
157157

@@ -212,11 +212,11 @@ upper = [0.80,
212212
-0.05, -0.05, +1.50]
213213

214214
function f0(d; rot=0, n_sample=50)
215-
return clean_loss(:box, pairs0, d2data(d), n_sample=n_sample, timestep=timestep, gravity=gravityscaled, rot=rot, opts=SolverOptions(btol=3e-4, rtol=3e-4))[1]
215+
return clean_loss(:block, pairs0, d2data(d), n_sample=n_sample, timestep=timestep, gravity=gravityscaled, rot=rot, opts=SolverOptions(btol=3e-4, rtol=3e-4))[1]
216216
end
217217

218218
function fgH0(d; rot=0, n_sample=50)
219-
f, g, H = clean_loss(:box, pairs0, d2data(d), n_sample=n_sample, timestep=timestep, gravity=gravityscaled, rot=rot, opts=SolverOptions(btol=3e-4, rtol=3e-4))
219+
f, g, H = clean_loss(:block, pairs0, d2data(d), n_sample=n_sample, timestep=timestep, gravity=gravityscaled, rot=rot, opts=SolverOptions(btol=3e-4, rtol=3e-4))
220220
return f, ∇d2data' * g, ∇d2data' * H * ∇d2data
221221
end
222222
dsol, Dsol = quasi_newton_solve(f0, fgH0, d0, iter=50, gtol=1e-8, ftol=1e-6,

0 commit comments

Comments
 (0)