Skip to content

Commit

Permalink
use small_vector in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
KRM7 committed Sep 14, 2024
1 parent 51fc547 commit 01bf321
Show file tree
Hide file tree
Showing 34 changed files with 198 additions and 190 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
-readability-suspicious-call-argument,
-readability-uppercase-literal-suffix,
-readability-braces-around-statements,
-readability-qualified-auto,
-bugprone-easily-swappable-parameters,
-bugprone-unchecked-optional-access,
-cppcoreguidelines-special-member-functions,
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
{ name: cppcheck, cmake-flag: CMAKE_CXX_CPPCHECK="cppcheck;--version;--verbose;--report-progress;--enable=all;--error-exitcode=1;--std=c++20;--suppressions-list=../.cppcheck-supressions" }
]
include:
- pkgs: clang-18 clang-tools-18 clang-tidy-18 iwyu cppcheck
- pkgs: clang-18 clang-tools-18 clang-tidy-18 cppcheck
cxx: clang++-18

defaults:
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
build-shared: [ "ON", "OFF" ]
exclude:
- platform: Win32
compiler: ClangCL
toolset: ClangCL
- platform: Win32
build-type: Release

Expand All @@ -33,9 +33,6 @@ jobs:
- name: checkout-repo
uses: actions/checkout@v4

#- name: setup-compiler
# run: choco install -y --allow-downgrade ${{ matrix.compiler.pkgs }}

- name: setup-catch
env:
CMAKE_GENERATOR: ${{ matrix.generator }}
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ else() # GNU style compiler interface
# gcc specific options
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
# -Warray-bounds, -Wstringop-overflow, -Wstringop-overread are regular false positives since g++-12
set(GAPP_WARN_FLAGS "${GAPP_WARN_FLAGS} -Wlogical-op -Wno-array-bounds -Wno-stringop-overflow -Wno-stringop-overread")
set(GAPP_WARN_FLAGS "${GAPP_WARN_FLAGS} -Wlogical-op -Wno-array-bounds -Wno-stringop-overflow -Wno-stringop-overread -Wno-free-nonheap-object")
endif()
# clang specific options
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Expand Down
10 changes: 5 additions & 5 deletions src/algorithm/algorithm_base.decl.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* Copyright (c) 2022 Krisztián Rugási. Subject to the MIT License. */

#ifndef GA_ALGORITHM_ALGORITHM_BASE_DECL_HPP
#define GA_ALGORITHM_ALGORITHM_BASE_DECL_HPP
#ifndef GAPP_ALGORITHM_ALGORITHM_BASE_DECL_HPP
#define GAPP_ALGORITHM_ALGORITHM_BASE_DECL_HPP

#include "selection_base.hpp"
#include "replacement_base.hpp"
#include "../core/population.hpp"
#include <vector>
#include "../utility/small_vector.hpp"
#include <cstddef>

namespace gapp
Expand Down Expand Up @@ -138,9 +138,9 @@ namespace gapp::algorithm
* @param ga The %GA that uses the algorithm.
* @returns The indices of the pareto optimal solutions in the current population.
*/
virtual std::vector<size_t> optimalSolutionsImpl(const GaInfo&) const { return {}; }
virtual small_vector<size_t> optimalSolutionsImpl(const GaInfo&) const { return {}; }
};

} // namespace gapp::algorithm

#endif // !GA_ALGORITHM_ALGORITHM_BASE_DECL_HPP
#endif // !GAPP_ALGORITHM_ALGORITHM_BASE_DECL_HPP
6 changes: 3 additions & 3 deletions src/algorithm/algorithm_base.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* Copyright (c) 2022 Krisztián Rugási. Subject to the MIT License. */

#ifndef GA_ALGORITHM_ALGORITHM_BASE_HPP
#define GA_ALGORITHM_ALGORITHM_BASE_HPP
#ifndef GAPP_ALGORITHM_ALGORITHM_BASE_HPP
#define GAPP_ALGORITHM_ALGORITHM_BASE_HPP

#include "algorithm_base.decl.hpp"
#include "algorithm_base.impl.hpp"

#endif //!GA_ALGORITHM_ALGORITHM_BASE_HPP
#endif //!GAPP_ALGORITHM_ALGORITHM_BASE_HPP
12 changes: 6 additions & 6 deletions src/algorithm/algorithm_base.impl.hpp
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. */

#ifndef GA_ALGORITHM_ALGORITHM_BASE_IMPL_HPP
#define GA_ALGORITHM_ALGORITHM_BASE_IMPL_HPP
#ifndef GAPP_ALGORITHM_ALGORITHM_BASE_IMPL_HPP
#define GAPP_ALGORITHM_ALGORITHM_BASE_IMPL_HPP

