Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Using xbt/random
[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 "xbt.h"
8 #include "xbt/random.hpp"
9 #include <math.h>
10
11 namespace simgrid {
12 namespace kernel {
13 namespace profile {
14
15 double StochasticDatedValue::draw(Distribution law, std::vector<double> params)
16 {
17   switch (law) {
18     case Dist_Det:
19       return params[0];
20     case Dist_Exp:
21       return simgrid::xbt::random::exponential(params[0]);
22     case Dist_Unif:
23       return simgrid::xbt::random::uniform_real(params[0], params[1]);
24     case Dist_Norm:
25       return simgrid::xbt::random::normal(params[0], params[1]);
26     default:
27       xbt_assert(false, "Unimplemented distribution");
28       return 0;
29   }
30 }
31 double StochasticDatedValue::get_value()
32 {
33   return draw(value_law, value_params);
34 }
35 double StochasticDatedValue::get_date()
36 {
37   return draw(date_law, date_params);
38 }
39 DatedValue StochasticDatedValue::get_datedvalue()
40 {
41   DatedValue event;
42   event.date_  = get_date();
43   event.value_ = get_value();
44   return event;
45 }
46
47 bool StochasticDatedValue::operator==(StochasticDatedValue const& e2) const
48 {
49   return (e2.date_law == date_law) && (e2.value_law == value_law) && (e2.value_params == value_params) &&
50          (e2.date_params == date_params);
51 }
52
53 std::ostream& operator<<(std::ostream& out, const StochasticDatedValue& e)
54 {
55   out << e.date_law << " (";
56   for (unsigned int i = 0; i < e.date_params.size(); i++) {
57     out << e.date_params[i];
58     if (i != e.date_params.size() - 1) {
59       out << ",";
60     }
61   }
62   out << ") " << e.value_law << " (";
63   for (unsigned int i = 0; i < e.value_params.size(); i++) {
64     out << e.value_params[i];
65     if (i != e.value_params.size() - 1) {
66       out << ",";
67     }
68   }
69   out << ")";
70   return out;
71 }
72
73 } // namespace profile
74 } // namespace kernel
75 } // namespace simgrid