本文整理汇总了Python中Bio.GA.Organism.random_population方法的典型用法代码示例。如果您正苦于以下问题:Python Organism.random_population方法的具体用法?Python Organism.random_population怎么用?Python Organism.random_population使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Bio.GA.Organism
的用法示例。
在下文中一共展示了Organism.random_population方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from Bio.GA import Organism [as 别名]
# 或者: from Bio.GA.Organism import random_population [as 别名]
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)
示例2: test_random_population_types
# 需要导入模块: from Bio.GA import Organism [as 别名]
# 或者: from Bio.GA.Organism import random_population [as 别名]
def test_random_population_types(self):
"""Creating a random population with different types of alphabets.
"""
class DoubleAlphabet:
letters = [1.0, 2.0]
class CharacterAlphabet:
letters = ["a", "b"]
class IntegerAlphabet:
letters = [1, 2]
def test_fitness(genome):
return 2
all_alphabets = [DoubleAlphabet(), CharacterAlphabet(),
IntegerAlphabet()]
for alphabet in all_alphabets:
new_pop = Organism.random_population(alphabet, 5, 10,
test_fitness)
示例3: test_random_population
# 需要导入模块: from Bio.GA import Organism [as 别名]
# 或者: from Bio.GA.Organism import random_population [as 别名]
def test_random_population(self):
"""Create a population randomly from a alphabet.
"""
num_orgs = 10
genome_size = 5
new_pop = Organism.random_population(self.alphabet, genome_size,
num_orgs, fitness_calculator)
assert len(new_pop) == num_orgs, "Expected %s organisms, got %s" \
% (num_orgs, len(new_pops))
for org in new_pop:
assert isinstance(org, Organism.Organism), \
"Expected to get an organism, got %r" % org
exp_fit = fitness_calculator(org.genome)
assert org.fitness == exp_fit, \
"Expected fitness of %s, got %s" % (org.fitness, exp_fit)
assert len(org.genome) == genome_size, \
"Expected genome size of %s, got %s" % (len(org.genome),
genome_size)
示例4: test_random_population
# 需要导入模块: from Bio.GA import Organism [as 别名]
# 或者: from Bio.GA.Organism import random_population [as 别名]
def test_random_population(self):
"""Create a population randomly from a alphabet.
"""
num_orgs = 10
genome_size = 5
new_pop = Organism.random_population(self.alphabet, genome_size,
num_orgs, fitness_calculator)
self.assertEqual(len(new_pop), num_orgs, "Expected %s organisms, "
"got %s " % (num_orgs, len(new_pop)))
for org in new_pop:
self.assertIsInstance(org, Organism.Organism,
"Expected to get an organism, got %r" % org)
exp_fit = fitness_calculator(org.genome)
self.assertEqual(org.fitness, exp_fit, "Expected fitness of %s, "
"got %s" % (org.fitness, exp_fit))
self.assertEqual(len(org.genome), genome_size, "Expected genome "
"size of %s, got %s"
% (len(org.genome), genome_size))