gap
Class GPPopulation

java.lang.Object
  |
  +--gap.GPPopulation
All Implemented Interfaces:
java.io.Serializable

public class GPPopulation
extends java.lang.Object
implements java.io.Serializable

A population of GPPrograms.

Author:
Pietro Berkes, Samuele Pedroni
See Also:
Serialized Form

Field Summary
static int ADF_EXCL
          Used in methods with a "mask" argument.
 GPProgram bestOfRun
          Best individual since the creation of the population.
 double bestOfRunFitness
          Fitness of the individual since the creation of the population.
static int EMPTY_MASK
          Used in methods with a "mask" argument.
 double[] fitness
          The adjusted fitness for each individual.
 Evaluator judge
          The fitness evaluator.
 LogBook lb
          A log book.
 boolean open
          Flag, true after actions which invalidate the current state of the population.
protected  float overSelBoundary
          Percent of the population in which to apply over-selection.
protected  boolean overSelection
          Flag for over-selection.
 GPParams params
          The parameters shared by all the individuals.
 float pMutation
          Probability of mutation.
 GPProgram[] pop
          The population.
 float pXOver
          Probability of crossover.
static int REPLACE_BRANCH
          Replacing method.
static int REPLACE_PROGRAM
          Default replacing method.
static int REPLACE_TREE
          Replacing method.
 int size
          Number of individuals.
 double totFitness
          The sum of all the adjusted fitnesses.
 
Constructor Summary
  GPPopulation(int size, GPParams params, Evaluator judge)
          Create a brand new population.
protected GPPopulation(int size, GPParams params, Evaluator judge, int internal)
          Create a void population.
 
Method Summary
 int addADF(int type, int[] ct, java.lang.Class[] fSet, java.lang.Class[] tSet, int[] links)
          Add an ADF to the function set.
 void addArg(int type)
          Add an argument.
 void addFunction(java.lang.Class f)
          Add a function to the function set.
 void addTerminal(java.lang.Class t)
          Add a terminal to the terminal set.
protected static void allign(GPFunction f, int[] perm, int[] specPerm)
          Allign the indices of a subtree.
protected static void allign(GPProgram ind, int[] perm, int[] specPerm, int[][] adfPerm, int[][] adfSpecPerm)
          Allign the indices of an individual.
protected  void clearBestOfRun()
          Clear the BestOfRun data after a change of the parameters.
 void closeLogBook()
          Stop using a log book.
 void closePopulation()
          Do what must be done after changes in the parameters and before evaluation.
 void eval()
          Evaluate the fitness by mean of an Evaluator, transforms the standarized- in the adjusted fitness, sort the individuals and print the results.
protected  int fitnessDependentChoice()
          Select an individual.
 double getBestFitness()
          Return the fitness of the best individual.
 GPProgram getBestIndividual()
          Return the best individual.
protected  java.lang.String getMethodName(int method)
          Return the name of a replacing method.
 boolean getOverSelection()
          Return true if the over-selection option is on.
 float getOverSelectionBoundary()
          Return the over-selected percentual of the population, or -1 if the over-selection option is off.
 int getSize()
          Return the number of individuals.
static GPPopulation load(java.io.File f)
          Load a population.
static GPPopulation load(java.lang.String filename)
          Load a population.
static GPPopulation merge(GPPopulation[] p, GPParams mparams, Evaluator judge, int size, double prc)
          Merge some populations.
protected static GPProgram mergeChooseProgram(GPPopulation cp)
          Choose a program from a population (while merging).
protected  GPProgram modifyProgram(GPProgram progr, int method, java.util.ArrayList refs, java.util.ArrayList adfRefList)
          Modify an individual.
protected  GPProgram newIndividual()
          Create a new individual.
 void nextGeneration()
          Create a new generation.
protected  void quickSortByFitness(int b, int t)
          Sort the population by fitness, in decreasing order.
 void removeADF(int adfNo, int method)
          Remove an ADF.
 void removeArg(int argNo, int method)
          Remove an argument.
 void removeFunction(java.lang.Class func, int method)
          Remove a function.
 void removeFunction(int funcNo, int method)
          Remove a function.
 void removeTerminal(java.lang.Class term, int method)
          Remove a terminal.
 void removeTerminal(int termNo, int method)
          Remove a terminal.
 void save(java.io.File f)
          Save the population.
 void save(java.lang.String filename)
          Save the population.
 void setLogBook(LogBook lb)
          Set the log book.
 void setOverSelection(float boundary)
          Set the over-selection option on.
 void stopOverSelection()
          Set the over-selection option off.
protected static void verifyParamsComp(GPParams p, GPParams mp, int[] perm, int[] specPerm, int[][] adfPerm, int[][] adfSpecPerm, int mask)
          Verify that two parameters are equivalent.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pop

public GPProgram[] pop
The population.

params

public GPParams params
The parameters shared by all the individuals.

judge

public Evaluator judge
The fitness evaluator.

lb

public transient LogBook lb
A log book.

open

public boolean open
Flag, true after actions which invalidate the current state of the population.

fitness

