-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryGASolver.h
More file actions
71 lines (60 loc) · 1.61 KB
/
BinaryGASolver.h
File metadata and controls
71 lines (60 loc) · 1.61 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
#include <vector>
#include <functional>
namespace BinaryGA
{
enum class MutationType
{
None,
Toggle,
Swap,
Custom
};
enum class CrossoverType
{
None,
OnePoint,
Ordered
};
enum class ParentSelectionType
{
RouletteWheel,
Ranked
};
template<typename T>
using ComputeFitnessCallback = std::function<double(const std::vector<T>&)>;
enum EvaluationResult
{
ObjectiveReached,
ContinueProcessing
};
template<typename T>
using EvaluateCurrentState = std::function<EvaluationResult(uint32_t, const std::vector<T>&)>;
// custom mutation callback receives value and index of gene in chromosome
// return mutated value of gene
template<typename T>
using CustomMutation = std::function<T(const T&, size_t)>;
template<typename T>
using CustomInitialization = std::function<std::vector<T>(size_t)>;
template<typename T>
struct Definition
{
uint32_t numberOfGenes;
uint32_t populationSize;
uint32_t maxNumberOfGenerations;
ComputeFitnessCallback<T> computeFitness;
EvaluateCurrentState<T> evaluate;
MutationType mutation;
CrossoverType crossover;
ParentSelectionType parentSelection;
double mutationProbability;
double crossoverFactor;
CustomMutation<T> mutationCustomCallback;
CustomInitialization<T> initializationCustomCallback;
};
template<typename T>
std::vector<T> Solve(const Definition<T> & definition);
// explicit template instantiation
template std::vector<bool> Solve(const Definition<bool> & definition);
template std::vector<uint8_t> Solve(const Definition<uint8_t> & definition);
template std::vector<double> Solve(const Definition<double> & definition);
}