Created by Scott Robert Ladd at Coyote Gulch Productions.
A finite state machine. More...
#include <state_machine.h>
Public Types | |
typedef InputT | t_input |
Exported input type. | |
typedef OutputT | t_output |
Exported output type. | |
typedef std::pair< t_output, size_t > | t_transition |
Type of a transition. | |
typedef std::map< t_input, t_transition > | t_input_map |
Mapping inputs to outputs and state transitions. | |
typedef std::vector< t_input_map > | t_state_table |
State table (the machine) | |
Public Member Functions | |
state_machine (size_t a_size, const std::vector< t_input > &a_inputs, const std::vector< t_output > &a_outputs) | |
Creation constructor. More... | |
state_machine (const state_machine< InputT, OutputT > &a_parent1, const state_machine< InputT, OutputT > &a_parent2) | |
Construct via bisexual crossover. More... | |
state_machine (const state_machine< InputT, OutputT > &a_source) | |
Copy constructor. More... | |
virtual | ~state_machine () |
Virtual destructor. More... | |
state_machine & | operator= (const state_machine< InputT, OutputT > &a_source) |
void | mutate (double a_rate, const std::vector< t_input > &a_inputs, const std::vector< t_output > &a_outputs, mutation_selector &a_selector=g_default_selector) |
Mutation. More... | |
t_output | transition (const state_machine< InputT, OutputT >::t_input &a_input) |
Cause state transition. More... | |
void | reset () |
Reset to start-up state. More... | |
t_state_table | get_table () const |
Get a copy of the internal table. More... | |
size_t | get_init_state () const |
Get initial state. More... | |
size_t | get_current_state () const |
Get current state. More... | |
Protected Attributes | |
t_state_table | m_state_table |
State table (the machine definition) | |
size_t | m_size |
Number of states. | |
size_t | m_init_state |
Initial state. | |
size_t | m_current_state |
Current state. | |
Static Protected Attributes | |
static mutation_selector | g_default_selector |
A static, default mutation selector. | |
![]() | |
static prng | g_random |
A shared random number generator. | |
static std::string | g_version |
Version number. | |
Additional Inherited Members | |
![]() | |
static size_t | rand_index (size_t n) |
Static function to allow use of g_random function pointer in random_shuffle. | |
static void | set_seed (uint32_t a_seed) |
Set the seed for the random number generator. | |
static uint32_t | get_seed () |
Set the seed for the random number generator. | |
static std::string | version () |
Get version number. | |
The class defines an abstract finite state machine with parameterized input and output types. A machine could take character inputs and return integer outputs, for example.
While this class provides great flexibility in FSM design (given that inputs and outputs can be almost any type of object), the class suffers from performance problems, especially when used in a genetic algorithm, where many, many objects are copied and created. In general, I've switched to using the simple_fsm class, mapping integer inputs and outputs to object tables where required.
InputT | Input type |
OutputT | Output type |
libevocosm::state_machine< InputT, OutputT >::state_machine | ( | size_t | a_size, |
const std::vector< t_input > & | a_inputs, | ||
const std::vector< t_output > & | a_outputs | ||
) |
Creates a new finite state machine with a given number of states, and input set and an output set.
a_size | - Initial number of states in this machine |
a_inputs | - A list of input values |
a_outputs | - A list of output values |
References libevocosm::state_machine< InputT, OutputT >::m_current_state, libevocosm::state_machine< InputT, OutputT >::m_init_state, libevocosm::state_machine< InputT, OutputT >::m_size, libevocosm::state_machine< InputT, OutputT >::m_state_table, and libevocosm::globals::rand_index().
libevocosm::state_machine< InputT, OutputT >::state_machine | ( | const state_machine< InputT, OutputT > & | a_parent1, |
const state_machine< InputT, OutputT > & | a_parent2 | ||
) |
Creates a new state_machine by combining the states of two parent machines. Each state in the child has an equal likelihood of being a copy of the corresponding state in either a_parent1 or a_parent2. If one parent has more states than the other, the child will also have copies of the "extra" states taken from the longer parent.
a_parent1 | - The first parent organism |
a_parent2 | - The second parent organism |
References libevocosm::globals::g_random, libevocosm::prng::get_real(), libevocosm::state_machine< InputT, OutputT >::m_current_state, libevocosm::state_machine< InputT, OutputT >::m_init_state, libevocosm::state_machine< InputT, OutputT >::m_size, and libevocosm::state_machine< InputT, OutputT >::m_state_table.
libevocosm::state_machine< InputT, OutputT >::state_machine | ( | const state_machine< InputT, OutputT > & | a_source | ) |
Creates a new state_machine identical to an existing one.
a_source | - Object to be copied |
|
virtual |
Does nothing in the base class; exists to allow destruction of derived class objects through base class (state_machine) pointers.
|
inline |
Returns the current (active) state.
|
inline |
Returns the initial (start up) state.
|
inline |
Returns a copy of the state transition table. Useful for reporting the "program" stored in an state_machine.
void libevocosm::state_machine< InputT, OutputT >::mutate | ( | double | a_rate, |
const std::vector< t_input > & | a_inputs, | ||
const std::vector< t_output > & | a_outputs, | ||
mutation_selector & | a_selector = g_default_selector |
||
) |
Mutates a finite state machine object. The four mutations supported are:
a_rate | - Chance that any given state will mutate |
a_inputs | - A list of valid input states |
a_outputs | - A list of valid output states |
a_selector | - A mutation selector |
state_machine< InputT, OutputT > & libevocosm::state_machine< InputT, OutputT >::operator= | ( | const state_machine< InputT, OutputT > & | a_source | ) |
Sets an existing state_machine to duplicate another.
a_source | - Object to be copied |
References libevocosm::state_machine< InputT, OutputT >::m_current_state, libevocosm::state_machine< InputT, OutputT >::m_init_state, libevocosm::state_machine< InputT, OutputT >::m_size, and libevocosm::state_machine< InputT, OutputT >::m_state_table.
|
inline |
Prepares the FSM to start running from its initial state.
state_machine< InputT, OutputT >::t_output libevocosm::state_machine< InputT, OutputT >::transition | ( | const state_machine< InputT, OutputT >::t_input & | a_input | ) |
Based on an input symbol, this function changes the state of an state_machine and returns an output symbol.
a_input | - An input symbol |
© 1996-2005 Scott Robert Ladd. All rights reserved.
HTML documentation generated by Dimitri van Heesch's excellent Doxygen tool.