Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- #include <string>
- #include <ctime>
- #include <math.h>
- #include <random>
- #include <chrono>
- using namespace std;
- const int u = 100;
- const int lambda = 5 * u;
- const float pi = 3.14159;
- float random_generator(float a, float b)
- {
- float n = (b - a)*((float)rand() / RAND_MAX) + a;
- return n;
- }
- void generation(float w[][7], int a, int b, const int pop) //generowanie zbioru liczb losowych z podanego przedziału
- {
- for (int i = 0; i < pop; i++)
- {
- for (int j = 0; j < 6; j++)
- {
- if (j < 3)
- {
- w[i][j] = random_generator(a, b);
- }
- else
- {
- w[i][j] = random_generator(0, b);
- }
- }
- }
- }
- void data(float w[101][2]) //przepisanie danych z pliku
- {
- fstream plik;
- plik.open("C:\\Users\\Lenovo\\Desktop\\AIdata17.dat", ios::in);
- if (plik.good() == true)
- {
- while (!plik.eof())
- {
- for (int i = 0; i <= 100; i++)
- {
- for (int j = 0; j <= 1; j++)
- {
- plik >> w[i][j];
- //cout << w[i][j] << "\t";
- }
- //cout << endl;
- }
- }
- plik.close();
- }
- }
- int main() {
- srand(time(NULL));
- float population[u][7] = { 0 };
- float lambda_population[lambda][7] = { 0 };
- float arguments[101][2];
- int random;
- float r,r1,r2;
- float tau1 = 1 / sqrt(2 * 6);
- float tau2 = 1 / sqrt(2 * sqrt(6));
- float error_parents[u] = { 0 };
- float error_offsprings[lambda] = { 0 };
- float merge_pop[u + lambda][7];
- int iteration = 0;
- unsigned seed = chrono::system_clock::now().time_since_epoch().count();
- default_random_engine generator(seed);
- normal_distribution <float> distribution(0,1);
- data(arguments);
- generation(population, -10, 10, u);
- float diff=0;
- float best_parent=1;
- float best_offspring=0;
- //random = rand() % 100;
- //cout<<random<<endl;
- while (abs(best_parent - best_offspring) > 0.00005)
- {
- for (int i = 0; i < u; i++)
- {
- population[i][6] = 0;
- }
- for (int i = 0; i < lambda; i++)
- {
- lambda_population[i][6] = 0;
- }
- iteration++;
- for (int i = 0; i < lambda; i++)
- {
- random = rand() % 100;
- for (int j = 0; j < 6; j++)
- {
- if (j < 3)
- {
- r = distribution(generator);
- lambda_population[i][j] = population[random][j] + population[random][j + 3] * r;
- }
- else
- {
- r1 = distribution(generator);
- r2 = distribution(generator);
- lambda_population[i][j] = population[random][j] * exp(tau1*r1)*exp(tau2*r2);
- //cout << i<<". "<<lambda_population[i][1] << endl;
- }
- }
- }
- //MSE of parents
- for (int i = 0; i < u; i++)
- {
- diff = 0;
- for (int j = 0; j <= 100; j++)
- {
- float temp = 0;
- temp = population[i][0] * (pow(arguments[j][0], 2) - population[i][1] * cos(pi*population[i][2] * arguments[j][0]));
- diff += pow((arguments[i][1] - temp), 2);
- }
- population[i][6] = diff / 101;
- error_parents[i] = diff / 101;
- cout << error_parents[i]<< endl;
- }
- //MSE of offsprings
- for (int i = 0; i < lambda; i++)
- { diff = 0;
- for (int j = 0; j <= 100; j++)
- {
- float temp = 0;
- temp = lambda_population[i][0] * (pow(arguments[j][0], 2) - lambda_population[i][1] * cos(pi*lambda_population[i][2] * arguments[j][0]));
- diff += pow((arguments[i][1] - temp), 2);
- }
- lambda_population[i][6] = diff / 101;
- error_offsprings[i] = diff / 101;
- //cout << i << " " << lambda_population[i][2]<<endl;
- }
- sort(error_parents, error_parents + u);
- sort(error_offsprings, error_offsprings + lambda);
- best_parent = error_parents[0];
- best_offspring = error_offsprings[0];
- //merging of 2 matrices
- for (int i = 0; i < (u+lambda); i++)
- {
- for (int j = 0; j < 7; j++)
- {
- if (i < u)
- merge_pop[i][j] = population[i][j];
- else
- merge_pop[i][j] = lambda_population[i][j];
- }
- }
- float temp[7];
- //sortowanie bąbelkowe - wg kolumny 7
- for (int i = 0; i<(lambda+u); i++) {
- for (int j = i + 1; j<(lambda+u); j++)
- {
- if (merge_pop[j][6] < merge_pop[i][6]) {
- for (int t = 0; t < 7; t++) {
- temp[t] = merge_pop[i][t];
- merge_pop[i][t] = merge_pop[j][t];
- merge_pop[j][t] = temp[t];
- }
- }
- }
- }
- for (int i = 0; i < u; i++)
- {
- for (int j = 0; j < 7; j++)
- {
- population[i][j] = merge_pop[i][j];
- }
- }
- cout << iteration << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement