Guide: How to build an agent in python?


  • Open this project in Visual Studio.
  • Let's see the files in there

C# files:

  • Kept CircleAgent.cs and RectangleAgent.cs files from previous version of competition submission
  • In these files, the code in the python folder is called using methods from a new class: PythonModule.cs - [IMPORTANT]: change the pythonLocation to the location of Python in your machine (see line 19 in the image below)

Card image cap
Python Folder

  • main.py (receives the arguments from the command line and redirects to the appropriate condition depending if we are initializing the agent rectangle or circle - or running it.)
  • jsonManager.py (deserializes the JSON strings received in the arguments, in order to create or update the environment)
  • environment.py (contains the environment class, and all its components as classes, circleCharacter, blackObstacles, collectibles,...)
  • agent.py (contains the agent classes - circle and rectangle - with a random implementation - random actions)

Card image cap
To initialize the environment and the agents:

In C#, the agents, circle and rectangle, start by running the Python script to initialize the corresponding agent with the important features of the environment, represented as JSON strings, as seen in the image. [IMPORTANT]: change the boolean "is_python" value for true if you are working in python.

Card image cap
To initialize the environment and the agents:

In Python, an environment object is initialized, given the JSON strings received, being saved in the agent object created (circle and rectangle). This agent object is saved in a file using “pickle”, since we will need it when we run the script again during the execution of the agent's actions:

Card image cap
To execute actions and update the environment:

To execute actions and update the environment: In C#, in the update function, the Python script is called every second to obtain the current agent action (it is not possible to call the Python script every frame). As arguments, we send the updated features of the environment, to update its state before selecting a new action (as JSON strings once again) - in the random agent is not needed but it will be useful for other implementations:

Card image cap
To execute actions and update the environment:

In Python, the agent object is loaded, and its environment object is updated according to the arguments received in the command line. Then, the agent changes its current action and prints it, i.e., prints the index of its current action given the list of its possible moves. In this case, the agent implementation is random. - [IMPORTANT]: the print of the current action can't be removed, it is needed in order to be used in C# to update the current action variable.

Card image cap

Now, this code can be updated to consider the current state of the environment when deciding on the action to return, however there's a few tools that weren't considered for now:

  • The C# forward model that simulates the actions to predict their outcome (since this is a physics game)
  • communication between agents (but I assume this is easy to implement)

Additional notes about this:

  • Don't forget to compile the solution before playing, in case you change anything in the C# files - in the GitHub directory there were problems doing this (it seemed like nothing was updating), not really sure why
  • You can run several simulations by writing something similar to this in the command line (inside the GeometryFriendsGame/Release folder): ./GeometryFriends.exe --batch-simulator. It's useful to train or test your agents, and you can easily select the level and agent you want to test, how many simulations, the speed (not recommended), ... and it will return a file with its results (time, collectibles caught, ...) - you can change the time limit of the levels that you have available, in their XML file

Card image cap
Note: You have at your disposal all the libraries available in Anaconda3 (Python 3.10) as well as TensorFlow, Pickle, Keras, Theano, and Takeaway.
© 2020 Copyright: GeometryFriends