def test_execution_multiple_conditions_all_false(self): sm = StateMachineNetwork([State(1), State(2)], [Transition(1, 2)]) sm.states[1].set_biases([1, 2]) sm.states[1].set_weights([[1, 2], [3, 4]]) sm.transitions[1][0].add_condition(Condition(0, operator.eq, 4)) sm.transitions[1][0].add_condition(Condition(1, operator.gt, 10)) next_state, output = sm.activate(1, [5, 10]) self.assertEqual(next_state, 1) self.assertEqual(output, [26, 57])
def test_distance_2(self): config = init_config() genome1 = TransitionGene((1, 2)) genome2 = TransitionGene((1, 2)) genome1.init_attributes(config) genome2.init_attributes(config) genome2.conditions.append(Condition(1, Condition.random_operator(), 1)) self.assertEqual(1, genome1.distance(genome2, config))
def encode(self, genome, generation, gen_hash=0): """ This function encodes a State Machine genome to be send over ROS. """ encoded_states = SMROSEncoder.encode_states(genome.states) encoded_transitions = [] for transition_id, transition in iteritems(genome.transitions): enc_conditions = [ SMCondition(condition[0], Condition.op_to_int(condition[1]), condition[2]) for condition in transition.conditions ] or_comparison = False # To support legacy code, always make sure that it is in there. if hasattr(transition, 'or_comparison'): or_comparison = transition.or_comparison encoded_transition = SMTransition(transition_id[0], transition_id[1], transition.enabled, or_comparison, enc_conditions) encoded_transitions.append(encoded_transition) encoded_genome = SMGenome( GenomeHeader(genome.key, gen_hash, generation), encoded_transitions, encoded_states) return encoded_genome
def test_many_conditions_true(self): transition = Transition(1, 2) transition.add_condition(Condition(1, operator.lt, 5)) transition.add_condition(Condition(2, operator.eq, 4)) transition.add_condition(Condition(2, operator.lt, 5)) transition.add_condition(Condition(0, operator.lt, 4)) transition.add_condition(Condition(1, operator.eq, 0)) transition.add_condition(Condition(0, operator.lt, 4)) self.assertTrue(transition.check_transition([0, 0, 4]))
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_sensor_out_of_range(self): condition = Condition(5, operator.gt, 1) self.assertRaises(IndexError, condition.compare, [10, 10, 0, 0.5])
def test_invalid_condition(self): condition = Condition(2, operator.gt, 1) self.assertFalse(condition.compare([10, 10, 0, 0.5]))
def test_valid_condition(self): condition = Condition(3, operator.lt, 1) self.assertTrue(condition.compare([10, 10, 10, 0.5]))
def test_two_conditions_false(self): transition = Transition(1, 2) transition.add_condition(Condition(1, operator.eq, 5)) transition.add_condition(Condition(2, operator.eq, 4)) self.assertFalse(transition.check_transition([0, 0, 4]))
def test_one_condition_true(self): transition = Transition(1, 2) transition.add_condition(Condition(2, operator.eq, 5)) self.assertTrue(transition.check_transition([0, 0, 5]))