📄 gplib_user.cpp
字号:
// *****************// GPLIB_user.cpp// User defined functions// Colin Frayn// December 2006// *****************
// GPLib v2.0, A Genetic programming Library for C++
// Copyright (C) 2006 Colin Frayn
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "GPLib.h"// ----=== User Defined Functions ===---- // Initialise any user-defined variables at the beginning of each generationvoid GPLIB_Environment::InitialiseGeneration(void) { if (bVerbose) fprintf(stdout,"\n[G] Generation %d\n",Generation); TotalLength = 0; for (int n=0;n<GPLIB_MaxGenome;n++) LengthTally[n] = 0; best = worst = 0; longest = 0; totalscore = 0.0f;}// Gather statistics after each individual has been runvoid GPLIB_Environment::PostEvaluationStatistics(void) { int n = CurrentEntity; // Keep track of average length TotalLength += (int)Entitylist[n].genome.size(); LengthTally[(int)Entitylist[n].genome.size()]++; // Keep track of the longest genome if ((int)Entitylist[n].genome.size() > longest) longest = (int)Entitylist[n].genome.size(); // Don't deal with genomes that give illegal return results if (GPLIB_NotANumber(Entitylist[n].GetScore())) return; // Check if this entity has beaten the best score so far if (Entitylist[n].GetScore() > Entitylist[best].GetScore()) best = n; // Keep track of the worst score too if (Entitylist[n].GetScore() < Entitylist[worst].GetScore()) worst = n; // Keep track of the average score totalscore += Entitylist[n].GetScore();}// Any extra output required after each generationvoid GPLIB_Environment::PostRunStatistics(void) { if (bVerbose) { fprintf(stdout,"\n[G] Complete : Best score = %f",Entitylist[best].GetScore()); fprintf(stdout," [len=%d]\n",(int)Entitylist[best].genome.size()); fprintf(stdout,"[G] Average = %f Longest = %d nodes\n",totalscore/(float)Entitylist.size(),longest); }}// Write out any post-simulation statistics neededvoid GPLIB_Entity::MakeLean(void) { int len; // Make sure that we have the leanest version of this genome possible do { len = (int)genome.size(); EditGenome(); } while (len != (int)genome.size());}// Write the results of this generation to diskvoid GPLIB_Environment::WriteGenomeFile(int best, string filename) { int n; FILE *fp; // Write out best entity to a file if ((fp = fopen(filename.c_str(),"w")) == NULL) { fprintf(stderr,"GPLIB Error : Could not open genome file %s!\n",filename.c_str()); return; } PrintGenome(Entitylist[best].genome,fp); for (n=0;n<(int)Entitylist[best].genome.size();n++) { if ((Entitylist[best].genome[n]).func < 0) fprintf(fp,"0"); else fprintf(fp,"%d",Functions[(Entitylist[best].genome[n]).func]->GetNP()); } fprintf(fp,"\n"); fprintf(fp,"Score=%f\n",Entitylist[best].GetScore()); fprintf(fp,"Length=%d\n",(int)Entitylist[best].genome.size()); fclose(fp);}// Write out best/worst/average data to a filevoid GPLIB_Environment::WriteRecordFile(string filename){ FILE *fp; static int OverwriteRecord = 1; if (OverwriteRecord) { if ((fp = fopen(filename.c_str(),"w")) == NULL) { fprintf(stderr,"GPLIB Error : Could not open results file %s!\n",filename.c_str()); return; } // Print out the column titles fprintf(fp,"Best Score,Worst Score,Average Score,Average Length\n"); } else fp = fopen(filename.c_str(),"a"); OverwriteRecord = 0; fprintf(fp,"%f,%f,%f,%f\n",Entitylist[best].GetScore(),Entitylist[worst].GetScore(),totalscore/(float)Entitylist.size(),(float)TotalLength/(float)Entitylist.size()); fclose(fp);}// Write the tally of genome lengths to a filevoid GPLIB_Environment::WriteTallyFile(string filename) { FILE *fp; // Write out length tally to a file if ((fp = fopen(filename.c_str(),"w")) == NULL) { fprintf(stderr,"GPLIB Error : Could not open length tally file %s!\n",filename.c_str()); return; } // Print out the column titles fprintf(fp,"Length,Count\n"); for (int n=0;n<GPLIB_MaxGenome;n++) fprintf(fp,"%d,%d\n",n,LengthTally[n]); fclose(fp);}// Write the output of the evaluation of entity number 'n' to a filevoid GPLIB_Environment::WriteOutputFile(int n, string filename) { float GP_value,target_value; FILE *fp; if ((fp = fopen(filename.c_str(),"w")) == NULL) { fprintf(stderr,"GPLIB Error : Could not open output file %s!\n",filename.c_str()); return; } // Print out the column titles fprintf(fp,"%s,GP\n",strTarget.c_str()); // Loop through every data point in the loaded data for (int n=0;n<CountData();n++) { // Set the current data point SetCurrentDatum(n); // Run this entity on the currently loaded data, with CurrentDatum set as above. // Store the value that the GP returns. GP_value = Entitylist[n].RunEntity(); // Get the Y-value (the dependent variable) that we're trying to fit target_value = GetYVal(n); // Print this out to the file fprintf(fp,"%f,%f\n",target_value,GP_value); } fclose(fp);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -