35 #if !defined(LIBEVOCOSM_FSM_H)
36 #define LIBEVOCOSM_FSM_H
47 #include "evocommon.h"
49 #include "fsm_tools.h"
68 template <
typename InputT,
typename OutputT>
95 fsm(
size_t a_size,
const std::vector<t_input> & a_inputs,
const std::vector<t_output> & a_outputs);
146 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);
168 t_state_table get_table()
const;
175 size_t get_init_state()
const;
182 size_t get_current_state()
const;
202 t_input_map create_input_map(
const std::vector<t_input> & a_inputs,
const std::vector<t_output> & a_outputs);
206 template <
typename InputT,
typename OutputT>
210 template <
typename InputT,
typename OutputT>
218 if ((a_size < 2) || (a_inputs.size() < 1) || (a_outputs.size() < 1))
219 throw std::runtime_error(
"invalid fsm creation parameters");
221 for (
size_t n = 0; n <
m_size; ++n)
224 m_state_table.push_back(create_input_map(a_inputs,a_outputs));
233 template <
typename InputT,
typename OutputT>
235 : m_state_table(a_parent1.m_state_table),
247 for (
size_t n = 0; n <
m_size; ++n)
267 template <
typename InputT,
typename OutputT>
269 : m_state_table(a_source.m_state_table),
270 m_init_state(a_source.m_init_state),
271 m_current_state(a_source.m_current_state),
272 m_size(a_source.m_size)
278 template <
typename InputT,
typename OutputT>
285 template <
typename InputT,
typename OutputT>
288 if (
this != &a_source)
300 template <
typename InputT,
typename OutputT>
302 const std::vector<t_input> & a_inputs,
303 const std::vector<t_output> & a_outputs,
306 if (g_random.get_real() < a_rate)
311 case MUTATE_OUTPUT_SYMBOL:
314 size_t state = rand_index(m_size);
315 size_t input = rand_index(a_inputs.size());
316 size_t output = rand_index(a_outputs.size());
317 m_state_table[state][a_inputs[input]].first = a_outputs[output];
320 case MUTATE_TRANSITION:
323 size_t state = rand_index(m_size);
324 size_t input = rand_index(a_inputs.size());
325 size_t new_state = rand_index(m_size);
326 m_state_table[state][a_inputs[input]].second = new_state;
329 case MUTATE_REPLACE_STATE:
332 size_t state = rand_index(m_size);
333 m_state_table[state] = create_input_map(a_inputs,a_outputs);
335 case MUTATE_SWAP_STATES:
338 size_t state1 = rand_index(m_size);
342 state2 = rand_index(m_size);
343 while (state2 == state1);
346 m_state_table[state1] = m_state_table[state2];
347 m_state_table[state2] = temp;
350 case MUTATE_INIT_STATE:
353 m_init_state = rand_index(m_size);
360 m_current_state = m_init_state;
364 template <
typename InputT,
typename OutputT>
368 t_transition & trans = m_state_table[m_current_state][a_input];
371 m_current_state = trans.second;
378 template <
typename InputT,
typename OutputT>
381 m_current_state = m_init_state;
385 template <
typename InputT,
typename OutputT>
388 return m_state_table;
392 template <
typename InputT,
typename OutputT>
399 template <
typename InputT,
typename OutputT>
402 return m_current_state;
406 template <
typename InputT,
typename OutputT>
410 size_t max_output = a_outputs.size();
413 t_input_map input_map;
416 for (
typename std::vector<t_input>::const_iterator input = a_inputs.begin(); input != a_inputs.end(); ++input)
419 t_output out_symbol = a_outputs[rand_index(max_output)];
420 size_t new_state = rand_index(m_size);
423 input_map[*input] = std::make_pair(out_symbol,new_state);
void reset()
Reset to start-up state.
Definition: fsm.h:379
Wraps a roulette wheel for selecting mutations.
Definition: fsm_tools.h:77
std::vector< t_input_map > t_state_table
State table (the machine)
Definition: fsm.h:85
std::pair< t_output, size_t > t_transition
Type of a transition.
Definition: fsm.h:79
fsm(size_t a_size, const std::vector< t_input > &a_inputs, const std::vector< t_output > &a_outputs)
Creation constructor.
Definition: fsm.h:211
size_t m_current_state
Current state.
Definition: fsm.h:195
virtual ~fsm()
Virtual destructor.
Definition: fsm.h:279
static size_t rand_index(size_t n)
Static function to allow use of g_random function pointer in random_shuffle.
Definition: evocommon.h:119
A set of common tools for finite state machines.
Definition: fsm_tools.h:47
size_t get_current_state() const
Get current state.
Definition: fsm.h:400
double get_real()
get the next value in the range [0,1)
Definition: evocommon.h:104
static mutation_selector g_default_selector
A static, default mutation selector.
Definition: fsm.h:198
static prng g_random
A shared random number generator.
Definition: evocommon.h:125
A finite state machine.
Definition: fsm.h:69
size_t get_init_state() const
Get initial state.
Definition: fsm.h:393
A toolkit and framework for implementing evolutionary algorithms.
Definition: evocommon.h:60
t_output transition(const fsm< InputT, OutputT >::t_input &a_input)
Cause state transition.
Definition: fsm.h:365
std::map< t_input, t_transition > t_input_map
Mapping inputs to outputs and state transitions.
Definition: fsm.h:82
fsm & operator=(const fsm< InputT, OutputT > &a_source)
Definition: fsm.h:286
InputT t_input
Exported input type.
Definition: fsm.h:73
size_t m_size
Number of states.
Definition: fsm.h:189
Elements shared by all classes in Evocosm.
Definition: evocommon.h:115
t_state_table m_state_table
State table (the machine definition)
Definition: fsm.h:186
OutputT t_output
Exported output type.
Definition: fsm.h:76
t_state_table get_table() const
Get a copy of the internal table.
Definition: fsm.h:386
size_t m_init_state
Initial state.
Definition: fsm.h:192
size_t get_index() const
Get a mutation index.
Definition: fsm_tools.h:141
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.
Definition: fsm.h:301