Ludii Forum
programing get score from heuristics - Printable Version

+- Ludii Forum (
+-- Forum: Questions (
+--- Forum: About Ludii (
+--- Thread: programing get score from heuristics (/showthread.php?tid=721)

programing get score from heuristics - RocWang - 09-24-2021

I want to get the score after each move in the Ludi AI, and I try below code (study from your source code of ,however ,when I move a piece of Town in the first step  error comes as below:

        Heuristics heuristicValueFunction = null;
        final aiMetadata = game.metadata().ai();
        if (aiMetadata != null && aiMetadata.heuristics() != null) {
            heuristicValueFunction = Heuristics.copy(aiMetadata.heuristics());

        final Context copyContext = copyContext(context);
        final Move m = currentRootMoves.get(0);
        game.apply(copyContext, m);

        final int maximisingPlayer = context.state().playerToAgent(context.state().mover());
        final int numPlayers = game.players().count();
        for (int j = 0; j < numPlayers; j++) {
                float heuristicScore = heuristicValueFunction.computeValue(copyContext, j, -100.f);
Exception in thread "Thread-4" java.lang.NullPointerException: Cannot invoke "main.collections.FVector.get(int)"
because "this.pieceWeights" is null
    at CannonAI.selectAction(
    at other.ThinkingThread$
    at java.base/

        (board Style chess)
        (bestAgent "Alpha-Beta")
        (heuristics {
            (material transformation:(divNumInitPlacement) pieceWeights:{
                (pair "Pawn1" 1.0)
                (pair "Pawn2" 1.0)
                (pair "Town1" 5.0)
                (pair "Town2" 5.0)

Could you tell me how to get the score after each step?

RE: programing get score from heuristics - DennisSoemers - 09-24-2021

The Heuristics class has, very similar to the abstract AI class, an (EDIT:) "init(game)" "initAI(Game)" method. If you're loading and using your AI inside the GUI, we already make sure to call initAI() on your AI class for you whenever necessary, but we can't know about anything else that might require initialising --- like heuristics --- inside of your own custom AI class, so you'll have to ensure to call that at least once before trying to use it. Doing that should solve the NullPointerException you're getting.

Aside from that, your loop through the player indices would likely cause issues because you're starting from index j = 0, but in Ludii we have a convention of starting to count at 1 for anything related to Player indices (such that Player 1 has index 1, Player 2 index 2, etc.). So you'd likely want to change the loop to be more like:

" for (int j = 1; j <= numPlayers; j++) "

i.e. start counting at 1, and keep going up to and INCLUDING numPlayers.

As a more general note on efficiency, loading heuristics from metadata like that --- and especially initialising it when you also do that to fix the bug --- can be kind of expensive, and is only actually required once per game. So I would recommend moving that into an initAI() method for your AI class (maybe it's already there, can't tell from your code snippet).