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

📄 example.cpp

📁 遗传规划算法 智能算法:遗传规划
💻 CPP
字号:
// Example GP project
// Using GPLib v2.0
// Colin Frayn
// Birmingham, UK
// December, 2006

// This project loads in the example file example.csv and fits it using a GP.
// This is a very easy test problem to solve

// Edit the LoadCSV() line below to load in 'example2.csv' for a 3D function example.
// You will need to increase the population size and generation count to solve this (harder) example
// In fact, you are unlikely to solve it (it's a complicated example), but you can learn a lot about it
// By examining the function nodes that are frequenly used. Remember to re-run the GP evolution several times!

// See the datafiles themselves for details on the functions they represent.

#include <stdio.h>

// Include the header file for GPLib
#include "GPLib.h"

// Declare and initialsie the environment for our GP simulation
GPLIB_Environment *GPE = new GPLIB_Environment;

int main(void) {
  float last=0.0f;
  int stable=0;

  fprintf(stdout,"Demo Application\n");

  // Load in the CSV file specified
  // First (n-1) columns are the independent variables,
  // Last column is the dependent variable
  GPE->LoadCSV("example.csv");

  // Setup the population size, and randomly initialise it
  GPE->SetupPopulation(10000);

  // Show some feedback
  GPE->SetVerbose(true);
  GPE->ShowProgress(true);

  // The main generation loop
  // This simple problem should be solved long before the generation limit.
  for (int n=0;n<500;n++) {

    // Run this generation (run all entities, breed all entities)
    GPE->RunGeneration();

    // Write out a record of the results of this generation
    GPE->WriteRecordFile("record.out");    // Write out the genome for this generation's best entity    GPE->WriteGenomeFile(GPE->GetBestEntity(),"best.out");    // Write out a tally file for the number of entities of each length    GPE->WriteTallyFile("tally.out");    // Write out the evaluation of the specified individual    // (Run this individual on the data and output its answers next to the correct answers)    GPE->WriteOutputFile(GPE->GetBestEntity(),"eval.out");    // If the fitness has been almost stable for 15 generations, perform a consolidation, which replaces    // the less fit members of the population with mutated versions of the top N (in this case, 5).    if ((float)fabs(GPE->GetFitness(GPE->GetBestEntity()) - last) < 0.001f) stable++;    else stable=0;    last = GPE->GetFitness(GPE->GetBestEntity());    if (stable>=15) {      GPE->Consolidate(5);      stable=0;    }  }

  // Clear the memory, tidy up.
  delete GPE;
}

// The fitness function. This part must be written by the user.// This is the function that each entity runs in order to assess its own fitness.void GPLIB_Entity::CalculateFitness(void) {  float GP_value,target_value;  // Loop through every data point in the loaded data  for (int n=0; n<GPE->CountData(); n++) {    // Set the current data point    GPE->SetCurrentDatum(n);    // Run this entity on the currently loaded data, with CurrentDatum set as above.    // Store the value that the GP returns.    GP_value = RunEntity();    // Get the Y-value (the dependent variable) that we're trying to fit    target_value = GPE->GetYVal(n);    // Add on a score penalty based on the difference between the GP's return value and the target value    // You should explore these ones (and others) to understand the effect they have    // Use the raw score error    AddScore(-10.0f*(float)fabs(GP_value - target_value));    // Use the square of the error//    AddScore(-(GP_value - target_value)*(GP_value - target_value));    // Use a weighted error (like Chi^2, but avoiding problems with very small values)//    AddScore(-20.0f * (float)fabs(GP_value - target_value)/((float)fabs(target_value)+1.0f));  }  // Penalise for genome length  AddScore(-5*NodeCount());}

⌨️ 快捷键说明

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