OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
FromFile.h
Go to the documentation of this file.
1 //
2 // Struct FromFile
3 // Simple functor that reads vector data from a file. If the file contains
4 // more than one value the sum is returned.
5 // \f[
6 // result = \sum_{i=0}^n value_i
7 // \f]
8 //
9 // Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
10 // All rights reserved
11 //
12 // Implemented as part of the PhD thesis
13 // "Toward massively parallel multi-objective optimization with application to
14 // particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
15 //
16 // This file is part of OPAL.
17 //
18 // OPAL is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
25 //
26 #ifndef __FROMFILE_H__
27 #define __FROMFILE_H__
28 
29 #include <string>
30 #include <map>
31 #include <set>
32 #include <fstream>
33 #include <iterator>
34 
35 #include "boost/tuple/tuple.hpp"
36 #include "boost/variant/get.hpp"
37 #include "boost/variant/variant.hpp"
38 #include "boost/smart_ptr.hpp"
39 
40 #include "Util/Types.h"
41 #include "Util/OptPilotException.h"
43 
44 struct FromFile {
45 
46  static const std::string name;
47 
49  if (args.size() != 1) {
50  throw OptPilotException("FromFile::operator()",
51  "fromFile expects 1 arguments, " + std::to_string(args.size()) + " given");
52  }
53 
54  filename_ = boost::get<std::string>(args[0]);
55 
56  double sum = 0;
57  bool is_valid = true;
58 
59  try {
60  readValues();
61  } catch(const OptPilotException& e) {
62  return boost::make_tuple(0.0, false);
63  }
64 
65  for(double obj_value : values_)
66  sum += obj_value;
67 
68  return boost::make_tuple(sum, is_valid);
69  }
70 
71 
72 private:
73 
74  std::vector<double> values_;
75 
76  std::string filename_;
77 
78  void readValues();
79 
80 };
81 
82 #endif
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
Definition: PETE.h:1111
boost::tuple< double, bool > Result_t
Definition: Expression.h:66
constexpr double e
The value of.
Definition: Physics.h:39
std::vector< argument_t > arguments_t
Definition: function.hpp:19
static const std::string name
Definition: FromFile.h:46
std::string filename_
Definition: FromFile.h:76
Expressions::Result_t operator()(client::function::arguments_t args)
Definition: FromFile.h:48
void readValues()
reads a simple list of double values
Definition: FromFile.cpp:29
std::vector< double > values_
Definition: FromFile.h:74