Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // diff_alg.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <time.h>
- #include <iomanip>
- #include <iostream>
- #include <random>
- using namespace std;
- #define number_of_variables 1
- #define values 1
- #define amount_number 10
- #define a -5.12
- #define b 5.12
- struct individual
- {
- double x_values[number_of_variables];
- double value_of_fitness_func;
- };
- void init_individuals(individual *indiv_array)
- {
- mt19937 rand_for_gen((unsigned)time(NULL));
- uniform_real_distribution <> dist(a, b);
- int i;
- int j;
- i = 0;
- while (i < amount_number)
- {
- j = 0;
- while (j < number_of_variables)
- {
- indiv_array[i].x_values[j] = dist(rand_for_gen);
- indiv_array[i].value_of_fitness_func = 0;
- j++;
- }
- i++;
- }
- }
- void sphere_model_func(individual *indiv_array)
- {
- int i;
- int j;
- i = 0;
- while (i < amount_number)
- {
- indiv_array[i].value_of_fitness_func = 0;
- j = 0;
- while (j < number_of_variables)
- {
- indiv_array[i].value_of_fitness_func += indiv_array[i].x_values[j] * indiv_array[i].x_values[j];
- j++;
- }
- i++;
- }
- }
- void my_func(individual *indiv_array)
- {
- int i;
- i = 0;
- while (i < amount_number)
- {
- indiv_array[i].value_of_fitness_func = indiv_array[i].x_values[0] * indiv_array[i].x_values[1] * indiv_array[i].x_values[2];
- i++;
- }
- }
- void show_me_individuals(individual *indiv_array)
- {
- int i;
- int j;
- i = 0;
- while (i < amount_number)
- {
- j = 0;
- while (j < number_of_variables)
- {
- cout << j << "Value: ";
- cout << indiv_array[i].x_values[j] << " ";
- cout << "Func value: " << indiv_array[i].value_of_fitness_func << endl;
- j++;
- }
- i++;
- }
- }
- void sort_by_fitness_func(individual *unsorted_ar)
- {
- int i;
- int j;
- individual temp;
- i = 0;
- while (i < amount_number)
- {
- j = 0;
- while (j < amount_number)
- {
- if (unsorted_ar[i].value_of_fitness_func > unsorted_ar[j].value_of_fitness_func)
- j++;
- else
- {
- temp = unsorted_ar[j];
- unsorted_ar[j] = unsorted_ar[i];
- unsorted_ar[i] = temp;
- j++;
- }
- }
- i++;
- }
- }
- void check_childrens(individual *new_chil)
- {
- int i;
- int j;
- i = 0;
- while (i < amount_number)
- {
- j = 0;
- while (j < number_of_variables)
- {
- if (new_chil[i].x_values[j] < a)
- new_chil[i].x_values[j] = a;
- if (new_chil[i].x_values[j] > b)
- new_chil[i].x_values[j] = b;
- j++;
- }
- i++;
- }
- }
- void init_mutant(individual *first, individual *mutants)
- {
- mt19937 rand_for_gen((unsigned)time(NULL));
- uniform_int_distribution <> dist1(0, amount_number - 1);
- double f;
- int i;
- int j;
- int r1;
- int r2;
- int r3;
- i = 0;
- j = 0;
- f = 1.25;
- while (i < amount_number)
- {
- j = 0;
- while (j < number_of_variables)
- {
- r1 = dist1(rand_for_gen);
- while (r1 == i)
- r1 = dist1(rand_for_gen);
- r2 = dist1(rand_for_gen);
- while (r2 == i || r2 == r1)
- r2 = dist1(rand_for_gen);
- r3 = dist1(rand_for_gen);
- while (r3 == i || r3 == r1 || r3 == r2)
- r3 = dist1(rand_for_gen);
- mutants[i].x_values[j] = first[r1].x_values[j] + f * (first[r2].x_values[j] - first[r3].x_values[j]);
- j++;
- }
- i++;
- }
- check_childrens(mutants);
- //my_func(mutants);
- sphere_model_func(mutants);
- }
- void init_first_again(individual *first, individual *last)
- {
- int i;
- int j;
- i = 0;
- while (i < amount_number)
- {
- j = 0;
- while (j < number_of_variables)
- {
- first[i].x_values[j] = last[i].x_values[j];
- j++;
- }
- first[i].value_of_fitness_func = last[i].value_of_fitness_func;
- i++;
- }
- }
- void find_best(individual *first, individual *temp_array, individual *last)
- {
- int i;
- int j;
- i = 0;
- while (i < amount_number)
- {
- j = 0;
- while (j < number_of_variables)
- {
- if (temp_array[i].value_of_fitness_func <= first[i].value_of_fitness_func)
- last[i].x_values[j] = temp_array[i].x_values[j];
- else
- last[i].x_values[j] = first[i].x_values[j];
- j++;
- }
- i++;
- }
- //my_func(last);
- sphere_model_func(last);
- }
- void make_one_great_evil(individual *first, individual *mutants, individual *last)
- {
- mt19937 rand_for_gen((unsigned)time(NULL));
- uniform_int_distribution <> dist1(0, number_of_variables - 1);
- uniform_real_distribution <> dist2(0, 1);
- individual t_array[amount_number];
- double cr;
- double rand_num;
- int i;
- int j;
- int any_number;
- cr = 0.2;
- i = 0;
- while (i < amount_number)
- {
- j = 0;
- while (j < number_of_variables)
- {
- any_number = dist1(rand_for_gen);
- rand_num = dist2(rand_for_gen);
- if (j == any_number || cr >= rand_num)
- t_array[i].x_values[j] = mutants[i].x_values[j];
- else
- t_array[i].x_values[j] = first[i].x_values[j];
- j++;
- }
- i++;
- }
- sphere_model_func(t_array);
- //my_func(t_array);
- find_best(first,t_array, last);
- init_first_again(first, last);
- }
- int main()
- {
- int iter;
- cout << "limits [" << a << ";" << b << "]" << endl;
- individual first[amount_number];
- individual mutant_array[amount_number];
- individual collaborative_array[amount_number];
- iter = 0;
- init_individuals(first);
- sphere_model_func(first);
- //my_func(first);
- show_me_individuals(first);
- cout << endl;
- while (iter < 300)
- {
- init_mutant(first, mutant_array);
- make_one_great_evil(first, mutant_array, collaborative_array);
- iter++;
- }
- cout << endl;
- sort_by_fitness_func(collaborative_array);
- show_me_individuals(collaborative_array);
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement