]> AND Private Git Repository - loba.git/blob - cost_func.cpp
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Automatically update "options.cpp".
[loba.git] / cost_func.cpp
1 #include <algorithm>
2 #include <tr1/functional>
3 #include <numeric>
4 #include <iterator>
5 #include <sstream>
6 #include <stdexcept>
7
8 #include "cost_func.h"
9
10 cost_func::cost_func(const char* param)
11 {
12     std::istringstream paramstream(param);
13     std::string token;
14     while (std::getline(paramstream, token, ',')) {
15         std::istringstream str(token);
16         double f;
17         if ((str >> f) && str.eof())
18             factors.push_back(f);
19         else
20             throw std::invalid_argument("cost_func(): "
21                                         "cannot parse \"" + token + "\"");
22     }
23     if (factors.empty())
24         throw std::invalid_argument("cost_func(): no factor");
25 }
26
27 cost_func::~cost_func()
28 {
29 }
30
31 double cost_func::operator()(double amount) const
32 {
33     using std::tr1::bind;
34     using std::tr1::placeholders::_1;
35     using std::tr1::placeholders::_2;
36     return std::accumulate(++factors.begin(), factors.end(), factors.front(),
37                            bind(std::plus<double>(),
38                                 bind(std::multiplies<double>(), amount, _1),
39                                 _2));
40 }
41
42 std::string cost_func::to_string()
43 {
44     std::ostringstream oss;
45     std::copy(factors.begin(), --factors.end(),
46               std::ostream_iterator<double>(oss, ", "));
47     oss << factors.back();
48     return oss.str();
49 }
50
51 // Local Variables:
52 // mode: c++
53 // End: