Preliminary study of genetic algorithm

Source: Internet
Author: User
Tags rand

Wrote a 10 number of the maximum sum, the value range of what 0-99, because the state space is relatively large, so the probability of mutation selection of 0.3, larger, crossover probability 0.8, population size 100, genetic algebra 1000

effect, not very ideal, distance 990 of the limit there is no small gap

As can be seen, only 8 times above 950, and after the increase of the genetic algebra to 10000, the effect did not increase significantly, quite failed

#include <bits/stdc++.h>using namespacestd;Const Doubleby=0.3;//mutation ProbabilityConst DoubleJh=0.8;//Free exchange probabilityConst intMax_size= -;Const intMax_d=100000;intsum;structnode{intv[Ten]; intFitness; voidCol () {Fitness=0;  for(intI=0;i<Ten; i++) Fitness+=V[i]; }}now[ the],max,min;//The population, the temporary, the largest individual, the smallest individual .//Generate initial populationvoidinit () { for(intI=0; i<max_size;i++){         for(intj=0;j<Ten; j + +) Now[i].v[j]=rand ()% -; }}voidjs () {sum=0; Max.fitness=-1; min.fitness=0x3f3f3f3f;  for(intI=0; i<max_size;i++) {now[i].col (); if(now[i].fitness>max.fitness) max=Now[i]; if(now[i].fitness<min.fitness) min=Now[i]; Sum+=now[i].fitness; }}//Print an individualvoidprint_o (Node A) { for(intI=0;i<Ten; i++) printf ("%d", A.v[i]); printf ("%d\n", a.fitness);}//Print all current stocksvoidprint () { for(intI=0; i<max_size;i++) Print_o (Now[i]);}intrand_id () {intD=rand ()% (sum+1); intCnt=0;  for(intI=0; i<max_size;i++) {CNT+=now[i].fitness; if(Cnt>=d)returni; }    returnRAND ()%max_size;}Doublerandd () {DoubleAns=rand ()% +; Ans/= +; returnans;}voidTran () {JS (); //find the best individuals    inttmp=0, I,j,pos;  for(intI=1; i<max_size;i++)        if(now[i].fitness>now[tmp].fitness) TMP=i;  for(intk=0; k<max_size;k+=2){        //Select a cross-entityI=rand_id (); J=rand_id (); //Intersection PositionPos=rand ()%Ten; //Exchange        if(RandD () <JH)            {swap (Now[k].v[pos],now[j].v[pos]);        Swap (Now[k].v[pos],now[i].v[pos]); }        //mutation        if(RandD () <y) {POS=rand ()%Ten; Now[k].v[pos]=rand ()% -;    }} JS (); intId=0;  for(intI=1; i<max_size;i++)      if(now[id].fitness>now[i].fitness) ID=i; Now[id]=Max;}voidga () {intCnt=0;  while(cnt<max_d) {tran (); CNT++; //print (); //puts ("\ n");    }    intans=now[0].fitness;  for(intI=0; i<max_size;i++) ans=Max (ans,now[i].fitness); printf ("%d\n", ans);}intMain () {srand (unsigned) time (NULL));  for(intI=1; i<= the; i++) {printf ("Case %d: \ n", i);        Init ();    GA (); }    return 0;}
View Code

Preliminary study of genetic algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.