智能算法-遗传算法


智能算法-遗传算法

参考博客:http://blog.csdn.net/yanguilaiwuwei/article/details/46670805

了解遗传算法,并解决如下问题。已知N个向量,他们属于同一个类,寻找描述此类的"特点向量"P = ( y1,y2,y3,y4,y5 ),使之满足如下目标:累计N个绝对值的cos<Xi,P>最小。(这两天没写题是在搞这个╮(╯▽╰)╭)

遗传算法模拟了自然选择的过程,是一种搜索算法,其一般步骤:

开始循环:

  1.评估每条染色体所对应个体的适应度。

  2.遵照适应度越高,选择概率越大的原则,从种群中选择两个个体作为父方和母方。

  3.抽取父母双方的染色体,进行交叉,产生子代。

  4.对子代的染色体进行变异。

  5.重复2,3,4步骤,直到新种群的产生。

结束循环。

这里没有对浮点数进行编码,而是简单地采用浮点数进行运算,并做了部分优化。

代码如下:

  1 #include<cstdio>
  2 #include<cmath>
  3 #include<ctime>
  4 #include<algorithm>
  5 #include<cstring>
  6 #define EPS 1e-8
  7 #define MAX_X 200
  8 #define SAVE 0.30                         /*保留比例*/
  9 #define MAX_Y 200                         /*种群数量*/
 10 #define MAX_GENS 80000                    /*最大代数*/
 11 #define PXCOVER 0.90                      /*交叉概率*/
 12 #define PMUTATION 0.05                    /*变异概率*/
 13 #define PA 0.01                           /*交叉因子*/
 14 #define PM 0.80                           /*变异因子*/
 15 #define MULTIPLE 1.3                      /*优化倍率*/
 16 #define N 6
 17 using namespace std;
 18 struct Xvector{                           /*向量x*/
 19     double vec[N];
 20     double mod;
 21 };
 22 struct Yvector{                           /*向量y*/
 23     double vec[N];
 24     double mod;
 25     double fitness;
 26     double fitsum;                        /*存储转盘选择值*/
 27 };
 28 Xvector x[MAX_X];                         /*已知n个向量x*/
 29 Yvector y[MAX_Y];
 30 Yvector newy[MAX_Y];
 31 Yvector fity;                             /*适应度最大的y*/
 32 bool index[MAX_Y];
 33 int n;                                    /*向量x的个数*/
 34 int generation;                           /*代数*/
 35 double lower[N],upper[N];
 36 void inputX();
 37 void inputY();
 38 double randval(double lower,double upper);/*给定上下限给出随机值*/
 39 void evaluate();                          /*评价*/
 40 void select();                            /*选择*/
 41 void xcover();                            /*交叉*/
 42 void mutate();                            /*变异*/
 43 void copyy();                             /*复制*/
 44 void findmaxfit();                        /*寻找适应度最大的y*/
 45 bool compare(Yvector a,Yvector b);
 46 int main(void){
 47     srand(time(NULL));
 48     freopen("input.txt","r",stdin);
 49     freopen("output.txt","w",stdout);
 50     inputX();
 51     inputY();
 52     evaluate();
 53     while(generation<=MAX_GENS){
 54         select();
 55         copyy();
 56         xcover();
 57         mutate();
 58         evaluate();
 59         findmaxfit();
 60     }
 61     printf("******************************************************************\n");
 62     printf("种群数量: %d\n",MAX_Y);
 63     printf("经历年代: %d\n",MAX_GENS);
 64     printf("保留比例: %.2lf\n",SAVE);
 65     printf("优化倍率: %.2lf\n",MULTIPLE);
 66     printf("交叉概率: %.2lf\n",PXCOVER);
 67     printf("变异概率: %.2lf\n",PMUTATION);
 68     printf("交叉因子: %.2lf\n",PA);
 69     printf("变异因子: %.2lf\n",PM);
 70     printf("Y = (%lf,%lf,%lf,%lf,%lf,%lf)\n",fity.vec[0],fity.vec[1],fity.vec[2],fity.vec[3],fity.vec[4],fity.vec[5]);
 71     printf("fitness = %lf\n",fity.fitness/MULTIPLE);
 72     printf("f(x) = %lf\n",1.0/(fity.fitness/MULTIPLE));
 73     fclose(stdin);
 74     fclose(stdout);
 75     return 0;
 76 }
 77 void inputX(){
 78     scanf("%d",&n);
 79     for(int i=0;i<n;++i){
 80         double mod=0;
 81         for(int j=0;j<N;++j){
 82             scanf("%lf",&x[i].vec[j]);
 83             mod+=x[i].vec[j]*x[i].vec[j];
 84         }
 85         x[i].mod=sqrt(mod);
 86     }
 87 }
 88 void inputY(){
 89     for(int i=0;i<N;++i){
 90         scanf("%lf%lf",&lower[i],&upper[i]);
 91         for(int j=0;j<MAX_Y;++j){
 92             y[j].vec[i]=randval(lower[i],upper[i]);
 93         }
 94     }
 95 }
 96 double randval(double lower,double upper){
 97     return lower+1.0*rand()/RAND_MAX*(upper-lower);
 98 }
 99 void evaluate(){
100     for(int i=0;i<MAX_Y;++i){
101         double mod=0;
102         for(int j=0;j<N;++j){
103             mod+=y[i].vec[j]*y[i].vec[j];
104         }
105         y[i].mod=sqrt(mod);
106     }
107     double fit=0;
108     for(int i=0;i<MAX_Y;++i){
109         double temp=0;
110         for(int j=0;j<n;++j){
111             double xy=0;
112             for(int k=0;k<N;++k){
113                 xy+=y[i].vec[k]*x[j].vec[k];
114             }
115             temp+=fabs((y[i].mod*x[j].mod)/xy);
116         }
117         fit+=temp;
118         y[i].fitness=temp*MULTIPLE;
119     }
120     fit*=MULTIPLE;
121     sort(y,y+MAX_Y,compare);
122     double temp=0;
123     for(int i=0;i<MAX_Y;++i){
124         temp+=y[i].fitness;
125         y[i].fitsum=temp/fit;
126     }
127 }
128 void select(){
129     if(generation){
130         newy[0]=fity;
131         for(int i=1;i<=MAX_Y*SAVE;++i){
132             newy[i]=y[i];
133         }
134         for(int i=MAX_Y*SAVE+1;i<MAX_Y;++i){
135             double rand=randval(0,1);
136             for(int j=0;j<MAX_Y;++j){
137                 if(y[j].fitsum>rand||fabs(y[j].fitness-rand)<=EPS){
138                     newy[i]=y[j];
139                     break;
140                 }
141             }
142         }
143     }else{
144         for(int i=0;i<MAX_Y*SAVE;++i){
145             newy[i]=y[i];
146         }
147         for(int i=MAX_Y*SAVE;i<MAX_Y;++i){
148             double rand=randval(0,1);
149             for(int j=0;j<MAX_Y;++j){
150                 if(y[j].fitsum>rand||fabs(y[j].fitness-rand)<=EPS){
151                     newy[i]=y[j];
152                     break;
153                 }
154             }
155         }
156     }
157     copyy();
158     generation++;
159 /*
160     printf("******************************************************************\n");
161     printf("\tgeneration: %d\n",generation);
162     for(int i=0;i<MAX_Y;++i){
163         for(int j=0;j<N;++j){
164             printf("%10lf ",y[i].vec[j]);
165         }
166         printf("\n");
167     }
168     printf("******************************************************************\n");
169 */
170 }
171 void copyy(){
172     memset(y,0,sizeof(y));
173     for(int i=0;i<MAX_Y;++i){
174         for(int j=0;j<N;++j){
175             y[i].vec[j]=newy[i].vec[j];
176         }
177     }
178 }
179 void xcover(){
180     memset(index,0,sizeof(index));
181     for(int i=0;i<MAX_Y;++i){
182         int temp;
183         do{
184             temp=rand()%MAX_Y;
185         }while(index[temp]);
186         index[temp]=1;
187         double p=1.0*rand()/RAND_MAX;
188         if(p<PXCOVER||fabs(p-PXCOVER)<=EPS){
189             for(int j=0;j<N;++j){
190                 double t=y[i].vec[j];
191                 y[i].vec[j]=(1-PA)*y[i].vec[j]+PA*y[temp].vec[j];
192                 y[temp].vec[j]=(1-PA)*y[temp].vec[j]+PA*t;
193             }
194         }
195     }
196 }
197 void mutate(){
198     for(int i=0;i<MAX_Y;i++){
199         double p=1.0*rand()/RAND_MAX;
200         if(p<PMUTATION||fabs(p-PMUTATION)<=EPS){
201             int temp=rand()%2;
202             if(temp){
203                 for(int j=0;j<N;++j){
204                     y[i].vec[j]=y[i].vec[j]+PM*(upper[j]-y[i].vec[j])*(1.0*rand()/RAND_MAX);
205                 }
206             }else{
207                 for(int j=0;j<N;++j){
208                     y[i].vec[j]=y[i].vec[j]-PM*(y[i].vec[j]-lower[j])*(1.0*rand()/RAND_MAX);
209                 }
210             }
211         }
212     }
213 }
214 void findmaxfit(){
215     if(y[0].fitness>fity.fitness||fabs(y[0].fitness-fity.fitness)<=EPS){
216         fity=y[0];
217     }
218 }
219 bool compare(Yvector a,Yvector b){
220     return a.fitness>b.fitness;
221 }

优质内容筛选与推荐>>
1、泛型 类 ComboBoxItem<T>
2、谈谈Javascript的this关键字(this is not this)
3、树形控件(CTreeCtrl和CTreeView)
4、python3 ModuleNotFoundError: No module named '_sqlite3'
5、jQuery.hover() 函数详解


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号