def gene_mutation(self,index): #1位变异算法,不能超过当前最大值(好像存在概率问题??对于uplimit和1<<bitnumber-1之间的数字?又好像没有??) uplimit = self.genes_max[index] curv = self.genes[index] nextv = curv while(nextv > uplimit or nextv == curv): nextv = helper.integer_mutation1(self.genes[index],self.genes_bits[index]) #print curv,self.genes_bits[index],uplimit,nextv return nextv
def gene_mutation( self, index ): #1位变异算法,不能超过当前最大值(好像存在概率问题??对于uplimit和1<<bitnumber-1之间的数字?又好像没有??) uplimit = self.genes_max[index] curv = self.genes[index] nextv = curv while (nextv > uplimit or nextv == curv): nextv = helper.integer_mutation1(self.genes[index], self.genes_bits[index]) #print curv,self.genes_bits[index],uplimit,nextv return nextv
def test_integer_mutation(self): oldr = helper.random.randint try: helper.random.randint = lambda x, y: 0 self.assertEquals(56, helper.integer_mutation1(57, 6)) self.assertEquals(57, helper.integer_mutation1(56, 6)) helper.random.randint = lambda x, y: 5 self.assertEquals(25, helper.integer_mutation1(57, 6)) self.assertEquals(57, helper.integer_mutation1(25, 6)) helper.random.randint = lambda x, y: 3 self.assertEquals(49, helper.integer_mutation1(57, 6)) self.assertEquals(57, helper.integer_mutation1(49, 6)) helper.random.randint = lambda x, y: 0 self.assertEquals(33, helper.integer_mutation1(32, 6)) self.assertEquals(32, helper.integer_mutation1(33, 6)) helper.random.randint = lambda x, y: 5 finally: helper.random.randint = oldr