#include "algorithm_base.decl.hpp"
#include "../core/ga_info.hpp"
Expand All @@ -17,7 +17,7 @@
namespace gapp::algorithm
{
template<typename T>
auto Algorithm::select(const GA<T>& ga, const Population<T>& pop, const FitnessMatrix& fmat) const -> const Candidate<T>&
const Candidate<T>& Algorithm::select(const GA<T>& ga, const Population<T>& pop, const FitnessMatrix& fmat) const
{
GAPP_ASSERT(ga.population_size() == pop.size());
GAPP_ASSERT(pop.size() == fmat.size());
Expand All @@ -30,7 +30,7 @@ namespace gapp::algorithm
}

template<typename T>
auto Algorithm::nextPopulation(const GA<T>& ga, Population<T> parents, Population<T> children) -> Population<T>
Population<T> Algorithm::nextPopulation(const GA<T>& ga, Population<T> parents, Population<T> children)
{
GAPP_ASSERT(ga.population_size() == parents.size());
GAPP_ASSERT(ga.population_size() <= children.size());
Expand All @@ -48,7 +48,7 @@ namespace gapp::algorithm
}

template<typename T>
auto Algorithm::optimalSolutions(const GA<T>& ga, const Population<T>& pop) const -> Candidates<T>
Candidates<T> Algorithm::optimalSolutions(const GA<T>& ga, const Population<T>& pop) const
{
GAPP_ASSERT(ga.population_size() == pop.size());

Expand All @@ -66,4 +66,4 @@ namespace gapp::algorithm

} // namespace gapp::algorithm

#endif //!GA_ALGORITHM_ALGORITHM_BASE_IMPL_HPP
#endif //!GAPP_ALGORITHM_ALGORITHM_BASE_IMPL_HPP
2 changes: 1 addition & 1 deletion src/algorithm/nd_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ namespace gapp::algorithm::dtl
pareto_fronts.reserve(popsize);

size_t current_rank = 0;
std::vector<size_t> removed_rows;
small_vector<size_t, 8> removed_rows;

while (pareto_fronts.size() != popsize)
{
Expand Down
10 changes: 5 additions & 5 deletions src/algorithm/nsga2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ namespace gapp::algorithm
GAPP_ASSERT(!fmat.empty());
GAPP_ASSERT(std::none_of(pareto_fronts.begin(), pareto_fronts.end(), detail::is_size(0)));

std::vector<double> crowding_distances(fmat.size(), 0.0);
std::vector crowding_distances(fmat.size(), 0.0);

for (size_t obj = 0; obj < fmat.ncols(); obj++)
{
const FitnessVector fvec = fmat.column(obj);
const auto fvec = fmat.column(obj);

for (const auto& front : pareto_fronts)
{
Expand Down Expand Up @@ -83,7 +83,7 @@ namespace gapp::algorithm
return idx2;
}

std::vector<size_t> NSGA2::nextPopulationImpl(const GaInfo& ga, const FitnessMatrix& fmat)
small_vector<size_t> NSGA2::nextPopulationImpl(const GaInfo& ga, const FitnessMatrix& fmat)
{
GAPP_ASSERT(ga.num_objectives() > 1);
GAPP_ASSERT(fmat.ncols() == ga.num_objectives());
Expand All @@ -103,7 +103,7 @@ namespace gapp::algorithm
dists_ = crowdingDistances(fmat, pareto_fronts.fronts());
dists_.resize(popsize);

std::vector<size_t> new_pop(popsize);
small_vector<size_t> new_pop(popsize);
for (size_t i = 0; i < popsize; i++)
{
new_pop[i] = pareto_fronts[i].idx;
Expand All @@ -113,7 +113,7 @@ namespace gapp::algorithm
return new_pop;
}

std::vector<size_t> NSGA2::optimalSolutionsImpl(const GaInfo&) const
small_vector<size_t> NSGA2::optimalSolutionsImpl(const GaInfo&) const
{
return detail::find_indices(ranks_, detail::equal_to(0_sz));
}
Expand Down
11 changes: 6 additions & 5 deletions src/algorithm/nsga2.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* Copyright (c) 2022 Krisztián Rugási. Subject to the MIT License. */

#ifndef GA_ALGORITHM_NSGA2_HPP
#define GA_ALGORITHM_NSGA2_HPP
#ifndef GAPP_ALGORITHM_NSGA2_HPP
#define GAPP_ALGORITHM_NSGA2_HPP

#include "algorithm_base.hpp"
#include "../utility/small_vector.hpp"
#include <vector>
#include <cstddef>

Expand Down Expand Up @@ -38,14 +39,14 @@ namespace gapp::algorithm
void prepareSelectionsImpl(const GaInfo&, const FitnessMatrix&) override {}
size_t selectImpl(const GaInfo& ga, const FitnessMatrix& fmat) const override;

std::vector<size_t> nextPopulationImpl(const GaInfo& ga, const FitnessMatrix& fmat) override;
small_vector<size_t> nextPopulationImpl(const GaInfo& ga, const FitnessMatrix& fmat) override;

std::vector<size_t> optimalSolutionsImpl(const GaInfo& ga) const override;
small_vector<size_t> optimalSolutionsImpl(const GaInfo& ga) const override;

std::vector<size_t> ranks_;
std::vector<double> dists_;
};

} // namespace gapp::algorithm

#endif // !GA_ALGORITHM_NSGA2_HPP
#endif // !GAPP_ALGORITHM_NSGA2_HPP
15 changes: 8 additions & 7 deletions src/algorithm/nsga3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../metrics/pop_stats.hpp"
#include "../utility/algorithm.hpp"
#include "../utility/functional.hpp"
#include "../utility/small_vector.hpp"
#include "../utility/thread_pool.hpp"
#include "../utility/math.hpp"
#include "../utility/rng.hpp"
Expand Down Expand Up @@ -42,11 +43,11 @@ namespace gapp::algorithm
}

/* Create a weight vector for the given axis (used in the ASF). */
static inline std::vector<double> weightVector(size_t dimensions, size_t axis)
static inline small_vector<double> weightVector(size_t dimensions, size_t axis)
{
GAPP_ASSERT(dimensions > axis);

std::vector weights(dimensions, 1E-6);
small_vector weights(dimensions, 1E-6);
weights[axis] = 1.0;

return weights;
Expand Down Expand Up @@ -127,7 +128,7 @@ namespace gapp::algorithm
void incrementNicheCount(std::vector<size_t>& refs, size_t ref);

/* Create a new population from pareto_fronts. */
std::vector<size_t> createPopulation(std::span<const FrontElement> pareto_fronts);
small_vector<size_t> createPopulation(std::span<const FrontElement> pareto_fronts);
};


Expand Down Expand Up @@ -296,9 +297,9 @@ namespace gapp::algorithm
std::iter_swap(current, std::prev(first_eq));
}

