jenetics icon indicating copy to clipboard operation
jenetics copied to clipboard

Using Jenetics with GraalVM

Open jenetics opened this issue 6 years ago • 4 comments

Make some GA example with Jenetics running with GraalVM and describe how to do so.

jenetics avatar Jun 24 '19 19:06 jenetics

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 👍

xtagon avatar Jun 02 '24 00:06 xtagon

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

jenetics avatar Jun 03 '24 06:06 jenetics

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 main at 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 main with the example's main and 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.

xtagon avatar Jun 08 '24 00:06 xtagon

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>

xtagon avatar Jun 08 '24 18:06 xtagon