Example shows freeze use.
#undef NDEBUG
#include <boost/assign/list_of.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <roboptim/core/decorator/finite-difference-gradient.hh>
#include <roboptim/core/solver-factory.hh>
#include <roboptim/core/visualization/gnuplot.hh>
#include <roboptim/core/visualization/gnuplot-commands.hh>
#include <roboptim/core/visualization/gnuplot-function.hh>
#include "shared-tests/common.hh"
using namespace roboptim;
using namespace roboptim::trajectory;
using namespace roboptim::visualization;
using namespace roboptim::visualization::gnuplot;
typedef Solver<EigenMatrixDense> test_solver_t;
typedef test_solver_t::problem_t::constraints_t constraint_t;
{
std::cout
<< "# Values:" << std::endl
<< "# " << normalize (spline (0.)) << std::endl
<< "# " << normalize (spline (2.5)) << std::endl
<< "# " << normalize (spline (4.)) << std::endl
<< "# 1st derivative:" << std::endl
<<
"# " << normalize (spline.
derivative (0., 1)) << std::endl
<<
"# " << normalize (spline.
derivative (2.5, 1)) << std::endl
<<
"# " << normalize (spline.
derivative (4., 1)) << std::endl
<< "# 2nd derivative:" << std::endl
<<
"# " << normalize (spline.
derivative (0., 2)) << std::endl
<<
"# " << normalize (spline.
derivative (2.5, 2)) << std::endl
<<
"# " << normalize (spline.
derivative (4., 2)) << std::endl
<< "# variationConfigWrtParam:" << std::endl
}
BOOST_FIXTURE_TEST_SUITE (trajectory, TestSuiteConfiguration)
BOOST_AUTO_TEST_CASE (trajectory_spline_optimization)
{
using roboptim::visualization::gnuplot::set;
using namespace boost::assign;
CubicBSpline::vector_t params (16);
params[0] = 0., params[1] = 0.;
params[2] = 0., params[3] = 0.;
params[4] = 0., params[5] = 0.;
params[6] = 25., params[7] = 100.;
params[8] = 75., params[9] = 0.;
params[10] = 100., params[11] = 100.;
params[12] = 100., params[13] = 100.;
params[14] = 100., params[15] = 100.;
CubicBSpline::interval_t timeRange = CubicBSpline::makeInterval (0., 4.);
discreteInterval_t interval (0., 4., 0.01);
showSpline (spline);
Gnuplot gnuplot = Gnuplot::make_interactive_gnuplot ();
gnuplot
<< set ("multiplot layout 1,2")
<< set ("grid")
try
{
Function::vector_t x (params.size ());
x.setZero ();
checkGradientAndThrow (cost, 0, x, 2e-3);
x = params;
checkGradientAndThrow (cost, 0, x, 2e-3);
}
catch (BadGradient<EigenMatrixDense>& bg)
{
std::cerr << bg << std::endl;
BOOST_CHECK(false);
}
test_solver_t::problem_t problem (cost);
problem.startingPoint () = params;
SolverFactory<test_solver_t> factory (TESTSUITE_SOLVER, problem);
test_solver_t& solver = factory ();
SET_LOG_FILE(solver);
std::cerr << "Cost function (before): " << cost (params) << std::endl;
std::cerr << "Parameters (before): " << params << std::endl;
std::cerr << solver << std::endl;
test_solver_t::result_t res = solver.minimum ();
switch (res.which ())
{
case GenericSolver::SOLVER_VALUE_WARNINGS:
case GenericSolver::SOLVER_VALUE:
{
Result& result = boost::get<Result> (res);
CubicBSpline optimizedSpline (timeRange, 2, result.x,
"after");
showSpline (optimizedSpline);
params = result.x;
gnuplot <<
plot_xy (optimizedSpline, interval);
break;
}
case GenericSolver::SOLVER_NO_SOLUTION:
{
std::cerr << "No solution" << std::endl;
BOOST_CHECK(false);
}
case GenericSolver::SOLVER_ERROR:
{
SolverError& result = boost::get<SolverError> (res);
std::cerr << result << std::endl;
BOOST_CHECK(false);
}
}
std::cerr << "Parameters (after): " << params << std::endl;
try
{
checkGradientAndThrow (cost, 0, params, 2e-3);
}
catch (BadGradient<EigenMatrixDense>& bg)
{
std::cerr << bg << std::endl;
BOOST_CHECK(false);
}
std::cout << (gnuplot << unset ("multiplot"));
}
BOOST_AUTO_TEST_SUITE_END ()