Venice 0.7beta

org.mov.analyser.gp
Class Mutator

java.lang.Object
  extended by org.mov.analyser.gp.Mutator

public class Mutator
extends java.lang.Object

The mutator can build random expressions and randomly mutate existing expressions. This class is at the heart of the GP as it creates the random buy and sell rules and combines the rules during "breeding".

Author:
Andrew Leppard
See Also:
Individual, GeneticProgramme

Constructor Summary
Mutator(java.util.Random random, GPGondolaSelection GPGondolaSelection, boolean allowHeld, boolean allowOrder)
          Create a new mutator.
 
Method Summary
 Expression createRandom(Expression model, int type, int subType, int level)
          Create a new random expression based on mutating the given expression.
 Expression createRandom(int type)
          Create a new random expression of the given type.
 Expression createRandom(int type, int subType)
          Create a new random expression of the given type and subType.
 Expression createRandom(int type, int subType, int level)
          Create a new random expression of the given type,subType at the given level.
 Expression createRandomNonTerminal(Expression model, int type, int subType, int level)
          Create a new random non-terminal expression based on mutating the given expression.
 Expression createRandomNonTerminal(int type)
          Create a new random non-terminal expression of the given type.
 Expression createRandomNonTerminal(int type, int subType)
          Create a new random non-terminal expression of the given type and subType.
 Expression createRandomNonTerminal(int type, int subType, int level)
          Create a new random non-terminal expression of the given type and subType at the given level.
 Expression createRandomTerminal(int type)
          Creates a random terminal expression of the given type.
 Expression createRandomTerminal(int type, int subType)
          Creates a random terminal expression of the given type and subType.
 Expression delete(Expression root, Expression destination)
          Perform a deletion mutation on the given expression.
 Expression findRandomSite(Expression expression)
          Randomly pick a node in the given expression.
 Expression findRandomSite(Expression expression, int type)
          Randomly pick a node of the given type in the given expression.
 Expression insert(Expression root, Expression destination, Expression source)
          Perform an insertion mutation on the given expression.
 Expression modify(Expression root, Expression destination)
          Perform a modification mutation on the given expression.
 Expression mutate(Expression expression)
          Possibly mutate the given expression.
 Expression mutate(Expression expression, int percent)
          Possibly mutate the given expression
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Mutator

public Mutator(java.util.Random random,
               GPGondolaSelection GPGondolaSelection,
               boolean allowHeld,
               boolean allowOrder)
Create a new mutator.

Parameters:
random - use this random number generator
GPGondolaSelection - UI containing user's desired expression probabilities
allowHeld - allow the creation of the held variable
allowOrder - allow the creation of the order variable
Method Detail

createRandom

public Expression createRandom(int type)
Create a new random expression of the given type.

Parameters:
type - the type of the expression, e.g. Expression.BOOLEAN_TYPE
Returns:
a randomly generated expression

createRandom

public Expression createRandom(int type,
                               int subType)
Create a new random expression of the given type and subType.

Parameters:
type - the type of the expression, e.g. Expression.BOOLEAN_TYPE
subType - the subType of the expression.
Returns:
a randomly generated expression

createRandom

public Expression createRandom(int type,
                               int subType,
                               int level)
Create a new random expression of the given type,subType at the given level. The level parameter is used to vary the probability of the expression being a non-terminal or a terminal expression. As the level of the expression tree gets larger, the probability of creating a non-terminal child expression decreases.

Parameters:
type - the type of the expression, e.g. Expression.BOOLEAN_TYPE
subType - the subType of the expression.
level - the level in the tree
Returns:
a randomly generated expression

createRandom

public Expression createRandom(Expression model,
                               int type,
                               int subType,
                               int level)
Create a new random expression based on mutating the given expression. If level < 1 then the top node of the created expression will not be terminal.

Parameters:
model - initial expression to work with
type - the type of the expression, e.g. Expression.BOOLEAN_TYPE
subType - the subType of the expression.
level - the level in the tree
Returns:
a randomly generated expression
See Also:
createRandom(int type, int subType, int level)

createRandomNonTerminal

public Expression createRandomNonTerminal(int type)
Create a new random non-terminal expression of the given type. A terminal expression is one that has children, e.g. an operator such as plus. (Thus plus operator would have two children, e.g. 1 and 1).

Parameters:
type - the type of the expression, e.g. Expression.BOOLEAN_TYPE
Returns:
a randomly generated non-terminal expression

createRandomNonTerminal

public Expression createRandomNonTerminal(int type,
                                          int subType)
Create a new random non-terminal expression of the given type and subType. A terminal expression is one that has children, e.g. an operator such as plus. (Thus plus operator would have two children, e.g. 1 and 1).

Parameters:
type - the type of the expression, e.g. Expression.BOOLEAN_TYPE
subType - the subType of the expression.
Returns:
a randomly generated non-terminal expression

createRandomNonTerminal

public Expression createRandomNonTerminal(int type,
                                          int subType,
                                          int level)
Create a new random non-terminal expression of the given type and subType at the given level.

Parameters:
type - the type of the expression, e.g. Expression.BOOLEAN_TYPE
subType - the subType of the expression.
level - the level in the tree
Returns:
a randomly generated non-terminal expression

createRandomNonTerminal

public Expression createRandomNonTerminal(Expression model,
                                          int type,
                                          int subType,
                                          int level)
Create a new random non-terminal expression based on mutating the given expression.

Parameters:
model - initial expression to work with
type - the type of the expression, e.g. Expression.BOOLEAN_TYPE
subType - the subType of the expression.
level - the level in the tree
Returns:
a randomly generated non-terminal expression
See Also:
createRandom(int type, int subType, int level)

createRandomTerminal

public Expression createRandomTerminal(int type)
Creates a random terminal expression of the given type. A terminal expression is one that does not have any children, e.g. a number or a variable expression.

Parameters:
type - the type of the expression, e.g. Expression.BOOLEAN_TYPE
Returns:
a randomly generated terminal expression

createRandomTerminal

public Expression createRandomTerminal(int type,
                                       int subType)
Creates a random terminal expression of the given type and subType. A terminal expression is one that does not have any children, e.g. a number or a variable expression.

Parameters:
type - the type of the expression, e.g. Expression.BOOLEAN_TYPE
subType - the subType of the expression.
Returns:
a randomly generated terminal expression

findRandomSite

public Expression findRandomSite(Expression expression)
Randomly pick a node in the given expression.

Parameters:
expression - the expression to search
Returns:
expression node

findRandomSite

public Expression findRandomSite(Expression expression,
                                 int type)
Randomly pick a node of the given type in the given expression.

Parameters:
expression - the expression node
type - the type of the node, e.g. Expression.BOOLEAN_TYPE
Returns:
expression node or null if one could not be found

delete

public Expression delete(Expression root,
                         Expression destination)
Perform a deletion mutation on the given expression. Since the mutation might chance the root of the expression, the updated root is returned. The returned root may be the same as the one passed in.

Parameters:
root - the root of the expression being mutated
destination - the destination site for the deletion
Returns:
the new root of the expression

insert

public Expression insert(Expression root,
                         Expression destination,
                         Expression source)
Perform an insertion mutation on the given expression.

Parameters:
root - the root of the expression being mutated
destination - the destination site for the insertion
source - the expression to insert
Returns:
the new root of the expression
See Also:
delete(Expression root, Expression destination)

modify

public Expression modify(Expression root,
                         Expression destination)
Perform a modification mutation on the given expression.

Parameters:
root - the root of the expression being mutated
destination - the destination site for the modification
Returns:
the new root of the expression
See Also:
delete(Expression root, Expression destination)

mutate

public Expression mutate(Expression expression)
Possibly mutate the given expression.

Parameters:
expression - the root of the expression to modify
Returns:
the new root of the expression

mutate

public Expression mutate(Expression expression,
                         int percent)
Possibly mutate the given expression

Parameters:
expression - the root of the expression to modify
percent - percent change of mutation
Returns:
the new root of the expression

Venice 0.7beta