class TournamentSelectionTest(unittest.TestCase):
    """Test selection based on a tournament style scheme."""

    def setUp(self):
        self.selector = TournamentSelection(NoMutation(), NoCrossover(),
                                            NoRepair(), 2)

    def test_select_best(self):
        """Ensure selection of the best organism in a population of 2."""

        # Create any two non equal organisms
        org_1 = random_organism()
        while True:
            org_2 = random_organism()
            if org_2.fitness != org_1.fitness:
                break
        # Sort them so org_1 is most fit
        if org_2.fitness > org_1.fitness:
            org_1, org_2 = org_2, org_1
        self.assertTrue(org_1.fitness > org_2.fitness)

        pop = [org_1, org_2]
        new_pop = self.selector.select(pop)
        for org in new_pop:
            self.assertEqual(org, org_1,
                             "Got a worse organism selected.")

        # Just to make sure the selector isn't doing something
        # silly with the order, try this with the input reserved:
        pop = [org_2, org_1]
        new_pop = self.selector.select(pop)
        for org in new_pop:
            self.assertEqual(org, org_1,
                             "Got a worse organism selected.")

    def test_selection(self):
        """Test basic selection on a small population."""

        pop = [random_organism() for org_num in range(50)]
        new_pop = self.selector.select(pop)

        self.assertEqual(len(new_pop), len(pop),
                         "Did not maintain population size.")
Exemple #2
0
class TournamentSelectionTest(unittest.TestCase):
    """Test selection based on a tournament style scheme."""

    def setUp(self):
        self.selector = TournamentSelection(NoMutation(), NoCrossover(),
                                            NoRepair(), 2)

    def test_select_best(self):
        """Ensure selection of the best organism in a population of 2."""

        # Create any two non equal organisms
        org_1 = random_organism()
        while True:
            org_2 = random_organism()
            if org_2.fitness != org_1.fitness:
                break
        # Sort them so org_1 is most fit
        if org_2.fitness > org_1.fitness:
            org_1, org_2 = org_2, org_1
        self.assertTrue(org_1.fitness > org_2.fitness)

        pop = [org_1, org_2]
        new_pop = self.selector.select(pop)
        for org in new_pop:
            self.assertEqual(org, org_1,
                             "Got a worse organism selected.")

        # Just to make sure the selector isn't doing something
        # silly with the order, try this with the input reserved:
        pop = [org_2, org_1]
        new_pop = self.selector.select(pop)
        for org in new_pop:
            self.assertEqual(org, org_1,
                             "Got a worse organism selected.")

    def test_selection(self):
        """Test basic selection on a small population."""

        pop = [random_organism() for org_num in range(50)]
        new_pop = self.selector.select(pop)

        self.assertEqual(len(new_pop), len(pop),
                         "Did not maintain population size.")
Exemple #3
0
    def _set_up_genetic_algorithm(self):
        """Overrideable function to set up the genetic algorithm parameters.

        This functions sole job is to set up the different genetic
        algorithm functionality. Since this can be quite complicated, this
        allows cusotmizablity of all of the parameters. If you want to
        customize specially, you can inherit from this class and override
        this function.
        """
        self.motif_generator = RandomMotifGenerator(self.alphabet)

        self.mutator = SinglePositionMutation(mutation_rate=0.1)
        self.crossover = SinglePointCrossover(crossover_prob=0.25)
        self.repair = AmbiguousRepair(Schema(self.alphabet.alphabet_matches),
                                      4)
        self.base_selector = TournamentSelection(self.mutator, self.crossover,
                                                 self.repair, 2)
        self.selector = DiversitySelection(self.base_selector,
                                           self.motif_generator.random_motif)
Exemple #4
0
def main(num_queens):

    print "Calculating for %s queens..." % num_queens

    num_orgs = 1000
    print "Generating an initial population of %s organisms..." % num_orgs
    queen_alphabet = QueensAlphabet(num_queens)

    start_population = Organism.random_population(queen_alphabet, num_queens,
                                                  num_orgs, queens_fitness)

    print "Evolving the population and searching for a solution..."

    mutator = QueensMutation(mutation_rate=0.05)
    crossover = QueensCrossover(queens_fitness,
                                crossover_prob=.2,
                                max_crossover_size=4)
    repair = QueensRepair()
    # rw_selector = RouletteWheelSelection(mutator, crossover, repair)
    t_selector = TournamentSelection(mutator, crossover, repair, 5)

    start_time = time.ctime(time.time())
    evolver = GenerationEvolver(start_population, t_selector)
    evolved_pop = evolver.evolve(queens_solved)
    end_time = time.ctime(time.time())

    unique_solutions = []
    for organism in evolved_pop:
        if organism.fitness == num_queens:
            if organism not in unique_solutions:
                unique_solutions.append(organism)

    if VERBOSE:
        print "Search started at %s and ended at %s" % (start_time, end_time)
        for organism in unique_solutions:
            print "We did it!", organism
            display_board(organism.genome)
 def setUp(self):
     self.selector = TournamentSelection(NoMutation(), NoCrossover(),
                                         NoRepair(), 2)
Exemple #6
0
 def setUp(self):
     self.selector = TournamentSelection(NoMutation(), NoCrossover(),
                                         NoRepair(), 2)