new python AI, needs testing! Current version: 0.4.7

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

Moderators: Forum Moderators, Developers

yobbo
Art Contributor
Posts: 151
Joined: September 16th, 2005, 6:31 am
Location: New Zealand

new python AI, needs testing! Current version: 0.4.7

Post by yobbo »

I've been working on a python AI!

My goal is to make an AI that's more fun to play against. Smarter than the current one, but also less suicidally punishing.

It's at the point where pitting it against the current AI and chesslike (bruteforce.py) isn't really highlighting its vulnerabilities any more. It usually just outlasts and overwhelms them. Personally, I hardly ever play multiplayer so I'm not sure whether some things it does are clever or stupid.

So I need some help testing!

I just need help playing against it and noting specific things it does which are stupid.

It should work fine with the latest development release of wesnoth, 1.3.16.

It's set to print errors and warnings, as well as some general info to the terminal wesnoth is run from, but this can be easily changed at the top of the script.

Current version is 0.4.7, a.k.a. "Onward soldiers! Fear not the enemies' reprisal!"

Changes in this version:
* much more aggressive when less than 50% of villages are held by allies,
* persuade units to spread out at the start
* scouts like venturing out on their own
* more aggressive at favourable times of day, more defensive at
unfavourable times
* dispelled completely unreasonable paralysing fear of ghosts
* fixed backstab? (x fingers)
* fixed healers? (x toes)
* make all units much braver in groups
* fixes for berserker value and danger
* revamped combat scoring and recruitment choices.
* improved village capturing efficiency at start of match

I'm leaving the original version up... nostalgia maybe?
Attachments
tommy-0.4.7.tar.gz
extract to <wesnoth dir>/data/ais
(33.45 KiB) Downloaded 341 times
tommy-0.4b.py.gz
obsolete
(25.74 KiB) Downloaded 322 times
Last edited by yobbo on February 17th, 2008, 2:31 am, edited 7 times in total.

AI
Developer
Posts: 2394
Joined: January 31st, 2008, 8:38 pm

Post by AI »

Another attempt to prove that it is in fact possible to write ugly code in python?

Anyway, it sounds like a good AI, but I'm running 1.2.8 (through distro) and 1.3.15 (in /usr/local) and I don't exactly want to add yet another version.

User avatar
Sapient
Inactive Developer
Posts: 4453
Joined: November 26th, 2005, 7:41 am
Contact:

Post by Sapient »

AI wrote:Another attempt to prove that it is in fact possible to write ugly code in python?

Anyway, it sounds like a good AI, but I'm running 1.2.8 (through distro) and 1.3.15 (in /usr/local) and I don't exactly want to add yet another version.
The code isn't that ugly. It is well commented and uses reasonable variable/function names. However, it does have regions of dead #-comment code and also a clear example of the "god class" anti-pattern.

Thanks for sharing it, yobbo. 8)
http://www.wesnoth.org/wiki/User:Sapient... "Looks like your skills saved us again. Uh, well at least, they saved Soarin's apple pie."

User avatar
Lizard
Posts: 352
Joined: January 19th, 2008, 8:20 am
Location: Hiding in a swamp (gtm +1; DST)

Post by Lizard »

Don't work for me :cry: .
I use the python inbuilt in the windows build, and the AI just say

Code: Select all

<AI> +===============================================+
<AI> | Tommy's Python AI performing moves for team 2 |
<AI> +===============================================+
<AI> ::: Successfully analysed gamemap :::
<AI> 51 critical locations found out of 784 total map tiles, of which 16 are villages
It tells this every round, but neither recruit nor move.
But the brueforce.py works fine.

(DEBUGLEVEL=3 on 4 i don't see the whole text in my 20 chat lines :( )
~ I'll heal you by 4 hp if you post next to me ~
Have a look at the Era of Strife, featuring Eltireans, Eventide, Minotaurs, Saurians and Triththa

yobbo
Art Contributor
Posts: 151
Joined: September 16th, 2005, 6:31 am
Location: New Zealand

Post by yobbo »

Sorry, it'll only work if you have your own SVN build.

Actually I'll just change the title. It'll be ready for testing next dev release. For now it's just more of a milestone for me and a heads up that it exists.


Re: messy code: the easier it is for me to fix... the easier it is to put off fixing it ;).

AI
Developer
Posts: 2394
Joined: January 31st, 2008, 8:38 pm

Post by AI »

True, but the messier it is, the more likely that when we want to change something or understand the code we'll,
A) Clean it up ourselves (cursing you along the way ;))
B) Give up

User avatar
allefant
Units Database Administrator
Posts: 516
Joined: May 6th, 2005, 3:04 pm

Post by allefant »

I just watched two battles between bruteforce.py and tommy-0.4.py - and bruteforce won both times :)

They were both on Basilisk, bruteforce starting, and first Undead vs Dragons then Rebels vs Rebels. Of course, given how Wesnoth is luck based, 2 games statistically tell nothing. But bruteforce, on that map, just seems to hog all villages and therefore in the long run has a big gold advantage. I'll let some more games run, but they take really long.. maybe if it has many units, it can somehow do less checks (as it is already winning anyway).

[edit:]Ran two more games with --nogui, Basilisk, tommy-0.4 starting, Rebels vs Rebels, and it won the first and lost the second. I also looked at the code, seems this is indeed a fun AI:

Code: Select all

elif 'hyperactive' in feelings:
...
elif 'jaded' in feelings:
...
elif 'wary' in feelings:
...
elif 'courageous' in feelings:
...
It even has feelings :)

User avatar
Lizard
Posts: 352
Joined: January 19th, 2008, 8:20 am
Location: Hiding in a swamp (gtm +1; DST)

Post by Lizard »

allefant wrote:I also looked at the code, seems this is indeed a fun AI:
Indeed :lol:

Code: Select all

            text = random.choice([ \
                " tells a bad joke",
                " can't think of anything motivational to say",
                " picks its nose and examines it",
                " coughs and shifts its weight uncomfortably",
                " ponders aloud the meaning of life",
                " gives a stirring speech, " + \
                    "but no one is listening",
                ' suddenly gives a rousing battle cry! "YEAARGH!!!"',
                " looks mildly amused for some reason",
                " inappropriately starts cooking lunch",
                " invites everyone to the pub for a drink"])
            dprint(str(leadername) + text)

Code: Select all

            print "Oh, my beloved %s, " % myname + \
                  "where have you gone?"

Code: Select all

            print '%s screams!' % myname + \
                  ' "YEEAARGHHLLLLLGGHH!!!"'

Code: Select all

              wprint("%s refuses to follow its commander's " % \
                  myname + "barbaric order to attack %s!" % \
~ I'll heal you by 4 hp if you post next to me ~
Have a look at the Era of Strife, featuring Eltireans, Eventide, Minotaurs, Saurians and Triththa

User avatar
allefant
Units Database Administrator
Posts: 516
Joined: May 6th, 2005, 3:04 pm

Post by allefant »

Btw., I made a script to pitch AIs against each others in nogui mode. It just runs wesnoth --nogui, but has easier to remember options I think.

For example:

Code: Select all

./aitest.py -r -a tommy-0.4.py -a default -s Cynsaun_Battlefield -f "Knalgan Alliance"
This will let tommy-0.4 fight against default on Cynsaun_Battlefield, both using Knalgan Alliance. There's also options to list all possible scenarios and factions and ais for those who cannot remember those as well :P

It will still take a long time with this AI though, and the drawback is you don't get to watch it, and Wesnoth currently has no way to save replays for nogui games either.
Attachments
aitest.py.txt
(4.38 KiB) Downloaded 260 times

yobbo
Art Contributor
Posts: 151
Joined: September 16th, 2005, 6:31 am
Location: New Zealand

Post by yobbo »

Losing to bruteforce on Basilisk? I must've broken something :oops:

I'll check the village protection code, something there will be amiss.

Thanks :)


And yeah - it's not optimised much yet, so it does hordes of checks it doesn't need to.

When the leader has orders for a unit, the unit only checks the order vs. not moving at all. If the order's not more than 2 times worse (or the unit is undead) it will follow the order without checking all its other possible moves.

Eventually I'll have the leader do a lot more ordering, but right now it just orders group attacks on single enemy units if it sees the opportunity :).

I'm trying to build up good instincts for my units!


EDIT: ooooopppsss
Uploaded a new version with village protection fixed

yobbo
Art Contributor
Posts: 151
Joined: September 16th, 2005, 6:31 am
Location: New Zealand

Post by yobbo »

I've reached another stopping place so here's a new version.

Most obvious change: it's about 10 times faster.

It's not quite as clever as 0.4b though :(. It's just not aggressive enough on big maps.

http://www.wesnoth.org/forum/download.php?id=20272

ghovs
Posts: 2
Joined: February 5th, 2008, 8:11 pm

Post by ghovs »

Thanks for sharing! It's nice to see more AI variation.

yobbo
Art Contributor
Posts: 151
Joined: September 16th, 2005, 6:31 am
Location: New Zealand

Post by yobbo »

New version 0.4.7

This version seems to be fairly stable. Wins against bruteforce.py about 80% of the time. I haven't tested it much against the default AI because it's a bit too easy to exploit. I'm still only doing multiplayer testing at the moment, but would welcome any feedback from people actually playing against it!

Changes include:
* much more aggressive when less than 50% of villages are held by allies,
* persuade units to spread out at the start
* scouts like venturing out on their own
* more aggressive at favourable times of day, more defensive at
unfavourable times
* dispelled completely unreasonable paralysing fear of ghosts
* fixed backstab? (x fingers)
* fixed healers? (x toes)
* make all units much braver in groups
* fixes for berserker value and danger
* revamped combat scoring and recruitment choices.
* improved village capturing efficiency at start of match


Thanks allefant for that python script, it's been useful :). I modified it a bit to be able to run more than one iteration and tally the results (and a few other changes). I'll post my version of it here too.
Attachments
tommy-0.4.7.tar.gz
extract to &lt;wesnoth dir&gt;/data/ais
(33.45 KiB) Downloaded 238 times
aitest.py.txt
not necessary to test the AI
(5.93 KiB) Downloaded 227 times

User avatar
allefant
Units Database Administrator
Posts: 516
Joined: May 6th, 2005, 3:04 pm

Post by allefant »

I let it play a few games here:

tommy-0.4.7.py vs bruteforce.py

Basilisk, Rebels vs Rebels: won 4/5
Cynsaun_Battlefield, Rebels vs Rebels: won 1/2 (I watched the first one in GUI, and aborted the second when bruteforce had twice as many villages, as they both spent quite some time thinking by then..)
Hamlets, Rebels vs Rebels: won 3/5

So, I can confirm what you said, seems it does win over bruteforce, but not easily.

tommy-0.4.7.py vs C++ AI

Basilisk, Rebels vs Rebels: won 5/5
Basilisk, Drakes vs Drakes: won 5/5
Cynsaun_Battlefield, Drakes vs Drakes: won 1/1 (I aborted when C++ had only half as much villages, as it got slow..)
Hamlets, Loyalists vs Loyalists: won 4/5

So yeah, I guess C++ is declassified by it.

I wonder if we could set up some automated AI tournaments using your improved script.. like if someone has some CPU to spare, it could just pit AIs against each other, using various maps and factions. Especially interesting also for testing the new WML AIs once they are competitive.

yobbo
Art Contributor
Posts: 151
Joined: September 16th, 2005, 6:31 am
Location: New Zealand

Post by yobbo »

I've been running tournaments using that script on my own machine :).

The main problem is that the tactics that beat bruteforce and default most consistently are not good tactics.

The current version I'm working on only beats bruteforce about 60% of the time now. But in general, it plays significantly better.

It's kind of depressing, but the default AI actually uses the best single tactic there is assume one thing: overwhelming economic superiority at the start of the match. In this case the best thing to do is capture as many villages as you can, then charge in suicidally, which is what it does :(.

I probably won't release anything new until my AI can:
a) properly assess the strategy the opponent is using
b) perform flanking manoeuvres
c) recognise choke points in the map

This isn't really as hard as it sounds. They're all pretty much the same thing. It's just annoying watching my AI lose :evil:. grarr!

Post Reply