使用 jenetics 遗传算法库,但我几乎可以肯定它与问题无关。似乎 Java 不理解 Java 8 中引入的双冒号表达式。Java 8 已安装并且 Gradle 正在使用它。
由于以下类中的双冒号表达式,构建不成功:
package gen;
import org.jenetics.BitChromosome;
import org.jenetics.BitGene;
import org.jenetics.Genotype;
import org.jenetics.engine.Engine;
import org.jenetics.engine.EvolutionResult;
public class HelloWorld {
// 2.) Definition of the fitness function.
private static Integer eval(Genotype<BitGene> gt) {
return ((BitChromosome)gt.getChromosome()).bitCount();
}
public static void main(String[] args) {
// 1.) Define the genotype (factory) suitable
// for the problem.
Factory<Genotype<BitGene>> gtf = Genotype.of(BitChromosome.of(10, 0.5));
// 3.) Create the execution environment.
Engine<BitGene, Integer> engine = Engine.builder(HelloWorld::eval, gtf).build();
// 4.) Start the execution (evolution) and
// collect the result.
// Genotype<BitGene> result = engine.stream().limit(100).collect(EvolutionResult.toBestGenotype());
// System.out.println("Hello World:\n" + result);
}
}
给出 Gradle 输出:
russell@Ubuntu-LTS:~/workspace/react_gen $ ./gradlew build
:compileJava
/home/russell/workspace/react_gen/src/main/java/gen/HelloWorld.java:21: error: ')' expected
Engine<BitGene, Integer> engine = Engine.builder(HelloWorld::main, gtf).build();
^
/home/russell/workspace/react_gen/src/main/java/gen/HelloWorld.java:21: error: illegal start of expression
Engine<BitGene, Integer> engine = Engine.builder(HelloWorld::main, gtf).build();
^
/home/russell/workspace/react_gen/src/main/java/gen/HelloWorld.java:21: error: ';' expected
Engine<BitGene, Integer> engine = Engine.builder(HelloWorld::main, gtf).build();
^
3 errors
:compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 3.077 secs