gap
Class GPParams

java.lang.Object
  |
  +--gap.GPParams
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable

public class GPParams
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

The parameters for a GP-Problem.

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

Field Summary
protected  java.util.ArrayList adf_ct
          ADF compatibility table.
static int ADF_EXCL
          Used in methods with a "mask" argument.
protected  java.util.HashMap adfDescr
          Method descriptor of each ADF.
protected  java.lang.Class[][] adfFSet
          Function sets of the ADFs, if defined.
protected  int[][] adfLinks
          Links within the ADFs.
protected  gap.GPParamsArray adfParams
          Parameters of each ADF.
protected  java.lang.Class[][] adfTSet
          Terminal sets of the ADFs, if defined.
protected  java.util.ArrayList adfTypes
          Type of the root of each ADF.
protected  int[] argTypes
          The type of each argument.
protected  java.lang.String cast
          Casting string.
protected  boolean definitive
          Flag; true if the method definitive() has been called.
protected  int depthNewADF
          Maximum depth for new ADFs.
 int depthNewProgram
          Maximum depth for new main programs.
static int EMPTY_MASK
          Used in methods with a "mask" argument.
protected  boolean[] exist
          Flags; true if there exists a function at the corresponding index.
protected  boolean[] existADF
          Flags; true if there exists an ADF at the corresponding index.
protected  boolean[] existArg
          Flags; true if there exists an argument at the corresponding index.
protected  boolean[] existTerminal
          Flags; true if there exists a terminal at the corresponding index.
protected  int[][][] fct
          Function compatibility table.
protected static int FUNCTION
          Compatibility table type.
protected  gap.ClassArray functionSet
          Function set.
 double geneticOpInADFFreq
          Probability of applying a genetic operation in an ADF instead of in the main program.
protected  GPFunction[] instFSet
          An instance for each function in the function set.
protected  GPFunction[] instTSet
          An instance for each terminal in the terminalset.
 int maxDepth
          Maximum depth for the main program during the run.
protected  int maxDepthADF
          Maximum depth for ADFs during the run.
 int mutationDepth
          Maximum depth of new branches created during a mutation operation.
protected  int nADF
          Number of Automatically Defined Functions.
protected  int nArgs
          Number of arguments of the main program.
protected  int nFunctions
          Number of functions.
protected  int nPADF
          Number of ADFs, holes included.
protected  int nPArgs
          Number of arguments, holes included.
protected  int nPFunctions
          Number of functions in the function set, holes included.
protected  int nPTerminals
          Number of terminals in the temrminal set, holes included.
protected  int nTerminals
          Number of terminals.
protected  int[] root_compatibleTypes
          Array of types compatible with the root of the main GPProgram.
protected  int[] root_ct
          Root compatibility table.
protected  int[][][] tct
          Terminal compatibility table.
protected static int TERMINAL
          Compatibility table type.
 double terminalFreq
          Probability to choose a terminal while creating a program tree.
protected  gap.ClassArray terminalSet
          Terminal set.
 
Constructor Summary
  GPParams(java.lang.Class[] functionSet, java.lang.Class[] terminalSet, int[] argTypes)
          Construct new parameters.
  GPParams(java.lang.Class[] functionSet, java.lang.Class[] terminalSet, int[] argTypes, long seed)
          Construct new parameters.
  GPParams(java.lang.Class[] functionSet, java.lang.Class[] terminalSet, int[] argTypes, RandomGenerator rg)
          Construct new parameters.
protected GPParams(gap.ClassArray functionSet, gap.ClassArray terminalSet, int[] argTypes)
          Construct new parameters.
 
Method Summary
protected  int addADF(int type, int[] ct, java.lang.Class[] fSet, java.lang.Class[] tSet, int[] links)
          Add an ADF to the parameters.
protected  void addArg(int type)
          Add a new argument to the main program.
protected  void addFunction(java.lang.Class f, int mask)
          Add a function to the function set.
protected  void addTerminal(java.lang.Class t, int mask)
          Add a terminal to the terminal set.
 java.lang.Object clone()
          Clone.
 java.lang.Object clone(int mask)
          Clone.
 boolean exist(int i)
          Return true if there exist a function with index i.
 boolean existADF(int i)
          Return true if there exist an ADF with index i.
 boolean existArg(int i)
          Return true if there exist an argument with index i.
 boolean existTerminal(int i)
          Return true if there exist a terminal with index i.
 int[] getADFLinks(int i)
          Return the links of the i-th ADF.
 GPParams getADFParams(int i)
          Return the parameters of the i-th ADF.
 int getADFType(int i)
          Return the types of the i-th ADF.
 int getArgType(int i)
          Return the types of the arguments of the main program.
 int getDepthNewADF()
          Get the maximum depth for new ADFs.
 int getDepthNewProgram()
          Get the maximum depth for new programs.
 java.lang.Class[] getFunctionSet()
          Return the function set.
 double getGeneticOpInADFFreq()
          Get the probability of applying a genetic operation in an ADF instead of in the main program.
 int getMaxDepth()
          Get the max depth for the program during the run.
 int getMaxDepthADF()
          Get the maximum depth for ADFs during the run.
 int getMutationDepth()
          Get the maximum depth of new branches created during a mutation operation.
 int getNADFs()
          Return the number of ADFs.
 int getNArgs()
          Return the number of arguments of the main program.
 int getNFunctions()
          Return the number of functions.
 int getNPADFs()
          Return the lenght of the ADF arrays.
 int getNPArgs()
          Return the length of the arguments arrays.
 int getNPFunctions()
          Return the number of functions, holes included.
 int getNPTerminals()
          Return the number of terminals, holes included.
 int getNTerminals()
          Return the number of terminals.
 int[] getRootCompatibility()
          Get the types compatible with the root of the main program.
 double getTerminalFreq()
          Get the terminal frequency.
 java.lang.Class[] getTerminalSet()
          Return the terminal set.
protected  void init(java.lang.Class[] fSet, java.lang.Class[] tSet, int[] argTypes)
          Common initialization for the constructors.
protected  void init(gap.ClassArray fSet, gap.ClassArray tSet, int[] argTypes)
          Common initialization for the constructors.
 boolean isDefinitive()
          Return true if the parameters are definitive.
 void makeDefinitive()
          Set all compatibility tables and the ADF parameters.
protected  void makeDefinitive(int mask)
          Set all compatibility tables and the ADF parameters.
protected  int remove(int idx, int[][] ct)
          Remove an index from a compatibility table of a single function.
protected  int remove(int idx, int[][][] ct)
          Remove an index from a function compatibility table (e.g. fct or tct).
protected  void removeADF(int adfNo, int mask)
          Remove an ADF from the parameters.
protected  void removeArg(int argNo)
          Remove an argument from the parameters.
protected  void removeFunction(int funcNo, int mask)
          Remove a function from the parameters.
protected  void removeTerminal(int termNo, int mask)
          Remove a terminal from the parameters.
 void setADFFunctionSets(int adf_idx, java.lang.Class[] fs, java.lang.Class[] ts)
          Set the function- and the terminal set of an ADF.
 void setADFLinks()
          Set the links of all the ADFs so, that the i-th ADF can accede to ADF no. 0, 1, ..., i-1.
 void setADFLinks(int adf_idx, int[] links)
          Set the links of an ADF.
 void setADFs(int[] adfTypes, int[][] adf_ct)
          Create the Automatically Defined Functions.
protected  int setCompatibilityTable(int[][][] ct, int set)
          Fill a compatibility table.
 void setDepthNewADF(int depth)
          Set the maximum depth for new ADFs.
 void setDepthNewProgram(int depth)
          Set the maximum depth for new programs.
 void setGeneticOpInADFFreq(double freq)
          Set the probability of applying a genetic operation in an ADF instead of in the main program.
 void setMaxDepth(int depth)
          Set the max depth for the program during the run.
 void setMaxDepthADF(int depth)
          Set the maximum depth for ADFs during the run.
 void setMutationDepth(int depth)
          Set the maximum depth of new branches created during a mutation operation.
 void setRootCompatibility(int[] compTypes)
          Set the types compatible with the root of the main program.
 void setTerminalFreq(double freq)
          Set the terminal frequency.
