📄 symbreg.cc
字号:
// symbreg.cc/* ---------------------------------------------------------------Symbolic RegressionAn example for how to use 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 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--------------------------------------------------------------- */#include <iostream.h>#include <fstream.h>#include <strstream.h>#include <stdlib.h>#include <new.h> // For the new-handler#include <math.h> // fabs()#include <string.h>// Include header file of genetic programming system.#include "gp.h" #include "gpconfig.h"#include "symbreg.h"// Define the function to be symbolically regressed and set up// question and answer array with some points for the function#define EQUATION(x) (x*x*x*x + x*x*x + x*x + x)#define DATAPOINTS 10double ques[DATAPOINTS];double answ[DATAPOINTS];// The TeX-fileofstream tout;int printTexStyle=0;// 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;char *InfoFileName="data";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}, {"SteadyState", DATAINT, &cfg.SteadyState}, {"AddBestToNewPopulation", DATAINT, &cfg.AddBestToNewPopulation}, {"InfoFileName", DATASTRING, &InfoFileName}, {"", DATAINT, NULL}};// Print out a gene in typical math style. Don't be confused, I don't// make a difference whether this gene is the main program or an ADF,// I assume the internal structure is correct.void MyGene::printMathStyle (ostream& os, int lastPrecedence){ int precedence; // Function or terminal? if (isFunction ()) { // Determine operator priority switch (node->value ()) { case '*': case '%': precedence=1; break; case '+': case '-': precedence=0; break; case 'A': precedence=2; break; default: GPExitSystem ("MyGene::printMathStyle", "Undefined function value"); } // Do we need brackets? if (lastPrecedence>precedence) os << "("; // Print out the operator and the parameters switch (node->value ()) { case '*': NthMyChild(0)->printMathStyle (os, precedence); os << "*"; NthMyChild(1)->printMathStyle (os, precedence); break; case '+': NthMyChild(0)->printMathStyle (os, precedence); os << "+"; NthMyChild(1)->printMathStyle (os, precedence); break; case '-': NthMyChild(0)->printMathStyle (os, precedence); os << "-"; NthMyChild(1)->printMathStyle (os, precedence); break; case '%': NthMyChild(0)->printMathStyle (os, precedence); os << "%"; NthMyChild(1)->printMathStyle (os, precedence); break; case 'A': // This is the ADF0-function. We put the parameters in // brackets and start again with precedence 0. os << "ADF0 ("; NthMyChild(0)->printMathStyle (os, 0); os << ","; NthMyChild(1)->printMathStyle (os, 0); os << ")"; break; default: GPExitSystem ("MyGene::printMathStyle", "Undefined function value"); } // Do we need brackets? if (lastPrecedence>precedence) os << ")"; } // Print the terminal if (isTerminal ()) os << *node;}// Print out a gene in LaTeX-style. Don't be confused, I don't make a// difference whether this gene is the main program or an ADF, I// assume the internal structure is correct.void MyGene::printTeXStyle (ostream& os, int lastPrecedence){ int precedence=0; // Function or terminal? if (isFunction ()) { // Determine operator priority switch (node->value()) { case '*': case '%': precedence=2; break; case '+': case '-': precedence=1; break; case 'A': precedence=3; break; default: GPExitSystem ("MyGene::printTeXStyle", "Undefined function value"); } // Do we need brackets? if (lastPrecedence>precedence) os << "\\left("; // Print out the operator and the parameters switch (node->value()) { case '*': NthMyChild(0)->printTeXStyle (os, precedence); os << " "; NthMyChild(1)->printTeXStyle (os, precedence); break; case '+': NthMyChild(0)->printTeXStyle (os, precedence); os << "+"; NthMyChild(1)->printTeXStyle (os, precedence); break; case '-': NthMyChild(0)->printTeXStyle (os, precedence); os << "-"; NthMyChild(1)->printTeXStyle (os, precedence); break; case '%': // As we use \frac, we start again with precedence 0 os << "\\frac{"; NthMyChild(0)->printTeXStyle (os, 0); os << "}{"; NthMyChild(1)->printTeXStyle (os, 0); os << "}"; break; case 'A': // This is the ADF0-function. We put the parameters in // brackets and start again with precedence 0. os << "f_2("; NthMyChild(0)->printTeXStyle (os, 0); os << ","; NthMyChild(1)->printTeXStyle (os, 0); os << ")"; break; default: GPExitSystem ("MyGene::printTeXStyle", "Undefined function value"); } // Do we need brackets? if (lastPrecedence>precedence) os << "\\right)"; } // We can't let the terminal print itself, because we want to modify // it a little bit if (isTerminal ()) switch (node->value ()) { case 'X': os << "x"; break; case 1: os << "x_1"; break; case 2: os << "x_2"; break; default: GPExitSystem ("MyGene::printTeXStyle", "Undefined terminal value"); }}// Print a Gene.void MyGene::printOn (ostream& os){ if (printTexStyle) printTeXStyle (os); else printMathStyle (os);}// Print a GP. If we want a LaTeX-output, we must provide for the// equation environment, otherwise we simply call the print function// of our base class.void MyGP::printOn (ostream& os){ // If we use LaTeX-style, we provide here for the right equation // overhead used for LaTeX. if (printTexStyle) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -