Tips for AI development for a beginner

Discussion of all aspects of the game engine, including development of new and existing features.

Moderators: Forum Moderators, Developers

Post Reply
Thnick
Posts: 3
Joined: February 23rd, 2019, 7:21 pm

Tips for AI development for a beginner

Post by Thnick » February 27th, 2019, 6:09 pm

To clarify, I’m not a beginner developer, but I’m new to Wesnoth. Some friends and I just came back to Wesnoth after a decade, and we noticed the AI made for a very poor teammate. Since I’ve gotten my BS in Computer Science recently I thought it’d be a fun project to mess around with making my own AI! I’ve read the wiki and some forums, and I can tell I’m far from the most qualified person who’s tried. I’m not anticipating creating the best bot out there; this is mostly for fun and personal development.

Now that that’s out of the way, how do I start? The wiki mostly talks about adding CA’s to the main loop, but I don’t even want to use the main loop. Anyone have some tips on how to make a bot from scratch?

holius
Posts: 26
Joined: May 17th, 2017, 8:49 am
Location: France

Re: Tips for AI development for a beginner

Post by holius » March 1st, 2019, 9:10 am

My first tip would be to try to comply with the AI framework, and not say you don't want to use it. Write a LUA CA, create an AI that has that CA as its only action if you want, but don't try to by-pass the main loop and its complete way of customizing AI.

User avatar
Pentarctagon
Forum Administrator
Posts: 4085
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Tips for AI development for a beginner

Post by Pentarctagon » March 1st, 2019, 9:15 am

To add to what holius said - even if you do eventually create an entirely custom bot, there are few ways to kill off your own enthusiasm by trying to jump straight into the deep end of something very complex.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code

Thnick
Posts: 3
Joined: February 23rd, 2019, 7:21 pm

Re: Tips for AI development for a beginner

Post by Thnick » March 2nd, 2019, 5:04 pm

So my reasoning for not wanting to use the main loop is that I'm not convinced it's actually a good structure, but maybe I'm misunderstanding it. It doesn't look like it makes decisions based on anything other than what's directly in front of it. A pretty standard structure for AI is to compare a ton of game states based off of potential moves not only by looking at the immediate outcomes, but also what the opponent can do in response. And then there are several optimizations that might allow you to look more turns ahead.

From what I've read it seems like the AI picks a move by comparing all the CA's that it can actually perform, and just choosing the one with the highest score. The scores appear to be hard-coded, and I've seen nothing to indicate those scores can change based off of future moves. If this is the case, I think it's worth risking my enthusiasm and trying to find a better way :)

For starters, is there any function hiding out there in the code that returns a game state? Like a 2d array of objects representing the board? Finding that would be a huge step forward for me.

User avatar
Pentarctagon
Forum Administrator
Posts: 4085
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Tips for AI development for a beginner

Post by Pentarctagon » March 2nd, 2019, 5:31 pm

If you have specific questions like that, you can also try asking on Wesnoth's Discord(#development) or IRC(#wesnoth-dev) channel, in case no one replies here.
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code

mattsc
Posts: 1123
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Tips for AI development for a beginner

Post by mattsc » March 2nd, 2019, 7:05 pm

Hi Thnick: What you write is correct for the mainline AI, but only because that's what it does, not because it is a fundamental limitation of the framework. Evaluation scores of the CAs do not have to be hard-coded (some of the Micro AIs use variable scores, for example, although most of that is pretty simplistic). So you could either have a single CA that does all of this internally, or you can have several CAs with adjustable scores that exchange information between them (e.g. via the 'data' variable for LuaAI). I've done both, it works just fine.
Thnick wrote:
March 2nd, 2019, 5:04 pm
For starters, is there any function hiding out there in the code that returns a game state? Like a 2d array of objects representing the board? Finding that would be a huge step forward for me.
Well, depends on what you mean with "game state". You can get all the information about the tiles, units, villages etc. with the 'get_*' functions in LuaWML. I'm assuming here that you want to use Lua, but the same is possible with the other AI languages as well. If you are looking for a "virtual gameboard", no, that does not exist. However, you can modify the real board inside the evaluation functions, as long as you restore it to its original state afterward.

Thnick
Posts: 3
Joined: February 23rd, 2019, 7:21 pm

Re: Tips for AI development for a beginner

Post by Thnick » March 2nd, 2019, 7:43 pm

That's awesome mattsc, those get_* functions would be super useful!

Do you know if anyone's already tried applying a sort of minimax decision tree to CA scores before? Something I can reference maybe?

mattsc
Posts: 1123
Joined: October 13th, 2010, 6:14 pm
Location: Hidden on the hex behind Fred

Re: Tips for AI development for a beginner

Post by mattsc » March 2nd, 2019, 8:03 pm

I am not aware of anybody having done that. In general, the Wesnoth state space is simply too large to make it feasible to look ahead enough for doing a real minmax evaluation of that sort. The closest I am aware of is probably my Fred AI project, but that really only has some very simple aspects of that approach either. (And the code is an absolute mess, esp. at the moment, so you don't really want to look at that. :P )

If you just wanted a super simple example of how to set up a conditional CA score, you could have a look at the evaluation function of the return guardian Micro AI, but I assume that's not what you are asking about.

Shiki
Developer
Posts: 275
Joined: July 13th, 2015, 9:53 pm
Location: Germany

Re: Tips for AI development for a beginner

Post by Shiki » March 3rd, 2019, 8:53 pm

I see one missunderstanding here: Candidate Actions are like modules for the AI, and the AI does first the instructions of the CA with the highest score, then the one with the second highestscore ... their score not about weighting the possibilities of how to do sth. This is a thing which one specific CA does.
E.g. here's the list of the CA, I think they are even ordered by scores. Each one does totally unrelated things.
https://github.com/wesnoth/wesnoth/blob ... lt_rca.cfg

There's also the experimental AI, which has slightly different / more CAs.
https://github.com/wesnoth/wesnoth/blob ... mental.cfg

You could for example set up an AI where you leave out the combat CA, and add your own one.
Or add an CA, which has a higher score than the combat CA, which does some specific combat you want to take care about, and let the combat CA do the rest. (The High XP CA does that: it takes specifically enemies into account, which would level up after 1 or 2 fights)

What I don't know is how the Combat CA works.
The Idea to modify the combat CA is maybe not practical though, because it's written in C++ IIRC. The High XP CA is written in Lua:
https://github.com/wesnoth/wesnoth/blob ... ta/ai/lua/
Introduction to WML filtering and WML variables.

Post Reply