fix stats

main
Brett 2025-07-15 14:07:32 -04:00
parent 2e09696a67
commit 665e019bad
4 changed files with 58 additions and 47 deletions

View File

@ -374,7 +374,11 @@ namespace blt::gp
{ {
current_generation = 0; current_generation = 0;
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
}); });
next_pop = population_t(current_pop); next_pop = population_t(current_pop);
BLT_ASSERT_MSG(current_pop.get_individuals().size() == config.population_size, BLT_ASSERT_MSG(current_pop.get_individuals().size() == config.population_size,
@ -393,7 +397,11 @@ namespace blt::gp
void generate_initial_population(const type_id root_type) void generate_initial_population(const type_id root_type)
{ {
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
}); });
next_pop = population_t(current_pop); next_pop = population_t(current_pop);
BLT_ASSERT_MSG(current_pop.get_individuals().size() == config.population_size, BLT_ASSERT_MSG(current_pop.get_individuals().size() == config.population_size,
@ -753,25 +761,13 @@ namespace blt::gp
storage = std::move(op); storage = std::move(op);
} }
template <blt::size_t size> template <size_t size>
std::array<blt::size_t, size> get_best_indexes() std::array<size_t, size> get_best_indexes() const
{ {
std::array<blt::size_t, size> arr; std::array<size_t, size> arr;
tracked_vector<std::pair<blt::size_t, double>> values; for (auto [i, a] : enumerate(arr))
values.reserve(current_pop.get_individuals().size()); a = std::min(i, config.population_size);
for (const auto& [index, value] : blt::enumerate(current_pop.get_individuals()))
values.emplace_back(index, value.fitness.adjusted_fitness);
std::sort(values.begin(), values.end(), [](const auto& a, const auto& b) {
return a.second > b.second;
});
for (blt::size_t i = 0; i < std::min(size, config.population_size); i++)
arr[i] = values[i].first;
for (blt::size_t i = std::min(size, config.population_size); i < size; i++)
arr[i] = 0;
return arr; return arr;
} }
@ -810,10 +806,6 @@ namespace blt::gp
current_stats.normalized_fitness.push_back(sum_of_prob + prob); current_stats.normalized_fitness.push_back(sum_of_prob + prob);
sum_of_prob += prob; sum_of_prob += prob;
} }
std::sort(current_pop.begin(), current_pop.end(), [](const auto& a, const auto& b)
{
return a.fitness.adjusted_fitness > b.fitness.adjusted_fitness;
});
thread_helper.evaluation_left = 0; thread_helper.evaluation_left = 0;
} }
}; };
@ -840,14 +832,6 @@ namespace blt::gp
perform_fitness_function(begin, end, fitness_function); perform_fitness_function(begin, end, fitness_function);
} }
thread_helper.barrier.wait(); thread_helper.barrier.wait();
if (thread_id == 0)
{
std::sort(current_pop.begin(), current_pop.end(), [](const auto& a, const auto& b)
{
return a.fitness.adjusted_fitness > b.fitness.adjusted_fitness;
});
}
thread_helper.barrier.wait();
} }
}; };
} }
@ -870,15 +854,15 @@ namespace blt::gp
fitness_function(ind.tree, ind.fitness, i); fitness_function(ind.tree, ind.fitness, i);
} }
auto old_best = current_stats.best_fitness.load(std::memory_order_relaxed); // auto old_best = current_stats.best_fitness.load(std::memory_order_relaxed);
while (ind.fitness.adjusted_fitness > old_best && !current_stats.best_fitness.compare_exchange_weak( // while (ind.fitness.adjusted_fitness > old_best && !current_stats.best_fitness.compare_exchange_weak(
old_best, ind.fitness.adjusted_fitness, std::memory_order_relaxed, std::memory_order_relaxed)) // old_best, ind.fitness.adjusted_fitness, std::memory_order_relaxed, std::memory_order_relaxed))
{} // {}
//
auto old_worst = current_stats.worst_fitness.load(std::memory_order_relaxed); // auto old_worst = current_stats.worst_fitness.load(std::memory_order_relaxed);
while (ind.fitness.adjusted_fitness < old_worst && !current_stats.worst_fitness.compare_exchange_weak( // while (ind.fitness.adjusted_fitness < old_worst && !current_stats.worst_fitness.compare_exchange_weak(
old_worst, ind.fitness.adjusted_fitness, std::memory_order_relaxed, std::memory_order_relaxed)) // old_worst, ind.fitness.adjusted_fitness, std::memory_order_relaxed, std::memory_order_relaxed))
{} // {}
auto old_overall = current_stats.overall_fitness.load(std::memory_order_relaxed); auto old_overall = current_stats.overall_fitness.load(std::memory_order_relaxed);
while (!current_stats.overall_fitness.compare_exchange_weak(old_overall, ind.fitness.adjusted_fitness + old_overall, while (!current_stats.overall_fitness.compare_exchange_weak(old_overall, ind.fitness.adjusted_fitness + old_overall,
@ -1003,6 +987,12 @@ namespace blt::gp
thread_helper.evaluation_left.store(config.population_size, std::memory_order_release); thread_helper.evaluation_left.store(config.population_size, std::memory_order_release);
(*thread_execution_service)(0); (*thread_execution_service)(0);
std::sort(current_pop.begin(), current_pop.end(), [](const auto& a, const auto& b) {
return a.fitness.adjusted_fitness > b.fitness.adjusted_fitness;
});
current_stats.best_fitness = current_pop.get_individuals()[0].fitness.adjusted_fitness;
current_stats.worst_fitness = current_pop.get_individuals()[current_pop.get_individuals().size() - 1].fitness.adjusted_fitness;
current_stats.average_fitness = current_stats.overall_fitness / static_cast<double>(config.population_size); current_stats.average_fitness = current_stats.overall_fitness / static_cast<double>(config.population_size);
} }

View File

@ -713,6 +713,27 @@ namespace blt::gp
double standardized_fitness = 0; double standardized_fitness = 0;
double adjusted_fitness = 0; double adjusted_fitness = 0;
i64 hits = 0; i64 hits = 0;
/**
* Sets fitness such that larger values of raw_fitness are worse
*/
void set_normal(const double raw_fit)
{
raw_fitness = raw_fit;
standardized_fitness = raw_fit;
adjusted_fitness = 1 / (1 + raw_fit);
}
/**
* Sets fitness such that smaller values of raw_fitness are worse
* Note: this function loses accuracy the larger the number. if your desired fitness is around 0-20 (raw) then this will work
*/
void set_inverted_normal(const double raw_fit)
{
raw_fitness = raw_fit;
standardized_fitness = raw_fit;
adjusted_fitness = 1 - (1 / (1 + raw_fit));
}
}; };
struct individual_t struct individual_t

