threading next?

thread
Brett 2024-07-11 13:51:14 -04:00
parent d6ebcd935c
commit 63d6e89136
5 changed files with 15 additions and 15 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.59) project(blt-gp VERSION 0.0.60)
include(CTest) include(CTest)

View File

@ -110,7 +110,7 @@ int main()
while (!program.should_terminate()) while (!program.should_terminate())
{ {
program.create_next_generation(blt::gp::select_fitness_proportionate_t{}, blt::gp::select_fitness_proportionate_t{}, blt::gp::select_fitness_proportionate_t{}); program.create_next_generation(blt::gp::select_tournament_t{}, blt::gp::select_tournament_t{}, blt::gp::select_tournament_t{});
program.next_generation(); program.next_generation();
program.evaluate_fitness(); program.evaluate_fitness();
} }
@ -125,6 +125,8 @@ int main()
i.tree.print(program, std::cout); i.tree.print(program, std::cout);
std::cout << "\n"; std::cout << "\n";
} }
BLT_INFO("");
// TODO: make stats helper
return 0; return 0;
} }

View File

@ -58,8 +58,8 @@ namespace blt::gp
{ {
for (blt::size_t i = 0; i < config.elites; i++) for (blt::size_t i = 0; i < config.elites; i++)
{ {
// BLT_INFO("%lf < %lf? // %lf", ind.second.standardized_fitness, values[i].second, ind.second.raw_fitness); // BLT_INFO("%lf >= %lf? // %lf", ind.second.fitness.adjusted_fitness, values[i].second, ind.second.fitness.raw_fitness);
if (ind.second.fitness.adjusted_fitness > values[i].second) if (ind.second.fitness.adjusted_fitness >= values[i].second)
{ {
bool doesnt_contain = true; bool doesnt_contain = true;
for (blt::size_t j = 0; j < config.elites; j++) for (blt::size_t j = 0; j < config.elites; j++)
@ -76,8 +76,7 @@ namespace blt::gp
for (blt::size_t i = 0; i < config.elites; i++) for (blt::size_t i = 0; i < config.elites; i++)
{ {
// BLT_DEBUG("%lf at %ld produces %lf", values[i].second, values[i].first, // BLT_DEBUG("%lf at %ld", values[i].second, values[i].first);
// current_pop.get_individuals()[values[i].first].tree.get_evaluation_value<float>(nullptr));
next_pop.get_individuals().push_back(current_pop.get_individuals()[values[i].first]); next_pop.get_individuals().push_back(current_pop.get_individuals()[values[i].first]);
} }
} }
@ -176,7 +175,7 @@ namespace blt::gp
tree_t& select(gp_program& program, population_t& pop, population_stats& stats) final; tree_t& select(gp_program& program, population_t& pop, population_stats& stats) final;
private: private:
blt::size_t selection_size; const blt::size_t selection_size;
}; };
class select_fitness_proportionate_t : public selection_t class select_fitness_proportionate_t : public selection_t
@ -185,8 +184,6 @@ namespace blt::gp
void pre_process(gp_program& program, population_t& pop, population_stats& stats) final; void pre_process(gp_program& program, population_t& pop, population_stats& stats) final;
tree_t& select(gp_program& program, population_t& pop, population_stats& stats) final; tree_t& select(gp_program& program, population_t& pop, population_stats& stats) final;
private:
std::vector<double> probabilities;
}; };
} }

View File

@ -150,6 +150,7 @@ namespace blt::gp
// these will never be null unless your pop is not initialized / fitness eval was not called! // these will never be null unless your pop is not initialized / fitness eval was not called!
individual* best_individual = nullptr; individual* best_individual = nullptr;
individual* worst_individual = nullptr; individual* worst_individual = nullptr;
std::vector<double> normalized_fitness;
}; };
class population_t class population_t

View File

@ -77,29 +77,29 @@ namespace blt::gp
return ind->tree; return ind->tree;
} }
tree_t& select_fitness_proportionate_t::select(gp_program& program, population_t& pop, population_stats&) tree_t& select_fitness_proportionate_t::select(gp_program& program, population_t& pop, population_stats& stats)
{ {
auto choice = program.get_random().get_double(); auto choice = program.get_random().get_double();
for (const auto& ind : blt::enumerate(pop)) for (const auto& ind : blt::enumerate(pop))
{ {
if (ind.first == 0) if (ind.first == 0 && choice <= stats.normalized_fitness[ind.first])
return ind.second.tree; return ind.second.tree;
if (choice >= probabilities[ind.first] && choice >= probabilities[ind.first - 1]) if (choice > stats.normalized_fitness[ind.first - 1] && choice <= stats.normalized_fitness[ind.first])
return ind.second.tree; return ind.second.tree;
} }
BLT_WARN("Unable to find individual with fitness proportionate. This should not be a possible code path!"); BLT_WARN("Unable to find individual with fitness proportionate. This should not be a possible code path! (%lf)", choice);
return pop.get_individuals()[0].tree; return pop.get_individuals()[0].tree;
//BLT_ABORT("Unable to find individual"); //BLT_ABORT("Unable to find individual");
} }
void select_fitness_proportionate_t::pre_process(gp_program&, population_t& pop, population_stats& stats) void select_fitness_proportionate_t::pre_process(gp_program&, population_t& pop, population_stats& stats)
{ {
probabilities.clear(); stats.normalized_fitness.clear();
double sum_of_prob = 0; double sum_of_prob = 0;
for (auto& ind : pop) for (auto& ind : pop)
{ {
auto prob = (ind.fitness.adjusted_fitness / stats.overall_fitness); auto prob = (ind.fitness.adjusted_fitness / stats.overall_fitness);
probabilities.push_back(sum_of_prob + prob); stats.normalized_fitness.push_back(sum_of_prob + prob);
sum_of_prob += prob; sum_of_prob += prob;
} }
} }