2

1 と 0 が最も多い 8 ビット文字列を決定することを目的とした遺伝的アルゴリズムを作成するように依頼されました。eval 関数は、変更の数に 1 を加えた数を返す必要があります。たとえば、00000000 は 1 を返し、00011100 は 3 を返し、01100101 は 6 を返します。これが私が持っているものです。

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)

遺伝的アルゴリズムの部分(クロスオーバー/突然変異)を実際に理解するのに苦労しています。数字をランダムにペアにしてから、ペアをランダムに選択し、1 つのペアをそのままにして、ランダムなポイントでクロスオーバーする必要があります。その後、人口全体から 1 ビットをランダムに変異させて終了します。crossover と mutate の現在のコードは、私が見つけて理解しようとしていた遺伝的アルゴリズムの例から取ったものです。どんな助けでも大歓迎です。

4

2 に答える 2

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
于 2013-04-13T20:07:44.140 に答える
0

私がやりたいと思ったことの1つはこれです:

  1. 最後のバッチの上位候補を選択して、5 としましょう。
  2. 2, 3, 4, 5 と 1 つのメイトを持ちます。
  3. 3, 4, 5 と 2 つのメイトを持っている
  4. 4 と 5 と 3 のつがいを持つ
  5. 4 匹を 5 匹と交配させます。一般に、元の 5 匹を次の世代に入れ、各交配で 2 匹の子が生まれる場合、この時点に到達する前に個体数はすでにいっぱいになります。1 つの交尾とその反対側の双子。
  6. 実際の交配に関しては、長さの 40% から 60% の間のポイントで染色体をランダムに切断し、次の交配で範囲内の別のランダムなポイントを選択するのが好きです。
  7. それらを交配させた後、染色体のすべてのビットを調べて、約 5% の確率で反転または変異させます。
  8. 場合によっては、極大値または極小値の可能性を下げるために、最悪の 2 つの組み合わせのいくつかを一致させることもあります。

これが少しお役に立てば幸いです。

-ジェフ

編集:ああ、これは4月に尋ねられました。墓穴掘ってごめんなさい。

于 2013-07-13T23:13:21.533 に答える