Go to the documentation of this file.00001
00019 #ifndef __INC_OPTIMIZER_GOAL_GLOBAL
00020 #define __INC_OPTIMIZER_GOAL_GLOBAL
00021
00022 #include <vector>
00023 #include <algorithm>
00024 #include <optimizer_goal.h>
00025
00026 namespace robotLibPbD {
00027
00028
00029 class OptimizerGoalGlobal : public OptimizerGoal
00030 {
00031 public:
00032 std::vector<double> item, mean;
00033 std::vector<std::pair<double, int> > sorted;
00034 std::vector<int> ids;
00035 std::vector<std::vector<double> > data;
00036 unsigned int trimming;
00037
00038 OptimizerGoalGlobal()
00039 {
00040 trimming = 0;
00041 };
00042
00043 virtual void add()
00044 {
00045 data.push_back(item);
00046 };
00047 virtual void reset()
00048 {
00049 data.clear();
00050 };
00051
00052 void setTrimming(unsigned int value)
00053 {
00054 trimming = value;
00055 };
00056
00057 double dist(std::vector<double> &first, std::vector<double> &second)
00058 {
00059 double tmpf = 0.0;
00060 for (unsigned int i=0; i<first.size() && i<second.size(); i++)
00061 tmpf += (first[i] - second[i])*(first[i] - second[i]);
00062 return sqrt(tmpf);
00063 }
00064
00065 double getTrimmedMeanAndVariance(std::vector<std::vector<double> > &data, int index, std::vector<double> &item)
00066 {
00067 if (data.size() == 0)
00068 return 10000.0;
00069
00070
00071 sorted.clear();
00072 for (unsigned int i=0; i<data.size(); i++)
00073 if ((int)i != index)
00074 sorted.push_back(std::make_pair(dist(data[i],item), i));
00075
00076 std::sort(sorted.begin(), sorted.end(), comparePairs<double, int>);
00077
00078 ids.clear();
00079 for (unsigned int i=trimming; i<sorted.size() - trimming; i++)
00080 ids.push_back(sorted[i].second);
00081
00082 if (index >= 0)
00083 ids.push_back(index);
00084
00085 double n = (double)ids.size();
00086
00087
00088 item.clear();
00089 item.resize(data[0].size(), 0.0);
00090 for (unsigned int i=0; i<ids.size(); i++)
00091 for (unsigned int j=0; j<item.size(); j++)
00092 item[j] += data[ids[i]][j] / n;
00093
00094
00095 n -= 1.0;
00096 double tmpf = 0.0, tmpf2;
00097 for (unsigned int i=0; i<ids.size(); i++)
00098 {
00099 tmpf2 = dist(data[ids[i]], item);
00100 tmpf += (tmpf2 * tmpf2) / n;
00101 }
00102 return tmpf;
00103
00104 }
00105
00106 virtual double getDistance()
00107 {
00108 if (data.size() == 0)
00109 return 10000.0;
00110
00111
00112 double min = 1.0e30;
00113 std::vector<double> tmpMean;
00114 for (unsigned int i=0; i<data.size(); i++)
00115 {
00116 tmpMean.clear();
00117 tmpMean = data[i];
00118 double tmpf = getTrimmedMeanAndVariance(data, i, tmpMean);
00119 if (tmpf < min)
00120 {
00121 min = tmpf;
00122 mean = tmpMean;
00123 }
00124 }
00125
00126 return sqrt(min);
00127 };
00128 };
00129
00130 };
00131
00132 #endif