-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameter_search.cpp
71 lines (65 loc) · 3.01 KB
/
parameter_search.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
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 <algorithm>
#include <iostream>
#include <fstream>
#include <numeric>
#include <random>
#include "Functions/function.hpp"
#include "Functions/dejong.hpp"
#include "Algorithms/simple_ga.hpp"
extern std::mt19937 &get_generator();
// Function to perform a random parameter search on the SimpleGA algorithm.
// The results are written to a file.
void random_parameter_search(size_t population_size, size_t num_of_generations, double crossover_prob, double mutation_prob, size_t chromosome_size, size_t number_of_chromosomes, OptimizationFunction &function, size_t num_of_runs, std::string filename)
{
// Open the file and write the header.
std::ofstream file;
file.open(filename, std::ios::out | std::ios::trunc);
if (!file.is_open())
{
std::cout << "Could not open file " << filename << std::endl;
return;
}
file << "Run,Best Fitness,Best Value,Population Size,Generations,Crossover Prob., Mutation Prob." << std::endl;
// Set up the random number distributions.
std::uniform_int_distribution<int> population_size_dist(10, 200);
std::uniform_int_distribution<int> generations_dist(10, 200);
std::uniform_real_distribution<double> crossover_dist(0.0, 1.0);
std::uniform_real_distribution<double> mutation_dist(0.0, 0.1);
// Run the algorithm num_of_runs times.
// If i == 0, use the parameters passed to the function.
// Otherwise, generate random parameters.
// Write the results to the file.
// Uses OpenMP to parallelize the runs.
#pragma omp parallel for
for (size_t i = 0; i < num_of_runs; i++)
{
auto internal_population_size = population_size;
auto internal_num_of_generations = num_of_generations;
auto internal_crossover_prob = crossover_prob;
auto internal_mutation_prob = mutation_prob;
if (i != 0)
{
internal_population_size = (size_t)population_size_dist(get_generator());
internal_num_of_generations = (size_t)generations_dist(get_generator());
internal_crossover_prob = crossover_dist(get_generator());
internal_mutation_prob = mutation_dist(get_generator());
}
std::cout << "Run " << i << " of " << num_of_runs - 1 << std::endl;
auto algorithm = SimpleGA(internal_population_size, internal_num_of_generations, internal_crossover_prob, internal_mutation_prob, chromosome_size, number_of_chromosomes, function);
auto performance = algorithm.run();
auto best_fitness = performance.back().best_fitness;
auto best_x = performance.back().best_solution;
// Write the results to the file.
// Has to be within this critical block to prevent race conditions.
#pragma omp critical
{
file << i << "," << best_fitness << ",( ";
for (auto &x : best_x)
{
file << x << " ";
}
file << ")," << internal_population_size << "," << internal_num_of_generations << "," << internal_crossover_prob << "," << internal_mutation_prob << std::endl;
}
}
file.close();
}