2013-04-13 49 views
2

我被要求制定一个遗传算法的目标,以确定一个8位字符串与最1和0的。 eval函数将返回改变的1数加上所以,例如00000000返回1,00011100回报3,和01100101回报6.这是我有:Python遗传算法的二进制数

def random_population(): 
    from random import choice 

    pop = ''.join(choice(('0','1')) for _ in range(8)) 
    return pop 

def mutate(dna): 
    """ For each gene in the DNA, there is a 1/mutation_chance chance 
    that it will be switched out with a random character. This ensures 
    diversity in the population, and ensures that is difficult to get stuck in 
    local minima. """ 
    dna_out = "" 
    mutation_chance = 100 
    for c in xrange(DNA_SIZE): 
     if int(random.random()*mutation_chance) == 1: 
      dna_out += random_char() 
     else: 
      dna_out += dna[c] return dna_out 

def crossover(dna1, dna2): 
    """ Slices both dna1 and dna2 into two parts at a random index within their 
    length and merges them. Both keep their initial sublist up to the crossover 
    index, but their ends are swapped. """ 
    pos = int(random.random()*DNA_SIZE) 
    return (dna1[:pos]+dna2[pos:], dna2[:pos]+dna1[pos:]) 

def eval(dna): 
    changes = 0 
    for index, bit in enumerate(dna): 
     if(index == 0): 
      prev = bit 
     else: 
      if(bit != prev): 
       changes += 1 
     prev = bit 
    return changes+1 


#============== End Functions =======================# 


#============== Main ================# changes = 0 

prev = 0 
dna = random_population() 
print "dna: " 
print dna 
print eval(dna) 

我无法真正搞清楚遗传算法部分(交叉/变异)。我应该随机配对数字,然后随机选择一对,保持一对不变,然后在随机点交叉。然后它将随着整个人口中的一点点随机变异而结束。目前的交叉和变异编码仅仅来自我发现并试图理解的遗传算法示例。欢迎任何fhelp。

+0

一个人口由许多个人组成 - 我只看到一个“dna”。交叉有助于收敛基因的“子程序”,而突变有助于创造出达到目标所需的错误。 – User

+0

此外,你需要一个fittness功能,确定一个人如何可能进行交叉和重组。您可以使用轮盘赌轮http://en.wikipedia.org/wiki/Fitness_proportionate_selection来确定哪些人可以交叉并创建进入下一代的孩子。 – User

回答

1

我建议的一部分:

该代码不起作用,但可能传输信息。我发现,我喜欢做

# a population consists of many individuals 
def random_population(population_size = 10): 
    from random import choice 

    pop = [''.join(choice(('0','1')) for _ in range(8)) for i in range(population_size)] 
    return pop 

# you need a fitness function 
def fitness(individual): 
    return # a value from 0 up 

def algorithm(): 
    # a simple algorithm somehow alike 
    # create population 
    population = random_population() 
    # this loop must stop after some rounds because the best result may never be reached 
    while goal_not_reached(population) and not time_is_up(): 
     # create the roulette wheel 
     roulette_wheel = map(fitness, population) 
     # highest value of roulette wheel 
     max_value = sum(roulette_wheel) 
     # the new generation 
     new_population = [] 
     for i in range(len(population) - len(new_population)): 
      # create children from the population 
       # choose 2 random values from 0 to max_value and find the individuals 
       # for it in the roulette wheel, combine them to new individuals 
      new_population.append(new_individual) 
     # mutate the population 
     population = map(mutate, new_population)    # a new generation is created 
0

有一件事情是这样的:

  1. 选择最后一批的热门人选让说5
  2. 有1个伴侣与2,3, 4,5
  3. 有2个队友与3,4,5
  4. 有3个队友与4和5
  5. 有4紧密配合5.一般来说你的人口都已经满befor如果你让原来的5进入下一代并且每次交配产生2个后代,你就达到了这一点。一个交配及其相反的双胞胎。
  6. 只要我喜欢在40%到60%的长度上随机切割染色体的实际交叉点,那么在下一次交配时,我会选择另一个随机点。
  7. 我将它们交配后,我会在我的染色体中检查每一点,并给它大概一个!5%的翻转或变异几率
  8. 有时我也会让一些最糟糕的两个伴侣降低我的机会本地最大值或最小值

我希望这对你有一点帮助。

-Jeff

编辑:哦,我这是在4月份的问道。对不起,严重挖掘。