Jump to content

Linking the AI scripts (Javascript) To a Python work environment


ramtzok1
 Share

Recommended Posts

Hello again everyone!

Link to the last thread : 

This is a follow-up to the last thread I made; Our project supervisor wanted me and my partner to consolidate all the information from the beginning, so I will go again and explain our problems regarding the project we are developing.

Our main idea is to develop machine-learning capabilities within the AI in the game. To do so, we are planning to use a work environment (probably TensorFlow) that works with python. Therefore, we need to find a way to connect our ML code that will be written in python with the AI files that are written in JavaScript. So far, we have been offered to use interfaces that are already found in the game code in C++, write the data that we need to a file that we can use ML on, and calling the JavaScript functions using python server in a way that could make us access the valuable API. We have also been offered TensorFlow.js, but unfortunately it turns out that the mathematical libraries essential to ML are not included with it. At the end, we have chosen the third option, and started working on a server that will fulfill our objectives in creating the link. However¸ we have ran into some problems regarding the JavaScript connection. We tried to use jQuery to create requests with the server, but spiderMonkey does not come with it.

At the end of the day, we really want to use the server as a connection between JS and Python, and we would like to ask for any way for us to do so. Even so, if there are any other good ways that exist and can work with the game, we would be happy to hear.

Thank you to all for your help and patience with us so far! It is really not that obvious.

EDIT: Some more insight on what we have done so far: 
We want to extend the existing AI code, written in javascript, with machine learning code written in Python. To do so, we need to invoke our Python code from within the javascript code. We have been offered to make this connection via http (i.e. AJAX), but the game's spidermonkey engine doesn't seem to support AJAX. 

Edited by ramtzok1
  • Like 2
Link to comment
Share on other sites

Regarding JQuery, well the game does use JS. But in a very different context. So I would recommend the server approach.

In my head, it was something like this.

AI <—> AIManager + server (running in a seperate thread)  <—> Python

Python would send and recieve data using a simple API.

Edited by Guest
Link to comment
Share on other sites

I'm not sure if the performance requirements for such an endeavor are met. Machine learning simulates the game with semi-random inputs, so I assume you will have to run at the very least a couple of hundreds of matches, right? Try running one match - depending on the number of units on the map  and the number of Petra AI instances, you will get a lot of "lag", i.e. the engine simulating slower than it should. Say a 30 minute 1v1 game on a small map taking 5-10 minutes to simulate, a 60min 4v4 on a normal map with 150 pop 60+ min. Which means you can simulate somewhere between 20 and 50 games per day with a casual desktop computer and your CPU/cooler won't be happy. One can start new matches and replay previous matches without graphics from commandline, check the readme.

If performance isn't a dealbreaker, and one would require http communication, one would have to use libCURL to do http posts to the webserver. If doing so, sending JSON would be the data format that is easiest do digest by spidermonkey.

Assuming a python agent can be used with pyrogenesis/0ad, how should the simulation data be parsed? If it's a very simple learning algorithm, it might alternatively be considerable to implement that in JS than to implement the interface to a different program and teach that program how to parse/send simulation data.

Link to comment
Share on other sites

20 hours ago, elexis said:

Say a 30 minute 1v1 game on a small map taking 5-10 minutes to simulate, a 60min 4v4 on a normal map with 150 pop 60+ min. Which means you can simulate somewhere between 20 and 50 games per day with a casual desktop computer and your CPU/cooler won't be happy

Good analysis. But if you consider, that 0ad mostly runs single-threaded, you can multiply that number by 3

Link to comment
Share on other sites

  • 5 weeks later...
On 1/17/2019 at 10:36 PM, stanislas69 said:

@ramtzok1 Have you guys given up ?

Hey,

No, we haven't!

I managed to make the linking!

I dug down into the game engine code and figured out what code was related to Engine.{FunctionName} in JavaScript files.

I modified the code by adding 2 functions, AppendToBuffer and WriteToFile so we can send data the Python, I also added useful functions that cannot be accessed from the simulation folder.

Right now we are trying to understand Petra's attack and defense in order to develop the machine learning and we really need an active Petra maintainer so we can ask him about stuff we don't fully understand.

  • Like 3
Link to comment
Share on other sites

1 hour ago, ramtzok1 said:

No, we haven't!

Great news :)

1 hour ago, ramtzok1 said:

I dug down into the game engine code and figured out what code was related to Engine.{FunctionName} in JavaScript files.

 I modified the code by adding 2 functions, AppendToBuffer and WriteToFile so we can send data the Python, I also added useful functions that cannot be accessed from the simulation folder.

Would be nice if you could fork the game on GitHub so we could see your changes and your code :)

1 hour ago, ramtzok1 said:

Right now we are trying to understand Petra's attack and defense in order to develop the machine learning and we really need an active Petra maintainer so we can ask him about stuff we don't fully understand.

