机器人与人工智能爱好者论坛

 找回密码
 立即注册
查看: 8947|回复: 0
打印 上一主题 下一主题

人工智能遗传算法c++实现

[复制链接]

85

主题

92

帖子

479

积分

中级会员

Rank: 3Rank: 3

积分
479
跳转到指定楼层
楼主
发表于 2015-12-3 00:18:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

人工智能遗传算法c++实现



  1. #include <iostream.h>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <cmath>
  6. #define PI 3.1415926

  7. using namespace std;

  8. class Software{        //软件类及其属性,即价格和运行时间
  9. public :
  10.         float price;        //价格
  11.         float time ;        //时间
  12.         float adapt;        //适应度范围0~1
  13. };

  14. double   AverageRandom(double   min,double   max) {  //产生在min与max间的随机数
  15.         int   minInteger   =   (int)(min*10000);
  16.         int   maxInteger   =   (int)(max*10000);
  17.         int   randInteger   =   rand()*rand();
  18.         int   diffInteger   =   maxInteger   -   minInteger;
  19.         int   resultInteger   =   randInteger   %   diffInteger   +   minInteger;
  20.         return   resultInteger/10000.0;
  21. }

  22. double   Normal(double   x,double   miu,double   sigma)  {  //概率密度函数
  23.         return 1.0/(sqrt(2*PI)*sigma) * exp(-1*(x-miu)*(x-miu)/(2*sigma*sigma));
  24. }

  25. double   NormalRandom(double   miu, double   sigma,double   min,double   max){ //产生正态分布随机数
  26.         double   x;
  27.         double   dScope;
  28.         double   y;
  29.         do {
  30.                 x   =   AverageRandom(min,max);   
  31.                 y   =   Normal(x,   miu,   sigma);
  32.                 dScope   =   AverageRandom(0,   Normal(miu,miu,sigma));
  33.         }while(   dScope   >   y);
  34.         return   x;
  35. }

  36. void Init(int type, int n , vector<vector<Software> > &soft){//初始化,产生software样本组
  37.         Software temp_s;
  38.         vector<Software> temp_v;
  39.         for(int i = 0;i<n;i++){
  40.                 cout<<"class        "<<i+1<<endl;
  41.                 for(int j=0;j<type;j++){
  42.                         temp_s.price=(int)NormalRandom(0, 0.2,1,10000);
  43.                         temp_s.time= (int)NormalRandom(0, 0.2,1,10);
  44.                         temp_s.adapt=2/((temp_s.price/1000)+temp_s.time);
  45.                         temp_v.push_back(temp_s);
  46.                         cout<<j+1<<"("<<temp_s.price<<","<<temp_s.time<<","<<temp_s.adapt<<")"<<"        ";
  47.                 }
  48.                 cout<<endl;
  49.                 soft.push_back(temp_v);
  50.                 temp_v.clear();
  51.         }
  52. }

  53. float Select(vector<vector<Software> > &soft){//选择
  54.         vector<vector<Software> > temp_soft(soft);
  55.         float share=0;
  56.         float one_share=0;
  57.         soft.clear();
  58.         for(int num=0;num<temp_soft.size();num++){
  59.                 for(int type=0;type<temp_soft[num].size();type++){
  60.                         share+=temp_soft[num][type].adapt;
  61.                 }
  62.         }
  63.         for(num=0;num<temp_soft.size();num++){
  64.                 for(int type=0;type<temp_soft[num].size();type++){
  65.                         one_share+=temp_soft[num][type].adapt;
  66.                 }
  67.                 if(one_share>share/(2*temp_soft.size()))//如果小于两倍适应度平均值就淘汰
  68.                         soft.push_back(temp_soft[num]);
  69.                 one_share=0;
  70.         }
  71.         return share;
  72. }

  73. void CROSs(int type,vector<vector<Software> > &soft){ //交叉
  74.         int dot=0;
  75.         float odds=0;
  76.         vector<Software>::iterator iter1,iter2;
  77.         for(int num=0;num<soft.size()-1;num++){
  78.                 odds=rand()%100/100.0;
  79.                 if(odds>0.6&&odds<0.95){//杂交率设置为0.6~0.95
  80.                         dot=NormalRandom(0, 0.2,1,type);
  81.                         iter1=soft[num].begin() + dot-1;
  82.                         iter2=soft[num+1].begin()+dot-1;
  83.                         while(iter1!=soft[num].end()){
  84.                                 swap(*iter1,*iter2);
  85.                                 iter1++;
  86.                                 iter2++;
  87.                         }
  88.                 }
  89.         }
  90. }

  91. void Mutation(int type,vector<vector<Software> >  &soft){//变异
  92.         float odds=0;
  93.         int dot=0;
  94.         vector<Software>::iterator iter;
  95.         for(int num=0;num<soft.size();num++){
  96.                 odds=rand()%1000/1000.0;
  97.                 if(odds>0.001&&odds<0.01){//变异率设置为0.001~0.01
  98.                         dot=NormalRandom(0, 0.2,1,type);
  99.                         iter=soft[num].begin() + dot-1;
  100.                         (*iter).price=(int)NormalRandom(0, 0.2,1,10000);
  101.                         (*iter).time= (int)NormalRandom(0, 0.2,1,10);
  102.                         (*iter).adapt=2/(((*iter).price/1000)+(*iter).time);
  103.                 }
  104.         }
  105. }

  106. float Genetic(int type,vector<vector<Software> > &soft){//进行选择,杂交,突变
  107.         float share=0;
  108.         Select(soft);
  109.         int i,j;
  110.         Cross(type,soft);
  111.         Mutation(type,soft);
  112.                
  113.         for(i = 0;i<soft.size();i++){
  114.                 for(j=0;j<type;j++){
  115.                 share+=soft[i][j].adapt;
  116.                 }
  117.         }
  118.         return share;
  119. }

  120. int Chose(int type,vector<vector<Software> > &soft){//选择最优组合
  121.         int best,i=type;
  122.         float old_share=0,new_share=0;
  123.         while(i--)
  124.                 old_share+=soft[0][i].adapt;
  125.         best=0;
  126.         for(i=1;i<soft.size();i++){
  127.                 for(int j=0;j<type;j++)
  128.                         new_share+=soft[i][j].adapt;
  129.                 if(new_share>old_share){
  130.                         old_share=new_share;
  131.                         best=i;
  132.                 }
  133.                 new_share=0;
  134.         }
  135.         return best;
  136. }

  137. int main(void){        //主函数
  138.         int type,num,best,times;
  139.         float old_share=0,new_share;
  140.         srand( (unsigned)time( NULL ) );
  141.         cout<<"please put in software's type number"<<endl;
  142.         cin>>type;
  143.         cout<<"please put in software's number"<<endl;
  144.         cin >> num ;
  145.         cout<<"please put in Genetic times"<<endl;
  146.         cin>>times;
  147.         vector<vector<Software> > soft;
  148.         Init(type,num,soft);
  149.         if(times<100){//小于100代就按代数运行
  150.                 for(int i=0;i<times;i++)
  151.                         Genetic(type,soft);
  152.         }
  153.         else{
  154.                 int i=0;
  155.                 while(1){//大于100代就比较前后两代总适应度变化,小于0.01且代数超过1000就停止
  156.                 new_share=Genetic(type,soft);
  157.                 if(abs(new_share-old_share)<0.01&&i>1000)
  158.                         break;
  159.                 old_share=new_share;
  160.                 i++;
  161.                 }
  162.         }
  163.         best=Chose(type,soft);//选出适应度最优组
  164.         cout<<endl<<"After "<<times<<" Genetic select , the soft classes as follows :"<<endl;
  165.         for(int i = 0;i<soft.size();i++){
  166.                 cout<<"class        "<<i+1<<endl;
  167.                 for(int j=0;j<type;j++){
  168.                         cout<<j+1<<"("<<soft[i][j].price<<","
  169.                                         <<soft[i][j].time<<","
  170.                                                 <<soft[i][j].adapt<<")"<<"        ";
  171.                 }
  172.                 cout<<endl;
  173.         }
  174.         cout<<endl;
  175.         cout<<"the best member is "<<best+1<<" class "<<endl;
  176.         for( i=0;i<type;i++)
  177.                 cout<<i+1<<"("<<soft[best][i].price<<","
  178.                                 <<soft[best][i].time<<","
  179.                                         <<soft[best][i].adapt<<")"<<"        ";
  180.         cout<<endl;
  181.         return 0;
  182. }
复制代码


人工智能遗传算法c 实现.zip (2 KB, 下载次数: 3)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

QQ|Archiver|手机版|小黑屋|陕ICP备15012670号-1    

GMT+8, 2024-4-28 04:05 , Processed in 0.076869 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表