-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCombinationGenerator.cpp
More file actions
136 lines (123 loc) · 3.99 KB
/
CombinationGenerator.cpp
File metadata and controls
136 lines (123 loc) · 3.99 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
// Copyright (c) 2025 Chek Wei Tan
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
#include <vector>
#include <cmath>
#include "ConfigManager.hpp"
#include "CombinationGenerator.hpp"
namespace CombinationGenerator
{
void GenerateCombinations(
size_t k,
std::vector<std::vector<double>> &combinations,
std::vector<double> ¤t_combination,
const std::vector<InputConfig> ¶ms)
{
if (k == params.size())
{
combinations.push_back(current_combination);
return;
}
const auto ¶m = params[k];
if (std::fabs(param.Increment) < 1e-9)
{
if (param.MinValue <= param.MaxValue)
{
current_combination.push_back(param.MinValue);
GenerateCombinations(k + 1, combinations, current_combination, params);
current_combination.pop_back();
}
}
else if (param.Increment > 0)
{
for (double i = param.MinValue; i <= param.MaxValue + 1e-9; i += param.Increment)
{
current_combination.push_back(i);
GenerateCombinations(k + 1, combinations, current_combination, params);
current_combination.pop_back();
}
}
else // param.Increment < 0
{
for (double i = param.MinValue; i >= param.MaxValue - 1e-9; i += param.Increment)
{
current_combination.push_back(i);
GenerateCombinations(k + 1, combinations, current_combination, params);
current_combination.pop_back();
}
}
}
std::vector<std::vector<double>> Generate(const std::vector<InputConfig> ¶ms)
{
std::vector<std::vector<double>> combinations;
std::vector<double> current_combination;
GenerateCombinations(0, combinations, current_combination, params);
return combinations;
}
std::vector<std::vector<double>> GenerateIterative(const std::vector<InputConfig> ¶ms)
{
std::vector<std::vector<double>> combinations;
std::vector<InputConfig> varyingParams;
for (const auto &p : params)
{
if (std::fabs(p.Increment) > 1e-9)
{
varyingParams.push_back(p);
}
}
if (varyingParams.empty())
{
if (!params.empty())
{
combinations.push_back({});
}
return combinations;
}
std::vector<double> currentCombination;
std::vector<size_t> p_indices(varyingParams.size(), 0);
std::vector<std::vector<double>> paramValues;
for (const auto &p : varyingParams)
{
std::vector<double> values;
if (p.Increment > 0)
{
for (double i = p.MinValue; i <= p.MaxValue + 1e-9; i += p.Increment)
{
values.push_back(i);
}
}
else
{
for (double i = p.MinValue; i >= p.MaxValue - 1e-9; i += p.Increment)
{
values.push_back(i);
}
}
paramValues.push_back(values);
}
while (true)
{
currentCombination.clear();
for (size_t i = 0; i < varyingParams.size(); ++i)
{
currentCombination.push_back(paramValues[i][p_indices[i]]);
}
combinations.push_back(currentCombination);
size_t k = varyingParams.size() - 1;
while (k >= 0)
{
p_indices[k]++;
if (p_indices[k] < paramValues[k].size())
{
break;
}
p_indices[k] = 0;
k--;
}
if (k < 0)
{
break;
}
}
return combinations;
}
}