View topic - Python AI Interface


All times are UTC + 1 hour


Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Python AI Interface
PostPosted: 31 May 2010, 11:23 
Spring Developer

Joined: 31 May 2009, 23:08
TheGeorge and me wrote an Python AI Interface for spring.

I think we will start to write an AI when it is integrated into the spring installer, currently there is only an Hello-World AI.

As we got no bug-reports (noone tested it?) it would be useful if someone would really test it and write an ai or so :-)

For short setup instructions look here:

http://springrts.com/wiki/AI:Development:Lang:Python

We are happy about any kind of feedback!


Last edited by abma on 04 Jun 2010, 08:47, edited 1 time in total.
Top
 Offline Profile  
 
 Post subject: Re: Python AI Interface
PostPosted: 01 Jun 2010, 19:53 
Community Lead
User avatar

Joined: 10 Apr 2006, 04:05
Location: Finland, 1944
I'll try to bring this to the attention of a friend who may be interested.
Top
 Offline Profile  
 
 Post subject: Re: Python AI Interface
PostPosted: 10 Jun 2010, 14:06 
AI Coder

Joined: 16 Nov 2009, 18:26
Does the interface make a copy of game state before it passes it off to python or does python directly expose the data structures of the game engine's state?
Top
 Offline Profile  
 
 Post subject: Re: Python AI Interface
PostPosted: 10 Jun 2010, 22:30 

Joined: 06 May 2010, 10:22
There are two ways the interface interacts with Spring:

Events:
The engine generates events which are translated into a Python data structure. This is an conversion from a C struct into a Python dict.

Callback:
The first event (init) holds in it's C struct a pointer to a callback object, which also is a C struct. The callback object contains all functions, which can be used to get information about the current engine state or to change it (like send a command to a unit or something else). There is a Python Callback Object, which does all the type conversions. You can use this callback direct or via the the classes, which is much nicer.

The interface only holds a reference to the corresponding callback objects. (if this is what you mean with the engine state)
Top
 Offline Profile  
 
 Post subject: Re: Python AI Interface
PostPosted: 16 Jun 2010, 06:04 
AI Coder

Joined: 16 Nov 2009, 18:26
Wanted to give some real quick feedback.

First, let me say thanks for providing this interface. It saves me from having to do it (I had actually already started - but you guys are much farther along) and I can assure you, your efforts are most appreciated.

While I've not done a lot with the API, I have played a little thus far. The network console interface is really, really, helpful. Since this project is currently sans documentation or even pydoc, allowing for introspection is a real life saver. I'm just throwing this out there, but I seem to recall ipython has an interface to allow for wrapping for network/interactive shells. I wouldn't swear to that though. You might want to see if this is a viable upgrade. Having something with the power of ipython as a console would be simply awesome. Then again, you guys might be considering the console as a short term solution for now.

I have noticed the interface in general is extremely fragile. This is already noted in the release notes but it doesn't hurt to reaffirm what you already know. ;) Parameter validation and exception/error handing would be a huge improvement over crashing Spring. In my opinion, this is the single largest obstacle to prevent wider acceptance of the python interface.

Here's what I'm working on. I've created an "AI" called, "RAIMI" (Remote AI Management Interface). This in turn uses Pyro (version 4 for now - for performance/simplification reasons). As the description implies, its really more of an interface for AIs rather than an AI proper. Then, "user AIs" make themselves known via the name server. On event from Spring, RAIMI looks up an AI for the team and passes the event to the AI. At any rate, RAIMI would be fairly static and generally not changed by would be AI authors. In turn, developers make their AI available via the name server whereby RAIMI is able to discover them. This glosses over various details because its both tedious and I'm working out some of the details and coding.

I fully expect RAIMI will do things like statistical analysis of the map, saving each remote AI from having to do so. None of which is remotely close to being coded yet. It of course, also acts as a synchronizing gateway for callbacks, back into the Spring engine; so as to insure single threadedness. This approach should reduce redundant work between multiple AIs as well as drastically improve performance for AI startup (metal mapping/analysis) without having to make a ton of AI calls to obtain such information (I assume).

In doing so I hope this approach allows each team AI to run on one or more CPUs on one or more computers. There are some very real questions yet to be answered which will determine if my approach has wings. If this works it means each AI can run on different machines/CPUs, independent of the game's CPU load.

