Ludeme / LudiiExampleAI

Project with examples for the implementation of third-party AI algorithms / agents for the Ludii general game system.
MIT License
26 stars 15 forks source link

RandomAI compilation and loading in Ludii #2

Closed n-ai-up8-edu closed 4 years ago

n-ai-up8-edu commented 4 years ago

Hi,

I wrote (mostly copied from ludii doc) and compiled the following RandomAI player. I compiled it with "javac -cp Ludii.jar RandomAI.java" I made a jar file with "jar cf RandomAI.jar RandomAI.class"

When I tried to load this player in ludii gui, it says : "Could not find any AI classes."

what am I supposed to do ?

Thx in advance

/////////////////////////
package random;

import java.util.concurrent.ThreadLocalRandom;

import game.Game;
import main.collections.FastArrayList;
import util.AI;
import util.Context;
import util.Move;
import utils.AIUtils;

public class RandomAI extends AI {

    protected int player = -1;

    public RandomAI() {
    this.friendlyName = "Example Random AI";
    }

    @Override
    public Move selectAction (final Game game, final Context context, final double maxSeconds, final int maxIterations, final int maxDepth)
    {
    FastArrayList<Move> legalMoves = game.moves(context).moves();

    if (legalMoves.isEmpty())
        return Game.createPassMove(context);

    if (!game.isAlternatingMoveGame())
        legalMoves = AIUtils.extractMovesForMover(legalMoves, player);

    final int r = ThreadLocalRandom.current().nextInt(legalMoves.size());
    return legalMoves.get(r);
    }

    @Override
    public void initAI(final Game game, final int playerID)
    {
    this.player = playerID;
    }

    @Override
    public boolean supportsGame(final Game game)
    {
    if( game.isStochasticGame( ) )
        return false;
    if ( ! game.isAlternatingMoveGame ( ) )
        return false;
    return true;
    }    
}

/////////////////////////
DennisSoemers commented 4 years ago

I just tried going through the process you described, and it worked correctly. Which version of Ludii.jar are you running? The code listed above works for 0.9.3, but probably not for some older versions. You can check the version through the Help > About menu.

Also: which Operating System are you using?

n-ai-up8-edu commented 4 years ago

Ludii version is 0.9.3 (11/05/2020) OS is Ubuntu 18.04.4 LTS Java is 14.0.1 2020-04-14

DennisSoemers commented 4 years ago

I have just tried again on Ubuntu, and there it works fine on my end too. But, that was with Java 8 instead of Java 14. The Ludii.jar file itself was also compiled for Java 8, so maybe there's too much of a version discrepancy there.

Could you try changing the javac command, and see if one of the following work?

javac --release 8 -cp Ludii.jar RandomAI.java

or

javac -target 1.8 -cp Ludii.jar RandomAI.java

n-ai-up8-edu commented 4 years ago

Using "-target 1.8", it does not produce class file and shows the following message : warning: target release 1.8 conflicts with default source release 14

Using "--realease 8", it works. By the way, javac usage indicates that supported releases are 7, 8, 9, 10, 11, 12, 13, 14. Thus after making a new jar file, loading it gives the same message : "Could not find any AI classes." ?

n-ai-up8-edu commented 4 years ago

Here is my jar file (just copy RandomAI.zip to RandomAI.jar to get the jar file) RandomAI.zip

DennisSoemers commented 4 years ago

Hmm yes if I download your JAR, I get the same issue with that. Even if I extract it, and then pack it in a new JAR again myself, the issue still persists. So it's an issue with the .class file that you have inside the JAR, not an issue with the JAR itself.

This is the .class file that you obtained using --release 8? Then the only solution I can really think of is to additionally install JDK 8 on your machine and really compile with that... a bit annoying, but seems to be necessary due to the dynamic class loading we have to do to make these kinds of things in the GUI work.

For any programmatic experiments with AIs, outside of the GUI, this shouldn't be an issue.

n-ai-up8-edu commented 4 years ago

Yes this .class file has been generated with --release 8.

I tried it with version jdk1.8.0_251 (compile and run).... and it gives the same error :-( (I put this new jar file here again, just in case) RandomAI.zip

DennisSoemers commented 4 years ago

Yep I get the same. That's strange. In the attachment I put the JAR with the class file that I initially produced following your initial report. That one works fine for me. Does it work for you too?

RandomAI.zip

I've looked at the bytecode of these two .class files side-by-side, and they're identical except for:

I can hardly imagine that either of those differences should be relevant... but on the other hand, the bytecode is literally identical barring those differences. Just to be sure, you could try placing your RandomAI class at the top level of a project (not inside any packages) and confirm whether or not that makes a difference?

n-ai-up8-edu commented 4 years ago

Oh, yeah, sorry, that's it. The package directory is missing ! Now it works. It works also with jdk-14.0.1.

After selecting the RandomAI class, a window titled "Select JAR file containing AI." stays open. This windows is empty. Thus I close it. And my Player 1 est bien "From JAR".

If I restart the game, the empty window titled "Select JAR ..." comes back in front of the GUI. So I have to close it again.

After closing the GUI in the middle of a game, when I reopen the GUI, it prints in the terminal many warnings and errors that are " WARNING: Failed to construct AI from JSON: {"AI": {"algorithm": "Example Random AI"}}" "java.lang.NullPointerException at display.views.PlayerViewUser.drawPlayerName(PlayerViewUser.java:269) ..."

Unselecting "From JAR" and reselecting Human player before closing the GUI solves this.

So thanks for the help on finding the error I made in the RandomAI player.

DennisSoemers commented 4 years ago

Yes indeed I did notice some oddities with the filechooser and dialogs like that popping up more often than they should. That's something we should fix for the next version. But good that it appears to work outside of that :)

DennisSoemers commented 4 years ago

The issues with the From JAR filechooser popping up more frequently than it should, and the issues with Ludii not being able to automatically load AIs from JARs when re-launching the application, should both have been resolved by the new 0.9.4 pre-release.