def test_run_2(self): #第二代找到最优解 #print 'begin run2' class TCell(object):pass cell,cell2 = TCell(),TCell() pls = [cell,cell] pls2 = [cell2,cell2] proxy = pmock.Mock() proxy.expects(pmock.once()).judge(pmock.eq(cell)).will(pmock.return_value(88)) proxy.expects(pmock.once()).judge(pmock.eq(cell2)).will(pmock.return_value(100)) proxy.stubs().method('fitness_ranker').will(pmock.return_value([50,50])) proxy.stubs().method('selector').will(pmock.return_value(([cell,cell],[cell,cell]))) reproducer = lambda seeds,gametes : pls2 reproducer.times_of_length = 1 nature = Nature(proxy.judge,proxy.fitness_ranker,proxy.selector,reproducer) times = 20 rpls,laststep = nature.run(pls,20) proxy.verify() self.assertEquals(1,laststep) self.assertEquals(pls2,rpls)
def test_cached_judge(self): class TCell(object):pass cell = TCell() proxy = pmock.Mock() proxy.expects(pmock.once()).judge(pmock.eq(cell)).will(pmock.return_value(88)) reproducer = helper.simple_reproducer_factory(0.8,0.001) nature = Nature(proxy.judge,helper.nonlinear_rank,helper.roulette_wheel_selection_factory(reproducer.times_of_length),reproducer) nature.cached_judge(cell) nature.cached_judge(cell) nature.cached_judge(cell) proxy.verify()