protected  int[] type2idx(int[] type_ct, int set)
          Convert a type compatibility table in a function compatibility table.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

definitive

protected boolean definitive
Flag; true if the method definitive() has been called.

Only definitive parameters can be used to construct a new GP program. The ADFs, the function set, the terminal set and the arguments in definitive parameters can only be modified by mean of a GP population.


functionSet

protected gap.ClassArray functionSet
Function set.

Restrictions to the function set: there must be at least one function in the function set, each function must accept at least one terminal.


terminalSet

protected gap.ClassArray terminalSet
Terminal set.

Restrictions to the terminal set: there must be at least one terminal or argument.


instFSet

protected GPFunction[] instFSet
An instance for each function in the function set.

instTSet

protected GPFunction[] instTSet
An instance for each terminal in the terminalset.

argTypes

protected int[] argTypes
The type of each argument.

exist

protected boolean[] exist
Flags; true if there exists a function at the corresponding index.

existADF

protected boolean[] existADF
Flags; true if there exists an ADF at the corresponding index.

existTerminal

protected boolean[] existTerminal
Flags; true if there exists a terminal at the corresponding index.

existArg

protected boolean[] existArg
Flags; true if there exists an argument at the corresponding index.

nFunctions

protected int nFunctions
Number of functions.

nPFunctions

protected int nPFunctions
Number of functions in the function set, holes included.

nTerminals

protected int nTerminals
Number of terminals.

nPTerminals

protected int nPTerminals
Number of terminals in the temrminal set, holes included.

nArgs

protected int nArgs
Number of arguments of the main program.

nPArgs

protected int nPArgs
Number of arguments, holes included.

fct

protected int[][][] fct
Function compatibility table.

fct[i][j][] : array of indices of functions compatible with the j-th argument of the i-th function.


tct

protected int[][][] tct
Terminal compatibility table.

tct[i][j][] : array of indices of terminals compatible with the j-th argument of the i-th function.


root_ct

protected int[] root_ct
Root compatibility table.

Array of indices of functions compatible with the root of the main GP program.


root_compatibleTypes

protected int[] root_compatibleTypes
Array of types compatible with the root of the main GPProgram.

nADF

protected int nADF
Number of Automatically Defined Functions.

nPADF

protected int nPADF
Number of ADFs, holes included.

adfTypes

protected java.util.ArrayList adfTypes
Type of the root of each ADF.

adf_ct

protected java.util.ArrayList adf_ct
ADF compatibility table.

(int[])adf_ct.get(i): Array of types of functions compatible with the i-th ADF.


adfFSet

protected java.lang.Class[][] adfFSet
Function sets of the ADFs, if defined.

null if the ADF uses the main function set.


adfTSet

protected java.lang.Class[][] adfTSet
Terminal sets of the ADFs, if defined.

null if the ADF uses the main terminal set.


adfLinks

protected int[][] adfLinks
Links within the ADFs.

adfLinks[i][]: indices of ADFs accessible from the i-th ADF. Each index must be < i. Each array must be sorted in ascending order.


adfParams

protected gap.GPParamsArray adfParams
Parameters of each ADF.

adfDescr

protected java.util.HashMap adfDescr
Method descriptor of each ADF. For compilation purposes.

cast

protected java.lang.String cast
Casting string. For compilation purposes.

depthNewProgram

public int depthNewProgram
Maximum depth for new main programs.

maxDepth

public int maxDepth
Maximum depth for the main program during the run.

depthNewADF

protected int depthNewADF
Maximum depth for new ADFs.

maxDepthADF

protected int maxDepthADF
Maximum depth for ADFs during the run.

terminalFreq

public double terminalFreq
Probability to choose a terminal while creating a program tree.

Used in the creation of the main program, of the ADFs and of new branches while mutation or other genetic operations.


mutationDepth

public int mutationDepth
Maximum depth of new branches created during a mutation operation.

geneticOpInADFFreq

public double geneticOpInADFFreq
Probability of applying a genetic operation in an ADF instead of in the main program.

In mutations the number of the ADF will be chosen in a size-proportional way, while in cross-overs uniformly over the numbers of the existing ADFs.


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.

FUNCTION

protected static final int FUNCTION
Compatibility table type.
See Also:
type2idx(int[], int)

TERMINAL

protected static final int TERMINAL
Compatibility table type.
See Also:
type2idx(int[], int)
Constructor Detail

GPParams

public GPParams(java.lang.Class[] functionSet,
                java.lang.Class[] terminalSet,
                int[] argTypes)
         throws ParametersNotProperlySetException
Construct new parameters.
Parameters:
functionSet - An array of GPFunctions.
terminalSet - An array of GPTerminals.
argTypes - The type of each argument of the main GP program. null if no argument is needed.

GPParams

public GPParams(java.lang.Class[] functionSet,
                java.lang.Class[] terminalSet,
                int[] argTypes,
                long seed)
         throws ParametersNotProperlySetException
Construct new parameters.
Parameters:
functionSet - An array of GPFunctions.
terminalSet - An array of GPTerminals.
argTypes - The type of each argument of the main GP program. null if no argument is needed.
seed - The seed for the standard Park-Miller random number generator.

GPParams

public GPParams(java.lang.Class[] functionSet,
                java.lang.Class[] terminalSet,
                int[] argTypes,
                RandomGenerator rg)
         throws ParametersNotProperlySetException
Construct new parameters.
Parameters:
functionSet - An array of GPFunctions.
terminalSet - An array of GPTerminals.
argTypes - The type of each argument of the main GP program. null if no argument is needed.
rg - A random number generator.

GPParams

protected GPParams(gap.ClassArray functionSet,
                   gap.ClassArray terminalSet,
                   int[] argTypes)
            throws ParametersNotProperlySetException
Construct new parameters.
Parameters:
functionSet - A ClassArray of GPFunctions.
terminalSet - A ClassArray of GPTerminals.
argTypes - The type of each argument of the main GP program. null if no argument is needed.
Method Detail

isDefinitive

public boolean isDefinitive()
Return true if the parameters are definitive.

See Also:
Only definitive parameters can be used to construct a new GP program.

exist

public boolean exist(int i)
Return true if there exist a function with index i.

existADF

public boolean existADF(int i)
Return true if there exist an ADF with index i.

existTerminal

public boolean existTerminal(int i)
Return true if there exist a terminal with index i.

existArg

public boolean existArg(int i)
Return true if there exist an argument with index i.

getFunctionSet

public java.lang.Class[] getFunctionSet()
Return the function set.

getTerminalSet

public java.lang.Class[] getTerminalSet()
Return the terminal set.

getNFunctions

public int getNFunctions()
Return the number of functions.

getNPFunctions

public int getNPFunctions()
Return the number of functions, holes included. (Length of the function set).

getNTerminals

public int getNTerminals()
Return the number of terminals.

getNPTerminals

public int getNPTerminals()
Return the number of terminals, holes included. (Length of the terminal set).

getNADFs

public int getNADFs()
Return the number of ADFs.

getNPADFs

public int getNPADFs()
Return the lenght of the ADF arrays.

getNArgs

public int getNArgs()
Return the number of arguments of the main program.

getNPArgs

public int getNPArgs()
Return the length of the arguments arrays.

getArgType

public int getArgType(int i)
Return the types of the arguments of the main program.

getADFType

public int getADFType(int i)
Return the types of the i-th ADF.

getADFParams

public GPParams getADFParams(int i)
Return the parameters of the i-th ADF.

getADFLinks

public int[] getADFLinks(int i)
Return the links of the i-th ADF.

getRootCompatibility

public int[] getRootCompatibility()
Get the types compatible with the root of the main program.

setRootCompatibility

public void setRootCompatibility(int[] compTypes)
                          throws ParametersNotProperlySetException
Set the types compatible with the root of the main program.
Parameters:
compTypes - Array of types compatible with the root of the main GPProgram.

