Ultra 0.9.4
Loading...
Searching...
No Matches
ultra::gene Struct Reference

#include <gene.h>

Public Types

using arg_pack = std::vector<value_t>
 Type used to store the arguments of the function.

Public Member Functions

 gene ()=default
 gene (const function *, const arg_pack &)
locus locus_of_argument (std::size_t) const noexcept
locus locus_of_argument (const arg_pack::value_type &) const noexcept
symbol::category_t category () const noexcept
bool operator== (const gene &) const noexcept=default
bool is_valid () const noexcept

Public Attributes

const function * func {}
 Pointer to the function symbol.
arg_pack args {}
 Arguments of the function.

Detailed Description

A gene is the atomic unit of a genetic programming individual.

In ULTRA's GP engine, a gene represents a single instruction in a Straight Line Program (SLP). It consists of:

  • a pointer to a function symbol (func);
  • a fixed-size list of arguments (args) whose size equals the function arity.

Arguments may be:

  • terminals (constants, variables);
  • addresses referring to genes at earlier loci, enforcing the acyclic, feed-forward structure of SLPs.

Invariants

A gene is always in one of the following states:

  • empty gene: func == nullptr and args.empty();
  • active gene: func != nullptr and args.size() == func->arity().

These states are recognised by is_valid(); callers modifying public members must preserve the invariant.

Ownership and lifetime

The gene class does not own the function it refers to. The pointed function object is expected to be managed by a longer-lived symbol_set.

Thread safety

gene is a passive value type and performs no internal synchronisation. Concurrent access is safe only if no thread mutates the object.

See also
gp::individual

Constructor & Destructor Documentation

◆ gene() [1/2]

ultra::gene::gene ( )
default

Constructs an empty (inactive) gene.

The resulting gene has func == nullptr and no arguments. This state is valid and typically represents an unused slot in an individual's genome.

◆ gene() [2/2]

ultra::gene::gene ( const function * f,
const arg_pack & a )

Constructs an active gene.

Parameters
[in]fpointer to the function symbol
[in]aarguments list for the function
Precondition
f must not be null
a.size() == f->arity()

This constructor is primarily intended for debugging and hand-crafted individuals:

std::vector<gene> g
{
  {f_add, {1, 2}},  // [0] ADD 1,2
  {    y,     {}},  // [1] Y
  {    x,     {}}   // [2] X
};

Member Function Documentation

◆ category()

symbol::category_t ultra::gene::category ( ) const
nodiscardnoexcept

Returns the output category of the gene.

Returns
the category of the function represented by this gene
Precondition
func != nullptr

◆ is_valid()

bool ultra::gene::is_valid ( ) const
nodiscardnoexcept

Checks the internal consistency of the gene.

Returns
true if the gene satisfies its invariants

This function verifies that the gene is either empty or fully initialised with a matching function and argument list.

◆ locus_of_argument() [1/2]

locus ultra::gene::locus_of_argument ( const arg_pack::value_type & a) const
nodiscardnoexcept

Returns the locus referenced by an argument value.

Parameters
[in]aan argument belonging to this gene
Returns
the locus addressed by a
Precondition
func != nullptr
a holds a D_ADDRESS
a must belong to this gene's argument list

The argument position is inferred from the argument list in order to recover the correct category.

◆ locus_of_argument() [2/2]

locus ultra::gene::locus_of_argument ( std::size_t i) const
nodiscardnoexcept

Returns the locus referenced by the i-th argument.

Parameters
[in]iindex of the argument
Returns
the locus addressed by argument i
Precondition
func != nullptr
i < func->arity()
args[i] holds a D_ADDRESS

The returned locus uses the category required by the function for the i-th argument.

◆ operator==()

bool ultra::gene::operator== ( const gene & ) const
nodiscarddefaultnoexcept

Equality comparison.

Two genes are equal if they reference the same function symbol and have identical arguments.


The documentation for this struct was generated from the following files: