AI Module API

classic Classic list List threaded Threaded
22 messages Options
12
Reply | Threaded
Open this post in threaded view
|

AI Module API

Rooser
Came upon tripleA a few days ago. It's been a treat.

I've taken a shine to the alternate scenarios available, particularly 270 BC - but therein lies the problem.

The default AIs are pretty terrible at playing alternate scenarios, especially if those scenarios completely overhaul all the units. In the case of 270 BC the "stronger" AI does patently nonsensical things, like only attack with cavalry, and Carthage just lumps all its units on this one particular territory that is of little strategic value.

I'm assuming this is because the AIs are still playing WWII while I'm playing Roman Conquest.

I'm rather experienced with Java, so I want to make my own AI algorithms, but there doesn't seem to be a particularly modular way to do this. I've looked at the source and found the files that represent the two AIs, so in theory I could rewrite those directly, but I think there should be a better way, (Maybe there is and I'm just missing it).

So my request is two-fold:

1) With the code the way it is now, what is the cleanest way to go about adding new AIs to that drop down list? I've already done some digging, but I'd prefer to write a new AI class and just get it added to the list. Little help navigating the code?

2) (And this one is something I'm willing to code myself) Since this is Java, and Java is awesome, can't you use a class loader to load up AI class files that might be sitting in a different directory and not the .jar file? This way if I, say, wrote a really good 270 BC AI and wanted to make it available right here on the Internets, it'd be nice if non-programmers could just throw the .class files into some appropriate directory and *BAMF!* they'd be available to use.

It's great that you have a good map-making community, but I'd be thrilled if we could get an AI community going too, and it would be especially nice for the non-standard mods if they could come with AIs tailored to their scenarios.

Again, I'm willing to help do the work on this one.
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

Veqryn
Administrator
Hi, I host all the maps currently on my sourceforge page, so i can edit them if need be.
If you do come up with some sort of ai, and that ai can read a specific file in the map folder using hooks, then i'd be happy to maps that are changed to have files added.  An alternative is to have the ai look somewhere else outside of the map zip, but that might have its own problems.
Please contribute to the TripleA 2013 donation drive:
http://tripleadev.1671093.n2.nabble.com/2013-TripleA-Donation-Drive-tp7583455.html
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

RogerCooper
In reply to this post by Rooser
I think it is better idea to try to write a general AI, than scenario specific ones. There is no inherent reason that you would need different code for different games. For example, Zillions of Games uses the exact same AI to play a huge variety of boardgames.

In A&A, it might be better approach to write AI for general approaches.A "Land AI" would be different from a "Sea AI" but could still be used in many scenarios.

The problems you see with the Moore AI are caused by AI glitches, not the differences between 270BC in WW2. Fix them for 270BC, and it will play WW2 better as well.

One particularly annoying glitch involves the easy AI getting stuck when dealing with static combat units. If this was fixed, the easy AI could be used for many more scenarios. When dealing with large maps, the easy AI's faster processing makes it the best choice.
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

Rooser
It's fine if I'm incorrect about AI performance depending on scenarios, but that doesn't change the fact that it'd be nice to have new AIs install as easily as new maps. Installing a new map is as simple as throwing the files in the right directory. AIs could be the same way. I understand that AI modding would never be as popular as map modding, (nor should it be), but that doesn't mean there wouldn't be a dedicated few of us.

I also would like to repeat my request to the devs to explain where in the source code the AI classes are actually instantiated, (Not the AI classes themselves, I found those). But I guess I'm capable of finding it on my own.

Or all of this put another way: To those of you who are working on your own AI, what approach are you taking to actually get the program to run your AI? Are you just rewriting EZ or Moore and recompiling? Because I could do that right now, but it feels the whole process should be more object oriented. It'd be nice to just extend/implement the classes and interfaces that I need to and add to the list of AIs instead of having to overwrite them. I mean, the whole darned point of Java is that it can be neat and modular like this, (And cross-platform - that's cool too :) )
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

Rooser
Update:  Okay so I found where the AIs get instantiated. I'm new and have like zero cred here and totally run the risk of sounding arrogant, (if I haven't already done so), but ... the code there is a little kludgy, very hard-coded and extremely mod-unfriendly.

I would like to help.

You can respond via e-mail at this point, but are you interested in me contributing code that could load external AIs? I don't think it would be very hard, I'd love to do it, and it basically doesn't interfere with any other development you are currently doing.
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

Wisconsin
In reply to this post by Rooser
I was experimenting with my own AI before, so I can help you with some of this.

Here is where the AI classes are instantiated: games.strategy.triplea.TripleA.java

