Class gpjpp.GPRun
All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class gpjpp.GPRun

java.lang.Object
   |
   +----gpjpp.GPRun

public class GPRun
extends Object
Abstract class that encapsulates the details of running a genetic programming test in a Java console application.

The user must override two methods of this class: createNodeSet(), which defines the branch structure and the node types used in each branch of each tree; and createPopulation(), which creates an instance of a user-defined subclass of GPPopulation.

It is common to override another method of this class: createVariables(), which creates an instance of GPVariables with the default configuration parameters for the run. Frequently you may need to add problem-specific configuration variables (the size of an ant trail, e.g.), which is best done by subclassing GPVariables and overriding createVariables() to create an instance of your own class.

GPRun then handles numerous details of managing a test run. These include:

1. Initializing the random number generator used by gpjpp.

2. Reading a configuration file so that most properties of the test run can be adjusted without recompiling. Also creates a default configuration file if the named one isn't found.

3. Creating output files for run statistics and detailed population reporting.

4. Registering all necessary classes with the stream manager.

5. Trapping all exceptions to report a detailed stack trace in case of error.

6. Loading a previous checkpoint stream if found, or creating an initial population of individuals.

7. Displaying status output to the console window and also printing configurable output to report files.

8. Streaming a checkpoint file after a configurable number of generations in case a long run needs to be interrupted.

9. Running a configurable number of generations and terminating the run when a configurable fitness target is reached.

10.Writing configurable reports on the best individual at the end of a run, including graphic images of its tree structure and possibly its fitness performance (such as the trail of an ant or a lawnmower).

11.Running multiple runs until a configurable number of good runs is found, and timing each run's performance.

GPRun is divided into a number of reasonably small routines so that additional aspects of its behavior can be customized by creating a subclass. Nevertheless, it is not suitable for writing applets or other graphics-intensive Java applications. These could be written by using the lower level classes of gpjpp directly, since these classes enforce no user interface of their own.

Version:
1.0

Variable Index

 o adfNs
The branch and node definitions for all the GPs used during the run.
 o baseName
The base name for all input and output files used in the run.
 o cfg
The configuration variables used for the run.
 o cntGen
The number of generations until the next checkpoint will be stored.
 o curGen
The current generation number.
 o curRun
The current run number.
 o dout
The PrintStream used for the detailed population output file.
 o goodRuns
The number of good runs found so far.
 o newPop
The secondary population.
 o outBufSize
The size in bytes of the output buffer used for PrintStreams.
 o pop
The main population, instantiated in GPRun() and reused repeatedly in run().
 o RUNENDID
An ID code written at the end of checkpoint streams.
 o sout
The PrintStream used for statistics and summary reporting.
 o stmBufSize
The size in bytes of the input or output buffer used for a DataInputStream or a DataOutputStream during checkpoint loading or saving.

Constructor Index

 o GPRun(String, boolean)
This constructor allocates the basic data structures and prepares for a run.

Method Index

 o checkChar(boolean)
Returns the symbols displayed in the statistical display when a checkpoint is finished or skipped.
 o createNodeSet(GPVariables)
Returns a properly initialized instance of GPAdfNodeSet, containing the branch and node definitions for this problem.
 o createOrAppend(String, boolean)
Creates or appends to a specified file and returns a valid reference to a GPPrintStream for that file.
 o createPopulation(GPVariables, GPAdfNodeSet)
You must override this method to return a new instance of a user subclass of GPPopulation.
 o createRandomGenerator()
Returns a valid instance of Random used to generate all random numbers throughout the package.
 o createVariables()
Returns a valid instance of GPVariables.
 o echoPrint(String)
Prints a string to System.out and also to sout.
 o getCheckpointFile()
Returns an instantiated File of the stream file if checkpointing is enabled and the stream file exists.
 o getCheckpointName()
Returns the checkpoint file name, baseName+".stm" by default.
 o getDetailName()
Returns the detail file name, baseName+".det" by default.
 o getStatisticsName()
Returns the statistics file name, baseName+".stc" by default.
 o load(DataInputStream)
Loads a checkpoint file from the specified stream.
 o loadCheckpoint()
