From e33b4d06d0a87093a04ae8c54580cf5371b20ab9 Mon Sep 17 00:00:00 2001 From: Ainur Date: Sat, 19 Aug 2023 20:34:09 +0300 Subject: [PATCH 01/10] Initial commit --- main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/main.cpp b/main.cpp index 118a02b..2f58bf1 100644 --- a/main.cpp +++ b/main.cpp @@ -228,5 +228,6 @@ int main() { } } + return 0; } \ No newline at end of file From 5f79bc89ad9558c4c12fe3e789f84c57468d1ad2 Mon Sep 17 00:00:00 2001 From: Ainur Date: Sun, 20 Aug 2023 20:53:27 +0300 Subject: [PATCH 02/10] =?UTF-8?q?=D0=9F=D1=80=D0=B5=D0=B4=D0=B2=D0=B0?= =?UTF-8?q?=D1=80=D0=B8=D1=82=D0=B5=D0=BB=D1=8C=D0=BD=D0=BE=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=BD=D0=B5=D1=81=D0=BA=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=B1=D0=B0=D0=B7=D0=BE=D0=B2=D1=8B?= =?UTF-8?q?=D1=85=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CubicEquation.cpp | 41 +++++++++++++++++++++++++++++++ CubicEquation.h | 14 +++++++++++ Entity.cpp | 39 +++++++++++++++++++++++++++++ Entity.h | 18 ++++++++++++++ GenePool.cpp | 29 ++++++++++++++++++++++ GenePool.h | 15 +++++++++++ Genetic-Algorithm.vcxproj | 8 ++++++ Genetic-Algorithm.vcxproj.filters | 20 +++++++++++++++ 8 files changed, 184 insertions(+) create mode 100644 CubicEquation.cpp create mode 100644 CubicEquation.h create mode 100644 Entity.cpp create mode 100644 Entity.h create mode 100644 GenePool.cpp create mode 100644 GenePool.h diff --git a/CubicEquation.cpp b/CubicEquation.cpp new file mode 100644 index 0000000..be2eaa8 --- /dev/null +++ b/CubicEquation.cpp @@ -0,0 +1,41 @@ +#include "CubicEquation.h" +#include + +CubicEquation::CubicEquation(const std::array& coefficients) + : coefficients_(coefficients), roots_{ 0.0, 0.0, 0.0 } {} + +std::array CubicEquation::solve() { + double a = coefficients_[0]; + double b = coefficients_[1]; + double c = coefficients_[2]; + double d = coefficients_[3]; + + // Calculate discriminants and intermediate values + double discriminant = 18 * a * b * c * d - 4 * b * b * b * d + b * b * c * c - 4 * a * c * c * c - 27 * a * a * d * d; + double delta0 = b * b - 3 * a * c; + double delta1 = 2 * b * b * b - 9 * a * b * c + 27 * a * a * d; + + // Calculate roots + if (discriminant > 0) { + double C = cbrt((delta1 + sqrt(discriminant)) / 2.0); + double D = cbrt((delta1 - sqrt(discriminant)) / 2.0); + roots_[0] = (-b + C + D) / (3 * a); + } + else if (discriminant == 0) { + roots_[0] = -b / (3 * a); + } + else { + double phi = acos(delta1 / (2 * sqrt(-delta0 * delta0 * delta0))); + double magnitude = 2 * sqrt(-delta0); + const double pi = 3.14159265358979323846; + roots_[0] = magnitude * cos(phi / 3) - b / (3 * a); + roots_[1] = magnitude * cos((phi + 2 * pi) / 3) - b / (3 * a); + roots_[2] = magnitude * cos((phi + 4 * pi) / 3) - b / (3 * a); + } + + return roots_; +} + +std::array CubicEquation::getRoots() const { + return roots_; +} diff --git a/CubicEquation.h b/CubicEquation.h new file mode 100644 index 0000000..e7690b1 --- /dev/null +++ b/CubicEquation.h @@ -0,0 +1,14 @@ +#pragma once + +#include + +class CubicEquation { +public: + CubicEquation(const std::array& coefficients); + std::array solve(); + std::array getRoots() const; + +private: + std::array coefficients_; + std::array roots_; +}; diff --git a/Entity.cpp b/Entity.cpp new file mode 100644 index 0000000..af51ebe --- /dev/null +++ b/Entity.cpp @@ -0,0 +1,39 @@ +#include "Entity.h" +#include +#include + +Entity::Entity() { + srand(time(NULL)); + for (int i = 0; i < 4; i++) { + coefficients[i] = rand() % 101; + } +} + +Entity::Entity(const std::array& coefficients) { + this->coefficients = coefficients; +} + +std::array Entity::getCoefficients() const { + return coefficients; +} + +void Entity::mutate() { + int geneIndex = rand() % 4; + coefficients[geneIndex] = rand() % 101; +} + +Entity Entity::crossover(const Entity& other) const { + int crossoverPoint = rand() % 4; + std::array newCoefficients; + + for (int i = 0; i < 4; i++) { + if (i < crossoverPoint) { + newCoefficients[i] = coefficients[i]; + } + else { + newCoefficients[i] = other.coefficients[i]; + } + } + + return Entity(newCoefficients); +} diff --git a/Entity.h b/Entity.h new file mode 100644 index 0000000..2448372 --- /dev/null +++ b/Entity.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +class Entity { +public: + Entity(); // + Entity(const std::array& coefficients); // + + // Entity + std::array getCoefficients() const; + void mutate(); + Entity crossover(const Entity& other) const; + +private: + std::array coefficients; +}; + diff --git a/GenePool.cpp b/GenePool.cpp new file mode 100644 index 0000000..6ec2e43 --- /dev/null +++ b/GenePool.cpp @@ -0,0 +1,29 @@ +#include "GenePool.h" +#include +#include +#include +#include + +// GenePool + +// +GenePool::GenePool() { + // + srand(time(NULL)); +} + +// +GenePool::~GenePool() { + // +} + +// +void GenePool::initialize() { + // +} + +// +void GenePool::evolve(int maxGenerations) { + // + // , , +} diff --git a/GenePool.h b/GenePool.h new file mode 100644 index 0000000..7fe4912 --- /dev/null +++ b/GenePool.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +class GenePool { +public: + GenePool(); // + ~GenePool(); // + + void initialize(); // + void evolve(int maxGenerations); // + +private: + // , +}; diff --git a/Genetic-Algorithm.vcxproj b/Genetic-Algorithm.vcxproj index aa45fa0..c78650a 100644 --- a/Genetic-Algorithm.vcxproj +++ b/Genetic-Algorithm.vcxproj @@ -119,11 +119,19 @@ + + + + + + + + diff --git a/Genetic-Algorithm.vcxproj.filters b/Genetic-Algorithm.vcxproj.filters index bc1d5b1..a75cfa3 100644 --- a/Genetic-Algorithm.vcxproj.filters +++ b/Genetic-Algorithm.vcxproj.filters @@ -18,8 +18,28 @@ Source Files + + Source Files + + + Source Files + + + Source Files + + + + Header Files + + + Header Files + + + Header Files + + \ No newline at end of file From 55cc21273ea164aecc99a1c0e4d3aaca3d214382 Mon Sep 17 00:00:00 2001 From: Ainur Date: Sun, 20 Aug 2023 21:43:34 +0300 Subject: [PATCH 03/10] =?UTF-8?q?=D0=97=D0=B0=D0=BC=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=BB=20solveCubicEquation=20=D0=B0=D0=BD=D0=B0=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=D0=BC=20=D0=B8=D0=B7=20=D0=BA=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CubicEquation.cpp | 4 ++++ CubicEquation.h | 1 + main.cpp | 52 +++++++++++------------------------------------ 3 files changed, 17 insertions(+), 40 deletions(-) diff --git a/CubicEquation.cpp b/CubicEquation.cpp index be2eaa8..8a3953d 100644 --- a/CubicEquation.cpp +++ b/CubicEquation.cpp @@ -39,3 +39,7 @@ std::array CubicEquation::solve() { std::array CubicEquation::getRoots() const { return roots_; } + +std::array CubicEquation::getCoefficients() const{ + return coefficients_; +} \ No newline at end of file diff --git a/CubicEquation.h b/CubicEquation.h index e7690b1..da6f36b 100644 --- a/CubicEquation.h +++ b/CubicEquation.h @@ -7,6 +7,7 @@ class CubicEquation { CubicEquation(const std::array& coefficients); std::array solve(); std::array getRoots() const; + std::array getCoefficients() const; private: std::array coefficients_; diff --git a/main.cpp b/main.cpp index 2f58bf1..e0d4146 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,9 @@ #include #include #include +#include "CubicEquation.h" +#include "Entity.h" +#include "GenePool.h" // Вывод числа в формате с отступами void printFormattedNumber(int num) { @@ -44,41 +47,6 @@ std::array, 10> initial_population() { return new_popul; } -// Решение кубического уравнения -std::array solveCubicEquation(const std::array& coefficients) { - double a = coefficients[0]; - double b = coefficients[1]; - double c = coefficients[2]; - double d = coefficients[3]; - - // Calculate discriminants and intermediate values - double discriminant = 18 * a * b * c * d - 4 * b * b * b * d + b * b * c * c - 4 * a * c * c * c - 27 * a * a * d * d; - double delta0 = b * b - 3 * a * c; - double delta1 = 2 * b * b * b - 9 * a * b * c + 27 * a * a * d; - - // Calculate roots - std::array roots; - - if (discriminant > 0) { - double C = cbrt((delta1 + sqrt(discriminant)) / 2.0); - double D = cbrt((delta1 - sqrt(discriminant)) / 2.0); - roots[0] = (-b + C + D) / (3 * a); - } - else if (discriminant == 0) { - roots[0] = -b / (3 * a); - } - else { - double phi = acos(delta1 / (2 * sqrt(-delta0 * delta0 * delta0))); - double magnitude = 2 * sqrt(-delta0); - const double pi = 3.14159265358979323846; // Число π - roots[0] = magnitude * cos(phi / 3) - b / (3 * a); - roots[1] = magnitude * cos((phi + 2 * pi) / 3) - b / (3 * a); - roots[2] = magnitude * cos((phi + 4 * pi) / 3) - b / (3 * a); - } - - return roots; -} - // Оценка приспособленности особей (близость к корням) std::array fitness_evaluation(const std::array, 10>& population, const std::array& target_roots) { @@ -88,7 +56,8 @@ std::array fitness_evaluation(const std::array, 1 double total_distance = 0.0; for (int j = 0; j < 3; j++) { - std::array roots = solveCubicEquation(population[i]); + CubicEquation entityI(population[i]); + std::array roots = entityI.solve(); for (int k = 0; k < 3; k++) { double distance = std::abs(roots[k] - target_roots[j]); total_distance += distance; @@ -182,12 +151,13 @@ int main() { std::cin >> coefficients[i]; } - std::array roots = solveCubicEquation(coefficients); // Корни кубического уравнения - - std::cout << std::endl << "Cubic Equation: " << coefficients[0] << "x^3 + " << coefficients[1] << "x^2 + " << coefficients[2] << "x + " << coefficients[3] << " = 0" << std::endl; + CubicEquation equation(coefficients); + equation.solve(); + + std::array roots = equation.getRoots(); std::cout << "Equation Roots: " << roots[0] << ", " << roots[1] << ", " << roots[2] << std::endl; int Generation_Number = 1; @@ -221,7 +191,9 @@ int main() { std::cout << "Cubic Equation: " << bestCoefficients[0] << "x^3 + " << bestCoefficients[1] << "x^2 + " << bestCoefficients[2] << "x + " << bestCoefficients[3] << " = 0" << std::endl; - std::array bestRoots = solveCubicEquation(bestCoefficients); // Корни уравнения + CubicEquation bestEntity(bestCoefficients); + ; + std::array bestRoots = bestEntity.solve(); // Корни уравнения std::cout << "Equation Roots: " << bestRoots[0] << ", " << bestRoots[1] << ", " << bestRoots[2] << std::endl; break; // Завершаем цикл после вывода результата From fa8d94fbcc63215455216212984660b15264ec7a Mon Sep 17 00:00:00 2001 From: Ainur Date: Sun, 20 Aug 2023 22:04:21 +0300 Subject: [PATCH 04/10] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8E=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20Population?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GenePool.cpp | 29 ---------------- GenePool.h | 15 -------- Genetic-Algorithm.vcxproj | 4 +-- Genetic-Algorithm.vcxproj.filters | 4 +-- Population.cpp | 57 +++++++++++++++++++++++++++++++ Population.h | 19 +++++++++++ main.cpp | 2 +- 7 files changed, 81 insertions(+), 49 deletions(-) delete mode 100644 GenePool.cpp delete mode 100644 GenePool.h create mode 100644 Population.cpp create mode 100644 Population.h diff --git a/GenePool.cpp b/GenePool.cpp deleted file mode 100644 index 6ec2e43..0000000 --- a/GenePool.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "GenePool.h" -#include -#include -#include -#include - -// GenePool - -// -GenePool::GenePool() { - // - srand(time(NULL)); -} - -// -GenePool::~GenePool() { - // -} - -// -void GenePool::initialize() { - // -} - -// -void GenePool::evolve(int maxGenerations) { - // - // , , -} diff --git a/GenePool.h b/GenePool.h deleted file mode 100644 index 7fe4912..0000000 --- a/GenePool.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include - -class GenePool { -public: - GenePool(); // - ~GenePool(); // - - void initialize(); // - void evolve(int maxGenerations); // - -private: - // , -}; diff --git a/Genetic-Algorithm.vcxproj b/Genetic-Algorithm.vcxproj index c78650a..c063d79 100644 --- a/Genetic-Algorithm.vcxproj +++ b/Genetic-Algorithm.vcxproj @@ -121,7 +121,7 @@ - + @@ -130,7 +130,7 @@ - + diff --git a/Genetic-Algorithm.vcxproj.filters b/Genetic-Algorithm.vcxproj.filters index a75cfa3..fd96223 100644 --- a/Genetic-Algorithm.vcxproj.filters +++ b/Genetic-Algorithm.vcxproj.filters @@ -21,7 +21,7 @@ Source Files - + Source Files @@ -35,7 +35,7 @@ Header Files - + Header Files diff --git a/Population.cpp b/Population.cpp new file mode 100644 index 0000000..c168826 --- /dev/null +++ b/Population.cpp @@ -0,0 +1,57 @@ +#include "Population.h" +#include +#include +#include +#include + +// Population + +// +Population::Population() { + // + srand(time(NULL)); +} + +// +Population::~Population() { + // +} + +// +void Population::initialize() { + // +} + +// +void Population::evolve(int maxGenerations) { + // + // , , +} + +// +void printPopulation(int Generation_Number, std::array, 10> new_popul) { + + std::cout << "\nGeneration " << Generation_Number << std::endl; + std::cout << " Chromosome " << " a b c d " << "\n\n"; + + for (int i = 0; i < 10; i++) { + std::cout << "Individual " << i << " "; + + for (int j = 0; j < 4; j++) { + printFormattedNumber(new_popul[i][j]); + } + + std::cout << std::endl; + } + +} + +// +void printFormattedNumber(int num) { + if (num == 100) + std::cout << num << " "; + else if (num < 10) + std::cout << " " << num << " "; + else if (num >= 10 && num < 100) + std::cout << " " << num << " "; +} \ No newline at end of file diff --git a/Population.h b/Population.h new file mode 100644 index 0000000..2855109 --- /dev/null +++ b/Population.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +class Population { +public: + Population(); // + ~Population(); // + + void initialize(); // + void evolve(int maxGenerations); // + + void printPopulation(int Generation_Number, std::array, 10> new_popul); + + +private: + int Generation_Number; + void printFormattedNumber(int num); +}; diff --git a/main.cpp b/main.cpp index e0d4146..45136ac 100644 --- a/main.cpp +++ b/main.cpp @@ -5,7 +5,7 @@ #include #include "CubicEquation.h" #include "Entity.h" -#include "GenePool.h" +#include "Population.h" // Вывод числа в формате с отступами void printFormattedNumber(int num) { From 34131d12c9b759f015f536315574d7cc3dd801d9 Mon Sep 17 00:00:00 2001 From: Ainur Date: Mon, 21 Aug 2023 11:04:10 +0300 Subject: [PATCH 05/10] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20Populatio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Population.cpp | 29 +++++++++++++++-------------- Population.h | 12 ++++++------ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/Population.cpp b/Population.cpp index c168826..cde1b5b 100644 --- a/Population.cpp +++ b/Population.cpp @@ -4,22 +4,23 @@ #include #include -// Population - -// +// Population::Population() { - // srand(time(NULL)); -} -// -Population::~Population() { - // + generationNumber = 1; + + for (int i = 0; i < 10; i++) { + + for (int j = 0; j < 4; j++) { + population_[i][j] = rand() % 101; + } + } } -// -void Population::initialize() { - // +Population::Population(std::array, 10> population) { + generationNumber = 1; + population_ = population; } // @@ -29,7 +30,7 @@ void Population::evolve(int maxGenerations) { } // -void printPopulation(int Generation_Number, std::array, 10> new_popul) { +void Population::printPopulation(int Generation_Number, std::array, 10> population) { std::cout << "\nGeneration " << Generation_Number << std::endl; std::cout << " Chromosome " << " a b c d " << "\n\n"; @@ -38,7 +39,7 @@ void printPopulation(int Generation_Number, std::array, 10> n std::cout << "Individual " << i << " "; for (int j = 0; j < 4; j++) { - printFormattedNumber(new_popul[i][j]); + printFormattedNumber(population[i][j]); } std::cout << std::endl; @@ -47,7 +48,7 @@ void printPopulation(int Generation_Number, std::array, 10> n } // -void printFormattedNumber(int num) { +void Population::printFormattedNumber(int num) { if (num == 100) std::cout << num << " "; else if (num < 10) diff --git a/Population.h b/Population.h index 2855109..5a2d8c4 100644 --- a/Population.h +++ b/Population.h @@ -4,16 +4,16 @@ class Population { public: - Population(); // - ~Population(); // + // + Population(); + Population(std::array, 10> population); - void initialize(); // void evolve(int maxGenerations); // - - void printPopulation(int Generation_Number, std::array, 10> new_popul); + void printPopulation(int Generation_Number, std::array, 10> population); private: - int Generation_Number; + int generationNumber; + std::array, 10> population_; void printFormattedNumber(int num); }; From e7c91d1238861f07d30bda9badd86828fde28aef Mon Sep 17 00:00:00 2001 From: Ainur Date: Mon, 21 Aug 2023 13:17:43 +0300 Subject: [PATCH 06/10] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=BB=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=BD=D1=8B=D0=B5=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4?= =?UTF-8?q?=D1=8B=20Population=20=D0=B2=20main?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Population.cpp | 10 +++++++--- Population.h | 5 +++-- main.cpp | 47 +++++------------------------------------------ 3 files changed, 15 insertions(+), 47 deletions(-) diff --git a/Population.cpp b/Population.cpp index cde1b5b..0a994bb 100644 --- a/Population.cpp +++ b/Population.cpp @@ -23,6 +23,10 @@ Population::Population(std::array, 10> population) { population_ = population; } +const std::array, 10>& Population::getPopulation() const { + return population_; +} + // void Population::evolve(int maxGenerations) { // @@ -30,16 +34,16 @@ void Population::evolve(int maxGenerations) { } // -void Population::printPopulation(int Generation_Number, std::array, 10> population) { +void Population::printPopulation() { - std::cout << "\nGeneration " << Generation_Number << std::endl; + std::cout << "\nGeneration " << generationNumber << std::endl; std::cout << " Chromosome " << " a b c d " << "\n\n"; for (int i = 0; i < 10; i++) { std::cout << "Individual " << i << " "; for (int j = 0; j < 4; j++) { - printFormattedNumber(population[i][j]); + printFormattedNumber(population_[i][j]); } std::cout << std::endl; diff --git a/Population.h b/Population.h index 5a2d8c4..13856b6 100644 --- a/Population.h +++ b/Population.h @@ -8,9 +8,10 @@ class Population { Population(); Population(std::array, 10> population); - void evolve(int maxGenerations); // - void printPopulation(int Generation_Number, std::array, 10> population); + const std::array, 10>& getPopulation() const; + void printPopulation(); + void evolve(int maxGenerations); // private: int generationNumber; diff --git a/main.cpp b/main.cpp index 45136ac..3e0dee7 100644 --- a/main.cpp +++ b/main.cpp @@ -7,46 +7,6 @@ #include "Entity.h" #include "Population.h" -// Вывод числа в формате с отступами -void printFormattedNumber(int num) { - if (num == 100) - std::cout << num << " "; - else if (num < 10) - std::cout << " " << num << " "; - else if (num >= 10 && num < 100) - std::cout << " " << num << " "; -} - -// Вывод текущей популяции -void printPopulation(int Generation_Number, std::array, 10> new_popul) { - - std::cout << "\nGeneration " << Generation_Number << std::endl; - std::cout << " Chromosome " << " a b c d " << "\n\n"; - - for (int i = 0; i < 10; i++) { - std::cout << "Individual " << i << " "; - - for (int j = 0; j < 4; j++) { - printFormattedNumber(new_popul[i][j]); - } - - std::cout << std::endl; - } -} - -// Генерация начальной популяции с случайными коэффициентами -std::array, 10> initial_population() { - std::array, 10> new_popul; - - for (int i = 0; i < 10; i++) { - for (int j = 0; j < 4; j++) { - new_popul[i][j] = rand() % 101; - } - } - - return new_popul; -} - // Оценка приспособленности особей (близость к корням) std::array fitness_evaluation(const std::array, 10>& population, const std::array& target_roots) { @@ -160,11 +120,14 @@ int main() { std::array roots = equation.getRoots(); std::cout << "Equation Roots: " << roots[0] << ", " << roots[1] << ", " << roots[2] << std::endl; + Population newPopul; + newPopul.printPopulation(); + int Generation_Number = 1; + std::array, 10> new_popul = newPopul.getPopulation(); + std::array sellist; // Массив наиболее приспособленных. - std::array, 10> new_popul = initial_population(); - printPopulation(Generation_Number, new_popul); // Основной цикл генетического алгоритма while (true) { From 246ceaf3fef067aac670f17ca62518be2064e2a4 Mon Sep 17 00:00:00 2001 From: Ainur Date: Mon, 21 Aug 2023 16:40:47 +0300 Subject: [PATCH 07/10] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BD=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20Entity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Entity.cpp | 20 ++++++++++++++++++++ Entity.h | 12 +++++++++--- main.cpp | 12 ++++-------- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Entity.cpp b/Entity.cpp index af51ebe..72803ca 100644 --- a/Entity.cpp +++ b/Entity.cpp @@ -1,22 +1,35 @@ #include "Entity.h" +#include "CubicEquation.h" +#include #include #include Entity::Entity() { srand(time(NULL)); + for (int i = 0; i < 4; i++) { coefficients[i] = rand() % 101; } + + CubicEquation equation(coefficients); + roots = equation.solve(); } Entity::Entity(const std::array& coefficients) { this->coefficients = coefficients; + + CubicEquation equation(coefficients); + roots = equation.solve(); } std::array Entity::getCoefficients() const { return coefficients; } +std::array Entity::getRoots() const { + return roots; +} + void Entity::mutate() { int geneIndex = rand() % 4; coefficients[geneIndex] = rand() % 101; @@ -37,3 +50,10 @@ Entity Entity::crossover(const Entity& other) const { return Entity(newCoefficients); } + +void Entity::printEquation() { + std::cout << std::endl << "Equation: " << coefficients[0] << "x^3 + " + << coefficients[1] << "x^2 + " << coefficients[2] << "x + " << coefficients[3] << " = 0" << std::endl; + + std::cout << "Equation Roots: " << roots[0] << ", " << roots[1] << ", " << roots[2] << std::endl; +} \ No newline at end of file diff --git a/Entity.h b/Entity.h index 2448372..df17ee4 100644 --- a/Entity.h +++ b/Entity.h @@ -4,15 +4,21 @@ class Entity { public: - Entity(); // - Entity(const std::array& coefficients); // + // + Entity(); + Entity(const std::array& coefficients); - // Entity + // std::array getCoefficients() const; + std::array getRoots() const; + + // void mutate(); Entity crossover(const Entity& other) const; + void printEquation(); private: std::array coefficients; + std::array roots; }; diff --git a/main.cpp b/main.cpp index 3e0dee7..cbdf989 100644 --- a/main.cpp +++ b/main.cpp @@ -111,15 +111,11 @@ int main() { std::cin >> coefficients[i]; } - std::cout << std::endl << "Cubic Equation: " << coefficients[0] << "x^3 + " - << coefficients[1] << "x^2 + " << coefficients[2] << "x + " << coefficients[3] << " = 0" << std::endl; - - CubicEquation equation(coefficients); - equation.solve(); - - std::array roots = equation.getRoots(); - std::cout << "Equation Roots: " << roots[0] << ", " << roots[1] << ", " << roots[2] << std::endl; + Entity targetEquation(coefficients); + targetEquation.printEquation(); + std::array roots = targetEquation.getRoots(); + Population newPopul; newPopul.printPopulation(); From e0794efdab6b7c501082bbb6981047c1dbc3009c Mon Sep 17 00:00:00 2001 From: Ainur Date: Wed, 23 Aug 2023 21:42:49 +0300 Subject: [PATCH 08/10] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BB=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20Populatio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Genetic-Algorithm.vcxproj | 2 ++ Genetic-Algorithm.vcxproj.filters | 6 ++++ Population.cpp | 35 +++++++++++++++------ Population.h | 9 ++++-- Selection.cpp | 39 +++++++++++++++++++++++ Selection.h | 18 +++++++++++ main.cpp | 52 ++++--------------------------- 7 files changed, 103 insertions(+), 58 deletions(-) create mode 100644 Selection.cpp create mode 100644 Selection.h diff --git a/Genetic-Algorithm.vcxproj b/Genetic-Algorithm.vcxproj index c063d79..d5253aa 100644 --- a/Genetic-Algorithm.vcxproj +++ b/Genetic-Algorithm.vcxproj @@ -123,6 +123,7 @@ + @@ -131,6 +132,7 @@ + diff --git a/Genetic-Algorithm.vcxproj.filters b/Genetic-Algorithm.vcxproj.filters index fd96223..1e94e47 100644 --- a/Genetic-Algorithm.vcxproj.filters +++ b/Genetic-Algorithm.vcxproj.filters @@ -27,6 +27,9 @@ Source Files + + Source Files + @@ -41,5 +44,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/Population.cpp b/Population.cpp index 0a994bb..cdb4f58 100644 --- a/Population.cpp +++ b/Population.cpp @@ -1,4 +1,5 @@ #include "Population.h" +#include "Entity.h" #include #include #include @@ -6,24 +7,20 @@ // Population::Population() { - srand(time(NULL)); - generationNumber = 1; for (int i = 0; i < 10; i++) { - for (int j = 0; j < 4; j++) { - population_[i][j] = rand() % 101; - } - } + population_[i] = Entity::Entity(); + } } -Population::Population(std::array, 10> population) { +Population::Population(std::array newPopulation) { generationNumber = 1; - population_ = population; + population_ = newPopulation; } -const std::array, 10>& Population::getPopulation() const { +const std::array& Population::getPopulation() const { return population_; } @@ -33,6 +30,26 @@ void Population::evolve(int maxGenerations) { // , , } +// ( ) +std::array Population::calculateFitness(std::array target_roots) { + for (int i = 0; i < 10; i++) { + double total_distance = 0.0; + + for (int j = 0; j < 3; j++) { + std::array roots = population_[i].getRoots(); + + for (int k = 0; k < 3; k++) { + double distance = std::abs(roots[k] - target_roots[j]); + total_distance += distance; + } + } + + fitness[i] = 1.0 / total_distance; + } + + return fitness; +} + // void Population::printPopulation() { diff --git a/Population.h b/Population.h index 13856b6..e46a8d5 100644 --- a/Population.h +++ b/Population.h @@ -1,20 +1,23 @@ #pragma once +#include "Entity.h" #include class Population { public: // Population(); - Population(std::array, 10> population); + Population(std::array newPopulation); - const std::array, 10>& getPopulation() const; + const std::array& getPopulation() const; void printPopulation(); + std::array calculateFitness(std::array targetRoots); void evolve(int maxGenerations); // private: int generationNumber; - std::array, 10> population_; + std::array fitness; + std::array population_; void printFormattedNumber(int num); }; diff --git a/Selection.cpp b/Selection.cpp new file mode 100644 index 0000000..ec2e633 --- /dev/null +++ b/Selection.cpp @@ -0,0 +1,39 @@ +#include "Selection.h" +#include "Entity.h" + +Selection::Selection(std::array& population, std::array fitness) { + fitness_ = fitness; + oldPopul = population; + +} + +// +std::array Selection::select() { + std::array sellist; + + for (int i = 0; i < 5; i++) { + int maxIndex = 0; + double maxFitness = -1.0; + + for (int j = 0; j < 10; j++) { + if (fitness_[j] > maxFitness) { + bool isAlreadySelected = false; + for (int k = 0; k < i; k++) { + if (sellist[k] == j) { + isAlreadySelected = true; + break; + } + } + + if (!isAlreadySelected) { + maxIndex = j; + maxFitness = fitness_[j]; + } + } + } + + sellist[i] = maxIndex; + } + + return sellist; +} \ No newline at end of file diff --git a/Selection.h b/Selection.h new file mode 100644 index 0000000..d3a615f --- /dev/null +++ b/Selection.h @@ -0,0 +1,18 @@ +#pragma once + +#include "Entity.h" +#include + +class Selection +{ +public: + Selection(std::array& population, std::array fitness); + + std::array select(); + +private: + std::array fitness_; + std::array oldPopul; + std::array newPopul; +}; + diff --git a/main.cpp b/main.cpp index cbdf989..7f1dd92 100644 --- a/main.cpp +++ b/main.cpp @@ -7,29 +7,6 @@ #include "Entity.h" #include "Population.h" -// Оценка приспособленности особей (близость к корням) -std::array fitness_evaluation(const std::array, 10>& population, - const std::array& target_roots) { - std::array fitness; - - for (int i = 0; i < 10; i++) { - double total_distance = 0.0; - - for (int j = 0; j < 3; j++) { - CubicEquation entityI(population[i]); - std::array roots = entityI.solve(); - for (int k = 0; k < 3; k++) { - double distance = std::abs(roots[k] - target_roots[j]); - total_distance += distance; - } - } - - fitness[i] = 1.0 / total_distance; - } - - return fitness; -} - // Выбор особей для следующего поколения std::array selection(const std::array& fitness) { std::array sellist; @@ -62,8 +39,8 @@ std::array selection(const std::array& fitness) { } // Создание новой популяции на основе выбранных особей -std::array, 10> createNewPopulation(const std::array, 10>& old_popul, const std::array& sellist) { - std::array, 10> new_popul; +std::array createNewPopulation(const std::array& old_popul, const std::array& sellist) { + std::array new_popul; // Копирование выбранных особей в новую популяцию for (int i = 0; i < 5; i++) { @@ -72,24 +49,7 @@ std::array, 10> createNewPopulation(const std::array, 10> new_popul = newPopul.getPopulation(); + std::array new_popul = newPopul.getPopulation(); std::array sellist; // Массив наиболее приспособленных. @@ -128,7 +88,7 @@ int main() { // Основной цикл генетического алгоритма while (true) { // Оценка приспособленности - std::array fitness_values = fitness_evaluation(new_popul, roots); + std::array fitness_values = newPopul.calculateFitness(roots); // Выбор особей для следующего поколения sellist = selection(fitness_values); @@ -144,7 +104,7 @@ int main() { // После определенного количества поколений, выводим наилучшее уравнение if (Generation_Number == MAX_GENERATIONS) { int bestIndividualIndex = sellist[0]; // Индекс самой приспособленной особи - std::array bestCoefficients = new_popul[bestIndividualIndex]; // Коэффициенты этой особи + std::array bestCoefficients = new_popul[bestIndividualIndex].getCoefficients(); // Коэффициенты этой особи std::cout << "\nBest Equation after " << MAX_GENERATIONS << " Generations:\n"; std::cout << "Cubic Equation: " << bestCoefficients[0] << "x^3 + " From 36d3479a9bb6857f41fc646ca8d1fea73507b24a Mon Sep 17 00:00:00 2001 From: Ainur Date: Thu, 24 Aug 2023 21:57:28 +0300 Subject: [PATCH 09/10] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=BB=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20Populatio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Genetic-Algorithm.vcxproj | 2 -- Genetic-Algorithm.vcxproj.filters | 6 ---- Population.cpp | 52 +++++++++++++++++++++++++-- Population.h | 4 ++- Selection.cpp | 39 --------------------- Selection.h | 18 ---------- main.cpp | 58 ++----------------------------- 7 files changed, 55 insertions(+), 124 deletions(-) delete mode 100644 Selection.cpp delete mode 100644 Selection.h diff --git a/Genetic-Algorithm.vcxproj b/Genetic-Algorithm.vcxproj index d5253aa..c063d79 100644 --- a/Genetic-Algorithm.vcxproj +++ b/Genetic-Algorithm.vcxproj @@ -123,7 +123,6 @@ - @@ -132,7 +131,6 @@ - diff --git a/Genetic-Algorithm.vcxproj.filters b/Genetic-Algorithm.vcxproj.filters index 1e94e47..fd96223 100644 --- a/Genetic-Algorithm.vcxproj.filters +++ b/Genetic-Algorithm.vcxproj.filters @@ -27,9 +27,6 @@ Source Files - - Source Files - @@ -44,8 +41,5 @@ Header Files - - Header Files - \ No newline at end of file diff --git a/Population.cpp b/Population.cpp index cdb4f58..c2115ee 100644 --- a/Population.cpp +++ b/Population.cpp @@ -25,9 +25,24 @@ const std::array& Population::getPopulation() const { } // -void Population::evolve(int maxGenerations) { - // - // , , +std::array Population::evolve() { + std::array new_popul; + + // + for (int i = 0; i < 5; i++) { + new_popul[i] = population_[sellist[i]]; + } + + // () + for (int i = 5; i < 10; i++) { + new_popul[i] = Entity::Entity(); + } + + for (int i = 0; i < 10; i++) { + population_[i] = new_popul[i]; + } + + return population_; } // ( ) @@ -50,6 +65,37 @@ std::array Population::calculateFitness(std::array target return fitness; } +// +std::array Population::selection() { + std::array sellist; + + for (int i = 0; i < 5; i++) { + int maxIndex = 0; + double maxFitness = -1.0; + + for (int j = 0; j < 10; j++) { + if (fitness[j] > maxFitness) { + bool isAlreadySelected = false; + for (int k = 0; k < i; k++) { + if (sellist[k] == j) { + isAlreadySelected = true; + break; + } + } + + if (!isAlreadySelected) { + maxIndex = j; + maxFitness = fitness[j]; + } + } + } + + sellist[i] = maxIndex; + } + + return sellist; +} + // void Population::printPopulation() { diff --git a/Population.h b/Population.h index e46a8d5..cd76def 100644 --- a/Population.h +++ b/Population.h @@ -13,10 +13,12 @@ class Population { void printPopulation(); std::array calculateFitness(std::array targetRoots); - void evolve(int maxGenerations); // + std::array Population::selection(); + std::array Population::evolve(); // private: int generationNumber; + std::array sellist; std::array fitness; std::array population_; void printFormattedNumber(int num); diff --git a/Selection.cpp b/Selection.cpp deleted file mode 100644 index ec2e633..0000000 --- a/Selection.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "Selection.h" -#include "Entity.h" - -Selection::Selection(std::array& population, std::array fitness) { - fitness_ = fitness; - oldPopul = population; - -} - -// -std::array Selection::select() { - std::array sellist; - - for (int i = 0; i < 5; i++) { - int maxIndex = 0; - double maxFitness = -1.0; - - for (int j = 0; j < 10; j++) { - if (fitness_[j] > maxFitness) { - bool isAlreadySelected = false; - for (int k = 0; k < i; k++) { - if (sellist[k] == j) { - isAlreadySelected = true; - break; - } - } - - if (!isAlreadySelected) { - maxIndex = j; - maxFitness = fitness_[j]; - } - } - } - - sellist[i] = maxIndex; - } - - return sellist; -} \ No newline at end of file diff --git a/Selection.h b/Selection.h deleted file mode 100644 index d3a615f..0000000 --- a/Selection.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include "Entity.h" -#include - -class Selection -{ -public: - Selection(std::array& population, std::array fitness); - - std::array select(); - -private: - std::array fitness_; - std::array oldPopul; - std::array newPopul; -}; - diff --git a/main.cpp b/main.cpp index 7f1dd92..170df48 100644 --- a/main.cpp +++ b/main.cpp @@ -7,55 +7,6 @@ #include "Entity.h" #include "Population.h" -// Выбор особей для следующего поколения -std::array selection(const std::array& fitness) { - std::array sellist; - - for (int i = 0; i < 5; i++) { - int maxIndex = 0; - double maxFitness = -1.0; - - for (int j = 0; j < 10; j++) { - if (fitness[j] > maxFitness) { - bool isAlreadySelected = false; - for (int k = 0; k < i; k++) { - if (sellist[k] == j) { - isAlreadySelected = true; - break; - } - } - - if (!isAlreadySelected) { - maxIndex = j; - maxFitness = fitness[j]; - } - } - } - - sellist[i] = maxIndex; - } - - return sellist; -} - -// Создание новой популяции на основе выбранных особей -std::array createNewPopulation(const std::array& old_popul, const std::array& sellist) { - std::array new_popul; - - // Копирование выбранных особей в новую популяцию - for (int i = 0; i < 5; i++) { - new_popul[i] = old_popul[sellist[i]]; - } - - // Генерация новых особей (потомков) через скрещивание - for (int i = 5; i < 10; i++) { - new_popul[i] = Entity::Entity(); - } - - return new_popul; -} - - int main() { srand(time(NULL)); @@ -88,16 +39,13 @@ int main() { // Основной цикл генетического алгоритма while (true) { // Оценка приспособленности - std::array fitness_values = newPopul.calculateFitness(roots); + newPopul.calculateFitness(roots); // Выбор особей для следующего поколения - sellist = selection(fitness_values); - - /*По логике будет 5 прислпособленнейших и генерироваться 5 новых. - Также мутация будет менят некоторыхе хромосомы в некоторых особях, которое будет выбираться рандомом.*/ + sellist = newPopul.selection(); // Создание новой популяции на основе выбранных особей - new_popul = createNewPopulation(new_popul, sellist); + new_popul = newPopul.evolve(); Generation_Number++; From 114ff7589b7035fc0d9a5cc0c6405973b3aa5dbc Mon Sep 17 00:00:00 2001 From: Ainur Date: Sat, 26 Aug 2023 21:17:44 +0300 Subject: [PATCH 10/10] =?UTF-8?q?=D0=A0=D0=B0=D1=81=D0=BF=D1=80=D0=B5?= =?UTF-8?q?=D0=B4=D0=B5=D0=BB=D0=B8=D0=BB=20=D0=BF=D0=BE=20=D0=BF=D0=B0?= =?UTF-8?q?=D0=BF=D0=BA=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Genetic-Algorithm.vcxproj | 12 ++++++------ Genetic-Algorithm.vcxproj.filters | 12 ++++++------ CubicEquation.h => include/CubicEquation.h | 0 Entity.h => include/Entity.h | 2 ++ Population.h => include/Population.h | 1 - main.cpp | 13 +++---------- CubicEquation.cpp => scr/CubicEquation.cpp | 2 +- Entity.cpp => scr/Entity.cpp | 20 ++++++++++++++++++-- Population.cpp => scr/Population.cpp | 21 +++------------------ 9 files changed, 39 insertions(+), 44 deletions(-) rename CubicEquation.h => include/CubicEquation.h (100%) rename Entity.h => include/Entity.h (87%) rename Population.h => include/Population.h (93%) rename CubicEquation.cpp => scr/CubicEquation.cpp (97%) rename Entity.cpp => scr/Entity.cpp (74%) rename Population.cpp => scr/Population.cpp (86%) diff --git a/Genetic-Algorithm.vcxproj b/Genetic-Algorithm.vcxproj index c063d79..3733113 100644 --- a/Genetic-Algorithm.vcxproj +++ b/Genetic-Algorithm.vcxproj @@ -119,18 +119,18 @@ - - - + + + - - - + + + diff --git a/Genetic-Algorithm.vcxproj.filters b/Genetic-Algorithm.vcxproj.filters index fd96223..2c07236 100644 --- a/Genetic-Algorithm.vcxproj.filters +++ b/Genetic-Algorithm.vcxproj.filters @@ -18,13 +18,13 @@ Source Files - + Source Files - + Source Files - + Source Files @@ -32,13 +32,13 @@ - + Header Files - + Header Files - + Header Files diff --git a/CubicEquation.h b/include/CubicEquation.h similarity index 100% rename from CubicEquation.h rename to include/CubicEquation.h diff --git a/Entity.h b/include/Entity.h similarity index 87% rename from Entity.h rename to include/Entity.h index df17ee4..765eeb6 100644 --- a/Entity.h +++ b/include/Entity.h @@ -16,9 +16,11 @@ class Entity { void mutate(); Entity crossover(const Entity& other) const; void printEquation(); + void printEntity(); private: std::array coefficients; std::array roots; + void printFormattedNumber(int num); }; diff --git a/Population.h b/include/Population.h similarity index 93% rename from Population.h rename to include/Population.h index cd76def..6bb9a54 100644 --- a/Population.h +++ b/include/Population.h @@ -21,5 +21,4 @@ class Population { std::array sellist; std::array fitness; std::array population_; - void printFormattedNumber(int num); }; diff --git a/main.cpp b/main.cpp index 170df48..257ed3d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,15 +1,10 @@ #include #include -#include -#include -#include -#include "CubicEquation.h" -#include "Entity.h" -#include "Population.h" +#include "include/CubicEquation.h" +#include "include/Entity.h" +#include "include/Population.h" int main() { - srand(time(NULL)); - const int MAX_GENERATIONS = 100; // Максимальное количество поколений std::cout << "New Genetic Algorithm for Cubic Equation\n\n"; @@ -35,7 +30,6 @@ int main() { std::array sellist; // Массив наиболее приспособленных. - // Основной цикл генетического алгоритма while (true) { // Оценка приспособленности @@ -67,6 +61,5 @@ int main() { } } - return 0; } \ No newline at end of file diff --git a/CubicEquation.cpp b/scr/CubicEquation.cpp similarity index 97% rename from CubicEquation.cpp rename to scr/CubicEquation.cpp index 8a3953d..47ab2c4 100644 --- a/CubicEquation.cpp +++ b/scr/CubicEquation.cpp @@ -1,4 +1,4 @@ -#include "CubicEquation.h" +#include "../include/CubicEquation.h" #include CubicEquation::CubicEquation(const std::array& coefficients) diff --git a/Entity.cpp b/scr/Entity.cpp similarity index 74% rename from Entity.cpp rename to scr/Entity.cpp index 72803ca..2e3594b 100644 --- a/Entity.cpp +++ b/scr/Entity.cpp @@ -1,5 +1,5 @@ -#include "Entity.h" -#include "CubicEquation.h" +#include "../include/Entity.h" +#include "../include/CubicEquation.h" #include #include #include @@ -56,4 +56,20 @@ void Entity::printEquation() { << coefficients[1] << "x^2 + " << coefficients[2] << "x + " << coefficients[3] << " = 0" << std::endl; std::cout << "Equation Roots: " << roots[0] << ", " << roots[1] << ", " << roots[2] << std::endl; +} + +void Entity::printEntity() { + for (int i = 0; i < 4; i++) { + printFormattedNumber(coefficients[i]); + } +} + +// +void Entity::printFormattedNumber(int num) { + if (num == 100) + std::cout << num << " "; + else if (num < 10) + std::cout << " " << num << " "; + else if (num >= 10 && num < 100) + std::cout << " " << num << " "; } \ No newline at end of file diff --git a/Population.cpp b/scr/Population.cpp similarity index 86% rename from Population.cpp rename to scr/Population.cpp index c2115ee..6fe745d 100644 --- a/Population.cpp +++ b/scr/Population.cpp @@ -1,5 +1,5 @@ -#include "Population.h" -#include "Entity.h" +#include "../include/Population.h" +#include "../include/Entity.h" #include #include #include @@ -104,22 +104,7 @@ void Population::printPopulation() { for (int i = 0; i < 10; i++) { std::cout << "Individual " << i << " "; - - for (int j = 0; j < 4; j++) { - printFormattedNumber(population_[i][j]); - } - + population_[i].printEntity(); std::cout << std::endl; } - -} - -// -void Population::printFormattedNumber(int num) { - if (num == 100) - std::cout << num << " "; - else if (num < 10) - std::cout << " " << num << " "; - else if (num >= 10 && num < 100) - std::cout << " " << num << " "; } \ No newline at end of file