Working single-threaded version
This commit is contained in:
35
src/main.cpp
35
src/main.cpp
@@ -6,19 +6,19 @@
|
||||
|
||||
using namespace genetic;
|
||||
|
||||
const int len = 10;
|
||||
const float max_float = 9999.9f;
|
||||
const int len = 12;
|
||||
const float max_float = 999.9f;
|
||||
static uint64_t seed = 12;
|
||||
static float num_mutate_chance = 0.5;
|
||||
static int num_parents = 2;
|
||||
static int num_children = 2;
|
||||
|
||||
|
||||
static int target_sum = 200;
|
||||
static int target_product = 300;
|
||||
static int target_sum = 20000;
|
||||
static int target_product = 10*target_sum;
|
||||
|
||||
Array<float> make_new_arr() {
|
||||
Array<float> arr = { (float*)malloc(sizeof(float)*len), len };
|
||||
Array<float> arr = make_array<float>(len);
|
||||
for (int i = 0; i < arr.len; i++) {
|
||||
arr[i] = norm_rand(seed) * max_float;
|
||||
}
|
||||
@@ -50,15 +50,16 @@ float fitness(const Array<float> &cell) {
|
||||
sum += cell.data[i];
|
||||
product *= cell.data[i];
|
||||
}
|
||||
return abs(sum - target_sum) + abs(product - target_product);
|
||||
return abs(sum - target_sum)*abs(sum - target_sum) + abs(product - target_product);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int num_gens = 2000;
|
||||
Strategy<Array<float>> strat {
|
||||
.num_threads = 1,
|
||||
.batch_size = 1,
|
||||
.num_cells = 10,
|
||||
.num_generations = 10,
|
||||
.num_cells = 100000,
|
||||
.num_generations = num_gens,
|
||||
.test_all = true,
|
||||
.test_chance = 0.0, // doesn't matter
|
||||
.enable_crossover = true,
|
||||
@@ -66,7 +67,7 @@ int main(int argc, char **argv) {
|
||||
.crossover_parent_stride = 1,
|
||||
.crossover_children_num = 2,
|
||||
.enable_mutation = true,
|
||||
.mutation_chance = 0.8,
|
||||
.mutation_chance = 0.7,
|
||||
.rand_seed = seed,
|
||||
.higher_fitness_is_better = false,
|
||||
.make_default_cell=make_new_arr,
|
||||
@@ -76,4 +77,20 @@ int main(int argc, char **argv) {
|
||||
};
|
||||
|
||||
auto res = run(strat);
|
||||
|
||||
float sum = 0;
|
||||
float product = 1;
|
||||
printf("Winning cell: ");
|
||||
for (int i = 0; i < res.best_cell.back().len; i++) {
|
||||
float val = res.best_cell.back()[i];
|
||||
sum += val;
|
||||
product *= val;
|
||||
printf("%f ", val);
|
||||
}
|
||||
printf("\n");
|
||||
printf("Final Sum: %f\n", sum);
|
||||
printf("Final Product: %f\n", product);
|
||||
printf("Setup Time (s): %f\n", sync::to_s(res.setup_time));
|
||||
printf("Run Time (s): %f\n", sync::to_s(res.run_time));
|
||||
printf("Average Gen Time (s): %f\n", sync::to_s(res.run_time)/num_gens);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user