FunctionApproximator.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotComponents::ArmarXObjects::FunctionApproximator
17  * @author zhou ( you dot zhou at kit dot edu )
18  * @date 2016
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "FunctionApproximator.h"
24 
25 
26 using namespace armarx;
27 
28 void
30 {
31  ARMARX_INFO << "initializing DMP component";
32 
33 
34  ARMARX_INFO << "successfully initialized DMP component";
35 }
36 
37 void
39 {
40 }
41 
42 void
44 {
45  ARMARX_INFO << "disconnecting FunctionApproximator component";
46 }
47 
48 void
50 {
51  ARMARX_INFO << "exiting FunctionApproximator component";
52 }
53 
54 void
55 FunctionApproximator::learn(const std::string& name,
56  const DVector2d& x,
57  const DVector2d& y,
58  const Ice::Current&)
59 {
60 
61  DMP::FunctionApproximationInterfacePtr approximator = funcAppPool.find(name)->second;
62  if (approximator)
63  {
64  if (x.size() != y.size())
65  {
66  ARMARX_ERROR << "The factors dimension should be the same as the responds dimension.";
67  }
68 
69 
70  DMP::DVec2d xx;
71  DMP::DVec2d yy;
72 
73  xx.resize(x.size());
74  yy.resize(y.size());
75 
76  for (size_t i = 0; i < x.size(); ++i)
77  {
78  DMP::DVec cx;
79  DMP::DVec cy;
80 
81  for (size_t j = 0; j < x[i].size(); ++j)
82  {
83  cx.push_back(x[i][j]);
84  cy.push_back(y[i][j]);
85  }
86 
87  xx[i] = cx;
88  yy[i] = cy;
89  }
90 
91  approximator->learn(xx, yy);
92  }
93  else
94  {
95  ARMARX_ERROR << "predict: function approximator not found.";
96  return;
97  }
98 }
99 
101 FunctionApproximator::predict(const std::string& name, const Ice::DoubleSeq& x, const Ice::Current&)
102 {
103  DMP::FunctionApproximationInterfacePtr approximator = funcAppPool.find(name)->second;
104  if (approximator)
105  {
106  DMP::DVec xx;
107 
108  for (size_t i = 0; i < x.size(); ++i)
109  {
110  xx.push_back(x.at(i));
111  }
112 
113  double res = (*approximator)(xx).at(0);
114 
115  return res;
116  }
117  else
118  {
119  ARMARX_ERROR << "predict: function approximator not found.";
120  return 0;
121  }
122 }
123 
124 void
125 FunctionApproximator::ilearn(const std::string& name,
126  const Ice::DoubleSeq& x,
127  Ice::Double y,
128  const Ice::Current&)
129 {
130  DMP::FunctionApproximationInterfacePtr approximator = funcAppPool.find(name)->second;
131  if (approximator)
132  {
133  DMP::DVec factors;
134 
135  for (size_t i = 0; i < x.size(); ++i)
136  {
137  factors.push_back(x.at(i));
138  }
139 
140  approximator->ilearn(factors, y);
141  }
142  else
143  {
144  ARMARX_ERROR << "ilearn: function approximator not found.";
145  return;
146  }
147 }
148 
149 void
150 FunctionApproximator::blearn(const std::string& name,
151  const Ice::DoubleSeq& x,
152  const Ice::DoubleSeq& y,
153  const Ice::Current&)
154 {
155  DMP::FunctionApproximationInterfacePtr approximator = funcAppPool.find(name)->second;
156  if (approximator)
157  {
158  DMP::DVec2d factors;
159  DMP::DVec2d responds;
160 
161  factors.push_back(DMP::DVec(x));
162  responds.push_back(DMP::DVec(y));
163 
164  approximator->learn(factors, responds);
165  }
166  else
167  {
168  ARMARX_ERROR << "ilearn: function approximator not found.";
169  return;
170  }
171 }
172 
173 Ice::DoubleSeq
174 FunctionApproximator::bpredict(const std::string& name,
175  const Ice::DoubleSeq& x,
176  const Ice::Current&)
177 {
178  DMP::FunctionApproximationInterfacePtr approximator = funcAppPool.find(name)->second;
179  if (approximator)
180  {
181  Ice::DoubleSeq responds;
182 
183  for (size_t i = 0; i < x.size(); ++i)
184  {
185  responds.push_back((*approximator)(x.at(i)).at(0));
186  }
187 
188  return responds;
189  }
190  else
191  {
192  ARMARX_ERROR << "ilearn: function approximator not found.";
193  return std::vector<double>();
194  }
195 }
196 
197 void
198 FunctionApproximator::reset(const Ice::Current&)
199 {
200 
201  for (FuncAppMap::iterator it = funcAppPool.begin(); it != funcAppPool.end(); ++it)
202  {
203  it->second.reset(new DMP::RBFInterpolator<DMP::GaussRadialBasisFunction>(2));
204  }
205 }
206 
207 //armarx::PropertyDefinitionsPtr FunctionApproximator::createPropertyDefinitions()
208 //{
209 // return armarx::PropertyDefinitionsPtr(new FunctionApproximatorPropertyDefinitions(
210 // getConfigIdentifier()));
211 //}
212 
213 
214 void
215 armarx::FunctionApproximator::initialize(const std::string& fappName,
216  const Ice::DoubleSeq& /*widthFactors*/,
217  const Ice::Current&)
218 {
219  //int dim = widthFactors.size();
220 
221  DMP::FunctionApproximationInterfacePtr approximator(new DMP::LWR<DMP::GaussKernel>(100));
222 
223  funcAppPool.insert(FuncAppPair(fappName, approximator));
224 }
225 
226 void
228  const std::string& name,
229  const Ice::Current&)
230 {
231  std::ifstream file(name);
232 
233  DMP::FunctionApproximationInterface* readPtr;
234 
235  boost::archive::xml_iarchive ar(file);
236  ar >> boost::serialization::make_nvp("approximator", readPtr);
237 
238  file.close();
239 
240  DMP::FunctionApproximationInterfacePtr approximator(readPtr);
241  funcAppPool.insert(FuncAppPair(funcName, approximator));
242 }
243 
244 void
246  const std::vector<std::string>& funcName,
247  const std::string& filename,
248  const Ice::Current&)
249 {
250  std::ifstream file(filename);
251 
252  std::vector<DMP::FunctionApproximationInterface*> readPtr;
253 
254  boost::archive::xml_iarchive ar(file);
255  ar >> boost::serialization::make_nvp("approximator", readPtr);
256 
257  file.close();
258 
259  for (size_t i = 0; i < readPtr.size(); i++)
260  {
261 
262  DMP::FunctionApproximationInterfacePtr approximator(readPtr[i]);
263  funcAppPool.insert(FuncAppPair(funcName[i], approximator));
264  }
265 }
266 
267 void
269  const std::string& name,
270  const Ice::Current&)
271 {
272 
273  std::cout << "save function approximator in file ... " << std::endl;
274  std::ofstream file(name);
275 
276  boost::archive::xml_oarchive ar(file);
277 
278  DMP::FunctionApproximationInterface* savedPtr = funcAppPool[funcName].get();
279 
280  ar << boost::serialization::make_nvp("approximator", savedPtr);
281 
282  file.close();
283 }
284 
285 void
287  const Ice::DoubleSeq& widthFactors)
288 {
289  int dim = widthFactors.size();
290 
291  DMP::FunctionApproximationInterfacePtr approximator(
292  new DMP::RBFInterpolator<DMP::GaussRadialBasisFunction>(dim, widthFactors));
293 
294  funcAppPool.insert(FuncAppPair(fappName, approximator));
295 }
296 
297 void
299  const std::string& name)
300 {
301 
302  std::cout << "save function approximator in file ... " << std::endl;
303  std::ofstream file(name);
304  DMP::FunctionApproximationInterfacePtr approximator = funcAppPool[funcName];
305 
306  boost::archive::text_oarchive ar(file);
307  DMP::FunctionApproximationInterface* savedPtr = approximator.get();
308  ar << boost::serialization::make_nvp("approximator", savedPtr);
309 
310  DMP::FunctionApproximationInterface* appPtr = approximator.get();
311  std::stringstream str;
312  {
313  boost::archive::xml_oarchive ar1(str);
314  ar1 << boost::serialization::make_nvp("approximator", appPtr);
315  }
316  std::cout << str.str() << std::endl;
317 
318  boost::archive::xml_iarchive ari(str);
319  DMP::FunctionApproximationInterface* newPtr;
320  ari >> boost::serialization::make_nvp("approximator", newPtr);
321 
322 
323  file.close();
324 }
armarx::FuncAppPair
std::pair< std::string, DMP::FunctionApproximationInterfacePtr > FuncAppPair
Definition: FunctionApproximator.h:71
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:43
armarx::FunctionApproximator::onInitComponent
void onInitComponent() override
Definition: FunctionApproximator.cpp:29
armarx::FunctionApproximator::onDisconnectComponent
void onDisconnectComponent() override
Definition: FunctionApproximator.cpp:43
armarx::FunctionApproximator::predict
Ice::Double predict(const std::string &name, const Ice::DoubleSeq &, const Ice::Current &) override
Definition: FunctionApproximator.cpp:101
armarx::FunctionApproximator::bpredict
Ice::DoubleSeq bpredict(const std::string &name, const Ice::DoubleSeq &x, const Ice::Current &) override
Definition: FunctionApproximator.cpp:174
armarx::FunctionApproximator::learn
void learn(const std::string &name, const DVector2d &, const DVector2d &, const Ice::Current &) override
Definition: FunctionApproximator.cpp:55
armarx::FunctionApproximator::onExitComponent
void onExitComponent() override
Definition: FunctionApproximator.cpp:49
armarx::FunctionApproximator::saveFunctionApproximatorInFile
void saveFunctionApproximatorInFile(const std::string &funcName, const std::string &name, const Ice::Current &) override
Definition: FunctionApproximator.cpp:268
armarx::VariantType::Double
const VariantTypeId Double
Definition: Variant.h:920
armarx::FunctionApproximator::onConnectComponent
void onConnectComponent() override
Definition: FunctionApproximator.cpp:38
armarx::control::common::mp::DVec
Ice::DoubleSeq DVec
Definition: MP.h:47
armarx::FunctionApproximator::reset
void reset(const Ice::Current &) override
Definition: FunctionApproximator.cpp:198
armarx::FunctionApproximator::saveFunctionApproximatorInFileTest
void saveFunctionApproximatorInFileTest(const std::string &funcName, const std::string &name)
Definition: FunctionApproximator.cpp:298
filename
std::string filename
Definition: VisualizationRobot.cpp:86
armarx::FunctionApproximator::initialize
void initialize(const std::string &fappName, const Ice::DoubleSeq &factors, const Ice::Current &) override
Definition: FunctionApproximator.cpp:215
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
armarx::FunctionApproximator::blearn
void blearn(const std::string &name, const Ice::DoubleSeq &, const Ice::DoubleSeq &, const Ice::Current &) override
Definition: FunctionApproximator.cpp:150
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
FunctionApproximator.h
armarx::FunctionApproximator::ilearn
void ilearn(const std::string &name, const Ice::DoubleSeq &, Ice::Double, const Ice::Current &) override
Definition: FunctionApproximator.cpp:125
armarx::FunctionApproximator::getFunctionApproximatorFromFile
void getFunctionApproximatorFromFile(const std::string &funcName, const std::string &name, const Ice::Current &) override
Definition: FunctionApproximator.cpp:227
armarx::FunctionApproximator::getFunctionApproximatorsFromFile
void getFunctionApproximatorsFromFile(const std::vector< std::string > &funcNameList, const std::string &filename, const Ice::Current &) override
Definition: FunctionApproximator.cpp:245
armarx::FunctionApproximator::initializeTest
void initializeTest(const std::string &fappName, const Ice::DoubleSeq &widthFactors)
Definition: FunctionApproximator.cpp:286
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27