working through compile bugs

This commit is contained in:
2025-08-21 00:41:51 -05:00
parent 3265f045d1
commit 3a901a0a40
3 changed files with 215 additions and 167 deletions

View File

@@ -2,8 +2,7 @@
namespace genetic { namespace genetic {
template <class T> struct ReadonlySpan; template <class T> struct Array;
template <class T> struct Span;
template <class T> struct Stats; template <class T> struct Stats;
template <class T> struct Strategy; template <class T> struct Strategy;
@@ -16,7 +15,8 @@ template <class T> struct Strategy {
// before accessing/locking the work queue again. // before accessing/locking the work queue again.
int num_cells; // Size of the population pool int num_cells; // Size of the population pool
int num_generations; // Number of times (epochs) to run the algorithm int num_generations; // Number of times (epochs) to run the algorithm
bool test_all; // Sets whether or not every cell is tested every generation 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 float test_chance; // Chance to test any given cell's fitness. Relevant only
// if test_all is false. // if test_all is false.
bool enable_crossover; // Cells that score well in the evaluation stage bool enable_crossover; // Cells that score well in the evaluation stage
@@ -25,11 +25,17 @@ template <class T> struct Strategy {
float crossover_mutation_chance; // Chance to mutate a child cell float crossover_mutation_chance; // Chance to mutate a child cell
int crossover_parent_num; // Number of unique high-scoring parents in a int crossover_parent_num; // Number of unique high-scoring parents in a
// crossover call. // crossover call.
int crossover_children_num; // Number of children to expect the user to int crossover_parent_stride; // Number of parents to skip over when moving to
// produce in the crossover function. // the next set of parents. A stride of 1 would
bool enable_mutation; // Cells may be mutated // produce maximum overlap because the set of
// before fitness evaluation // parents would only change by one every
float mutation_chance; // Chance to mutate cells before fitness evaluation // 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; uint64_t rand_seed;
bool higher_fitness_is_better; // Sets whether or not to consider higher bool higher_fitness_is_better; // Sets whether or not to consider higher
// fitness values better or worse. Set this to // fitness values better or worse. Set this to
@@ -38,16 +44,16 @@ template <class T> struct Strategy {
// User defined functions // User defined functions
T (*make_default_cell)(); T (*make_default_cell)();
void (*mutate)(T &cell_to_modify); void (*mutate)(T &cell_to_modify);
void (*crossover)(const Span<T *> parents, const Span<T *> out_children); void (*crossover)(const Array<T *> parents, const Array<T *> out_children);
float (*fitness)(const T &cell); float (*fitness)(const T &cell);
}; };
template <class T> struct Stats { template <class T> struct Stats {
std::vector<T> best_cell; std::vector<T> best_cell;
std::vector<float> average_fitness; std::vector<float> best_cell_fitness;
}; };
template <class T> struct Span { template <class T> struct Array {
T *_data; T *_data;
int len; int len;

View File

@@ -1,9 +1,14 @@
#include "genetic.h" #include <algorithm>
#include "pthread.h" #include <cstdint>
#include <cstdlib>
#include <optional> #include <optional>
#include <variant> #include <variant>
#include <vector> #include <vector>
#include "genetic.h"
#include "pthread.h"
#include "rand.h"
#define NUM_QUEUE_RETRIES 10 #define NUM_QUEUE_RETRIES 10
using namespace std; using namespace std;
@@ -19,23 +24,27 @@ template <class... Ts> overload(Ts...) -> overload<Ts...>;
namespace genetic { namespace genetic {
template <class T> struct CellEntry { template <class T> struct cell_entry {
float score; float score;
T *cell; T *cell;
bool stale; bool stale;
}; };
template <class T> struct CrossoverJob { template <class T> struct crossover_job {
Span<CellEntry<T> *> &parents; Array<cell_entry<T> *> &parents;
Span<CellEntry<T> *> &children_out; Array<cell_entry<T> *> &children_out;
}; };
template <class T> struct FitnessJob { template <class T> struct fitness_job {
CellEntry<T> *cell_entry; cell_entry<T> *cell_entry;
}; };
template <class T> struct WorkQueue { template <class T> struct mutate_job {
variant<CrossoverJob<T>, FitnessJob<T>> *jobs; cell_entry<T> *cell_entry;
};
template <class T> struct work_queue {
variant<crossover_job<T>, fitness_job<T>, mutate_job<T>> *jobs;
int len; int len;
int read_i; int read_i;
int write_i; int write_i;
@@ -49,9 +58,9 @@ template <class T> struct WorkQueue {
pthread_cond_t jobs_available_cond; pthread_cond_t jobs_available_cond;
}; };
template <class T> WorkQueue<T> make_work_queue(int len) { template <class T> work_queue<T> make_work_queue(int len) {
return {.jobs = (variant<FitnessJob<T>, CrossoverJob<T>> *)malloc( return {.jobs = (variant<fitness_job<T>, crossover_job<T>> *)malloc(
sizeof(variant<FitnessJob<T>, CrossoverJob<T>>) * len), sizeof(variant<fitness_job<T>, crossover_job<T>>) * len),
.len = len, .len = len,
.read_i = 0, .read_i = 0,
.write_i = 0, .write_i = 0,
@@ -63,19 +72,19 @@ template <class T> WorkQueue<T> make_work_queue(int len) {
.jobs_available_cond = PTHREAD_COND_INITIALIZER}; .jobs_available_cond = PTHREAD_COND_INITIALIZER};
} }
template <class T> struct JobBatch { template <class T> struct job_batch {
ReadonlySpan<variant<CrossoverJob<T>, FitnessJob<T>>> jobs; Array<variant<crossover_job<T>, fitness_job<T>>> jobs;
bool gen_complete; bool gen_complete;
}; };
template <class T> template <class T>
optional<JobBatch<T>> get_job_batch(WorkQueue<T> &queue, int batch_size, optional<job_batch<T>> get_job_batch(work_queue<T> &queue, int batch_size,
bool *stop_flag) { bool *stop_flag) {
while (true) { while (true) {
for (int i = 0; i < NUM_QUEUE_RETRIES; i++) { for (int i = 0; i < NUM_QUEUE_RETRIES; i++) {
if (queue.read_i < queue.write_i && if (queue.read_i < queue.write_i &&
pthread_mutex_trylock(&queue.data_mutex)) { pthread_mutex_trylock(&queue.data_mutex)) {
JobBatch<T> res; job_batch<T> res;
res.jobs._data = &queue._jobs[queue.read_i]; res.jobs._data = &queue._jobs[queue.read_i];
int span_size = min(batch_size, queue.write_i - queue.read_i); int span_size = min(batch_size, queue.write_i - queue.read_i);
res.jobs.len = span_size; res.jobs.len = span_size;
@@ -94,27 +103,41 @@ optional<JobBatch<T>> get_job_batch(WorkQueue<T> &queue, int batch_size,
} }
} }
template <class T> struct WorkerThreadArgs { template <class T> struct worker_thread_args {
Strategy<T> &strat; Strategy<T> &strat;
WorkQueue<T> &queue; work_queue<T> &queue;
bool *stop_flag; bool *stop_flag;
}; };
template <class T> void do_crossover_job(CrossoverJob<T> cj) {}
template <class T> void *worker(void *args) { template <class T> void *worker(void *args) {
WorkerThreadArgs<T> *work_args = (WorkerThreadArgs<T> *)args; worker_thread_args<T> *work_args = (worker_thread_args<T> *)args;
Strategy<T> &strat = work_args->strat; Strategy<T> &strat = work_args->strat;
WorkQueue<T> &queue = work_args->queue; work_queue<T> &queue = work_args->queue;
bool *stop_flag = work_args->stop_flag; bool *stop_flag = work_args->stop_flag;
auto JobDispatcher = overload{ auto job_dispatcher = overload{
[strat](FitnessJob<T> fj) { [strat](mutate_job<T> mj) {
fj.cell_entry->result_out = strat.fitness(*(fj.cell_entry->cell)); strat.mutate(*mj.cell_entry->cell);
fj.cell_entry->stale = true; mj.cell_entry->stale = true;
}, },
[strat](CrossoverJob<T> cj) { [strat](fitness_job<T> fj) {
strat.crossover(cj.parents, cj.children_out); fj.cell_entry->score = strat.fitness(*fj.cell_entry->cell);
fj.cell_entry->stale = false;
},
[strat](crossover_job<T> cj) {
Array<T *> parent_cells, child_cells;
parent_cells = {(T **)malloc(sizeof(T *) * cj.parents.len),
cj.parents.len};
child_cells = {(T **)malloc(sizeof(T *) * cj.children_out.len),
cj.children_out.len};
for (int i = 0; i < cj.parents.len; i++) {
parent_cells[i] = cj.parents[i].cell;
}
for (int i = 0; i < cj.children_out.len; i++) {
child_cells[i] = cj.children_out[i].cell;
cj.children_out[i].stale = true;
}
strat.crossover(parent_cells, child_cells);
}, },
}; };
@@ -125,7 +148,7 @@ template <class T> void *worker(void *args) {
// Do the actual work // Do the actual work
for (int i = 0; i < batch->jobs.len; i++) { for (int i = 0; i < batch->jobs.len; i++) {
visit(JobDispatcher, batch->jobs[i]); visit(job_dispatcher, batch->jobs[i]);
} }
if (batch->gen_complete) { if (batch->gen_complete) {
@@ -136,20 +159,24 @@ template <class T> void *worker(void *args) {
template <class T> Stats<T> run(Strategy<T> strat) { template <class T> Stats<T> run(Strategy<T> strat) {
Stats<T> stats; Stats<T> stats;
WorkQueue<T> work_queue = make_work_queue<T>(strat.num_cells);
// The work queue is what all the worker threads will checking
// for jobs
work_queue<T> queue = make_work_queue<T>(strat.num_cells);
// The actual cells. Woo!
T cells[strat.num_cells]; T cells[strat.num_cells];
// Using a vector so I can use the make_heap, push_heap, etc. // Using a vector so I can use the make_heap, push_heap, etc.
vector<CellEntry<T>> cell_queue; vector<cell_entry<T>> cell_queue;
for (int i = 0; i < strat.num_cells; i++) { for (int i = 0; i < strat.num_cells; i++) {
cells[i] = strat.make_default_cell(); cells[i] = strat.make_default_cell();
cell_queue.push_back({0, &cells[i], true}); cell_queue.push_back({0, &cells[i], true});
} }
bool stop_flag = false; bool stop_flag = false;
WorkerThreadArgs<T> args = { worker_thread_args<T> args = {
.strat = strat, .queue = work_queue, .stop_flag = &stop_flag}; .strat = strat, .queue = queue, .stop_flag = &stop_flag};
// spawn worker threads // spawn worker threads
pthread_t threads[strat.num_threads]; pthread_t threads[strat.num_threads];
@@ -157,28 +184,95 @@ template <class T> Stats<T> run(Strategy<T> strat) {
pthread_create(&threads[i], NULL, worker<T>, (void *)args); pthread_create(&threads[i], NULL, worker<T>, (void *)args);
} }
for (int i = 0; i < strat.num_generations; i++) { uint64_t rand_state = strat.rand_seed;
// generate fitness jobs
if (strat.test_all) {
} else { for (int i = 0; i < strat.num_generations; i++) {
// Mutate some random cells in the population
for (int i = 0; i < cell_queue.size(); i++) {
if (abs(norm_rand(rand_state)) < strat.mutation_chance) {
queue.jobs[queue.write_i] = mutate_job<T>{&cell_queue[i]};
queue.write_i++;
}
} }
pthread_cond_broadcast(&queue.jobs_available_cond);
// Potential issue here where mutations aren't done computing and fitness
// jobs begin. maybe need to gate this.
// Generate fitness jobs
for (int i = 0; i < cell_queue.size(); i++) {
if (cell_queue[i].stale &&
(strat.test_all || abs(norm_rand(rand_state)) < strat.test_chance)) {
queue.jobs[queue.write_i] = fitness_job<T>{&cell_queue[i]};
queue.write_i++;
}
pthread_cond_broadcast(&queue.jobs_available_cond);
}
queue.done_writing = true;
// wait for fitness jobs to complete // wait for fitness jobs to complete
// sort cells on performance pthread_mutex_lock(&queue.gen_complete_mutex);
// Before going to sleep, do a quick check to see if the fitness jobs are
// already complete.
pthread_mutex_lock(&queue.data_mutex);
bool already_complete = queue.read_i != queue.write_i;
pthread_mutex_unlock(&queue.data_mutex);
if (already_complete) {
pthread_mutex_unlock(&queue.gen_complete_mutex);
} else {
pthread_cond_wait(&queue.gen_complete_cond, &queue.gen_complete_mutex);
}
// Sort cells on performance
std::sort(cell_queue.begin(), cell_queue.end(),
[strat](cell_entry<T> a, cell_entry<T> b) {
return strat.higher_fitness_is_better ? a > b : a < b;
});
printf("Top Score: %f\n", cell_queue[0].score);
if (!strat.enable_crossover)
continue;
// generate crossover jobs // generate crossover jobs
// dear god. forgive me father
queue.write_i = 0;
queue.read_i = 0;
int count = 0;
int n_par = strat.crossover_parent_num;
int n_child = strat.crossover_children_num;
int child_i = cell_queue.size() - 1;
int par_i = 0;
while (child_i - par_i <= n_par + n_child) {
Array<cell_entry<T> *> parents = {
(cell_entry<T> **)malloc(sizeof(cell_entry<T> *) * n_par), n_par};
Array<cell_entry<T> *> children = {
(cell_entry<T> **)malloc(sizeof(cell_entry<T> *) * n_child), n_child};
for (; par_i < par_i + n_par; par_i++) {
parents[i] = cell_queue[par_i];
}
for (; child_i > child_i - n_child; child_i--) {
children[i] = cell_queue[child_i];
}
queue.jobs[queue.write_i] = crossover_job<T>{parents, children};
par_i += strat.crossover_parent_stride;
child_i += strat.crossover_children_stride;
}
} }
// stop worker threads // stop worker threads
stop_flag = true; stop_flag = true;
pthread_cond_broadcast(work_queue.jobs_available_cond); pthread_cond_broadcast(&queue.jobs_available_cond);
for (int i = 0; i < strat.num_threads; i++) { for (int i = 0; i < strat.num_threads; i++) {
pthread_join(threads[i], NULL); pthread_join(threads[i], NULL);
} }
} }
template <class T> T &Span<T>::operator[](int i) { template <class T> T &Array<T>::operator[](int i) {
assert(i >= 0 && i < len);
return _data[i]; return _data[i];
} }

View File

@@ -1,133 +1,81 @@
#include <algorithm>
#include <cassert> #include <cassert>
#include <cstdint>
#include <cstdlib> #include <cstdlib>
#include <iostream> #include "genetic.h"
#include <vector> #include "rand.h"
#define MUTATION_CHANCE 1.0 using namespace genetic;
float norm_rand() { return (float)rand() / RAND_MAX; } const int len = 10;
const float max_float = 9999.9f;
static uint64_t seed = 12;
static float num_mutate_chance = 0.5;
static int num_parents = 2;
static int num_children = 2;
enum class ConstraintType {
PRODUCT = 0,
SUM = 1,
INDEX_EQ = 2,
};
struct Constraint { static int target_sum = 200;
ConstraintType type; static int target_product = 300;
int optional_i;
float value;
};
static std::vector<Constraint> constraints;
struct Cell { Array<float> make_new_arr() {
int n; Array<float> arr = { (float*)malloc(sizeof(float)*len), len };
float *params; for (int i = 0; i < arr.len; i++) {
}; arr[i] = norm_rand(seed) * max_float;
}
Cell make_cell(int num_params) { return arr;
Cell res = {num_params, (float *)malloc(num_params * sizeof(float))};
for (int i = 0; i < num_params; i++) {
res.params[i] = 0;
}
return res;
} }
float get_cell_err(const Cell &a) { void mutate(Array<float> &arr_to_mutate) {
float total_diff = 0; for (int i = 0; i < len; i++) {
for (auto c : constraints) { if (norm_rand(seed) < num_mutate_chance) {
switch (c.type) { arr_to_mutate[i] = norm_rand(seed) * max_float;
case ConstraintType::SUM: { }
float sum = 0;
for (int i = 0; i < a.n; i++) {
sum += a.params[i];
}
total_diff += abs(c.value - sum);
break;
} }
case ConstraintType::PRODUCT: {
float prod = 1;
for (int i = 0; i < a.n; i++) {
prod *= a.params[i];
}
total_diff += abs(c.value - prod);
break;
}
case ConstraintType::INDEX_EQ: {
assert(c.optional_i < a.n);
total_diff += abs(c.value - a.params[c.optional_i]);
break;
}
}
}
return total_diff;
} }
bool operator<(const Cell &a, const Cell &b) { void crossover(const Array<Array<float>*> parents, const Array<Array<float> *> out_children) {
assert(a.n == b.n); for (int i = 0; i < len; i++) {
return get_cell_err(a) < get_cell_err(b); (*out_children._data[0])[i] = i < len/2 ? (*parents._data[0])[i] : (*parents._data[1])[i];
(*out_children._data[1])[i] = i < len/2 ? (*parents._data[1])[i] : (*parents._data[0])[i];
}
} }
void combine_cells(const Cell &a, const Cell &b, Cell *child) { // norm_rand can go negative. fix in genetic.cpp
bool a_first = norm_rand() > 0.5f; // child stride doesn't make sense. Should always skip over child num
for (int i = 0; i < a.n; i++) {
float offset = norm_rand() * 10; float fitness(const Array<float> &cell) {
float roll = norm_rand(); float sum = 0;
if (a_first) { float product = 1;
child->params[i] = (i < a.n / 2 ? a.params[i] : b.params[i]) + for (int i = 0; i < cell.len; i++) {
(roll > 0.5 ? offset : -offset); sum += cell._data[i];
} else { product *= cell._data[i];
child->params[i] = (i < a.n / 2 ? b.params[i] : a.params[i]) +
(roll > 0.5 ? offset : -offset);
} }
} return abs(sum - target_sum) + abs(product - target_product);
float r = norm_rand();
child->params[(int)r * (a.n - 1)] = r * 100.0;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
int num_params, num_cells, num_generations, num_constraints = 0; Strategy<Array<float>> strat {
std::cin >> num_params >> num_cells >> num_generations >> num_constraints; .num_threads = 1,
.batch_size = 1,
.num_cells = 10,
.num_generations = 10,
.test_all = true,
.test_chance = 0.0, // doesn't matter
.enable_crossover = true,
.enable_crossover_mutation = true,
.crossover_mutation_chance = 0.6f,
.crossover_parent_num = 2,
.crossover_parent_stride = 1,
.crossover_children_num = 2,
.enable_mutation = true,
.mutation_chance = 0.8,
.rand_seed = seed,
.higher_fitness_is_better = false,
.make_default_cell=make_new_arr,
.mutate=mutate,
.crossover=crossover,
.fitness=fitness
};
std::cout << num_params << " " << num_cells << " " << num_generations << " " auto res = run(strat);
<< num_constraints << std::endl;
for (int i = 0; i < num_constraints; i++) {
int type_in, optional_i = 0;
float value;
std::cin >> type_in >> value;
ConstraintType type = static_cast<ConstraintType>(type_in);
if (type == ConstraintType::INDEX_EQ) {
std::cin >> optional_i;
}
constraints.push_back({type, optional_i, value});
}
std::vector<Cell> cells;
for (int i = 0; i < num_cells; i++) {
cells.push_back(make_cell(num_params));
}
for (int i = 0; i < num_generations; i++) {
std::sort(cells.begin(), cells.end());
for (int j = 0; j < num_cells / 2; j++) {
combine_cells(cells[j], cells[j + 1], &cells[num_cells / 2 + j]);
}
if (i % 1000 == 0) {
std::cout << i << "\t" << get_cell_err(cells[0]) << std::endl;
}
}
std::cout << "Final Answer: ";
float sum = 0;
float product = 1;
for (int i = 0; i < cells[0].n; i++) {
std::cout << cells[0].params[i] << " ";
sum += cells[0].params[i];
product *= cells[0].params[i];
}
std::cout << std::endl;
std::cout << "Sum: " << sum << std::endl;
std::cout << "Product: " << product << std::endl;
} }