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 return cfg.group.toString();
76 const arondto::MPGroup&
85 return cfg.role ==
"taskspace" or
cfg.role ==
"object";
91 start(std::vector<double>());
97 start(goals, std::vector<double>());
103 cfg.durationSec = timeDuration;
110 cfg.durationSec = timeDuration;
116 cfg.durationSec = timeDuration;
117 start(goals, starts);
138 auto vps =
vmp->getViaPoints();
141 for (
const auto& v : vps)
143 ARMARX_INFO <<
"------ (" <<
index <<
") viapoint at " << v.first <<
" --- "
145 mplib::core::SystemState::convertStatesToArray(v.second, 0));
186 ARMARX_INFO <<
"cannot reset a running mp, please stop it first.";
234 ARMARX_INFO <<
"-- Train MP '" <<
cfg.name <<
"' from trajectories";
238 ARMARX_INFO <<
"---- using default trajectories from the MP configuration";
239 trajList =
cfg.trajectoryList;
247 std::vector<mplib::core::SampledTrajectory> trajs;
248 for (
const auto& mpTraj : trajList)
250 std::map<double, std::vector<double>> trajMap;
251 for (
int i = 0; i < mpTraj.time.size(); ++i)
253 auto& row_vector = mpTraj.traj.row(i);
254 std::vector<double> vec(row_vector.data(), row_vector.data() + row_vector.size());
255 trajMap.emplace(mpTraj.time(i), vec);
257 mplib::core::SampledTrajectory traj(trajMap);
258 trajs.push_back(traj);
268 std::vector<std::string> fileList;
269 if (fileNames.empty())
271 ARMARX_INFO <<
"---- using default trajectory list: " <<
cfg.fileList[0];
272 fileList =
cfg.fileList;
276 fileList = fileNames;
279 std::vector<mplib::core::SampledTrajectory> trajs;
280 for (
auto& file : fileList)
282 mplib::core::SampledTrajectory traj;
283 traj.readFromCSVFile(file);
284 trajs.push_back(traj);
292 if (
cfg.regressionModel ==
"gpr")
294 vmp->setBaseFunctionApproximator(
295 std::make_unique<mplib::math::function_approximation::GaussianProcessRegression<
296 mplib::math::kernel::SquaredExponentialCovarianceFunction>>(
297 0.01, 0.1, 0.000001));
298 for (
auto& traj : trajs)
300 traj = mplib::core::SampledTrajectory::downSample(traj, 100);
304 vmp->learnFromTrajectories(trajs);
311 for (
size_t i = 0; i < trajs[0].dim(); i++)
313 mplib::representation::MPState state(trajs[0].begin()->getPosition(i),
314 trajs[0].begin()->getDeriv(i, 1));
326 if (not
cfg.fileList.empty())
330 else if (not
cfg.trajectoryList.empty())
338 <<
": You don't provide any trajectory files (path on the robot) nor trajectories "
340 "type std::vector<armarx::control::common::mp::arondto::MPTraj> in your "
341 "configuration file. \n"
342 <<
"If you intended train MPs by providing files or "
343 "trajectories on the fly, consider using \n"
344 <<
"ctrl.learnFromCSV(Ice::StringSeq& fileNames) or \n"
345 <<
"ctrl.learnFromCSVlearnFromTrajs(armarx::aron::data::dto::DictPtr& dict)";
361 <<
"---- user specified empty goal, try to use goals in your configuration file:\n"
367 <<
"---- user specified empty goal, try to use goals learned from trajectory:\n"
384 <<
": size of starts and learned mp are not consistent";
393 std::vector<double> learnedStart;
398 ARMARX_INFO <<
"---- user doesn't define start point, fall back to start point "
399 "learned from trajectory: "
407 const auto& startState =
vmp->getStartState();
408 std::vector<double> mpStartVec;
409 for (
const auto& state : startState)
411 mpStartVec.push_back(state.pos);
419 return cfg.startFromPrevTarget;
437 if (canVal <= vmp->getUmin())
441 else if (canVal >= 1.0 -
vmp->getUmin())
452 ARMARX_ERROR <<
" -- mp is not trained yet, potential memory allocation error! "
453 "Please train mp before setting any via points";
461 vmp->removeViaPoints();
474 vmp->setWeights(weights);
480 return vmp->getWeights();
522 return vmp->getGoals();
std::vector< double > getStartVec()
std::atomic_bool stopRequested
double getCanonicalValue()
std::string getGroupName() const
void setDurationSec(Ice::Double timeDuration)
const std::string & getClassName() const
void setStartAndGoal(const DVec &starts, const DVec &goals)
const std::string & getNodeSetName() const
void setViaPoint(Ice::Double u, const DVec &viapoint)
virtual DVec validateViaPoint(const DVec &viapoint)
bool getStartFromPrevTarget()
std::vector< std::pair< double, DVec > > userDefinedViaPoints
void setWeight(const DVecVec &weights)
serialze
bool isTaskSpaceMP() const
std::atomic_bool goalSetByUser
DVec deserialize(const std::string &, const std::string &mode="string")
std::string serialize(const std::string &mode="string")
std::vector< mplib::representation::MPState > currentState
const arondto::MPGroup & getGroup() const
std::atomic_bool mpTrained
Note: if mp is not trained, the dimension is not initialized, thus mp->getDim() is undefined,...
void trainMPFromTraj(std::vector< mplib::core::SampledTrajectory > &trajs)
void learnFromCSV(const Ice::StringSeq &fileNames=std::vector< std::string >())
setting
std::atomic_bool firstRun
const std::string & getMPName() const
void setStart(const DVec &starts)
const std::string & getRole() const
mplib::representation::VMPType vmpType
void setGoal(const DVec &goals)
void learnFromTraj(const MPTrajs &trajs=MPTrajs())
#define ARMARX_CHECK_LESS_EQUAL(lhs, rhs)
This macro evaluates whether lhs is less or equal (<=) rhs and if it turns out to be false it will th...
#define ARMARX_CHECK_GREATER_EQUAL(lhs, rhs)
This macro evaluates whether lhs is greater or equal (>=) rhs and if it turns out to be false it will...
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
#define ARMARX_INFO
The normal logging level.
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
This file is part of ArmarX.
std::vector< MPTraj > MPTrajs
std::lock_guard< std::recursive_mutex > LockGuardType
std::string dVecToString(const DVec &dvec)