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

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

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

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Anaconda

Anacondaは、Python本体とPythonで利用されるライブラリを一括でインストールできるパッケージです。環境構築が容易になるため、Python開発者間ではよく利用されており、商用目的としても利用できます。

Python

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

Q&A

解決済

1回答

2237閲覧

jupyter notebookeの実行結果が表示されない

bbiiq

総合スコア51

Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Anaconda

Anacondaは、Python本体とPythonで利用されるライブラリを一括でインストールできるパッケージです。環境構築が容易になるため、Python開発者間ではよく利用されており、商用目的としても利用できます。

Python

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

0グッド

0クリップ

投稿2021/07/24 10:23

編集2021/07/24 12:53

以下のコードを実行したところ結果が表示されません。どうしたらよいのでしょうか
In [*]となったまま止まってる感じです。
カーネルのリスタートなどは試しましたが結果が表示されないままです。
os:windows10

<原因(多分)となっている部分>

python

1def select(eva): 2 random.shuffle(eva) 3 tournament = [()] 4 while len(eva) > 1: 5 if len(eva) % 2 != 0: 6 for i in range(1,len(eva)-1,1): 7 if eva[i] < eva[i+1]: 8 tournament.append(eva[i+1]) 9 else: 10 tournament.append(eva[i]) 11 eva = tournament 12 tournament = [()] 13 else: 14 for i in range(0,len(eva)-1,1): 15 if eva[i] < eva[i+1]: 16 tournament.append(eva[i+1]) 17 else: 18 tournament.append(eva[i]) 19 eva = tournament 20 tournament = [()] 21 return eva 22

<全体のコード>

python

1import random 2import copy 3 4random.seed(1870258) 5gene_length = 10 6# 遺伝子長 7individual_length = 10 # 個体数 8generation = 30 # 世代数 9 10#二次元配列で遺伝子長と個体数を表現 11def get_population(): 12 population = [] 13 for i in range(individual_length): #遺伝子長 14 population.append([random.randint(0,1) for j in range(gene_length)]) #個体数 15 return population 16 17#適応度 18def fitness(pop): 19 return sum(pop) 20 21#評価 22def evaluate(pop): 23 pop.sort(reverse=True) 24 return pop 25 26#トーナメント選択 27def select(eva): 28 random.shuffle(eva) 29 tournament = [()] 30 while len(eva) > 1: 31 if len(eva) % 2 != 0: 32 for i in range(1,len(eva)-1,1): 33 if eva[i] < eva[i+1]: 34 tournament.append(eva[i+1]) 35 else: 36 tournament.append(eva[i]) 37 eva = tournament 38 tournament = [()] 39 else: 40 for i in range(0,len(eva)-1,1): 41 if eva[i] < eva[i+1]: 42 tournament.append(eva[i+1]) 43 else: 44 tournament.append(eva[i]) 45 eva = tournament 46 tournament = [()] 47 return eva 48 49 50#一点交叉 51def one_point_crossover(parent1, parent2): 52 r1 = random.randint(0, gene_length-1) 53 child1 = copy.deepcopy(parent1) 54 child2 = copy.deepcopy(parent2) 55 child1[r1:gene_length-1] = parent2[r1:gene_length-1] 56 child2[r1:gene_length-1] = parent1[r1:gene_length-1] 57 return child1,child2 58 59 60def main(): 61 # 初期個体生成 62 pop = evaluate([(fitness(p), p) for p in get_population()]) 63 print('Generation: 0') 64 print('Min : {}'.format(pop[-1][0])) 65 print('Max : {}'.format(pop[0][0])) 66 print('--------------------------') 67 68 for g in range(generation): 69 if 10 != pop[0][0] or 10 != pop[-1][0]: 70 71 g += 1 72 print('Generation: ' + str(g)) 73 74 75 eva = evaluate(pop) 76 tournament = select(eva) 77 pop = tournament 78 79 while len(pop) < individual_length: 80 m1 = random.randint(0, len(tournament)-1) 81 m2 = random.randint(0, len(tournament)-1) 82 child1,child2 = one_point_crossover(tournament[m1][1], tournament[m2][1]) 83 pop.append((fitness(child1), child1)) 84 pop.append((fitness(child2), child2)) 85 86 print('Min : {}'.format(pop[-1][0]),' Result : {}'.format(pop[-1])) 87 print('Max : {}'.format(pop[0][0]),' Result : {}'.format(pop[0])) 88 print('--------------------------') 89 else: 90 break 91 92 93 94 print('\n--------------------------End of evolution--------------------------') 95 print('Generation: ' + str(g)) 96 print('Min : {}'.format(pop[-1][0]),' Result : {}'.format(pop[-1])) 97 print('Max : {}'.format(pop[0][0]),' Result : {}'.format(pop[0])) 98 99 100 101if __name__ == '__main__': 102 main()

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

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

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

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

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

guest

回答1

0

ベストアンサー

def select(eva)の中のwhileループから抜け出す処理がないのが原因ではないでしょうか?
len(eva)の値を確認すると下記の様にどんどん増えていっています。

10 10 19 36 71 140 279 556 1111 2220 4439 8876 17751 35500 70999 141996 283991 567980 1135959 2271916 4543831 9087660 18175319 36350636 72701271

処理内容が分からないので具体的なコードは提示出来ませんがどこかでbreak処理を入れるかlen(eva)1以下になるような処理が必要だと思います。

投稿2021/07/24 10:42

meg_

総合スコア10579

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

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

bbiiq

2021/07/24 12:46

回答ありがとうございます。 以前も質問に答えていただきありがとうございます。 参考にさせていただきます。
bbiiq

2021/07/24 13:11

実行結果が表示されるようになりました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問