std::vector<size_t> NSGA3::Impl::createPopulation(std::span<const FrontElement> pareto_fronts)
small_vector<size_t> NSGA3::Impl::createPopulation(std::span<const FrontElement> pareto_fronts)
{
std::vector<size_t> new_pop;
small_vector<size_t> new_pop;
std::vector<Impl::CandidateInfo> new_info;

new_pop.reserve(pareto_fronts.size());
Expand Down Expand Up @@ -340,7 +341,7 @@ namespace gapp::algorithm
pimpl_->recalcNicheCounts(pareto_fronts);
}

std::vector<size_t> NSGA3::nextPopulationImpl(const GaInfo& ga, const FitnessMatrix& fmat)
small_vector<size_t> NSGA3::nextPopulationImpl(const GaInfo& ga, const FitnessMatrix& fmat)
{
GAPP_ASSERT(ga.num_objectives() > 1);
GAPP_ASSERT(fmat.ncols() == ga.num_objectives());
Expand Down Expand Up @@ -403,7 +404,7 @@ namespace gapp::algorithm
return pimpl_->nichedCompare(idx1, idx2) ? idx1 : idx2;
}

std::vector<size_t> NSGA3::optimalSolutionsImpl(const GaInfo&) const
small_vector<size_t> NSGA3::optimalSolutionsImpl(const GaInfo&) const
{
return detail::find_indices(pimpl_->sol_info_, [](const Impl::CandidateInfo& sol) { return sol.rank == 0; });
}
Expand Down
12 changes: 6 additions & 6 deletions src/algorithm/nsga3.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* Copyright (c) 2022 Krisztián Rugási. Subject to the MIT License. */

#ifndef GA_ALGORITHM_NSGA3_HPP
#define GA_ALGORITHM_NSGA3_HPP
#ifndef GAPP_ALGORITHM_NSGA3_HPP
#define GAPP_ALGORITHM_NSGA3_HPP

#include "algorithm_base.hpp"
#include "reference_lines.hpp"
#include "../utility/math.hpp"
#include <vector>
#include "../utility/small_vector.hpp"
#include <functional>
#include <memory>
#include <cstddef>
Expand Down Expand Up @@ -64,14 +64,14 @@ namespace gapp::algorithm
void initializeImpl(const GaInfo& ga) override;
size_t selectImpl(const GaInfo& ga, const FitnessMatrix& fmat) const override;

std::vector<size_t> nextPopulationImpl(const GaInfo& ga, const FitnessMatrix& fmat) override;
small_vector<size_t> nextPopulationImpl(const GaInfo& ga, const FitnessMatrix& fmat) override;

std::vector<size_t> optimalSolutionsImpl(const GaInfo& ga) const override;
small_vector<size_t> optimalSolutionsImpl(const GaInfo& ga) const override;

struct Impl;
std::unique_ptr<Impl> pimpl_;
};

} // namespace gapp::algorithm

#endif // !GA_ALGORITHM_NSGA2_HPP
#endif // !GAPP_ALGORITHM_NSGA2_HPP
10 changes: 5 additions & 5 deletions src/algorithm/replacement_base.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* Copyright (c) 2023 Krisztián Rugási. Subject to the MIT License. */

#ifndef GA_ALGORITHM_SOGA_REPLACEMENT_BASE_HPP
#define GA_ALGORITHM_SOGA_REPLACEMENT_BASE_HPP
#ifndef GAPP_ALGORITHM_SOGA_REPLACEMENT_BASE_HPP
#define GAPP_ALGORITHM_SOGA_REPLACEMENT_BASE_HPP

#include "../core/population.hpp"
#include <vector>
#include "../utility/small_vector.hpp"
#include <cstddef>

namespace gapp
Expand Down Expand Up @@ -44,7 +44,7 @@ namespace gapp::replacement
* @param fmat The fitness matrix of the combined parent and child populations.
* @returns The indices of the candidates selected from the fitness matrix.
*/
virtual std::vector<size_t> nextPopulationImpl(const GaInfo& ga, const FitnessMatrix& fmat) = 0;
virtual small_vector<size_t> nextPopulationImpl(const GaInfo& ga, const FitnessMatrix& fmat) = 0;


/** Destructor. */
Expand All @@ -61,4 +61,4 @@ namespace gapp::replacement

} // namespace gapp::replacement

#endif // !GA_ALGORITHM_SOGA_REPLACEMENT_BASE_HPP
#endif // !GAPP_ALGORITHM_SOGA_REPLACEMENT_BASE_HPP
2 changes: 1 addition & 1 deletion src/algorithm/single_objective.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace gapp::algorithm
selection_->initializeImpl(ga);
}

std::vector<size_t> SingleObjective::nextPopulationImpl(const GaInfo& ga, const FitnessMatrix& fmat)
small_vector<size_t> SingleObjective::nextPopulationImpl(const GaInfo& ga, const FitnessMatrix& fmat)
{
GAPP_ASSERT(replacement_);
GAPP_ASSERT(ga.num_objectives() == 1, "The number of objectives must be 1 for the single-objective algorithms.");
Expand Down
12 changes: 6 additions & 6 deletions src/algorithm/single_objective.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* Copyright (c) 2022 Krisztián Rugási. Subject to the MIT License. */

#ifndef GA_ALGORITHM_SINGLE_OBJECTIVE_HPP
#define GA_ALGORITHM_SINGLE_OBJECTIVE_HPP
#ifndef GAPP_ALGORITHM_SINGLE_OBJECTIVE_HPP
#define GAPP_ALGORITHM_SINGLE_OBJECTIVE_HPP

#include "algorithm_base.decl.hpp"
#include "selection_base.hpp"
#include "replacement_base.hpp"
#include "soga_selection.hpp"
#include "soga_replacement.hpp"
#include "../utility/small_vector.hpp"
#include "../utility/utility.hpp"
#include <vector>
#include <concepts>
#include <functional>
#include <memory>
Expand Down Expand Up @@ -45,7 +45,7 @@ namespace gapp::algorithm
* when not using a replacement policy derived from replacement::Replacement.
* @see replacement_method()
*/
using ReplacementCallable = std::function<std::vector<size_t>(const GaInfo&, const FitnessMatrix&)>;
using ReplacementCallable = std::function<small_vector<size_t>(const GaInfo&, const FitnessMatrix&)>;


/**
Expand Down Expand Up @@ -153,7 +153,7 @@ namespace gapp::algorithm
void prepareSelectionsImpl(const GaInfo& ga, const FitnessMatrix& fmat) override;
size_t selectImpl(const GaInfo& ga, const FitnessMatrix& fmat) const override;

std::vector<size_t> nextPopulationImpl(const GaInfo& ga, const FitnessMatrix& fmat) override;
small_vector<size_t> nextPopulationImpl(const GaInfo& ga, const FitnessMatrix& fmat) override;

std::unique_ptr<selection::Selection> selection_;
std::unique_ptr<replacement::Replacement> replacement_;
Expand Down Expand Up @@ -204,4 +204,4 @@ namespace gapp::algorithm

} // namespace gapp::algorithm

#endif // !GA_ALGORITHM_SINGLE_OBJECTIVE_HPP
#endif // !GAPP_ALGORITHM_SINGLE_OBJECTIVE_HPP
Loading

0 comments on commit 01bf321

Please sign in to comment.