当前位置: 首页>>代码示例>>Python>>正文


Python Population.max_fitness方法代码示例

本文整理汇总了Python中population.Population.max_fitness方法的典型用法代码示例。如果您正苦于以下问题:Python Population.max_fitness方法的具体用法?Python Population.max_fitness怎么用?Python Population.max_fitness使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在population.Population的用法示例。


在下文中一共展示了Population.max_fitness方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: evolve

# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import max_fitness [as 别名]
def evolve(target, replace):
    replacements = 0

    population = Population(500, target)
    while population.max_fitness() < len(target):
        replace(population)
        replacements = replacements + 1

    print("After",replacements,"replacements, the best individual is \"" +
          str(population.best_individual()) + "\".")
开发者ID:HarryCutts,项目名称:like-a-weasel,代码行数:12,代码来源:genetic_algorithm.py

示例2: TestPopulation

# 需要导入模块: from population import Population [as 别名]
# 或者: from population.Population import max_fitness [as 别名]
class TestPopulation(TC):
    def setUp(self):
        self.orgs = [MockOrganism(1), MockOrganism(2),
                     MockOrganism(3), MockOrganism(4)]

        self.pop = Population(self.orgs)

    def test_init_nogivensize(self):
        self.assertEqual(self.pop.carrying_capacity, 4)

    def test_init_givensize(self):
        population = Population([1, 2, 3, 4], carrying_capacity=5)
        self.assertEqual(population.carrying_capacity, 5)

    def test_removal(self):
        population = Population([1, 2, 3], carrying_capacity=2)
        population.remove_at_random()
        self.assertEqual(len(population), 2)

    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)

    def test_replicate(self):
        self.pop.replicate()
        self.assertEqual(8, len(self.pop))

    def test_advance(self):
        big_pop = Population(
            [MockOrganism(1), MockOrganism(2),
                MockOrganism(90)], 3)

        big_pop.advance_generation()
        self.assertLessEqual(len(big_pop), 3)

    def test_iter_len(self):
        self.assertEqual(4, len(self.pop))

        for org, poporg in zip(self.orgs, self.pop):
            self.assertEqual(org, poporg)

    def test_add_to_pop(self):
        org = MockOrganism(6)
        self.pop.add_to_pop(org)
        self.assertEqual(5, len(self.pop))

    def test_set(self):
        self.pop[2] = MockOrganism(6)
        print self.pop[2]
        self.assertEqual(self.pop[2].eval_fit(), MockOrganism(6).eval_fit())

    def test_full(self):
        self.assertTrue(self.pop.is_full())

    def test_max_fit(self):
        self.assertEqual(self.pop.max_fitness(), MockOrganism(4).eval_fit())

    def test_mean_fit(self):
        self.assertEqual(self.pop.mean_fitness(), 2.5)
开发者ID:jaredc-s,项目名称:structure_and_landscapes,代码行数:66,代码来源:test_population.py


注:本文中的population.Population.max_fitness方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。