Ludii Forum
Game specific heuristics - Printable Version

+- Ludii Forum (https://ludii.games/forums)
+-- Forum: Questions (https://ludii.games/forums/forumdisplay.php?fid=13)
+--- Forum: About Ludii (https://ludii.games/forums/forumdisplay.php?fid=14)
+--- Thread: Game specific heuristics (/showthread.php?tid=428)

Pages: 1 2


Game specific heuristics - Michael - 02-10-2021

Is there a way to tell the AI how to play a particular game better? And what kinds of skills do I need to do this?


RE: Game specific heuristics - Michael - 02-10-2021

I see that there is a section on this in the LLR, but … I don't understand any of it, I'm afraid. Could I get an example of how to tell the AI to value occupying the corners higher than any other sites?


RE: Game specific heuristics - DennisSoemers - 02-10-2021

Chapter 19 of the Ludii Language Reference contains some information on AI-related metadata that can be entered in the metadata section of your .lud description, and some AIs can then make use of that metadata. Alpha-Beta is probably the easiest AI to improve through manually-specified metadata; more specifically, it will use any heuristics specified in the metadata (see Section 19.3).

For Ludii's built-in games, we generally move AI metadata out of the .lud files because it can get a bit ugly and verbose, and into separate .def files which can be found in /def_ai/. For custom games outside the JAR, Ludii wouldn't be able to find where such external files are, but you can still just write the metadata directly inside the .lud file as you would for any other metadata. For instance, the metadata for chess would look something like this (excluding everything except for the AI metadata):


Code:
(metadata
    (ai
    (bestAgent "Alpha-Beta")
    (heuristics {
        (material transformation:(divNumInitPlacement) pieceWeights:{
            (pair "Pawn1" 1.0)
            (pair "Pawn2" 1.0)
            (pair "Rook1" 5.0)
            (pair "Rook2" 5.0)
            (pair "Bishop1" 3.0)
            (pair "Bishop2" 3.0)
            (pair "Knight1" 3.0)
            (pair "Knight2" 3.0)
            (pair "Queen1" 9.0)
            (pair "Queen2" 9.0)
        })
    })
    )
   
)

The (bestAgent "Alpha-Beta") thing lets Ludii know that for this particular game, Alpha-Beta is the best algorithm we currently have available, and this is then the algorithm that is automatically selected if you choose for "Ludii AI". The rest are heuristics; in this case just some very simple weights for different piece types.

Aside from heuristics, it is also possible to add geometric patterns / "features" in the metadata, which can then be used by the Biased MCTS algorithm, but working manually with those (as opposed to automatically training them) is probably quite a bit more complicated.


RE: Game specific heuristics - DennisSoemers - 02-10-2021

(02-10-2021, 07:24 PM)Michael Wrote: I see that there is a section on this in the LLR, but … I don't understand any of it, I'm afraid. Could I get an example of how to tell the AI to value occupying the corners higher than any other sites?

Something like the following metadata code would encourage Alpha-Beta agents to move more towards corners:

Code:
(metadata
    (ai
        (heuristics
            (cornerProximity)
        )
    )
)

Note that this would not solely encourage occupying corners; it would encourage anything that's closer to a corner over anything that's further away from all corners. But, of course, the corners themselves are the closest possible, so those are still the most encouraged sites.


RE: Game specific heuristics - Michael - 02-10-2021

Thank you, Dennis! I need to try that very simple line of code.

By the way, how is proximity to corners measured? What I'm after is a way to prioritize the highest possible straight line distance from the center intersection of the board. Is that the same?

Haha. I tried adding the corner proximity term in a game where the corners are very important, but the AI still has no clue. On the second player's second turn, the agent just walked away form the corner. Of all the possible moves, they made the one that lost them the most important spot on the board.


RE: Game specific heuristics - DennisSoemers - 02-10-2021

(02-10-2021, 08:07 PM)Michael Wrote: By the way, how is proximity to corners measured? What I'm after is a way to prioritize the highest possible straight line distance from the center intersection of the board. Is that the same?

I'm not 100% sure off the top of my mind what the exact calculation was. It's not going to be exactly straight-line distance, but close enough (assuming a regularly-tiled board). The distance computation will be based on either orthogonal steps only (which would mean a distance of 2 for a diagonal step on a chessboard), or orthogonal + diagonal steps (which would mean a distance of just 1 for a diagonal step on a chessboard), whereas straight-line distance would be somewhere in between (with a distance of about 1.4 for a diagonal step on a chessboard). And then proximity is kind of just the inverse of distance.

Proximity to corners is not necessarily the same as distance from centre though. On many boards it will be, but you could imagine some highly irregular shape where there are some corners close to the centre. If you really want distance to centre, you can do that with the "centreProximity" heuristic and a negative weight (in the example in my previous post, for cornerProximity, I did not specify a weight, and that just results in a default weight of positive 1.0). For example, something like:

Code:
(centreProximity weight:-1.0)

---

(02-10-2021, 08:07 PM)Michael Wrote: Haha. I tried adding the corner proximity term in a game where the corners are very important, but the AI still has no clue. On the second players second turn, the agent just walked away form the corner. Of all the possible moves, they made the one that lost them the most important spot on the board.

Which AI was playing? Some AIs, like UCT, do not make use of the heuristics. If you want an algorithm that actually uses the heuristics, make sure to select AlphaBeta. To ensure that this one is automatically picked for Ludii AI, make sure to also include something like this in your AI metadata (as shown by the Chess example):

Code:
(bestAgent "Alpha-Beta")



RE: Game specific heuristics - Michael - 02-10-2021

Thanks. It was Alpha-Beta. I'll try the negative weight thing to compare. Maybe I should use both. When center value is set to -1, does it make a difference whether corner value is 1 or more than 1?


RE: Game specific heuristics - DennisSoemers - 02-10-2021

(02-10-2021, 08:34 PM)Michael Wrote: Thanks. It was Alpha-Beta. I'll try the negative weight thing to compare. Maybe I should use both. When center value is set to -1, does it make a difference whether corner value is 1 or more than 1?

Err that depends on the board. If you just have a standard square board (like in chess or Go), I think those two heuristics are actually "symmetric", and I would expect (cornerProximity weight:1.0) to have exactly the same behaviour as (centreProximity weight:-1.0). So, combining those two together like that would probably also be the same as just one of them with a weight of 2.0 or -2.0, respectively. And since they're giving the same advice anyway, it wouldn't really matter which magnitudes you use for the weights, only the sign matters. The relative magnitudes only start mattering when you also involve other heuristics, like (score) or (material) or whatever, anything that actually gives different advice.

If you have an asymmetric board it can be very different though.


RE: Game specific heuristics - Michael - 02-11-2021

I tried negative weight for center proximity, and the first player's first move was to give up a corner. This can't be right. It simply doesn't work. That was Alpha-Beta again. It only had 10 seconds to think, but I can't see how that can matter.


RE: Game specific heuristics - Michael - 02-11-2021

Also: I tried typing "(centreProximity weight:-1.0)" into the evaluate heristic box, and nothing happened when I clicked OK. The box was closed, but no text appeared in the status tab or in the analysis tab.