The createPlayer method in the class creates all the player classes. It finds what type of player it is using the value obtained from the playerNames map. After the players are created, it runs the startGame method with the players as a parameter.

To implement your own AI, you have to create a display and identification name by adding a line like this above the createPlayers method:

private static final String CUSTOM_COMPUTER_PLAYER_TYPE = "Custom AI";

Then add the instantiation code for the custom AI class to the createPlayers method.

Then there are a few other things you have to do, and it's ready to go!

Let me know if you need more info,
    Wisconsin
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

Wisconsin
In reply to this post by Rooser
Never mind, I see you already figured that out... :)
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

Rooser
In reply to this post by Veqryn
Ugh. Sorry to triple-post.

@Verqyn - What I was envisioning was just a change that would allow newly installed AI modules to just be added to that drop-down menu when you choose who plays what faction. It really wouldn't require any maps to change. If someone wrote a scenario-specific AI, its up to the player to select it where appropriate from the menu. The mapmaker shouldn't have to worry about it.
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

Rooser
@Wisconsin: Hey thanks anyway, I didn't realize that the TripleA class handled the menu's list as well, which at least makes internal testing and development pretty simple.

But I'm sure there are some non-programmer's out there who would like to play against other people's custom AI. It'd be neat if it could be as simple as just, "Throw this folder into that folder."
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

Sean Bridges
Administrator
In reply to this post by Rooser
Rooser wrote
Update:  Okay so I found where the AIs get instantiated. I'm new and have like zero cred here and totally run the risk of sounding arrogant, (if I haven't already done so), but ... the code there is a little kludgy, very hard-coded and extremely mod-unfriendly.

I would like to help.

You can respond via e-mail at this point, but are you interested in me contributing code that could load external AIs? I don't think it would be very hard, I'd love to do it, and it basically doesn't interfere with any other development you are currently doing.
The biggest concern with loading an ai from a mod is security.  Currently all that can be loaded from a mod is images.  When a mod can include arbitrary code, we need to lock the code down so that it can't do something it shouldn't.  

This could be done with a security manager, but I'm not very familiar with how those work.

Loading .class files from a mod would not be very hard.  All mod files are currently loaded using classloaders (look at ResourceLoader), which allows us to do thing like loading mods from zips, and having mods depend on other modes.

At this point, I think it would be more useful to work on general AI's, rather than mod specific ones.
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

