-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.cpp
More file actions
180 lines (145 loc) · 5.19 KB
/
Copy pathFunction.cpp
File metadata and controls
180 lines (145 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <vector>
#include "Function.h"
#include "Node.h"
#include "Random.h"
using namespace std;
Function::Function()
{
this->name = "Undefined function";
this->parity = -1;
this->func = [](const Terminal& x, const Terminal& y) { return 0; };
}
Function::Function(string name, int parity, function < double (const double& a, const double& b) > func)
{
this->name = name;
this->parity = parity;
this->func = func;
}
Function::Function(const Function& function)
{
this->name = function.name;
this->parity = function.parity;
this->func = function.func;
}
string Function::getName() const
{
return this->name;
}
int Function::getParity() const
{
return this->parity;
}
double Function::evaluate(const double& a, const double& b) const
{
return this->func(a,b);
}
FunctionSet::FunctionSet(const int& size)
{
this->functions = vector<Function>(size);
}
void FunctionSet::addFunction(const Function& func)
{
this->functions.push_back(func);
}
FunctionSet FunctionSet::createArithmeticFunctionSet()
{
FunctionSet functionSet = FunctionSet();
functionSet.addFunction(Function("+", 2, [](const double &x, const double& y) { return x + y; }));
functionSet.addFunction(Function("-", 2, [](const double& x, const double& y) { return x - y; }));
functionSet.addFunction(Function("*", 2, [](const double& x, const double& y) { return x * y; }));
functionSet.addFunction(Function("%", 2, [](const double& x, const double& y) { return x / y; }));
functionSet.addFunction(Function("neg", 1, [](const double& x, const double& y) { return -x; }));
functionSet.addFunction(Function("inv", 1, [](const double& x, const double& y) { return 1/x; }));
return functionSet;
}
FunctionSet FunctionSet::createBinaryOnlyFunctionSet()
{
FunctionSet functionSet = FunctionSet();
functionSet.addFunction(Function("+", 2, [](const double& x, const double& y) { return x + y; }));
functionSet.addFunction(Function("-", 2, [](const double& x, const double& y) { return x - y; }));
functionSet.addFunction(Function("*", 2, [](const double& x, const double& y) { return x * y; }));
functionSet.addFunction(Function("%", 2, [](const double& x, const double& y) { return x / y; }));
return functionSet;
}
FunctionSet FunctionSet::createArithmeticFunctionSetNoDivide()
{
FunctionSet functionSet = FunctionSet(4);
functionSet.addFunction(Function("+", 2, [](const double& x, const double& y) { return x + y; }));
functionSet.addFunction(Function("-", 2, [](const double& x, const double& y) { return x - y; }));
functionSet.addFunction(Function("x", 2, [](const double& x, const double& y) { return x * y; }));
functionSet.addFunction(Function("neg", 1, [](const double& x, const double& y) { return -x; }));
return functionSet;
}
Function FunctionSet::getRandomFunction() const
{
const size_t & seed = Random::randInt(0, this->functions.size() - 1);
return this->functions.at(seed);
}
Function FunctionSet::getRandomFunction(const std::map<std::string, double>& probabilityMap) const {
// Vytvoøíme vektor kumulativních pravdìpodobností
std::vector<double> cumulative;
std::vector<size_t> indices;
double total = 0.0;
for (size_t i = 0; i < functions.size(); ++i) {
const Function& f = functions[i];
std::map<std::string, double>::const_iterator it = probabilityMap.find(f.getName());
if (it == probabilityMap.end()) {
throw std::invalid_argument("Missing probability for function: " + f.getName());
}
double p = it->second;
if (p < 0.0) {
throw std::invalid_argument("Negative probability for function: " + f.getName());
}
total += p;
cumulative.push_back(total); // mezisouèet
indices.push_back(i); // odpovídající index ve functions
}
if (total == 0.0) {
throw std::runtime_error("Total function probability is zero.");
}
if (std::abs(total - 1.0) > 1e-6) {
throw std::runtime_error("Function probabilities must sum to 1.0");
}
// Náhodná hodnota v rozsahu <0, total)
double r = Random::randProb();
// Najdi první index, kde kumulativní pravdìpodobnost > r
for (size_t i = 0; i < cumulative.size(); ++i) {
if (r < cumulative[i]) {
return functions[indices[i]];
}
}
// Pokud nic nenajdeme (kvùli zaokrouhlení), vra poslední
return functions[indices.back()];
}
std::vector<int> FunctionSet::prepareFunctionIndexPool(const std::map<std::string, double>& functionProbabilities) const {
std::vector<int> indexPool;
for (int i = 0; i < functions.size(); ++i) {
const Function& func = functions[i];
auto it = functionProbabilities.find(func.getName());
if (it == functionProbabilities.end()) {
continue; // nebo výchozí pravdìpodobnost 0
}
double probability = it->second;
int repeat = static_cast<int>(probability * 100); // škálujeme pro diskrétní rozložení
for (int j = 0; j < repeat; ++j) {
indexPool.push_back(i);
}
}
if (indexPool.empty()) {
throw std::invalid_argument("Function probability pool is empty. Check your probability map.");
}
return indexPool;
}
const std::vector<Function>& FunctionSet::getFunctions() const {
return functions;
}
bool operator==(const Function& lhs, const Function& rhs)
{
return lhs.getName() == rhs.getName() &&
lhs.getParity() == rhs.getParity();
}
bool operator==(const Function lhs, const Function rhs)
{
return false;
}