public double[] fitness
The adjusted fitness for each individual.

The adjusted fitness is 0.0 for the worst individual, 1.0 for the best. The adjusted fitness is 1.0/(1.0+standarized_fitness).


totFitness

public double totFitness
The sum of all the adjusted fitnesses.

bestOfRun

public GPProgram bestOfRun
Best individual since the creation of the population.

bestOfRunFitness

public double bestOfRunFitness
Fitness of the individual since the creation of the population.

size

public int size
Number of individuals.

pXOver

public float pXOver
Probability of crossover.

pMutation

public float pMutation
Probability of mutation.

overSelection

protected boolean overSelection
Flag for over-selection. Empirical rule: over-select if #individuals >= 1000.

overSelBoundary

protected float overSelBoundary
Percent of the population in which to apply over-selection.

EMPTY_MASK

public static final int EMPTY_MASK
Used in methods with a "mask" argument. No special modifiers.

ADF_EXCL

public static final int ADF_EXCL
Used in methods with a "mask" argument. The ADF are excluded from the computation.

REPLACE_PROGRAM

public static final int REPLACE_PROGRAM
Default replacing method. Replace the program by a new one.

REPLACE_TREE

public static final int REPLACE_TREE
Replacing method. Replace the (main- or ADF-) tree by a new one.

REPLACE_BRANCH

public static final int REPLACE_BRANCH
Replacing method. Replace only the no more valid branches.
Constructor Detail

GPPopulation

public GPPopulation(int size,
                    GPParams params,
                    Evaluator judge)
             throws ParametersNotProperlySetException
Create a brand new population.
Parameters:
size - The number of individuals.
params - The parameters shared by all the individuals.
judge - The fitness evaluator.

GPPopulation

protected GPPopulation(int size,
                       GPParams params,
                       Evaluator judge,
                       int internal)
Create a void population.
Parameters:
size - The number of individuals.
params - The parameters shared by all the individuals.
judge - The fitness evaluator.
Method Detail

setLogBook

public void setLogBook(LogBook lb)
Set the log book.

closeLogBook

public void closeLogBook()
Stop using a log book.

getSize

public int getSize()
Return the number of individuals.

setOverSelection

public void setOverSelection(float boundary)
Set the over-selection option on. The request is refused if #individuals<500.
Parameters:
boundary - The over selection boundary. If <0.0 or >0.5, an automatical choice following the empirical rule 320.0/#individuals will be made.

stopOverSelection

public void stopOverSelection()
Set the over-selection option off.

getOverSelection

public boolean getOverSelection()
Return true if the over-selection option is on.

getOverSelectionBoundary

public float getOverSelectionBoundary()
Return the over-selected percentual of the population, or -1 if the over-selection option is off.

clearBestOfRun

protected void clearBestOfRun()
Clear the BestOfRun data after a change of the parameters. Some classes could be missing...

newIndividual

protected GPProgram newIndividual()
                           throws ParametersNotProperlySetException
Create a new individual.

merge

public static GPPopulation merge(GPPopulation[] p,
                                 GPParams mparams,
                                 Evaluator judge,
                                 int size,
                                 double prc)
                          throws ParametersNotProperlySetException
Merge some populations.

In each population choose 1/(no. populations)*size*prc individuals and put a clone in the new population. The selection is weighted by the fitness, overselection allowed.

Warning: if the fitness of the populations to merge isn't uptodate, the populations are going to be evaluated.

Parameters:
p - The populations to merge.
mparams - The parameters of the new population.
judge - The evaluator of the new population.
size - The size of the new population.
prc - The percent of the population that is not created ex novo (<1.0).
Throws:
ParametersNotProperlySetException - If the parameters of the source populations aren't compatible with those of the new population.
See Also:
overSelection

mergeChooseProgram

protected static GPProgram mergeChooseProgram(GPPopulation cp)
Choose a program from a population (while merging).

verifyParamsComp

protected static void verifyParamsComp(GPParams p,
                                       GPParams mp,
                                       int[] perm,
                                       int[] specPerm,
                                       int[][] adfPerm,
                                       int[][] adfSpecPerm,
                                       int mask)
                                throws ParametersNotProperlySetException
Verify that two parameters are equivalent. If not, throw a detailed exception.

allign

protected static void allign(GPProgram ind,
                             int[] perm,
                             int[] specPerm,
                             int[][] adfPerm,
                             int[][] adfSpecPerm)
Allign the indices of an individual.

allign

protected static void allign(GPFunction f,
                             int[] perm,
                             int[] specPerm)
Allign the indices of a subtree.

eval

public void eval()
Evaluate the fitness by mean of an Evaluator, transforms the standarized- in the adjusted fitness, sort the individuals and print the results.
See Also:
judge

getBestFitness

public double getBestFitness()
Return the fitness of the best individual. If the population is open, close it and eval the individuals.

getBestIndividual

public GPProgram getBestIndividual()
Return the best individual. If the population is open, close it and eval the individuals.

quickSortByFitness

protected void quickSortByFitness(int b,
                                  int t)
Sort the population by fitness, in decreasing order.

