⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gabasega.c

📁 这是一个面向对象的GA遗传算法库GAlib: A C++ Library of Genetic Algorithm Components)
💻 C
📖 第 1 页 / 共 2 页
字号:
// $Header$/* ----------------------------------------------------------------------------  gabase.C  mbwall 28jul94  Copyright (c) 1995 Massachusetts Institute of Technology                     all rights reserved  Source file for the base genetic algorithm object.---------------------------------------------------------------------------- */#include <stdio.h>#include <string.h>#include <ga/GABaseGA.h>#include <ga/garandom.h>#include <ga/gaversion.h>	// gets the RCS string in for ident purposes//#define GA_DEBUG// Here we assign the default values of the GAlib default parameters.int   gaDefNumGen           = 250;float gaDefPConv            = 0.99;int   gaDefNConv            = 20;float gaDefPMut             = 0.01;float gaDefPCross           = 0.90;int   gaDefPopSize          = 50;int   gaDefNPop             = 10;float gaDefPRepl            = 0.5;int   gaDefNRepl            = 10;int   gaDefNumOff           = 2;float gaDefPMig             = 0.1;int   gaDefNMig             = 5;int   gaDefSelectScores     = GAStatistics::Maximum;int   gaDefMiniMaxi         = 1;GABoolean gaDefDivFlag      = gaFalse;GABoolean gaDefElitism      = gaTrue;int   gaDefSeed             = 0;// return the configuration string that identifies this build of the library.static const char* rcsid = GALIB_LIBRARY_IDENTIFIER;const char* GAConfig() { return rcsid; }// Here are a few termination functions that you can use.  Terminators return// gaTrue if the algorithm should finish, gaFalse otherwise.GABooleanGAGeneticAlgorithm::TerminateUponGeneration(GAGeneticAlgorithm & ga){  return(ga.generation() < ga.nGenerations() ? gaFalse : gaTrue);}// If we are maximizing, then terminate when the convergence has exceeded the// specified convergence.  If we are minimizing, then terminate when the // convergence has dropped below the specified convergence.GABoolean GAGeneticAlgorithm::TerminateUponConvergence(GAGeneticAlgorithm & ga){  GABoolean val = gaFalse;  if(ga.minimaxi() == GAGeneticAlgorithm::MINIMIZE) {    if(ga.convergence() == 0 || ga.convergence() > ga.pConvergence())      val = gaFalse;    else       val = gaTrue;  }  else {    if(ga.convergence() < ga.pConvergence())      val = gaFalse;    else       val = gaTrue;  }  return val;}// Use the ratio between the minimum and maximum to determine whether the // population has converged.  This method will not work if the values cross// zero!// Note that this is significantly different than the definition (and the // bug-laden implementation) that was in version of GAlib prior to 2.4.5.//// For historical purposes, here is the old definition of this method://// Use the ratio of the population mean divided by the population maximum to// determine whether the population has converged.  If we are maximizing, then// check to see if the ratio exceeds the convergence.  If we are minimizing, // then check to see if the ratio has dropped below the convergence.GABoolean GAGeneticAlgorithm::TerminateUponPopConvergence(GAGeneticAlgorithm & ga){  GABoolean val = gaFalse;  if(ga.statistics().current(GAStatistics::Maximum) == 0) {    return val;  }  float ratio =     ga.statistics().current(GAStatistics::Minimum) /    ga.statistics().current(GAStatistics::Maximum);  if(ga.minimaxi() == GAGeneticAlgorithm::MINIMIZE) {    if(ratio <= ga.pConvergence())      val = gaTrue;    else      val = gaFalse;  }  else {    if(ratio >= ga.pConvergence())      val = gaTrue;    else      val = gaFalse;  }  return val;}GAParameterList&GAGeneticAlgorithm::registerDefaultParameters(GAParameterList& p) {  p.add(gaNseed, gaSNseed, GAParameter::INT, &gaDefSeed);  p.add(gaNminimaxi, gaSNminimaxi, GAParameter::INT, &gaDefMiniMaxi);  p.add(gaNnGenerations, gaSNnGenerations, GAParameter::INT, &gaDefNumGen);  p.add(gaNnConvergence, gaSNnConvergence, GAParameter::INT, &gaDefNConv);  p.add(gaNpConvergence, gaSNpConvergence, GAParameter::FLOAT, &gaDefPConv);  p.add(gaNpCrossover, gaSNpCrossover, GAParameter::FLOAT, &gaDefPCross);  p.add(gaNpMutation, gaSNpMutation, GAParameter::FLOAT, &gaDefPMut);  p.add(gaNpopulationSize, gaSNpopulationSize, 	GAParameter::INT, &gaDefPopSize);  p.add(gaNnBestGenomes, gaSNnBestGenomes, 	GAParameter::INT, &gaDefNumBestGenomes);  p.add(gaNscoreFrequency, gaSNscoreFrequency,	GAParameter::INT, &gaDefScoreFrequency1);  p.add(gaNflushFrequency, gaSNflushFrequency,	GAParameter::INT, &gaDefFlushFrequency);  p.add(gaNrecordDiversity, gaSNrecordDiversity,	GAParameter::INT, &gaDefDivFlag);  p.add(gaNscoreFilename, gaSNscoreFilename, 	GAParameter::STRING, gaDefScoreFilename);  p.add(gaNselectScores, gaSNselectScores, 	GAParameter::INT, &gaDefSelectScores);  return p;}// When we create a GA, we stuff the parameters with the basics that will be// needed by most genetic algorithms - num generations, p convergence, etc.GAGeneticAlgorithm::GAGeneticAlgorithm(const GAGenome& g) : stats(), params() {  pop = new GAPopulation(g, gaDefPopSize);  pop->geneticAlgorithm(*this);  ud = (void *)0;  cf = GAGeneticAlgorithm::DEFAULT_TERMINATOR;  d_seed = gaDefSeed;  params.add(gaNseed, gaSNseed, GAParameter::INT, &d_seed);  minmax = gaDefMiniMaxi;  params.add(gaNminimaxi, gaSNminimaxi, GAParameter::INT, &minmax);  ngen = gaDefNumGen;  params.add(gaNnGenerations, gaSNnGenerations, GAParameter::INT, &ngen);  nconv = gaDefNConv; stats.nConvergence(nconv);  params.add(gaNnConvergence, gaSNnConvergence, GAParameter::INT, &nconv);  pconv = gaDefPConv;  params.add(gaNpConvergence, gaSNpConvergence, GAParameter::FLOAT, &pconv);  pcross = gaDefPCross;  params.add(gaNpCrossover, gaSNpCrossover, GAParameter::FLOAT, &pcross);  pmut = gaDefPMut;  params.add(gaNpMutation, gaSNpMutation, GAParameter::FLOAT, &pmut);  int psize = pop->size();  params.add(gaNpopulationSize, gaSNpopulationSize, GAParameter::INT, &psize);  stats.scoreFrequency(gaDefScoreFrequency1);  params.add(gaNscoreFrequency, gaSNscoreFrequency,	     GAParameter::INT, &gaDefScoreFrequency1);  stats.flushFrequency(gaDefFlushFrequency);  params.add(gaNflushFrequency, gaSNflushFrequency,	     GAParameter::INT, &gaDefFlushFrequency);  stats.recordDiversity(gaDefDivFlag);  params.add(gaNrecordDiversity, gaSNrecordDiversity, 	     GAParameter::INT, &gaDefDivFlag);  stats.scoreFilename(gaDefScoreFilename);  params.add(gaNscoreFilename, gaSNscoreFilename, 	     GAParameter::STRING, gaDefScoreFilename);  stats.selectScores(gaDefSelectScores);  params.add(gaNselectScores, gaSNselectScores, 	     GAParameter::INT, &gaDefSelectScores);  stats.nBestGenomes(g, gaDefNumBestGenomes);  params.add(gaNnBestGenomes, gaSNnBestGenomes,	     GAParameter::INT, &gaDefNumBestGenomes);  scross = g.sexual();  across = g.asexual();}GAGeneticAlgorithm::GAGeneticAlgorithm(const GAPopulation& p) : stats(), params() {  pop = new GAPopulation(p);  pop->geneticAlgorithm(*this);  ud = (void *)0;  cf = GAGeneticAlgorithm::DEFAULT_TERMINATOR;  d_seed = gaDefSeed;  params.add(gaNseed, gaSNseed, GAParameter::INT, &d_seed);  minmax = gaDefMiniMaxi;  params.add(gaNminimaxi, gaSNminimaxi, GAParameter::INT, &minmax);  ngen = gaDefNumGen;  params.add(gaNnGenerations, gaSNnGenerations, GAParameter::INT, &ngen);  nconv = gaDefNConv; stats.nConvergence(nconv);  params.add(gaNnConvergence, gaSNnConvergence, GAParameter::INT, &nconv);  pconv = gaDefPConv;  params.add(gaNpConvergence, gaSNpConvergence, GAParameter::FLOAT, &pconv);  pcross = gaDefPCross;  params.add(gaNpCrossover, gaSNpCrossover, GAParameter::FLOAT, &pcross);  pmut = gaDefPMut;  params.add(gaNpMutation, gaSNpMutation, GAParameter::FLOAT, &pmut);  int psize = pop->size();  params.add(gaNpopulationSize, gaSNpopulationSize, GAParameter::INT, &psize);  stats.scoreFrequency(gaDefScoreFrequency1);  params.add(gaNscoreFrequency, gaSNscoreFrequency,	     GAParameter::INT, &gaDefScoreFrequency1);  stats.flushFrequency(gaDefFlushFrequency);  params.add(gaNflushFrequency, gaSNflushFrequency,	     GAParameter::INT, &gaDefFlushFrequency);  stats.recordDiversity(gaDefDivFlag);  params.add(gaNrecordDiversity, gaSNrecordDiversity, 	     GAParameter::INT, &gaDefDivFlag);  stats.scoreFilename(gaDefScoreFilename);  params.add(gaNscoreFilename, gaSNscoreFilename, 	     GAParameter::STRING, gaDefScoreFilename);  stats.selectScores(gaDefSelectScores);  params.add(gaNselectScores, gaSNselectScores, 	     GAParameter::INT, &gaDefSelectScores);  stats.nBestGenomes(p.individual(0), gaDefNumBestGenomes);  params.add(gaNnBestGenomes, gaSNnBestGenomes,	     GAParameter::INT, &gaDefNumBestGenomes);  scross = p.individual(0).sexual();  across = p.individual(0).asexual();}GAGeneticAlgorithm::GAGeneticAlgorithm(const GAGeneticAlgorithm& ga) : stats(ga.stats), params(ga.params){  pop = ga.pop->clone();  pop->geneticAlgorithm(*this);  cf = ga.cf; ud = ga.ud;  ngen = ga.ngen; nconv = ga.nconv; pconv = ga.pconv;  pcross = ga.pcross; pmut = ga.pmut; minmax = ga.minmax;  scross = ga.scross; across = ga.across;  d_seed = ga.d_seed;}GAGeneticAlgorithm::~GAGeneticAlgorithm() {  delete pop;}voidGAGeneticAlgorithm::copy(const GAGeneticAlgorithm& ga) {  if(pop) pop->copy(*(ga.pop));  else pop = ga.pop->clone();  pop->geneticAlgorithm(*this);  stats = ga.stats;  params = ga.params;  cf = ga.cf; ud = ga.ud;  ngen = ga.ngen; nconv = ga.nconv; pconv = ga.pconv;  pcross = ga.pcross; pmut = ga.pmut; minmax = ga.minmax;  scross = ga.scross; across = ga.across;  d_seed = ga.d_seed;}const GAParameterList&GAGeneticAlgorithm::parameters(const GAParameterList& list){  for(int i=0; i<list.size(); i++)    setptr(list[i].fullname(), list[i].value());  return params;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -