質問編集履歴
12
追記4を追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -112,4 +112,43 @@
|
|
112
112
|
val piyo = java.util.function.Function { gf: Genotype<BitGene> -> eval(gf) }
|
113
113
|
```
|
114
114
|
|
115
|
+
###■追記4
|
116
|
+
|
117
|
+
KSwordOfHasteさんに教えていただいた結果、動作するようにできました。
|
118
|
+
ご指摘のとおり、kotlinのコンパイラのバグのようです。
|
115
|
-
|
119
|
+
Collectorのメソッド型引数を明示的にすることで CompilationException 発生しなくなりました。
|
120
|
+
|
121
|
+
```Kotlin
|
122
|
+
// 2.) Definition of the fitness function.
|
123
|
+
fun eval(gt: Genotype<BitGene>): Int {
|
124
|
+
return gt.chromosome
|
125
|
+
.`as`(BitChromosome::class.java)
|
126
|
+
.bitCount()
|
127
|
+
}
|
128
|
+
|
129
|
+
fun main(vararg args: String) {
|
130
|
+
|
131
|
+
// 1.) Define the genotype (factory) suitable for the problem.
|
132
|
+
val gtf = Genotype.of(BitChromosome.of(10, 0.5))
|
133
|
+
|
134
|
+
// 3.) Create the execution environment.
|
135
|
+
val engine = Engine.builder(java.util.function.Function<Genotype<BitGene>, Int> { eval(it) }, gtf).build()
|
136
|
+
|
137
|
+
// 4.) Start the execution (evolution) and collect the result.
|
138
|
+
val result = engine.stream()
|
139
|
+
.limit(100)
|
140
|
+
.collect(EvolutionResult.toBestGenotype<BitGene, Int>())
|
141
|
+
|
142
|
+
println("Hello World:\n$result")
|
143
|
+
|
144
|
+
}
|
145
|
+
```
|
146
|
+
|
147
|
+
ちなみに結果は以下のようになり、遺伝的アルゴリズムにより10個体100世代で10bitのうち最もビットが立つものを探索する、というものでした。
|
148
|
+
|
149
|
+
```
|
150
|
+
Hello World:
|
151
|
+
[00000011|11111111]
|
152
|
+
```
|
153
|
+
|
154
|
+
ありがとうございました。
|
11
追記3を追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -55,7 +55,9 @@
|
|
55
55
|
|
56
56
|
---
|
57
57
|
|
58
|
+
|
58
|
-
■追記1
|
59
|
+
###■追記1
|
60
|
+
|
59
61
|
「3.」の部分を以下のように書き換えたところ、コンパイルは通ったのですが、実行時に以下のエラーとなってしまいました。
|
60
62
|
|
61
63
|
```Kotlin
|
@@ -86,12 +88,28 @@
|
|
86
88
|
|
87
89
|
```
|
88
90
|
|
89
|
-
---
|
90
91
|
|
91
|
-
■追記2
|
92
|
+
###■追記2
|
92
93
|
「3.」の部分を以下のように書き換えたところ、追記1と同じ現象になってしましました。
|
93
94
|
|
94
95
|
```Kotlin
|
95
96
|
val func: java.util.function.Function<Genotype<BitGene>, Int> = java.util.function.Function { gf: Genotype<BitGene> -> eval(gf) }
|
96
97
|
val engine = Engine.builder(func, gtf).build()
|
97
|
-
```
|
98
|
+
```
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
###■追記3
|
103
|
+
|
104
|
+
元の問題についてはだいぶわかりました。変性は関係ありませんでした。
|
105
|
+
当たり前なのかもしれませんが、 Java の Function を引数にとるメソッドに Kotlin のラムダ式を渡そうとしても型が違うのでコンパイルが通らない、ということを理解していませんでした。
|
106
|
+
|
107
|
+
```Kotlin
|
108
|
+
// これの型は (Genotype<BitGene>) -> Int
|
109
|
+
val hoge = { gf: Genotype<BitGene> -> eval(gf) }
|
110
|
+
|
111
|
+
// これの型は java.util.function.Function<Genotype<GitGene>, Int>
|
112
|
+
val piyo = java.util.function.Function { gf: Genotype<BitGene> -> eval(gf) }
|
113
|
+
```
|
114
|
+
|
115
|
+
**※「追記1」「追記2」で記載した org.jetbrains.kotlin.codegen.CompilationException が発生してしまう理由はまだわかっていません。**
|
10
追記2を追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -84,4 +84,14 @@
|
|
84
84
|
・
|
85
85
|
・
|
86
86
|
|
87
|
+
```
|
88
|
+
|
89
|
+
---
|
90
|
+
|
91
|
+
■追記2
|
92
|
+
「3.」の部分を以下のように書き換えたところ、追記1と同じ現象になってしましました。
|
93
|
+
|
94
|
+
```Kotlin
|
95
|
+
val func: java.util.function.Function<Genotype<BitGene>, Int> = java.util.function.Function { gf: Genotype<BitGene> -> eval(gf) }
|
96
|
+
val engine = Engine.builder(func, gtf).build()
|
87
97
|
```
|
9
表記崩れを修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -52,6 +52,7 @@
|
|
52
52
|
Java: 1.8.0_152-release-1136-b27 amd64
|
53
53
|
IntelliJ IDEA 2018.1.1 (Ultimate Edition) Build #IU-181.4445.78
|
54
54
|
Windows10
|
55
|
+
|
55
56
|
---
|
56
57
|
|
57
58
|
■追記1
|
8
コード「4.」を追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -19,6 +19,13 @@
|
|
19
19
|
// 3.) Create the execution environment.
|
20
20
|
val engine = Engine.builder({ gf: Genotype<BitGene> -> eval(gf) }, gtf)
|
21
21
|
|
22
|
+
// 4.) Start the execution (evolution) and collect the result.
|
23
|
+
val result = engine.stream()
|
24
|
+
.limit(100)
|
25
|
+
.collect(EvolutionResult.toBestGenotype())
|
26
|
+
|
27
|
+
println("Hello World:\n$result")
|
28
|
+
|
22
29
|
}
|
23
30
|
```
|
24
31
|
|
7
Windowsであることを追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -44,7 +44,7 @@
|
|
44
44
|
Kotlin: 1.2.31
|
45
45
|
Java: 1.8.0_152-release-1136-b27 amd64
|
46
46
|
IntelliJ IDEA 2018.1.1 (Ultimate Edition) Build #IU-181.4445.78
|
47
|
-
|
47
|
+
Windows10
|
48
48
|
---
|
49
49
|
|
50
50
|
■追記1
|
6
エラー全文を記載
title
CHANGED
File without changes
|
body
CHANGED
@@ -54,4 +54,26 @@
|
|
54
54
|
val engine = Engine.builder(java.util.function.Function { gf: Genotype<BitGene> -> eval(gf) }, gtf).build()
|
55
55
|
```
|
56
56
|
|
57
|
-

|
57
|
+

|
58
|
+
|
59
|
+
```
|
60
|
+
Error:Kotlin: [Internal Error] org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: Error type encountered: (???..???) (FlexibleTypeImpl).
|
61
|
+
Cause: Error type encountered: (???..???) (FlexibleTypeImpl).
|
62
|
+
File being compiled at position: (26,3) in C:/Users/mosa/Documents/hoge/src/main/java/HelloWorld.kt
|
63
|
+
The root cause was thrown at: KotlinTypeMapper.java:122
|
64
|
+
at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:328)
|
65
|
+
at org.jetbrains.kotlin.codegen.ExpressionCodegen.genStatement(ExpressionCodegen.java:372)
|
66
|
+
at org.jetbrains.kotlin.codegen.ExpressionCodegen.generateBlock(ExpressionCodegen.java:1193)
|
67
|
+
at org.jetbrains.kotlin.codegen.ExpressionCodegen.generateBlock(ExpressionCodegen.java:1138)
|
68
|
+
at org.jetbrains.kotlin.codegen.CodegenStatementVisitor.visitBlockExpression(CodegenStatementVisitor.java:56)
|
69
|
+
at org.jetbrains.kotlin.codegen.CodegenStatementVisitor.visitBlockExpression(CodegenStatementVisitor.java:22)
|
70
|
+
at org.jetbrains.kotlin.psi.KtBlockExpression.accept(KtBlockExpression.java:44)
|
71
|
+
at org.jetbrains.kotlin.codegen.ExpressionCodegen.genQualified(ExpressionCodegen.java:307)
|
72
|
+
at org.jetbrains.kotlin.codegen.ExpressionCodegen.genStatement(ExpressionCodegen.java:372)
|
73
|
+
at org.jetbrains.kotlin.codegen.ExpressionCodegen.gen(ExpressionCodegen.java:338)
|
74
|
+
at org.jetbrains.kotlin.codegen.ExpressionCodegen.returnExpression(ExpressionCodegen.java:1590)
|
75
|
+
・
|
76
|
+
・
|
77
|
+
・
|
78
|
+
|
79
|
+
```
|
5
バージョン表記を詳細にしました
title
CHANGED
File without changes
|
body
CHANGED
@@ -41,8 +41,9 @@
|
|
41
41
|
ちょっと複雑なので混乱しているだけかもしれません。
|
42
42
|
ご教示いただければと思います。
|
43
43
|
|
44
|
-
Kotlin 1.2.31
|
44
|
+
Kotlin: 1.2.31
|
45
|
-
Java 1.8.
|
45
|
+
Java: 1.8.0_152-release-1136-b27 amd64
|
46
|
+
IntelliJ IDEA 2018.1.1 (Ultimate Edition) Build #IU-181.4445.78
|
46
47
|
|
47
48
|
---
|
48
49
|
|
4
バージョンを追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -41,6 +41,9 @@
|
|
41
41
|
ちょっと複雑なので混乱しているだけかもしれません。
|
42
42
|
ご教示いただければと思います。
|
43
43
|
|
44
|
+
Kotlin 1.2.31
|
45
|
+
Java 1.8.0
|
46
|
+
|
44
47
|
---
|
45
48
|
|
46
49
|
■追記1
|
3
微修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -45,6 +45,9 @@
|
|
45
45
|
|
46
46
|
■追記1
|
47
47
|
「3.」の部分を以下のように書き換えたところ、コンパイルは通ったのですが、実行時に以下のエラーとなってしまいました。
|
48
|
+
|
49
|
+
```Kotlin
|
48
50
|
val engine = Engine.builder(java.util.function.Function { gf: Genotype<BitGene> -> eval(gf) }, gtf).build()
|
51
|
+
```
|
49
52
|
|
50
53
|

|
2
追記1
title
CHANGED
File without changes
|
body
CHANGED
@@ -39,4 +39,12 @@
|
|
39
39
|
|
40
40
|
JavaとKotlinのジェネリクス変性について私が理解できていないか、理解が誤っていることが原因だと思うのですが、何が問題でどう記述するのが正しいかがわかりません。
|
41
41
|
ちょっと複雑なので混乱しているだけかもしれません。
|
42
|
-
ご教示いただければと思います。
|
42
|
+
ご教示いただければと思います。
|
43
|
+
|
44
|
+
---
|
45
|
+
|
46
|
+
■追記1
|
47
|
+
「3.」の部分を以下のように書き換えたところ、コンパイルは通ったのですが、実行時に以下のエラーとなってしまいました。
|
48
|
+
val engine = Engine.builder(java.util.function.Function { gf: Genotype<BitGene> -> eval(gf) }, gtf).build()
|
49
|
+
|
50
|
+

|
1
エラーの図をつけ忘れていたので追記しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -25,6 +25,7 @@
|
|
25
25
|
「3.」のEngine.builder の部分でコンパイルエラーとなっています。
|
26
26
|
エラーは以下の通りです。
|
27
27
|
|
28
|
+

|
28
29
|
|
29
30
|
呼び出し先のメソッドの定義は以下の通りです。
|
30
31
|
|