i think elites are working now

thread
Brett 2024-07-11 00:52:36 -04:00
parent 93dcf12b69
commit ef46bc3189
6 changed files with 44 additions and 42 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.57) project(blt-gp VERSION 0.0.58)
include(CTest) include(CTest)

View File

@ -60,8 +60,8 @@ void print_best()
auto& v = program.get_current_pop().get_individuals()[i]; auto& v = program.get_current_pop().get_individuals()[i];
auto& tree = v.tree; auto& tree = v.tree;
auto size = tree.get_values().size(); auto size = tree.get_values().size();
BLT_TRACE("%lf (fitness: %lf, raw: %lf) (depth: %ld) (blocks: %ld) (size: t: %ld m: %ld u: %ld r: %ld) filled: %f%%", BLT_TRACE("%lf [index %ld] (fitness: %lf, raw: %lf) (depth: %ld) (blocks: %ld) (size: t: %ld m: %ld u: %ld r: %ld) filled: %f%%",
tree.get_evaluation_value<float>(nullptr), v.adjusted_fitness, v.raw_fitness, tree.get_evaluation_value<float>(nullptr), i, v.standardized_fitness, v.raw_fitness,
tree.get_depth(program), size.blocks, size.total_size_bytes, size.total_no_meta_bytes, size.total_used_bytes, tree.get_depth(program), size.blocks, size.total_size_bytes, size.total_no_meta_bytes, size.total_used_bytes,
size.total_remaining_bytes, size.total_remaining_bytes,
static_cast<double>(size.total_used_bytes) / static_cast<double>(size.total_no_meta_bytes)); static_cast<double>(size.total_used_bytes) / static_cast<double>(size.total_no_meta_bytes));
@ -110,7 +110,7 @@ int main()
current_tree.get_depth(program), size.blocks, size.total_size_bytes, size.total_no_meta_bytes, size.total_used_bytes, current_tree.get_depth(program), size.blocks, size.total_size_bytes, size.total_no_meta_bytes, size.total_used_bytes,
size.total_remaining_bytes, static_cast<double>(size.total_used_bytes) / static_cast<double>(size.total_no_meta_bytes));*/ size.total_remaining_bytes, static_cast<double>(size.total_used_bytes) / static_cast<double>(size.total_no_meta_bytes));*/
container[index] = current_tree.get_evaluation_value<float>(nullptr); container[index] = current_tree.get_evaluation_value<float>(nullptr);
return container[index]; return container[index] / 1000000000.0;
}, result_container); }, result_container);
print_best(); print_best();
program.create_next_generation(blt::gp::select_tournament_t{}, blt::gp::select_tournament_t{}, program.create_next_generation(blt::gp::select_tournament_t{}, blt::gp::select_tournament_t{},
@ -120,7 +120,7 @@ int main()
program.evaluate_fitness([](blt::gp::tree_t& current_tree, decltype(result_container)& container, blt::size_t index) { program.evaluate_fitness([](blt::gp::tree_t& current_tree, decltype(result_container)& container, blt::size_t index) {
container[index] = current_tree.get_evaluation_value<float>(nullptr); container[index] = current_tree.get_evaluation_value<float>(nullptr);
return container[index]; return container[index] / 1000000000.0;
}, result_container); }, result_container);
print_best(); print_best();

View File

@ -287,7 +287,7 @@ namespace blt::gp
{ {
if (ind.raw_fitness < min) if (ind.raw_fitness < min)
min = ind.raw_fitness; min = ind.raw_fitness;
if (larger_better && ind.raw_fitness > max) if (ind.raw_fitness > max)
max = ind.raw_fitness; max = ind.raw_fitness;
} }
@ -301,29 +301,29 @@ namespace blt::gp
for (auto& ind : current_pop.get_individuals()) for (auto& ind : current_pop.get_individuals())
{ {
// make standardized fitness [0, +inf) // make standardized fitness [0, +inf)
auto standardized_fitness = ind.raw_fitness + diff; ind.standardized_fitness = ind.raw_fitness + diff;
BLT_WARN(standardized_fitness); //BLT_WARN(ind.standardized_fitness);
if (larger_better) if (larger_better)
standardized_fitness = (max + diff) - standardized_fitness; ind.standardized_fitness = (max + diff) - ind.standardized_fitness;
BLT_WARN(standardized_fitness); //BLT_WARN(ind.standardized_fitness);
ind.adjusted_fitness = (1.0 / (1.0 + standardized_fitness)); //ind.adjusted_fitness = (1.0 / (1.0 + ind.standardized_fitness));
if (ind.adjusted_fitness > worst_fitness) if (ind.standardized_fitness > worst_fitness)
{ {
worst_fitness = ind.adjusted_fitness; worst_fitness = ind.standardized_fitness;
worst = &ind; worst = &ind;
} }
if (ind.adjusted_fitness < best_fitness) if (ind.standardized_fitness < best_fitness)
{ {
best_fitness = ind.adjusted_fitness; best_fitness = ind.standardized_fitness;
best = &ind; best = &ind;
} }
overall_fitness += ind.adjusted_fitness; overall_fitness += ind.standardized_fitness / static_cast<double>(config.population_size);
} }
current_stats = {overall_fitness, overall_fitness / static_cast<double>(config.population_size), best_fitness, worst_fitness, best, current_stats = {overall_fitness, overall_fitness, best_fitness, worst_fitness, best,
worst}; worst};
} }
@ -347,7 +347,7 @@ namespace blt::gp
values.reserve(current_pop.get_individuals().size()); values.reserve(current_pop.get_individuals().size());
for (const auto& ind : blt::enumerate(current_pop.get_individuals())) for (const auto& ind : blt::enumerate(current_pop.get_individuals()))
values.emplace_back(ind.first, ind.second.adjusted_fitness); values.emplace_back(ind.first, ind.second.standardized_fitness);
std::sort(values.begin(), values.end(), [](const auto& a, const auto& b) { std::sort(values.begin(), values.end(), [](const auto& a, const auto& b) {
return a.second < b.second; return a.second < b.second;

View File

@ -52,31 +52,32 @@ namespace blt::gp
std::vector<std::pair<std::size_t, double>> values; std::vector<std::pair<std::size_t, double>> values;
for (blt::size_t i = 0; i < config.elites; i++) for (blt::size_t i = 0; i < config.elites; i++)
values.emplace_back(i, current_pop.get_individuals()[i].adjusted_fitness); values.emplace_back(i, current_pop.get_individuals()[i].standardized_fitness);
for (const auto& ind : blt::enumerate(current_pop.get_individuals())) for (const auto& ind : blt::enumerate(current_pop.get_individuals()))
{ {
blt::i64 largest = -1;
double largest_fit = 0;
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.adjusted_fitness, values[i].second, ind.second.raw_fitness); // BLT_INFO("%lf < %lf? // %lf", ind.second.standardized_fitness, values[i].second, ind.second.raw_fitness);
if (ind.second.adjusted_fitness < values[i].second && values[i].second > largest_fit) if (ind.second.standardized_fitness < values[i].second)
{ {
largest = static_cast<blt::i64>(i); bool doesnt_contain = true;
largest_fit = values[i].second; for (blt::size_t j = 0; j < config.elites; j++)
{
if (ind.first == values[j].first)
doesnt_contain = false;
}
if (doesnt_contain)
values[i] = {ind.first, ind.second.standardized_fitness};
break;
} }
} }
if (largest > 0)
{
BLT_INFO("%ld %lf", largest, largest_fit);
values[largest] = {ind.first, ind.second.adjusted_fitness};
}
} }
for (blt::size_t i = 0; i < config.elites; i++) for (blt::size_t i = 0; i < config.elites; i++)
{ {
BLT_DEBUG("%lf %lf", values[i].second, current_pop.get_individuals()[values[i].first].tree.get_evaluation_value<float>(nullptr)); // BLT_DEBUG("%lf at %ld produces %lf", 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]);
} }
} }

