質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1431閲覧

PyhtonによるAttributeErrorについてです

bbiiq

総合スコア51

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/07/16 00:42

編集2021/07/16 01:34

前提・実現したいこと

Pythonによる遺伝的アルゴリズムを使ったOneMax問題プログラムを参考書を見ながら打ち込んだのですが、以下のエラーが発生してしましす。
原因は、ToolBoxオブジェクトにIndividualがないということらしいのですが、どう改善したらよいのでしょうか?

発生している問題・エラーメッセージ

AttributeError Traceback (most recent call last) <ipython-input-1-6818f7387b43> in <module> 21 toolbox.register("Individual",tools.initRepeat,creator.Individual,toolbox.attr_bool,num_bits) 22 ---> 23 toolbox.register("population",tools.initRepeat,list,toolbox.individual) 24 toolbox.register("evaluate",eval_func) 25 toolbox.register("mate",tools.cxTwoPoint) AttributeError: 'Toolbox' object has no attribute 'individual'

該当のソースコード

Python

1import random 2from deap import base,creator, tools 3random.seed(7) 4num_bits = 75 5 6 7#----適応度関数(1を75個生成し、そのうち45個を1にすることが目標)---- 8def eval_func(individual): 9 target_sum = 45 10 return(len(individual) - abs(sum(individual) - target_sum),) 11 12 13#----適応度関数や個体群(遺伝子)を管理するためのオブジェクト生成---- 14creator.create("FitnessMax",base.Fitness,weight=(1.0,)) 15creator.create("Individual",list,fitness=creator.FitnessMax) 16 17 18#----toolboxを作成し、関数を定義しながら格納する---- 19toolbox = base.Toolbox() 20toolbox.register("attr_bool",random.randint,0,1) 21toolbox.register("Individual",tools.initRepeat,creator.Individual,toolbox.attr_bool,num_bits) 22 23toolbox.register("population",tools.initRepeat,list,toolbox.individual) 24toolbox.register("evaluate",eval_func) 25toolbox.register("mate",tools.cxTwoPoint) #交叉 26toolbox.register("mutate",tools.mutFlipBit,indpb=0.05) 27toolbox.register("select",tools.selTournament,tournsize=3) 28 29 30#----初期個体生成---- 31population = toolbox.population(n=500) 32probab_crossing,probab_mutating = 0.5,0.2 33num_generations = 60 34 35 36#----個体評価---- 37print('\n評価開始') 38fitnesses = list(map(toolbox.evaluate,population)) 39for ind,fit in zip(population , fitnesses): 40 ind.fitness.values = fit 41 42print('\n評価済み個体 : ',len(population)) 43for g in range(num_generations): 44 print('\n======第' , g , '世代======') 45 46 47#----新世代作成---- 48offspring = toolbox.select(population,len(population)) 49offspring = list(map(toolbox.clone,offspring)) 50 51 52#----交叉---- 53for child1,child2 in zip(offspring[::2],offspring[1::2]): 54 if random.random() < probab_crossing: 55 toolbox.mate(child1,child2) 56 #適応度をリセットする 57 del child1.fitness.values 58 del child2.fitness.values 59 60 61#----突然変異---- 62for mutant in offspring: 63 if random.random() < probab_mutating: 64 toolbox.mutate(mutant) 65 del mutant.fitness.values 66 67 68#----リセットされた個体を再評価---- 69invalid_ind = [ind for ind in offspring if not ind.fitness.valid] 70fitnesses = map(toolbox.evaluate,invalid_ind) 71for ind , fit in zip(invalid_ind,fitnesses): 72 ind.fitness.values = fit 73 74print('Evaluated',len(invalid_ind),'individuals') 75 76 77#----新世代に置き換える---- 78population[:] = offspring 79 80 81#----進捗を表示---- 82fits = [ind.fitness.values[0] for ind in population] 83length = len(poplulation) 84mean = sum(fits) / length 85sum2 = sum(x*x for x in fits) 86std = abs(sum2 / length - mean**2)**0.5 87print('最小 = ', min(fits),' , 最大 = ',max(fits)) 88print('平均 = ',round(mean,2),' , 標準偏差 = ',round(std,2)) 89 90 91#----最終出力---- 92print('\n======最終評価======') 93best_ind = tools.selBest(population,1)[0] 94print('\n最大となるのは : \n',best_ind) 95print('\n1の個数 : ',sum(best_ind))

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

その場合Iは大文字であるべきなのではないですか?

投稿2021/07/16 01:01

編集2021/07/16 01:02
szx

総合スコア36

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

bbiiq

2021/07/16 01:37

ありがとうございます。解決しました。
szx

2021/07/16 01:40

補足にはなりますが、基本的に大文字小文字が別だと違う扱いになるので、21行目でIndividualとしているならほかで使うときもIでないとだめなんですね。 よくあるミスですので今後は確かめてみるといいと思いますよ~。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問