遗传算法的C++实现
2016-03-20 10:26
387 查看
求解一维映射最大值:
#include <iostream>
#include <cstdlib>
#include <ctime>//for srand,rand
#include <cmath>//for cos,sin,pow
using namespace std;
// const int inf = 0xffffff;
double maxValue = -100000;
double x_final = 0;
//fitness function where maximum to be found
double f(double x)
{
return cos(x)+sin(x);
}
//fitness function value for individual
double f_value(double(*func)(double),int * arr,int& N,double a,double b)
{
double res;
double m=0.0;
for(int j=0; j<N; j++)
{
double k=j;
m+=arr[N-j-1]*pow(2.0,k);
}
double x=a+m*(b-a)/(pow(2.0,N)-1.0);
res=func(x);
return res;
}
//x_value at global maximum
double x_value(int * arr,int& N,double a,double b)
{
double m=0.0;
for(int j=0; j<N; j++)
{
double k=j;
m+=arr[N-j-1]*pow(2.0,k);
}
double x=a+m*(b-a)/(pow(2.0,N)-1.0);
return x;
}
//setup the population(farm)
void setup(int ** farm,int M,int N)
{
srand((unsigned long) time(NULL));
for(int j=0; j<M; j++)
for(int k=0; k<N; k++)
farm[j][k]=rand()%2;
}
//cross two individuals
void crossings(int ** farm,int& M,int& N,double& a,double& b)
{
int K=2;
int ** temp=new int * [K];
for(int i=0; i<K; i++)
temp[i]=new int
;
double res[4];
int r1=rand()%M;
int r2=rand()%M;
//random returns a value between 0 and one less than its parameter
while(r2==r1)
r2=rand()%M;
res[0]=f_value(f,farm[r1],N,a,b);
res[1]=f_value(f,farm[r2],N,a,b);
for(int j=0; j<N; j++)
{
temp[0][j]=farm[r1][j];
temp[1][j]=farm[r2][j];
}
int r3=rand()%(N-2)+1;
for(int j=r3; j<N; j++)
{
temp[0][j]=farm[r2][j];
temp[1][j]=farm[r1][j];
}
res[2]=f_value(f,temp[0],N,a,b);
res[3]=f_value(f,temp[1],N,a,b);
if(res[2]>res[0])
{
for(int j=0; j<N; j++) farm[r1][j]=temp[0][j];
res[0]=res[2];
}
if(res[3]>res[1])
{
for(int j=0; j<N; j++)
farm[r2][j]=temp[1][j];
res[1]=res[3];
}
for(int j=0; j<K; j++)
delete[] temp[j];
delete[] temp;
}
//mutate an individual
void mutate(int ** farm,int& M,int& N,double& a,double& b)
{
double res[2];
int r4=rand()%N;
int r1=rand()%M;
res[0]=f_value(f,farm[r1],N,a,b);
int v1=farm[r1][r4];
if(v1==0)
farm[r1][r4]=1;
if(v1==1)
farm[r1][r4]=0;
double a1=f_value(f,farm[r1],N,a,b);
if(a1<res[0])
farm[r1][r4]=v1;
int r5=rand()%N;
int r2=rand()%M;
res[1]=f_value(f,farm[r2],N,a,b);
int v2=farm[r2][r5];
if(v2==0)
farm[r2][r5]=1;
if(v2==1)
farm[r2][r5]=0;
double a2=f_value(f,farm[r2],N,a,b);
if(a2<res[1])
farm[r2][r5]=v2;
}
int main(void)
{
int M=12;
int N=10;
int ** farm=new int * [M];
for(int i=0; i<M; i++)
{
farm[i]=new int
;
}
setup(farm,M,N);
double a=0.0, b=6.28318;//fanwei
for(int k=0; k<1000; k++)
{
crossings(farm,M,N,a,b);
mutate(farm,M,N,a,b);
}
for(int j=0; j<N; j++)
cout<<"farm[1]["<<j<<"]="
4000
<<farm[1][j]<<endl;
cout<<endl;
for(int j=0; j<M; j++)
{
double ff = f_value(f,farm[j],N,a,b);
double xx = x_value(farm[j],N,a,b);
// cout << ff << "----" << xx << endl;
cout<<"fitness f_value["<<j<<"]="<<ff<<" "<<"x_value["<<j<<"]="<<xx<<endl;
if(ff > maxValue)
{
maxValue = ff;
x_final = xx;
}
// cout << maxValue << "-----" << x_final << endl;
}
cout << "================================================" << endl;
cout << "The max value is " << maxValue << " at x = " << x_final << endl;
for(int j=0; j<M; j++)
delete[] farm[j];
delete[] farm;
return 0;
}
求解二维映射最大值:
#include <iostream>
#include <cstdlib>
#include <ctime>//for srand,rand
#include <cmath>//for cos,sin,pow
using namespace std;
// const int inf = 0xffffff;
double maxValue = -100000;
double x_final = 0;
//fitness function where maximum to be found
double f(double x)
{
return cos(x)+sin(x);
}
//fitness function value for individual
double f_value(double(*func)(double),int * arr,int& N,double a,double b)
{
double res;
double m=0.0;
for(int j=0; j<N; j++)
{
double k=j;
m+=arr[N-j-1]*pow(2.0,k);
}
double x=a+m*(b-a)/(pow(2.0,N)-1.0);
res=func(x);
return res;
}
//x_value at global maximum
double x_value(int * arr,int& N,double a,double b)
{
double m=0.0;
for(int j=0; j<N; j++)
{
double k=j;
m+=arr[N-j-1]*pow(2.0,k);
}
double x=a+m*(b-a)/(pow(2.0,N)-1.0);
return x;
}
//setup the population(farm)
void setup(int ** farm,int M,int N)
{
srand((unsigned long) time(NULL));
for(int j=0; j<M; j++)
for(int k=0; k<N; k++)
farm[j][k]=rand()%2;
}
//cross two individuals
void crossings(int ** farm,int& M,int& N,double& a,double& b)
{
int K=2;
int ** temp=new int * [K];
for(int i=0; i<K; i++)
temp[i]=new int
;
double res[4];
int r1=rand()%M;
int r2=rand()%M;
//random returns a value between 0 and one less than its parameter
while(r2==r1)
r2=rand()%M;
res[0]=f_value(f,farm[r1],N,a,b);
res[1]=f_value(f,farm[r2],N,a,b);
for(int j=0; j<N; j++)
{
temp[0][j]=farm[r1][j];
temp[1][j]=farm[r2][j];
}
int r3=rand()%(N-2)+1;
for(int j=r3; j<N; j++)
{
temp[0][j]=farm[r2][j];
temp[1][j]=farm[r1][j];
}
res[2]=f_value(f,temp[0],N,a,b);
res[3]=f_value(f,temp[1],N,a,b);
if(res[2]>res[0])
{
for(int j=0; j<N; j++) farm[r1][j]=temp[0][j];
res[0]=res[2];
}
if(res[3]>res[1])
{
for(int j=0; j<N; j++)
farm[r2][j]=temp[1][j];
res[1]=res[3];
}
for(int j=0; j<K; j++)
delete[] temp[j];
delete[] temp;
}
//mutate an individual
void mutate(int ** farm,int& M,int& N,double& a,double& b)
{
double res[2];
int r4=rand()%N;
int r1=rand()%M;
res[0]=f_value(f,farm[r1],N,a,b);
int v1=farm[r1][r4];
if(v1==0)
farm[r1][r4]=1;
if(v1==1)
farm[r1][r4]=0;
double a1=f_value(f,farm[r1],N,a,b);
if(a1<res[0])
farm[r1][r4]=v1;
int r5=rand()%N;
int r2=rand()%M;
res[1]=f_value(f,farm[r2],N,a,b);
int v2=farm[r2][r5];
if(v2==0)
farm[r2][r5]=1;
if(v2==1)
farm[r2][r5]=0;
double a2=f_value(f,farm[r2],N,a,b);
if(a2<res[1])
farm[r2][r5]=v2;
}
int main(void)
{
int M=12;
int N=10;
int ** farm=new int * [M];
for(int i=0; i<M; i++)
{
farm[i]=new int
;
}
setup(farm,M,N);
double a=0.0, b=6.28318;//fanwei
for(int k=0; k<1000; k++)
{
crossings(farm,M,N,a,b);
mutate(farm,M,N,a,b);
}
for(int j=0; j<N; j++)
cout<<"farm[1]["<<j<<"]="<<farm[1][j]<<endl;
cout<<endl;
for(int j=0; j<M; j++)
{
double ff = f_value(f,farm[j],N,a,b);
double xx = x_value(farm[j],N,a,b);
// cout << ff << "----" << xx << endl;
cout<<"fitness f_value["<<j<<"]="<<ff<<" "<<"x_value["<<j<<"]="<<xx<<endl;
if(ff > maxValue)
{
maxValue = ff;
x_final = xx;
}
// cout << maxValue << "-----" << x_final << endl;
}
cout << "================================================" << endl;
cout << "The max value is " << maxValue << " at x = " << x_final << endl;
for(int j=0; j<M; j++)
delete[] farm[j];
delete[] farm;
return 0;
}
#include <iostream>
#include <cstdlib>
#include <ctime>//for srand,rand
#include <cmath>//for cos,sin,pow
using namespace std;
// const int inf = 0xffffff;
double maxValue = -100000;
double x_final = 0;
//fitness function where maximum to be found
double f(double x)
{
return cos(x)+sin(x);
}
//fitness function value for individual
double f_value(double(*func)(double),int * arr,int& N,double a,double b)
{
double res;
double m=0.0;
for(int j=0; j<N; j++)
{
double k=j;
m+=arr[N-j-1]*pow(2.0,k);
}
double x=a+m*(b-a)/(pow(2.0,N)-1.0);
res=func(x);
return res;
}
//x_value at global maximum
double x_value(int * arr,int& N,double a,double b)
{
double m=0.0;
for(int j=0; j<N; j++)
{
double k=j;
m+=arr[N-j-1]*pow(2.0,k);
}
double x=a+m*(b-a)/(pow(2.0,N)-1.0);
return x;
}
//setup the population(farm)
void setup(int ** farm,int M,int N)
{
srand((unsigned long) time(NULL));
for(int j=0; j<M; j++)
for(int k=0; k<N; k++)
farm[j][k]=rand()%2;
}
//cross two individuals
void crossings(int ** farm,int& M,int& N,double& a,double& b)
{
int K=2;
int ** temp=new int * [K];
for(int i=0; i<K; i++)
temp[i]=new int
;
double res[4];
int r1=rand()%M;
int r2=rand()%M;
//random returns a value between 0 and one less than its parameter
while(r2==r1)
r2=rand()%M;
res[0]=f_value(f,farm[r1],N,a,b);
res[1]=f_value(f,farm[r2],N,a,b);
for(int j=0; j<N; j++)
{
temp[0][j]=farm[r1][j];
temp[1][j]=farm[r2][j];
}
int r3=rand()%(N-2)+1;
for(int j=r3; j<N; j++)
{
temp[0][j]=farm[r2][j];
temp[1][j]=farm[r1][j];
}
res[2]=f_value(f,temp[0],N,a,b);
res[3]=f_value(f,temp[1],N,a,b);
if(res[2]>res[0])
{
for(int j=0; j<N; j++) farm[r1][j]=temp[0][j];
res[0]=res[2];
}
if(res[3]>res[1])
{
for(int j=0; j<N; j++)
farm[r2][j]=temp[1][j];
res[1]=res[3];
}
for(int j=0; j<K; j++)
delete[] temp[j];
delete[] temp;
}
//mutate an individual
void mutate(int ** farm,int& M,int& N,double& a,double& b)
{
double res[2];
int r4=rand()%N;
int r1=rand()%M;
res[0]=f_value(f,farm[r1],N,a,b);
int v1=farm[r1][r4];
if(v1==0)
farm[r1][r4]=1;
if(v1==1)
farm[r1][r4]=0;
double a1=f_value(f,farm[r1],N,a,b);
if(a1<res[0])
farm[r1][r4]=v1;
int r5=rand()%N;
int r2=rand()%M;
res[1]=f_value(f,farm[r2],N,a,b);
int v2=farm[r2][r5];
if(v2==0)
farm[r2][r5]=1;
if(v2==1)
farm[r2][r5]=0;
double a2=f_value(f,farm[r2],N,a,b);
if(a2<res[1])
farm[r2][r5]=v2;
}
int main(void)
{
int M=12;
int N=10;
int ** farm=new int * [M];
for(int i=0; i<M; i++)
{
farm[i]=new int
;
}
setup(farm,M,N);
double a=0.0, b=6.28318;//fanwei
for(int k=0; k<1000; k++)
{
crossings(farm,M,N,a,b);
mutate(farm,M,N,a,b);
}
for(int j=0; j<N; j++)
cout<<"farm[1]["<<j<<"]="
4000
<<farm[1][j]<<endl;
cout<<endl;
for(int j=0; j<M; j++)
{
double ff = f_value(f,farm[j],N,a,b);
double xx = x_value(farm[j],N,a,b);
// cout << ff << "----" << xx << endl;
cout<<"fitness f_value["<<j<<"]="<<ff<<" "<<"x_value["<<j<<"]="<<xx<<endl;
if(ff > maxValue)
{
maxValue = ff;
x_final = xx;
}
// cout << maxValue << "-----" << x_final << endl;
}
cout << "================================================" << endl;
cout << "The max value is " << maxValue << " at x = " << x_final << endl;
for(int j=0; j<M; j++)
delete[] farm[j];
delete[] farm;
return 0;
}
求解二维映射最大值:
#include <iostream>
#include <cstdlib>
#include <ctime>//for srand,rand
#include <cmath>//for cos,sin,pow
using namespace std;
// const int inf = 0xffffff;
double maxValue = -100000;
double x_final = 0;
//fitness function where maximum to be found
double f(double x)
{
return cos(x)+sin(x);
}
//fitness function value for individual
double f_value(double(*func)(double),int * arr,int& N,double a,double b)
{
double res;
double m=0.0;
for(int j=0; j<N; j++)
{
double k=j;
m+=arr[N-j-1]*pow(2.0,k);
}
double x=a+m*(b-a)/(pow(2.0,N)-1.0);
res=func(x);
return res;
}
//x_value at global maximum
double x_value(int * arr,int& N,double a,double b)
{
double m=0.0;
for(int j=0; j<N; j++)
{
double k=j;
m+=arr[N-j-1]*pow(2.0,k);
}
double x=a+m*(b-a)/(pow(2.0,N)-1.0);
return x;
}
//setup the population(farm)
void setup(int ** farm,int M,int N)
{
srand((unsigned long) time(NULL));
for(int j=0; j<M; j++)
for(int k=0; k<N; k++)
farm[j][k]=rand()%2;
}
//cross two individuals
void crossings(int ** farm,int& M,int& N,double& a,double& b)
{
int K=2;
int ** temp=new int * [K];
for(int i=0; i<K; i++)
temp[i]=new int
;
double res[4];
int r1=rand()%M;
int r2=rand()%M;
//random returns a value between 0 and one less than its parameter
while(r2==r1)
r2=rand()%M;
res[0]=f_value(f,farm[r1],N,a,b);
res[1]=f_value(f,farm[r2],N,a,b);
for(int j=0; j<N; j++)
{
temp[0][j]=farm[r1][j];
temp[1][j]=farm[r2][j];
}
int r3=rand()%(N-2)+1;
for(int j=r3; j<N; j++)
{
temp[0][j]=farm[r2][j];
temp[1][j]=farm[r1][j];
}
res[2]=f_value(f,temp[0],N,a,b);
res[3]=f_value(f,temp[1],N,a,b);
if(res[2]>res[0])
{
for(int j=0; j<N; j++) farm[r1][j]=temp[0][j];
res[0]=res[2];
}
if(res[3]>res[1])
{
for(int j=0; j<N; j++)
farm[r2][j]=temp[1][j];
res[1]=res[3];
}
for(int j=0; j<K; j++)
delete[] temp[j];
delete[] temp;
}
//mutate an individual
void mutate(int ** farm,int& M,int& N,double& a,double& b)
{
double res[2];
int r4=rand()%N;
int r1=rand()%M;
res[0]=f_value(f,farm[r1],N,a,b);
int v1=farm[r1][r4];
if(v1==0)
farm[r1][r4]=1;
if(v1==1)
farm[r1][r4]=0;
double a1=f_value(f,farm[r1],N,a,b);
if(a1<res[0])
farm[r1][r4]=v1;
int r5=rand()%N;
int r2=rand()%M;
res[1]=f_value(f,farm[r2],N,a,b);
int v2=farm[r2][r5];
if(v2==0)
farm[r2][r5]=1;
if(v2==1)
farm[r2][r5]=0;
double a2=f_value(f,farm[r2],N,a,b);
if(a2<res[1])
farm[r2][r5]=v2;
}
int main(void)
{
int M=12;
int N=10;
int ** farm=new int * [M];
for(int i=0; i<M; i++)
{
farm[i]=new int
;
}
setup(farm,M,N);
double a=0.0, b=6.28318;//fanwei
for(int k=0; k<1000; k++)
{
crossings(farm,M,N,a,b);
mutate(farm,M,N,a,b);
}
for(int j=0; j<N; j++)
cout<<"farm[1]["<<j<<"]="<<farm[1][j]<<endl;
cout<<endl;
for(int j=0; j<M; j++)
{
double ff = f_value(f,farm[j],N,a,b);
double xx = x_value(farm[j],N,a,b);
// cout << ff << "----" << xx << endl;
cout<<"fitness f_value["<<j<<"]="<<ff<<" "<<"x_value["<<j<<"]="<<xx<<endl;
if(ff > maxValue)
{
maxValue = ff;
x_final = xx;
}
// cout << maxValue << "-----" << x_final << endl;
}
cout << "================================================" << endl;
cout << "The max value is " << maxValue << " at x = " << x_final << endl;
for(int j=0; j<M; j++)
delete[] farm[j];
delete[] farm;
return 0;
}
相关文章推荐
- C++中++符号的前置和后置的重载格式
- C++中的类成员函数
- c++中int,float,double转string的一个简单方法
- C++类的封装与类库的组成
- 《C++0x漫谈》系列之:Concept, Concept!
- #碰到的小问题#c++中vector<int> 和vector<int>::iterator有什么不同
- 谈谈C++类与主函数的结构安排
- 以OLE方式读写EXCEL的C++类【转载】
- C++进阶—— helper function 的设计与实现
- C++实验2—标准体重
- C语言实现单链表面试题汇总
- C语言32个关键字
- C++有子对象的派生类的构造函数
- C语言32个关键字
- File: f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\winocc.cpp错误
- C++实验2-模拟ATM
- C++派生类的构造函数
- C++离港篇
- c/c++命令行接口
- 对c++11下新增字符编码格式转换函数的简单封装