GALib案例详解(一)

2 篇文章 0 订阅
1 篇文章 0 订阅
/* ----------------------------------------------------------------------------
  ex1.C
  mbwall 28jul94
  Copyright (c) 1995-1996  Massachusetts Institute of Technology
  翻译:天明Future
 DESCRIPTION:
   Example program for the SimpleGA class and 2DBinaryStringGenome class.
This program tries to fill the 2Dgenome with alternating 1s and 0s. 
  This example uses the default crossover (single point), default mutator
(uniform random bit flip), and default initializer (uniform random) for the
2D genome.
SimpleGA类和2DBinaryStringGenome类的示例程序。
该程序尝试用交替的1和0填充2Dgenome。
   本示例为2D genome 使用默认的corssover(单点),默认的mutator(均匀随机位翻转),
和默认初始化(均匀随机)。

  Notice that one-point crossover is not necessarily the best kind of crossover
to use if you want to generate a 'good' genome with this kind of objective 
function.  But it does work.
请注意,如果您想生成具有此类目标函数的“良好”基因组,单点交叉不一定是最好的交叉类型。
但这确实有效。

---------------------------------------------------------------------------- */
#include <ga/GASimpleGA.h>	// 我们将使用普通的GA
#include <ga/GA2DBinStrGenome.h> // 和2D二进制stringgenome
#include <ga/std_stream.h>

#define cout STD_COUT

float Objective(GAGenome &);	//目标函数的声明,定义在文件的最后

int
main(int argc, char **argv)
{
  cout << "Example 1\n\n";
  cout << "This program tries to fill a 2DBinaryStringGenome with\n";
  cout << "alternating 1s and 0s using a SimpleGA\n\n"; cout.flush();
/*
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.

查看是否已获得种子供使用(用于测试目的)。
当你指定随机种子,每次的演化将完全相同。
您使用该种子编号。
*/
  for(int ii=1; ii<argc; ii++) {
    if(strcmp(argv[ii++],"seed") == 0) {
      GARandomSeed((unsigned int)atoi(argv[ii]));
    }
  }

// Declare variables for the GA parameters and set them to some default values.
// 为GA参数声明变量,并且赋予初始值。
  int width    = 10;
  int height   = 5;
  int popsize  = 30;
  int ngen     = 400;
  float pmut   = 0.001;
  float pcross = 0.9;
/*
Now create the GA and run it.  First we create a genome of the type that
we want to use in the GA.  The ga doesn't operate on this genome in the
optimization - it just uses it to clone a population of genomes.
现在创建GA并运行它。 首先,我们创建一个我们想在GA中使用的基因组类型。
。 ga在优化过程中不操作这个genome,它仅用它来克隆一组基因组的种群。
*/
  GA2DBinaryStringGenome genome(width, height, Objective);

/*
Now that we have the genome, we create the genetic algorithm and set
its parameters - number of generations, mutation probability, and crossover
probability.  And finally we tell it to evolve itself.
现在我们有了基因组,我们创建遗传算法并设置它的参数(世代数,突变概率和交叉概率)。
最后,我们告诉它自我进化。
*/
  GASimpleGA ga(genome);
  ga.populationSize(popsize);
  ga.nGenerations(ngen);
  ga.pMutation(pmut);
  ga.pCrossover(pcross);
  ga.evolve();

// Now we print out the best genome that the GA found.
// 现在我们打印出来GA找到的最好的genome

  cout << "The GA found:\n" << ga.statistics().bestIndividual() << "\n";

  return 0;
}
 


/*
 This is the objective function.  All it does is check for alternating 0s and
 1s.  If the gene is odd and contains a 1, the fitness is incremented by 1.
 If the gene is even and contains a 0, the fitness is incremented by 1.  No
 penalties are assigned. 
这是目标函数。它所做的只是检查交替的0和1。如果基因是奇数且包含1,则适应度增加1。
如果基因是偶数且包含0,则适应度增加1。没有增加惩罚机制。

  We have to do the cast because a plain, generic GAGenome doesn't have 
the members that a GA2DBinaryStringGenome has.  And it's ok to cast it
because we know that we will only get GA2DBinaryStringGenomes and
nothing else.
我们必须进行转换,因为普通的通用Genome没有GA2DBinaryStringGenome所拥有的成员。
并且可以安全的进行转换,因为我们知道我们只会得到GA2DBinaryStringGenomes,而非其他。
*/
float
Objective(GAGenome& g) {
  GA2DBinaryStringGenome & genome = (GA2DBinaryStringGenome &)g;
  float score=0.0;
  int count=0;
  for(int i=0; i<genome.width(); i++){
    for(int j=0; j<genome.height(); j++){
      if(genome.gene(i,j) == 0 && count%2 == 0)
	score += 1.0;
      if(genome.gene(i,j) == 1 && count%2 != 0)
	score += 1.0;
      count++;
    }
  }
  return score;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值