Skip to content

Commit 7fdb52b

Browse files
committed
fix LogScale, adjust sample gradient planner step size
1 parent 8ef5ded commit 7fdb52b

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

mjpc/planners/sample_gradient/planner.cc

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,12 @@ void SampleGradientPlanner::GradientCandidates(int num_trajectory,
476476
num_parameters);
477477
}
478478

479-
// compute step sizes along gradient
480-
std::vector<double> step_size(num_gradient);
481-
LogScale(step_size.data(), gradient_max_step_size, gradient_min_step_size,
482-
num_gradient);
479+
// compute step sizes for gradient direction
480+
if (step_size_.size() != num_gradient) {
481+
step_size_.resize(num_gradient);
482+
LogScale(step_size_.data(), gradient_max_step_size, gradient_min_step_size,
483+
num_gradient);
484+
}
483485

484486
// gradient filter gf * grad + (1 - gf) * grad_prev
485487
double gradient_filter = gradient_filter_;
@@ -492,7 +494,7 @@ void SampleGradientPlanner::GradientCandidates(int num_trajectory,
492494
candidate_policy[i].representation = resampled_policy.representation;
493495

494496
// scaling
495-
double scaling = step_size[i - num_noisy] / noise_exploration;
497+
double scaling = step_size_[i - num_noisy] / noise_exploration;
496498

497499
// gradient step
498500
mju_addToScl(candidate_policy[i].parameters.data(), gradient.data(),
@@ -620,17 +622,23 @@ void SampleGradientPlanner::Plots(mjvFigure* fig_planner, mjvFigure* fig_timer,
620622
mju_log10(mju_max(improvement, 1.0e-6)), 100,
621623
0 + planner_shift, 0, 1, -100);
622624

623-
// winner type
624-
double winner_type =
625-
winner_type_ == kPerturb ? -6.0 : (winner_type_ == kGradient ? 6.0 : 0.0);
625+
// winner plot value
626+
double winner_plot_val = -6.0; // nominal
627+
if (winner_type_ == kPerturb) {
628+
winner_plot_val = 0.0;
629+
} else if (winner_type_ == kGradient) {
630+
int num_noisy = num_trajectory_ - num_gradient_;
631+
winner_plot_val = 6.0 * (winner - num_noisy) / num_gradient_;
632+
}
633+
626634
mjpc::PlotUpdateData(fig_planner, planner_bounds,
627635
fig_planner->linedata[1 + planner_shift][0] + 1,
628-
winner_type, 100, 1 + planner_shift, 0, 1, -100);
636+
winner_plot_val, 100, 1 + planner_shift, 0, 1, -100);
629637

630638
// legend
631639
mju::strcpy_arr(fig_planner->linename[0 + planner_shift], "Improvement");
632640
mju::strcpy_arr(fig_planner->linename[1 + planner_shift],
633-
"Perturb|Nominal|Gradient");
641+
"Nominal|Perturb|Gradient");
634642

635643
fig_planner->range[1][0] = planner_bounds[0];
636644
fig_planner->range[1][1] = planner_bounds[1];

mjpc/planners/sample_gradient/planner.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ class SampleGradientPlanner : public Planner {
152152
std::vector<double> gradient;
153153
std::vector<double> gradient_previous;
154154
double gradient_filter_ = 1.0;
155+
156+
// gradient step size
157+
std::vector<double> step_size_;
155158
double gradient_max_step_size = 2.0;
156159
double gradient_min_step_size = 1.0e-3;
157160

mjpc/utilities.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,9 @@ bool CheckWarnings(mjData* data) {
839839
// compute vector with log-based scaling between min and max values
840840
void LogScale(double* values, double max_value, double min_value, int steps) {
841841
double step =
842-
mju_log(max_value) - mju_log(min_value) / mju_max((steps - 1), 1);
842+
(std::log(max_value) - std::log(min_value)) / std::max((steps - 1), 1);
843843
for (int i = 0; i < steps; i++) {
844-
values[i] = mju_exp(mju_log(min_value) + i * step);
844+
values[i] = std::exp(std::log(min_value) + i * step);
845845
}
846846
}
847847

0 commit comments

Comments
 (0)