Vita
Classes | Public Types | Public Member Functions | Friends | List of all members
vita::symbol_set Class Reference

A container for the symbols used by the GP engine. More...

#include <symbol_set.h>

Public Types

using weight_t = unsigned
 

Public Member Functions

category_t categories () const
 
void clear ()
 Clears the current symbol set. More...
 
symboldecode (const std::string &) const
 
symboldecode (opcode_t) const
 
bool enough_terminals () const
 We want at least one terminal for every used category. More...
 
template<class S , class ... Args>
symbolinsert (Args &&... args)
 Adds a symbol to the symbol set. More...
 
template<class S , class ... Args>
symbolinsert (Args &&...)
 
symbolinsert (std::unique_ptr< symbol >, double=1.0)
 Adds a new symbol to the set. More...
 
bool is_valid () const
 
const symbolroulette (category_t) const
 Extracts a random symbol from the symbol set without bias between terminals and functions . More...
 
const symbolroulette_free (category_t) const
 Extracts a random symbol from the symbol set. More...
 
const functionroulette_function (category_t) const
 
const terminalroulette_terminal (category_t) const
 
 symbol_set ()
 Sets up the object. More...
 
std::size_t terminals (category_t) const
 
weight_t weight (const symbol &) const
 

Friends

std::ostream & operator<< (std::ostream &, const symbol_set &)
 Prints the symbol set to an output stream. More...
 

Detailed Description

A container for the symbols used by the GP engine.

Symbols are stored to be quickly recalled by category and randomly selected.

Note
The functions and terminals used should be powerful enough to be able to represent a solution to the problem. On the other hand, it's better not to use a symbol set too large (this enlarges the search space and makes harder the search for a solution).

Definition at line 36 of file symbol_set.h.

Member Typedef Documentation

◆ weight_t

using vita::symbol_set::weight_t = unsigned

Definition at line 39 of file symbol_set.h.

Constructor & Destructor Documentation

◆ symbol_set()

vita::symbol_set::symbol_set ( )

Sets up the object.

The constructor allocates memory for up to k_args arguments.

Definition at line 27 of file symbol_set.cc.

Member Function Documentation

◆ categories()

category_t vita::symbol_set::categories ( ) const
Returns
number of categories in the symbol set (>= 1)
See also
category_set::size().

Definition at line 214 of file symbol_set.cc.

◆ clear()

void vita::symbol_set::clear ( )

Clears the current symbol set.

Definition at line 35 of file symbol_set.cc.

◆ decode() [1/2]

symbol * vita::symbol_set::decode ( const std::string &  dex) const
Parameters
[in]dexthe name of a symbol
Returns
a pointer to the symbol identified by dex (0 if not found)
Attention
Please note that opcodes are automatically generated and fully identify a symbol (they're primary keys). Conversely the name of a symbol is chosen by the user, so, if you don't pay attention, different symbols may have the same name.

Definition at line 198 of file symbol_set.cc.

◆ decode() [2/2]

symbol * vita::symbol_set::decode ( opcode_t  opcode) const
Parameters
[in]opcodenumerical code used as primary key for a symbol
Returns
a pointer to the vita::symbol identified by opcode (nullptr if not found).

Definition at line 179 of file symbol_set.cc.

◆ enough_terminals()

bool vita::symbol_set::enough_terminals ( ) const

We want at least one terminal for every used category.

Returns
true if there are enough terminals for secure individual generation

Definition at line 235 of file symbol_set.cc.

◆ insert() [1/2]

template<class S , class ... Args>
symbol * vita::symbol_set::insert ( Args &&...  args)

Adds a symbol to the symbol set.

Template Parameters
Ssymbol to be added
Parameters
[in]argsarguments used to build S
Returns
a raw pointer to the symbol just added (or nullptr in case of error)

Insert a symbol in the symbol set without the user having to allocate memory.

Note
Only partially replaces the insert(std::unique_ptr) method (e.g. building from factory).
Remarks
Assumes a standard frequency (1.0) for symbol S.

Definition at line 167 of file symbol_set.h.

◆ insert() [2/2]

symbol * vita::symbol_set::insert ( std::unique_ptr< symbol s,
double  wr = 1.0 
)

Adds a new symbol to the set.

Parameters
[in]ssymbol to be added
[in]wrthe weight of s (1.0 means standard frequency, 2.0 double probability of selection)
Returns
a raw pointer to the symbol just added (or nullptr in case of error)

A symbol with undefined category will be changed to the first free category.

Definition at line 55 of file symbol_set.cc.

◆ is_valid()

bool vita::symbol_set::is_valid ( ) const
Returns
true if the object passes the internal consistency check

Definition at line 307 of file symbol_set.cc.

◆ roulette()

const symbol & vita::symbol_set::roulette ( category_t  c) const

Extracts a random symbol from the symbol set without bias between terminals and functions .

Parameters
[in]ca category
Returns
a random symbol of category c
Attention
  • $P(terminal) = P(function) = 1/2$
  • $P(terminal_i|terminal) = \frac{w_i}{\sum_{t \in terminals} w_t}$
  • $P(function_i|function) = \frac{w_i}{\sum_{f \in functions} w_f}$
Note
If all symbols have the same probability to appear into a chromosome, there could be some problems. For instance, if our problem has many variables (let's say 100) and the function set has only 4 symbols we cannot get too complex trees because the functions have a reduced chance to appear in the chromosome (e.g. it happens in the Forex example).
See also
https://github.com/morinim/vita/wiki/bibliography#1

Definition at line 143 of file symbol_set.cc.

◆ roulette_free()

const symbol & vita::symbol_set::roulette_free ( category_t  c) const

Extracts a random symbol from the symbol set.

Parameters
[in]ca category
Returns
a random symbol of category c
Attention
Given $S_t = \sum_{i \in terminals} {w_i}$ and $S_f = \sum_{i \in functions} {w_i}$ we have:
  • $P(terminal_i|terminal) = \frac {w_i} {S_t}$
  • $P(function_i|function) = \frac {w_i} {S_f}$
  • $P(terminal) = \frac {S_t} {S_t + S_f}$
  • $P(function) = \frac {S_f} {S_t + S_f}$

Definition at line 168 of file symbol_set.cc.

◆ roulette_function()

const function & vita::symbol_set::roulette_function ( category_t  c) const
Parameters
[in]ca category
Returns
a random function of category c

Definition at line 101 of file symbol_set.cc.

◆ roulette_terminal()

const terminal & vita::symbol_set::roulette_terminal ( category_t  c) const
Parameters
[in]ca category
Returns
a random terminal of category c

Definition at line 113 of file symbol_set.cc.

◆ terminals()

std::size_t vita::symbol_set::terminals ( category_t  c) const
Parameters
[in]ca category
Returns
number of terminals in category c

Definition at line 223 of file symbol_set.cc.

◆ weight()

symbol_set::weight_t vita::symbol_set::weight ( const symbol s) const
Parameters
[in]sa symbol
Returns
the weight of s

Definition at line 260 of file symbol_set.cc.

Friends And Related Function Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  ,
const symbol_set  
)
friend

Prints the symbol set to an output stream.

Parameters
[out]ooutput stream
[in]sssymbol set to be printed
Returns
output stream including ss
Note
Useful for debugging purpose.

Definition at line 278 of file symbol_set.cc.


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