-
Notifications
You must be signed in to change notification settings - Fork 2
Cmake modernizations, update .clangformat #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bndkr
wants to merge
4
commits into
BiagioFesta:master
Choose a base branch
from
bndkr:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| build*/ | ||
| _bld*/ | ||
| .vscode/* | ||
| .ccls-cache/ | ||
| compile_commands.json | ||
| .dir-locals.el | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,28 @@ | ||
| cmake_minimum_required(VERSION 3.14) | ||
| cmake_minimum_required(VERSION 3.23) | ||
| project(EvolutionNet) | ||
|
|
||
| option(BUILD_EXAMPLES "Build Examples" OFF) | ||
| option(BUILD_TESTS "Build Tests" OFF) | ||
|
|
||
| add_library(${PROJECT_NAME} INTERFACE) | ||
| ######################### | ||
| set(${PROJECT_NAME}_srcs | ||
| include/${PROJECT_NAME}/ConnectionGene.hpp | ||
| include/${PROJECT_NAME}/EvolutionNet.hpp | ||
| include/${PROJECT_NAME}/FlatMap.hpp | ||
| include/${PROJECT_NAME}/FlatSet.hpp | ||
| include/${PROJECT_NAME}/Genome.hpp | ||
| include/${PROJECT_NAME}/Network.hpp | ||
| include/${PROJECT_NAME}/Population.hpp | ||
| include/${PROJECT_NAME}/Random.hpp | ||
| include/${PROJECT_NAME}/Types.hpp | ||
| ) | ||
| add_library(${PROJECT_NAME} INTERFACE ${${PROJECT_NAME}_srcs}) | ||
| target_sources(${PROJECT_NAME} INTERFACE FILE_SET header_files TYPE HEADERS FILES ${${PROJECT_NAME}_srcs}) | ||
| source_group("" FILES ${${PROJECT_NAME}_srcs}) | ||
| target_include_directories(${PROJECT_NAME} INTERFACE include/) | ||
| target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17) | ||
|
|
||
| ######################### | ||
| if(${BUILD_EXAMPLES}) | ||
| add_subdirectory(${PROJECT_SOURCE_DIR}/examples) | ||
| endif() | ||
|
|
||
| if(${BUILD_TESTS}) | ||
| add_subdirectory(${PROJECT_SOURCE_DIR}/tests) | ||
| endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| add_executable(${PROJECT_NAME}_example_xor_minimal xor_minimal.cpp) | ||
| target_link_libraries(${PROJECT_NAME}_example_xor_minimal PRIVATE ${PROJECT_NAME}) | ||
|
|
||
| add_executable(${PROJECT_NAME}_example_xor_advanced xor_advanced.cpp) | ||
| target_link_libraries(${PROJECT_NAME}_example_xor_advanced PRIVATE ${PROJECT_NAME}) | ||
| set(xor_advanced_srcs xor_advanced.cpp) | ||
| set(xor_minimal_srcs xor_minimal.cpp) | ||
| #################### | ||
| add_executable(xor_advanced_example ${xor_advanced_srcs}) | ||
| target_link_libraries(xor_advanced_example PRIVATE ${PROJECT_NAME}) | ||
| source_group("" FILES ${xor_advanced_srcs}) | ||
| #################### | ||
| add_executable(xor_minimal_example ${xor_minimal_srcs}) | ||
| target_link_libraries(xor_minimal_example PRIVATE ${PROJECT_NAME}) | ||
| source_group("" FILES ${xor_minimal_srcs}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ | |
| #include <type_traits> | ||
|
|
||
| namespace EvolutionNet { | ||
|
|
||
| // clang-format off | ||
| /*! \class EvolutionNet | ||
| * \brief This class represents the main interface of this library. | ||
| * Some of the network features are set at compile time with template logic. | ||
|
|
@@ -40,6 +40,7 @@ namespace EvolutionNet { | |
| * net.evolve(); // Evolve in the next generation and loop! | ||
| * } | ||
| */ | ||
| // clang-format on | ||
| template <int NumInput, int NumOutput, bool Bias, typename ParamConfig> | ||
| class EvolutionNet { | ||
| public: | ||
|
|
@@ -51,10 +52,12 @@ class EvolutionNet { | |
|
|
||
| /*! \brief Initialize the Evolution Net. | ||
| * You need to call this function before any other operation. | ||
| * Here you can set the size of the population and the random seed used internally. | ||
| * \note `populationSize` must be greater than zero, otherwise Undefined Behavior. | ||
| * Here you can set the size of the population and the random seed used | ||
| * internally. \note `populationSize` must be greater than zero, otherwise | ||
| * Undefined Behavior. | ||
| */ | ||
| void initialize(const std::size_t populationSize, const SeedT rndSeed = DefaultSeed); | ||
| void initialize(const std::size_t populationSize, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: why is breaking line? |
||
| const SeedT rndSeed = DefaultSeed); | ||
|
|
||
| //! \return all the networks (size of the population). | ||
| inline std::vector<NetworkT>& getNetworks() noexcept; | ||
|
|
@@ -68,19 +71,23 @@ class EvolutionNet { | |
| template <typename Fn> | ||
| inline void evaluateAll(Fn&& fn); | ||
|
|
||
| //! \return the best fitness score for this current generation. Call this only after ending evaluation. | ||
| //! \return the best fitness score for this current generation. Call this only | ||
| //! after ending evaluation. | ||
| inline FitnessScore getBestFitness() const noexcept; | ||
|
|
||
| //! \return the best network accordinlying with the max fitness score. Call this only after ending evaluation. | ||
| //! \return the best network accordinlying with the max fitness score. Call | ||
| //! this only after ending evaluation. | ||
| inline const NetworkT& getBestNetwork() const noexcept; | ||
| inline NetworkT* getBestNetworkMutable() noexcept; | ||
|
|
||
| /*! \brief Evolve the population. | ||
| * Call this only after ending evaluation, thus, fitness for each network has been set. | ||
| * Call this only after ending evaluation, thus, fitness for each network has | ||
| * been set. | ||
| */ | ||
| inline void evolve(); | ||
|
|
||
| //! \return the size of the population. This is constant (does not change during evolution). | ||
| //! \return the size of the population. This is constant (does not change | ||
| //! during evolution). | ||
| inline std::size_t getPopulationSize() const noexcept; | ||
|
|
||
| //! \return the generation counter. First generation stats from 0. | ||
|
|
@@ -91,7 +98,9 @@ class EvolutionNet { | |
| struct IsValidEvaluationFunction : std::false_type {}; | ||
|
|
||
| template <typename Fn> | ||
| struct IsValidEvaluationFunction<Fn, std::void_t<decltype(std::declval<Fn>()(std::declval<NetworkT*>()))>> | ||
| struct IsValidEvaluationFunction< | ||
| Fn, | ||
| std::void_t<decltype(std::declval<Fn>()(std::declval<NetworkT*>()))>> | ||
| : std::true_type {}; | ||
|
|
||
| RndEngine rndEngine_; | ||
|
|
@@ -104,8 +113,9 @@ class EvolutionNet { | |
| }; | ||
|
|
||
| template <int NumInput, int NumOutput, bool Bias, typename ParamConfig> | ||
| void EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::initialize(const std::size_t populationSize, | ||
| const SeedT rndSeed) { | ||
| void EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::initialize( | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer having first argument on the same line. we can adjust the clang format I guess |
||
| const std::size_t populationSize, | ||
| const SeedT rndSeed) { | ||
| assert(populationSize > 0); | ||
|
|
||
| rndEngine_.seed(rndSeed); | ||
|
|
@@ -120,23 +130,27 @@ void EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::initialize(const std: | |
| } | ||
|
|
||
| template <int NumInput, int NumOutput, bool Bias, typename ParamConfig> | ||
| inline std::vector<typename EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::NetworkT>& | ||
| inline std::vector< | ||
| typename EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::NetworkT>& | ||
| EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::getNetworks() noexcept { | ||
| return networks_; | ||
| } | ||
|
|
||
| template <int NumInput, int NumOutput, bool Bias, typename ParamConfig> | ||
| inline typename EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::NetworkT& | ||
| EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::getNetworkNth(const std::size_t i) noexcept { | ||
| EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::getNetworkNth( | ||
| const std::size_t i) noexcept { | ||
| assert(i < networks_.size()); | ||
| return networks_[i]; | ||
| } | ||
|
|
||
| template <int NumInput, int NumOutput, bool Bias, typename ParamConfig> | ||
| template <typename Fn> | ||
| inline void EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::evaluateAll(Fn&& fn) { | ||
| inline void EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::evaluateAll( | ||
| Fn&& fn) { | ||
| static_assert(IsValidEvaluationFunction<Fn>::value, | ||
| "Fn is not a valid evaluation function. It should be something like `void(*)(Network*)`"); | ||
| "Fn is not a valid evaluation function. It should be something " | ||
| "like `void(*)(Network*)`"); | ||
|
|
||
| const std::size_t numNetworks = networks_.size(); | ||
| FitnessScore fitness, fitnessMax; | ||
|
|
@@ -163,21 +177,25 @@ inline void EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::evaluateAll(Fn | |
| } | ||
|
|
||
| template <int NumInput, int NumOutput, bool Bias, typename ParamConfig> | ||
| FitnessScore EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::getBestFitness() const noexcept { | ||
| FitnessScore | ||
| EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::getBestFitness() | ||
| const noexcept { | ||
| assert(bestNetwork_ != nullptr); | ||
| return bestNetwork_->getFitness(); | ||
| } | ||
|
|
||
| template <int NumInput, int NumOutput, bool Bias, typename ParamConfig> | ||
| const typename EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::NetworkT& | ||
| EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::getBestNetwork() const noexcept { | ||
| EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::getBestNetwork() | ||
| const noexcept { | ||
| assert(bestNetwork_ != nullptr); | ||
| return *bestNetwork_; | ||
| } | ||
|
|
||
| template <int NumInput, int NumOutput, bool Bias, typename ParamConfig> | ||
| typename EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::NetworkT* | ||
| EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::getBestNetworkMutable() noexcept { | ||
| EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>:: | ||
| getBestNetworkMutable() noexcept { | ||
| assert(bestNetwork_ != nullptr); | ||
| return bestNetwork_; | ||
| } | ||
|
|
@@ -192,17 +210,22 @@ inline void EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::evolve() { | |
| } | ||
|
|
||
| template <int NumInput, int NumOutput, bool Bias, typename ParamConfig> | ||
| inline std::size_t EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::getPopulationSize() const noexcept { | ||
| inline std::size_t | ||
| EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::getPopulationSize() | ||
| const noexcept { | ||
| return population_.getPopulationSize(); | ||
| } | ||
|
|
||
| template <int NumInput, int NumOutput, bool Bias, typename ParamConfig> | ||
| inline std::size_t EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::getCounterGeneration() const noexcept { | ||
| inline std::size_t | ||
| EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::getCounterGeneration() | ||
| const noexcept { | ||
| return counterGeneration_; | ||
| } | ||
|
|
||
| template <int NumInput, int NumOutput, bool Bias, typename ParamConfig> | ||
| inline void EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::computeNetworks() { | ||
| inline void | ||
| EvolutionNet<NumInput, NumOutput, Bias, ParamConfig>::computeNetworks() { | ||
| const std::size_t popSize = population_.getPopulationSize(); | ||
| assert(networks_.size() == popSize); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are we able to keep indentation for doxygen comments?
\commandshould be prefixed by newline