setDepthNewADF

public void setDepthNewADF(int depth)
                    throws ParametersNotProperlySetException
Set the maximum depth for new ADFs.

getDepthNewADF

public int getDepthNewADF()
Get the maximum depth for new ADFs.

setMaxDepthADF

public void setMaxDepthADF(int depth)
                    throws ParametersNotProperlySetException
Set the maximum depth for ADFs during the run.

getMaxDepthADF

public int getMaxDepthADF()
Get the maximum depth for ADFs during the run.

setDepthNewProgram

public void setDepthNewProgram(int depth)
Set the maximum depth for new programs.

getDepthNewProgram

public int getDepthNewProgram()
Get the maximum depth for new programs.

setMaxDepth

public void setMaxDepth(int depth)
Set the max depth for the program during the run.

getMaxDepth

public int getMaxDepth()
Get the max depth for the program during the run.

setTerminalFreq

public void setTerminalFreq(double freq)
Set the terminal frequency.
See Also:
terminalFreq

getTerminalFreq

public double getTerminalFreq()
Get the terminal frequency.
See Also:
terminalFreq

setMutationDepth

public void setMutationDepth(int depth)
Set the maximum depth of new branches created during a mutation operation.

getMutationDepth

public int getMutationDepth()
Get the maximum depth of new branches created during a mutation operation.

setGeneticOpInADFFreq

public void setGeneticOpInADFFreq(double freq)
Set the probability of applying a genetic operation in an ADF instead of in the main program.
See Also:
geneticOpInADFFreq

getGeneticOpInADFFreq

public double getGeneticOpInADFFreq()
Get the probability of applying a genetic operation in an ADF instead of in the main program.
See Also:
geneticOpInADFFreq

init

protected void init(java.lang.Class[] fSet,
                    java.lang.Class[] tSet,
                    int[] argTypes)
             throws ParametersNotProperlySetException
Common initialization for the constructors.

init

protected void init(gap.ClassArray fSet,
                    gap.ClassArray tSet,
                    int[] argTypes)
Common initialization for the constructors.

setADFs

public void setADFs(int[] adfTypes,
                    int[][] adf_ct)
             throws ParametersNotProperlySetException
Create the Automatically Defined Functions.
Parameters:
adfTypes - Type of the root of each ADF.
adf_ct - ADF compatibility table.
See Also:
adf_ct

setADFFunctionSets

public void setADFFunctionSets(int adf_idx,
                               java.lang.Class[] fs,
                               java.lang.Class[] ts)
                        throws ParametersNotProperlySetException
Set the function- and the terminal set of an ADF.
Parameters:
adf_idx - The index of the ADF to modify.
fs - The new function set.
ts - The new terminal set.
See Also:
functionSet, adfFSet, terminalSet, adfTSet

setADFLinks

public void setADFLinks(int adf_idx,
                        int[] links)
                 throws ParametersNotProperlySetException
Set the links of an ADF. An ADF can use the linked ADFs as functions of its function set.

The i-th ADF can link only ADFs with index < i.

Parameters:
adf_idx - The index of the ADF to modify.
links - The indices of the ADFs this ADF can link and use. If null, set the links to {0, 1, ..., adf_idx-1}.
See Also:
adfLinks

setADFLinks

public void setADFLinks()
                 throws ParametersNotProperlySetException
Set the links of all the ADFs so, that the i-th ADF can accede to ADF no. 0, 1, ..., i-1. An ADF can use the linked ADFs as functions of its function set.
See Also:
adfLinks

makeDefinitive

public void makeDefinitive()
                    throws ParametersNotProperlySetException
Set all compatibility tables and the ADF parameters.

Only definitive parameters can be used to construct a new GP program. Additional changes to the parameters can be made by mean of a GP population.

See Also:
definitive

makeDefinitive

protected void makeDefinitive(int mask)
                       throws ParametersNotProperlySetException
Set all compatibility tables and the ADF parameters.
Parameters:
mask - EMPTY_MASK, ADF_EXCL allowed.
See Also:
EMPTY_MASK, ADF_EXCL, definitive