Loads a checkpoint if cfg.CheckpointGens is greater than zero and the checkpoint file is found.
 o readIni(String, boolean)
Reads the specified file to get run configuration variables.
 o registerAllClasses()
Registers with the GPObject stream manager all classes needed for checkpointing.
 o run()
The main method of GPRun creates and evolves populations, writes reports, loads and saves checkpoint files, and does multiple runs until a configurable number of good ones is found.
 o save(DataOutputStream)
Saves a checkpoint to the specified stream.
 o saveCheckpoint()
Saves a checkpoint to disk if cfg.CheckpointGens is greater than zero and that many generations has passed since the last checkpoint.
 o showConfiguration()
Prints the configuration variables and the node definitions to sout.
 o showCreation(boolean)
Prints a status message to System.out while the initial population is being created.
 o showFinalGeneration(int, boolean)
Prints information about the final generation of a run.
 o showGeneration(boolean, int, boolean)
Prints information to System.out and to sout about the generation just completed.
 o showRunNumber(int, int)
Prints the current run number and number of good runs so far to System.out, sout, and dout.
 o showTiming(double, double)
Print to System.out and to sout the total elapsed seconds for a run and also the number of seconds to process each generation.

Variables

 o RUNENDID
  protected final static int RUNENDID
An ID code written at the end of checkpoint streams. This is primarily useful for checking to see whether a stream was fully flushed in the event of a machine crash.
 o outBufSize
  protected static int outBufSize
The size in bytes of the output buffer used for PrintStreams. These streams are flushed at the end of every line, so there is little point in using a larger buffer.
 o stmBufSize
  protected static int stmBufSize
The size in bytes of the input or output buffer used for a DataInputStream or a DataOutputStream during checkpoint loading or saving. A reasonably large buffer provides better performance, since these streams are composed of very many small data items.
 o baseName
  protected String baseName
The base name for all input and output files used in the run. To get the configuration file name, ".ini" is appended to baseName. To get the statistics file name, ".stc" is appended to baseName. To get the detailed population file name, ".det" is appended to baseName. To get the checkpoint file name, ".stm" is appended to baseName. This field is passed as a parameter to the GPRun constructor.
 o cfg
  protected GPVariables cfg
The configuration variables used for the run. The instance is created by the createVariables() abstract method. Its values are assigned from the configuration file (baseName+".ini") if found.
 o adfNs
  protected GPAdfNodeSet adfNs
The branch and node definitions for all the GPs used during the run.
 o dout
  protected GPPrintStream dout
The PrintStream used for the detailed population output file. If cfg.PrintDetails is false, this field is null and the report file is not created.
 o sout
  protected GPPrintStream sout
The PrintStream used for statistics and summary reporting. This file (baseName+".stc") is always created. It includes a list of the configuration parameters, a list of the branch and node definitions, the printStatistics output for each generation, timing per run and per generation, and a report on the best individual at the end of each run. In case a run is restarted from a checkpoint, additional output is appended to the existing statistics file if found.
 o pop
  protected GPPopulation pop
The main population, instantiated in GPRun() and reused repeatedly in run().
 o newPop
  protected GPPopulation newPop
The secondary population. If cfg.SteadyState is true, newPop is not used and remains null. Otherwise each new generation is created in the run() method by calling pop.generate(newPop). Then the newPop and pop references are swapped to proceed with the next generation.
 o curGen
  protected int curGen
The current generation number. It starts with 0 for the initial population and increments to cfg.NumberOfGenerations. When restarted from a checkpoint, the run starts again from the last generation that was checkpointed.
 o curRun
  protected int curRun
The current run number.
 o goodRuns
  protected int goodRuns
The number of good runs found so far. A good run is one where the best individual's fitness becomes less than cfg.TerminationFitness.
 o cntGen
  protected int cntGen
The number of generations until the next checkpoint will be stored. If cfg.CheckpointGens is 0, this field is not used. Otherwise it is initialized to cfg.CheckpointGens and decremented after each generation.

Constructors

 o GPRun
  public GPRun(String baseName,
               boolean createDefaultIni)
