前提
実現したいこと
以前もここで質問されてたのですが詳細が書いていなかったためもう一度質問します
ythonで遺伝的アルゴリズムを使い、OneMax問題を解くプログラムを作成したいのですが、一様交叉の部分がうまくいっていません。エラーは起きていないのですが、配列に変化が起きていないので交叉の部分がよくないと考えているのですが、原因がわかりません。
何が悪いのか、これを試してはという意見がありましたら教えていただきたいです。
発生している問題・エラーメッセージ
エラーメッセージ
該当のソースコード
python
ソースコード
import random import copy random.seed(1870258) gene_length = 10 # 遺伝子長 individual_length = 10 # 個体数 generation = 30 # 世代数 elite_rate = 0.2 # エリート選択の割合 #個体生成 def get_population(): population = [] for i in range(individual_length): #遺伝子長 population.append([random.randint(0,1) for j in range(gene_length)]) #個体数 return population #適応度 def fitness(pop): return sum(pop) #評価 def evaluate(pop): pop.sort(reverse=True) return pop #エリート選択 def select(eva,pop): elites = eva[:int(len(pop)*elite_rate)] return elites #一様交叉 def cxUniform(parent1, parent2): for i in range(len(parent1)): if random.random() < 0.5: parent1[i], parent2[i] = parent2[i], parent1[i] return random.choice([parent1,parent2]) def main(): # 初期個体生成 pop = evaluate([(fitness(p), p) for p in get_population()]) print('Generation: 0') print('Min : {}'.format(pop[-1][0])) print('Max : {}'.format(pop[0][0])) print('--------------------------') for g in range(generation): if 10 != pop[0][0] and 10 != pop[-1][0]: g += 1 print('Generation: ' + str(g)) #選択 eva = evaluate(pop) elites = select(eva,pop) #交叉 new_pop = elites while len(new_pop) < individual_length: m1 = random.randint(0, len(elites)-1) m2 = random.randint(0, len(elites)-1) child = cxUniform(elites[m1][1], elites[m2][1]) new_pop.append((fitness(child), child)) new_pop = evaluate(new_pop) print('Min : {}'.format(pop[-1][0]),' Result : {}'.format(pop[-1])) print('Max : {}'.format(pop[0][0]),' Result : {}'.format(pop[0])) print('--------------------------') else: break print('\n--------------------------End of evolution--------------------------') print('Generation: ' + str(g)) print('Min : {}'.format(pop[-1][0]),' Result : {}'.format(pop[-1])) print('Max : {}'.format(pop[0][0]),' Result : {}'.format(pop[0])) if __name__ == '__main__': main() ### 試したこと ここに問題に対して試したことを記載してください。 実行結果 Generation: 0 Min : 2 Max : 8 -------------------------- Generation: 1 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 1, 1, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 2 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 0, 0, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 3 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 0, 0, 1, 1, 1, 1, 1, 1]) -------------------------- Generation: 4 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 0, 1, 1, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 5 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 0, 1, 1, 1, 1, 1, 1, 1, 1]) -------------------------- Generation: 6 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 0, 0, 1, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 7 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 0, 1, 0, 1, 1, 1, 1, 1, 1]) -------------------------- Generation: 8 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 0, 0, 1, 1, 1, 1, 1, 1]) -------------------------- Generation: 9 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 1, 0, 1, 1, 1, 1, 1, 1]) -------------------------- Generation: 10 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 1, 0, 1, 1, 1, 1, 1, 1]) -------------------------- Generation: 11 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 0, 1, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 12 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 0, 1, 0, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 13 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 0, 0, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 14 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 1, 0, 1, 1, 1, 1, 1, 1]) -------------------------- Generation: 15 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 0, 0, 0, 1, 1, 1, 1, 1, 1]) -------------------------- Generation: 16 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 0, 1, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 17 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 0, 0, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 18 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 0, 1, 1, 1, 1, 1, 1, 1, 1]) -------------------------- Generation: 19 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 0, 0, 0, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 20 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 0, 1, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 21 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) -------------------------- Generation: 22 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 0, 0, 1, 1, 1, 1, 1, 1]) -------------------------- Generation: 23 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 0, 0, 1, 1, 1, 1, 1, 1]) -------------------------- Generation: 24 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 0, 0, 1, 1, 1, 1, 1, 1, 1]) -------------------------- Generation: 25 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 0, 0, 0, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 26 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 0, 1, 0, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 27 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 0, 1, 0, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 28 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 0, 0, 0, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 29 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 0, 0, 1, 1, 0, 1, 1, 1]) -------------------------- Generation: 30 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 0, 0, 1, 1, 0, 1, 1, 1]) -------------------------- --------------------------End of evolution-------------------------- Generation: 30 Min : 2 Result : (2, [1, 0, 0, 0, 0, 0, 1, 0, 0, 0]) Max : 8 Result : (8, [1, 1, 0, 0, 1, 1, 0, 1, 1, 1]) ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/08/28 12:28
退会済みユーザー
2022/08/28 12:31 編集
2022/08/28 12:36
2022/08/28 12:37
退会済みユーザー
2022/08/28 12:44
2022/08/28 13:23
退会済みユーザー
2022/08/29 14:01 編集