-
Notifications
You must be signed in to change notification settings - Fork 2
/
multi_peak_function_int.cpp
46 lines (38 loc) · 1.21 KB
/
multi_peak_function_int.cpp
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
/**
* @file multi_peak_function_int.cpp
* @author Ali Marouf (https://github.com/AliMarouf97)
* @brief solving a maximization problem using GA
* @version 0.1
*
* @copyright Copyright (c) 2022
*
*/
#include <iostream>
#include "GeneticAlgorithm.h"
using namespace std;
// In the range [-205, 205] what is the maximum value of the function $f(x) = x * (0.4 + sin(x / 2))$.
// Define range as Macros
#define MN -205
#define MX 205
// Save MOD to const int to reduce recalculating operations.
const uint16_t MOD = RANGE_ENCODER(MN, MX);
#define MY_DECODER(chromosome) ((int)RANGE_DECODER(chromosome, MOD, MN))
// int MY_DECODER(uint16_t chromosome)
// {
// return ((int) RANGE_DECODER(chromosome, MOD, MN));
// }
double fitnessFunction(uint16_t chromosome)
{
double x = MY_DECODER(chromosome);
return x * (0.4 + sin(x / 2));
}
int main()
{
GeneticAlgorithm<uint16_t> ga(fitnessFunction, true, 1500, 2, 30);
ga.initializePopulation(100);
ga.terminationConditions.setMaxIterations(20);
auto best = ga.solve();
cout << "Best: f(" << MY_DECODER(best.getChromosome()) << ") = " << best.getFitness() << "\n";
// Best: f(204) = 284.545
return 0;
}