Skip to content
This repository was archived by the owner on Sep 18, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ environment:
configuration:
- Debug
- Release
version: 4.4.0.{build}
version: 4.5.0.{build}

init:
- cmd: echo Project - %APPVEYOR_PROJECT_NAME%
Expand Down
58 changes: 58 additions & 0 deletions src/Spectre.libGaClassifier.Tests/ClassifierFactoryTest.cpp
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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
<ItemGroup>
<ClCompile Include="..\Common\Main.cpp" />
<ClCompile Include="AllLabelTypesIncludedConditionTest.cpp" />
<ClCompile Include="ClassifierFactoryTest.cpp" />
<ClCompile Include="GaClassifierTest.cpp" />
<ClCompile Include="ClassifierFitnessFunctionTest.cpp" />
<ClCompile Include="IndividualFeasibilityConditionsFactoryTest.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<ClCompile Include="ClassifierFitnessFunctionTest.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ClassifierFactoryTest.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
58 changes: 58 additions & 0 deletions src/Spectre.libGaClassifier/ClassifierFactory.cpp
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
Copy link
Copy Markdown
Member

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 GaClassifierOptions or something similar?

{
std::unique_ptr<IClassifier> classifier;
if (name == "svm")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sooo... in every other case an empty pointer to IClassifier is passed to the GaClassifier constructor?

{
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;
}
}
58 changes: 58 additions & 0 deletions src/Spectre.libGaClassifier/ClassifierFactory.h
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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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;
};
}
4 changes: 4 additions & 0 deletions src/Spectre.libGaClassifier/Spectre.libGaClassifier.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@
<ClInclude Include="AllLabelTypesIncludedCondition.h">
<SubType>Header Files</SubType>
</ClInclude>
<ClInclude Include="ClassifierFactory.h">
<SubType>Header Files</SubType>
</ClInclude>
<ClInclude Include="GaClassifier.h">
<SubType>Header Files</SubType>
</ClInclude>
Expand All @@ -156,6 +159,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="AllLabelTypesIncludedCondition.cpp" />
<ClCompile Include="ClassifierFactory.cpp" />
<ClCompile Include="GaClassifier.cpp" />
<ClCompile Include="ClassifierFitnessFunction.cpp" />
<ClCompile Include="IndividualFeasibilityConditionsFactory.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<ClInclude Include="ClassifierFitnessFunction.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ClassifierFactory.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand All @@ -59,5 +62,8 @@
<ClCompile Include="ClassifierFitnessFunction.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ClassifierFactory.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>