[Ironpython-users] Gradually executing a python script

Keith Rome rome at Wintellect.com
Tue Jul 24 02:05:43 CEST 2012


That is a pretty cool demo already :)

The term "abstract machine" used by PAWN on that page you have linked is a reference to the execution environment of that language. The "abstract machine" (more often referred to as a "virtual machine") in that sense is roughly equivalent to the .NET Runtime. You can also view the IronPython engine similarly as a virtual machine - think of it as a runtime within a runtime.

What you have described as "ticks" of the AI runtime sounds roughly equivalent to what I described as "turns". Either way, the idea is to break up actions into a series of discrete steps, each step with an associated unit of cost. For example, the "shoot" action might have a cost of 5, which means it can only be activated once every 5 turns (or ticks).

I am pretty sure that this is what Kevin is referring to when he says "state machine": http://en.wikipedia.org/wiki/Finite-state_machine. It can be a little confusing because these are also sometimes called "abstract machines" - but these are an entirely unrelated concept from the virtual machine environment I describe above.

Essentially, a state machine is a way to describe the various viable "states" something can be in, and the allowable transitions that can occur to move from one state to another. For example, your robots could have states for rotation speed of each wheel that can only be changed by 0.1 at a time, up to a maximum speed of ±1.0. Or, your robots might have a "gun cooldown" state that lasts for 5 turns/ticks, and the "shoot" action is only usable while not in a "gun cooldown" state (and using the "shoot" action will trigger the 5-turn "gun cooldown"). And you can imagine numerous additional mechanics that build on the general principle... but the main idea is that you have one model (the "state machine") that ensures that the AI must adhere to the rules of your game. In other words, making sure a robot is not able to "shoot" again until it's gun cools down.


Keith Rome
Senior Consultant and Architect
MCPD-EAD, MCSD, MCDBA, MCTS-WPF, MCTS-TFS, MCTS-WSS
Wintellect | 770.617.4016 | krome at wintellect.com<mailto:rome at wintellect.com>
www.wintellect.com<http://www.wintellect.com/>

From: Jesper Taxbøl [mailto:jesper at taxboel.dk]
Sent: Monday, July 23, 2012 6:18 PM
To: Kevin Hazzard
Cc: Keith Rome; ironpython-users at python.org
Subject: Re: [Ironpython-users] Gradually executing a python script

First of all, thank you for your time.  I am not that experienced when it comes to all the terms used in this area of software, so please be patient with me.

To give you a more clear idea of what I am doing I have posted a video of the current progress here http://www.youtube.com/watch?v=HER6WSIwSBQ&feature=youtu.be . This is a simple gameengine, with a physics based robot. It has simple senes (A vector to the nearest bot and the length of a raycast in front of the robot) and three actions (left_wheel_speed, right_wheel_speed and fire). IronPython is embeeded inside the Unity3D game engine which runs on .NET 2.0. I am using the dll version of ironpython in the project. (I could not figure out how to integrate the source into my game project.)

The whole prototype was made at  48 hour game jam called "No More Sweden" in Malmö the past weekend.

I did not know that the python code is compiled in IronPython before it is executed, and therefore will catch syntax errors before execution. That will be very usefull. :)

I will need to look into the settrace advice, but my gut feeling is that it is not the direction I want to be going in. It might be later during work on the GUI / programming interface.

I agree that I should have a simple singlethreaded, turn based solution, as that can be made deterministic and its far easier to implement. That is definitely the way I want to go. The threading was only used in the prototype because I could not execute my script gradually. I saw the execute command as the only way to have Ironpython run my script, and saw a thread as the only way out. (at 3 am in the morning)

