遗传算法求函数极值Word下载.docx
《遗传算法求函数极值Word下载.docx》由会员分享,可在线阅读,更多相关《遗传算法求函数极值Word下载.docx(17页珍藏版)》请在冰豆网上搜索。
#include<
iostream>
//#includ<stdio、h>
#include<
stdlib、h>
#include<math、h>
#include<time、h>
#include<float、h>
usingnamespacestd;
#define NUMBER50//种群规模
#define GENE_NUMBER 10000//迭代次数
intUnit[NUMBER][30];
//初始种群
intUnit_choose[NUMBER][30];
//选择、交叉、变异后得种群
intNumber[NUMBER];
//被选择得个体编号
float Fitness[NUMBER];
//适应度
float select_probability[NUMBER];
//选择概率
float accumula_probability[NUMBER];
//积累概率
floatf_max=0、0;
//最大值
floatf_x=0、0;
//最大值对应得自变量
int hwj_coding(int start,int end);
//编码
voidhwj_initial_population(intnum);
//产生初始种群
void hwj_fitness(intnum);
//适应度计算
void hwj_choose();
//选择个体
inthwj_binary_search(intl, intr,float temp);
//查找选择
//void hwj_N_M(int a[],int b[],int N,int M);
//从M个数中选N个不一样得数
voidhwj_cross(int num,float cross);
//交叉后得得到种群
voidhwj_aberrance(intnum,float aberrance);
//变异后得得到得种群
void hwj_max(intnum);
//找到最适应得个体
intmain(){
ﻩintstrat,end;
//区间
intNum;
//编码大小
float cross=0、8;
//交叉概率
floataberrance= 0、04;
//变异概率
int key=1;
cout<
<
"
请输入求解区间:
<endl;
cin>
>
strat>
>end;
ﻩNum=hwj_coding(strat,end);
ﻩcout<
<"Num:
<Num<
endl;
//cout<<
"--------------------------1-----------------"<<endl;
hwj_initial_population(Num);
//cout<
--------------------------2初始种群-----------------"
endl;
/*ﻩfor(inti=0;
i<
NUMBER;
i++){
ﻩfor(int j=0;
j<
Num;
j++){
cout<
Unit[i][j]<
'
';
ﻩﻩ}
ﻩcout<<
endl;
ﻩ}
*/
ﻩwhile(key!
=GENE_NUMBER){
hwj_fitness(Num);
//ﻩcout<
<"--------------------------3适应度-----------------"
<<
endl;
//for(inti=0;
i<
NUMBER;
i++){
//ﻩﻩcout<
Fitness[i]<
//}
hwj_choose();
//ﻩcout<
--------------------------4被选择得个体-----------------"
endl;
/*for(inti=0;
NUMBER;
i++){
ﻩﻩfor(intj=0;
j<
Num;
j++){
cout<<Unit_choose[i][j]<
';
ﻩ}
ﻩcout<
ﻩhwj_cross(Num,cross);
/*ﻩcout<
<"
--------------------------5交叉后得种群-----------------"<
endl;
ﻩfor(inti=0;
NUMBER;
i++){
ﻩﻩfor(intj=0;
Num;
cout<
Unit[i][j]<
'
;
}
ﻩﻩcout<
}
ﻩ*/
ﻩhwj_aberrance(Num,aberrance);
/*cout<
--------------------------6变异后得种群-----------------"<
<endl;
ﻩfor(inti=0;
NUMBER;
i++){
for(intj=0;
cout<<
Unit[i][j]<
<'
';
ﻩ}
cout<
endl;
ﻩ*/
key++;
ﻩhwj_max(Num);
ﻩcout<
最大值就是对应得x值就是:
endl;
cout<
f_x<
endl;
最大值为:
<<f_max<
endl;
ﻩreturn0;
}
inthwj_coding(int start,intend){//种群编码
ﻩfloatprecision;
ﻩinttemp=2;
intsum;
intN=1;
ﻩcout<
请输入精度范围:
ﻩcin>
precision;
ﻩif(precision==0){
ﻩ cout<
对不起精度不能为零:
return0;
else{
ﻩﻩsum=(end-start)/precision;
cout<
sum:
sum<
ﻩﻩwhile(temp<sum){
ﻩﻩﻩtemp*=2;
N++;
ﻩﻩﻩ
ﻩreturn N;
ﻩ}ﻩ
void hwj_initial_population(int num){//生成初始种群
ﻩsrand(time(NULL));
for(inti=0;
NUMBER;
i++){
ﻩfor(intj=0;
num;
Unit[i][j]=rand()%2;
}
voidhwj_fitness(intnum){//计算适应度
float sum;
ﻩinttemp;
ﻩfor(inti=0;
i<NUMBER;
temp=1;
ﻩsum=0、0;
for(intj=num-1;
j>
=0;
j--){
ﻩsum+=Unit[i][j]*temp;
ﻩﻩtemp*=2、0;
ﻩFitness[i]=sum*3/(temp-1、0)-1、0;
//cout<<
Fitness[i];
//ﻩcout<<
"--------------+++++"
;
Fitness[i]=Fitness[i]*sin(10*3、1415926*Fitness[i])+2、0;
//cout<
<Fitness[i]<
int hwj_binary_search(intl,intr,float temp){
for(inti=0;
NUMBER;
i++){
ﻩif(temp<=accumula_probability[i]&&
temp>
accumula_probability[i-1]){
ﻩﻩreturn i;
ﻩreturn -1;
}ﻩ
voidhwj_choose(){//选择个体
floatsum=0、0;
floattemp;
inti;
ﻩfor(i=0;
i<NUMBER;
sum+=Fitness[i];
select_probability[0]=Fitness[0]/sum;
temp=accumula_probability[0]=select_probability[0];
ﻩfor(i=1;
i<NUMBER;
ﻩselect_probability[i]=Fitness[i]/sum;
temp+=select_probability[i];
ﻩaccumula_probability[i]=temp;
//ﻩﻩcout<
accumula_probability[i]<
endl;
for(i=0;
NUMBER;
//ﻩsrand(time(NULL));
ﻩﻩtemp=(rand()%1000000)/1000000、0;
//ﻩﻩcout<
temp;
ﻩNumber[i]=hwj_binary_search(0,NUMBER,temp);
//ﻩcout<
Number[i]<
endl;
ﻩfor(int j=0;
NUMBER;
ﻩﻩﻩUnit_choose[i][j]=Unit[Number[i]][j];
}
/*void hwj_N_M(inta[],intb[],intN,intM){//从M个数中选N个不一样得数
inti=1;
ﻩsrand(time(NULL));
a[0]=rand()%M;
ﻩb[a[0]]=1;
while(i!
=N){
ﻩa[i]=rand()%M;
if(b[a[i]]==0){
i++;
ﻩb[a[i]]=1;
ﻩcout<
a[i]<<endl;
}ﻩ
// cout<
a[i]<
'
b[a[i]]<<
}*/
void hwj_cross(intnum,floatcross){//交叉后得得到种群
intnum_cross=NUMBER*cross;
intk;
//交叉点
inti,j;
if(num_cross%2!
=0){
ﻩnum_cross=num_cross+1;
ﻩ}//需要交叉得个体数
ﻩintcro[NUMBER];
//被交叉得个体编号
ﻩint temp[NUMBER];
//就是否交叉数组一览
for(i=0;
i++){
ﻩcro[i]=-1;
temp[i]=0;
//ﻩhwj_N_M(cro,temp,num_cross,NUMBER);
srand(time(NULL));
cro[0]=rand()%NUMBER;
temp[cro[0]]=1;
ﻩi=1;
while(i!
=num_cross){
ﻩcro[i]=rand()%NUMBER;
if(temp[cro[i]]==0){
temp[cro[i]]=1;
ﻩi++;
ﻩﻩ}
//for(inti=0;
NUMBER;
i++){
//ﻩﻩcout<
<temp[i]<
"<
<cro[i]<<
//}
//cout<<num_cross<
ﻩfor(i=0;
num_cross/2;
ﻩﻩsrand(time(NULL));
ﻩk=rand()%num;
ﻩfor(j=0;
j<num;
j++){
ﻩﻩif(j<
=k){
ﻩﻩﻩUnit[i][j]=Unit_choose[cro[num_cross-i]][j];
Unit[i+num_cross/2][j]=Unit_choose[cro[i]][j];
ﻩﻩ
}
ﻩelse{
ﻩUnit[i][j]=Unit_choose[cro[i]][j];
Unit[i+num_cross/2][j]=Unit_choose[cro[i]][j];
ﻩﻩ
ﻩﻩ}
ﻩ}
ﻩfor(i=0;
NUMBER;
//cout<
<temp[i]<
ﻩﻩﻩif(temp[i]==0){
ﻩfor(j=0;
num;
j++){
ﻩﻩﻩUnit[num_cross][j]=Unit_choose[i][j];
ﻩ
ﻩﻩﻩﻩ}
num_cross++;
ﻩ}
voidhwj_aberrance(intnum,floataberrance){//变异后得得到得种群
intnum_aberrance=NUMBER*aberrance;
//变异得个体数
ﻩintk;
//变异点
ﻩintabe[NUMBER];
//变异得个体编号
ﻩint temp[NUMBER];
//就是否变异数组一览
ﻩinti,j,p;
ﻩfor(i=0;
NUMBER;
i++){
ﻩabe[i]=-1;
ﻩﻩtemp[i]=0;
//hwj_N_M(cro,temp,num_cross,NUMBER);
srand(time(NULL));
abe[0]=rand()%NUMBER;
temp[abe[0]]=1;
i=1;
while(i!
=num_aberrance){
ﻩﻩ abe[i]=rand()%NUMBER;
ﻩ if(temp[abe[i]]==0){
temp[abe[i]]=1;
ﻩﻩi++;
ﻩﻩ}
ﻩ}
ﻩfor(i=0;
i<NUMBER;
ﻩfor(j=0;
num_aberrance;
ﻩﻩﻩif(i==abe[j]){
ﻩﻩk=rand()%num;
ﻩﻩfor(p=0;
p<
num;
p++){
ﻩﻩﻩif(p==k){
ﻩﻩﻩif(Unit[i][p]==1){
ﻩﻩﻩﻩﻩﻩUnit[i][p]==0;
ﻩﻩﻩﻩ}
ﻩﻩﻩﻩelse{
ﻩﻩﻩﻩﻩUnit[i][p]==1;
ﻩﻩﻩ}
ﻩﻩ}
ﻩ}
ﻩﻩ}
voidhwj_max(intnum){
hwj_fitness(num);
//floatmax=0、0;
inti;
int temp=1;
ﻩfloatsum=0、0;
int k;
ﻩfor(i=0;
NUMBER;
//ﻩcout<<
Fitness[i]<
ﻩﻩif(Fitness[i]>
f_max){
ﻩﻩﻩf_max=Fitness[i];
ﻩk=i;
ﻩﻩ}
ﻩfor(intj=num-1;
j--){
ﻩsum+=Unit[k][j]*temp;
ﻩﻩﻩtemp*=2、0;
ﻩf_x=sum*3/(temp-1、0)-1、0;