sumobjective.cpp
Go to the documentation of this file.00001 #include <fstream>
00002 #include <iostream>
00003 #include <iterator>
00004 #include <algorithm>
00005 #include <string>
00006 #include <vector>
00007 #include "statutils.h"
00008 #include "Util.h"
00009
00010 using namespace std;
00011 using namespace gplib;
00012
00013
00014
00015
00016
00017
00018
00019
00020 int main(int argc, char *argv[])
00021 {
00022 string infilename, outfilename;
00023 string version =
00024 "$Id: sumobjective.cpp 1816 2009-09-07 11:28:35Z mmoorkamp $";
00025 cout << endl << endl;
00026 cout << "Program " << version << endl;
00027 cout << "Sum up the objective function values as output by the Pareto-GA"
00028 << endl;
00029 cout << "and find the model with smallest distance from origin. " << endl;
00030 cout
00031 << "The program expects a file that contains exactly 4 objective function values per line. "
00032 << endl;
00033 cout << "If your program output differs, you have to adjust your file. "
00034 << endl;
00035 cout
00036 << "The name of the output file is the same is the inputfile + .sum. "
00037 << endl;
00038 if (argc > 1)
00039 {
00040 infilename = argv[1];
00041 }
00042 else
00043 {
00044 infilename = AskFilename("Infile name:");
00045 }
00046
00047 outfilename = infilename + ".sum";
00048
00049 ifstream objfile(infilename.c_str());
00050 vector<double> objvalues;
00051 copy(istream_iterator<double> (objfile), istream_iterator<double> (),
00052 back_inserter(objvalues));
00053
00054 if ((objvalues.size() % 4) != 0)
00055 {
00056 cerr << "Not 4 objective function values ! " << endl;
00057 return 100;
00058 }
00059 size_t index = 0;
00060 vector<double> sumvalues, first, second, third, fourth;
00061 ofstream sumfile(outfilename.c_str());
00062 while (index <= objvalues.size() - 4)
00063 {
00064 first.push_back(objvalues.at(index));
00065 second.push_back(objvalues.at(index + 1));
00066 third.push_back(objvalues.at(index + 2));
00067 fourth.push_back(objvalues.at(index + 3));
00068 index += 4;
00069 }
00070
00071
00072
00073
00074
00075 for (size_t i = 0; i < first.size(); ++i)
00076 {
00077 double sum = 0;
00078 sum = pow(first.at(i), 2) + pow(second.at(i), 2) + pow(third.at(i), 2)
00079 + pow(fourth.at(i), 2);
00080 sumvalues.push_back(sum / 4);
00081 sumfile << i << " " << sum / 4 << endl;
00082 }
00083
00084 vector<double>::iterator minref = min_element(sumvalues.begin(),
00085 sumvalues.end());
00086 index = distance(sumvalues.begin(), minref);
00087 cout << "Minimum: " << *minref << " at index " << index << endl;
00088 cout << "Objective Function values: ";
00089 for (size_t i = index * 4; i < index * 4 + 4; ++i)
00090 cout << objvalues.at(i) << " ";
00091 cout << endl;
00092 ofstream namefile((infilename + ".name").c_str());
00093 namefile << "best_" << index << ".mtt";
00094 }
00095