The reason I keep mentioning gradually execution requires some backstory: The reference project I have been inspired by is an old game called GunTactyx (http://www.youtube.com/watch?v=vT8PYETav7A&feature=relmfu), where you program robots to hunt down other robots. It uses an old version of the PAWN Language/abstract machine (http://www.compuphase.com/pawn/pawn.htm). As far as I understand each robot there has an isolated abstract machine with limited memory and it is ticked gradually, like you control the clock on its CPU. If a robot crashes, has an infinite loop or a memory leak it does not affect the cpu in the other robots. The reason I am not using PAWN is that it is C++ based and my target platform runs .NET 2.0. Besides that I prefer Python over the PAWN language.

Kevin: When you say statemachine, I imagined Ironpython had something of that sort inside its belly? That might be where I am wrong. Is a statemachine and an abstract machine the same thing in this context?

With regard to the dostuff() functions, I dont mind the robots firing many instructions at a time. I just need to be able to let the robots run the same number of "ticks" between each game cycle. I imagine assigning a tick cost to the dostuff() functions, as they probably will do stuff inside the game world.

I have been looking at other solutions to allow me to make a programming game. During the 48 hours I briefly used the AluminumLua project which I was able to tick, but it lacked debelopment on the language side. (for loops was not implemented) I would prefer to stick to Pythoon as I love the language and I feel it contains the elements needed to write an interristing AI.

I need to read up on the IronPython  system and have therefore just ordered "Ironpython in action" book, I hope it will give me a better understanding. Other recommendations are welcome.

Kind regards and thank you again for your feedback.

Tax



2012/7/23 Kevin Hazzard <wkhazzard at gmail.com<mailto:wkhazzard at gmail.com>>
What you're describing is a state machine. There are many ways to build such a thing and honestly, I've never thought of single-stepping through code as a way to do that. It's actually quite an intriguing idea, if not inefficient. But efficiency isn't always measured with elegance (and vice versa). Keith's settrace idea is good, too. You should check that out.

What it really comes down to is this: do you want the controller to also define the code or merely to drive it? For the example you posted, do you want the while loops implemented in Python to execute your dostuff() and dootherstuff() methods? Or do you want the controller that authorizes a "step" to do that? In that context, you could write the controller in Python, too, but the design of your code would look very different than what you proposed.

Kevin
On Mon, Jul 23, 2012 at 1:48 PM, Jesper Taxbøl <jesper at taxboel.dk<mailto:jesper at taxboel.dk>> wrote:
The reason I want to do it gradually is that I cant be sure that the script ever terminates or has syntax errors. Would that not be a problem in a turn based setup?

2012/7/23 Keith Rome <rome at wintellect.com<mailto:rome at wintellect.com>>
You may want to look into leveraging the sys.settrace() feature of Python for controlling line-by-line execution. This API allows you to install a profiling function that gets invoked for every frame of script code that is executed. In your profiling function, you could compute the amount of memory being used by variables within the ScriptScope, and halt execution if you need to. Just be careful about what you do in your profiling function, as it will be called extremely often by the runtime.

http://docs.python.org/library/sys.html#sys.settrace


The reason you are seeing variant results is probably due to how you have implemented multithreading. The IronPython runtime is mostly thread-safe (as long as you don't use libraries that make use of mutable objects, and as long as you import all libraries used at least once prior to forking threads). But your code must also be thread-safe as well. From your descriptions of your game engine, it sounds like your game engine is not designed to be thread-safe so I would strongly recommend avoiding multithreading as a means of providing resource sharing. It is very difficult to write 100% thread-safe code, and nothing will stop people from writing unsafe scripts in your game.

Instead, I would suggest implementing your game engine as a turn-based system. For each turn, the script for each character is executed completely. This will allow you to cycle through all characters one turn at a time, equally, and will also eliminate the problem of having variant outcomes since the program will become deterministic.



Keith Rome
Senior Consultant and Architect
MCPD-EAD, MCSD, MCDBA, MCTS-WPF, MCTS-TFS, MCTS-WSS
Wintellect | 770.617.4016<tel:770.617.4016> | krome at wintellect.com<mailto:rome at wintellect.com>
www.wintellect.com<http://www.wintellect.com/>

From: ironpython-users-bounces+rome=wintellect.com at python.org<mailto:wintellect.com at python.org> [mailto:ironpython-users-bounces+rome<mailto:ironpython-users-bounces%2Brome>=wintellect.com at python.org<mailto:wintellect.com at python.org>] On Behalf Of Jesper Taxbøl
Sent: Monday, July 23, 2012 11:05 AM
To: Kevin Hazzard
Cc: ironpython-users at python.org<mailto:ironpython-users at python.org>
Subject: Re: [Ironpython-users] Gradually executing a python script

Would that allow me to step gradually through a loop?

like:

x = 0
while x < 10:
   dostuff()
   x=x+1
while x > 0:
   dootherstuff()
   x=x-1



2012/7/23 Kevin Hazzard <wkhazzard at gmail.com<mailto:wkhazzard at gmail.com>>
Why don't you use a scripting host and inject commands into a ScriptEngine reusing a ScriptScope as you need to execute them?

Kevin
On Mon, Jul 23, 2012 at 5:31 AM, Jesper Taxbøl <jesper at taxboel.dk<mailto:jesper at taxboel.dk>> wrote:
Hi,

I am not that familiar with Ironpython yet, but I have a question that I hope you can help me answer.

I am working on a programming-game where I will allow users to do some simple python scripting against a simple API that I will control a game character. Instructions like move and shoot etc, alongside some simple sense functions that return info on the game world.

My current prototype creates an ironpython engine for each game character and executes the script in a thread by itself, which sort of works. But I have the problem that the outcome of executing the game gives different results every time. Therefore I would like to ask the question:

Is it possible to execute a script inside the Ironpython engine gradually?

I imagine that I could update a list of engines with a tick(int cycles) and get a fair sharing of resources between engines and ensure the same result every time.

Kind regards

Tax


P.S:
As this is a programming game I would also like to be able to limit the available memory each script is using. Is there a way to limit this, so a script like this would be killed.

x = 0
v = {}
while True:
   v[x]=x
   x= x + 1



_______________________________________________
Ironpython-users mailing list
Ironpython-users at python.org<mailto:Ironpython-users at python.org>
http://mail.python.org/mailman/listinfo/ironpython-users



--
W. Kevin Hazzard
Consultant, Author, Teacher, Microsoft MVP
(804) 928-3444<tel:%28804%29%20928-3444>
book<http://manning.com/hazzard> | mvp<https://mvp.support.microsoft.com/profile/Kevin.Hazzard> | twitter<http://twitter.com/#%21/KevinHazzard> | facebook<http://www.facebook.com/wkhazzard> | linkedin<http://www.linkedin.com/in/kevinhazzard> | captech<http://captechconsulting.com>






--
W. Kevin Hazzard
Consultant, Author, Teacher, Microsoft MVP
(804) 928-3444<tel:%28804%29%20928-3444>
book<http://manning.com/hazzard> | mvp<https://mvp.support.microsoft.com/profile/Kevin.Hazzard> | twitter<http://twitter.com/#!/KevinHazzard> | facebook<http://www.facebook.com/wkhazzard> | linkedin<http://www.linkedin.com/in/kevinhazzard> | captech<http://captechconsulting.com>


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20120724/4a097846/attachment.html>


More information about the Ironpython-users mailing list