34 ARMARX_INFO <<
"successfully initialized DMP component";
45 ARMARX_INFO <<
"disconnecting FunctionApproximator component";
51 ARMARX_INFO <<
"exiting FunctionApproximator component";
61 DMP::FunctionApproximationInterfacePtr approximator = funcAppPool.find(name)->second;
64 if (x.size() != y.size())
66 ARMARX_ERROR <<
"The factors dimension should be the same as the responds dimension.";
76 for (
size_t i = 0; i < x.size(); ++i)
81 for (
size_t j = 0; j < x[i].size(); ++j)
83 cx.push_back(x[i][j]);
84 cy.push_back(y[i][j]);
91 approximator->learn(xx, yy);
95 ARMARX_ERROR <<
"predict: function approximator not found.";
103 DMP::FunctionApproximationInterfacePtr approximator = funcAppPool.find(name)->second;
108 for (
size_t i = 0; i < x.size(); ++i)
110 xx.push_back(x.at(i));
113 double res = (*approximator)(xx).at(0);
119 ARMARX_ERROR <<
"predict: function approximator not found.";
126 const Ice::DoubleSeq& x,
130 DMP::FunctionApproximationInterfacePtr approximator = funcAppPool.find(name)->second;
135 for (
size_t i = 0; i < x.size(); ++i)
137 factors.push_back(x.at(i));
140 approximator->ilearn(factors, y);
144 ARMARX_ERROR <<
"ilearn: function approximator not found.";
151 const Ice::DoubleSeq& x,
152 const Ice::DoubleSeq& y,
155 DMP::FunctionApproximationInterfacePtr approximator = funcAppPool.find(name)->second;
159 DMP::DVec2d responds;
164 approximator->learn(factors, responds);
168 ARMARX_ERROR <<
"ilearn: function approximator not found.";
175 const Ice::DoubleSeq& x,
178 DMP::FunctionApproximationInterfacePtr approximator = funcAppPool.find(name)->second;
181 Ice::DoubleSeq responds;
183 for (
size_t i = 0; i < x.size(); ++i)
185 responds.push_back((*approximator)(x.at(i)).at(0));
192 ARMARX_ERROR <<
"ilearn: function approximator not found.";
193 return std::vector<double>();
201 for (FuncAppMap::iterator it = funcAppPool.begin(); it != funcAppPool.end(); ++it)
203 it->second.reset(
new DMP::RBFInterpolator<DMP::GaussRadialBasisFunction>(2));
216 const Ice::DoubleSeq& ,
221 DMP::FunctionApproximationInterfacePtr approximator(
new DMP::LWR<DMP::GaussKernel>(100));
223 funcAppPool.insert(
FuncAppPair(fappName, approximator));
228 const std::string& name,
231 std::ifstream file(name);
233 DMP::FunctionApproximationInterface* readPtr;
235 boost::archive::xml_iarchive ar(file);
236 ar >> boost::serialization::make_nvp(
"approximator", readPtr);
240 DMP::FunctionApproximationInterfacePtr approximator(readPtr);
241 funcAppPool.insert(
FuncAppPair(funcName, approximator));
246 const std::vector<std::string>& funcName,
252 std::vector<DMP::FunctionApproximationInterface*> readPtr;
254 boost::archive::xml_iarchive ar(file);
255 ar >> boost::serialization::make_nvp(
"approximator", readPtr);
259 for (
size_t i = 0; i < readPtr.size(); i++)
262 DMP::FunctionApproximationInterfacePtr approximator(readPtr[i]);
263 funcAppPool.insert(
FuncAppPair(funcName[i], approximator));
269 const std::string& name,
273 std::cout <<
"save function approximator in file ... " << std::endl;
274 std::ofstream file(name);
276 boost::archive::xml_oarchive ar(file);
278 DMP::FunctionApproximationInterface* savedPtr = funcAppPool[funcName].get();
280 ar << boost::serialization::make_nvp(
"approximator", savedPtr);
287 const Ice::DoubleSeq& widthFactors)
289 int dim = widthFactors.size();
291 DMP::FunctionApproximationInterfacePtr approximator(
292 new DMP::RBFInterpolator<DMP::GaussRadialBasisFunction>(dim, widthFactors));
294 funcAppPool.insert(
FuncAppPair(fappName, approximator));
299 const std::string& name)
302 std::cout <<
"save function approximator in file ... " << std::endl;
303 std::ofstream file(name);
304 DMP::FunctionApproximationInterfacePtr approximator = funcAppPool[funcName];
306 boost::archive::text_oarchive ar(file);
307 DMP::FunctionApproximationInterface* savedPtr = approximator.get();
308 ar << boost::serialization::make_nvp(
"approximator", savedPtr);
310 DMP::FunctionApproximationInterface* appPtr = approximator.get();
311 std::stringstream
str;
313 boost::archive::xml_oarchive ar1(
str);
314 ar1 << boost::serialization::make_nvp(
"approximator", appPtr);
316 std::cout <<
str.str() << std::endl;
318 boost::archive::xml_iarchive ari(
str);
319 DMP::FunctionApproximationInterface* newPtr;
320 ari >> boost::serialization::make_nvp(
"approximator", newPtr);