2024-06-02 21:27:00 -04:00
|
|
|
/*
|
|
|
|
* <Short Description>
|
|
|
|
* Copyright (C) 2024 Brett Terpstra
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <blt/gp/program.h>
|
2024-07-11 21:14:23 -04:00
|
|
|
#include <iostream>
|
2024-06-02 21:27:00 -04:00
|
|
|
|
|
|
|
namespace blt::gp
|
|
|
|
{
|
2024-07-08 22:20:51 -04:00
|
|
|
// default static references for mutation, crossover, and initializer
|
|
|
|
// this is largely to not break the tests :3
|
|
|
|
// it's also to allow for quick setup of a gp program if you don't care how crossover or mutation is handled
|
2024-08-16 19:38:27 -04:00
|
|
|
static advanced_mutation_t s_mutator;
|
2024-07-08 22:20:51 -04:00
|
|
|
static crossover_t s_crossover;
|
|
|
|
static ramped_half_initializer_t s_init;
|
|
|
|
|
2024-07-09 14:11:19 -04:00
|
|
|
prog_config_t::prog_config_t(): mutator(s_mutator), crossover(s_crossover), pop_initializer(s_init)
|
2024-07-08 22:20:51 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-07-09 14:11:19 -04:00
|
|
|
prog_config_t::prog_config_t(const std::reference_wrapper<population_initializer_t>& popInitializer):
|
2024-07-08 22:20:51 -04:00
|
|
|
mutator(s_mutator), crossover(s_crossover), pop_initializer(popInitializer)
|
2024-07-09 03:57:58 -04:00
|
|
|
{}
|
2024-07-08 22:20:51 -04:00
|
|
|
|
2024-07-09 14:11:19 -04:00
|
|
|
prog_config_t::prog_config_t(size_t populationSize, const std::reference_wrapper<population_initializer_t>& popInitializer):
|
|
|
|
population_size(populationSize), mutator(s_mutator), crossover(s_crossover), pop_initializer(popInitializer)
|
2024-07-09 03:57:58 -04:00
|
|
|
{}
|
|
|
|
|
2024-07-09 14:11:19 -04:00
|
|
|
prog_config_t::prog_config_t(size_t populationSize):
|
2024-07-09 03:57:58 -04:00
|
|
|
population_size(populationSize), mutator(s_mutator), crossover(s_crossover), pop_initializer(s_init)
|
|
|
|
{}
|
2024-07-11 21:14:23 -04:00
|
|
|
|
|
|
|
random_t& gp_program::get_random() const
|
|
|
|
{
|
2024-08-28 17:08:59 -04:00
|
|
|
thread_local static blt::gp::random_t random_engine{seed_func()};
|
2024-07-11 21:14:23 -04:00
|
|
|
return random_engine;
|
|
|
|
}
|
|
|
|
|
2024-08-19 12:13:07 -04:00
|
|
|
stack_allocator::Allocator& stack_allocator::get_allocator()
|
|
|
|
{
|
2024-09-04 02:42:31 -04:00
|
|
|
static Allocator allocator;
|
2024-08-19 12:13:07 -04:00
|
|
|
return allocator;
|
|
|
|
}
|
|
|
|
|
2024-07-11 21:14:23 -04:00
|
|
|
void gp_program::create_threads()
|
|
|
|
{
|
2024-09-02 01:55:15 -04:00
|
|
|
#ifdef BLT_TRACK_ALLOCATIONS
|
|
|
|
tracker.reserve();
|
|
|
|
#endif
|
2024-09-03 20:34:45 -04:00
|
|
|
statistic_history.reserve(config.max_generations + 1);
|
2024-07-11 21:14:23 -04:00
|
|
|
if (config.threads == 0)
|
2024-07-12 14:09:45 -04:00
|
|
|
config.set_thread_count(std::thread::hardware_concurrency());
|
|
|
|
// main thread is thread0
|
|
|
|
for (blt::size_t i = 1; i < config.threads; i++)
|
2024-07-11 21:14:23 -04:00
|
|
|
{
|
2024-07-13 15:36:49 -04:00
|
|
|
thread_helper.threads.emplace_back(new std::thread([i, this]() {
|
2024-09-02 01:55:15 -04:00
|
|
|
#ifdef BLT_TRACK_ALLOCATIONS
|
|
|
|
tracker.reserve();
|
2024-09-02 15:41:20 -04:00
|
|
|
tracker.await_thread_loading_complete(config.threads);
|
2024-09-02 01:55:15 -04:00
|
|
|
#endif
|
2024-07-13 15:36:49 -04:00
|
|
|
std::function<void(blt::size_t)>* execution_function = nullptr;
|
2024-07-11 21:14:23 -04:00
|
|
|
while (!should_thread_terminate())
|
|
|
|
{
|
2024-07-13 15:36:49 -04:00
|
|
|
if (execution_function == nullptr)
|
|
|
|
{
|
2024-07-14 14:08:39 -04:00
|
|
|
std::unique_lock lock(thread_helper.thread_function_control);
|
2024-07-16 21:56:21 -04:00
|
|
|
while (thread_execution_service == nullptr)
|
|
|
|
{
|
|
|
|
thread_helper.thread_function_condition.wait(lock);
|
|
|
|
if (should_thread_terminate())
|
|
|
|
return;
|
|
|
|
}
|
2024-08-30 23:27:25 -04:00
|
|
|
execution_function = thread_execution_service.get();
|
2024-07-13 15:36:49 -04:00
|
|
|
}
|
|
|
|
if (execution_function != nullptr)
|
|
|
|
(*execution_function)(i);
|
2024-07-11 21:14:23 -04:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2024-09-02 03:08:03 -04:00
|
|
|
#ifdef BLT_TRACK_ALLOCATIONS
|
2024-09-02 15:41:20 -04:00
|
|
|
tracker.await_thread_loading_complete(config.threads);
|
2024-09-02 03:08:03 -04:00
|
|
|
#endif
|
2024-07-11 21:14:23 -04:00
|
|
|
}
|
2024-06-02 21:27:00 -04:00
|
|
|
}
|