This constructor allocates the basic data structures and prepares for a run. GPRun() traps all exceptions. If it catches one, it dumps a stack trace and calls System.exit(1).
Parameters:
baseName - specifies the base file name for the run's configuration file, output files, and stream file.
createDefaultIni - if true, and if baseName+".ini" is not found, GPRun creates a configuration file holding the default configuration values.

Methods

 o createNodeSet
  protected abstract GPAdfNodeSet createNodeSet(GPVariables cfg)
Returns a properly initialized instance of GPAdfNodeSet, containing the branch and node definitions for this problem. You must override this method to create your own set of functions and terminals. Your version can create a different node set depending on whether cfg.UseADFs is true or false. createNodeSet() can also refer to other user-defined configuration parameters when cfg is a subclass of GPVariables to further customize the node set to the problem configuration.
 o createPopulation
  protected abstract GPPopulation createPopulation(GPVariables cfg,
                                                   GPAdfNodeSet adfNs)
You must override this method to return a new instance of a user subclass of GPPopulation. The user subclass must at least override the createGP() method of GPPopulation to create instances of a user subclass of GP. If the configuration does not specify a steady state population, createPopulation() is called twice; otherwise it is called just once regardless of the number of generations or runs.
 o run
  protected void run()
The main method of GPRun creates and evolves populations, writes reports, loads and saves checkpoint files, and does multiple runs until a configurable number of good ones is found. run() traps all exceptions. If it catches one, it dumps a stack trace and calls System.exit(1). run() calls a number of small methods for reporting output to the console and to files; these can be overridden to make small changes to its behavior. However, most changes are accomplished by modifying the configuration file that controls the run.
 o createVariables
  protected GPVariables createVariables()
Returns a valid instance of GPVariables. Override to create an instance of a subclass of GPVariables that contains problem-specific variables as well.
 o createRandomGenerator
  protected Random createRandomGenerator()
Returns a valid instance of Random used to generate all random numbers throughout the package. The default implementation uses the current time to seed the generator.
 o readIni
  protected void readIni(String iniName,
                         boolean createDefaultIni) throws IOException
Reads the specified file to get run configuration variables. Calls GPProperties.load() to interpret the file. If the file is not found and createDefaultIni is true, readIni() creates a new file and writes the current values of cfg to this file using GPVariables.printOn.
See Also:
load, printOn
 o createOrAppend
  protected GPPrintStream createOrAppend(String fname,
                                         boolean append) throws IOException
Creates or appends to a specified file and returns a valid reference to a GPPrintStream for that file. If append is true and fname exists, createOrAppend() renames fname to "tmp.tmp", opens tmp.tmp for reading, creates fname anew, copies tmp.tmp to fname, deletes tmp.tmp, and leaves fname open for writing. Otherwise, createOrAppend() creates fname, overwriting any existing file of that name without warning.
 o echoPrint
  protected void echoPrint(String dispStr)
Prints a string to System.out and also to sout.
 o showConfiguration
  protected void showConfiguration()
Prints the configuration variables and the node definitions to sout.
 o showRunNumber
  protected void showRunNumber(int curRun,
                               int goodRuns)
Prints the current run number and number of good runs so far to System.out, sout, and dout.
 o showCreation
  protected void showCreation(boolean preCreation)
Prints a status message to System.out while the initial population is being created. When the population is done, finishes the status message and writes to System.out and sout the number of individuals that were rejected because they were too complex or were duplicates.
 o checkChar
  protected char checkChar(boolean savedCheckpoint)
Returns the symbols displayed in the statistical display when a checkpoint is finished or skipped. The default implementation returns 'c' for a checkpoint and ' ' for no checkpoint.
 o showGeneration
  protected void showGeneration(boolean showLegend,
                                int curGen,
                                boolean savedCheckpoint)
Prints information to System.out and to sout about the generation just completed. GPPopulation.printStatisticsLegend() is called if showLegend is true. GPPopulation.printStatistics() is always called. GPPopulation.printDetails() is called only if cfg.PrintDetails is true and it prints to dout.
 o showFinalGeneration
  protected void showFinalGeneration(int curGen,
                                     boolean goodRun) throws IOException
