This repository was archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Creates ClassifierFactory for easier classifier creation #42
Open
Regis128
wants to merge
4
commits into
feature/DatasetFactory
Choose a base branch
from
feature/classifier_factory
base: feature/DatasetFactory
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
468de2e
Creates ClassifierFactory for easier classifier creation
Regis128 b8ab36e
Merge branch 'feature/DatasetFactory' of https://github.com/spectre-t…
Regis128 2a6daa5
Merge branch 'feature/DatasetFactory' of https://github.com/spectre-t…
Regis128 e17be79
updated version
Regis128 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
58 changes: 58 additions & 0 deletions
58
src/Spectre.libGaClassifier.Tests/ClassifierFactoryTest.cpp
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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * ClassifierFactoryTest.cpp | ||
| * The factory for creating classifiers | ||
| * | ||
| Copyright 2018 Spectre Team | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| #include "Spectre.libGenetic/DataTypes.h" | ||
| #include <gtest/gtest.h> | ||
| #include "Spectre.libGaClassifier/ClassifierFactory.h" | ||
|
|
||
| namespace spectre::supervised | ||
| { | ||
| const std::string CLASSIFIER = "svm"; | ||
| const double TRAINING_SET_SPLIT_RATE = 0.7; | ||
| const double MUTATION_RATE = 0.5; | ||
| const double BIT_SWAP_RATE = 0.5; | ||
| const double PRESERVATION_RATE = 0.5; | ||
| const unsigned int GENERATIONS_NUMBER = 5u; | ||
| const unsigned int POPULATION_SIZE = 5u; | ||
| const unsigned int INITIAL_FILLUP = 3u; | ||
| const algorithm::genetic::Seed SEED = 1; | ||
|
|
||
| TEST(ClassifierFactoryInitialization, initializes) | ||
| { | ||
| EXPECT_NO_THROW(ClassifierFactory{}); | ||
| } | ||
|
|
||
| class ClassifierFactoryTest : public ::testing::Test | ||
| { | ||
| protected: | ||
| ClassifierFactory classifierFactory{}; | ||
| }; | ||
|
|
||
| TEST_F(ClassifierFactoryTest, build_svm_no_throw) | ||
| { | ||
| EXPECT_NO_THROW(classifierFactory.buildSvm()); | ||
| } | ||
|
|
||
| TEST_F(ClassifierFactoryTest, build_gaclassifier_with_svm_no_throw) | ||
| { | ||
| EXPECT_NO_THROW(classifierFactory.buildGaClassifier(CLASSIFIER, TRAINING_SET_SPLIT_RATE, MUTATION_RATE, BIT_SWAP_RATE, | ||
| PRESERVATION_RATE, GENERATIONS_NUMBER, POPULATION_SIZE, INITIAL_FILLUP)); | ||
| } | ||
|
|
||
| } |
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
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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * ClassifierFactory.cpp | ||
| * The factory for creating classifiers | ||
| * | ||
| Copyright 2018 Spectre Team | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| #include "ClassifierFactory.h" | ||
| #include "Spectre.libClassifier/Svm.h" | ||
|
|
||
| namespace spectre::supervised | ||
| { | ||
| ClassifierFactory::ClassifierFactory() {} | ||
|
|
||
| Svm ClassifierFactory::buildSvm(unsigned iterationsLimit, double tolerance) const | ||
| { | ||
| Svm svm(iterationsLimit, tolerance); | ||
| return svm; | ||
| } | ||
|
|
||
| GaClassifier ClassifierFactory::buildGaClassifier( | ||
| std::string name, | ||
| double trainingSetSplitRate, | ||
| double mutationRate, | ||
| double bitSwapRate, | ||
| double preservationRate, | ||
| unsigned generationsNumber, | ||
| unsigned populationSize, | ||
| unsigned initialFillup, | ||
| spectre::algorithm::genetic::Seed seed, | ||
| size_t minimalFillup, | ||
| size_t maximalFillup, | ||
| unsigned int iterationsLimit, | ||
| double tolerance) const | ||
| { | ||
| std::unique_ptr<IClassifier> classifier; | ||
| if (name == "svm") | ||
|
Member
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. Sooo... in every other case an empty pointer to |
||
| { | ||
| Svm svm = this->buildSvm(iterationsLimit, tolerance); | ||
| classifier = std::make_unique<Svm>(svm); | ||
| } | ||
| GaClassifier gaClassifier(std::move(classifier), trainingSetSplitRate, mutationRate, bitSwapRate, preservationRate, generationsNumber, | ||
| populationSize, initialFillup, seed, minimalFillup, maximalFillup); | ||
| return gaClassifier; | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * ClassifierFactory.h | ||
| * The factory for creating classifiers | ||
| * | ||
| Copyright 2018 Spectre Team | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
| #include "Spectre.libClassifier/Svm.h" | ||
| #include "Spectre.libGenetic/DataTypes.h" | ||
| #include "GaClassifier.h" | ||
|
|
||
| namespace spectre::supervised | ||
| { | ||
| /// <summary> | ||
| /// The factory for creating classifiers | ||
| /// </summary> | ||
| class ClassifierFactory | ||
|
Member
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. Static class maybe? This is yet another class that does not store any state, so I think this will be a suitable approach. |
||
| { | ||
| public: | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ClassifierFactory"/> class. | ||
| /// </summary> | ||
| ClassifierFactory(); | ||
| /// <summary> | ||
| /// Gets svm classifier | ||
| /// </summary> | ||
| Svm buildSvm(unsigned int iterationsLimit = 100, double tolerance = 1e-6) const; | ||
| /// <summary> | ||
| /// Gets GaClassifier | ||
| /// </summary> | ||
| GaClassifier buildGaClassifier(std::string name, | ||
| double trainingSetSplitRate, | ||
| double mutationRate, | ||
| double bitSwapRate, | ||
| double preservationRate, | ||
| unsigned int generationsNumber, | ||
| unsigned int populationSize, | ||
| unsigned int initialFillup, | ||
| spectre::algorithm::genetic::Seed seed = 0, | ||
| size_t minimalFillup = 1ul, | ||
| size_t maximalFillup = std::numeric_limits<size_t>::max(), | ||
| unsigned int iterationsLimit = 100, | ||
| double tolerance = 1e-6) const; | ||
| }; | ||
| } | ||
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
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.
That is a lot of parameters. Have you considered putting at least some of them into a struct like
GaClassifierOptionsor something similar?