ubernaut
i always thought the idea of different personalities might be interesting (in a sense there's already 2) might also make for better AI performance since different AI behavioral traits would lend themselves to certain situations. for example a "stalin-like" like personality might emphasize infantry production or something along those lines.
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

Rooser
In reply to this post by Sean Bridges
First of all, I just want to make it clear that I respect that this is a very complex project that has taken years of work from numerous people and that most of the code is the way it is for a reason. I'm prefacing this post this way because I just want to acknowledge that I'm basically just some guy who came out of the blue and started making demands.

But I don't think your security concerns matter. So a programmer who knows the source code well can make an obnoxious AI that cheats, makes units vanish and appear, and then crashes the game when its about to lose. Or maybe it ignores the game-paradigm entirely and opens a new JPanel with pornography on it. Yeah. So? Who on earth would play with it then if it did that stuff?

I could also make a really terrible map with poorly designed units, and awful game balance. And nobody would like it. And it wouldn't get played.

You already have a really great community host that only distributes non-crap mods. AIs that act like big jerks will simply never have an audience. You say "security," but its not like they could swipe my credit card number or anything. If the only threat is that they might start running code that is annoying, its pretty easy to just not use it.
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

Wisconsin
If TripleA allows code from other people to be loaded and run from within TripleA, it could also be used to spread viruses or run malicious code.

Once code has begun to run from a remote source, it can be used to perform many harmful functions. There are hundreds of things that could be done to harm a computer, including the deletion of files or the sending of private information.

Although the possibility of these things are very slim, it is still a big concern. If anything like that did happen, it could severely ruin the TripleA project in multiple ways.

I agree that custom AI's would be really nice to have, but until we're sure it's safe, the risks outweigh the benefits, at least in my opinion.

Thanks for listening,
    Wisconsin
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

ComradeKev
Administrator
In reply to this post by Rooser
I hadn't had a chance to reply yet, but I'm happy to have extra help in development.  I've been working in the code for about two years. If you need direction or have questions, feel free to post or email me directly. If you deal with any of the networking code, I'll direct you to contact sgb directly, but he's also pretty active on the boards. It looks like you have what you need for now.  Let me know if you need anything.
 
If emailing me at ComradeKev at yahoo.com , please add TripleA to the subject line
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

RogerCooper
In reply to this post by ubernaut
I had some interesting (and easy to implement) ideas for AI personalties:

Land AI: Just attacks by land, not building or moving naval units. Should run faster.
Sea AI: Builds naval units and uses them extensively
Static AI: Just builds and doesn't move. Great for running powers which have not yet entered the war.
Do Nothing AI: Just sits and does nothing. For minor powers in too big scenarios.
Zombie AI: Randomly attacks the nearest enemy-controlled area, ignoring numbers
Cautious AI: Requires a high superiority for attack. Builds lots of defensive units.
Aggressive AI: Reverse of above.
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

Rooser
I agree with the security thing on second thought. If you feel a Security Manager, (also not something I'm familiar with), is necessary, and I'm beginning to agree with you that it is, I'm willing to look into that if it means getting this feature added.

I mean, yeah, the odds of a malicious AI getting wide distribution among the player base is very very slim, but it'd probably be better for the reputation of the game to just never allow it to even be possible. It'd be annoying if all AI mods were "use at your own risk."

Not sure exactly how it works but probably the best place to start would just be to disallow any reading or writing from files, and to disallow all System calls. Machine learning enthusiasts might groan at the inability to keep local data files, but for a game like this it's probably overkill anyway and its a sure-fire way to prevent the installation of malware. Preventing System calls prevents the running of any code outside the JRE.
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

Veqryn
Administrator
In reply to this post by RogerCooper
instead of building entire AI's, why not focus on modules?

like, how about a really good AI bidding module?

and a really good "when to retreat from battle" module?

and a good "where to land your planes after combat" module?

a "which direction(s) should i attack or defend in this turn module?"

that would be ideal, because when something is a smaller chunk, more people can take a look at it and analyze it for errors (logical and/or bugs).  
Please contribute to the TripleA 2013 donation drive:
http://tripleadev.1671093.n2.nabble.com/2013-TripleA-Donation-Drive-tp7583455.html
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

KMoore
In reply to this post by Rooser
Rooser...not sure about an API, but if you want to work another AI, the best starting point is the WeakAI. I'm not sure if you want to have a ton of different AIs, i.e. landAI, seaAI, etc. Probably 3 choices for AI would be good.

I have made a lot of changes to the Moore AI over the past 3 weeks. I have not posted them yet because I'm working out loading AI Targets which could be included in each map. This just lets an individual map have specified key targets for the AI. It does not force the AI to use them, but on land, it increases the AI probability of moving toward it. Also, the AI was ignoring certain well fortified neutrals and when I read over "recommended" strategies, it specified to take them (i.e. Italy take Greece in NWO). Without getting too long winded, the AI will be able to read a file which has some targets laid out for it. This impacts the Amphibious part of the AI significantly and the land moderately. Even without it, I've made a boatload of improvements to the AI in general, including better purchasing.

This is my first time on the forums in about 4 months. I just haven't had anytime to work on the program until the last few weeks, so I haven't followed the threads related to the AI for a while now.
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

Daral
In reply to this post by Veqryn
Pretty simple reason.  You cannot effectively build a strategy for a single phase in isolation from the other phases.  For instance, you could have a bidding phase that (accurately) decides to buy tons of planes or ships, but if you attack/move phases suck at using those units, or use them in contradictory ways, then you will have inconsistent and ineffective play.

Your suggestion isn't impossible, but ultimately it does require coordination between different phases to build an effective strategy.


@Rooser I'm another programmer-commentator (i.e. I don't do work but I'm happy to share my opinion ;)), and from digging in the code I can say that writing a new AI is a really tough project because the game interface is very complex.  Writing even a simple AI (such as Weak AI) is very difficult.  Writing a good AI (something like alpha-beta* with a solid heuristic) would be at least 30-50 hours of work for developer new to TripleA.

Of course, trying to simplify the interface would essentially mean rewriting the whole game engine, and while that might be a good long-term goal, it's not going to happen anytime this year (or next probably).

As for writing a general AI vs. map specific, probably the best compromise is to build a general AI that is parameterizable and you configure the parameters for each specific map.  This is the most tenable long-term approach (because maps are added or change often), but designing a solid configurable AI is a ton of work.  Of course, it can be done, but you should just know up front that it's a lot of work.

The current AIs are basically just knowledge systems.  If you want to build anything from formal AI (such as a search algorithm), then there are a couple major hurdles you would have to overcome, but it all depends on what approach you want to take.
Reply | Threaded
Open this post in threaded view
|

Re: AI Module API

erik542
Well modular AI gives us the opportunity for the AI to behave differently in different parts of the map. For example, we don't want the AI to behave the same way with german troops in europe as with british troops in the middle east. Those regions have to completely different play styles. Also allows the AI to treat the german navy different from the american navy.
12