GP Generators

generate(prim_set, min_depth, max_depth, condition, ret_type=None)

Generates a tree as a list of primitives and terminals in a depth-first order. The tree is built from the root to the leaves. It recursively grows each branch until the condition is fulfilled. The returned list can then be used to instantiate a ‘PrimitiveTree’ object to build the actual tree object.

Parameters
  • prim_set (PrimitiveSetTyped) – Primitive set from which primitives are selected.

  • min_depth (int) – Minimum depth of the random tree.

  • max_depth (int) – Maximum depth of the random tree.

  • condition (Callable) – A function that takes two arguments: the height of the branch to grow and the current depth in the tree.

  • ret_type (Optional[Any]) – The type that should return the tree when called, optional. If not provided, the type of ‘p_set.ret’ is used.

Returns

A grown tree with leaves at possibly different depths depending on the condition function.

Return type

list

gen_full(prim_set, min_depth, max_depth, ret_type=None)

Generates an expression where each leaf has the same depth between min and max.

Parameters
  • prim_set (PrimitiveSetTyped) – Primitive set from which primitives are selected.

  • min_depth (int) – Minimum depth of the random tree.

  • max_depth (int) – Maximum depth of the random tree.

  • ret_type (Optional[Any]) – The type that should return the tree when called, optional. If not provided, the type of ‘p_set.ret’ is used.

Returns

A full tree with all leaves at the same depth.

Return type

list

gen_grow(prim_set, min_depth, max_depth, ret_type=None)

Generates an expression where each leaf might have a different depth between min and max.

Parameters
  • prim_set (PrimitiveSetTyped) – Primitive set from which primitives are selected.

  • min_depth (int) – Minimum depth of the random tree.

  • max_depth (int) – Maximum depth of the random tree.

  • ret_type (Optional[Any]) – The type that should return the tree when called, optional. If not provided, the type of ‘p_set.ret’ is used.

Returns

A grown tree with leaves at possibly different depths.

Return type

list

gen_half_and_half(prim_set, min_depth, max_depth, ret_type=None)

Generates an expression with a random choice between ‘gen_grow’ and ‘gen_full’.

Parameters
  • prim_set (PrimitiveSetTyped) – Primitive set from which primitives are selected.

  • min_depth (int) – Minimum depth of the random tree.

  • max_depth (int) – Maximum depth of the random tree.

  • ret_type (Optional[Any]) – The type that should return the tree when called, optional. If not provided, the type of ‘p_set.ret’ is used.

Returns

Either a full tree or a grown tree.

Return type

list