-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathplanner.cc
More file actions
535 lines (442 loc) · 17.1 KB
/
Copy pathplanner.cc
File metadata and controls
535 lines (442 loc) · 17.1 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
// Copyright 2022 DeepMind Technologies Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "mjpc/planners/sampling/planner.h"
#include <algorithm>
#include <chrono>
#include <shared_mutex>
#include <absl/random/random.h>
#include <mujoco/mujoco.h>
#include "mjpc/array_safety.h"
#include "mjpc/planners/planner.h"
#include "mjpc/planners/sampling/policy.h"
#include "mjpc/spline/spline.h"
#include "mjpc/states/state.h"
#include "mjpc/task.h"
#include "mjpc/threadpool.h"
#include "mjpc/trajectory.h"
#include "mjpc/utilities.h"
namespace mjpc {
namespace mju = ::mujoco::util_mjpc;
using mjpc::spline::SplineInterpolation;
using mjpc::spline::TimeSpline;
// initialize data and settings
void SamplingPlanner::Initialize(mjModel* model, const Task& task) {
// delete mjData instances since model might have changed.
data_.clear();
// allocate one mjData for nominal.
ResizeMjData(model, 1);
// model
this->model = model;
// task
this->task = &task;
// sampling noise std
noise_exploration[0] = GetNumberOrDefault(0.1, model, "sampling_exploration");
// optional second std (defaults to 0)
int se_id = mj_name2id(model, mjOBJ_NUMERIC, "sampling_exploration");
if (se_id >= 0 && model->numeric_size[se_id] > 1) {
int se_adr = model->numeric_adr[se_id];
noise_exploration[1] = model->numeric_data[se_adr+1];
}
// set number of trajectories to rollout
num_trajectory_ = GetNumberOrDefault(10, model, "sampling_trajectories");
interpolation_ = GetNumberOrDefault(SplineInterpolation::kCubicSpline, model,
"sampling_representation");
sliding_plan_ = GetNumberOrDefault(0, model, "sampling_sliding_plan");
if (num_trajectory_ > kMaxTrajectory) {
mju_error_i("Too many trajectories, %d is the maximum allowed.",
kMaxTrajectory);
}
winner = 0;
}
// allocate memory
void SamplingPlanner::Allocate() {
// initial state
int num_state = model->nq + model->nv + model->na;
// state
state.resize(num_state);
mocap.resize(7 * model->nmocap);
userdata.resize(model->nuserdata);
// policy
policy.Allocate(model, *task, kMaxTrajectoryHorizon);
previous_policy.Allocate(model, *task, kMaxTrajectoryHorizon);
plan_scratch = TimeSpline(/*dim=*/model->nu);
// noise
noise.resize(kMaxTrajectory * (model->nu * kMaxTrajectoryHorizon));
// trajectory and parameters
winner = -1;
for (int i = 0; i < kMaxTrajectory; i++) {
trajectory[i].Initialize(num_state, model->nu, task->num_residual,
task->num_trace, kMaxTrajectoryHorizon);
trajectory[i].Allocate(kMaxTrajectoryHorizon);
candidate_policy[i].Allocate(model, *task, kMaxTrajectoryHorizon);
}
}
// reset memory to zeros
void SamplingPlanner::Reset(int horizon,
const double* initial_repeated_action) {
// state
std::fill(state.begin(), state.end(), 0.0);
std::fill(mocap.begin(), mocap.end(), 0.0);
std::fill(userdata.begin(), userdata.end(), 0.0);
time = 0.0;
// policy parameters
policy.Reset(horizon, initial_repeated_action);
previous_policy.Reset(horizon, initial_repeated_action);
// scratch
plan_scratch.Clear();
// noise
std::fill(noise.begin(), noise.end(), 0.0);
// trajectory samples
for (int i = 0; i < kMaxTrajectory; i++) {
trajectory[i].Reset(kMaxTrajectoryHorizon);
candidate_policy[i].Reset(horizon, initial_repeated_action);
}
for (const auto& d : data_) {
if (initial_repeated_action) {
mju_copy(d->ctrl, initial_repeated_action, model->nu);
} else {
mju_zero(d->ctrl, model->nu);
}
}
// improvement
improvement = 0.0;
// winner
winner = 0;
}
// set state
void SamplingPlanner::SetState(const State& state) {
state.CopyTo(this->state.data(), this->mocap.data(), this->userdata.data(),
&this->time);
}
int SamplingPlanner::OptimizePolicyCandidates(int ncandidates, int horizon,
ThreadPool& pool) {
// if num_trajectory_ has changed, use it in this new iteration.
// num_trajectory_ might change while this function runs. Keep it constant
// for the duration of this function.
int num_trajectory = num_trajectory_;
ncandidates = std::min(ncandidates, num_trajectory);
ResizeMjData(model, pool.NumThreads());
// ----- rollout noisy policies ----- //
// start timer
auto rollouts_start = std::chrono::steady_clock::now();
// simulate noisy policies
policy.plan.SetInterpolation(interpolation_);
this->Rollouts(num_trajectory, horizon, pool);
// sort candidate policies and trajectories by score
trajectory_order.clear();
trajectory_order.reserve(num_trajectory);
for (int i = 0; i < num_trajectory; i++) {
trajectory_order.push_back(i);
}
// sort so that the first ncandidates elements are the best candidates, and
// the rest are in an unspecified order
std::partial_sort(
trajectory_order.begin(), trajectory_order.begin() + ncandidates,
trajectory_order.end(), [trajectory = trajectory](int a, int b) {
return trajectory[a].total_return < trajectory[b].total_return;
});
// stop timer
rollouts_compute_time = GetDuration(rollouts_start);
return ncandidates;
}
// optimize nominal policy using random sampling
void SamplingPlanner::OptimizePolicy(int horizon, ThreadPool& pool) {
// resample nominal policy to current time
this->UpdateNominalPolicy(horizon);
OptimizePolicyCandidates(1, horizon, pool);
// ----- update policy ----- //
// start timer
auto policy_update_start = std::chrono::steady_clock::now();
CopyCandidateToPolicy(0);
// improvement: compare nominal to winner
double best_return = trajectory[0].total_return;
improvement = mju_max(best_return - trajectory[winner].total_return, 0.0);
// stop timer
policy_update_compute_time = GetDuration(policy_update_start);
}
// compute trajectory using nominal policy
void SamplingPlanner::NominalTrajectory(int horizon, ThreadPool& pool) {
// set policy
auto nominal_policy = [&cp = candidate_policy[0]](
double* action, const double* state, double time) {
cp.Action(action, state, time);
};
// rollout nominal policy
trajectory[0].Rollout(nominal_policy, task, model, data_[0].get(),
state.data(), time, mocap.data(), userdata.data(),
horizon);
}
// set action from policy
void SamplingPlanner::ActionFromPolicy(double* action, const double* state,
double time, bool use_previous) {
const std::shared_lock<std::shared_mutex> lock(mtx_);
if (use_previous) {
previous_policy.Action(action, state, time);
} else {
policy.Action(action, state, time);
}
}
// update policy via resampling
void SamplingPlanner::UpdateNominalPolicy(int horizon) {
// dimensions
int num_spline_points = candidate_policy[winner].num_spline_points;
// set time
double nominal_time = time;
double time_horizon = (horizon - 1) * model->opt.timestep;
if (sliding_plan_) {
// extra points required outside of the horizon window
int extra_points;
switch (interpolation_) {
case spline::SplineInterpolation::kZeroSpline:
extra_points = 1;
break;
case spline::SplineInterpolation::kLinearSpline:
extra_points = 2;
break;
case spline::SplineInterpolation::kCubicSpline:
extra_points = 4;
break;
}
// temporal distance between spline points
double time_shift;
if (num_spline_points > extra_points) {
time_shift = mju_max(time_horizon /
(num_spline_points - extra_points), 1.0e-5);
} else {
// not a valid setting, but avoid division by zero
time_shift = time_horizon;
}
const std::shared_lock<std::shared_mutex> lock(mtx_);
policy.plan.DiscardBefore(nominal_time);
if (policy.plan.Size() == 0) {
policy.plan.AddNode(time);
}
while (policy.plan.Size() < num_spline_points) {
// duplicate the last node, with a time further in the future.
double new_node_time = (policy.plan.end() - 1)->time() + time_shift;
TimeSpline::Node new_node = policy.plan.AddNode(new_node_time);
std::copy((policy.plan.end() - 2)->values().begin(),
(policy.plan.end() - 2)->values().end(),
new_node.values().begin());
}
} else {
// non-sliding, resample the plan into a scratch plan
double time_shift;
if (interpolation_ == spline::SplineInterpolation::kZeroSpline) {
time_shift = mju_max(time_horizon / num_spline_points, 1.0e-5);
} else {
time_shift = mju_max(time_horizon / (num_spline_points - 1), 1.0e-5);
}
// resample the nominal plan on a new set of spline points
plan_scratch.Clear();
plan_scratch.SetInterpolation(interpolation_);
plan_scratch.Reserve(num_spline_points);
// get spline points
for (int t = 0; t < num_spline_points; t++) {
TimeSpline::Node node = plan_scratch.AddNode(nominal_time);
candidate_policy[winner].Action(node.values().data(), /*state=*/nullptr,
nominal_time);
nominal_time += time_shift;
}
// copy scratch into plan
{
const std::shared_lock<std::shared_mutex> lock(mtx_);
policy.plan = plan_scratch;
}
}
}
// add random noise to nominal policy
void SamplingPlanner::AddNoiseToPolicy(double start_time, int i) {
// start timer
auto noise_start = std::chrono::steady_clock::now();
// sampling token
absl::BitGen gen_;
// get standard deviation, fixed or mixture of noise_exploration[0,1]
double std = noise_exploration[0];
constexpr double kStd2Proportion = 0.2; // hardcoded proportion of 2nd std
if (noise_exploration[1] > 0 && absl::Bernoulli(gen_, kStd2Proportion)) {
std = noise_exploration[1];
}
for (const TimeSpline::Node& node : candidate_policy[i].plan) {
for (int k = 0; k < model->nu; k++) {
double scale = 0.5 * (model->actuator_ctrlrange[2 * k + 1] -
model->actuator_ctrlrange[2 * k]);
double noise = absl::Gaussian<double>(gen_, 0.0, scale * std);
node.values()[k] += noise;
}
Clamp(node.values().data(), model->actuator_ctrlrange, model->nu);
}
// end timer
IncrementAtomic(noise_compute_time, GetDuration(noise_start));
}
// compute candidate trajectories
void SamplingPlanner::Rollouts(int num_trajectory, int horizon,
ThreadPool& pool) {
// reset noise compute time
noise_compute_time = 0.0;
// random search
int count_before = pool.GetCount();
for (int i = 0; i < num_trajectory; i++) {
pool.Schedule([&s = *this, &model = this->model, &task = this->task,
&state = this->state, &time = this->time,
&mocap = this->mocap, &userdata = this->userdata, horizon,
i]() {
// copy nominal policy
{
const std::shared_lock<std::shared_mutex> lock(s.mtx_);
s.candidate_policy[i].CopyFrom(s.policy, s.policy.num_spline_points);
}
// sample noise policy
if (i != 0) s.AddNoiseToPolicy(time, i);
// ----- rollout sample policy ----- //
// policy
auto sample_policy_i = [&candidate_policy = s.candidate_policy, &i](
double* action, const double* state,
double time) {
candidate_policy[i].Action(action, state, time);
};
// policy rollout
s.trajectory[i].Rollout(
sample_policy_i, task, model, s.data_[ThreadPool::WorkerId()].get(),
state.data(), time, mocap.data(), userdata.data(), horizon);
});
}
pool.WaitCount(count_before + num_trajectory);
pool.ResetCount();
}
// return trajectory with best total return
const Trajectory* SamplingPlanner::BestTrajectory() {
return winner >= 0 ? &trajectory[winner] : nullptr;
}
// visualize planner-specific traces
void SamplingPlanner::Traces(mjvScene* scn) {
// sample color
float color[4];
color[0] = 1.0;
color[1] = 1.0;
color[2] = 1.0;
color[3] = 1.0;
// width of a sample trace, in pixels
double width = GetNumberOrDefault(3, model, "agent_sample_width");
// scratch
double zero3[3] = {0};
double zero9[9] = {0};
// best
auto best = this->BestTrajectory();
// sample traces
for (int k = 0; k < num_trajectory_; k++) {
// skip winner
if (k == winner) continue;
// plot sample
for (int i = 0; i < best->horizon - 1; i++) {
if (scn->ngeom + task->num_trace > scn->maxgeom) break;
for (int j = 0; j < task->num_trace; j++) {
// initialize geometry
mjv_initGeom(&scn->geoms[scn->ngeom], mjGEOM_LINE, zero3, zero3, zero9,
color);
// make geometry
mjv_makeConnector(
&scn->geoms[scn->ngeom], mjGEOM_LINE, width,
trajectory[k].trace[3 * task->num_trace * i + 3 * j],
trajectory[k].trace[3 * task->num_trace * i + 1 + 3 * j],
trajectory[k].trace[3 * task->num_trace * i + 2 + 3 * j],
trajectory[k].trace[3 * task->num_trace * (i + 1) + 3 * j],
trajectory[k].trace[3 * task->num_trace * (i + 1) + 1 + 3 * j],
trajectory[k].trace[3 * task->num_trace * (i + 1) + 2 + 3 * j]);
// increment number of geometries
scn->ngeom += 1;
}
}
}
}
// planner-specific GUI elements
void SamplingPlanner::GUI(mjUI& ui) {
mjuiDef defSampling[] = {
{mjITEM_SLIDERINT, "Rollouts", 2, &num_trajectory_, "0 1"},
{mjITEM_SELECT, "Spline", 2, &interpolation_,
"Zero\nLinear\nCubic"},
{mjITEM_SLIDERINT, "Spline Pts", 2, &policy.num_spline_points, "0 1"},
{mjITEM_SLIDERNUM, "Noise Std", 2, noise_exploration, "0 1"},
{mjITEM_SLIDERNUM, "Noise Std2", 2, noise_exploration+1, "0 1"},
{mjITEM_CHECKBYTE, "Sliding plan", 2, &sliding_plan_, ""},
{mjITEM_END}};
// set number of trajectory slider limits
mju::sprintf_arr(defSampling[0].other, "%i %i", 1, kMaxTrajectory);
// set spline point limits
mju::sprintf_arr(defSampling[2].other, "%i %i", MinSamplingSplinePoints,
MaxSamplingSplinePoints);
// set noise standard deviation limits
mju::sprintf_arr(defSampling[3].other, "%f %f", MinNoiseStdDev,
MaxNoiseStdDev);
// add sampling planner
mjui_add(&ui, defSampling);
}
// planner-specific plots
void SamplingPlanner::Plots(mjvFigure* fig_planner, mjvFigure* fig_timer,
int planner_shift, int timer_shift, int planning,
int* shift) {
// ----- planner ----- //
double planner_bounds[2] = {-6.0, 6.0};
// improvement
mjpc::PlotUpdateData(fig_planner, planner_bounds,
fig_planner->linedata[0 + planner_shift][0] + 1,
mju_log10(mju_max(improvement, 1.0e-6)), 100,
0 + planner_shift, 0, 1, -100);
// legend
mju::strcpy_arr(fig_planner->linename[0 + planner_shift], "Improvement");
fig_planner->range[1][0] = planner_bounds[0];
fig_planner->range[1][1] = planner_bounds[1];
// bounds
double timer_bounds[2] = {0.0, 1.0};
// ----- timer ----- //
PlotUpdateData(fig_timer, timer_bounds,
fig_timer->linedata[0 + timer_shift][0] + 1,
1.0e-3 * noise_compute_time * planning, 100,
0 + timer_shift, 0, 1, -100);
PlotUpdateData(fig_timer, timer_bounds,
fig_timer->linedata[1 + timer_shift][0] + 1,
1.0e-3 * rollouts_compute_time * planning, 100,
1 + timer_shift, 0, 1, -100);
PlotUpdateData(fig_timer, timer_bounds,
fig_timer->linedata[2 + timer_shift][0] + 1,
1.0e-3 * policy_update_compute_time * planning, 100,
2 + timer_shift, 0, 1, -100);
// legend
mju::strcpy_arr(fig_timer->linename[0 + timer_shift], "Noise");
mju::strcpy_arr(fig_timer->linename[1 + timer_shift], "Rollout");
mju::strcpy_arr(fig_timer->linename[2 + timer_shift], "Policy Update");
// planner shift
shift[0] += 1;
// timer shift
shift[1] += 3;
}
double SamplingPlanner::CandidateScore(int candidate) const {
return trajectory[trajectory_order[candidate]].total_return;
}
// set action from candidate policy
void SamplingPlanner::ActionFromCandidatePolicy(double* action, int candidate,
const double* state,
double time) {
candidate_policy[trajectory_order[candidate]].Action(action, state, time);
}
void SamplingPlanner::CopyCandidateToPolicy(int candidate) {
// set winner
winner = trajectory_order[candidate];
{
const std::shared_lock<std::shared_mutex> lock(mtx_);
previous_policy = policy;
policy = candidate_policy[winner];
}
}
} // namespace mjpc