optimizer_goal_global.h
Go to the documentation of this file.
1 
19 #ifndef __INC_OPTIMIZER_GOAL_GLOBAL
20 #define __INC_OPTIMIZER_GOAL_GLOBAL
21 
22 #include <vector>
23 #include <algorithm>
24 #include <optimizer_goal.h>
25 
26 namespace robotLibPbD {
27 
28 
30 {
31 public:
32  std::vector<double> item, mean;
33  std::vector<std::pair<double, int> > sorted;
34  std::vector<int> ids;
35  std::vector<std::vector<double> > data;
36  unsigned int trimming;
37 
39  {
40  trimming = 0;
41  };
42 
43  virtual void add()
44  {
45  data.push_back(item);
46  };
47  virtual void reset()
48  {
49  data.clear();
50  };
51 
52  void setTrimming(unsigned int value)
53  {
54  trimming = value;
55  };
56 
57  double dist(std::vector<double> &first, std::vector<double> &second)
58  {
59  double tmpf = 0.0;
60  for (unsigned int i=0; i<first.size() && i<second.size(); i++)
61  tmpf += (first[i] - second[i])*(first[i] - second[i]);
62  return sqrt(tmpf);
63  }
64 
65  double getTrimmedMeanAndVariance(std::vector<std::vector<double> > &data, int index, std::vector<double> &item)
66  {
67  if (data.size() == 0)
68  return 10000.0;
69 
70  // get variance
71  sorted.clear();
72  for (unsigned int i=0; i<data.size(); i++)
73  if ((int)i != index)
74  sorted.push_back(std::make_pair(dist(data[i],item), i));
75 
76  std::sort(sorted.begin(), sorted.end(), comparePairs<double, int>);
77 
78  ids.clear();
79  for (unsigned int i=trimming; i<sorted.size() - trimming; i++)
80  ids.push_back(sorted[i].second);
81 
82  if (index >= 0)
83  ids.push_back(index);
84 
85  double n = (double)ids.size();
86 
87  // get mean
88  item.clear();
89  item.resize(data[0].size(), 0.0);
90  for (unsigned int i=0; i<ids.size(); i++)
91  for (unsigned int j=0; j<item.size(); j++)
92  item[j] += data[ids[i]][j] / n;
93 
94  // get variance
95  n -= 1.0;
96  double tmpf = 0.0, tmpf2;
97  for (unsigned int i=0; i<ids.size(); i++)
98  {
99  tmpf2 = dist(data[ids[i]], item);
100  tmpf += (tmpf2 * tmpf2) / n;
101  }
102  return tmpf;
103 
104  }
105 
106  virtual double getDistance()
107  {
108  if (data.size() == 0)
109  return 10000.0;
110 
111  // get best trimmed mean
112  double min = 1.0e30;
113  std::vector<double> tmpMean;
114  for (unsigned int i=0; i<data.size(); i++)
115  {
116  tmpMean.clear();
117  tmpMean = data[i];
118  double tmpf = getTrimmedMeanAndVariance(data, i, tmpMean);
119  if (tmpf < min)
120  {
121  min = tmpf;
122  mean = tmpMean;
123  }
124  }
125 
126  return sqrt(min);
127  };
128 };
129 
130 };
131 
132 #endif
std::vector< std::pair< double, int > > sorted
double dist(std::vector< double > &first, std::vector< double > &second)
void setTrimming(unsigned int value)
std::vector< std::vector< double > > data
double getTrimmedMeanAndVariance(std::vector< std::vector< double > > &data, int index, std::vector< double > &item)


asr_kinematic_chain_optimizer
Author(s): Aumann Florian, Heller Florian, Jäkel Rainer, Wittenbeck Valerij
autogenerated on Mon Jun 10 2019 12:35:36