本文整理汇总了Python中population.Population类的典型用法代码示例。如果您正苦于以下问题:Python Population类的具体用法?Python Population怎么用?Python Population使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Population类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_advance
def test_advance(self):
big_pop = Population(
[MockOrganism(1), MockOrganism(2),
MockOrganism(90)], 3)
big_pop.advance_generation()
self.assertLessEqual(len(big_pop), 3)
示例2: test_evolve
def test_evolve(self):
pop1 = Population(4)
pop1.evolve()
self.assertEqual(len(pop1.pop), 4)
self.assertEqual(pop1.selected, [])
self.assertEqual(pop1.selected_indexes, [])
self.assertEqual(pop1.fitness_table, [])
示例3: run_synth_test
def run_synth_test():
""" Run a test with synthetic data and MCMC inference
"""
# Parse command line args
(options, args) = parse_cmd_line_args()
print "Creating master population object"
model = make_model(options.model, N=options.N)
popn = Population(model)
results_file = os.path.join(options.resultsDir, "geweke_results.pkl")
if os.path.exists(results_file) and not options.force_recompute:
with open(results_file) as f:
x_smpls = cPickle.load(f)
else:
x0 = popn.sample()
data = gen_synth_data(popn, x0, options.N, options.T)
popn.set_data(data)
# Perform inference
N_samples = 1000
x_smpls = geweke_test(popn, data, N_samples=N_samples)
# Save results
print "Saving results to %s" % results_file
with open(results_file, "w") as f:
cPickle.dump(x_smpls, f, protocol=-1)
# Plot empirical parameter distributions
print "Plotting results."
plot_geweke_results(popn, x_smpls, model, resdir=options.resultsDir)
示例4: test_evalutaion
def test_evalutaion(self):
pop1 = Population(4)
result = pop1.evaluate()
self.assertEqual(len(pop1.fitness_table), 4)
self.assertEqual(pop1.fitness_table, result)
self.assertIn(pop1.pop[0].fitness, [0, 1])
self.assertEqual(pop1.pop[0].fitness, result[0])
示例5: TestPopulationBitstring
class TestPopulationBitstring(unittest.TestCase):
def setUp(self):
bit0 = Bitstring("0000")
bit1 = Bitstring("1111")
col0 = ColicinBit(bit0)
col1 = ColicinBit(bit1)
org = Organism([col0], [ImmunityBit(bit0, 2)])
self.pop = Population([org for _ in range(5)])
def test_replicate(self):
self.assertEqual(len(self.pop), 5)
self.pop.replicate()
self.assertEqual(len(self.pop), 10)
def test_remove_self_toxic(self):
self.assertEqual(len(self.pop), 5)
self.pop.remove_self_toxic()
self.assertEqual(len(self.pop), 5)
def test_cull_by_iterative_colicin(self):
self.assertEqual(len(self.pop), 5)
self.pop.cull_by_iterative_colicin()
self.assertEqual(len(self.pop), 5)
def test_advance_generation(self):
self.pop.advance_generation()
def test_sample_with_replacement(self):
self.pop.sample_with_replacement()
示例6: tournament
def tournament(self,population,data,dataEvaluation,heuristicStrategy,attributeCount):
gladiators = Population(self.gladiator,data,dataEvaluation,False,heuristicStrategy)
for index in range(self.gladiator):
chosenOne = random.randint(0,population.size()-1)
gladiators.saveIndividuals(index,population.getIndividual(chosenOne))
morpheus = gladiators.getFittest()
return morpheus
示例7: test_population_step
def test_population_step(self):
p = Population()
data_before = p.data[:]
size_before = len(p)
p.step()
self.assertEqual(len(p.data), size_before)
self.assertNotEqual(p.data, data_before)
示例8: __init__
class AI:
def __init__(self):
self.score = 0
self.count = 0
self.population = Population(10)
def restart_game(self):
if self.score > 0:
print("{ ind_score: " + str(self.score) + " }")
self.count += 1
self.population.get_cur().score = self.score
self.population.go_next()
def get_allowed_moves(self, board):
allowed = [False, False, False, False]
for x in range(0, 4):
for y in range(0, 4):
if board[y][x] != None:
if y < 3 and board[y + 1][x] in (None, board[y][x]):
allowed[0] = True # DOWN
if y > 0 and board[y - 1][x] in (None, board[y][x]):
allowed[1] = True # UP
if x > 0 and board[y][x - 1] in (None, board[y][x]):
allowed[2] = True # LEFT
if x < 3 and board[y][x + 1] in (None, board[y][x]):
allowed[3] = True # RIGHT
return allowed
def get_move(self, score, board):
self.score = score
features = feature_reader.extract_features(board)
g = self.population.get_cur()
return g.decide(features, self.get_allowed_moves(board))
示例9: tourney
def tourney(self):
tourneyPop = Population(Breeder.tourneySize, False)
for i in xrange(self.pop.size()):
tourneyPop.addIndividual( self.pop.individuals[random.randrange(0,self.pop.size())] )
return tourneyPop.getFittest()
示例10: test_leastfit_removal
def test_leastfit_removal(self):
this_pop = Population([MockOrganism(1), MockOrganism(2),
MockOrganism(100)], carrying_capacity=1)
this_pop.remove_least_fit()
self.assertIn(this_pop[0].eval_fit(),
[1, 2, 100])
self.assertEqual(len(this_pop), 1)
示例11: test_normal_random_population_creation
def test_normal_random_population_creation(self):
# XXX: I suppose it's possible these asserts could fail because we're dealing with
# probabalistic outcomes; I haven't taken the time to prove these always work
p = Population()
p.randomize(1024)
self.assertLess(len(p.coaches), len(p.students))
self.assertLess(len(p.studying_coaches), len(p.coaches))
self.assertLess(1, max([len(u.coaches()) for u in p.students]))
示例12: tournament_selection
def tournament_selection(self, pop):
tournament = Population(self._tournament_size, False)
for i in xrange(self._tournament_size):
random_id = int(random.random() * pop.population_size())
tournament.save_tour(i, pop.get_tour(random_id))
return tournament.get_fittest()
示例13: colicin_int_demo
def colicin_int_demo():
org = Organism([Colicin(0)], [Immunity(0, 5)])
pop = Population(org for _ in range(5))
for gen in range(1000):
pop.advance_generation()
average_id = average_colicin_int(pop)
if gen % 100 == 0:
print("{}\t{}".format(gen, average_id))
示例14: create
def create(self):
population = Population()
population.chromosomes = [self.chromosomeFactory.create() for _ in xrange(self.populationSize)]
population.fitnessFunction = self.fitnessFunction
return population
示例15: test_pessimistic_random_population_creation
def test_pessimistic_random_population_creation(self):
p = Population()
# 10 students who are all infected, all teachers, and all study
p.randomize(10, infect_rate=1, feature='A', coach_rate=1, coach_study_rate=1)
self.assertEqual(10, p.N)
self.assertEqual(p.N, len(p.population))
self.assertEqual(len(p.population), len(p.infected))
self.assertEqual(len(p.infected), len(p.students))
self.assertEqual(len(p.students), len(p.coaches))