Ultra 0.9.4
Loading...
Searching...
No Matches
ultra::function Class Referenceabstract

#include <function.h>

Inheritance diagram for ultra::function:
ultra::symbol ultra::integer::add ultra::integer::div ultra::integer::ife ultra::integer::ifl ultra::integer::ifz ultra::integer::mod ultra::integer::mul ultra::integer::shl ultra::integer::sub ultra::real::abs ultra::real::add ultra::real::aq ultra::real::cos ultra::real::div ultra::real::exp ultra::real::gt ultra::real::idiv ultra::real::ife ultra::real::ifl ultra::real::ifz ultra::real::length ultra::real::ln ultra::real::lt ultra::real::max ultra::real::mod ultra::real::mul ultra::real::sigmoid ultra::real::sin ultra::real::sqrt ultra::real::sub ultra::str::ife

Classes

class  params

Public Types

using param_data_types = std::vector<category_t>
using return_type = category_t
 Type used to describe the return category of the function.
Public Types inherited from ultra::symbol
enum  format { c_format , cpp_format , python_format , sup_format }
 Supported rendering formats for symbol stringification.
using category_t = unsigned
using opcode_t = unsigned

Public Member Functions

 function (const std::string &, return_type, param_data_types)
 function (const std::string &, std::size_t)
category_t param_category (std::size_t) const noexcept
const param_data_types & categories () const noexcept
std::size_t arity () const noexcept
virtual value_t eval (const params &) const =0
virtual std::string to_string (format=c_format) const override
bool is_valid () const override
Public Member Functions inherited from ultra::symbol
 symbol (const std::string &, category_t=default_category)
virtual ~symbol ()=default
 Virtual destructor to allow safe polymorphic deletion.
category_t category () const noexcept
opcode_t opcode () const noexcept
const std::string & name () const noexcept
void category (category_t) noexcept

Additional Inherited Members

Static Public Attributes inherited from ultra::symbol
static constexpr category_t default_category = 0
 Default category assigned when typing is not used.
static constexpr category_t undefined_category
 Sentinel value indicating that the category has not yet been assigned.

Detailed Description

A callable GP symbol with one or more input parameters.

In ULTRA, a function represents a non-terminal symbol in a genetic programming "tree". Functions label internal nodes and are evaluated by recursively evaluating their arguments and combining the results.

Each function has:

  • a return category (inherited from symbol);
  • a fixed arity;
  • a category for each input parameter.

Categories are used to enforce strong typing constraints during program construction, mutation, and crossover.

Note
Functions must satisfy the closure property: they must be able to handle any input values that conform to their parameter categories without causing undefined behaviour or runtime errors.

Member Typedef Documentation

◆ param_data_types

Type used to describe the categories of input parameters.

The size of this container defines the arity of the function.

Constructor & Destructor Documentation

◆ function() [1/2]

ultra::function::function ( const std::string & name,
return_type r,
param_data_types par )

Constructs a typed function.

Parameters
[in]namestring representation of the function (e.g. "ADD", "+")
[in]rreturn category of the function
[in]parcategories of the input parameters

This constructor is typically used in strongly typed GP, where both argument and return categories are explicitly specified.

◆ function() [2/2]

ultra::function::function ( const std::string & name,
std::size_t n_pars )

Constructs an untyped function with the given arity.

Parameters
[in]namestring representation of the function
[in]n_parsnumber of input parameters
Remarks
The function and all its parameters are assigned symbol::default_category. This constructor is intended for GP configurations where strong typing is not used.

Member Function Documentation

◆ arity()

std::size_t ultra::function::arity ( ) const
nodiscardnoexcept

Returns the number of input parameters of the function.

Returns
function arity

◆ categories()

const function::param_data_types & ultra::function::categories ( ) const
nodiscardnoexcept

Returns the list of input parameter categories.

Returns
constant reference to the parameter category list
Remarks
The size of the returned container is equal to the function arity.

◆ eval()

virtual value_t ultra::function::eval ( const params & ) const
pure virtual

Evaluates the function for the given parameters.

Returns
result of the function evaluation
Remarks
Parameters are accessed via the params interface, which supports lazy evaluation and optional referential transparency. Implementations should avoid fetching arguments multiple times unless necessary.
Warning
Implementations must not assume any particular evaluation order of parameters.

Implemented in ultra::integer::add, ultra::integer::div, ultra::integer::ife, ultra::integer::ifl, ultra::integer::ifz, ultra::integer::mod, ultra::integer::mul, ultra::integer::shl, ultra::integer::sub, ultra::real::abs, ultra::real::add, ultra::real::aq, ultra::real::cos, ultra::real::div, ultra::real::exp, ultra::real::gt, ultra::real::idiv, ultra::real::ife, ultra::real::ifl, ultra::real::ifz, ultra::real::length, ultra::real::ln, ultra::real::lt, ultra::real::max, ultra::real::mod, ultra::real::mul, ultra::real::sigmoid, ultra::real::sin, ultra::real::sqrt, ultra::real::sub, and ultra::str::ife.

◆ is_valid()

bool ultra::function::is_valid ( ) const
nodiscardoverridevirtual

Performs an internal consistency check.

Returns
true if the function satisfies basic validity constraints

Reimplemented from ultra::symbol.

◆ param_category()

symbol::category_t ultra::function::param_category ( std::size_t i) const
nodiscardnoexcept

Returns the category of a specific input parameter.

Parameters
[in]iindex of the input parameter
Returns
category of the i-th parameter
Precondition
i < arity()

◆ to_string()

std::string ultra::function::to_string ( format = c_format) const
nodiscardoverridevirtual

Returns a string representation of the function.

Remarks
The base implementation produces a generic functional notation (e.g. ADD({0},{1})). Derived classes may override this method to support alternative syntaxes or formatting conventions.
Note
Format templates should not contain unbalanced parentheses inside comments or string literals, as program rendering checks enclosing parentheses directly on the template.
Returns
the name of the function
Warning
Specific functions have to specialize this method to support different output formats. Format templates should avoid unbalanced parentheses in comments or string literals.

Implements ultra::symbol.

Reimplemented in ultra::integer::add, ultra::integer::div, ultra::integer::ife, ultra::integer::ifl, ultra::integer::ifz, ultra::integer::mod, ultra::integer::mul, ultra::integer::shl, ultra::integer::sub, ultra::real::abs, ultra::real::add, ultra::real::aq, ultra::real::cos, ultra::real::div, ultra::real::exp, ultra::real::gt, ultra::real::idiv, ultra::real::ife, ultra::real::ifl, ultra::real::ifz, ultra::real::length, ultra::real::ln, ultra::real::lt, ultra::real::max, ultra::real::mod, ultra::real::mul, ultra::real::sigmoid, ultra::real::sin, ultra::real::sqrt, ultra::real::sub, and ultra::str::ife.


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