完整代码:
#include <bits/stdc++.h>
#define indiv_per_group (50)
#define probability (60)
#define genmax (100)
using namespace std;
typedef struct indi
{
int x;
char bi[5];
int fx;
bool live;
}INDI;
typedef struct group
{
INDI individuals[indiv_per_group];
INDI best;
int best_gen;
int cur_gen;
}GROUP;
int fx(int x)
{
return x*x*x-60*x*x+900*x+100;
}
GROUP Group;
char *stop;
void init()
{
Group.best.fx = -0xffffffff;
Group.best_gen = 0;
Group.cur_gen = 0;
for(int j = 0; j < indiv_per_group; j++)
{
int t = rand() % 100;
for(int k=0;k<5;k++)
{
if(t>50)
Group.individuals[j].bi[k]=1;
else
Group.individuals[j].bi[k]=0;
}
Group.individuals[j].x = strtol(Group.individuals[j].bi,&stop,2);
Group.individuals[j].live = true;
}
}
void assess()
{
for(int i = 0; i < indiv_per_group; i++)
{
Group.individuals[i].fx = fx(Group.individuals[i].x);
}
}
void choose(int gen)
{
double totalFxValue = 0.0;
int i = 0;
for(i = 0; i < indiv_per_group; i++){
totalFxValue += Group.individuals[i].fx;
}
Group.best.fx = Group.individuals[0].fx;
Group.best.x = Group.individuals[0].x;
double tmp_add = 0;
for(i = 0; i < indiv_per_group; i++){
double t = rand() / double(RAND_MAX);
tmp_add += Group.individuals[i].fx;
double tmp = tmp_add / totalFxValue;
if(tmp >= t){
if(Group.individuals[i].fx > Group.best.fx){
Group.best.fx = Group.individuals[i].fx;
Group.best.x = Group.individuals[i].x;
for(int k=0;k<5;k++)
Group.best.bi[k]=Group.individuals[i].bi[k];
}
continue;
}
else{
Group.individuals[i].live = false;
}
}
if(Group.best.fx > Group.individuals[0].fx)
{
Group.best_gen = gen;
}
}
void cross()
{
int first = 0, second = 0;
for(int j = 0; j < indiv_per_group; j++)
{
if(Group.individuals[j].live == false){
while(1){
while(1){
first = rand() % indiv_per_group;
if(Group.individuals[first].live == true)
break;
}
second = rand() % indiv_per_group;
if(Group.individuals[second].live == true)
break;
}
char str[5];
for(int k=0;k<3;k++)
str[k]=Group.individuals[first].bi[k];
for(int k=3;k<5;k++)
str[k]=Group.individuals[second].bi[k];
int new_x=strtol(str,&stop,2);
int new_fx=fx(new_x);
if(new_fx>Group.individuals[first].fx && new_fx > Group.individuals[second].fx)
{
Group.individuals[j].x = new_x;
Group.individuals[j].fx = new_fx;
for(int k=0;k<5;k++)
Group.individuals[j].bi[k]=str[k];
}
else{
if(Group.individuals[first].fx > Group.individuals[second].fx){
Group.individuals[j].x = Group.individuals[first].x;
Group.individuals[j].fx = Group.individuals[first].fx;
for(int k=0;k<5;k++)
Group.individuals[j].bi[k]=Group.individuals[first].bi[k];
}else{
Group.individuals[j].x = Group.individuals[second].x;
Group.individuals[j].fx = Group.individuals[second].fx;
for(int k=0;k<5;k++)
Group.individuals[j].bi[k]=Group.individuals[second].bi[k];
}
}
Group.individuals[j].live = true;
}
}
return;
}
void mutation_one(int x)
{
int pro = rand() % 100;
if(pro > probability)
return ;
for(int k=0;k<5;k++)
{
int t=rand()%100;
if(t>50)
{
if(Group.individuals[x].bi[k]=='1')
Group.individuals[x].bi[k]='0';
else
Group.individuals[x].bi[k]='1';
}
Group.individuals[x].bi[k]^=1;
}
Group.individuals[x].x=strtol(Group.individuals[x].bi,&stop,2);
Group.individuals[x].fx=fx(Group.individuals[x].x);
if(Group.individuals[x].fx > Group.best.fx)
{
Group.best.fx = Group.individuals[x].fx;
Group.best.x = Group.individuals[x].x;
for(int k=0;k<5;k++)
Group.best.bi[k]=Group.individuals[x].bi[k];
}
}
void mutation()
{
for(int i = 0;i < indiv_per_group; i++)
{
if(Group.individuals[i].live == true){
mutation_one(i);
}
}
}
void sovel()
{
init();
for(int i = 1;i <= genmax; i++)
{
Group.cur_gen = i;
assess();
choose(Group.cur_gen);
cross();
mutation();
}
}
int main()
{
srand(time(0));
sovel();
cout<<Group.best.fx<<endl;
cout<<Group.best.x<<endl;
cout<<Group.best.bi<<endl;
return 0;
}
实验结果: