亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? ex26.c

?? 單目標遺傳算法優化的經典例子c++源碼
?? C
字號:
/* ----------------------------------------------------------------------------
  ex26.C
  mbwall 24mar96
  Copyright (c) 1995-1996  Massachusetts Institute of Technology

  The code for this example is adapted from an original implementation 
  by Thomas Grueninger (from Uni Stuttgart, visiting scholar at MIT)

 DESCRIPTION:
   GAs are lousy at solving the travelling salesperson problem.  But here is an
example of how to do it anyway.  In this case we use a steady-state genetic
algorithm and give you the compile-time choice of partial match or edge 
recombination crossover.
   This one looks really good when you draw an entire population of individuals
and watch them all evolve in real time.  It becomes very obvious what the
genetic algorithm is doing and how well the speciation is working (or not).
   This implementation is by no means efficient, so please do not complain 
about all of the silly little inefficient aspects of the implementation.  But 
it does get the job done.
---------------------------------------------------------------------------- */
#include <math.h>
#include <ga/GASStateGA.h>
#include <ga/GAListGenome.h>
#include <ga/garandom.h>

// Set this up for your favorite TSP.  The sample one is a contrived problem
// with the towns laid out in a grid (so it is easy to figure out what the 
// shortest distance is, and there are many different paths with the same
// shortest path).  File format is that used by the TSPLIB problems.  You can 
// grab more problems from 
// 
// 
// Apologies for using fixed-length arrays.  But this is an example, not 
// production code ;)
#define MAX_TOWNS 50
#define TSP_FILE "tsp_rect_20.txt"

float DISTANCE[MAX_TOWNS][MAX_TOWNS];
double x[MAX_TOWNS],y[MAX_TOWNS];
int ntowns = 0;

// You can use either edge recombination crossover or partial match crossover.
// Which one you select makes a HUGE difference in the performance of the
// genetic algorithm.  Only one of the two following lines should be commented.
//#define XOVER PMXover       // (Partial Match Crossover)
#define XOVER ERXover         // (Edge Recombination Crossover)


float Objective(GAGenome&);
int   Mutator(GAGenome&, float);
void  Initializer(GAGenome&);
float Comparator(const GAGenome&, const GAGenome&); 
int   ERXover(const GAGenome&, const GAGenome&, GAGenome*, GAGenome*);
int   PMXover(const GAGenome&, const GAGenome&, GAGenome*, GAGenome*);
void  ERXOneChild(const GAGenome&, const GAGenome&, GAGenome*);


int
main(int argc, char** argv) {
  cout << "Example 26\n\n";
  cout << "The Travelling Salesman Problem (TSP) demo program.\n" << endl;

// See if we've been given a seed to use (for testing purposes).  When you
// specify a random seed, the evolution will be exactly the same each time
// you use that seed number.

  unsigned int seed = 0;
  for(int ii=1; ii<argc; ii++) {
    if(strcmp(argv[ii++],"seed") == 0) {
      seed = atoi(argv[ii]);
    }
  }

  double dump;
  ifstream in(TSP_FILE); 
  if(!in) {
    cerr << "could not read data file " << TSP_FILE << "\n";
    exit(1);
  }
  ntowns=0;
  do {
    in >> dump;
    in >> x[ntowns];
    in >> y[ntowns];
    ntowns++;
  } while(!in.eof() && ntowns < MAX_TOWNS);
  in.close();
  if(ntowns >= MAX_TOWNS) {
    cerr << "data file contains more towns than allowed for in the fixed\n";
    cerr << "arrays.  Recompile the program with larger arrays or try a\n";
    cerr << "smaller problem.\n";
    exit(1);
  }

  double dx,dy;
  for(int i=0;i<ntowns;i++) {
    for(int j=i; j<ntowns;j++) {
      dx=x[i]-x[j]; dy=y[i]-y[j];
      DISTANCE[j][i]=DISTANCE[i][j]=sqrt(dx*dx+dy*dy);
    }
  }

  GAListGenome<int> genome(Objective);
  genome.initializer(::Initializer);
  genome.mutator(::Mutator);
  genome.comparator(::Comparator);
  genome.crossover(XOVER);

  GASteadyStateGA ga(genome);
  ga.minimize();
  ga.pReplacement(1.0);
  ga.populationSize(100);
  ga.nGenerations(1000);
  ga.pMutation(0.1);
  ga.pCrossover(1.0);
  ga.selectScores(GAStatistics::AllScores);
  ga.parameters(argc, argv);
  cout << "initializing..."; cout.flush();
  ga.initialize(seed);
  cout << "evolving..."; cout.flush();
  while(!ga.done()) {
    ga.step();
    if(ga.generation() % 10 == 0) {
      cout << ga.generation() << " "; cout.flush();
    }
  }

  genome = ga.statistics().bestIndividual();
  cout << "the shortest path found is " << genome.score() << "\n";
  cout << "this is the distance from the sequence\n";
  cout << genome << "\n\n";
  cout << ga.statistics() << "\n";

  return 0;
}






// Here are the genome operators that we want to use for this problem.
// Thanks to Jan Kees IJspeert for isolating an order-of-evaluation problem
// in the previous implementation of this function.
float
Objective(GAGenome& g) {
  GAListGenome<int> & genome = (GAListGenome<int> &)g;
  float dist = 0;
  if(genome.head()) {
    for(int i=0; i<ntowns; i++) {
      int xx = *genome.current();
      int yy = *genome.next();
      dist += DISTANCE[xx][yy];
    }
  }
  return dist;
}

void
Initializer(GAGenome& g) {
  GAListGenome<int> &child=(GAListGenome<int> &)g;
  while(child.head()) child.destroy(); // destroy any pre-existing list

  int i,town;
  static int visit[MAX_TOWNS];

  memset(visit, 0, MAX_TOWNS*sizeof(int));
  town=GARandomInt(0,ntowns-1);
  visit[town]=1;
  child.insert(town,GAListBASE::HEAD); // the head node
 
  for( i=1; i<ntowns; i++) {
    do {
      town=GARandomInt(0,ntowns-1);
    } while (visit[town]);
    visit[town]=1;
    child.insert(town);
  }		// each subsequent node 
}

int
Mutator(GAGenome& g, float pmut) {
  GAListGenome<int> &child=(GAListGenome<int> &)g;
  register int n, i;
  if ((GARandomFloat() >= pmut) || (pmut <= 0)) return 0;

  n = child.size();
  
  if (GARandomFloat()<0.5) {
    child.swap(GARandomInt(0,n-1),GARandomInt(0,n-1)); // swap only one time
  }
  else {
    int nNodes = GARandomInt(1,((int)(n/2-1)));       // displace nNodes 
    child.warp(GARandomInt(0,n-1));                   // with or without
    GAList<int> TmpList;                              // inversion
    for(i=0;i<nNodes;i++) {
      int *iptr = child.remove();
      TmpList.insert(*iptr,GAListBASE::AFTER);
      delete iptr;
      child.next();
    }
    int invert;
    child.warp(GARandomInt(0,n-nNodes));
    invert = (GARandomFloat()<0.5) ? 0 : 1;
    if (invert) TmpList.head(); else TmpList.tail();

    for(i=0;i<nNodes;i++) {
      int *iptr = TmpList.remove();
      child.insert(*iptr,GAListBASE::AFTER);
      delete iptr;
      if (invert) TmpList.prev(); else TmpList.next();
    }
  }
  child.head();		// set iterator to root node

  return (1);
}

int
ERXover(const GAGenome& g1, const GAGenome& g2, GAGenome* c1, GAGenome* c2) {
  int nc=0;
  if(c1) { ERXOneChild(g1,g2,c1); nc+=1; }
  if(c2) { ERXOneChild(g1,g2,c2); nc+=1; }
  return nc;
}

void
ERXOneChild(const GAGenome& g1, const GAGenome& g2, GAGenome* c1) {
  GAListGenome<int> &mate1=(GAListGenome<int> &)g1;
  GAListGenome<int> &mate2=(GAListGenome<int> &)g2;
  GAListGenome<int> &sis=(GAListGenome<int> &)*c1;
  
  int i,j,k,t1,t2,town;

  static char CM[MAX_TOWNS][MAX_TOWNS],visit[MAX_TOWNS];
  memset(CM, 0, MAX_TOWNS*MAX_TOWNS*sizeof(char));
  memset(visit, 0, MAX_TOWNS*sizeof(char));

  while (sis.head()) sis.destroy();

  // create connection matrix
  mate1.head();
  for(j=0; j<ntowns; j++) {
    t1 = *mate1.current(); t2 = *mate1.next();
    CM[t1][t2]=1; CM[t2][t1]=1;
  }
  mate2.head();
  for(j=0; j<ntowns; j++) {
    t1 = *mate2.current(); t2 = *mate2.next();
    CM[t1][t2]=1; CM[t2][t1]=1;
  }
  
  // select 1st town randomly
  town=GARandomInt(0,ntowns-1);
  visit[town]=1; memset(CM[town], 0, MAX_TOWNS*sizeof(char));
  sis.insert(town); // the head node 
  
  GAList<int> PossFollowList;
  GAList<int> FollowersList[5];
  while (PossFollowList.head()) PossFollowList.destroy();
  for(k=0; k<5; k++) {
    while (FollowersList[k].head()) FollowersList[k].destroy(); 
  }
  
  // select the following town with the minimal no of next folling towns
  int nPoss,nFollow;
  for(i=1; i<ntowns; i++) {           
    nPoss = 0;
    for(j=0; j<ntowns; j++) {          // no of poss. following towns
      if (CM[j][town]) {
	nPoss += 1;
	PossFollowList.insert(j);}
    }
    // nPoss = 0;
    if (nPoss == 0) {
      do {town=GARandomInt(0,ntowns-1);} while (visit[town]); // no follower
      visit[town]=1; memset(CM[town], 0, MAX_TOWNS*sizeof(char));
      sis.insert(town); 
    }
    else {
      PossFollowList.head();
      for(j=0; j<nPoss; j++) {
	nFollow = 0; 
	town = (*PossFollowList.current());
	for(k=0; k<ntowns; k++) {
	  if (CM[k][town]) nFollow++; 
	}
	FollowersList[nFollow].insert(town);
	PossFollowList.next();
      }
      k=0;
      while (FollowersList[k].size() == 0) k++;
      FollowersList[k].warp(GARandomInt(0,FollowersList[k].size()));
      town = (*FollowersList[k].current());
      visit[town]=1; memset(CM[town], 0, MAX_TOWNS*sizeof(char));
      sis.insert(town); 
    }
    while (PossFollowList.head()) PossFollowList.destroy();
    for(k=0; k<5; k++) {
      while (FollowersList[k].head()) FollowersList[k].destroy(); 
    }
  }
  sis.head();         // set iterator to head of list
}

int
PMXover(const GAGenome& g1, const GAGenome& g2, GAGenome* c1, GAGenome* c2) {
  GAListGenome<int> &mom=(GAListGenome<int> &)g1;
  GAListGenome<int> &dad=(GAListGenome<int> &)g2;

  int a = GARandomInt(0, mom.size());
  int b = GARandomInt(0, dad.size());
  int h;
  if (b<a) { h=a; a=b; b=h; }

  int* index;
  int i,j,nc=0;

  if(c1) {
    GAListGenome<int> &sis=(GAListGenome<int> &)*c1;
    sis.GAList<int>::copy(mom);
    GAListIter<int> diter(dad);
    index = diter.warp(a);
    for(i=a; i<b; i++, index=diter.next()){
      if(*sis.head() == *index){
	sis.swap(i,0);
      }
      else{
	for(j=1; (j<sis.size()) && (*sis.next() != *index); j++);
	sis.swap(i,j);  // no op if j>size
      }
    }
    sis.head();         // set iterator to head of list
    nc += 1;
  }
  if(c2) {
    GAListGenome<int> &sis=(GAListGenome<int> &)*c2;
    sis.GAList<int>::copy(mom);
    GAListIter<int> diter(dad);
    index = diter.warp(a);
    for(i=a; i<b; i++, index=diter.next()){
      if(*sis.head() == *index){
	sis.swap(i,0);
      }
      else{
	for(j=1; (j<sis.size()) && (*sis.next() != *index); j++);
	sis.swap(i,j);  // no op if j>size
      }
    }
    sis.head();         // set iterator to head of list
    nc += 1;
  }

  return nc;
}

