change default function to be more in line with koza's description of a gp

thread
Brett 2024-07-15 18:18:13 -04:00
parent 2081dd3e5f
commit 34a3343a89
3 changed files with 47 additions and 39 deletions

View File

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

View File

@ -283,7 +283,7 @@ namespace blt::gp
* NOTE: 0 is considered the best, in terms of standardized fitness * NOTE: 0 is considered the best, in terms of standardized fitness
*/ */
template<typename FitnessFunc> template<typename FitnessFunc>
void generate_population(type_id root_type, FitnessFunc& fitness_function) void generate_population(type_id root_type, FitnessFunc& fitness_function, bool eval_fitness_now = true)
{ {
current_pop = config.pop_initializer.get().generate( current_pop = config.pop_initializer.get().generate(
{*this, root_type, config.population_size, config.initial_min_tree_size, config.initial_max_tree_size}); {*this, root_type, config.population_size, config.initial_min_tree_size, config.initial_max_tree_size});
@ -351,6 +351,7 @@ namespace blt::gp
}); });
thread_helper.thread_function_condition.notify_all(); thread_helper.thread_function_condition.notify_all();
} }
if (eval_fitness_now)
evaluate_fitness_internal(); evaluate_fitness_internal();
} }

View File

@ -74,7 +74,7 @@ namespace blt::gp
}; };
template<typename Crossover, typename Mutation, typename Reproduction> template<typename Crossover, typename Mutation, typename Reproduction>
constexpr inline auto default_next_pop_creator = []( constexpr inline auto proportionate_next_pop_creator = [](
const selector_args& args, Crossover crossover_selection, Mutation mutation_selection, Reproduction reproduction_selection) { const selector_args& args, Crossover crossover_selection, Mutation mutation_selection, Reproduction reproduction_selection) {
auto& [program, next_pop, current_pop, current_stats, config, random] = args; auto& [program, next_pop, current_pop, current_stats, config, random] = args;
@ -132,7 +132,7 @@ namespace blt::gp
}; };
template<typename Crossover, typename Mutation, typename Reproduction> template<typename Crossover, typename Mutation, typename Reproduction>
constexpr inline auto non_deterministic_next_pop_creator = []( constexpr inline auto default_next_pop_creator = [](
const blt::gp::selector_args& args, Crossover crossover_selection, Mutation mutation_selection, Reproduction reproduction_selection) { const blt::gp::selector_args& args, Crossover crossover_selection, Mutation mutation_selection, Reproduction reproduction_selection) {
auto& [program, next_pop, current_pop, current_stats, config, random] = args; auto& [program, next_pop, current_pop, current_stats, config, random] = args;
@ -140,6 +140,9 @@ namespace blt::gp
while (next_pop.get_individuals().size() < config.population_size) while (next_pop.get_individuals().size() < config.population_size)
{ {
int sel = random.get_i32(0, 3);
switch (sel){
case 0:
// everyone gets a chance once per loop. // everyone gets a chance once per loop.
if (random.choice(config.crossover_chance)) if (random.choice(config.crossover_chance))
{ {
@ -158,22 +161,26 @@ namespace blt::gp
next_pop.get_individuals().emplace_back(std::move(results->child2)); next_pop.get_individuals().emplace_back(std::move(results->child2));
} }
} }
if (next_pop.get_individuals().size() >= config.population_size)
break; break;
case 1:
if (random.choice(config.mutation_chance)) if (random.choice(config.mutation_chance))
{ {
// mutation // mutation
auto& p = mutation_selection.select(program, current_pop, current_stats); auto& p = mutation_selection.select(program, current_pop, current_stats);
next_pop.get_individuals().emplace_back(std::move(config.mutator.get().apply(program, p))); next_pop.get_individuals().emplace_back(std::move(config.mutator.get().apply(program, p)));
} }
if (next_pop.get_individuals().size() >= config.population_size)
break; break;
case 2:
if (config.reproduction_chance > 0 && random.choice(config.reproduction_chance)) if (config.reproduction_chance > 0 && random.choice(config.reproduction_chance))
{ {
// reproduction // reproduction
auto& p = reproduction_selection.select(program, current_pop, current_stats); auto& p = reproduction_selection.select(program, current_pop, current_stats);
next_pop.get_individuals().push_back(individual{p}); next_pop.get_individuals().push_back(individual{p});
} }
break;
default:
BLT_ABORT("This is not possible!");
}
} }
}; };