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

📄 skeleton.cc

📁 用C++编写的遗传算法
💻 CC
字号:
// skeleton.cc/* -------------------------------------------------------------------This is the skeleton for a new problem the user wants to tackle usinggenetic programming techniques.gpc++ - The Genetic Programming KernelThis program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 1, or (at your option)any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.Copyright 1993, 1994 Adam P. Fraser and 1996, 1997 Thomas WeinbrennerFor comments, improvements, additions (or even money) contact:Thomas WeinbrennerGrauensteinstr. 2635789 LaimbachGermanyE-mail: thomasw@emk.e-technik.th-darmstadt.deWWW:    http://www.emk.e-technik.th-darmstadt/~thomasw  or (Address may be out of date)Adam Fraser, Postgraduate Section, Dept of Elec & Elec Eng,Maxwell Building, University Of Salford, Salford, M5 4WT, United Kingdom.E-mail: a.fraser@eee.salford.ac.ukTel:    (UK) 061 745 5000 x3633Fax:    (UK) 061 745 5999------------------------------------------------------------------- */#include <stdlib.h>#include <new.h>#include <fstream.h>#include <strstream.h>#include "gp.h"#include "gpconfig.h"// Define configuration parameters and the neccessary array to// read/write the configuration to a file.  If you need more// variables, just add them below and insert an entry in the// configArray.GPVariables cfg;struct GPConfigVarInformation configArray[]={  {"PopulationSize", DATAINT, &cfg.PopulationSize},  {"NumberOfGenerations", DATAINT, &cfg.NumberOfGenerations},  {"CreationType", DATAINT, &cfg.CreationType},  {"CrossoverProbability", DATADOUBLE, &cfg.CrossoverProbability},  {"CreationProbability", DATADOUBLE, &cfg.CreationProbability},  {"MaximumDepthForCreation", DATAINT, &cfg.MaximumDepthForCreation},  {"MaximumDepthForCrossover", DATAINT, &cfg.MaximumDepthForCrossover},  {"SelectionType", DATAINT, &cfg.SelectionType},  {"TournamentSize", DATAINT, &cfg.TournamentSize},  {"DemeticGrouping", DATAINT, &cfg.DemeticGrouping},  {"DemeSize", DATAINT, &cfg.DemeSize},  {"DemeticMigProbability", DATADOUBLE, &cfg.DemeticMigProbability},  {"SwapMutationProbability", DATADOUBLE, &cfg.SwapMutationProbability},  {"ShrinkMutationProbability", DATADOUBLE, &cfg.ShrinkMutationProbability},  {"AddBestToNewPopulation", DATAINT, &cfg.AddBestToNewPopulation},  {"SteadyState", DATAINT, &cfg.SteadyState},  {"", DATAINT, NULL}};// Define function and terminal identifiersconst int FUNCTION1=0;const int FUNCTION2=1;const int TERMINAL1=2;const int TERMINAL2=3;// Define class identifiersconst int MyGeneID=GPUserID;const int MyGPID=GPUserID+1;const int MyPopulationID=GPUserID+2;// Inherit the three GP classes GPGene, GP and GPPopulationclass MyGene : public GPGene{public:  // Duplication (mandatory)  MyGene (const MyGene& gpo) : GPGene (gpo) { }  virtual GPObject& duplicate () { return *(new MyGene(*this)); }  // Creation of own class objects (mandatory)  MyGene (GPNode& gpo) : GPGene (gpo) {}  virtual GPGene* createChild (GPNode& gpo) {    return new MyGene (gpo); }  // Tree evaluation (not mandatory, but somehow the trees must be  // parsed to evaluate the fitness)  double evaluate ();  // Load and save (not mandatory)  MyGene () {}  virtual int isA () { return MyGeneID; }  virtual GPObject* createObject() { return new MyGene; }  // virtual char* load (istream& is);  // virtual void save (ostream& os);  // Print (not mandatory)   // virtual void printOn (ostream& os);  // Access children (not mandatory)  MyGene* NthMyChild (int n) {    return (MyGene*) GPContainer::Nth (n); }};class MyGP : public GP {public:  // Duplication (mandatory)  MyGP (MyGP& gpo) : GP (gpo) { }  virtual GPObject& duplicate () { return *(new MyGP(*this)); }  // Creation of own class objects (mandatory)  MyGP (int genes) : GP (genes) {}  virtual GPGene* createGene (GPNode& gpo) {    return new MyGene (gpo); }  // Tree evaluation (mandatory)  virtual void evaluate ();  // Print (not mandatory)   // virtual void printOn (ostream& os);  // Load and save (not mandatory)  MyGP () {}  virtual int isA () { return MyGPID; }  virtual GPObject* createObject() { return new MyGP; }  // virtual char* load (istream& is);  // virtual void save (ostream& os);  // Access trees (not mandatory)  MyGene* NthMyGene (int n) {    return (MyGene*) GPContainer::Nth (n); }};class MyPopulation : public GPPopulation{public:  // Constructor (mandatory)  MyPopulation (GPVariables& GPVar_, GPAdfNodeSet& adfNs_) :     GPPopulation (GPVar_, adfNs_) {}  // Duplication (mandatory)  MyPopulation (MyPopulation& gpo) : GPPopulation(gpo) {}  virtual GPObject& duplicate () { return *(new MyPopulation(*this)); }  // Creation of own class objects (mandatory)  virtual GP* createGP (int numOfTrees) { return new MyGP (numOfTrees); }  // Load and save (not mandatory)  MyPopulation () {}  virtual int isA () { return MyPopulationID; }  virtual GPObject* createObject() { return new MyPopulation; }  // virtual char* load (istream& is);  // virtual void save (ostream& os);  // Print (not mandatory)   // virtual void printOn (ostream& os);  // Access genetic programs (not mandatory)  MyGP* NthMyGP (int n) {    return (MyGP*) GPContainer::Nth (n); }};// This function evaluates the fitness of a genetic tree.  We have the// freedom to define this function in any way we like.  double MyGene::evaluate (){  double returnValue;  switch (node->value ())    {    case FUNCTION1:       returnValue=NthMyChild(0)->evaluate ();      break;    case FUNCTION2:       returnValue=NthMyChild(0)->evaluate ();      break;          case TERMINAL1:      returnValue=0.0;      break;          case TERMINAL2:      returnValue=0.0;      break;    default:       GPExitSystem ("MyGene::evaluate", "Undefined node value");    }    return returnValue;}// Evaluate the fitness of a GP and save it into the class variable// fitness.void MyGP::evaluate (){  // Evaluate main tree  stdFitness=NthMyGene (0)->evaluate ();}// Create function and terminal setvoid createNodeSet (GPAdfNodeSet& adfNs){  // Reserve space for the node sets  adfNs.reserveSpace (1);    // Now define the function and terminal set for each ADF and place  // function/terminal sets into overall ADF container  GPNodeSet& ns=*new GPNodeSet (4);  adfNs.put (0, ns);    // Define functions/terminals and place them into the appropriate  // sets.  Terminals take two arguments, functions three (the third  // parameter is the number of arguments the function has)  ns.putNode (*new GPNode (FUNCTION1, "FUNCTION1", 1));  ns.putNode (*new GPNode (FUNCTION2, "FUNCTION2", 1));  ns.putNode (*new GPNode (TERMINAL1, "TERMINAL1"));  ns.putNode (*new GPNode (TERMINAL2, "TERMINAL2"));}void newHandler (){  cerr << "\nFatal error: Out of memory." << endl;  exit (1);}int main (){  // Set up a new-handler, because we might need a lot of memory, and  // we don't know it's there.  set_new_handler (newHandler);    // Init GP system.  GPInit (1, -1);    // Read configuration file.  GPConfiguration config (cout, "skeleton.ini", configArray);    // Print the configuration  cout << cfg << endl;    // Create the adf function/terminal set and print it out.  GPAdfNodeSet adfNs;  createNodeSet (adfNs);  cout << adfNs << endl;     // Open the main output file for the data and statistics file.  // First set up names for data file.  Remember we should delete the  // string from the stream, well just a few bytes  ostrstream strOutFile, strStatFile;  strOutFile  << "data.dat" << ends;  strStatFile << "data.stc" << ends;  ofstream fout (strOutFile.str());  ofstream bout (strStatFile.str());    // Create a population with this configuration  cout << "Creating initial population ..." << endl;  MyPopulation* pop=new MyPopulation (cfg, adfNs);  pop->create ();  cout << "Ok." << endl;  pop->createGenerationReport (1, 0, fout, bout);    // This next for statement is the actual genetic programming system  // which is in essence just repeated reproduction and crossover loop  // through all the generations ...  MyPopulation* newPop=NULL;  for (int gen=1; gen<=cfg.NumberOfGenerations; gen++)    {      // Create a new generation from the old one by applying the      // genetic operators      if (!cfg.SteadyState)	newPop=new MyPopulation (cfg, adfNs);      pop->generate (*newPop);            // Delete the old generation and make the new the old one      if (!cfg.SteadyState)	{	  delete pop;	  pop=newPop;	}      // Create a report of this generation and how well it is doing      pop->createGenerationReport (0, gen, fout, bout);    }  return 0;}

⌨️ 快捷键说明

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