diff --git a/src/core/ga_base.decl.hpp b/src/core/ga_base.decl.hpp index 89580a9c..9176efee 100644 --- a/src/core/ga_base.decl.hpp +++ b/src/core/ga_base.decl.hpp @@ -105,7 +105,8 @@ namespace gapp * The mutation probability used will be deduced from the chromosome length. * * @param population_size The number of candidates in the population. Must be at least 1. - * @param algorithm The algorithm to use. Can't be a nullptr. + * @param algorithm The algorithm to use. The default algorithm will be used if it's a + * nullptr. */ GA(Positive population_size, std::unique_ptr algorithm); @@ -117,7 +118,8 @@ namespace gapp * @param population_size The number of candidates in the population. Must be at least 1. * @param crossover The crossover operator to use. Can't be a nullptr. * @param mutation The mutation operator to use. Can't be a nullptr. - * @param stop_condition The early-stop condition to use. Can't be a nullptr. + * @param stop_condition The early-stop condition to use. No early-stopping will be used + * if it's a nullptr. */ GA(Positive population_size, std::unique_ptr> crossover, @@ -128,10 +130,12 @@ namespace gapp * Create a genetic algorithm using the specified algorithm and operators. * * @param population_size The number of candidates in the population. Must be at least 1. - * @param algorithm The algorithm to use. Can't be a nullptr. + * @param algorithm The algorithm to use. The default algorithm will be used if it's a + * nullptr. * @param crossover The crossover operator to use. Can't be a nullptr. * @param mutation The mutation operator to use. Can't be a nullptr. - * @param stop_condition The early-stop condition to use. Can't be a nullptr. + * @param stop_condition The early-stop condition to use. No early-stopping will be used + * if it's a nullptr. */ GA(Positive population_size, std::unique_ptr algorithm, diff --git a/src/core/ga_base.impl.hpp b/src/core/ga_base.impl.hpp index a29b58cc..31266af0 100644 --- a/src/core/ga_base.impl.hpp +++ b/src/core/ga_base.impl.hpp @@ -38,10 +38,8 @@ namespace gapp std::unique_ptr stop_condition) : GaInfo(population_size, std::move(algorithm), std::move(stop_condition)), crossover_(std::move(crossover)), mutation_(std::move(mutation)) { - GAPP_ASSERT(algorithm_, "The algorithm can't be a nullptr."); GAPP_ASSERT(crossover_, "The crossover method can't be a nullptr."); GAPP_ASSERT(mutation_, "The mutation method can't be a nullptr."); - GAPP_ASSERT(stop_condition_, "The stop condition can't be a nullptr."); } template diff --git a/src/core/ga_info.cpp b/src/core/ga_info.cpp index 01bdfcf3..0f40a3e9 100644 --- a/src/core/ga_info.cpp +++ b/src/core/ga_info.cpp @@ -1,7 +1,7 @@ /* Copyright (c) 2022 Krisztián Rugási. Subject to the MIT License. */ #include "ga_info.hpp" -#include "../algorithm/algorithm_base.hpp" +#include "../algorithm/single_objective.hpp" #include "../stop_condition/stop_condition.hpp" #include "../utility/utility.hpp" #include @@ -19,7 +19,9 @@ namespace gapp GaInfo::GaInfo(Positive population_size, std::unique_ptr algorithm, std::unique_ptr stop_condition) noexcept : algorithm_(std::move(algorithm)), stop_condition_(std::move(stop_condition)), population_size_(population_size) { - GAPP_ASSERT(stop_condition_, "The stop condition can't be a nullptr."); + use_default_algorithm_ = !algorithm; + if (!algorithm_) algorithm_ = std::make_unique(); + if (!stop_condition_) stop_condition_ = std::make_unique(); } size_t GaInfo::num_fitness_evals() const noexcept @@ -30,10 +32,14 @@ namespace gapp void GaInfo::algorithm(std::unique_ptr f) { - GAPP_ASSERT(f, "The algorithm can't be a nullptr."); + use_default_algorithm_ = !f; + algorithm_ = f ? std::move(f) : std::make_unique(); + } - algorithm_ = std::move(f); - use_default_algorithm_ = false; + void GaInfo::algorithm(std::nullptr_t) + { + use_default_algorithm_ = true; + algorithm_ = std::make_unique(); } void GaInfo::stop_condition(std::unique_ptr f) diff --git a/src/core/ga_info.hpp b/src/core/ga_info.hpp index c2e856f2..6d059f2b 100644 --- a/src/core/ga_info.hpp +++ b/src/core/ga_info.hpp @@ -176,10 +176,18 @@ namespace gapp * be a single-objective algorithm for single-objective problems, and a multi-objective * algorithm for multi-objective problems). * - * @param f The algorithm used by the %GA. Can't be a nullptr. + * @param f The algorithm used by the %GA. The default algorithm will be used if it's a + * nullptr. */ void algorithm(std::unique_ptr f); + /** + * Clear the algorithm currently set for the GA. \n + * The GA will use the default algorithm that is selected based on the number of + * objectives of the fitness functions. + */ + void algorithm(std::nullptr_t); + /** @returns The algorithm used by the %GA. */ [[nodiscard]] const algorithm::Algorithm& algorithm() const& noexcept { GAPP_ASSERT(algorithm_); return *algorithm_; }