View File

@ -178,22 +178,22 @@ namespace blt::gp
{ {
move.overall_fitness = 0; move.overall_fitness = 0;
move.average_fitness = 0; move.average_fitness = 0;
move.best_fitness = 0; move.best_fitness = std::numeric_limits<double>::min();
move.worst_fitness = 0; move.worst_fitness = std::numeric_limits<double>::max();
} }
std::atomic<double> overall_fitness = 0; std::atomic<double> overall_fitness = 0;
std::atomic<double> average_fitness = 0; std::atomic<double> average_fitness = 0;
std::atomic<double> best_fitness = 0; std::atomic<double> best_fitness = std::numeric_limits<double>::min();
std::atomic<double> worst_fitness = 1; std::atomic<double> worst_fitness = std::numeric_limits<double>::max();
tracked_vector<double> normalized_fitness{}; tracked_vector<double> normalized_fitness{};
void clear() void clear()
{ {
overall_fitness = 0; overall_fitness = 0;
average_fitness = 0; average_fitness = 0;
best_fitness = 0; best_fitness = std::numeric_limits<double>::min();
worst_fitness = 0; worst_fitness = std::numeric_limits<double>::max();
normalized_fitness.clear(); normalized_fitness.clear();
} }
}; };

@ -1 +1 @@
Subproject commit 729a16ab574e31bf1b44446a777e4ee834518c6e Subproject commit 284743c683aebae3277f64a07cdd0deedf629039