float
Comparator(const GAGenome& g1, const GAGenome& g2) {
  GAListGenome<int> &a = (GAListGenome<int> &)g1;
  GAListGenome<int> &b = (GAListGenome<int> &)g2;

  int i,j,t1,t2;
  float dist=ntowns;

  static char CM1[MAX_TOWNS][MAX_TOWNS],CM2[MAX_TOWNS][MAX_TOWNS];
  memset(CM1, 0, MAX_TOWNS*MAX_TOWNS*sizeof(char));
  memset(CM2, 0, MAX_TOWNS*MAX_TOWNS*sizeof(char));

  // create connection matrix CM1
  a.head();
  for(i=0; i<ntowns; i++) {
    t1 = *a.current(); t2 = *a.next();
    CM1[t1][t2]=1; CM1[t2][t1]=1;
  }
  // create connection matrix CM2
  b.head();
  for(i=0; i<ntowns; i++) {
    t1 = *b.current(); t2 = *b.next();
    CM2[t1][t2]=1; CM2[t2][t1]=1;
  }
  //calc distance = how many edges are different
  for (i=0; i<ntowns; i++) {
    for (j=i; j<ntowns; j++) {
      if (CM1[i][j]&CM2[i][j]) dist--;
    }
  }
  return (dist);
}




//   Here we override the _write method for the List class.  This lets us see
// exactly what we want (the default _write method dumps out pointers to the
// data rather than the data contents).
//   This routine prints out the contents of each element of the list, 
// separated by a space.  It does not put a newline at the end of the list.
//   Notice that you can override ANY function of a template class.  This is
// called "specialization" in C++ and it lets you tailor the behaviour of a 
// template class to better fit the type.
int
GAListGenome<int>::write(ostream & os) const
{
  int *cur, *head;
  GAListIter<int> tmpiter(*this);
  if((head=tmpiter.head()) != 0) {
    os << *head << " ";
    for(cur=tmpiter.next(); cur && cur != head; cur=tmpiter.next())
      os << *cur << " ";
  }

  return os.fail() ? 1 : 0;
}


#ifdef NO_AUTO_INST
#include <ga/GAList.C>
#include <ga/GAListGenome.C>
#if defined(__GNUG__)
template class GAList<int>;
template class GAListGenome<int>;
#else
GAList<int>;
GAListGenome<int>;
#endif
#endif

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
3d动漫精品啪啪一区二区竹菊| 成人污污视频在线观看| 国产精品久久夜| 久久久久久久av麻豆果冻| 精品精品国产高清一毛片一天堂| 制服丝袜成人动漫| 欧美精品v日韩精品v韩国精品v| 欧美色大人视频| 制服.丝袜.亚洲.中文.综合| 91精品国产福利在线观看| 欧美一卡二卡三卡四卡| 日韩精品中文字幕在线一区| 日韩美女主播在线视频一区二区三区 | 欧美日本在线视频| 欧美体内she精视频| 欧美一级生活片| www国产精品av| 国产精品久久久久毛片软件| 亚洲另类一区二区| 亚洲高清中文字幕| 国产在线精品免费| 成人精品视频一区二区三区| 色综合久久中文综合久久牛| 在线观看日韩高清av| 日韩一区二区三| 国产欧美日韩另类一区| 亚洲情趣在线观看| 天堂成人免费av电影一区| 麻豆精品视频在线观看| 国产91丝袜在线播放九色| 色噜噜狠狠色综合中国| 91精品国产综合久久久久| 国产三级一区二区三区| 尤物在线观看一区| 国内成人精品2018免费看| 91在线观看视频| 91精品国产福利在线观看| 中文字幕乱码一区二区免费| 亚洲va韩国va欧美va| 国产一本一道久久香蕉| 欧洲国产伦久久久久久久| 久久亚洲影视婷婷| 亚洲一区二区欧美激情| 国产高清在线观看免费不卡| 欧美私模裸体表演在线观看| 国产喂奶挤奶一区二区三区| 午夜视黄欧洲亚洲| 波多野结衣在线一区| 欧美一区二区视频免费观看| 国产精品久久久久毛片软件| 久久99日本精品| 欧美性受xxxx| 欧美国产一区二区| 美脚の诱脚舐め脚责91| 欧美三区在线视频| 亚洲精选视频免费看| 国产精品996| 久久综合资源网| 日韩国产高清在线| 欧美在线免费播放| 亚洲欧美成aⅴ人在线观看| 激情综合亚洲精品| 日韩小视频在线观看专区| 亚洲最新在线观看| 色综合久久久久综合99| 国产精品看片你懂得| 国产精品18久久久久久久网站| 欧美一区二区三区成人| 日韩一区精品字幕| 欧美日韩国产片| 亚洲综合视频在线| 91极品美女在线| 亚洲精品久久嫩草网站秘色| 色婷婷香蕉在线一区二区| 亚洲色图色小说| 国产乱码精品一区二区三 | 豆国产96在线|亚洲| 精品国产sm最大网站免费看| 久久99久久久久久久久久久| 日韩欧美亚洲国产另类| 奇米在线7777在线精品| 日韩欧美在线1卡| 久久国产夜色精品鲁鲁99| 欧美不卡在线视频| 韩国v欧美v亚洲v日本v| 久久久久国产精品麻豆ai换脸| 精品一区二区三区日韩| 久久久亚洲精华液精华液精华液| 国产一区二区在线观看免费| 久久精品视频一区二区三区| 成人性生交大片免费看视频在线 | av在线不卡免费看| 亚洲欧美在线观看| 91麻豆成人久久精品二区三区| 亚洲品质自拍视频| 欧美片网站yy| 久久国产精品免费| 国产精品久久久久久久久免费樱桃| 波多野结衣91| 亚洲午夜免费视频| 欧美一区二区三区爱爱| 国产激情一区二区三区桃花岛亚洲 | 欧美日韩五月天| 免费的成人av| 亚洲欧洲成人自拍| 欧美一区二区三区在线电影| 国产真实乱偷精品视频免| 国产精品妹子av| 69堂成人精品免费视频| 国产精品一区二区在线播放| 亚洲精品美腿丝袜| 欧美大尺度电影在线| 岛国精品在线观看| 亚洲大片免费看| 国产欧美中文在线| 欧美日韩久久不卡| 成人午夜激情片| 免费在线欧美视频| 亚洲人成在线观看一区二区| 日韩欧美亚洲一区二区| 色欧美日韩亚洲| 国产寡妇亲子伦一区二区| 亚洲成人av电影| 国产精品拍天天在线| 精品久久久三级丝袜| 欧美日本一区二区| 99久久久国产精品| 狠狠色丁香婷婷综合久久片| 国产精品电影一区二区三区| 精品免费视频.| 欧美喷潮久久久xxxxx| 91亚洲精品乱码久久久久久蜜桃| 韩国中文字幕2020精品| 五月婷婷激情综合网| 亚洲欧美日韩系列| 国产精品免费久久| 欧美激情一区二区三区在线| 日韩欧美专区在线| 欧美裸体一区二区三区| 色美美综合视频| 97久久超碰国产精品电影| 韩国精品免费视频| 激情综合网激情| 蜜桃av一区二区| 热久久免费视频| 青青草伊人久久| 三级亚洲高清视频| 视频一区二区中文字幕| 一区二区三区四区精品在线视频| 中文文精品字幕一区二区| 精品成a人在线观看| 日韩精品一区二区三区视频在线观看| 欧美怡红院视频| 欧美综合在线视频| 在线观看网站黄不卡| 欧美在线观看你懂的| 欧美亚洲综合色| 欧美日韩一区二区三区不卡| 欧美日韩一二三区| 5566中文字幕一区二区电影| 欧美精选一区二区| 日韩一区国产二区欧美三区| 欧美第一区第二区| 日本一区二区不卡视频| 国产精品免费视频一区| 亚洲精品一二三区| 亚洲一区二区三区四区五区中文| 午夜精品123| 久久91精品久久久久久秒播 | 国产精品高潮呻吟久久| 综合久久国产九一剧情麻豆| 亚洲欧洲av在线| 亚洲成人av一区二区| 精品一区二区三区的国产在线播放| 蜜芽一区二区三区| 国产精品综合视频| 91丨porny丨国产| 欧美精品丝袜中出| 精品少妇一区二区三区免费观看 | 国产精品剧情在线亚洲| 一区二区不卡在线视频 午夜欧美不卡在 | 亚洲伦在线观看| 亚洲成av人影院| 日本va欧美va精品| 国产成人在线视频免费播放| av在线不卡电影| 欧美一区二区三区在线电影 | 欧美这里有精品| 欧美tk—视频vk| 一区二区三区在线免费| 久久99精品久久久久久动态图 | 婷婷国产v国产偷v亚洲高清| 极品瑜伽女神91| 色视频欧美一区二区三区| 日韩精品中文字幕一区二区三区| 国产精品久久久久aaaa樱花 | 国产精品1区2区| 69堂成人精品免费视频| 综合久久久久综合| 韩国午夜理伦三级不卡影院|