Skip to content

Commit

Permalink
use the default algorithm when set to nullptr
Browse files Browse the repository at this point in the history
  • Loading branch information
KRM7 committed Aug 10, 2023
1 parent 5673c14 commit c91149b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
12 changes: 8 additions & 4 deletions src/core/ga_base.decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t> population_size, std::unique_ptr<algorithm::Algorithm> algorithm);

Expand All @@ -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<size_t> population_size,
std::unique_ptr<crossover::Crossover<T>> crossover,
Expand All @@ -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<size_t> population_size,
std::unique_ptr<algorithm::Algorithm> algorithm,
Expand Down
2 changes: 0 additions & 2 deletions src/core/ga_base.impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ namespace gapp
std::unique_ptr<typename stopping::StopCondition> 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<typename T>
Expand Down
16 changes: 11 additions & 5 deletions src/core/ga_info.cpp
Original file line number Diff line number Diff line change
@@ -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 <atomic>
Expand All @@ -19,7 +19,9 @@ namespace gapp
GaInfo::GaInfo(Positive<size_t> population_size, std::unique_ptr<algorithm::Algorithm> algorithm, std::unique_ptr<stopping::StopCondition> 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<algorithm::SingleObjective>();
if (!stop_condition_) stop_condition_ = std::make_unique<stopping::NoEarlyStop>();
}

size_t GaInfo::num_fitness_evals() const noexcept
Expand All @@ -30,10 +32,14 @@ namespace gapp

void GaInfo::algorithm(std::unique_ptr<algorithm::Algorithm> f)
{
GAPP_ASSERT(f, "The algorithm can't be a nullptr.");
use_default_algorithm_ = !f;
algorithm_ = f ? std::move(f) : std::make_unique<algorithm::SingleObjective>();
}

algorithm_ = std::move(f);
use_default_algorithm_ = false;
void GaInfo::algorithm(std::nullptr_t)
{
use_default_algorithm_ = true;
algorithm_ = std::make_unique<algorithm::SingleObjective>();
}

void GaInfo::stop_condition(std::unique_ptr<stopping::StopCondition> f)
Expand Down
10 changes: 9 additions & 1 deletion src/core/ga_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<algorithm::Algorithm> 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_; }
Expand Down

0 comments on commit c91149b

Please sign in to comment.