def test_single_state(self): genome = StateMachineGenome(1) config = init_config() genome.configure_new(config) self.assertEqual(len(genome.states), 1) self.assertEqual(len(genome.transitions), 0)
def test_multiple_initial_states(self): genome = StateMachineGenome(1) config = init_config() config.num_initial_states = 4 genome.configure_new(config) self.assertEqual(len(genome.states), 4) self.assertEqual(len(genome.transitions), 0)
def test_one_state(self): genome = StateMachineGenome(1) config = init_config() genome.configure_new(config) smn = StateMachineNetwork.create(genome, config) self.assertEqual(len(smn.states), 1) self.assertEqual(len(smn.transitions), 1)
def test_add_transition_to_existing(self): genome = StateMachineGenome(1) config = init_config() genome.configure_new(config) genome.mutate_add_state(config) genome.mutate_add_transition(config) self.assertEqual(2, len(genome.states)) self.assertEqual(2, len(genome.transitions))
def test_create_transition(self): genome = StateMachineGenome(1) config = init_config() genome.configure_new(config) genome.mutate_add_state(config) genome.mutate_add_transition(config) smn = StateMachineNetwork.create(genome, config) self.assertEqual(len(smn.states), 2) self.assertEqual(len(smn.transitions), 2) self.assertEqual(2, len(smn.transitions[1]) + len(smn.transitions[0]))
def decode(self, encoded_genome): """ This function decodes a ROS encoded state machine genome into a normal state machine genome.""" states = SMROSEncoder.decode_states(encoded_genome.states) transitions = {} for enc_transition in encoded_genome.transitions: key = (enc_transition.source, enc_transition.dest) transitions[key] = TransitionGene(key) transitions[key].enabled = enc_transition.enabled transitions[key].conditions = [ (enc_condition.inputSensor, Condition.int_to_op(enc_condition.operator), enc_condition.comparator_value) for enc_condition in enc_transition.conditions ] genome = StateMachineGenome(encoded_genome.header.key) genome.transitions = transitions genome.states = states return genome
def test_add_transition_transition_free_state_machine(self): genome = StateMachineGenome(1) config = init_config() genome.configure_new(config) state = genome.create_state(config, 1) genome.states[1] = state self.assertEqual(len(genome.states), 2) self.assertEqual(len(genome.transitions), 0) try: genome.mutate_add_transition(config) self.assertEqual(1, len(genome.transitions)) except AssertionError: print("Same state, all ok.")
def init_config(): params = dict() params['num_inputs'] = 3 params['num_outputs'] = 2 params['num_initial_states'] = 1 params['state_add_prob'] = 1 params['state_delete_prob'] = 1 params['transition_add_prob'] = 1 params['transition_delete_prob'] = 1 params['activation'] = 'sigmoid' params['aggregation'] = 'sum' params['compatibility_disjoint_coefficient'] = 0.5 params['compatibility_difference_coefficient'] = 1.0 params['weight_init_mean'] = 1 params['weight_init_stdev'] = 1 params['weight_replace_rate'] = 0.1 params['weight_mutate_rate'] = 0.5 params['weight_mutate_power'] = 1 params['weight_max_value'] = 2 params['weight_min_value'] = 0 params['bias_init_mean'] = 1 params['bias_init_stdev'] = 1 params['bias_replace_rate'] = 0.1 params['bias_mutate_rate'] = 0.5 params['bias_mutate_power'] = 1 params['bias_max_value'] = 2 params['bias_min_value'] = 0 params['conditions_add_condition_prob'] = 1 params['conditions_remove_condition_prob'] = 1 params['condition_mutate_input_prob'] = 1 params['condition_mutate_comp_prob'] = 1 params['condition_comparator_init_mean'] = 1 params['condition_comparator_init_stdev'] = 1 params['condition_comparator_replace_rate'] = 0.1 params['condition_comparator_mutate_rate'] = 0.5 params['condition_comparator_mutate_power'] = 1 params['condition_comparator_max_value'] = 2 params['condition_comparator_min_value'] = 0 params['enabled_default'] = 'true' params['enabled_mutate_rate'] = 1 config = StateMachineGenome.parse_config(params) return config
def test_two_state_genome(self): genome = StateMachineGenome(1) config = init_config() genome.configure_new(config) genome.mutate_add_state(config) self.assertEqual(len(genome.states), 2) self.assertEqual(len(genome.transitions), 2) self.assertIn((0, 1), genome.transitions) self.assertIn((1, 0), genome.transitions)
def test_state_difference_distance(self): genome = StateMachineGenome(1) genome2 = StateMachineGenome(2) config = init_config() config.compatibility_difference_coefficient = 0 config.compatibility_disjoint_coefficient = 1 config.num_initial_states = config.node_indexer = 4 genome.configure_new(config) genome2.configure_new(config) genome2.mutate_add_state(config) self.assertEqual(1, genome.distance(genome2, config))
def test_empty_sm_creation(self): genome = StateMachineGenome(1) smn = StateMachineNetwork.create(genome, init_config()) self.assertEqual(len(smn.states), 0) self.assertEqual(len(smn.transitions), 0)
params['condition_comparator_init_stdev'] = 1 params['condition_comparator_replace_rate'] = 0.1 params['condition_comparator_mutate_rate'] = 0.5 params['condition_comparator_mutate_power'] = 1 params['condition_comparator_max_value'] = 2 params['condition_comparator_min_value'] = 0 params['enabled_default'] = 'true' params['enabled_mutate_rate'] = 1 return params if __name__ == '__main__': config_params = init_params() config = StateMachineGenome.parse_config(config_params) attribute = SimpleNeuralNetworkAttribute('nn') nn = attribute.init_value(config) print(nn) nn1 = attribute.mutate_value(nn, config) print(nn1) run_time = timeit.timeit('attribute.mutate_value(nn, config)', 'from __main__ import config, attribute, nn', number=100000) print(run_time)
def mutate_transitions(self, config): """ Override mutate_transitions to prevent transition mutations if that section is fixed.""" if config.fixed_section not in ['transitions', 'layout']: # Only allow to mutate transitions if the number of states is fixed. StateMachineGenome.mutate_transitions(self, config)