Ejemplo n.º 1
0
class Test_individual(unittest.TestCase):
    def test_binary_chromosome(self):
        pool = ["0", "1"]
        length = 5
        self.generator = Generator(pool, length)
        self.Individual = Individual(self.generator.generate_sequence())

        # Generate a sequence
        assert self.Individual.chromosome == "01101", "Failed test test_binary_chromosome, generated wrong sequence"

        # Test fitness
        assert self.Individual.evaluate_fitness("01101") == 5, "Failed test test_binary_chromosome, got wrong fitness " \
                                                               "instead of 5 "
        assert self.Individual.evaluate_fitness("11011") == 2, "Failed test test_binary_chromosome, got wrong fitness " \
                                                               "instead of 2 "

    def test_character_chromosome(self):
        pool = list("abcdefghijklmnñopqrstuvwxyz")
        length = 5
        self.generator = Generator(pool, length)
        self.Individual = Individual(self.generator.generate_sequence())

        # Generate a sequence
        assert self.Individual.chromosome == "bjicr", "Failed test test_binary_chromosome, generated wrong sequence"

        # Test fitness
        assert self.Individual.evaluate_fitness("bjicr") == 5, "Failed test test_binary_chromosome, got wrong fitness " \
                                                               "instead of 5 "
        assert self.Individual.evaluate_fitness("bjcri") == 2, "Failed test test_binary_chromosome, got wrong fitness " \
                                                               "instead of 2 "
Ejemplo n.º 2
0
    def test_binary_chromosome(self):
        pool = ["0", "1"]
        length = 5
        self.generator = Generator(pool, length)
        self.Individual = Individual(self.generator.generate_sequence())

        # Generate a sequence
        assert self.Individual.chromosome == "01101", "Failed test test_binary_chromosome, generated wrong sequence"

        # Test fitness
        assert self.Individual.evaluate_fitness("01101") == 5, "Failed test test_binary_chromosome, got wrong fitness " \
                                                               "instead of 5 "
        assert self.Individual.evaluate_fitness("11011") == 2, "Failed test test_binary_chromosome, got wrong fitness " \
                                                               "instead of 2 "
Ejemplo n.º 3
0
    def make(self, father, mother):
        dna_a = father.dna
        dna_b = mother.dna

        dna_ab = dna_a.cross(dna_b)
        dna_ab = dna_ab.mutate()

        sex = random.choice(["M", "F"])

        individual = Individual(dna_ab, self.NameFactory.generate_name(sex),
                                father.lastname, sex, father, mother, [])

        print(father.info() + " & " + mother.info() + " MADE --> " +
              individual.info())

        return individual
Ejemplo n.º 4
0
    def reproduce(self, individualA, individualB):

        # Obtain trees
        nodeA = individualA.chromosome
        nodeB = individualB.chromosome

        # Case of trees of single node
        if self.generator.height == 1:
            best_tree = None
            best_score = None
            for candidate_tree in [nodeA, nodeB]:
                candidate_score = self.fitness_calculator.calculate_fitness(
                    candidate_tree)
                if best_score is None or candidate_score > best_score:
                    best_score = candidate_score
                    best_tree = candidate_tree
        else:
            # Create new tree, as the best combination of the individuals
            # Generate all combinations between two nodes and operations
            combinations = [self.operations, nodeA.toList(), nodeB.toList()]
            combinations_perm = [
                self.operations,
                nodeB.toList(),
                nodeA.toList()
            ]
            combinations = itertools.product(*combinations)
            combinations_perm = itertools.product(*combinations_perm)

            combinations = itertools.chain(combinations, combinations_perm)

            # Find the best combination
            best_tree = None
            best_score = None
            for combination in combinations:
                candidate_tree = OpTree(combination[0], combination[1],
                                        combination[2])

                # If candidate height is higher that the allowed one, then is skipped
                if candidate_tree.height() > self.generator.height:
                    continue

                # Obtain fitness
                candidate_score = self.fitness_calculator.calculate_fitness(
                    candidate_tree)

                # New best candidate
                if best_score is None or candidate_score > best_score:
                    best_score = candidate_score
                    best_tree = candidate_tree

        # Mutate based of mutation_rate
        best_tree = self.generator.mutate_tree(best_tree, self.mutation_rate)

        return Individual(best_tree)
Ejemplo n.º 5
0
from Individual.Individual import Individual
from Individual.DNA import DNA

from Factories.BabyFactory import BabyFactory
from Factories.MatchFactory import MatchFactory
from Factories.NameFactory import NameFactory

from Universe.Universe import Universe

adam = Individual(DNA("00000000"), "Adam", "Pomegranate", "M", "", "", [])
eve = Individual(DNA("11111111"), "Eve", "Watermelon", "F", "", "", [])

adam.marry(eve)

if (adam.lastname == eve.lastname):
    print("Names Match! - " + adam.lastname + " & " + eve.lastname)
else:
    print("FAIL! - " + adam.lastname + " & " + eve.lastname)

print(adam.name + "'s Spouse: " + adam.spouse.name)
print(eve.name + "'s Spouse: " + eve.spouse.name)

BabyFactory = BabyFactory()

baby_1 = BabyFactory.make(adam, eve)
baby_2 = BabyFactory.make(adam, eve)
baby_3 = BabyFactory.make(adam, eve)

baby_1.add_sibling(baby_2)
baby_1.add_sibling(baby_3)
Ejemplo n.º 6
0
from Universe.Universe import Universe

from Individual.Individual import Individual
from Individual.DNA import DNA

universe = Universe()

adam = Individual(DNA("11110000"), "Eve", "GOD", "F", "GOD", "GOD", [])
eve = Individual(DNA("00001111"), "Adam", "GOD", "M", "GOD", "GOD", [])
seed = [adam, eve]

universe.spawn(seed)

universe.run(10)
universe.run(3)
universe.run(2)

universe.end()
Ejemplo n.º 7
0
 def initialize_population(self):
     for _ in range(self.population_size):
         new_individual = Individual(
             self.generator.generate_tree(self.height))
         self.population.append(new_individual)