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

📄 gp.h

📁 用C++编写的遗传算法
💻 H
📖 第 1 页 / 共 2 页
字号:
// gp.h/* -------------------------------------------------------------------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------------------------------------------------------------------- */// Genetic Program class definitions#ifndef __GP_H#define __GP_H#include <iostream.h>// Version and copyright messageextern char* GPVersion;void GPPrintCopyright (ostream& os);// Make internal checks or not. Better do it... it saved me a lot of// work I can tell.  It results in a speed loss of roughly 3%, that is// less than it sounds.  A good, optimising compiler can do much more// here.#define GPINTERNALCHECK 1// If an error occurs (GPExitSystem()), a segmentation fault helps// debugging the code to find the location that caused the error#define GPCREATE_SEGMENTATIONFAULT_ON_ERROR 1// Random number functions for gp systemvoid GPsrand (long);long GPrand ();int GPRandomPercent (double percent);// Init and exitvoid GPInit (int printCopyright, long seedRandomGenerator);void GPExitSystem (char *functionName, char *errorMessage); // Registering for load/saveclass GPObject;GPObject* GPCreateRegisteredClassObject (int ID);void GPRegisterClass (GPObject* gpo);void GPRegisterKernelClasses ();// Every class gets an unique identification number that is returned// by the function isA().#define GPObjectID       1#define GPContainerID    2#define GPNodeID         3#define GPNodeSetID      4#define GPAdfNodeSetID   5#define GPVariablesID    6#define GPGeneID         7#define GPID             8#define GPPopulationID   9#define GPUserID        50// ------------------------------------------------------------------// The base class for all other classes.  This is an abstract class.// The destructor is virtual.class GPObject{public:  GPObject () {}  virtual ~GPObject () {}  GPObject (const GPObject&) {}  // Duplicate object. This function must be provided by every  // inheriting class!!!  virtual GPObject& duplicate ()=0;  virtual int isA ()=0;  virtual char* load (istream& is)=0;  virtual void save (ostream& os)=0;  virtual GPObject* createObject()=0;  // Print function. It's virtual, so only one operator << is needed.  virtual void printOn (ostream& os)=0;};// Print operator for all classes that inherit from GPObjectinline ostream& operator << (ostream& os, GPObject& gpo){  gpo.printOn (os);  return os;}class GPContainer : public GPObject{public:  GPContainer ();  GPContainer (int numObjects);  virtual ~GPContainer ();  GPContainer (const GPContainer& gpc);  virtual GPObject& duplicate () { return *(new GPContainer(*this)); }  virtual void printOn (ostream& os);  void reserveSpace (int numObjects);  int containerSize() const { return contSize; }  GPObject* Nth (int n) const;  GPObject** getPointerAddress (int n) const;  void put (int n, GPObject& gpo);  GPObject& get (int n);  virtual int isA () { return GPContainerID; }  virtual char* load (istream& is);  virtual void save (ostream& os);  virtual GPObject* createObject() { return new GPContainer; }  GPContainer& operator = (GPContainer& gpo) {    GPExitSystem ("operator =", "Assignment operator not yet implemented");     return gpo; }protected:  void deleteContainer ();  // The actual container is an array of pointers to GPObject  GPObject** container;  // The container size  int contSize;};// ------------------------------------------------------------------class GPNode : public GPObject{public:  GPNode () { nodeValue=0; numOfArgs=0; representation=0; }  GPNode (int nVal, char* str, int args=0) : nodeValue(nVal),       numOfArgs(args) { representation=copyString (str); }  virtual ~GPNode () { delete [] representation; }  GPNode (const GPNode& gpo);  virtual GPObject& duplicate () { return *(new GPNode(*this)); }  virtual void printOn (ostream& os) { os << representation; }  int value () { return nodeValue; }  int isFunction () { return numOfArgs!=0; }  int isTerminal () { return numOfArgs==0; }  int arguments () { return numOfArgs; }  virtual int isA () { return GPNodeID; }  virtual char* load (istream& is);  virtual void save (ostream& os);  virtual GPObject* createObject() { return new GPNode; }  GPNode& operator = (GPNode& gpo) {    GPExitSystem ("operator =", "Assignment operator not yet implemented");     return gpo; }protected:  // The nodes numerical value, the number of arguments it takes (0 if  // it is a terminal) and its string representation.  We operate on a  // duplicate  int nodeValue;  int numOfArgs;  char* representation;  char *copyString (char *s);};class GPNodeSet : public GPContainer{public:  GPNodeSet () { numFunctions=0; numTerminals=0; }  GPNodeSet (int numOfNodes) : GPContainer (numOfNodes) {    numFunctions=0; numTerminals=0; }  GPNodeSet (const GPNodeSet& gpo) : GPContainer(gpo) {     numFunctions=gpo.numFunctions; numTerminals=gpo.numTerminals; }  virtual GPObject& duplicate () { return *(new GPNodeSet(*this)); }  virtual void put (int, GPObject&);  virtual void putNode (GPNode& gpo);  virtual void printOn (ostream& os);  virtual GPNode* searchForNode (int value);  GPNode* NthNode (int n) {    return (GPNode*) GPContainer::Nth (n); }  virtual GPNode& chooseFunction();  virtual GPNode& chooseTerminal();  virtual GPNode* chooseNodeWithArgs (int args);  virtual int isA () { return GPNodeSetID; }  virtual char* load (istream& is);  virtual void save (ostream& os);  virtual GPObject* createObject() { return new GPNodeSet; }  GPNodeSet& operator = (GPNodeSet& gpo) {    GPExitSystem ("operator =", "Assignment operator not yet implemented");     return gpo; }protected:  // Container contains functions from 0..numFunctions-1 and terminals  // from containerSize()-1-numTerminals..containerSize()-1  int numFunctions, numTerminals;};class GPAdfNodeSet : public GPContainer{public:  GPAdfNodeSet () {}  GPAdfNodeSet (int numOfTrees) : GPContainer(numOfTrees) {}  GPAdfNodeSet (const GPAdfNodeSet& gpo) : GPContainer(gpo) {}  virtual GPObject& duplicate () { return *(new GPAdfNodeSet(*this)); }  // As we have no variables, we leave load and save to our container  // class  virtual int isA () { return GPAdfNodeSetID; }  virtual GPObject* createObject() { return new GPAdfNodeSet; }  virtual void printOn (ostream& os);  GPNodeSet* NthNodeSet (int n) {     return (GPNodeSet*) GPContainer::Nth (n); }  GPAdfNodeSet& operator = (GPAdfNodeSet& gpo) {    GPExitSystem ("operator =", "Assignment operator not yet implemented");     return gpo; }};// ------------------------------------------------------------------// Prototypesclass GP;class GPPopulation;

⌨️ 快捷键说明

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