예제 #1
0
 def test_make_rule(self):
     r_gen = RuleGenerator(3, 2)
     rule = r_gen.make_rule()
     self.assertIn(rule[0], list(range(3)))
     self.assertIn(rule[1], list(range(2)))
     self.assertIn(rule[2], list(range(2)))
     self.assertIn(rule[3], [True, False])
     self.assertIn(rule[4], list(range(3)))
예제 #2
0
class Populaiton(object):
    def __init__(self, number_of_orgs, number_of_states, number_of_symbols):
        if number_of_orgs % 2 != 0 or number_of_orgs < 2:
            raise ValueError(
                    "Number of orgs must be an even number 2 or greater")
        self.rule_gen = RuleGenerator(number_of_states, number_of_symbols)

        number_of_inputs = number_of_states * number_of_symbols
        self.organisms = []
        for _ in range(number_of_orgs):
            rules = [self.rule_gen.make_rule()
                    for _ in range(number_of_inputs)]
            self.organisms.append(Organism(rules))


    def next(self):
        self.cull_half()
        self.duplicate_with_mutations()

    def cull_half(self):
        sorted_by_fitness = sorted(self.organisms, key=lambda org: org.fitness)
        self.organisms = sorted_by_fitness[:len(self.organisms)]

    def duplicate_with_mutations(self):
        new_orgs = [mutate(org) for org in self.organisms]
        self.organisms.extend(new_orgs)
예제 #3
0
    def __init__(self, number_of_orgs, number_of_states, number_of_symbols):
        if number_of_orgs % 2 != 0 or number_of_orgs < 2:
            raise ValueError(
                    "Number of orgs must be an even number 2 or greater")
        self.rule_gen = RuleGenerator(number_of_states, number_of_symbols)

        number_of_inputs = number_of_states * number_of_symbols
        self.organisms = []
        for _ in range(number_of_orgs):
            rules = [self.rule_gen.make_rule()
                    for _ in range(number_of_inputs)]
            self.organisms.append(Organism(rules))
예제 #4
0
 def test_modify_rule(self):
     r_gen = RuleGenerator(3, 2)
     rule = Rule(0, 1, 1, True, 2)
     rule2 = r_gen.modify_rule(rule)
     self.assertNotEqual(rule, rule2)