/usr/include/roboptim/core/finite-difference-gradient.hxx is in libroboptim-core-dev 2.0-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | // Copyright (C) 2009 by Thomas Moulard, AIST, CNRS, INRIA.
//
// This file is part of the roboptim.
//
// roboptim is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// roboptim is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with roboptim. If not, see <http://www.gnu.org/licenses/>.
#ifndef ROBOPTIM_CORE_FINITE_DIFFERENCE_GRADIENT_HXX
# define ROBOPTIM_CORE_FINITE_DIFFERENCE_GRADIENT_HXX
# include "boost/type_traits/is_same.hpp"
# include <boost/mpl/same_as.hpp>
namespace roboptim
{
template <typename T>
BadGradient<T>::BadGradient (const vector_t& x,
const gradient_t& analyticalGradient,
const gradient_t& finiteDifferenceGradient,
const value_type& threshold)
: std::runtime_error ("bad gradient"),
x_ (x),
analyticalGradient_ (analyticalGradient),
finiteDifferenceGradient_ (finiteDifferenceGradient),
maxDelta_ (),
maxDeltaComponent_ (),
threshold_ (threshold)
{
assert (analyticalGradient.size () == finiteDifferenceGradient.size ());
maxDelta_ = -std::numeric_limits<Function::value_type>::infinity ();
for (size_type i = 0; i < analyticalGradient.size (); ++i)
{
value_type delta =
std::fabs (analyticalGradient[i] - finiteDifferenceGradient[i]);
if (delta > maxDelta_)
{
maxDelta_ = delta;
maxDeltaComponent_ = i;
}
}
}
template <typename T>
BadGradient<T>::~BadGradient () throw ()
{}
template <typename T>
std::ostream&
BadGradient<T>::print (std::ostream& o) const throw ()
{
o << this->what () << incindent << iendl
<< "X: " << x_ << iendl
<< "Analytical gradient: " << analyticalGradient_ << iendl
<< "Finite difference gradient: " << finiteDifferenceGradient_
<< iendl
<< "Max. delta: " << maxDelta_ << iendl
<< "Max. delta in component: " << maxDeltaComponent_ << iendl
<< "Max. allowed delta: " << threshold_ << decindent;
return o;
}
template <typename T>
std::ostream&
operator<< (std::ostream& o, const BadGradient<T>& bg)
{
return bg.print (o);
}
template <typename T, typename FdgPolicy>
GenericFiniteDifferenceGradient<T, FdgPolicy>::GenericFiniteDifferenceGradient
(const GenericFunction<T>& adaptee,
typename GenericDifferentiableFunction<T>::value_type epsilon)
throw ()
: GenericDifferentiableFunction<T>
(adaptee.inputSize (), adaptee.outputSize ()),
FdgPolicy (),
adaptee_ (adaptee),
epsilon_ (epsilon),
xEps_ (adaptee.inputSize ())
{
// Avoid meaningless values for epsilon such as 0 or NaN.
assert (epsilon != 0. && epsilon == epsilon);
}
template <typename T, typename FdgPolicy>
GenericFiniteDifferenceGradient<
T, FdgPolicy>::~GenericFiniteDifferenceGradient () throw ()
{
}
template <typename T, typename FdgPolicy>
void
GenericFiniteDifferenceGradient<T, FdgPolicy>::impl_compute
(result_t& result, const argument_t& argument) const throw ()
{
adaptee_ (result, argument);
}
template <typename T, typename FdgPolicy>
void
GenericFiniteDifferenceGradient<T, FdgPolicy>::impl_gradient
(gradient_t& gradient,
const argument_t& argument,
size_type idFunction) const throw ()
{
#ifndef ROBOPTIM_DO_NOT_CHECK_ALLOCATION
Eigen::internal::set_is_malloc_allowed (true);
#endif //! ROBOPTIM_DO_NOT_CHECK_ALLOCATION
this->computeGradient (adaptee_, epsilon_, gradient,
argument, idFunction, xEps_);
}
template <typename T>
bool
checkGradient
(const GenericDifferentiableFunction<T>& function,
typename GenericDifferentiableFunction<T>::size_type functionId,
const typename GenericDifferentiableFunction<T>::vector_t& x,
typename GenericDifferentiableFunction<T>::value_type threshold)
throw ()
{
GenericFiniteDifferenceGradient<T> fdfunction (function);
typename GenericDifferentiableFunction<T>::gradient_t grad =
function.gradient (x, functionId);
typename GenericDifferentiableFunction<T>::gradient_t fdgrad =
fdfunction.gradient (x, functionId);
return grad.isApprox (fdgrad, threshold);
return true;
}
template <typename T>
void
checkGradientAndThrow
(const GenericDifferentiableFunction<T>& function,
typename GenericDifferentiableFunction<T>::size_type functionId,
const typename GenericDifferentiableFunction<T>::vector_t& x,
typename GenericDifferentiableFunction<T>::value_type threshold)
throw (BadGradient<T>)
{
GenericFiniteDifferenceGradient<T> fdfunction (function);
DifferentiableFunction::gradient_t grad =
function.gradient (x, functionId);
DifferentiableFunction::gradient_t fdgrad =
fdfunction.gradient (x, functionId);
if (!checkGradient (function, functionId, x, threshold))
throw BadGradient<T> (x, grad, fdgrad, threshold);
}
namespace detail
{
template <typename T>
void
compute_deriv (const GenericFunction<T>& adaptee,
typename GenericFunction<T>::size_type j,
double h,
double& result,
double& round,
double& trunc,
const typename GenericFunction<T>::argument_t& argument,
typename GenericFunction<T>::size_type idFunction,
typename GenericFunction<T>::argument_t& xEps);
/// Algorithm from the Gnu Scientific Library.
template <typename T>
void
compute_deriv (const GenericFunction<T>& adaptee,
typename GenericFunction<T>::size_type j,
double h,
double& result,
double& round,
double& trunc,
const typename GenericFunction<T>::argument_t& argument,
typename GenericFunction<T>::size_type idFunction,
typename GenericFunction<T>::argument_t& xEps)
{
/* Compute the derivative using the 5-point rule (x-h, x-h/2, x,
x+h/2, x+h). Note that the central point is not used.
Compute the error using the difference between the 5-point and
the 3-point rule (x-h,x,x+h). Again the central point is not
used. */
xEps = argument;
xEps[j] = argument[j] - h;
double fm1 = adaptee (xEps)[idFunction];
xEps[j] = argument[j] + h;
double fp1 = adaptee (xEps)[idFunction];
xEps[j] = argument[j] - (h / 2.);
double fmh = adaptee (xEps)[idFunction];
xEps[j] = argument[j] + (h / 2.);
double fph = adaptee (xEps)[idFunction];
double r3 = .5 * (fp1 - fm1);
double r5 = (4. / 3.) * (fph - fmh) - (1. / 3.) * r3;
double e3 = (std::fabs (fp1) + std::fabs (fm1))
* std::numeric_limits<double>::epsilon ();
double e5 = 2. * (std::fabs (fph) + std::fabs (fmh))
* std::numeric_limits<double>::epsilon () + e3;
/* The next term is due to finite precision in x+h = O (eps * x) */
double dy =
std::max (std::fabs (r3 / h), std::fabs (r5 / h))
* (std::fabs (argument[j]) / h)
* std::numeric_limits<double>::epsilon ();
/* The truncation error in the r5 approximation itself is O(h^4).
However, for safety, we estimate the error from r5-r3, which is
O(h^2). By scaling h we will minimise this estimated error, not
the actual truncation error in r5. */
result = r5 / h;
trunc = std::fabs ((r5 - r3) / h); // Estimated truncation error O(h^2)
round = std::fabs (e5 / h) + dy; // Rounding error (cancellations)
}
} // end of namespace detail.
namespace finiteDifferenceGradientPolicies
{
template <>
inline void
Simple<EigenMatrixSparse>::computeGradient
(const GenericFunction<EigenMatrixSparse>& adaptee,
value_type epsilon,
gradient_t& gradient,
const argument_t& argument,
size_type idFunction,
argument_t& xEps) const throw ()
{
assert (adaptee.outputSize () - idFunction > 0);
result_t res = adaptee (argument);
for (size_type j = 0; j < adaptee.inputSize (); ++j)
{
xEps = argument;
xEps[j] += epsilon;
result_t resEps = adaptee (xEps);
gradient.insert (j) =
(resEps[idFunction] - res[idFunction]) / epsilon;
}
}
template <typename T>
void
Simple<T>::computeGradient
(const GenericFunction<T>& adaptee,
value_type epsilon,
gradient_t& gradient,
const argument_t& argument,
size_type idFunction,
argument_t& xEps) const throw ()
{
assert (adaptee.outputSize () - idFunction > 0);
result_t res = adaptee (argument);
for (size_type j = 0; j < adaptee.inputSize (); ++j)
{
xEps = argument;
xEps[j] += epsilon;
typename GenericFunction<T>::result_t resEps = adaptee (xEps);
gradient (j) = (resEps[idFunction] - res[idFunction]) / epsilon;
}
}
template <>
inline void
FivePointsRule<EigenMatrixSparse>::computeGradient
(const GenericFunction<EigenMatrixSparse>& adaptee,
value_type epsilon,
gradient_t& gradient,
const argument_t& argument,
size_type idFunction,
argument_t& xEps) const throw ()
{
assert (adaptee.outputSize () - idFunction > 0);
value_type h = epsilon / 2.;
value_type r_0 = 0.;
value_type round = 0.;
value_type trunc = 0.;
value_type error = 0.;
for (size_type j = 0; j < argument.size (); ++j)
{
detail::compute_deriv (adaptee, j, h,
r_0, round, trunc,
argument, idFunction, xEps);
error = round + trunc;
if (round < trunc && (round > 0 && trunc > 0))
{
value_type r_opt = 0., round_opt = 0., trunc_opt = 0.,
error_opt = 0.;
/* Compute an optimised stepsize to minimize the total error,
using the scaling of the truncation error (O(h^2)) and
rounding error (O(1/h)). */
value_type h_opt =
h * std::pow (round / (2. * trunc), 1. / 3.);
detail::compute_deriv (adaptee, j, h_opt,
r_opt, round_opt, trunc_opt,
argument, idFunction,
xEps);
error_opt = round_opt + trunc_opt;
/* Check that the new error is smaller, and that the new
derivative is consistent with the error bounds of the
original estimate. */
if (error_opt < error && std::fabs (r_opt - r_0) < 4. * error)
{
r_0 = r_opt;
error = error_opt;
}
}
gradient.insert (j) = r_0;
}
}
template <typename T>
void
FivePointsRule<T>::computeGradient
(const GenericFunction<T>& adaptee,
value_type epsilon,
gradient_t& gradient,
const argument_t& argument,
size_type idFunction,
argument_t& xEps) const throw ()
{
assert (adaptee.outputSize () - idFunction > 0);
value_type h = epsilon / 2.;
value_type r_0 = 0.;
value_type round = 0.;
value_type trunc = 0.;
value_type error = 0.;
for (size_type j = 0; j < argument.size (); ++j)
{
detail::compute_deriv (adaptee, j, h,
r_0, round, trunc,
argument, idFunction, xEps);
error = round + trunc;
if (round < trunc && (round > 0 && trunc > 0))
{
value_type r_opt = 0., round_opt = 0., trunc_opt = 0.,
error_opt = 0.;
/* Compute an optimised stepsize to minimize the total error,
using the scaling of the truncation error (O(h^2)) and
rounding error (O(1/h)). */
value_type h_opt =
h * std::pow (round / (2. * trunc), 1. / 3.);
detail::compute_deriv (adaptee, j, h_opt,
r_opt, round_opt, trunc_opt,
argument, idFunction,
xEps);
error_opt = round_opt + trunc_opt;
/* Check that the new error is smaller, and that the new
derivative is consistent with the error bounds of the
original estimate. */
if (error_opt < error && std::fabs (r_opt - r_0) < 4. * error)
{
r_0 = r_opt;
error = error_opt;
}
}
gradient[j] = r_0;
}
}
} // end of namespace finiteDifferenceGradientPolicies.
} // end of namespace roboptim
#endif //! ROBOPTIM_CORE_FINITE_DIFFERENCE_GRADIENT_HXX
|