Files
GeneticAlgo/inc/genetic.h

299 lines
9.8 KiB
C++

#pragma once
#include <algorithm>
#include <cstdlib>
#include "util.h"
#include "sync.h"
#include "rand.h"
using namespace sync;
namespace genetic {
template <class T> struct Stats;
template <class T> struct Strategy;
struct CellTracker;
template <class T> T run(Strategy<T>);
template <class T> struct Strategy {
// Number of worker threads that will be evaluating cell fitness
int num_threads;
// Period of print statements (in seconds)
float stats_print_period_s;
// Size of the population pool per sim thread
int num_cells_per_thread;
// Number of times (epochs) to run the algorithm
int num_generations;
bool test_all; // Sets whether or not every cell's fitness is evaluated every
// generation
float test_chance; // Chance to test any given cell's fitness. Relevant only
// if test_all is false.
bool enable_crossover; // Cells that score well in the evaluation stage
// produce children that replace low-scoring cells
int crossover_parent_num; // Number of unique high-scoring parents in a
// crossover call.
int crossover_parent_stride; // Number of parents to skip over when moving to
// the next set of parents. A stride of 1 would
// produce maximum overlap because the set of
// parents would only change by one every
// crossover.
int crossover_children_num; // Number of children to expect the user to
// produce in the crossover function.
bool enable_mutation; // Cells may be mutated
// before fitness evaluation
float mutation_chance; // Chance for any given cell to be mutated cells during
// the mutation
uint64_t rand_seed;
bool higher_fitness_is_better; // Sets whether or not to consider higher
// fitness values better or worse. Set this to
// false if fitness is an error function.
// User defined functions
T (*make_default_cell)();
void (*mutate)(T &cell_to_modify);
void (*crossover)(const Array<T *> parents, const Array<T *> out_children);
float (*fitness)(const T &cell);
};
template<class T> struct Stats {
DynArray<T> best_cells;
DynArray<float> best_cell_fitness;
int gen;
bool done;
TimeSpan start, end;
TimeSpan total_crossover_time;
int total_crossovers;
TimeSpan total_mutate_time;
int total_mutates;
TimeSpan total_fitness_time;
int total_evaluations;
TimeSpan total_sorting_time;
int total_sorts;
Mutex m;
};
struct CellTracker {
float score;
int cellid;
};
template<class T>
struct WorkerThreadArgs {
Strategy<T> strat;
Array<T> cells;
Array<CellTracker> trackers;
Stats<T> *stats;
};
template<class T> T* _cellp(Array<T> cells, CellTracker tracker) { return &cells[tracker.cellid]; }
template <class T> DWORD worker(LPVOID args) {
// Unpack everything...
WorkerThreadArgs<T>* worker_args = static_cast<WorkerThreadArgs<T>*>(args);
Strategy<T> strat = worker_args->strat;
Array<T> cells = worker_args->cells;
Array<CellTracker> trackers = worker_args->trackers;
Stats<T> &stats = *worker_args->stats;
// Prepare crossover operations as these will be the same every time except
// for the exact cell pointers
int npar = strat.crossover_parent_num;
int nchild = strat.crossover_children_num;
Array<T*> parents = make_array<T*>(npar);
Array<T*> children = make_array<T*>(nchild);
TimeSpan start_algo = now();
TimeSpan start;
while(stats.gen < strat.num_generations) {
// 1. crossover
start = now();
if (strat.enable_crossover) {
int parent_end = npar;
int child_begin = trackers.len-nchild;
while (parent_end <= child_begin) {
// Get pointers to all the parent cells
for (int i = parent_end-npar; i < parent_end; i++) {
parents[i - (parent_end-npar)] = _cellp(cells, trackers[i]);
}
// Get pointers to all the child cells (these will be overwritten)
for (int i = child_begin; i < child_begin+nchild; i++) {
children[i-child_begin] = _cellp(cells, trackers[i]);
}
strat.crossover(parents, children);
parent_end += strat.crossover_parent_stride;
child_begin -= nchild;
}
}
lock(stats.m);
stats.total_crossover_time = stats.total_crossover_time + (now() - start);
stats.total_crossovers++;
unlock(stats.m);
// 2. mutate
start = now();
for (int i = 0; i < trackers.len; i++) {
if (abs(norm_rand(strat.rand_seed)) < strat.mutation_chance) {
strat.mutate(cells[trackers[i].cellid]);
}
}
lock(stats.m);
stats.total_mutate_time = stats.total_mutate_time + (now() - start);
stats.total_mutates++;
unlock(stats.m);
// 3. evaluate
start = now();
if (strat.test_all) {
for (int i = 0; i < trackers.len; i++) {
trackers[i].score = strat.fitness(cells[trackers[i].cellid]);
}
} else {
for (int i = 0; i < trackers.len; i++) {
if (abs(norm_rand(strat.rand_seed)) < strat.test_chance) {
trackers[i].score = strat.fitness(cells[trackers[i].cellid]);
}
}
}
lock(stats.m);
stats.total_fitness_time = stats.total_fitness_time + (now() - start);
stats.total_evaluations++;
unlock(stats.m);
// 4. sort
start = now();
std::sort(&trackers[0], &trackers[trackers.len-1], [strat](CellTracker &a, CellTracker &b){ return strat.higher_fitness_is_better ? a.score > b.score : a.score < b.score; });
lock(stats.m);
stats.total_sorting_time = stats.total_sorting_time + (now() - start);
stats.total_sorts++;
append(stats.best_cells, cells[trackers[0].cellid]);
append(stats.best_cell_fitness, trackers[0].score);
stats.gen++;
unlock(stats.m);
}
stats.done = true;
stats.end = now();
return 0;
}
template <class T> T run(Strategy<T> strat) {
Array<Stats<T>> stats = make_array<Stats<T>>(strat.num_threads);
Array<Thread> threads = make_array<Thread>(strat.num_threads);
Array<T> cells = make_array<T>(strat.num_threads*strat.num_cells_per_thread);
Array<CellTracker> trackers = make_array<CellTracker>(cells.len);
Array<WorkerThreadArgs<T>> args = make_array<WorkerThreadArgs<T>>(strat.num_threads);
for (int i = 0; i < cells.len; i++) {
cells[i] = strat.make_default_cell();
trackers[i] = {0, i};
}
for (int i = 0; i < strat.num_threads; i++) {
stats[i] = {
.best_cells=make_dynarray<T>(strat.num_generations),
.best_cell_fitness=make_dynarray<float>(strat.num_generations),
.gen=0,
.done=false,
.start=from_s(0),
.end=from_s(0),
.total_crossover_time=from_s(0),
.total_crossovers=0,
.total_mutate_time=from_s(0),
.total_mutates=0,
.total_fitness_time=from_s(0),
.total_evaluations=0,
.total_sorting_time=from_s(0),
.total_sorts=0,
.m=make_mutex()
};
Array<T> tcells = { &cells[i*strat.num_cells_per_thread], strat.num_cells_per_thread };
Array<CellTracker> ttrackers = { &trackers[i*strat.num_cells_per_thread], strat.num_cells_per_thread };
args[i].strat=strat;
args[i].cells=tcells;
args[i].trackers=ttrackers;
args[i].stats=&stats[i];
threads[i] = make_thread(worker<T>, &args[i]);
}
// We are the stats thread
bool complete = false;
while (!complete) {
sleep(from_s(strat.stats_print_period_s));
printf("**********************\n");
float g_avg_crossover_time = 0;
float g_avg_mutate_time = 0;
float g_avg_fitness_time = 0;
float g_avg_sorting_time = 0;
float g_progress_per = 0;
float g_best_fitness = strat.higher_fitness_is_better ? 0.0 : 999999999999999999.9;
complete = true;
for (int i = 0; i < stats.len; i++) {
lock(stats[i].m);
complete &= stats[i].done;
float avg_crossover_time = to_s(stats[i].total_crossover_time) / static_cast<float>(stats[i].total_crossovers);
float avg_mutate_time = to_s(stats[i].total_mutate_time) / static_cast<float>(stats[i].total_mutates);
float avg_fitness_time = to_s(stats[i].total_fitness_time) / static_cast<float>(stats[i].total_evaluations);
float avg_sorting_time = to_s(stats[i].total_sorting_time) / static_cast<float>(stats[i].total_sorts);
float progress_per = static_cast<float>(stats[i].gen) / static_cast<float>(strat.num_generations) * 100;
float best_score = back(stats[i].best_cell_fitness);
g_avg_crossover_time += avg_crossover_time;
g_avg_mutate_time += avg_mutate_time;
g_avg_fitness_time += avg_fitness_time;
g_avg_sorting_time += avg_sorting_time;
g_progress_per += progress_per;
g_best_fitness = strat.higher_fitness_is_better ? max(best_score, g_best_fitness) : min(best_score, g_best_fitness);
printf("THREAD %d, Progress %.1f\%, Top Score %.5e, Cross %.5f (s), Mutate: %.5f (s), Fitness: %.5f (s), Sorting: %.5f (s)\n", i, progress_per, best_score, avg_crossover_time, avg_mutate_time, avg_fitness_time, avg_sorting_time);
unlock(stats[i].m);
}
g_avg_crossover_time /= stats.len;
g_avg_mutate_time /= stats.len;
g_avg_fitness_time /= stats.len;
g_avg_sorting_time /= stats.len;
g_progress_per /= stats.len;
printf("OVERALL, Progress %.1f\%, Top Score: %.5e, Cross %.5f (s), Mutate: %.5f (s), Fitness: %.5f (s), Sorting: %.5f (s)\n", g_progress_per, g_best_fitness, g_avg_crossover_time, g_avg_mutate_time, g_avg_fitness_time, g_avg_sorting_time);
if (complete) break;
}
T best_cell;
// TODO: bad
float best_score = strat.higher_fitness_is_better ? 0.0 : 999999999999999999.9;
for (int i = 0; i < stats.len; i++) {
float score = back(stats[i].best_cell_fitness);
if (strat.higher_fitness_is_better ? score > best_score : score < best_score) {
best_cell = back(stats[i].best_cells);
best_score = score;
}
}
return best_cell;
}
} // namespace genetic