closePopulation

public void closePopulation()
Do what must be done after changes in the parameters and before evaluation.

nextGeneration

public void nextGeneration()
                    throws ParametersNotProperlySetException
Create a new generation.

The pXOver per cent of the new population is the result of cross-overs, another pMutation per cent is generated by mutation, and the rest is breeded by simple reproduction. In all the genetic operations the individual are selected in a fitness-dependent way.

If the population is open, it is closed and the individuals are evaluated.

See Also:
pXOver, pMutation, fitnessDependentChoice()

fitnessDependentChoice

protected int fitnessDependentChoice()
Select an individual.

The selection is weighted by the fitness. If the overselection option is set, the 80% of the selected individuals are under the overselection boundary.

See Also:
overSelection

removeADF

public void removeADF(int adfNo,
                      int method)
               throws RemoveFailureException
Remove an ADF.
Parameters:
adfNo - The number of the ADF to be removed.
method - REPLACE_PROGRAM, REPLACE_TREE or REPLACE_BRANCH allowed.
See Also:
REPLACE_PROGRAM, REPLACE_TREE, REPLACE_BRANCH

removeFunction

public void removeFunction(java.lang.Class func,
                           int method)
                    throws RemoveFailureException
Remove a function.
Parameters:
func - Class of the function to remove.
method - REPLACE_PROGRAM, REPLACE_TREE or REPLACE_BRANCH allowed.
See Also:
REPLACE_PROGRAM, REPLACE_TREE, REPLACE_BRANCH

removeFunction

public void removeFunction(int funcNo,
                           int method)
                    throws RemoveFailureException
Remove a function.
Parameters:
method - REPLACE_PROGRAM, REPLACE_TREE or REPLACE_BRANCH allowed.
See Also:
REPLACE_PROGRAM, REPLACE_TREE, REPLACE_BRANCH

removeTerminal

public void removeTerminal(java.lang.Class term,
                           int method)
                    throws RemoveFailureException
Remove a terminal.
Parameters:
term - Class of the terminal to remove.
method - REPLACE_PROGRAM, REPLACE_TREE or REPLACE_BRANCH allowed.
See Also:
REPLACE_PROGRAM, REPLACE_TREE, REPLACE_BRANCH

removeTerminal

public void removeTerminal(int termNo,
                           int method)
                    throws RemoveFailureException
Remove a terminal.
Parameters:
method - REPLACE_PROGRAM, REPLACE_TREE or REPLACE_BRANCH allowed.
See Also:
REPLACE_PROGRAM, REPLACE_TREE, REPLACE_BRANCH

removeArg

public void removeArg(int argNo,
                      int method)
               throws RemoveFailureException
Remove an argument.
Parameters:
method - REPLACE_PROGRAM, REPLACE_TREE or REPLACE_BRANCH allowed.
See Also:
REPLACE_PROGRAM, REPLACE_TREE, REPLACE_BRANCH

modifyProgram

protected GPProgram modifyProgram(GPProgram progr,
                                  int method,
                                  java.util.ArrayList refs,
                                  java.util.ArrayList adfRefList)
                           throws RemoveFailureException
Modify an individual.
Parameters:
progr - The program to modify.
method - REPLACE_PROGRAM, REPLACE_TREE or REPLACE_BRANCH allowed.
refs - A list of Refs to invalid points in the main programs.
adfRefLis - A list of lists (one for each ADF) to invalid points in the ADFs.
See Also:
REPLACE_PROGRAM, REPLACE_TREE, REPLACE_BRANCH

getMethodName

protected java.lang.String getMethodName(int method)
Return the name of a replacing method.

addADF

public int addADF(int type,
                  int[] ct,
                  java.lang.Class[] fSet,
                  java.lang.Class[] tSet,
                  int[] links)
           throws ParametersNotProperlySetException
Add an ADF to the function set.
Parameters:
type - Type of the root of the new ADF.
ct - Type of each argument.
fSet - A function set. If null, use the function set of the main program.
tSet - A terminal set. If null, use the terminal set of the main program.
links - Links to other ADFs. If null, no link will be established.
Returns:
The position to which the ADF is inserted.

addFunction

public void addFunction(java.lang.Class f)
                 throws ParametersNotProperlySetException
Add a function to the function set.
Parameters:
f - A subclass of GPFunction.

addTerminal

public void addTerminal(java.lang.Class t)
                 throws ParametersNotProperlySetException
Add a terminal to the terminal set.
Parameters:
t - A subclass of GPTerminal.

addArg

public void addArg(int type)
            throws ParametersNotProperlySetException
Add an argument.
Parameters:
type - Type of the new agument.

save

public void save(java.lang.String filename)
          throws java.io.IOException
Save the population.

save

public void save(java.io.File f)
          throws java.io.IOException
Save the population.

load

public static GPPopulation load(java.lang.String filename)
                         throws java.io.IOException,
                                java.lang.ClassNotFoundException
Load a population.

load

public static GPPopulation load(java.io.File f)
                         throws java.io.IOException,
                                java.lang.ClassNotFoundException
Load a population.