Working single-threaded version

This commit is contained in:
2025-09-07 22:38:04 -05:00
parent bfde57caac
commit bd9820dd68
4 changed files with 67 additions and 25 deletions

View File

@@ -1,5 +1,13 @@
( (
cd bin cd bin
rm main.*
srcs=../src/* srcs=../src/*
cl $srcs -I ../inc/ -Od -std:c++20 -Fe -ZI if [ $# -eq 1 ] && [ "$1" == "release" ]
then
flags="-O2"
else
flags="-Od -ZI"
fi
echo $flags
cl $srcs -I ../inc/ $flags -std:c++20 -Fe
) )

View File

@@ -1,11 +1,13 @@
// raddbg 0.9.21 project file // raddbg 0.9.21 project file
recent_file: path: "src/main.cpp"
recent_file: path: "inc/genetic.h" recent_file: path: "inc/genetic.h"
recent_file: path: "../../../../../Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.42.34433/include/algorithm"
recent_file: path: "../../../../../Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.42.34433/include/xutility"
recent_file: path: "src/main.cpp"
target: target:
{ {
executable: "bin/main.exe" executable: "bin/main.exe"
working_directory: "bin/" working_directory: bin
label: main label: main
enabled: 1 enabled: 1
} }

View File

@@ -7,6 +7,8 @@
#include "sync.h" #include "sync.h"
#include "rand.h" #include "rand.h"
using namespace sync;
namespace genetic { namespace genetic {
template <class T> struct Array; template <class T> struct Array;
@@ -58,6 +60,8 @@ template <class T> struct Strategy {
template<class T> struct Stats { template<class T> struct Stats {
std::vector<T> best_cell; std::vector<T> best_cell;
std::vector<float> best_cell_fitness; std::vector<float> best_cell_fitness;
TimeSpan setup_time;
TimeSpan run_time;
}; };
struct CellTracker { struct CellTracker {
@@ -80,6 +84,11 @@ template <class T> Array<T> make_array(int len) {
} }
template <class T> Stats<T> run(Strategy<T> strat) { template <class T> Stats<T> run(Strategy<T> strat) {
Stats<T> stats;
// ************* SETUP **************
TimeSpan start_setup = now();
// Create cells // Create cells
Array<T> cells = make_array<T>(strat.num_cells); Array<T> cells = make_array<T>(strat.num_cells);
for (int i = 0; i < cells.len; i++) cells[i] = strat.make_default_cell(); for (int i = 0; i < cells.len; i++) cells[i] = strat.make_default_cell();
@@ -88,10 +97,10 @@ template <class T> Stats<T> run(Strategy<T> strat) {
Array<CellTracker> trackers = make_array<CellTracker>(strat.num_cells); Array<CellTracker> trackers = make_array<CellTracker>(strat.num_cells);
for (int i = 0; i < trackers.len; i++) trackers[i] = { .score=0, .cellid=i }; for (int i = 0; i < trackers.len; i++) trackers[i] = { .score=0, .cellid=i };
// Init stat tracker stats.setup_time = now() - start_setup;
Stats<T> stats;
// Run the algorithm // *********** ALGORITHM ************
TimeSpan start_algo = now();
for (int gen = 0; gen < strat.num_generations; gen++) { for (int gen = 0; gen < strat.num_generations; gen++) {
// 1. mutate // 1. mutate
for (int i = 0; i < trackers.len; i++) { for (int i = 0; i < trackers.len; i++) {
@@ -101,24 +110,29 @@ template <class T> Stats<T> run(Strategy<T> strat) {
} }
// 2. crossover // 2. crossover
if (strat.enable_crossover) { if (strat.enable_crossover) {
int parent_end = strat.crossover_parent_num; int npar = strat.crossover_parent_num;
int child_begin = trackers.len-strat.crossover_children_num; int nchild = strat.crossover_children_num;
int parent_end = npar;
int child_begin = trackers.len-nchild;
Array<T*> parents = make_array<T*>(npar);
Array<T*> children = make_array<T*>(nchild);
while (parent_end <= child_begin) { while (parent_end <= child_begin) {
// Get pointers to all the parent cells // Get pointers to all the parent cells
Array<T*> parents = make_array<T*>(strat.crossover_parent_num); for (int i = parent_end-npar; i < parent_end; i++) {
for (int i = parent_end-strat.crossover_parent_num; i < parent_end; i++) { parents[i - (parent_end-npar)] = &cells[trackers[i].cellid];
parents[i] = &cells[trackers[i].cellid];
} }
// Get pointers to all the child cells (these will be overwritten) // Get pointers to all the child cells (these will be overwritten)
Array<T*> children = make_array<T*>(strat.crossover_children_num); for (int i = child_begin; i < child_begin+nchild; i++) {
for (int i = child_begin; i < child_begin+strat.crossover_children_num; i++) { children[i-child_begin] = &cells[trackers[i].cellid];
children[i] = &cells[trackers[i].cellid];
} }
strat.crossover(parents, children); strat.crossover(parents, children);
parent_end += strat.crossover_parent_stride; parent_end += strat.crossover_parent_stride;
child_begin -= strat.crossover_children_num; child_begin -= nchild;
} }
free(parents.data);
free(children.data);
} }
// 3. evaluate // 3. evaluate
if (strat.test_all) { if (strat.test_all) {
@@ -133,12 +147,13 @@ template <class T> Stats<T> run(Strategy<T> strat) {
} }
} }
// 4. sort // 4. sort
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; }); 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; });
printf("Gen: %d, Best Score: %f\n", gen, trackers[0].score); printf("Gen: %d, Best Score: %f\n", gen, trackers[0].score);
stats.best_cell.push_back(cells[trackers[0].cellid]); stats.best_cell.push_back(cells[trackers[0].cellid]);
stats.best_cell_fitness.push_back(trackers[0].score); stats.best_cell_fitness.push_back(trackers[0].score);
} }
stats.run_time = now() - start_algo;
return stats; return stats;
} }

View File

@@ -6,19 +6,19 @@
using namespace genetic; using namespace genetic;
const int len = 10; const int len = 12;
const float max_float = 9999.9f; const float max_float = 999.9f;
static uint64_t seed = 12; static uint64_t seed = 12;
static float num_mutate_chance = 0.5; static float num_mutate_chance = 0.5;
static int num_parents = 2; static int num_parents = 2;
static int num_children = 2; static int num_children = 2;
static int target_sum = 200; static int target_sum = 20000;
static int target_product = 300; static int target_product = 10*target_sum;
Array<float> make_new_arr() { 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++) { for (int i = 0; i < arr.len; i++) {
arr[i] = norm_rand(seed) * max_float; arr[i] = norm_rand(seed) * max_float;
} }
@@ -50,15 +50,16 @@ float fitness(const Array<float> &cell) {
sum += cell.data[i]; sum += cell.data[i];
product *= 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 main(int argc, char **argv) {
int num_gens = 2000;
Strategy<Array<float>> strat { Strategy<Array<float>> strat {
.num_threads = 1, .num_threads = 1,
.batch_size = 1, .batch_size = 1,
.num_cells = 10, .num_cells = 100000,
.num_generations = 10, .num_generations = num_gens,
.test_all = true, .test_all = true,
.test_chance = 0.0, // doesn't matter .test_chance = 0.0, // doesn't matter
.enable_crossover = true, .enable_crossover = true,
@@ -66,7 +67,7 @@ int main(int argc, char **argv) {
.crossover_parent_stride = 1, .crossover_parent_stride = 1,
.crossover_children_num = 2, .crossover_children_num = 2,
.enable_mutation = true, .enable_mutation = true,
.mutation_chance = 0.8, .mutation_chance = 0.7,
.rand_seed = seed, .rand_seed = seed,
.higher_fitness_is_better = false, .higher_fitness_is_better = false,
.make_default_cell=make_new_arr, .make_default_cell=make_new_arr,
@@ -76,4 +77,20 @@ int main(int argc, char **argv) {
}; };
auto res = run(strat); 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);
} }