more progress on drafting the worker thread model, get job batch func, etc...

This commit is contained in:
2025-08-15 16:09:33 -05:00
parent 65c7ea743b
commit edda3761d1
2 changed files with 166 additions and 112 deletions

View File

@@ -1,15 +1,19 @@
#include <vector>
#include <span>
namespace genetic {
template <class T> struct ReadonlySpan;
template <class T> struct Span;
template <class T> struct Stats;
template <class T> struct Strategy;
template <class T> Stats<T> run(Strategy<T>);
template <class T> struct Strategy {
int num_threads; // Number of worker threads that will be evaluating cell
// fitness.
int num_retries; // Number of times worker threads will try to grab work pool
// lock before sleeping
int batch_size; // Number of cells a worker thread tries to evaluate in a row
// before locking the pool again.
int batch_size; // Number of cells a worker thread tries to work on in a row
// before accessing/locking the work queue again.
int num_cells; // Size of the population pool
int num_generations; // Number of times (epochs) to run the algorithm
bool test_all; // Sets whether or not every cell is tested every generation
@@ -21,14 +25,17 @@ template <class T> struct Strategy {
float crossover_mutation_chance; // Chance to mutate a child cell
int crossover_parent_num; // Number of unique high-scoring parents in a
// crossover call.
int crossover_children_num; // Number of children produced in a crossover
bool enable_mutation; // Cells may be mutated before fitness evaluation
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 to mutate cells before fitness evaluation
// User defined functions
T (*make_default_cell)();
void (*mutate)(T &cell);
void (*crossover)(const std::span<T> &parents, std::span<T> &out_children);
void (*mutate)(T &cell_to_modify);
void (*crossover)(const ReadonlySpan<T> &parents,
const Span<T> &out_children);
float (*fitness)(const T &cell);
};
@@ -37,6 +44,18 @@ template <class T> struct Stats {
std::vector<float> average_fitness;
};
template <class T> Stats<T> run(Strategy<T>);
template <class T> struct ReadonlySpan {
T *_data;
int len;
const T &operator[](int i);
};
template <class T> struct Span {
T *_data;
int len;
T &operator[](int i);
};
} // namespace genetic