diff --git a/trunk/paradiseo-mo/src/mo.h b/trunk/paradiseo-mo/src/mo.h index f74b07fcc..1a0d9deeb 100755 --- a/trunk/paradiseo-mo/src/mo.h +++ b/trunk/paradiseo-mo/src/mo.h @@ -158,6 +158,7 @@ #include #include #include +#include #include #include diff --git a/trunk/paradiseo-mo/src/sampling/moStatistics.h b/trunk/paradiseo-mo/src/sampling/moStatistics.h new file mode 100644 index 000000000..ec0ea5f46 --- /dev/null +++ b/trunk/paradiseo-mo/src/sampling/moStatistics.h @@ -0,0 +1,215 @@ +/* + + Copyright (C) DOLPHIN Project-Team, INRIA Lille - Nord Europe, 2006-2010 + + Sebastien Verel, Arnaud Liefooghe, Jeremie Humeau + + This software is governed by the CeCILL license under French law and + abiding by the rules of distribution of free software. You can use, + modify and/ or redistribute the software under the terms of the CeCILL + license as circulated by CEA, CNRS and INRIA at the following URL + "http://www.cecill.info". + + As a counterpart to the access to the source code and rights to copy, + modify and redistribute granted by the license, users are provided only + with a limited warranty and the software's author, the holder of the + economic rights, and the successive licensors have only limited liability. + + In this respect, the user's attention is drawn to the risks associated + with loading, using, modifying and/or developing or reproducing the + software by the user in light of its specific status of free software, + that may mean that it is complicated to manipulate, and that also + therefore means that it is reserved for developers and experienced + professionals having in-depth computer knowledge. Users are therefore + encouraged to load and test the software's suitability as regards their + requirements in conditions enabling the security of their systems and/or + data to be ensured and, more generally, to use and operate it in the + same conditions as regards security. + The fact that you are presently reading this means that you have had + knowledge of the CeCILL license and that you accept its terms. + + ParadisEO WebSite : http://paradiseo.gforge.inria.fr + Contact: paradiseo-help@lists.gforge.inria.fr +*/ + +#ifndef moStatistics_h +#define moStatistics_h + +#include +#include + +/** + * Tools to compute some basic statistics + * + * Remember it is better to use some statistic tool like R, etc. + * But it could be usefull to have here in paradisEO + */ +class moStatistics +{ +public: + /** + * Default Constructor + */ + moStatistics() { } + + /** + * To compute min, max , average and standard deviation of a vector of double + * + * @param data vector of double + * @param min to compute + * @param max to compute + * @param avg average to compute + * @param std standard deviation to compute + */ + void basic(const vector & data, + double & min, double & max, double & avg, double & std) { + + if (data.size() == 0) { + min = 0.0; + max = 0.0; + avg = 0.0; + std = 0.0; + } else { + unsigned int n = data.size(); + + min = data[0]; + max = data[0]; + avg = 0.0; + std = 0.0; + + double d; + for(unsigned int i = 0; i < n; i++) { + d = data[i]; + if (d < min) + min = d; + if (max < d) + max = d; + avg += d; + std += d * d; + } + + avg /= n; + + std = std / n - avg * avg ; + if (std > 0) + std = sqrt(std); + } + } + + /** + * To compute the distance between solutions + * + * @param data vector of solutions + * @param distance method to compute the distance + * @param matrix matrix of distances between solutions + */ + template + void distances(const vector & data, eoDistance & distance, + vector< vector > & matrix) { + if (data.size() == 0) { + matrix.resize(0); + } else { + unsigned int n = data.size(); + + matrix.resize(n); + for(unsigned i = 0; i < n; i++) + matrix[i].resize(n); + + unsigned j; + for(unsigned i = 0; i < n; i++) { + matrix[i][i] = 0.0; + for(j = 0; j < i; j++) { + matrix[i][j] = distance(data[i], data[j]); + matrix[j][i] = matrix[i][j]; + } + } + } + } + + /** + * To compute the autocorrelation and partial autocorrelation + * + * @param data vector of double + * @param nbS number of correlation coefficients + * @param rho autocorrelation coefficients + * @param phi partial autocorrelation coefficients + */ + void autocorrelation(const vector & data, unsigned int nbS, + vector & rho, vector & phi) { + if (data.size() == 0) { + rho.resize(0); + phi.resize(0); + } else { + unsigned int n = data.size(); + + double cov[nbS+1]; + double m[nbS+1]; + double sig[nbS+1]; + + rho.resize(nbS+1); + phi.resize(nbS+1); + rho[0] = 1.0; + phi[0] = 1.0; // ? + + unsigned s, k; + + for(s = 0; s <= nbS; s++) { + cov[s] = 0; + m[s] = 0; + sig[s] = 0; + } + + double m0, s0; + unsigned j; + + k = 0; + s = nbS; + while (s > 0) { + while (k + s < n) { + for(j = 0; j <= s; j++) { + m[j] += data[k+j]; + sig[j] += data[k+j] * data[k+j]; + cov[j] += data[k] * data[k+j]; + } + k++; + } + + m[s] /= n - s; + sig[s] = sig[s] / (n - s) - m[s] * m[s]; + if (sig[s] <= 0) + sig[s] = 0; + else + sig[s] = sqrt(sig[s]); + m0 = m[0] / (n - s); + s0 = sqrt(sig[0] / (n - s) - m0 * m0); + cov[s] = cov[s] / (n - s) - (m[0] / (n - s)) * m[s]; + rho[s] = cov[s] / (sig[s] * s0); + s--; + } + + double phi2[nbS+1][nbS+1]; + double tmp1, tmp2; + + phi2[1][1] = rho[1]; + for(k = 2; k <= nbS; k++) { + tmp1 = 0; + tmp2 = 0; + for(j = 1; j < k; j++) { + tmp1 += phi2[k-1][j] * rho[k-j]; + tmp2 += phi2[k-1][j] * rho[j]; + } + phi2[k][k] = (rho[k] - tmp1) / (1 - tmp2); + for(j = 1; j < k; j++) + phi2[k][j] = phi2[k-1][j] - phi2[k][k] * phi2[k-1][k-j]; + } + + for(j = 1; j <= nbS; j++) + phi[j] = phi2[j][j]; + + } + } + +}; + + +#endif diff --git a/trunk/paradiseo-mo/tutorial/Lesson6/adaptiveWalks.cpp b/trunk/paradiseo-mo/tutorial/Lesson6/adaptiveWalks.cpp index 4c22af8f2..bae65a273 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson6/adaptiveWalks.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson6/adaptiveWalks.cpp @@ -193,6 +193,7 @@ void main_function(int argc, char **argv) std::cout << "Last values:" << std::endl; std::cout << "Length " << lengthValues[lengthValues.size() - 1] << std::endl; + } // A main that catches the exceptions diff --git a/trunk/paradiseo-mo/tutorial/Lesson6/autocorrelation.cpp b/trunk/paradiseo-mo/tutorial/Lesson6/autocorrelation.cpp index 9b2c63d2a..982fecf73 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson6/autocorrelation.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson6/autocorrelation.cpp @@ -40,6 +40,10 @@ using namespace std; // the sampling class #include +//----------------------------------------------------------------------------- +// the statistics class +#include + // Declaration of types //----------------------------------------------------------------------------- // Indi is the typedef of the solution type like in paradisEO-eo @@ -195,6 +199,16 @@ void main_function(int argc, char **argv) std::cout << "Last values:" << std::endl; std::cout << "Fitness " << fitnessValues[fitnessValues.size() - 1] << std::endl; + + // more basic statistics on the distribution: + moStatistics statistics; + + vector rho, phi; + + statistics.autocorrelation(fitnessValues, 10, rho, phi); + + for(unsigned s = 0; s < rho.size(); s++) + std::cout << s << " " << "rho=" << rho[s] << ", phi=" << phi[s] << std::endl; } // A main that catches the exceptions diff --git a/trunk/paradiseo-mo/tutorial/Lesson6/densityOfStates.cpp b/trunk/paradiseo-mo/tutorial/Lesson6/densityOfStates.cpp index 3286d90f6..df4d5b424 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson6/densityOfStates.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson6/densityOfStates.cpp @@ -34,6 +34,10 @@ using namespace std; // the sampling class #include +//----------------------------------------------------------------------------- +// the statistics class +#include + // Declaration of types //----------------------------------------------------------------------------- // Indi is the typedef of the solution type like in paradisEO-eo @@ -165,6 +169,14 @@ void main_function(int argc, char **argv) std::cout << "Last values:" << std::endl; std::cout << "Fitness " << fitnessValues[fitnessValues.size() - 1] << std::endl; + + // more basic statistics on the distribution: + double min, max, avg, std; + + moStatistics statistics; + + statistics.basic(fitnessValues, min, max, avg, std); + std::cout << "min=" << min << ", max=" << max << ", average=" << avg << ", std dev=" << std << std::endl; } // A main that catches the exceptions diff --git a/trunk/paradiseo-mo/tutorial/Lesson6/neutralWalk.cpp b/trunk/paradiseo-mo/tutorial/Lesson6/neutralWalk.cpp index 1102affd2..b2a46bbea 100644 --- a/trunk/paradiseo-mo/tutorial/Lesson6/neutralWalk.cpp +++ b/trunk/paradiseo-mo/tutorial/Lesson6/neutralWalk.cpp @@ -40,6 +40,10 @@ using namespace std; #include #include +//----------------------------------------------------------------------------- +// the statistics class +#include + // Declaration of types //----------------------------------------------------------------------------- // Indi is the typedef of the solution type like in paradisEO-eo @@ -209,6 +213,27 @@ void main_function(int argc, char **argv) // export only the solution into file sampling.fileExport(0, str_out + "_sol"); + + // more basic statistics on the distribution: + moStatistics statistics; + + vector< vector > dist; + vector v; + + statistics.distances(solutions, distance, dist); + + for(unsigned i = 0; i < dist.size(); i++) { + for(unsigned j = 0; j < dist.size(); j++) { + std::cout << dist[i][j] << " " ; + if (j < i) + v.push_back(dist[i][j]); + } + std::cout << std::endl; + } + + double min, max, avg, std; + statistics.basic(v, min, max, avg, std); + std::cout << "min=" << min << ", max=" << max << ", average=" << avg << ", std dev=" << std << std::endl; } // A main that catches the exceptions