If my approach pans out, and that's currently a big if, I intent to provide a complimentary library to aid in developing AIs such that the library helps with common tasks, asset tracking, basic strategies, etc. In doing so, I hope to allow people to concentrate on AI implementation rather than becoming mired in the many small details of AI game development.

I'll keep you guys updated as I continue to make progress.
Top
 Offline Profile  
 
 Post subject: Re: Python AI Interface
PostPosted: 16 Jun 2010, 13:29 

Joined: 06 May 2010, 10:22
That are great news.

I will implement type checking as soon as possible. I just have little time at the moment. But it will definitely be implemented. If done so, an exception would be thrown. The engine however wouldn't be crashing anymore.

There is no documentation of the interface, because... well, there is not much documentation at all. The interface is generated out of the C sources and the little documentation that is there is not used in the interface. However, there is documentation about the types of the arguments that should be used. In some further version, there will be more documentation about what the function does as there is more in the C interface.

But it's really cool to see someone using the interface allready. :-)
Top
 Offline Profile  
 
 Post subject: Re: Python AI Interface
PostPosted: 19 Jun 2010, 13:03 

Joined: 06 May 2010, 10:22
Update:

To call functions of the interface, you do not need to instantiate the interface classes anymore.

Past:
self.map=Map(self.team)
self.map.get...()

Now:
Map.get...()

If an event occurs, only the AI which receives the event is able to call the interface.
Top
 Offline Profile  
 
 Post subject: Re: Python AI Interface
PostPosted: 19 Jun 2010, 14:43 

Joined: 06 May 2010, 10:22
Update:

There now is type checking in the interface, so the engine won't crash if wrong types are used.
Top
 Offline Profile  
 
 Post subject: Re: Python AI Interface
PostPosted: 20 Jun 2010, 13:46 
AI Coder

Joined: 16 Nov 2009, 18:26
I have a couple of minor improvements. Where can I submit patches?
Top
 Offline Profile  
 
 Post subject: Re: Python AI Interface
PostPosted: 16 Aug 2010, 18:48 
AI Coder

Joined: 16 Nov 2009, 18:26
I did a quick search and found nothing. Can we get the UnitCommandOptions structure values exposed in the Python interface? If its already exposed, please point the way. These values are required to allow AI's to provide command options - which I'm now in need of using.

http://github.com/spring/spring/blob/master/rts/ExternalAI/Interface/AISCommands.h
Top
 Offline Profile  
 
 Post subject: Re: Python AI Interface
PostPosted: 15 Sep 2010, 20:42 
Spring Developer
User avatar

Joined: 22 Sep 2007, 08:51
This is now the last missing thing (of officially supported stuff) left to be ported to the pureint branch.
Lets have a look at it together when you are in the lobby some day. :-)
Top
 Offline Profile  
 
 Post subject: Re: Python AI Interface
PostPosted: 28 Sep 2010, 16:19 
AI Coder

Joined: 16 Nov 2009, 18:26
BTW, in other threads it was made clear that structure definitions are frequently exposed in other interfaces. Is this planned for the python interface?

Right now, obtaining information frequently requires a series of function calls which are fairly expensive in python. In fact, they are so expensive, getters/setters idioms are frequently a source of performance headache for python apps and as such should be avoided. Unfortunately, this is the exact mode of operation which is forced on AIs by the current interface. As such, having shared structures which directly expose attributes would be a significant performance gain.

In my own testing, while these are not currently performance bottlenecks, they are playing a significant role in "bursty" behavior. I've not determined if this is an inherent artifact of my design but I do not believe it to be so. Regardless, more testing is required to actually draw a conclusion.

What happens is, during large games, lots of units require a refresh of current stats. So for example, lets say we have fifty units which require refresh. If we are interested in four attributes for each, we now have 200 synchronously blocking function falls. In large battles fifty units represents a fairly low count and that still ignores that other functions will likely need to be periodically called too.

One thing that will help, at least a little, is to release the python lock for callbacks. This allows pure python threads to continue to run while another thread is busy falling a spring interface function. Increased concurrency is always good. Given the automated nature of the interface, this is likely easy to add.

Having said all that, I have identified several ways my framework have help avoid these issues, including the bursty issue, just the same, I feel there are several items which can be improved to help further alleviate the issue, thusly reducing the work required of python AI developers.
Top
 Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Site layout created by Roflcopter et al.