Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cmath>
- #include <functional>
- #include <vector>
- #include <iostream>
- #include <random>
- #include <iomanip>
- using namespace std;
- double MathFunction(double x, double y) {
- return 1 / sqrt(0.01 + x * x + y * y);
- }
- double randomPoint(int a, int b) {
- random_device rd;
- mt19937 gen(rd());
- uniform_real_distribution<> dis(a, b);
- return dis(gen);
- }
- void generateIndividual(pair<double,double>& individual,int x1,int x2,int y1,int y2){
- individual.first = randomPoint(x1,x2);
- individual.second = randomPoint(y1,y2);
- }
- pair<double, double> generateIndividual(int x1, int x2, int y1, int y2) {
- pair<double, double> individual;
- individual.first = randomPoint(x1, x2);
- individual.second = randomPoint(y1, y2);
- return individual;
- }
- double maxValue(const vector<pair<double, double>>& population) {
- vector<double> result;
- result.reserve(population.size());
- for (auto& i : population) {
- result.push_back(MathFunction(i.first, i.second));
- }
- return *max_element(result.begin(), result.end());
- }
- double averageValue(const vector<pair<double, double>>& population) {
- vector<double> result;
- result.reserve(population.size());
- for (auto& i : population) {
- result.push_back(MathFunction(i.first, i.second));
- }
- double sum = 0;
- for (auto& i : result) {
- sum += i;
- }
- return sum / result.size();
- }
- void print(vector<pair<double, double>> population, size_t i) {
- cout << setw(4) << i << setw(11) << fixed << setprecision(6) << population[0].first << setw(11) << population[0].second << setw(11) << MathFunction(population[0].first, population[0].second) << setw(13) << maxValue(population) << setw(13) << averageValue(population) << endl;
- for (size_t j = 1; j < population.size(); ++j) {
- cout << setw(15) << fixed << setprecision(6) << population[j].first << setw(11) << population[j].second << setw(11) << MathFunction(population[j].first, population[j].second) << endl;
- }
- cout << endl;
- }
- vector<size_t> Roulette(vector<pair<double, double>>& points) {
- double sum = 0;
- for (auto value : points) {
- sum += MathFunction(value.first, value.second);
- }
- vector<size_t> answer;
- for (auto value : points) {
- answer.push_back(MathFunction(value.first, value.second) / sum * 100);
- }
- return answer;
- }
- size_t CheckIndex(vector<size_t>& prob, double p) {
- size_t a = 0;
- for (size_t i = 0; i < prob.size(); i++) {
- a += prob[i];
- if (a > p) return i;
- }
- return -1;
- }
- bool func_sort(pair<double, double> pair1, pair<double, double> pair2) {
- return (MathFunction(pair1.first, pair1.second) > MathFunction(pair2.first, pair2.second));
- }
- vector<pair<double, double>> Crossover(vector<size_t> prob,
- vector<pair<double, double>>& points){
- vector<pair<double, double>> answer;
- for (auto value : points) {
- random_device rd;
- mt19937 gen(rd());
- uniform_real_distribution<double> probability(0, 100);
- auto p = static_cast<double>(100 - probability(gen));
- size_t index = CheckIndex(prob, p);
- if (index != -1) {
- answer.push_back(make_pair(points[index].first, value.second));
- answer.push_back(make_pair(value.first, points[index].second));
- }
- }
- sort(answer.begin(), answer.end(), func_sort);
- answer.resize(4);
- return answer;
- }
- void Mutation(vector<pair<double, double>>& points) {
- random_device rd;
- mt19937 gen(rd());
- uniform_real_distribution<double> probability(0, 100);
- auto p = static_cast<size_t>(probability(gen));
- for (auto value : points) {
- if (p > 25) {
- uniform_real_distribution<double> probability(0, 100);
- auto p = static_cast<size_t>(probability(gen));
- if (p > 50) {
- value.first += 0.1;
- value.second += 0.1;
- }
- else {
- value.first -= 0.1;
- value.second -= 0.1;
- }
- }
- }
- }
- void genetic(int x1, int x2, int y1, int y2) {
- pair<double, double> individual;
- vector<pair<double, double>> population;
- // Генерация начальной популяции (4 рандомных особи)
- for (size_t i = 0; i < 4; ++i) {
- individual = generateIndividual(x1, x2, y1, y2);
- population.push_back(individual);
- }
- cout << setw(4) << "N" << setw(11) << "X" << setw(11) << "Y" << setw(11) << " FIT " << setw(13) << "MAX " << setw(13) << "Average" << endl;
- for (size_t i = 0; i < 10; ++i) {
- print(population, i);
- population = Crossover(Roulette(population), population);
- Mutation(population);
- }
- }
- int main() {
- genetic(-1, 1, -1, 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement