GPLIB++
sumobjective.cpp
Go to the documentation of this file.
1 #include <fstream>
2 #include <iostream>
3 #include <iterator>
4 #include <algorithm>
5 #include <string>
6 #include <vector>
7 #include "statutils.h"
8 #include "Util.h"
9 
10 using namespace std;
11 using namespace gplib;
12 /*!
13  * \addtogroup UtilProgs Utility Programs
14  *@{
15  * \file
16  * Find the model from the .final output file of 1dinvga with minimal squared sum of objective functions.
17  * The current version assumes 4 objective functions.
18  */
19 
20 int main(int argc, char *argv[])
21  {
22  string infilename, outfilename;
23  string version =
24  "$Id: sumobjective.cpp 1816 2009-09-07 11:28:35Z mmoorkamp $";
25  cout << endl << endl;
26  cout << "Program " << version << endl;
27  cout << "Sum up the objective function values as output by the Pareto-GA"
28  << endl;
29  cout << "and find the model with smallest distance from origin. " << endl;
30  cout
31  << "The program expects a file that contains exactly 4 objective function values per line. "
32  << endl;
33  cout << "If your program output differs, you have to adjust your file. "
34  << endl;
35  cout
36  << "The name of the output file is the same is the inputfile + .sum. "
37  << endl;
38  if (argc > 1)
39  {
40  infilename = argv[1];
41  }
42  else
43  {
44  infilename = AskFilename("Infile name:");
45  }
46 
47  outfilename = infilename + ".sum";
48 
49  ifstream objfile(infilename.c_str());
50  vector<double> objvalues;
51  copy(istream_iterator<double> (objfile), istream_iterator<double> (),
52  back_inserter(objvalues));
53 
54  if ((objvalues.size() % 4) != 0)
55  {
56  cerr << "Not 4 objective function values ! " << endl;
57  return 100;
58  }
59  size_t index = 0;
60  vector<double> sumvalues, first, second, third, fourth;
61  ofstream sumfile(outfilename.c_str());
62  while (index <= objvalues.size() - 4)
63  {
64  first.push_back(objvalues.at(index));
65  second.push_back(objvalues.at(index + 1));
66  third.push_back(objvalues.at(index + 2));
67  fourth.push_back(objvalues.at(index + 3));
68  index += 4;
69  }
70  // double firstmean = Mean(first.begin(),first.end());
71  // double secondmean = Mean(second.begin(),second.end());
72  // double thirdmean = Mean(third.begin(),third.end());
73  // double fourthmean = Mean(fourth.begin(),fourth.end());
74 
75  for (size_t i = 0; i < first.size(); ++i)
76  {
77  double sum = 0;
78  sum = pow(first.at(i), 2) + pow(second.at(i), 2) + pow(third.at(i), 2)
79  + pow(fourth.at(i), 2);
80  sumvalues.push_back(sum / 4);
81  sumfile << i << " " << sum / 4 << endl;
82  }
83 
84  vector<double>::iterator minref = min_element(sumvalues.begin(),
85  sumvalues.end());
86  index = distance(sumvalues.begin(), minref);
87  cout << "Minimum: " << *minref << " at index " << index << endl;
88  cout << "Objective Function values: ";
89  for (size_t i = index * 4; i < index * 4 + 4; ++i)
90  cout << objvalues.at(i) << " ";
91  cout << endl;
92  ofstream namefile((infilename + ".name").c_str());
93  namefile << "best_" << index << ".mtt";
94  }
95 /*@}*/
int main(int argc, char *argv[])
string version
Definition: makeinput.cpp:16