working on new stuff?

shared
Brett 2024-08-28 17:08:59 -04:00
parent 144b3a4ceb
commit bc903e0251
6 changed files with 66 additions and 23 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25) cmake_minimum_required(VERSION 3.25)
project(blt-gp VERSION 0.1.30) project(blt-gp VERSION 0.1.31)
include(CTest) include(CTest)

View File

@ -28,7 +28,7 @@
#include "operations_common.h" #include "operations_common.h"
#include "blt/fs/loader.h" #include "blt/fs/loader.h"
static const unsigned long SEED = std::random_device()(); static const auto SEED_FUNC = [] { return std::random_device()(); };
enum class rice_type_t enum class rice_type_t
{ {
@ -59,11 +59,11 @@ blt::gp::prog_config_t config = blt::gp::prog_config_t()
.set_mutation_chance(0.1) .set_mutation_chance(0.1)
.set_reproduction_chance(0) .set_reproduction_chance(0)
.set_max_generations(50) .set_max_generations(50)
.set_pop_size(5000) .set_pop_size(500)
.set_thread_count(0); .set_thread_count(0);
blt::gp::type_provider type_system; blt::gp::type_provider type_system;
blt::gp::gp_program program{type_system, SEED, config}; blt::gp::gp_program program{type_system, SEED_FUNC, config};
auto lit = blt::gp::operation_t([]() { auto lit = blt::gp::operation_t([]() {
return program.get_random().get_float(-32000.0f, 32000.0f); return program.get_random().get_float(-32000.0f, 32000.0f);
@ -159,6 +159,48 @@ void load_rice_data(std::string_view rice_file_path)
BLT_INFO("Created training set of size %ld, testing set is of size %ld", training_size, testing_cases.size()); BLT_INFO("Created training set of size %ld, testing set is of size %ld", training_size, testing_cases.size());
} }
struct test_results_t
{
blt::size_t cc = 0;
blt::size_t co = 0;
blt::size_t oo = 0;
blt::size_t oc = 0;
blt::size_t hits = 0;
blt::size_t size = 0;
double percent_hit = 0;
};
test_results_t test_individual(blt::gp::individual& i)
{
test_results_t results;
for (auto& testing_case : testing_cases)
{
auto result = i.tree.get_evaluation_value<float>(&testing_case);
switch (testing_case.type)
{
case rice_type_t::Cammeo:
if (result >= 0)
results.cc++; // cammeo cammeo
else if (result < 0)
results.co++; // cammeo osmancik
break;
case rice_type_t::Osmancik:
if (result < 0)
results.oo++; // osmancik osmancik
else if (result >= 0)
results.oc++; // osmancik cammeo
break;
}
}
results.hits = results.cc + results.oo;
results.size = testing_cases.size();
results.percent_hit = static_cast<double>(results.hits) / static_cast<double>(results.size) * 100;
return results;
}
int main(int argc, const char** argv) int main(int argc, const char** argv)
{ {
blt::arg_parse parser; blt::arg_parse parser;

View File

@ -332,11 +332,19 @@ namespace blt::gp
* @param context_size number of arguments which are always present as "context" to the GP system / operators * @param context_size number of arguments which are always present as "context" to the GP system / operators
*/ */
explicit gp_program(type_provider& system, blt::u64 seed): explicit gp_program(type_provider& system, blt::u64 seed):
system(system), seed(seed) system(system), seed_func([seed]{return seed;})
{ create_threads(); } { create_threads(); }
explicit gp_program(type_provider& system, blt::u64 seed, prog_config_t config): explicit gp_program(type_provider& system, blt::u64 seed, prog_config_t config):
system(system), seed(seed), config(config) system(system), seed_func([seed]{return seed;}), config(config)
{ create_threads(); }
explicit gp_program(type_provider& system, std::function<blt::u64()> seed_func):
system(system), seed_func(std::move(seed_func))
{ create_threads(); }
explicit gp_program(type_provider& system, std::function<blt::u64()> seed_func, prog_config_t config):
system(system), seed_func(std::move(seed_func)), config(config)
{ create_threads(); } { create_threads(); }
void create_next_generation() void create_next_generation()
@ -719,7 +727,7 @@ namespace blt::gp
std::atomic_uint64_t current_generation = 0; std::atomic_uint64_t current_generation = 0;
std::atomic_bool fitness_should_exit = false; std::atomic_bool fitness_should_exit = false;
blt::u64 seed; std::function<blt::u64()> seed_func;
prog_config_t config{}; prog_config_t config{};
struct concurrency_storage struct concurrency_storage

View File

@ -177,7 +177,7 @@ namespace blt::gp
public: public:
explicit select_tournament_t(blt::size_t selection_size = 3): selection_size(selection_size) explicit select_tournament_t(blt::size_t selection_size = 3): selection_size(selection_size)
{ {
if (selection_size < 1) if (selection_size == 0)
BLT_ABORT("Unable to select with this size. Must select at least 1 individual!"); BLT_ABORT("Unable to select with this size. Must select at least 1 individual!");
} }

View File

@ -46,7 +46,7 @@ namespace blt::gp
random_t& gp_program::get_random() const random_t& gp_program::get_random() const
{ {
thread_local static blt::gp::random_t random_engine{seed}; thread_local static blt::gp::random_t random_engine{seed_func()};
return random_engine; return random_engine;
} }

View File

@ -60,22 +60,15 @@ namespace blt::gp
tree_t& select_tournament_t::select(gp_program& program, population_t& pop, population_stats&) tree_t& select_tournament_t::select(gp_program& program, population_t& pop, population_stats&)
{ {
blt::u64 best = program.get_random().get_u64(0, pop.get_individuals().size());
auto& first = pop.get_individuals()[program.get_random().get_size_t(0ul, pop.get_individuals().size())]; auto& i_ref = pop.get_individuals();
individual* ind = &first; for (blt::size_t i = 0; i < selection_size; i++)
double best_guy = first.fitness.adjusted_fitness;
for (blt::size_t i = 0; i < selection_size - 1; i++)
{ {
auto& sel = pop.get_individuals()[program.get_random().get_size_t(0ul, pop.get_individuals().size())]; auto sel_point = program.get_random().get_u64(0ul, pop.get_individuals().size());
BLT_TRACE("Selection %ld (of %ld) = %lf, ind %p, first: %p", i, selection_size, sel.fitness.adjusted_fitness, &sel, &first); if (i_ref[sel_point].fitness.adjusted_fitness > i_ref[best].fitness.adjusted_fitness)
if (sel.fitness.adjusted_fitness > best_guy) best = sel_point;
{
best_guy = sel.fitness.adjusted_fitness;
ind = &sel;
} }
} return i_ref[best].tree;
return ind->tree;
} }
tree_t& select_fitness_proportionate_t::select(gp_program& program, population_t& pop, population_stats& stats) tree_t& select_fitness_proportionate_t::select(gp_program& program, population_t& pop, population_stats& stats)