Let me see if I can find a way to help you with that. @mimo

 

@ramtzok1 Our lead and only AI dev left the team to make a Fork of the game. Your best bet is to contact him here:

On 12/11/2018 at 10:09 PM, fatherbushido said:

As said before, the state of that fork is not yet publicly available, but all information will be given on our website when available.

Meanwhile, you can reach us at https://webchat.freenode.net/?channels=forkad

Kind regards,

Link to comment
Share on other sites

If I'm not mistaken, many of the files in Petra deal with hard-coded decisions to evaluate feature X of the game state and take an action, but if you add machine learning you have to scratch most of that. It would be nice to have a clean base, richer than baseAI with the queues and serialization and whatnots, so people would have to worry only with stimuli and action to allow easy modding with different machine learning methods.

Link to comment
Share on other sites

3 hours ago, (-_-) said:

This AI is now lightyears behind anyway.

Really ? They made so many fixes during the freeze ? :)

I guess since they changed the simulation as well we won't be able to use it but it will be nice to check when they release the source once they make a release.

Link to comment
Share on other sites

36 minutes ago, stanislas69 said:

Really ? They made so many fixes during the freeze ? :)

Yes. (And not just for the AI.)

("during the freeze is wrong" I guess. "Since mimo left" would be better as the freeze had ended for a while now. I dont see any AI commits after r21773 back in April 2018)

Don’t take what I say as facts. Who am I to say with so much certainty that there is currently no one here at WFG working on the AI?...but it is the logical conclusion I can arrive at considering all available information.

Edited by Guest
Link to comment
Share on other sites

8 minutes ago, (-_-) said:

Don’t take what I say as facts. Who am I to say with so much certainty that there is currently no one here at WFG working on the AI?...but it is the logical conclusion I can arrive at considering all available information.

I can say there is no one. In fact I even stated it above :)

9 minutes ago, (-_-) said:

Yes. (And not just for the AI.)

Ah nice for them. Can't wait to see their first release :) Do you also happen to know an ETA ?

 

10 minutes ago, (-_-) said:

I dont see any AI commits after r21773 back in April 2018)

Well save for Mimo who didn't really have to go through the review process even though he made some reviews there is no commit by the two other members in 2018. Leper's work was merged by Itms and I don't remember seeing patches from fatherbushido. only reviews and comments. :)

Link to comment
Share on other sites

On 1/24/2019 at 12:14 AM, stanislas69 said:

Would be nice if you could fork the game on GitHub so we could see your changes and your code

No problem, I will upload to my fork, though we are using an old version of the game with no plans of updating to the new one.

 

On 1/24/2019 at 12:14 AM, stanislas69 said:

Our lead and only AI dev left the team to make a Fork of the game. Your best bet is to contact him here:

Ok, will do, thank you for that!

On 1/24/2019 at 7:04 AM, sarcoma said:

If I'm not mistaken, many of the files in Petra deal with hard-coded decisions to evaluate feature X of the game state and take an action, but if you add machine learning you have to scratch most of that. It would be nice to have a clean base, richer than baseAI with the queues and serialization and whatnots, so people would have to worry only with stimuli and action to allow easy modding with different machine learning methods. 

1

We won't build the AI from scratch in our case instead the idea is to use some of Petra's parts (our project is very limited on time) for an example gathering. It will basically be bunch of if-else statements reading the output of the Python ML module.

Our machine learning will be focusing mostly on defending properly since we found it to be a lot easier to change and improve than attacking (because of deferent attacking and execution plans).

Link to comment
Share on other sites

  • 1 month later...
On 12/16/2018 at 3:25 PM, ramtzok1 said:

Our main idea is to develop machine-learning capabilities within the AI in the game. To do so, we are planning to use a work environment (probably TensorFlow) that works with python. Therefore, we need to find a way to connect our ML code that will be written in python with the AI files that are written in JavaScript. So far, we have been offered to use interfaces that are already found in the game code in C++, write the data that we need to a file that we can use ML on, and calling the JavaScript functions using python server in a way that could make us access the valuable API. We have also been offered TensorFlow.js, but unfortunately it turns out that the mathematical libraries essential to ML are not included with it. 

1
1

How about binding some necessary  C++ functions in python using Boost.Python? For instance, tensorFlow has C++ API. Hence, the key functionality can be written in C++, and the user functions for extending/writing AI can be exposed in Python.   

 

(It's only been 2 days since I started looking at the code, so I cannot comment on the feasibility of this, but I didn't see this option mentioned above.)

Link to comment
Share on other sites

  • 11 months later...

I realize I am pretty late to comment on this but if anyone is still interested in this, this revision adds support for using 0 AD like an OpenAI gym environment. There are a couple simple examples using this API at https://github.com/brollb/simple-0ad-example where the agent learns how to kite cavalry archers when fighting infantry!

  • Like 3
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...