AdditiveEpsilonIndicatorMain.cpp
Go to the documentation of this file.
1 #include <shark/Algorithms/DirectSearch/Indicators/AdditiveEpsilonIndicator.h>
2 #include <shark/Algorithms/DirectSearch/FitnessExtractor.h>
3 #include <shark/Algorithms/DirectSearch/ParetoDominanceComparator.h>
4 #include <shark/Algorithms/DirectSearch/FastNonDominatedSort.h>
5 
6 #include <boost/program_options.hpp>
7 #include <boost/algorithm/string.hpp>
8 
9 #include <fstream>
10 #include <string>
11 #include <vector>
12 
13 namespace shark {
14  typedef std::vector< shark::RealVector > FrontType;
15 
16  template<typename Stream>
17  FrontType read_front( Stream & in, std::size_t noObjectives, const std::string & separator = " ", std::size_t headerLines = 0 ) {
18 
19  if( !in ) {
20  throw( shark::Exception( "Bad stream in shark::read_front", __FILE__, __LINE__ ) );
21  }
22 
23  std::string line;
24 
25  // Skip header lines
26  std::size_t counter = 0;
27  for( counter = 0; counter < headerLines; counter++ )
28  std::getline( in, line );
29 
30  FrontType result;
31 
32  while( std::getline( in, line ) ) {
33  if( line.empty() )
34  continue;
35 
36  std::vector< std::string > tokens;
37  boost::algorithm::split( tokens, line, boost::is_any_of( separator ), boost::token_compress_on );
38 
39  if( tokens.size() < noObjectives )
40  continue;
41 
42  shark::RealVector v( noObjectives, 0. );
43 
44  for( std::size_t i = 0; i < noObjectives; i++ ) {
45  v[ i ] = boost::lexical_cast<double>( tokens[ i ] );
46  // TODO: floating point checks.
47  }
48 
49  result.push_back( v );
50  }
51 
52  return( result );
53  }
54 }
55 
56 int main( int argc, char ** argv ) {
57 
58  boost::program_options::options_description options;
59  options.add_options()
60  ( "referenceFront", boost::program_options::value< std::string >(), "File containing the reference front" )
61  ( "noObjectives", boost::program_options::value< std::size_t >(), "No of objectives" )
62  ( "separator", boost::program_options::value< std::string >()->default_value( " " ), "Character that separates fields" )
63  ( "headerLines", boost::program_options::value< std::size_t >()->default_value( 0 ), "Amount of header lines to skip" );
64 
65 
66  boost::program_options::variables_map vm;
67  try {
68  boost::program_options::store(boost::program_options::parse_command_line(argc, argv, options), vm);
69  boost::program_options::notify(vm);
70  } catch( ... ) {
71  std::cerr << options << std::endl;
72  return( EXIT_FAILURE );
73  }
74 
75  if( vm.count( "referenceFront" ) == 0 ) {
76  std::cerr << options << std::endl;
77  return( EXIT_FAILURE );
78  }
79 
80  if( vm.count( "noObjectives" ) == 0 ) {
81  std::cerr << options << std::endl;
82  return( EXIT_FAILURE );
83  }
84 
85  std::size_t noObjectives = vm[ "noObjectives" ].as< std::size_t >();
86  std::string referenceFront = vm[ "referenceFront" ].as< std::string >();
87 
88  std::ifstream in( referenceFront.c_str() );
89 
90  shark::FrontType refFront;
91 
92  try {
93  refFront = shark::read_front( in, noObjectives, vm[ "separator" ].as< std::string >() );
94  } catch( shark::Exception exp ) {
95  std::cerr << "Problem reading reference front, aborting now." << std::endl;
96  std::cerr << exp.what() << std::endl;
97  return( EXIT_FAILURE );
98  }
99 
100  shark::FrontType front;
101 
102  try {
103  front = shark::read_front( std::cin, noObjectives, vm[ "separator" ].as< std::string >(), vm[ "headerLines" ].as< std::size_t >() );
104  } catch( ... ) {
105  std::cerr << "Problem reading front from std::cin, aborting now." << std::endl;
106  return( EXIT_FAILURE );
107  }
108 
109  //std::copy( front.begin(), front.end(), std::ostream_iterator< shark::RealVector >( std::cout, "\n" ) );
111  shark::IdentityFitnessExtractor ife;
112 
113  std::cout << addEps( front.begin(),front.end(), refFront.begin(),refFront.end(), ife ) << std::endl;
114 
115  return( EXIT_SUCCESS );
116 
117 }