📄 group.cpp
字号:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include "group.h"
#include <time.h>
using namespace std;
Genericgroup::Genericgroup()
{
int i,j;
for (i=0;i<100;i++)
{
for (j=0;j<14;j++)
{
binary[i][j]=0;
}
decimal[i]=0;
}
cross_possibility=0.8;
mutation_possibility=0.005;
end_generation=200;
}
void Genericgroup::get_rand_initial()
{
int i,j;
srand((unsigned)time(NULL));
for (i=0;i<100;i++)
{
for (j=0;j<14;j++)
{
binary[i][j]=rand()%2;
}
}
}
void Genericgroup::binary_to_decimal()
{
int i,j;
int sum;
for (i=0;i<100;i++)
{
sum=0;
for (j=0;j<14;j++)
{
sum=sum+binary[i][j]*(int)pow(2.0,(13-j));
}
decimal[i]=1+sum*8.0/(pow(2.0,14.0)-1);
}
}
void Genericgroup::get_fitness()
{
int i;
double rate;
for (i=0;i<100;i++)
{
rate=(decimal[i]+5*sin(5*decimal[i])+7*cos(2*decimal[i]));
decimal[i]=pow(rate,2);
}
}
double Genericgroup::get_the_sum()
{
int i;
double sum=0;
for (i=0;i<100;i++)
{
sum=sum+decimal[i];
}
return sum;
}
double Genericgroup::get_mutation_possibility()
{
return mutation_possibility;
}
double Genericgroup::get_cross_possibility()
{
return cross_possibility;
}
double Genericgroup::get_end_generation()
{
return end_generation;
}
void Genericgroup::get_choice_result()
{
int i,j,k;
int choose_result[100][14];
double getsum;
double percentage[100];
srand((unsigned)time(NULL));
getsum=get_the_sum();
for (i=0;i<100;i++)
{
percentage[i]=decimal[i]/getsum;
}
for (i=0;i<100;i++)
{
double random;
random=(rand()%10000)/10000.0;
j=0;
do
{
random=random-percentage[j];
if (random<0)
{
for (k=0;k<14;k++)
{
choose_result[i][k]=binary[j][k];
}
}
j=j+1;
}while (random>0);
}
for (i=0;i<100;i++)
{
for (j=0;j<14;j++)
{
binary[i][j]=choose_result[i][j];
}
}
}
void Genericgroup::get_cross_result()
{
int i,j;
double choose;
int place,temp[14];
srand((unsigned)time(NULL));
for (i=0;i<100;i=i+2)
{
choose=(rand()%10000/10000.0);
if (choose<=cross_possibility)
{
place=rand()%10;
for (j=place+1;j<14;j++)
{
temp[j]=binary[i][j];
binary[i][j]=binary[i+1][j];
binary[i+1][j]=temp[j];
}
}
}
}
void Genericgroup::get_mutation_result()
{
int i;
double choose;
srand((unsigned)time(NULL));
for (i=0;i<100;i++)
{
choose=(rand()%10000/10000.0);
if (choose<=mutation_possibility)
{
int place;
place=rand()%10;
binary[i][place]=1-binary[i][place];
}
}
}
double Genericgroup::get_average()
{
int i;
double sum=0;
for (i=0;i<100;i++)
{
sum=sum+decimal[i];
}
return sum/100.0;
}
void Genericgroup::order()
{
int i,j,k;
double max,temp;
for (i=0;i<100;i++)
{
max=decimal[i];
for (j=i;j<100;j++)
{
if (max<decimal[j])
{
max=decimal[j];
temp=decimal[j];
decimal[j]=decimal[i];
decimal[i]=temp;
for (k=0;k<14;k++)
{
temp=binary[j][k];
binary[j][k]=binary[i][k];
binary[i][k]=(int)temp;
}
}
}
}
}
void Genericgroup::output(ofstream& fout1)
{
int i,j;
fout1.setf(ios::showpoint);
fout1.setf(ios::fixed);
fout1.precision(6);
fout1<<"the result of the evolutionary"<<endl;
fout1<<"the possibility of the cross is "<<get_cross_possibility()<<endl;
fout1<<"the possibility of the mutation is "<<get_mutation_possibility()<<endl;
fout1<<"the result of the average is"<<get_average()<<endl;
fout1<<"result of the final step "<<endl<<endl;
for (i=0;i<100;i++)
{
for (j=0;j<14;j++)
{
fout1<<binary[i][j];
}
fout1<<setw(12)<<decimal[i];
fout1<<endl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -