teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

エラーの場所の明記

2021/04/26 14:34

投稿

papikonoqui
papikonoqui

スコア10

title CHANGED
File without changes
body CHANGED
@@ -5,7 +5,9 @@
5
5
  どなたかお力添えいただけると幸いです。
6
6
 
7
7
  ### 発生している問題・エラーメッセージ
8
-
8
+ エラーはevaluation関数の
9
+     fitness.append(sum(gene[i]))
10
+ で発生しています。
9
11
  ```
10
12
  TypeError: unsupported operand type(s) for +: 'int' and 'list'
11
13
  ```

1

関数の呼び出しを行うmain関数のソースコードを追加しました。

2021/04/26 14:34

投稿

papikonoqui
papikonoqui

スコア10

title CHANGED
File without changes
body CHANGED
@@ -14,6 +14,7 @@
14
14
  以下のコードは
15
15
  first_generation():要素数10の0と1のビット配列を100個生成→リストgeneに100個体を格納
16
16
  evaluation(gene):要素の合計を適応度とし、100個体分の適応度をリストfitnessに格納
17
+ main(): 他関数の呼び出しを行う。
17
18
  となっています
18
19
  ```ここに言語名を入力
19
20
  def first_generation():
@@ -25,6 +26,29 @@
25
26
  for i in range(100):
26
27
  fitness.append(sum(gene[i]))
27
28
  return fitness
29
+
30
+ def main():
31
+ gene = first_generation()
32
+ next_gene = []
33
+ count_a = 0
34
+ while count_a < generation:
35
+ fitness = evaluation(gene)
36
+ generation_evaluation(fitness, count_a+1)
37
+ elite = elite_selection(gene, fitness)
38
+ # 優秀な30個体はそのまま引き継がれる
39
+ next_gene.append(elite)
40
+
41
+ count_b = 0
42
+ while count_b < child_n: # 生成する子個体の数は70
43
+ parents = tournament_selection(gene, fitness)
44
+ child = cross(parents)
45
+ child = mutation(child)
46
+ next_gene.append(child)
47
+ count_b += 1
48
+
49
+ gene = copy.deepcopy(next_gene)
50
+ count_a += 1
51
+
28
52
  ```
29
53
 
30
54
  ### 試したこと