removeADF

protected void removeADF(int adfNo,
                         int mask)
                  throws RemoveFailureException
Remove an ADF from the parameters.
Parameters:
adfNo - The index of the ADF to remove.
mask - EMPTY_MASK, ADF_EXCL allowed.
See Also:
EMPTY_MASK, ADF_EXCL

removeFunction

protected void removeFunction(int funcNo,
                              int mask)
                       throws RemoveFailureException
Remove a function from the parameters.
Parameters:
funcNo - The index of the function to remove.
mask - EMPTY_MASK, ADF_EXCL allowed.
See Also:
EMPTY_MASK, ADF_EXCL

removeTerminal

protected void removeTerminal(int termNo,
                              int mask)
                       throws RemoveFailureException
Remove a terminal from the parameters.
Parameters:
termNo - The index of the terminal to remove.
mask - EMPTY_MASK, ADF_EXCL allowed.
See Also:
EMPTY_MASK, ADF_EXCL

removeArg

protected void removeArg(int argNo)
                  throws RemoveFailureException
Remove an argument from the parameters.
Parameters:
argNo - The index of the argument to remove.

remove

protected int remove(int idx,
                     int[][][] ct)
Remove an index from a function compatibility table (e.g. fct or tct).
Parameters:
idx - The index to remove.
ct - The compatibility table.
Returns:
The idx of the last function which has an argument that is no more compatible with anything, -1 otherwise.

remove

protected int remove(int idx,
                     int[][] ct)
Remove an index from a compatibility table of a single function.
Parameters:
idx - The index to remove.
ct - The compatibility table.
Returns:
The index of the last argument which is no more compatible with anything, -1 otherwise.

addADF

protected int addADF(int type,
                     int[] ct,
                     java.lang.Class[] fSet,
                     java.lang.Class[] tSet,
                     int[] links)
              throws ParametersNotProperlySetException
Add an ADF to the parameters.
Parameters:
type - Type of the root of the new ADF.
ct - The 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 - The links.
Returns:
The position to which the ADF is inserted.
See Also:
adfLinks, setADFs(int[], int[][])

addFunction

protected void addFunction(java.lang.Class f,
                           int mask)
                    throws ParametersNotProperlySetException
Add a function to the function set.
Parameters:
f - A subclass of GPFunction.
mask - EMPTY_MASK, ADF_EXCL allowed.
See Also:
EMPTY_MASK, ADF_EXCL

addTerminal

protected void addTerminal(java.lang.Class t,
                           int mask)
                    throws ParametersNotProperlySetException
Add a terminal to the terminal set.
Parameters:
t - A subclass of GPTerminal.
mask - EMPTY_MASK, ADF_EXCL allowed.
See Also:
EMPTY_MASK, ADF_EXCL

addArg

protected void addArg(int type)
               throws ParametersNotProperlySetException
Add a new argument to the main program.
Parameters:
type - The type of the new argument.

type2idx

protected int[] type2idx(int[] type_ct,
                         int set)
Convert a type compatibility table in a function compatibility table.
Parameters:
type_ct - The type compatibility table, null if the function is compatible with everything.
set - The type of the table to create: a function c.t. or a terminal c.t.. Allowed: FUNCTION, TERMINAL.

setCompatibilityTable

protected int setCompatibilityTable(int[][][] ct,
                                    int set)
Fill a compatibility table.
Parameters:
ct - The compatibility table.
set - The type of the table to create: a function c.t. or a terminal c.t.. Allowed: FUNCTION, TERMINAL.
Returns:
The index of the last function not compatible with anything.

clone

public java.lang.Object clone()
Clone. The parameters must be definitive.

The random generator won't be cloned.

Overrides:
clone in class java.lang.Object
Returns:
A clone, null if the parameters aren't definitive.

clone

public java.lang.Object clone(int mask)
Clone. The parameters must be definitive.

The random generator won't be cloned.

Parameters:
mask - EMPTY_MASK, ADF_EXCL allowed.
See Also:
EMPTY_MASK, ADF_EXCL