View File

@ -115,7 +115,8 @@ namespace blt::gp
{ {
tree_t tree; tree_t tree;
double raw_fitness = 0; double raw_fitness = 0;
double adjusted_fitness = 0; double standardized_fitness = 0;
//double adjusted_fitness = 0;
double probability = 0; double probability = 0;
individual() = default; individual() = default;

View File

@ -24,13 +24,13 @@ namespace blt::gp
tree_t& select_best_t::select(gp_program&, population_t& pop, population_stats&) tree_t& select_best_t::select(gp_program&, population_t& pop, population_stats&)
{ {
auto& first = pop.get_individuals()[0]; auto& first = pop.get_individuals()[0];
double best_fitness = first.adjusted_fitness; double best_fitness = first.standardized_fitness;
tree_t* tree = &first.tree; tree_t* tree = &first.tree;
for (auto& ind : pop.get_individuals()) for (auto& ind : pop.get_individuals())
{ {
if (ind.adjusted_fitness < best_fitness) if (ind.standardized_fitness < best_fitness)
{ {
best_fitness = ind.adjusted_fitness; best_fitness = ind.standardized_fitness;
tree = &ind.tree; tree = &ind.tree;
} }
} }
@ -40,13 +40,13 @@ namespace blt::gp
tree_t& select_worst_t::select(gp_program&, population_t& pop, population_stats&) tree_t& select_worst_t::select(gp_program&, population_t& pop, population_stats&)
{ {
auto& first = pop.get_individuals()[0]; auto& first = pop.get_individuals()[0];
double worst_fitness = first.adjusted_fitness; double worst_fitness = first.standardized_fitness;
tree_t* tree = &first.tree; tree_t* tree = &first.tree;
for (auto& ind : pop.get_individuals()) for (auto& ind : pop.get_individuals())
{ {
if (ind.adjusted_fitness > worst_fitness) if (ind.standardized_fitness > worst_fitness)
{ {
worst_fitness = ind.adjusted_fitness; worst_fitness = ind.standardized_fitness;
tree = &ind.tree; tree = &ind.tree;
} }
} }
@ -63,13 +63,13 @@ namespace blt::gp
auto& first = pop.get_individuals()[program.get_random().get_size_t(0ul, pop.get_individuals().size())]; auto& first = pop.get_individuals()[program.get_random().get_size_t(0ul, pop.get_individuals().size())];
individual* ind = &first; individual* ind = &first;
double best_guy = first.adjusted_fitness; double best_guy = first.standardized_fitness;
for (blt::size_t i = 0; i < selection_size - 1; i++) 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 = pop.get_individuals()[program.get_random().get_size_t(0ul, pop.get_individuals().size())];
if (sel.adjusted_fitness < best_guy) if (sel.standardized_fitness < best_guy)
{ {
best_guy = sel.adjusted_fitness; best_guy = sel.standardized_fitness;
ind = &sel; ind = &sel;
} }
} }
@ -97,7 +97,7 @@ namespace blt::gp
double sum_of_prob = 0; double sum_of_prob = 0;
for (auto& ind : pop) for (auto& ind : pop)
{ {
ind.probability = sum_of_prob + (ind.adjusted_fitness / stats.overall_fitness); ind.probability = sum_of_prob + (ind.standardized_fitness / stats.overall_fitness);
sum_of_prob += ind.probability; sum_of_prob += ind.probability;
} }
} }