BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
ExpectedValueFactory.cpp
Go to the documentation of this file.
2#include <stdexcept>
3#include <algorithm>
4
5using namespace BPMNOS;
6
7void ExpectedValueFactory::registerFunctions(LIMEX::Handle<double>& handle) {
8 const auto& existingNames = handle.getNames();
9 auto nameExists = [&](const std::string& name) {
10 return std::find(existingNames.begin(), existingNames.end(), name) != existingNames.end();
11 };
12
13 // uniform(min, max) - Expected value: (min + max) / 2
14 if (!nameExists("uniform")) {
15 handle.add("uniform", [](const std::vector<double>& args) -> double {
16 if (args.size() != 2) {
17 throw std::runtime_error("uniform requires 2 arguments: min, max");
18 }
19 return (args[0] + args[1]) / 2.0;
20 });
21 }
22
23 // uniform_int(min, max) - Expected value: (min + max) / 2
24 if (!nameExists("uniform_int")) {
25 handle.add("uniform_int", [](const std::vector<double>& args) -> double {
26 if (args.size() != 2) {
27 throw std::runtime_error("uniform_int requires 2 arguments: min, max");
28 }
29 return (args[0] + args[1]) / 2.0;
30 });
31 }
32
33 // normal(mean, stddev) - Expected value: mean
34 if (!nameExists("normal")) {
35 handle.add("normal", [](const std::vector<double>& args) -> double {
36 if (args.size() != 2) {
37 throw std::runtime_error("normal requires 2 arguments: mean, stddev");
38 }
39 return args[0];
40 });
41 }
42
43 // exponential(rate) - Expected value: 1 / rate
44 if (!nameExists("exponential")) {
45 handle.add("exponential", [](const std::vector<double>& args) -> double {
46 if (args.size() != 1) {
47 throw std::runtime_error("exponential requires 1 argument: rate");
48 }
49 return 1.0 / args[0];
50 });
51 }
52
53 // poisson(mean) - Expected value: mean
54 if (!nameExists("poisson")) {
55 handle.add("poisson", [](const std::vector<double>& args) -> double {
56 if (args.size() != 1) {
57 throw std::runtime_error("poisson requires 1 argument: mean");
58 }
59 return args[0];
60 });
61 }
62
63 // bernoulli(p) - Expected value: p
64 if (!nameExists("bernoulli")) {
65 handle.add("bernoulli", [](const std::vector<double>& args) -> double {
66 if (args.size() != 1) {
67 throw std::runtime_error("bernoulli requires 1 argument: probability");
68 }
69 return args[0];
70 });
71 }
72
73 // binomial(n, p) - Expected value: n * p
74 if (!nameExists("binomial")) {
75 handle.add("binomial", [](const std::vector<double>& args) -> double {
76 if (args.size() != 2) {
77 throw std::runtime_error("binomial requires 2 arguments: trials, probability");
78 }
79 return args[0] * args[1];
80 });
81 }
82
83 // gamma(shape, scale) - Expected value: shape * scale
84 if (!nameExists("gamma")) {
85 handle.add("gamma", [](const std::vector<double>& args) -> double {
86 if (args.size() != 2) {
87 throw std::runtime_error("gamma requires 2 arguments: shape, scale");
88 }
89 return args[0] * args[1];
90 });
91 }
92
93 // lognormal(logscale, shape) - Expected value: exp(logscale + shape^2 / 2)
94 if (!nameExists("lognormal")) {
95 handle.add("lognormal", [](const std::vector<double>& args) -> double {
96 if (args.size() != 2) {
97 throw std::runtime_error("lognormal requires 2 arguments: logscale, shape");
98 }
99 return std::exp(args[0] + args[1] * args[1] / 2.0);
100 });
101 }
102
103 // geometric(p) - Expected value: (1 - p) / p
104 if (!nameExists("geometric")) {
105 handle.add("geometric", [](const std::vector<double>& args) -> double {
106 if (args.size() != 1) {
107 throw std::runtime_error("geometric requires 1 argument: probability");
108 }
109 return (1.0 - args[0]) / args[0];
110 });
111 }
112}
void registerFunctions(LIMEX::Handle< double > &handle)
Register all expected value functions with the given LIMEX handle.