3 #include <boost/archive/text_iarchive.hpp>
4 #include <boost/archive/text_oarchive.hpp>
5 #include <boost/archive/xml_iarchive.hpp>
6 #include <boost/archive/xml_oarchive.hpp>
11 #include <mplib/core/SystemState.h>
21 mplib::factories::VMPFactory mpfactory;
22 mpfactory.addConfig(
"kernelSize",
c.kernelSize);
24 resetVMPType(
c.mpTypeString);
26 std::shared_ptr<mplib::representation::AbstractMovementPrimitive> mp =
28 vmp = std::dynamic_pointer_cast<mplib::representation::vmp::PrincipalComponentVMP>(mp);
30 if (
cfg.viaPoints.size() > 0)
34 for (
const auto& vp :
cfg.viaPoints)
36 ARMARX_INFO <<
"---- (" <<
index <<
") canonical value: " << vp.canonicalValue
61 return cfg.nodeSetName;
73 start(std::vector<double>());
79 start(goals, std::vector<double>());
85 cfg.durationSec = timeDuration;
92 cfg.durationSec = timeDuration;
98 cfg.durationSec = timeDuration;
120 auto vps =
vmp->getViaPoints();
123 for (
const auto&
v : vps)
127 mplib::core::SystemState::convertStatesToArray(
v.second, 0));
168 ARMARX_INFO <<
"cannot reset a running mp, please stop it first.";
216 ARMARX_INFO <<
"-- Train MP '" <<
cfg.name <<
"' from trajectories";
220 ARMARX_INFO <<
"---- using default trajectories from the MP configuration";
221 trajList =
cfg.trajectoryList;
229 std::vector<mplib::core::SampledTrajectory> trajs;
230 for (
const auto& mpTraj : trajList)
232 std::map<double, std::vector<double>> trajMap;
233 for (
int i = 0; i < mpTraj.time.size(); ++i)
235 auto& row_vector = mpTraj.traj.row(i);
236 std::vector<double> vec(row_vector.data(), row_vector.data() + row_vector.size());
237 trajMap.emplace(mpTraj.time(i), vec);
239 mplib::core::SampledTrajectory traj(trajMap);
240 trajs.push_back(traj);
250 std::vector<std::string> fileList;
251 if (fileNames.empty())
253 ARMARX_INFO <<
"---- using default trajectory list: " <<
cfg.fileList[0];
254 fileList =
cfg.fileList;
258 fileList = fileNames;
261 std::vector<mplib::core::SampledTrajectory> trajs;
262 for (
auto& file : fileList)
264 mplib::core::SampledTrajectory traj;
265 traj.readFromCSVFile(file);
266 trajs.push_back(traj);
274 if (
cfg.regressionModel ==
"gpr")
276 vmp->setBaseFunctionApproximator(
277 std::make_unique<mplib::math::function_approximation::GaussianProcessRegression<
278 mplib::math::kernel::SquaredExponentialCovarianceFunction>>(
279 0.01, 0.1, 0.000001));
280 for (
auto& traj : trajs)
282 traj = mplib::core::SampledTrajectory::downSample(traj, 100);
286 vmp->learnFromTrajectories(trajs);
293 for (
size_t i = 0; i < trajs[0].dim(); i++)
295 mplib::representation::MPState state(trajs[0].begin()->getPosition(i),
296 trajs[0].begin()->getDeriv(i, 1));
308 if (not
cfg.fileList.empty())
312 else if (not
cfg.trajectoryList.empty())
320 <<
": You don't provide any trajectory files (path on the robot) nor trajectories "
322 "type std::vector<armarx::control::common::mp::arondto::MPTraj> in your "
323 "configuration file. \n"
324 <<
"If you intended train MPs by providing files or "
325 "trajectories on the fly, consider using \n"
326 <<
"ctrl.learnFromCSV(Ice::StringSeq& fileNames) or \n"
327 <<
"ctrl.learnFromCSVlearnFromTrajs(armarx::aron::data::dto::DictPtr& dict)";
343 <<
"---- user specified empty goal, try to use goals in your configuration file:\n"
349 <<
"---- user specified empty goal, try to use goals learned from trajectory:\n"
366 <<
": size of starts and learned mp are not consistent";
375 std::vector<double> learnedStart;
380 ARMARX_INFO <<
"---- user doesn't define start point, fall back to start point "
381 "learned from trajectory: "
389 const auto& startState =
vmp->getStartState();
390 std::vector<double> mpStartVec;
391 for (
const auto& state : startState)
393 mpStartVec.push_back(state.pos);
401 return cfg.startFromPrevTarget;
419 if (canVal <= vmp->getUmin())
423 else if (canVal >= 1.0 -
vmp->getUmin())
434 ARMARX_ERROR <<
" -- mp is not trained yet, potential memory allocation error! "
435 "Please train mp before setting any via points";
443 vmp->removeViaPoints();
456 vmp->setWeights(weights);
462 return vmp->getWeights();
504 return vmp->getGoals();