机器人与人工智能爱好者论坛
标题:
人工智能遗传算法c++实现
[打印本页]
作者:
nicholas
时间:
2015-12-3 00:18
标题:
人工智能遗传算法c++实现
人工智能遗传算法c++实现
#include <iostream.h>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>
#define PI 3.1415926
using namespace std;
class Software{ //软件类及其属性,即价格和运行时间
public :
float price; //价格
float time ; //时间
float adapt; //适应度范围0~1
};
double AverageRandom(double min,double max) { //产生在min与max间的随机数
int minInteger = (int)(min*10000);
int maxInteger = (int)(max*10000);
int randInteger = rand()*rand();
int diffInteger = maxInteger - minInteger;
int resultInteger = randInteger % diffInteger + minInteger;
return resultInteger/10000.0;
}
double Normal(double x,double miu,double sigma) { //概率密度函数
return 1.0/(sqrt(2*PI)*sigma) * exp(-1*(x-miu)*(x-miu)/(2*sigma*sigma));
}
double NormalRandom(double miu, double sigma,double min,double max){ //产生正态分布随机数
double x;
double dScope;
double y;
do {
x = AverageRandom(min,max);
y = Normal(x, miu, sigma);
dScope = AverageRandom(0, Normal(miu,miu,sigma));
}while( dScope > y);
return x;
}
void Init(int type, int n , vector<vector<Software> > &soft){//初始化,产生software样本组
Software temp_s;
vector<Software> temp_v;
for(int i = 0;i<n;i++){
cout<<"class "<<i+1<<endl;
for(int j=0;j<type;j++){
temp_s.price=(int)NormalRandom(0, 0.2,1,10000);
temp_s.time= (int)NormalRandom(0, 0.2,1,10);
temp_s.adapt=2/((temp_s.price/1000)+temp_s.time);
temp_v.push_back(temp_s);
cout<<j+1<<"("<<temp_s.price<<","<<temp_s.time<<","<<temp_s.adapt<<")"<<" ";
}
cout<<endl;
soft.push_back(temp_v);
temp_v.clear();
}
}
float Select(vector<vector<Software> > &soft){//选择
vector<vector<Software> > temp_soft(soft);
float share=0;
float one_share=0;
soft.clear();
for(int num=0;num<temp_soft.size();num++){
for(int type=0;type<temp_soft[num].size();type++){
share+=temp_soft[num][type].adapt;
}
}
for(num=0;num<temp_soft.size();num++){
for(int type=0;type<temp_soft[num].size();type++){
one_share+=temp_soft[num][type].adapt;
}
if(one_share>share/(2*temp_soft.size()))//如果小于两倍适应度平均值就淘汰
soft.push_back(temp_soft[num]);
one_share=0;
}
return share;
}
void Cross(int type,vector<vector<Software> > &soft){ //交叉
int dot=0;
float odds=0;
vector<Software>::iterator iter1,iter2;
for(int num=0;num<soft.size()-1;num++){
odds=rand()%100/100.0;
if(odds>0.6&&odds<0.95){//杂交率设置为0.6~0.95
dot=NormalRandom(0, 0.2,1,type);
iter1=soft[num].begin() + dot-1;
iter2=soft[num+1].begin()+dot-1;
while(iter1!=soft[num].end()){
swap(*iter1,*iter2);
iter1++;
iter2++;
}
}
}
}
void Mutation(int type,vector<vector<Software> > &soft){//变异
float odds=0;
int dot=0;
vector<Software>::iterator iter;
for(int num=0;num<soft.size();num++){
odds=rand()%1000/1000.0;
if(odds>0.001&&odds<0.01){//变异率设置为0.001~0.01
dot=NormalRandom(0, 0.2,1,type);
iter=soft[num].begin() + dot-1;
(*iter).price=(int)NormalRandom(0, 0.2,1,10000);
(*iter).time= (int)NormalRandom(0, 0.2,1,10);
(*iter).adapt=2/(((*iter).price/1000)+(*iter).time);
}
}
}
float Genetic(int type,vector<vector<Software> > &soft){//进行选择,杂交,突变
float share=0;
Select(soft);
int i,j;
Cross(type,soft);
Mutation(type,soft);
for(i = 0;i<soft.size();i++){
for(j=0;j<type;j++){
share+=soft[i][j].adapt;
}
}
return share;
}
int Chose(int type,vector<vector<Software> > &soft){//选择最优组合
int best,i=type;
float old_share=0,new_share=0;
while(i--)
old_share+=soft[0][i].adapt;
best=0;
for(i=1;i<soft.size();i++){
for(int j=0;j<type;j++)
new_share+=soft[i][j].adapt;
if(new_share>old_share){
old_share=new_share;
best=i;
}
new_share=0;
}
return best;
}
int main(void){ //主函数
int type,num,best,times;
float old_share=0,new_share;
srand( (unsigned)time( NULL ) );
cout<<"please put in software's type number"<<endl;
cin>>type;
cout<<"please put in software's number"<<endl;
cin >> num ;
cout<<"please put in Genetic times"<<endl;
cin>>times;
vector<vector<Software> > soft;
Init(type,num,soft);
if(times<100){//小于100代就按代数运行
for(int i=0;i<times;i++)
Genetic(type,soft);
}
else{
int i=0;
while(1){//大于100代就比较前后两代总适应度变化,小于0.01且代数超过1000就停止
new_share=Genetic(type,soft);
if(abs(new_share-old_share)<0.01&&i>1000)
break;
old_share=new_share;
i++;
}
}
best=Chose(type,soft);//选出适应度最优组
cout<<endl<<"After "<<times<<" Genetic select , the soft classes as follows :"<<endl;
for(int i = 0;i<soft.size();i++){
cout<<"class "<<i+1<<endl;
for(int j=0;j<type;j++){
cout<<j+1<<"("<<soft[i][j].price<<","
<<soft[i][j].time<<","
<<soft[i][j].adapt<<")"<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<"the best member is "<<best+1<<" class "<<endl;
for( i=0;i<type;i++)
cout<<i+1<<"("<<soft[best][i].price<<","
<<soft[best][i].time<<","
<<soft[best][i].adapt<<")"<<" ";
cout<<endl;
return 0;
}
复制代码
人工智能遗传算法c 实现.zip
(2 KB, 下载次数: 3)
2015-12-3 00:17 上传
点击文件名下载附件
下载积分: 金钱 -1
欢迎光临 机器人与人工智能爱好者论坛 (http://www.robot-ai.org/)
Powered by Discuz! X3.2