Using Jenetics with GraalVM
Make some GA example with Jenetics running with GraalVM and describe how to do so.
I have been running some of the provided examples as well as custom experiments on GraalVM for version 8.0.0 of Jenetics and haven't run into any problems yet 👍
Great! I haven't had time jet to tryout GraalVM myself. It would be amazing if you could post a short description what you did and how you ran the examples. :)
Regards Franz
In short:
- Installed IntelliJ IDEA (Community Edition)
- Initialized a new Java project in the IDE (When given the option, chose the latest GraalVM as the JDK)
- Wrote my own
mainat first, using the Jenetics quick start tutorial - Used examples such as https://github.com/jenetics/jenetics/blob/master/jenetics.example/src/main/java/io/jenetics/example/GrammaticalScriptEvolution.java as an example, replaced my
mainwith the example'smainand copied over any dependent code
Java isn't my primary language, so I'm sure there's an easier way than by copying, but it was simple and worked.
I haven't tried any GraalVM specific behaviors yet, such as interop. But Jenetics seems to "just work" as is.
Update: I just tried modifying GrammaticalJavaScriptEvolution.java to use GraalVM's polyglot interop instead of the nashorn engine (which isn't available in GraalVM, and potentially no longer available in other JDKs if I recall?)
This basically amounted to replacing the codec with just a String representing the script, and setting up a fitness function like so:
@Override
public Function<String, Double> fitness() {
return script -> {
final Result<Double> result = _sampling.eval(args -> {
try (Context context = Context.create()) {
String source = script.isEmpty() ? "x => NaN" : "x => " + script;
Value function = context.eval("js", source);
assert function.canExecute();
return function.execute(args[0]).asDouble();
}
});
return ERROR.apply(TreeNode.of(), result.calculated(), result.expected());
};
}
Note that this is just experimental and potentially unsafe because it's not using GraalVM's isolate, which is not available on the community edition.
Tested on GraalVM CE 21 with the following dependencies in pom.xml:
<dependencies>
<dependency>
<groupId>io.jenetics</groupId>
<artifactId>jenetics</artifactId>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>io.jenetics</groupId>
<artifactId>jenetics.ext</artifactId>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>io.jenetics</groupId>
<artifactId>jenetics.prog</artifactId>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>io.jenetics</groupId>
<artifactId>jenetics.xml</artifactId>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>polyglot</artifactId>
<version>23.1.1</version>
</dependency>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<!-- Select language: js, ruby, python, java, llvm, wasm, languages-->
<artifactId>js-community</artifactId>
<version>23.1.1</version>
<type>pom</type>
</dependency>
</dependencies>