Add project files.
This commit is contained in:
31
Exponential.sln
Normal file
31
Exponential.sln
Normal file
@ -0,0 +1,31 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.3.32929.385
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Exponential", "Exponential\Exponential.vcxproj", "{74C04891-9509-4EA4-BC52-6D86492B203A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|x64 = Debug|x64
|
||||
Debug|x86 = Debug|x86
|
||||
Release|x64 = Release|x64
|
||||
Release|x86 = Release|x86
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{74C04891-9509-4EA4-BC52-6D86492B203A}.Debug|x64.ActiveCfg = Debug|x64
|
||||
{74C04891-9509-4EA4-BC52-6D86492B203A}.Debug|x64.Build.0 = Debug|x64
|
||||
{74C04891-9509-4EA4-BC52-6D86492B203A}.Debug|x86.ActiveCfg = Debug|Win32
|
||||
{74C04891-9509-4EA4-BC52-6D86492B203A}.Debug|x86.Build.0 = Debug|Win32
|
||||
{74C04891-9509-4EA4-BC52-6D86492B203A}.Release|x64.ActiveCfg = Release|x64
|
||||
{74C04891-9509-4EA4-BC52-6D86492B203A}.Release|x64.Build.0 = Release|x64
|
||||
{74C04891-9509-4EA4-BC52-6D86492B203A}.Release|x86.ActiveCfg = Release|Win32
|
||||
{74C04891-9509-4EA4-BC52-6D86492B203A}.Release|x86.Build.0 = Release|Win32
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {C33279A4-898B-46DE-9C0E-87EE72B702AF}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
147
Exponential/Exponential.vcxproj
Normal file
147
Exponential/Exponential.vcxproj
Normal file
@ -0,0 +1,147 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{74c04891-9509-4ea4-bc52-6d86492b203a}</ProjectGuid>
|
||||
<RootNamespace>Exponential</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
<GenerateXMLDocumentationFiles>false</GenerateXMLDocumentationFiles>
|
||||
<XMLDocumentationFileName>$(SolutionDir)</XMLDocumentationFileName>
|
||||
<AssemblerOutput>NoListing</AssemblerOutput>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
<GenerateXMLDocumentationFiles>false</GenerateXMLDocumentationFiles>
|
||||
<XMLDocumentationFileName>$(SolutionDir)</XMLDocumentationFileName>
|
||||
<AssemblerOutput>NoListing</AssemblerOutput>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="FunctionsTemplate.h" />
|
||||
<ClInclude Include="Timer.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Source.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
30
Exponential/Exponential.vcxproj.filters
Normal file
30
Exponential/Exponential.vcxproj.filters
Normal file
@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="FunctionsTemplate.h">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Timer.h">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Source.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
475
Exponential/FunctionsTemplate.h
Normal file
475
Exponential/FunctionsTemplate.h
Normal file
@ -0,0 +1,475 @@
|
||||
#pragma once
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
#include <float.h>
|
||||
#include <random>
|
||||
#include <algorithm>
|
||||
#include <exception>
|
||||
|
||||
namespace MATH
|
||||
{
|
||||
constexpr double GA_DEFAULT_MIN_RANGE = -100;
|
||||
constexpr double GA_DEFAULT_MAX_RANGE = 100;
|
||||
constexpr int GA_DEFAULT_NUM_OF_GENERATIONS = 100;
|
||||
constexpr int GA_DEFAULT_SAMPLE_SIZE = 1000;
|
||||
constexpr int GA_DEFAULT_DATA_SIZE = 100000;
|
||||
constexpr double GA_DEFAULT_MUTATION_PERCENTAGE = 0.01;
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard("MATH::ABS(T) returns a value of type T")]] T ABS(const T& n) noexcept
|
||||
{
|
||||
return n < 0 ? n * -1 : n;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard("MATH::NEGATE(T) returns a value of type T")]] T NEGATE(const T& n) noexcept
|
||||
{
|
||||
return n * -1;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard("MATH::POW(T, int) returns a value of type T")]] T POW(const T& n, const int& exp) noexcept
|
||||
{
|
||||
if (exp == 0)
|
||||
return 1;
|
||||
|
||||
T res = n;
|
||||
for (int i = 1; i < exp; i++)
|
||||
{
|
||||
res *= n;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] T SUM(const std::vector<T>& vec) noexcept
|
||||
{
|
||||
T res{};
|
||||
for (auto& val : vec)
|
||||
res += val;
|
||||
return res;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] T MEDIAN(std::vector<T> vec) noexcept
|
||||
{
|
||||
std::sort(
|
||||
vec.begin(),
|
||||
vec.end(),
|
||||
[](const auto& lhs, const auto& rhs){
|
||||
return lhs < rhs;
|
||||
});
|
||||
|
||||
return vec[vec.size() / 2];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
[[nodiscard]] double MEAN(const std::vector<T>& vec) noexcept
|
||||
{
|
||||
return SUM(vec) / vec.size();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void SortASC(std::vector<T>& vec)
|
||||
{
|
||||
std::sort(
|
||||
vec.begin(), vec.end(),
|
||||
[](const auto& lhs, const auto& rhs) {
|
||||
return lhs < rhs;
|
||||
});
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void SortDESC(std::vector<T>& vec)
|
||||
{
|
||||
std::sort(
|
||||
vec.begin(), vec.end(),
|
||||
[](const auto& lhs, const auto& rhs) {
|
||||
return lhs > rhs;
|
||||
});
|
||||
}
|
||||
|
||||
class Coordinate2D
|
||||
{
|
||||
private:
|
||||
double X, Y;
|
||||
|
||||
public:
|
||||
Coordinate2D() : X(0), Y(0) {}
|
||||
Coordinate2D(double x) : X(x), Y(x) {}
|
||||
Coordinate2D(double x, double y) : X(x), Y(y) {}
|
||||
virtual ~Coordinate2D() = default;
|
||||
|
||||
inline void set_x(const double val) noexcept { X = val; }
|
||||
inline void set_y(const double val) noexcept { Y = val; }
|
||||
|
||||
[[nodiscard]] inline double get_x() const noexcept { return X; }
|
||||
[[nodiscard]] inline double get_y() const noexcept { return Y; }
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const Coordinate2D& coord)
|
||||
{
|
||||
os << '(' << coord.X << ", " << coord.Y << ") ";
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
namespace INTERNAL
|
||||
{
|
||||
template <int lrgst_exp> // Genetic Algorithm helper struct
|
||||
struct GA_Solution
|
||||
{
|
||||
double rank, x;
|
||||
|
||||
GA_Solution(double Rank, double x_val) : rank(Rank), x(x_val) {}
|
||||
virtual ~GA_Solution() = default;
|
||||
|
||||
void fitness(const std::vector<int>& constants)
|
||||
{
|
||||
std::vector<bool> exceptions;
|
||||
for (int i : constants)
|
||||
exceptions.push_back(i != 0);
|
||||
|
||||
double ans = 0;
|
||||
for (int i = lrgst_exp; i >= 0; i--)
|
||||
{
|
||||
if (exceptions[i])
|
||||
{
|
||||
ans += constants[i] * POW(x, (lrgst_exp - i));
|
||||
}
|
||||
}
|
||||
rank = (ans == 0) ? DBL_MAX : ABS(1 / ans);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
namespace EXP
|
||||
{
|
||||
template <int lrgst_exp>
|
||||
class Function
|
||||
{
|
||||
private:
|
||||
std::vector<int> constants;
|
||||
|
||||
public:
|
||||
Function(const std::vector<int>& constnts);
|
||||
Function(std::vector<int>&& constnts);
|
||||
Function(const Function& other) = default;
|
||||
Function(Function&& other) noexcept = default;
|
||||
virtual ~Function();
|
||||
|
||||
Function& operator=(const Function& other) = default;
|
||||
Function& operator=(Function&& other) noexcept = default;
|
||||
|
||||
[[nodiscard("MATH::EXP::Function::differential() returns the differential, the calling object is not changed")]]
|
||||
Function<lrgst_exp - 1> differential() const; // This function returns the differential (dy/dx) of the Function object
|
||||
|
||||
// Operator function to display function object in a human readable format
|
||||
friend std::ostream& operator<<(std::ostream& os, const Function<lrgst_exp> func)
|
||||
{
|
||||
if (lrgst_exp == 0)
|
||||
{
|
||||
os << func.constants[0];
|
||||
return os;
|
||||
}
|
||||
|
||||
if (func.constants[0] == 1)
|
||||
os << "x";
|
||||
else if (func.constants[0] == -1)
|
||||
os << "-x";
|
||||
else
|
||||
os << func.constants[0] << "x";
|
||||
|
||||
if (lrgst_exp != 1)
|
||||
os << "^" << lrgst_exp;
|
||||
|
||||
for (int i = lrgst_exp - 1; i > 0; i--)
|
||||
{
|
||||
int n = func.constants[lrgst_exp - i];
|
||||
if (n == 0) continue;
|
||||
|
||||
auto s = n > 0 ? " + " : " - ";
|
||||
|
||||
if (n != 1)
|
||||
os << s << ABS(n) << "x";
|
||||
else
|
||||
os << s << "x";
|
||||
|
||||
if (i != 1)
|
||||
os << "^" << i;
|
||||
}
|
||||
|
||||
int n = func.constants[lrgst_exp];
|
||||
if (n == 0) return os;
|
||||
|
||||
auto s = n > 0 ? " + " : " - ";
|
||||
os << s;
|
||||
|
||||
os << ABS(n);
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
// Speicialty function to get the real roots of a Quadratic Function without relying on a Genetic Algorithm to approximate
|
||||
friend std::vector<double> QuadraticSolve(const Function<2>& f);
|
||||
|
||||
template<int e1, int e2, int r>
|
||||
friend Function<r> operator+(const Function<e1>& f1, const Function<e2>& f2); // Operator to add two functions
|
||||
template<int e1, int e2, int r>
|
||||
friend Function<r> operator-(const Function<e1>& f1, const Function<e2>& f2); // Operator to subtract two functions
|
||||
|
||||
// Operators to multiply a function by a constant (Scaling it)
|
||||
friend Function<lrgst_exp> operator*(const Function<lrgst_exp>& f, const int& c)
|
||||
{
|
||||
if (c == 1) return f;
|
||||
if (c == 0) throw std::logic_error("Cannot multiply a function by 0");
|
||||
|
||||
std::vector<int> res;
|
||||
for (auto& val : f.constants)
|
||||
res.push_back(c * val);
|
||||
|
||||
return Function<lrgst_exp>(res);
|
||||
}
|
||||
Function<lrgst_exp>& operator*=(const int& c)
|
||||
{
|
||||
for (auto& val : this->constants)
|
||||
val *= c;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Function that uses a genetic algorithm to find the approximate roots of the function
|
||||
[[nodiscard]] std::vector<double> get_real_roots_ga(
|
||||
const double& min_range = GA_DEFAULT_MIN_RANGE,
|
||||
const double& max_range = GA_DEFAULT_MAX_RANGE,
|
||||
const int& num_of_generations = GA_DEFAULT_NUM_OF_GENERATIONS,
|
||||
const int& sample_size = GA_DEFAULT_SAMPLE_SIZE,
|
||||
const int& data_size = GA_DEFAULT_DATA_SIZE,
|
||||
const double& mutation_percentage = GA_DEFAULT_MUTATION_PERCENTAGE) const;
|
||||
|
||||
// Function that returns the y-intercept of the function i.e. where x = 0
|
||||
[[nodiscard]] inline Coordinate2D get_y_intrcpt() const noexcept { return Coordinate2D{ 0, (double)constants[lrgst_exp] }; }
|
||||
|
||||
};
|
||||
|
||||
template <int lrgst_exp>
|
||||
Function<lrgst_exp>::Function(const std::vector<int>& constnts)
|
||||
{
|
||||
if (lrgst_exp < 0)
|
||||
throw std::logic_error("Function template argument must not be less than 0");
|
||||
|
||||
if (constnts.size() != lrgst_exp + 1)
|
||||
throw std::logic_error("Function<n> must be created with (n+1) integers in vector object");
|
||||
|
||||
if (constnts[0] == 0)
|
||||
throw std::logic_error("First value should not be 0");
|
||||
|
||||
constants = constnts;
|
||||
}
|
||||
|
||||
template<int lrgst_exp>
|
||||
Function<lrgst_exp>::Function(std::vector<int>&& constnts)
|
||||
{
|
||||
if (lrgst_exp < 0)
|
||||
throw std::logic_error("Function template argument must not be less than 0");
|
||||
|
||||
if (constnts.size() != lrgst_exp + 1)
|
||||
throw std::logic_error("Function<n> must be created with (n+1) integers in vector object");
|
||||
|
||||
if (constnts[0] == 0)
|
||||
throw std::logic_error("First value should not be 0");
|
||||
|
||||
constants = std::move(constnts);
|
||||
}
|
||||
|
||||
template <int lrgst_exp>
|
||||
Function<lrgst_exp>::~Function()
|
||||
{
|
||||
constants.clear();
|
||||
}
|
||||
|
||||
template <int lrgst_exp>
|
||||
Function<lrgst_exp - 1> Function<lrgst_exp>::differential() const
|
||||
{
|
||||
if (lrgst_exp == 0)
|
||||
throw std::logic_error("Cannot differentiate a number (Function<0>)");
|
||||
|
||||
std::vector<int> result;
|
||||
for (int i = 0; i < lrgst_exp; i++)
|
||||
{
|
||||
result.push_back(constants[i] * (lrgst_exp - i));
|
||||
}
|
||||
|
||||
return Function<lrgst_exp - 1>{result};
|
||||
}
|
||||
|
||||
template<int lrgst_exp>
|
||||
std::vector<double> Function<lrgst_exp>::get_real_roots_ga(
|
||||
const double& min_range, const double& max_range,
|
||||
const int& num_of_generations,
|
||||
const int& sample_size, const int& data_size,
|
||||
const double& mutation_percentage) const
|
||||
{
|
||||
// Create initial random solutions
|
||||
std::random_device device;
|
||||
std::uniform_real_distribution<double> unif(static_cast<double>(min_range), static_cast<double>(max_range));
|
||||
std::vector<INTERNAL::GA_Solution<lrgst_exp>> solutions;
|
||||
|
||||
for (int i = 0; i < sample_size; i++)
|
||||
solutions.push_back(INTERNAL::GA_Solution<lrgst_exp>{0, unif(device)});
|
||||
|
||||
for(int count = 0; count < num_of_generations; count++)
|
||||
{
|
||||
for (int i = sample_size; i < data_size; i++)
|
||||
solutions.push_back(INTERNAL::GA_Solution<lrgst_exp>{0, unif(device)});
|
||||
|
||||
// Run our fitness function
|
||||
for (auto& s : solutions) { s.fitness(constants); }
|
||||
|
||||
// Sort our solutions by rank
|
||||
std::sort(solutions.begin(), solutions.end(),
|
||||
[](const auto& lhs, const auto& rhs) {
|
||||
return lhs.rank > rhs.rank;
|
||||
});
|
||||
|
||||
// Take top solutions
|
||||
std::vector<INTERNAL::GA_Solution<lrgst_exp>> sample;
|
||||
std::copy(
|
||||
solutions.begin(),
|
||||
solutions.begin() + sample_size,
|
||||
std::back_inserter(sample)
|
||||
);
|
||||
solutions.clear();
|
||||
|
||||
if (count + 1 == num_of_generations)
|
||||
{
|
||||
std::copy(
|
||||
sample.begin(),
|
||||
sample.end(),
|
||||
std::back_inserter(solutions)
|
||||
);
|
||||
sample.clear();
|
||||
break;
|
||||
}
|
||||
|
||||
// Mutate the top solutions by %
|
||||
std::uniform_real_distribution<double> m((1 - mutation_percentage), (1 + mutation_percentage));
|
||||
std::for_each(sample.begin(), sample.end(), [&m, &device](auto& s) {
|
||||
s.x *= m(device);
|
||||
});
|
||||
|
||||
// Cross over not needed as it's only one value
|
||||
|
||||
std::copy(
|
||||
sample.begin(),
|
||||
sample.end(),
|
||||
std::back_inserter(solutions)
|
||||
);
|
||||
sample.clear();
|
||||
}
|
||||
|
||||
std::sort(solutions.begin(), solutions.end(),
|
||||
[](const auto& lhs, const auto& rhs) {
|
||||
return lhs.x < rhs.x;
|
||||
});
|
||||
|
||||
std::vector<double> ans;
|
||||
for (auto& s : solutions)
|
||||
{
|
||||
ans.push_back(s.x);
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
std::vector<double> QuadraticSolve(const Function<2>& f)
|
||||
{
|
||||
std::vector<double> res;
|
||||
|
||||
const int& a = f.constants[0];
|
||||
const int& b = f.constants[1];
|
||||
const int& c = f.constants[2];
|
||||
|
||||
const double sqr_val = static_cast<double>(POW(b, 2) - (4 * a * c));
|
||||
|
||||
if (sqr_val < 0)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
const double root1 = (NEGATE(b) + sqrt(sqr_val)) / 2 * a;
|
||||
const double root2 = (NEGATE(b) - sqrt(sqr_val)) / 2 * a;
|
||||
|
||||
res.push_back(root1);
|
||||
res.push_back(root2);
|
||||
return res;
|
||||
}
|
||||
|
||||
template<int e1, int e2, int r = (e1 > e2 ? e1 : e2)>
|
||||
Function<r> operator+(const Function<e1>& f1, const Function<e2>& f2)
|
||||
{
|
||||
std::vector<int> res;
|
||||
if (e1 > e2)
|
||||
{
|
||||
for (auto& val : f1.constants)
|
||||
res.push_back(val);
|
||||
|
||||
int i = e1 - e2;
|
||||
for (auto& val : f2.constants)
|
||||
{
|
||||
res[i] += val;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto& val : f2.constants)
|
||||
res.push_back(val);
|
||||
|
||||
int i = e2 - e1;
|
||||
for (auto& val : f1.constants)
|
||||
{
|
||||
res[i] += val;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return Function<r>{res};
|
||||
}
|
||||
|
||||
template<int e1, int e2, int r = (e1 > e2 ? e1 : e2)>
|
||||
Function<r> operator-(const Function<e1>& f1, const Function<e2>& f2)
|
||||
{
|
||||
std::vector<int> res;
|
||||
if (e1 > e2)
|
||||
{
|
||||
for (auto& val : f1.constants)
|
||||
res.push_back(val);
|
||||
|
||||
int i = e1 - e2;
|
||||
for (auto& val : f2.constants)
|
||||
{
|
||||
res[i] -= val;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto& val : f2.constants)
|
||||
res.push_back(val);
|
||||
|
||||
int i = e2 - e1;
|
||||
|
||||
for (int j = 0; j < i; j++)
|
||||
res[j] *= -1;
|
||||
|
||||
for (auto& val : f1.constants)
|
||||
{
|
||||
res[i] = val - res[i];
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return Function<r>{res};
|
||||
}
|
||||
}
|
||||
}
|
45
Exponential/Source.cpp
Normal file
45
Exponential/Source.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include "FunctionsTemplate.h"
|
||||
#include "Timer.h"
|
||||
|
||||
template <int n>
|
||||
using Function = MATH::EXP::Function<n>;
|
||||
|
||||
typedef TIMER::Timer timer;
|
||||
|
||||
int main()
|
||||
{
|
||||
std::vector<int> vec{ 1, 5, 4 };
|
||||
Function<2> f{ vec };
|
||||
Function<3> g{ { 1, -6, 11, -6 } };
|
||||
|
||||
auto res = f * 2;
|
||||
std::cout << res << '\n';
|
||||
|
||||
timer t;
|
||||
|
||||
for (int i = 11; i < 2; i++)
|
||||
{
|
||||
t.Reset();
|
||||
auto gr = g.get_real_roots_ga(-100, 100, i, 1000, 100000, 0.005);
|
||||
t.SetEnd();
|
||||
|
||||
std::cout << "Time took: " << t.GetTimeInS() << "s\n";
|
||||
|
||||
|
||||
std::for_each(gr.begin(), gr.end(),
|
||||
[](const auto& val) {
|
||||
std::cout << "x:" << val << '\n';
|
||||
});
|
||||
}
|
||||
//std::cout << "Median: " << MATH::MEDIAN(gr) << '\n';
|
||||
//std::cout << "Mean: " << MATH::MEAN(gr) << '\n';
|
||||
|
||||
//std::cout << g << '\n';
|
||||
//std::cout << fr[0] << ", " << fr[1] << '\n';
|
||||
//std::cout << f.get_y_intrcpt() << '\n';
|
||||
//std::cout << f.differential() << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
42
Exponential/Timer.h
Normal file
42
Exponential/Timer.h
Normal file
@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
#include <ostream>
|
||||
#include <chrono>
|
||||
|
||||
namespace TIMER{
|
||||
struct Timer
|
||||
{
|
||||
std::chrono::time_point<std::chrono::steady_clock> start, end;
|
||||
std::chrono::duration<float> duration;
|
||||
|
||||
Timer()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
|
||||
~Timer()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
inline void Reset() noexcept
|
||||
{
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
void SetEnd() noexcept
|
||||
{
|
||||
end = std::chrono::high_resolution_clock::now();
|
||||
duration = end - start;
|
||||
}
|
||||
|
||||
inline float GetTimeInMS() const noexcept
|
||||
{
|
||||
return float(duration.count() * 1000.f);
|
||||
}
|
||||
|
||||
inline float GetTimeInS() const noexcept
|
||||
{
|
||||
return float(duration.count());
|
||||
}
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user