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

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

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

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

Q&A

解決済

2回答

1409閲覧

なぜかプログラムが動きません..

yutaroooooo

総合スコア32

Python

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

0グッド

0クリップ

投稿2019/12/17 23:55

編集2019/12/18 00:30
#必要なライブラリ,モジュールのインポートーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー import random import random import numpy from deap import algorithms from deap import base from deap import creator from deap import tools #適応度の定義ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー creator.create("FitnessMax", base.Fitness, weights=(1.0,)) #個体の定義ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー """元々遺伝子を保存している個体listに,適応度fitnessというメンバ変数を追加してIndividualクラスを作成""" creator.create("Individual", numpy.ndarray, fitness=creator.FitnessMax) toolbox = base.Toolbox() #1,個体の取りうる値の範囲ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー n_gene = 100 #個体内の遺伝子の個数 min_ind = numpy.ones(n_gene) * -1.0 #取りうる範囲のmax max_ind = numpy.ones(n_gene) * 1.0 #min #個体を生成する関数の定義ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー def create_ind_uniform(min_ind, max_ind): ind = [] for min, max in zip(min_ind, max_ind): ind.append(random.uniform(min, max)) return ind #遺伝子,固体,世代を設定する関数の作成ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー toolbox.register("create_ind", create_ind_uniform, min_ind, max_ind) toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.create_ind) toolbox.register("population", tools.initRepeat, list, toolbox.individual) #2,目的関数の設定ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー def evalOneMax(individual): return sum(individual) #交叉関数の再定義ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー def cxTwoPointCopy(ind1, ind2): size = len(ind1) cxpoint1 = random.randint(1, size) cxpoint2 = random.randint(1, size - 1) if cxpoint2 >= cxpoint1: cxpoint2 += 1 else: # Swap the two cx points cxpoint1, cxpoint2 = cxpoint2, cxpoint1 ind1[cxpoint1:cxpoint2], ind2[cxpoint1:cxpoint2] = ind2[cxpoint1:cxpoint2].copy(), ind1[cxpoint1:cxpoint2].copy() return ind1, ind2 #突然変異関数の再定義ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー def mutUniformDbl(individual, min_ind, max_ind, indpb): size = len(individual) for i, min, max in zip(xrange(size), min_ind, max_ind): if random.random() < indpb: individual[i] = random.uniform(min, max) return individual, #目的関数,交叉,突然変異,選択を求める関数を読み替えるーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー toolbox.register("evaluate", evalOneMax) toolbox.register("mate", cxTwoPointCopy) toolbox.register("mutate", mutUniformDbl, min_ind=min_ind, max_ind=max_ind, indpb=0.05) toolbox.register("select", tools.selTournament, tournsize=3) #3,メイン関数ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー def main(): random.seed(64) pop = toolbox.population(n=300) hof = tools.HallOfFame(1, similar=numpy.array_equal) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", numpy.mean) stats.register("std", numpy.std) stats.register("min", numpy.min) stats.register("max", numpy.max) algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=40, stats=stats,halloffame=hof) return pop, stats, hof #ここがおかしい?? if __name__ == "__main__": main() --------------------------------------------------------------------------- TypeError Traceback (most recent call last) C:\Users\hashimoto\Anaconda3\lib\site-packages\deap\base.py in setValues(self, values) 184 try: --> 185 self.wvalues = tuple(map(mul, values, self.weights)) 186 except TypeError: TypeError: 'numpy.float64' object is not iterable During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) <ipython-input-1-b85bc257b291> in <module> 92 93 if __name__ == "__main__": ---> 94 main() <ipython-input-1-b85bc257b291> in main() 87 stats.register("max", numpy.max) 88 ---> 89 algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=1000, stats=stats,halloffame=hof) 90 91 return pop, stats, hof C:\Users\hashimoto\Anaconda3\lib\site-packages\deap\algorithms.py in eaSimple(population, toolbox, cxpb, mutpb, ngen, stats, halloffame, verbose) 150 fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) 151 for ind, fit in zip(invalid_ind, fitnesses): --> 152 ind.fitness.values = fit 153 154 if halloffame is not None: C:\Users\hashimoto\Anaconda3\lib\site-packages\deap\base.py in setValues(self, values) 191 "fitness with weights %s." 192 % (self.__class__, values, type(values), --> 193 self.weights)).with_traceback(traceback) 194 195 def delValues(self): C:\Users\hashimoto\Anaconda3\lib\site-packages\deap\base.py in setValues(self, values) 183 def setValues(self, values): 184 try: --> 185 self.wvalues = tuple(map(mul, values, self.weights)) 186 except TypeError: 187 _, _, traceback = sys.exc_info() TypeError: Both weights and assigned values must be a sequence of numbers when assigning to values of <class 'deap.creator.FitnessMax'>. Currently assigning value(s) -1.3549805685970155 of <class 'numpy.float64'> to a fitness with weights (1.0,).

長文失礼します.プログラムが何故か動きません.おそらく最後の二行に問題があると思うのですがどなたか助けていただけないでしょうか.

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

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

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

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

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

y_waiwai

2019/12/17 23:59

このままではコードが読めないので、質問を編集し、<code>ボタンを押し、出てくる’’’の枠の中にコードを貼り付けてください
yutaroooooo

2019/12/18 00:05

アドバイスありがとうございます. すぐに編集します!!
y_waiwai

2019/12/18 00:12

おお、すばらしい ありがとうございます あと、エラー・メッセージはでてきた全文を提示しただければよろしいかと
yutaroooooo

2019/12/18 00:28

何度も申し訳ございません.. すぐに編集します!!
guest

回答2

0

ベストアンサー

主なエラーは下記の2点です。

  1. TypeError: 'numpy.float64' object is not iterable

During handling of the above exception, another exception occurred:

訳:TypeError: 'numpy.float64'オブジェクトは反復可能ではありません。上記の例外の処理中に、別の例外が発生しました。

  1. TypeError: Both weights and assigned values must be a sequence of numbers when assigning to values of <class 'deap.creator.FitnessMax'>. Currently assigning value(s) -1.3549805685970155 of <class 'numpy.float64'> to a fitness with weights (1.0,).

訳:TypeError:<class 'deap.creator.FitnessMax'>の値に割り当てる場合、重みと割り当てられた値の両方が数字のシーケンスでなければなりません。 現在、<class 'numpy.float64'>の値-1.3549805685970155を重み(1.0、)のフィットネスに割り当てています。

Python

1n_gene = 100 #個体内の遺伝子の個数 2min_ind = numpy.ones(n_gene) * -1.0 #取りうる範囲のmax 3max_ind = numpy.ones(n_gene) * 1.0 #min

多文原因はここでしょう。
numpy.onesで返される値は、この場合、100個配列なので*-1.0をしようとしてダメだと言われているのではないでしょうか?
格好悪いと思いますが、for文回した方が確実だと思います。

そしてこれは、あまり関係ない答えかも知れませんが、main関数で戻り値を返しているにも関わらず、その戻り値を受け取っていません。意味ないのでmain関数では戻り値を設定しないように願います。

投稿2019/12/18 01:21

stdio

総合スコア3307

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

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

0

from deap import base

という内容からエラーが呼ばれてます。
このファイルが自作のものであれば、その中身を修正してください。

また、外部でインストールしているものであれば、関数に引き渡す変数などに問題があると思います。

投稿2019/12/18 01:00

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問