Prints information about the final generation of a run. Calls GPPopulation.printDetails to write information about the run's best individual to sout. If cfg.PrintTree is true, a GPDrawing drawing surface is created and GP.drawOn is called to draw the best individual's trees to gif files. These files are named as follows: baseName+curRun+branchName+".gif" where branchName is "RPB" for the result-producing branch and "ADFn" for the ADF branches, or "" for single-branch trees.

For some Java implementations, Microsoft J++ in particular, the console window loses focus temporarily while the off-screen drawing window is active. Focus is returned to the previous window once drawing is complete. The same behavior is not seen for the Sun virtual machine running under Windows 95.

Parameters:
curGen - the final generation number, which can be cfg.NumberOfGenerations or less.
goodrun - true if the best individual's fitness was less than cfg.TerminationFitness.
Throws: IOException
if an I/O error occurs while writing gif files.
 o showTiming
  protected void showTiming(double elapsedSecs,
                            double secsPerGen)
Print to System.out and to sout the total elapsed seconds for a run and also the number of seconds to process each generation. The latter figure does not include time spent creating the initial population or printing details about the final generation.
 o registerAllClasses
  protected void registerAllClasses() throws IllegalAccessException
Registers with the GPObject stream manager all classes needed for checkpointing. Does nothing if cfg.CheckpointGens is 0 or less. Also creates the node index used to store the multitudinous genes efficiently on the stream.
Throws: IllegalAccessException
if any registered class or null constructor is not public.
See Also:
createNodeIndex
 o getCheckpointName
  protected String getCheckpointName()
Returns the checkpoint file name, baseName+".stm" by default.
 o getStatisticsName
  protected String getStatisticsName()
Returns the statistics file name, baseName+".stc" by default.
 o getDetailName
  protected String getDetailName()
Returns the detail file name, baseName+".det" by default.
 o getCheckpointFile
  protected File getCheckpointFile()
Returns an instantiated File of the stream file if checkpointing is enabled and the stream file exists. Otherwise returns null.
 o loadCheckpoint
  protected boolean loadCheckpoint() throws FileNotFoundException, IOException
Loads a checkpoint if cfg.CheckpointGens is greater than zero and the checkpoint file is found. For a successful load, the configuration variables and node definitions in this run must exactly match those in force when the checkpoint file was created. loadCheckpoint() generates a RuntimeException with an informative message if this is not the case. You can either bring the current configuration in line with the original run or delete the stream file to start over.

After the checkpoint is loaded, the run picks up exactly where it left off when the checkpoint was stored. The only difference is that the random number sequence used in the new run won't match any results obtained after the original checkpoint was stored. (The random number seed is not stored on the stream.)

Returns:
true if a checkpoint was loaded; otherwise false.
Throws: FileNotFoundException
if the stream file is not found (shouldn't happen).
Throws: IOException
if an I/O error occurs while reading the stream.
Throws: RuntimeException
if the current configuration doesn't match the checkpoint's.
 o saveCheckpoint
  protected boolean saveCheckpoint() throws IOException
Saves a checkpoint to disk if cfg.CheckpointGens is greater than zero and that many generations has passed since the last checkpoint. Returns true if a checkpoint was saved.
 o load
  protected void load(DataInputStream is) throws ClassNotFoundException, IOException, InstantiationException, IllegalAccessException
Loads a checkpoint file from the specified stream. The stream contains an image of the configuration variables, the node set, the current run and generation numbers, the population of individuals, and a terminator code. If the configuration variables or node set don't match those of the current run, load() throws an InstantiationException which is caught by loadCheckpoint().
Throws: ClassNotFoundException
if the class indicated by the stream's ID code is not registered with GPObject.
Throws: InstantiationException
if an error occurs while calling new or the null constructor of the specified class.
Throws: IllegalAccessException
if a class or its null constructor is not public.
Throws: IOException
if an error occurs while reading the stream.
 o save
  protected void save(DataOutputStream os) throws IOException
Saves a checkpoint to the specified stream.
Throws: IOException
if an error occurs while writing the stream.
See Also:
load

All Packages  Class Hierarchy  This Package  Previous  Next  Index