Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dff938ee7604446b2a5b2c0e937cd11b58f13e96
[simgrid.git] / src / kernel / resource / profile / StochasticDatedValue.cpp
1 /* Copyright (c) 2004-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "src/kernel/resource/profile/StochasticDatedValue.hpp"
7 #include "src/statmc/rng.hpp"
8 #include "xbt.h"
9 #include "xbt/random.hpp"
10 #include <math.h>
11
12 namespace simgrid {
13 namespace kernel {
14 namespace profile {
15
16 double StochasticDatedValue::draw(Distribution law, std::vector<double> params)
17 {
18   switch (law) {
19     case Dist_Det:
20       return params[0];
21     case Dist_Exp:
22       return simgrid::xbt::random::exponential(params[0]);
23     case Dist_Unif:
24       return simgrid::xbt::random::uniform_real(params[0], params[1]);
25     case Dist_Norm:
26       return simgrid::xbt::random::normal(params[0], params[1]);
27     default:
28       xbt_assert(false, "Unimplemented distribution");
29       return 0;
30   }
31 }
32 double StochasticDatedValue::get_value()
33 {
34   return draw(value_law, value_params);
35 }
36 double StochasticDatedValue::get_date()
37 {
38   return draw(date_law, date_params);
39 }
40 DatedValue StochasticDatedValue::get_datedvalue()
41 {
42   DatedValue event;
43   event.date_  = get_date();
44   event.value_ = get_value();
45   return event;
46 }
47
48 bool StochasticDatedValue::operator==(StochasticDatedValue const& e2) const
49 {
50   return (e2.date_law == date_law) && (e2.value_law == value_law) && (e2.value_params == value_params) &&
51          (e2.date_params == date_params);
52 }
53
54 std::ostream& operator<<(std::ostream& out, const StochasticDatedValue& e)
55 {
56   out << e.date_law << " (";
57   for (unsigned int i = 0; i < e.date_params.size(); i++) {
58     out << e.date_params[i];
59     if (i != e.date_params.size() - 1) {
60       out << ",";
61     }
62   }
63   out << ") " << e.value_law << " (";
64   for (unsigned int i = 0; i < e.value_params.size(); i++) {
65     out << e.value_params[i];
66     if (i != e.value_params.size() - 1) {
67       out << ",";
68     }
69   }
70   out << ")";
71   return out;
72 }
73
74 } // namespace profile
75 } // namespace kernel
76 } // namespace simgrid