Running. Only tested single thread version. Stats are looking nice. Needs more validation

This commit is contained in:
2025-09-09 19:57:27 -05:00
parent 1b8801519e
commit 5a048bf469
4 changed files with 105 additions and 44 deletions

View File

@@ -1,8 +1,11 @@
// raddbg 0.9.21 project file // raddbg 0.9.21 project file
recent_file: path: "inc/genetic.h" recent_file: path: "inc/genetic.h"
recent_file: path: "d:/os/obj/amd64fre/minkernel/crts/ucrt/src/appcrt/misc/mt/objfre/amd64/minkernel/crts/ucrt/src/appcrt/misc/invalid_parameter.cpp" recent_file: path: "inc/sync.h"
recent_file: path: "src/main.cpp" recent_file: path: "src/main.cpp"
recent_file: path: "../../../../../Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/include/vector"
recent_file: path: "d:/os/obj/amd64fre/minkernel/crts/ucrt/src/appcrt/misc/mt/objfre/amd64/minkernel/crts/ucrt/src/appcrt/misc/invalid_parameter.cpp"
recent_file: path: "../../../../../Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/include/xmemory"
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/algorithm"
recent_file: path: "../../../../../Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.42.34433/include/xutility" recent_file: path: "../../../../../Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.42.34433/include/xutility"
target: target:

View File

@@ -2,8 +2,8 @@
#include <algorithm> #include <algorithm>
#include <cstdlib> #include <cstdlib>
#include <vector>
#include "util.h"
#include "sync.h" #include "sync.h"
#include "rand.h" #include "rand.h"
@@ -11,7 +11,6 @@ using namespace sync;
namespace genetic { namespace genetic {
template <class T> struct Array;
template <class T> struct Stats; template <class T> struct Stats;
template <class T> struct Strategy; template <class T> struct Strategy;
struct CellTracker; struct CellTracker;
@@ -22,12 +21,15 @@ template <class T> struct Strategy {
// Number of worker threads that will be evaluating cell fitness // Number of worker threads that will be evaluating cell fitness
int num_threads; int num_threads;
float stats_print_period; // 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;
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_per_thread; // Size of the population pool per sim thread
int num_generations; // Number of times (epochs) to run the algorithm
bool test_all; // Sets whether or not every cell's fitness is evaluated every bool test_all; // Sets whether or not every cell's fitness is evaluated every
// generation // 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
@@ -60,21 +62,17 @@ template <class T> struct Strategy {
}; };
template<class T> struct Stats { template<class T> struct Stats {
std::vector<T> best_cell; DynArray<T> best_cells;
std::vector<float> best_cell_fitness; DynArray<float> best_cell_fitness;
int gen; int gen;
bool done; bool done;
TimeSpan start, end; TimeSpan start, end;
TimeSpan total_crossover_time; TimeSpan total_crossover_time;
int total_crossovers; int total_crossovers;
TimeSpan total_mutate_time; TimeSpan total_mutate_time;
int total_mutates; int total_mutates;
TimeSpan total_fitness_time; TimeSpan total_fitness_time;
int total_evaluations; int total_evaluations;
TimeSpan total_sorting_time; TimeSpan total_sorting_time;
int total_sorts; int total_sorts;
@@ -86,26 +84,12 @@ struct CellTracker {
int cellid; int cellid;
}; };
template <class T> struct Array {
T *data;
int len;
T &operator[](int i) { return data[i]; }
};
template <class T> Array<T> make_array(int len) {
return {
.data = (T*)malloc(sizeof(T)*len),
.len = len
};
}
template<class T> template<class T>
struct WorkerThreadArgs { struct WorkerThreadArgs {
Strategy<T> strat; Strategy<T> strat;
Array<T> cells; Array<T> cells;
Array<CellTracker> trackers; Array<CellTracker> trackers;
Stats<T> &stats; Stats<T> *stats;
}; };
template<class T> T* _cellp(Array<T> cells, CellTracker tracker) { return &cells[tracker.cellid]; } template<class T> T* _cellp(Array<T> cells, CellTracker tracker) { return &cells[tracker.cellid]; }
@@ -116,7 +100,7 @@ template <class T> DWORD worker(LPVOID args) {
Strategy<T> strat = worker_args->strat; Strategy<T> strat = worker_args->strat;
Array<T> cells = worker_args->cells; Array<T> cells = worker_args->cells;
Array<CellTracker> trackers = worker_args->trackers; Array<CellTracker> trackers = worker_args->trackers;
Stats<T> &stats = worker_args->stats; Stats<T> &stats = *worker_args->stats;
// Prepare crossover operations as these will be the same every time except // Prepare crossover operations as these will be the same every time except
// for the exact cell pointers // for the exact cell pointers
@@ -192,8 +176,8 @@ template <class T> DWORD worker(LPVOID args) {
stats.total_sorting_time = stats.total_sorting_time + (now() - start); stats.total_sorting_time = stats.total_sorting_time + (now() - start);
stats.total_sorts++; stats.total_sorts++;
stats.best_cell.push_back(cells[trackers[0].cellid]); append(stats.best_cells, cells[trackers[0].cellid]);
stats.best_cell_fitness.push_back(trackers[0].score); append(stats.best_cell_fitness, trackers[0].score);
stats.gen++; stats.gen++;
unlock(stats.m); unlock(stats.m);
} }
@@ -217,8 +201,21 @@ template <class T> T run(Strategy<T> strat) {
for (int i = 0; i < strat.num_threads; i++) { for (int i = 0; i < strat.num_threads; i++) {
stats[i] = { stats[i] = {
.gen=0, .best_cells=make_dynarray<T>(strat.num_generations),
.m=make_mutex() .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<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 }; Array<CellTracker> ttrackers = { &trackers[i*strat.num_cells_per_thread], strat.num_cells_per_thread };
@@ -226,7 +223,7 @@ template <class T> T run(Strategy<T> strat) {
args[i].strat=strat; args[i].strat=strat;
args[i].cells=tcells; args[i].cells=tcells;
args[i].trackers=ttrackers; args[i].trackers=ttrackers;
args[i].stats=stats[i]; args[i].stats=&stats[i];
threads[i] = make_thread(worker<T>, &args[i]); threads[i] = make_thread(worker<T>, &args[i]);
} }
@@ -234,7 +231,7 @@ template <class T> T run(Strategy<T> strat) {
// We are the stats thread // We are the stats thread
bool complete = false; bool complete = false;
while (!complete) { while (!complete) {
sleep(from_s(strat.stats_print_period)); sleep(from_s(strat.stats_print_period_s));
printf("**********************\n"); printf("**********************\n");
float g_avg_crossover_time = 0; float g_avg_crossover_time = 0;
@@ -242,6 +239,7 @@ template <class T> T run(Strategy<T> strat) {
float g_avg_fitness_time = 0; float g_avg_fitness_time = 0;
float g_avg_sorting_time = 0; float g_avg_sorting_time = 0;
float g_progress_per = 0; float g_progress_per = 0;
float g_best_fitness = strat.higher_fitness_is_better ? 0.0 : 999999999999999999.9;
complete = true; complete = true;
@@ -259,13 +257,16 @@ template <class T> T run(Strategy<T> strat) {
float progress_per = static_cast<float>(stats[i].gen) / static_cast<float>(strat.num_generations) * 100; 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_crossover_time += avg_crossover_time;
g_avg_mutate_time += avg_mutate_time; g_avg_mutate_time += avg_mutate_time;
g_avg_fitness_time += avg_fitness_time; g_avg_fitness_time += avg_fitness_time;
g_avg_sorting_time += avg_sorting_time; g_avg_sorting_time += avg_sorting_time;
g_progress_per += progress_per; 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%, Average Crossover Time/Cell %.5f (s), Average Mutate Time/Cell: %.5f (s), Average Fitness Time/Cell: %.5f (s), Average Sorting Time: %.5f (s)\n", i, progress_per, avg_crossover_time, avg_mutate_time, avg_fitness_time, avg_sorting_time); 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); unlock(stats[i].m);
} }
@@ -275,18 +276,18 @@ template <class T> T run(Strategy<T> strat) {
g_avg_sorting_time /= stats.len; g_avg_sorting_time /= stats.len;
g_progress_per /= stats.len; g_progress_per /= stats.len;
printf("OVERALL, Progress %.1f%, Average Crossover Time/Cell %.5f (s), Average Mutate Time/Cell: %.5f (s), Average Fitness Time/Cell: %.5f (s), Average Sorting Time: %.5f (s)\n", g_progress_per, g_avg_crossover_time, g_avg_mutate_time, g_avg_fitness_time, g_avg_sorting_time); 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; if (complete) break;
} }
T best_cell; T best_cell;
// TODO: bad // TODO: bad
float best_score = strat.higher_fitness_is_better ? 999999999999999999.9 : 0.0; float best_score = strat.higher_fitness_is_better ? 0.0 : 999999999999999999.9;
for (int i = 0; i < stats.len; i++) { for (int i = 0; i < stats.len; i++) {
float score = stats[i].best_cell_fitness.back(); float score = back(stats[i].best_cell_fitness);
if (strat.higher_fitness_is_better ? score > best_score : score < best_score) { if (strat.higher_fitness_is_better ? score > best_score : score < best_score) {
best_cell = stats[i].best_cell.back(); best_cell = back(stats[i].best_cells);
best_score = score; best_score = score;
} }
} }

53
inc/util.h Normal file
View File

@@ -0,0 +1,53 @@
#pragma once
#include <cstring>
#define min(A, B) ((A < B) ? (A) : (B))
#define max(A, B) ((A > B) ? (A) : (B))
template <class T> struct Array {
T *data;
int len;
T &operator[](int i) { return data[i]; }
};
template <class T> Array<T> make_array(int len) {
return {
.data=(T*)malloc(sizeof(T)*len),
.len=len
};
}
template <class T> struct DynArray {
T* _data;
int end;
int cap;
T &operator[](int i) { return _data[i]; }
};
template <class T> DynArray<T> make_dynarray(int cap) {
return {
._data=(T*)malloc(sizeof(T)*cap),
.end=0,
.cap=cap
};
}
template <class T> void resize(DynArray<T> &a, int new_cap) {
T* old = a._data;
a._data = (T*)malloc(sizeof(T)*new_cap);
memcpy(a._data, old, min(sizeof(T)*a.end, sizeof(T)*new_cap));
a.cap = new_cap;
free(old);
}
template <class T> void append(DynArray<T> &a, T el) {
if (a.end == a.cap) resize(a, min(1, a.cap*2));
a[a.end++] = el;
}
template <class T> T& back(DynArray<T> &a) { return a._data[a.end-1]; }
template <class T> T& front(DynArray<T> &a) { return a._data[0]; }

View File

@@ -3,6 +3,7 @@
#include <cstdlib> #include <cstdlib>
#include "genetic.h" #include "genetic.h"
#include "rand.h" #include "rand.h"
#include "sync.h"
using namespace genetic; using namespace genetic;
@@ -54,11 +55,11 @@ float fitness(const Array<float> &cell) {
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
int num_gens = 2000; int num_gens = 1000;
Strategy<Array<float>> strat { Strategy<Array<float>> strat {
.num_threads = 1, .num_threads = 1,
.batch_size = 1, .stats_print_period_s = 2,
.num_cells_per_thread = 100000, .num_cells_per_thread = 10000,
.num_generations = num_gens, .num_generations = num_gens,
.test_all = true, .test_all = true,
.test_chance = 0.0, // doesn't matter .test_chance = 0.0, // doesn't matter
@@ -76,7 +77,9 @@ int main(int argc, char **argv) {
.fitness=fitness .fitness=fitness
}; };
TimeSpan start = now();
auto best_cell = run(strat); auto best_cell = run(strat);
TimeSpan runtime = now() - start;
float sum = 0; float sum = 0;
float product = 1; float product = 1;
@@ -90,4 +93,5 @@ int main(int argc, char **argv) {
printf("\n"); printf("\n");
printf("Final Sum: %f\n", sum); printf("Final Sum: %f\n", sum);
printf("Final Product: %f\n", product); printf("Final Product: %f\n", product);
printf("Execution Time %d (min) %f (s)\n", static_cast<int>(sync::to_min(runtime)), fmod(to_s(runtime), 60) );
} }