📄 evaluat.cpp
字号:
// Evaluat.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <math.h>
#include <Winbase.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define pi 3.14;
float count(float x,float y);
void Iniliat(int int_array[20][33]);
void Convert(int int_array[20][33],float float_array[40]);
void Evalu(float float_array[40],float eval_value[20]);
void Roullte_wheel(float eval_value[20],int site[20]);
void re_iniliat(int int_array[20][33],int site[20]);
void Crossover(int int_array[20][33],int address);
void Mutation(int int_array[20][33],float & Pm);
float Keep_best(float eval_value[20],int & address);
void main(int argc, char* argv[])
{
float eval_value[20];// store the value of the int array;
float float_array[40]; //store the number which form int convert to float;
int int_array[20][33];
int site[20];
int address=0; //store the site of the max in the array;
int run_time,store_time;
float this_turn_max;
float Pm=0.08;
float x1,x2;
float max_number=0;
srand( (unsigned)time( NULL ) );//iniliat the paramate of the rand;
Iniliat(int_array);
for(run_time=0;run_time<1500;run_time++)
{
Convert(int_array,float_array);
Evalu(float_array,eval_value);
this_turn_max=Keep_best(eval_value,address);
//renew the max in each time;
if(this_turn_max>max_number)
{
max_number=this_turn_max;
x1=float_array[address];
x2=float_array[20+address];
store_time=run_time;
}
Roullte_wheel(eval_value,site);
re_iniliat(int_array,site);
Crossover(int_array,address);
//if((run_time%100)==0)
//{
// Pm=Pm+0.01;
Mutation(int_array,Pm);
//}
}
cout<<"the max value is "<<max_number<<",and result by\n";
cout<<x1<<" and "<<x2<<" in the "<<store_time<<" time!\n";
cout<<"press any key to continue!";
getchar();
}
float count(float x,float y)
{
float sum=0;
double x1,x2;
x1=4*x*pi;
x2=20*y*pi;
sum=x*sin(x1)+y*sin(x2)+21.5;
//cout<<sum;
return sum;
}
void Iniliat(int int_array[20][33])
{
int i,j,k;
for(i=0;i<20;i++)
for(j=0;j<33;j++)
{
k=rand( )%2;
//cout<<k;
int_array[i][j]=k;
}
}
void Convert(int int_array[20][33],float float_array[40])
{
long double sum;
int i,j;
int index=1;
long double counter=0;
int counter2;
for(i=0;i<20;i++)
{
//convert the 18 bits x1 to float;
for(j=15;j<33;j++)
{
counter2=int_array[i][j];
counter=counter+counter2*index;
index=2*index;
}
sum=(-3.0)+(counter/(pow(2,18)-1))*15.11;
float_array[i]=sum;
index=1;
counter=0;
}
index=1;
counter=0;
for(i=0;i<20;i++)
{
//convert 15 bits x2 to float;
for(j=0;j<15;j++)
{
counter2=int_array[i][j];
counter=counter+counter2*index;
index=2*index;
}
sum=4.1+(counter/(pow(2,15)-1))*1.7;
float_array[20+i]=sum;
counter=0;
index=1;
}
//cout<<sum;
}
void Evalu(float float_array[40],float eval_value[20])
{
int i;
float sum;
for(i=0;i<20;i++)
{
//count the value of the eqution;
sum=count(float_array[i],float_array[20+i]);
eval_value[i]=sum;
}
}
void Roullte_wheel(float eval_value[20],int site[20])
{
float sum=0;
float divid=32767;
float k,j,random;
float opportunity_eval[20];
int i,s;
//cout the sum of the eval_value;
for(i=0;i<20;i++)
sum+=eval_value[i]-6;
//cout the statistic of the each eval_value;
j=0;
for(i=0;i<20;i++)
{
k=(eval_value[i]-6)/sum;
j=j+k;
opportunity_eval[i]=j;
}
//run the roullte wheel;
s=0;
for(i=0;i<20;i++)
{
random=(float)(rand()/divid);
while(random>opportunity_eval[s])
s++;
site[i]=s;
s=0;
}
}
float Keep_best(float eval_value[20],int & address)
{
float a=0;
int i;
for(i=0;i<20;i++)
{
if(eval_value[i]>a)
{
a=eval_value[i];
address=i;
}
}
return a;
}
void re_iniliat(int int_array[20][33],int site[20])
{
int new_array[20][33];
int sign;
int i,j;
//iniliat the new_array;
for(i=0;i<20;i++)
for(j=0;j<33;j++)
new_array[i][j]=0;
//input the number which hsa be choosed;
for(i=0;i<20;i++)
{
sign=site[i];
for(j=0;j<33;j++)
new_array[i][j]=int_array[sign][j];
}
//renew the int_array;
for(i=0;i<20;i++)
for(j=0;j<33;j++)
int_array[i][j]=new_array[i][j];
}
void Crossover(int int_array[20][33],int address)
{
int i,j;
int random;
int exchang;
int site1,site2;
for(i=0;i<20;)
{
if((i!=address)&&((i+1)!=address))
{
//crossover the x1;
random=rand();
site1=random%18;
for(j=15;j<(15+site1);j++)
{
exchang=int_array[i][j];
int_array[i][j]=int_array[i+1][j];
int_array[i+1][j]=exchang;
}
//crossover the x2;
random=rand();
site2=random%15;
for(j=0;j<site2;j++)
{
exchang=int_array[i][j];
int_array[i][j]=int_array[i+1][j];
int_array[i+1][j]=exchang;
}
}
i=i+2;
}
}
void Mutation(int int_array[20][33],float & Pm)
{
int i,j;
int random;
float devid=32776;
for(i=0;i<20;i++)
for(j=0;j<33;j++)
{
random=(float)rand()/devid;
if(random<=Pm)
{
if(int_array[i][j]==1)
int_array[i][j]=0;
else
int_array[i][j]=1;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -