Compare commits
10 Commits
2451d08710
...
7be8d8bb75
| Author | SHA1 | Date | |
|---|---|---|---|
| 7be8d8bb75 | |||
| ff250af7e8 | |||
| 3a901a0a40 | |||
| 3265f045d1 | |||
| edda3761d1 | |||
| 65c7ea743b | |||
| db2272b768 | |||
| b4d4683f8d | |||
| 6157a80584 | |||
| 05cc2c3f4f |
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
**PTHREADS-BUILT**
|
||||
**obj**
|
||||
**bin**
|
||||
.cache**
|
||||
1
ext/pthreads4w-code
Submodule
1
ext/pthreads4w-code
Submodule
Submodule ext/pthreads4w-code added at 8e467a62a1
@@ -2,29 +2,62 @@
|
||||
|
||||
namespace genetic {
|
||||
|
||||
template <class T> struct Array;
|
||||
template <class T> struct Stats;
|
||||
template <class T> struct Strategy;
|
||||
|
||||
template <class T> Stats<T> run(Strategy<T>);
|
||||
|
||||
template <class T> struct Strategy {
|
||||
// The recommended number of threads is <= number of cores on your pc.
|
||||
// Set this to -1 use the default value (number of cores - 1)
|
||||
int num_threads; // Number of worker threads that will be evaluating cell fitness
|
||||
int num_cells; // Size of the population pool
|
||||
int num_threads; // Number of worker threads that will be evaluating cell
|
||||
// fitness.
|
||||
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
|
||||
float test_chance; // Chance to test any given cell's fitness. Relevant only if test_all is false.
|
||||
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
|
||||
bool enable_crossover_mutation; // Mutations can occur after crossover
|
||||
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_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);
|
||||
void (*mutate)(const T &cell, T *out);
|
||||
void (*crossover)(const T &a, const T &b, T *out);
|
||||
|
||||
float mutation_chance_per_gen;
|
||||
};
|
||||
|
||||
template <class T> struct Stats {
|
||||
std::vector<T> best_cell;
|
||||
std::vector<float> average_fitness;
|
||||
std::vector<float> best_cell_fitness;
|
||||
};
|
||||
|
||||
template <class T> Stats<T> run(Strategy<T>);
|
||||
template <class T> struct Array {
|
||||
T *_data;
|
||||
int len;
|
||||
|
||||
T &operator[](int i);
|
||||
};
|
||||
|
||||
} // namespace genetic
|
||||
|
||||
18
inc/rand.h
Normal file
18
inc/rand.h
Normal file
@@ -0,0 +1,18 @@
|
||||
// TODO: This file needs a serious audit
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
constexpr uint64_t half_max = UINT64_MAX / 2;
|
||||
|
||||
// From https://en.wikipedia.org/wiki/Xorshift
|
||||
inline void xorshift64(uint64_t &state) {
|
||||
state ^= state << 13;
|
||||
state ^= state >> 7;
|
||||
state ^= state << 17;
|
||||
}
|
||||
|
||||
// returns a random value between -1 and 1. modifies seed
|
||||
inline float norm_rand(uint64_t &state) {
|
||||
xorshift64(state);
|
||||
return (state - half_max) / half_max;
|
||||
}
|
||||
155
inc/sync.h
Normal file
155
inc/sync.h
Normal file
@@ -0,0 +1,155 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "windows.h"
|
||||
#endif
|
||||
|
||||
namespace sync {
|
||||
|
||||
#ifdef _WIN32
|
||||
typedef CRITICAL_SECTION Mutex;
|
||||
typedef CONDITION_VARIABLE ConditionVar;
|
||||
typedef HANDLE Semaphore;
|
||||
typedef HANDLE Thread;
|
||||
typedef DWORD TimeSpan;
|
||||
typedef DWORD WINAPI (*ThreadFunc)(_In_ LPVOID lpParameter);
|
||||
typedef LPVOID ThreadArg
|
||||
const TimeSpan infinite_ts = INFINITE;
|
||||
#endif
|
||||
|
||||
Thread make_thread(ThreadFunc t);
|
||||
void join(Thread t);
|
||||
|
||||
Mutex make_mutex();
|
||||
void lock(Mutex &m);
|
||||
bool trylock(Mutex &m);
|
||||
void unlock(Mutex &m);
|
||||
void dispose(Mutex &m);
|
||||
|
||||
ConditionVar make_condition_var();
|
||||
void wait(ConditionVar &c, Mutex &m, TimeSpan ts);
|
||||
void wake_one(ConditionVar &c);
|
||||
void wake_all(ConditionVar &c);
|
||||
void dispose(ConditionVar &c);
|
||||
|
||||
Semaphore make_semaphore(int initial, int max);
|
||||
void wait(Semaphore &s);
|
||||
void post(Semaphore &s);
|
||||
void dispose(Semaphore &s);
|
||||
|
||||
TimeSpan from_ms(double milliseconds);
|
||||
TimeSpan from_s(double seconds);
|
||||
TimeSpan from_min(double minutes);
|
||||
TimeSpan from_hours(double hours);
|
||||
|
||||
double to_ms(TimeSpan &sp);
|
||||
double to_s(TimeSpan &sp);
|
||||
double to_min(TimeSpan &sp);
|
||||
double to_hours(TimeSpan &sp);
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
Thread make_thread(ThreadFunc f, ThreadArg a) {
|
||||
DWORD tid;
|
||||
return CreateThread(NULL, 0, t, a, 0, &tid);
|
||||
}
|
||||
|
||||
void join(Thread t) {
|
||||
WaitForSingleObject(t, infinite_ts);
|
||||
}
|
||||
|
||||
Mutex make_mutex() {
|
||||
Mutex m;
|
||||
InitializeCriticalSection(&m);
|
||||
return m;
|
||||
}
|
||||
|
||||
void lock(Mutex &m) {
|
||||
EnterCriticalSection(&m);
|
||||
}
|
||||
|
||||
bool trylock(Mutex &m) {
|
||||
return TryEnterCriticalSection(&m);
|
||||
}
|
||||
|
||||
void unlock(Mutex &m) {
|
||||
LeaveCriticalSection(&m);
|
||||
}
|
||||
|
||||
void dispose(Mutex &m) {
|
||||
DeleteCriticalSection(&m);
|
||||
}
|
||||
|
||||
ConditionVar make_condition_var() {
|
||||
ConditionVar c;
|
||||
InitializeConditionVariable(&c);
|
||||
return c;
|
||||
}
|
||||
|
||||
void wait(ConditionVar &c, Mutex &m, TimeSpan ts) {
|
||||
SleepConditionVariable(&c, &m, ts);
|
||||
}
|
||||
|
||||
void wake_one(ConditionVar &c) {
|
||||
WakeConditionVariable(&c);
|
||||
}
|
||||
|
||||
void wake_all(ConditionVar &c) {
|
||||
WakeAllConditionVariable(&c);
|
||||
}
|
||||
|
||||
void dispose(ConditionVar &c) {
|
||||
return; // Windows doesn't have a delete condition variable func
|
||||
}
|
||||
|
||||
Semaphore make_semaphore(int initial, int max) {
|
||||
return CreateSemaphoreA(NULL, (long)initial, (long)max, NULL);
|
||||
}
|
||||
|
||||
void wait(Semaphore &s) {
|
||||
WaitForSingleObject(s, infinite_ts);
|
||||
}
|
||||
|
||||
void post(Semaphore &s) {
|
||||
ReleaseSemaphore(s);
|
||||
}
|
||||
|
||||
void dispose(Semaphore &s) {
|
||||
CloseHandle(s);
|
||||
}
|
||||
|
||||
TimeSpan from_ms(double milliseconds) {
|
||||
return static_cast<TimeSpan>(milliseconds);
|
||||
}
|
||||
|
||||
TimeSpan from_s(double seconds) {
|
||||
return static_cast<TimeSpan>(seconds*1000.0);
|
||||
}
|
||||
|
||||
TimeSpan from_min(double minutes) {
|
||||
return static_cast<TimeSpan>(minutes*60.0*1000.0);
|
||||
}
|
||||
|
||||
TimeSpan from_hours(double hours) {
|
||||
return static_cast<TimeSpan>(hours*60.0*60.0*1000.0);
|
||||
}
|
||||
|
||||
double to_ms(TimeSpan &sp) {
|
||||
return static_cast<double>(sp);
|
||||
}
|
||||
|
||||
double to_s(TimeSpan &sp) {
|
||||
return static_cast<double>(sp)/1000.0;
|
||||
}
|
||||
|
||||
double to_min(TimeSpan &sp) {
|
||||
return static_cast<double>(sp)/(1000.0*60.0);
|
||||
}
|
||||
|
||||
double to_hours(TimeSpan &sp) {
|
||||
return static_cast<double>(sp)/(1000.0*60.0*60.0);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace sync
|
||||
64
makefile
64
makefile
@@ -1,13 +1,61 @@
|
||||
src_files = $(find src -iname "*.cpp")
|
||||
obj_files = $(src_files:%.cpp=%.o)
|
||||
src_files = $(shell find src -iname "*.cpp")
|
||||
obj_files = $(src_files:src/%.cpp=obj/%.o)
|
||||
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
CCFLAGS += -D WIN32 -Iext/PTHREADS-BUILT/include -std=c++20
|
||||
PTHREADLIB = ext/PTHREADS-BUILT/lib/pthreadVCE3.lib
|
||||
ifeq ($(PROCESSOR_ARCHITEW6432),AMD64)
|
||||
CCFLAGS += -D AMD64
|
||||
else
|
||||
ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
|
||||
CCFLAGS += -D AMD64
|
||||
endif
|
||||
ifeq ($(PROCESSOR_ARCHITECTURE),x86)
|
||||
CCFLAGS += -D IA32
|
||||
endif
|
||||
endif
|
||||
else
|
||||
UNAME_S := $(shell uname -s)
|
||||
ifeq ($(UNAME_S),Linux)
|
||||
CCFLAGS += -D LINUX
|
||||
endif
|
||||
ifeq ($(UNAME_S),Darwin)
|
||||
CCFLAGS += -D OSX
|
||||
endif
|
||||
UNAME_P := $(shell uname -p)
|
||||
ifeq ($(UNAME_P),x86_64)
|
||||
CCFLAGS += -D AMD64
|
||||
endif
|
||||
ifneq ($(filter %86,$(UNAME_P)),)
|
||||
CCFLAGS += -D IA32
|
||||
endif
|
||||
ifneq ($(filter arm%,$(UNAME_P)),)
|
||||
CCFLAGS += -D ARM
|
||||
endif
|
||||
endif
|
||||
|
||||
debug: OPTIMIZATION_FLAG = -g
|
||||
release: OPTIMIZATION_FLAG = -O3
|
||||
|
||||
release: all export_comp_db
|
||||
|
||||
debug: all export_comp_db
|
||||
|
||||
all: $(obj_files)
|
||||
echo $(obj_files)
|
||||
echo $(src_files)
|
||||
g++ -o main $^
|
||||
@ mkdir -p bin
|
||||
g++ -I inc/ $^ $(PTHREADLIB) -o bin/main $(OPTIMIZATION_FLAG) $(CCFLAGS)
|
||||
|
||||
%.o: %.cpp
|
||||
g++ -o $@ $<
|
||||
obj/%.o: src/%.cpp
|
||||
@ mkdir -p obj
|
||||
g++ -I inc/ -c $< -o $@ $(OPTIMIZATION_FLAG) $(CCFLAGS)
|
||||
|
||||
export_comp_db:
|
||||
echo [ > compile_commands.json
|
||||
make debug -B --dry-run > temp
|
||||
awk '/g\+\+.*\.cpp/ { f="compile_commands.json"; printf "\t\{\n\t\t\"directory\": \"%s\",\n\t\t\"command\": \"%s\",\n\t\t\"file\": \"%s\"\n\t\},\n", ENVIRON["PWD"], $$0, $$5 >> f }' temp
|
||||
echo ] >> compile_commands.json
|
||||
rm temp
|
||||
|
||||
clean:
|
||||
rm -f *.o *.exe
|
||||
rm -f obj/*.o bin/*.exe
|
||||
|
||||
285
src/genetic.cpp
285
src/genetic.cpp
@@ -1,42 +1,279 @@
|
||||
#include "genetic.h"
|
||||
#include <queue>
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <optional>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "genetic.h"
|
||||
#include "pthread.h"
|
||||
#include "rand.h"
|
||||
|
||||
#define NUM_QUEUE_RETRIES 10
|
||||
|
||||
using namespace std;
|
||||
|
||||
// std::visit/std::variant overload pattern
|
||||
// See:
|
||||
// https://www.modernescpp.com/index.php/visiting-a-std-variant-with-the-overload-pattern/
|
||||
// You don't have to understand this, just use it :)
|
||||
template <typename... Ts> struct overload : Ts... {
|
||||
using Ts::operator()...;
|
||||
};
|
||||
template <class... Ts> overload(Ts...) -> overload<Ts...>;
|
||||
|
||||
namespace genetic {
|
||||
|
||||
template <class T> struct CellEntry {
|
||||
template <class T> struct cell_entry {
|
||||
float score;
|
||||
T cell;
|
||||
T *cell;
|
||||
bool stale;
|
||||
};
|
||||
|
||||
template <class T> struct WorkEntry {
|
||||
const std::vector<CellEntry<T>> &cur;
|
||||
std::vector<CellEntry<T>> &next;
|
||||
int cur_i;
|
||||
template <class T> struct crossover_job {
|
||||
Array<cell_entry<T> *> &parents;
|
||||
Array<cell_entry<T> *> &children_out;
|
||||
};
|
||||
|
||||
// Definitions
|
||||
template <class T> Stats<T> run(Strategy<T> strat) {
|
||||
Stats<T> stats;
|
||||
template <class T> struct fitness_job {
|
||||
cell_entry<T> *cell_entry;
|
||||
};
|
||||
|
||||
std::queue<WorkEntry<T>> fitness_queue;
|
||||
std::vector<CellEntry<T>> cells_a, cells_b;
|
||||
for (int i = 0; i < strat.num_cells; i++) {
|
||||
T cell = strat.make_default_cell();
|
||||
cells_a.push_back({0, cell, true});
|
||||
cells_b.push_back({0, cell, true});
|
||||
}
|
||||
template <class T> struct mutate_job {
|
||||
cell_entry<T> *cell_entry;
|
||||
};
|
||||
|
||||
std::vector<CellEntry<T>> &cur_cells = cells_a;
|
||||
std::vector<CellEntry<T>> &next_cells = cells_b;
|
||||
template <class T> struct work_queue {
|
||||
variant<crossover_job<T>, fitness_job<T>, mutate_job<T>> *jobs;
|
||||
int len;
|
||||
int read_i;
|
||||
int write_i;
|
||||
bool done_writing;
|
||||
|
||||
for (int i = 0; i < strat.num_generations; i++) {
|
||||
pthread_mutex_t data_mutex;
|
||||
pthread_mutex_t gen_complete_mutex;
|
||||
pthread_mutex_t jobs_available_mutex;
|
||||
|
||||
cur_cells = cur_cells == cells_a ? cells_b : cells_a;
|
||||
next_cells = cur_cells == cells_a ? cells_b : cells_a;
|
||||
pthread_cond_t gen_complete_cond;
|
||||
pthread_cond_t jobs_available_cond;
|
||||
};
|
||||
|
||||
template <class T> work_queue<T> make_work_queue(int len) {
|
||||
return {.jobs = (variant<fitness_job<T>, crossover_job<T>> *)malloc(
|
||||
sizeof(variant<fitness_job<T>, crossover_job<T>>) * len),
|
||||
.len = len,
|
||||
.read_i = 0,
|
||||
.write_i = 0,
|
||||
.done_writing = false,
|
||||
.data_mutex = PTHREAD_MUTEX_INITIALIZER,
|
||||
.gen_complete_mutex = PTHREAD_MUTEX_INITIALIZER,
|
||||
.jobs_available_mutex = PTHREAD_MUTEX_INITIALIZER,
|
||||
.gen_complete_cond = PTHREAD_COND_INITIALIZER,
|
||||
.jobs_available_cond = PTHREAD_COND_INITIALIZER};
|
||||
}
|
||||
|
||||
template <class T> struct job_batch {
|
||||
Array<variant<crossover_job<T>, fitness_job<T>>> jobs;
|
||||
bool gen_complete;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
optional<job_batch<T>> get_job_batch(work_queue<T> &queue, int batch_size,
|
||||
bool *stop_flag) {
|
||||
while (true) {
|
||||
for (int i = 0; i < NUM_QUEUE_RETRIES; i++) {
|
||||
if (queue.read_i < queue.write_i &&
|
||||
pthread_mutex_trylock(&queue.data_mutex)) {
|
||||
job_batch<T> res;
|
||||
res.jobs._data = &queue._jobs[queue.read_i];
|
||||
int span_size = min(batch_size, queue.write_i - queue.read_i);
|
||||
res.jobs.len = span_size;
|
||||
|
||||
queue.read_i += span_size;
|
||||
res.gen_complete = queue.done_writing && queue.read_i == queue.write_i;
|
||||
|
||||
pthread_mutex_unlock(&queue.data_mutex);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
pthread_mutex_lock(&queue.jobs_available_mutex);
|
||||
pthread_cond_wait(queue.jobs_available_cond, &queue.jobs_available_mutex);
|
||||
if (stop_flag)
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
template <class T> struct worker_thread_args {
|
||||
Strategy<T> &strat;
|
||||
work_queue<T> &queue;
|
||||
bool *stop_flag;
|
||||
};
|
||||
|
||||
template <class T> void *worker(void *args) {
|
||||
worker_thread_args<T> *work_args = (worker_thread_args<T> *)args;
|
||||
Strategy<T> &strat = work_args->strat;
|
||||
work_queue<T> &queue = work_args->queue;
|
||||
bool *stop_flag = work_args->stop_flag;
|
||||
|
||||
auto job_dispatcher = overload{
|
||||
[strat](mutate_job<T> mj) {
|
||||
strat.mutate(*mj.cell_entry->cell);
|
||||
mj.cell_entry->stale = true;
|
||||
},
|
||||
[strat](fitness_job<T> fj) {
|
||||
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);
|
||||
},
|
||||
};
|
||||
|
||||
while (true) {
|
||||
auto batch = get_job_batch(queue, strat.batch_size, stop_flag);
|
||||
if (!batch || *stop_flag)
|
||||
return NULL;
|
||||
|
||||
// Do the actual work
|
||||
for (int i = 0; i < batch->jobs.len; i++) {
|
||||
visit(job_dispatcher, batch->jobs[i]);
|
||||
}
|
||||
|
||||
if (batch->gen_complete) {
|
||||
pthread_cond_signal(&queue.gen_complete_cond, &queue.gen_complete_mutex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <class T> Stats<T> run(Strategy<T> strat) {
|
||||
Stats<T> stats;
|
||||
|
||||
// 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];
|
||||
|
||||
// Using a vector so I can use the make_heap, push_heap, etc.
|
||||
vector<cell_entry<T>> cell_queue;
|
||||
for (int i = 0; i < strat.num_cells; i++) {
|
||||
cells[i] = strat.make_default_cell();
|
||||
cell_queue.push_back({0, &cells[i], true});
|
||||
}
|
||||
|
||||
bool stop_flag = false;
|
||||
worker_thread_args<T> args = {
|
||||
.strat = strat, .queue = queue, .stop_flag = &stop_flag};
|
||||
|
||||
// spawn worker threads
|
||||
pthread_t threads[strat.num_threads];
|
||||
for (int i = 0; i < strat.num_threads; i++) {
|
||||
pthread_create(&threads[i], NULL, worker<T>, (void *)args);
|
||||
}
|
||||
|
||||
uint64_t rand_state = strat.rand_seed;
|
||||
|
||||
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
|
||||
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
|
||||
// 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_flag = true;
|
||||
pthread_cond_broadcast(&queue.jobs_available_cond);
|
||||
for (int i = 0; i < strat.num_threads; i++) {
|
||||
pthread_join(threads[i], NULL);
|
||||
}
|
||||
}
|
||||
|
||||
template <class T> T &Array<T>::operator[](int i) {
|
||||
return _data[i];
|
||||
}
|
||||
|
||||
} // namespace genetic
|
||||
|
||||
174
src/main.cpp
174
src/main.cpp
@@ -1,133 +1,81 @@
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "genetic.h"
|
||||
#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 {
|
||||
ConstraintType type;
|
||||
int optional_i;
|
||||
float value;
|
||||
};
|
||||
static std::vector<Constraint> constraints;
|
||||
static int target_sum = 200;
|
||||
static int target_product = 300;
|
||||
|
||||
struct Cell {
|
||||
int n;
|
||||
float *params;
|
||||
};
|
||||
|
||||
Cell make_cell(int num_params) {
|
||||
Cell res = {num_params, (float *)malloc(num_params * sizeof(float))};
|
||||
for (int i = 0; i < num_params; i++) {
|
||||
res.params[i] = 0;
|
||||
}
|
||||
return res;
|
||||
Array<float> make_new_arr() {
|
||||
Array<float> arr = { (float*)malloc(sizeof(float)*len), len };
|
||||
for (int i = 0; i < arr.len; i++) {
|
||||
arr[i] = norm_rand(seed) * max_float;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
float get_cell_err(const Cell &a) {
|
||||
float total_diff = 0;
|
||||
for (auto c : constraints) {
|
||||
switch (c.type) {
|
||||
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;
|
||||
void mutate(Array<float> &arr_to_mutate) {
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (norm_rand(seed) < num_mutate_chance) {
|
||||
arr_to_mutate[i] = norm_rand(seed) * max_float;
|
||||
}
|
||||
}
|
||||
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) {
|
||||
assert(a.n == b.n);
|
||||
return get_cell_err(a) < get_cell_err(b);
|
||||
void crossover(const Array<Array<float>*> parents, const Array<Array<float> *> out_children) {
|
||||
for (int i = 0; i < len; i++) {
|
||||
(*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) {
|
||||
bool a_first = norm_rand() > 0.5f;
|
||||
for (int i = 0; i < a.n; i++) {
|
||||
float offset = norm_rand() * 10;
|
||||
float roll = norm_rand();
|
||||
if (a_first) {
|
||||
child->params[i] = (i < a.n / 2 ? a.params[i] : b.params[i]) +
|
||||
(roll > 0.5 ? offset : -offset);
|
||||
} else {
|
||||
child->params[i] = (i < a.n / 2 ? b.params[i] : a.params[i]) +
|
||||
(roll > 0.5 ? offset : -offset);
|
||||
// norm_rand can go negative. fix in genetic.cpp
|
||||
// child stride doesn't make sense. Should always skip over child num
|
||||
|
||||
float fitness(const Array<float> &cell) {
|
||||
float sum = 0;
|
||||
float product = 1;
|
||||
for (int i = 0; i < cell.len; i++) {
|
||||
sum += cell._data[i];
|
||||
product *= cell._data[i];
|
||||
}
|
||||
}
|
||||
float r = norm_rand();
|
||||
child->params[(int)r * (a.n - 1)] = r * 100.0;
|
||||
return abs(sum - target_sum) + abs(product - target_product);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int num_params, num_cells, num_generations, num_constraints = 0;
|
||||
std::cin >> num_params >> num_cells >> num_generations >> num_constraints;
|
||||
Strategy<Array<float>> strat {
|
||||
.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 << " "
|
||||
<< 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;
|
||||
auto res = run(strat);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user