예제 #1
0
    def find_schemas(self, fitness, num_schemas):
        """Find the given number of unique schemas using a genetic algorithm

        Arguments:

        - fitness - A callable object (ie. function) which will evaluate
        the fitness of a motif.

        - num_schemas - The number of unique schemas with good fitness
        that we want to generate.
        """
        start_population = \
           Organism.function_population(self.motif_generator.random_motif,
                                        self.initial_population,
                                        fitness)
        finisher = SimpleFinisher(num_schemas, self.min_generations)

        # set up the evolver and do the evolution
        evolver = GenerationEvolver(start_population, self.selector)
        evolved_pop = evolver.evolve(finisher.is_finished)

        # convert the evolved population into a PatternRepository
        schema_info = {}
        for org in evolved_pop:
            # convert the Genome from a MutableSeq to a Seq so that
            # the schemas are just strings (and not array("c")s)
            seq_genome = org.genome.toseq()
            schema_info[str(seq_genome)] = org.fitness

        return PatternRepository(schema_info)
예제 #2
0
    def find_schemas(self, fitness, num_schemas):
        """Find the given number of unique schemas using a genetic algorithm

        Arguments:

        o fitness - A callable object (ie. function) which will evaluate
        the fitness of a motif.

        o num_schemas - The number of unique schemas with good fitness
        that we want to generate.
        """
        start_population = \
           Organism.function_population(self.motif_generator.random_motif,
                                        self.initial_population,
                                        fitness)
        finisher = SimpleFinisher(num_schemas, self.min_generations)

        # set up the evolver and do the evolution
        evolver = GenerationEvolver(start_population, self.selector)
        evolved_pop = evolver.evolve(finisher.is_finished)

        # convert the evolved population into a PatternRepository
        schema_info = {}
        for org in evolved_pop:
            # convert the Genome from a MutableSeq to a Seq so that
            # the schemas are just strings (and not array("c")s)
            seq_genome = org.genome.toseq()
            schema_info[str(seq_genome)] = org.fitness

        return PatternRepository(schema_info)
예제 #3
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=0.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)
예제 #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)