[Ironpython-users] Gradually executing a python script

Jesper Taxbøl jesper at taxboel.dk
Tue Jul 24 11:59:01 CEST 2012


Thanx, :)

Meanwhile i was looking at sandbox environments in .NET via AppDomain's. It
is used here http://www.finalbot.com. From reading their FAQ they battle
the exact same issues I do, and solve them by passing them to the
player. :( http://www.finalbot.com/faq.aspx

To better understand what I want I have listed my priorities (highest
first):

1) Completely seperate execution of avatar code.
No limitations on what code can be run. (Infinite loops, division by zero,
memory leaks)

Code will be hostile and must support syntax like this without hanging the
game (its ok for the avatar to crash as that is the game):

while True:
   format_harddisk()
   while sense1==False:
      action_1=True
      action_2=False
   while sense2==True:
      action_1=False
      action_2=True
   a = 99.0 / 0

Note: My goal code-form is actually quite similar to the types of programs
you write on the Arduino platform where you basically just write a main
loop. (I am ignoring interrupts, even though they could be cool to have
available)

2) Fair sharing of memory and CPU cycles.
3) Ability to communicate with game engine to exchange actions and senses.
4) Deterministic outcome.
5) It must be based on a higher level language that can send players into
other fields of programming. (Python is my first choice)
6) A prototype must be below  100 hours of development time. ()


So it looks I am on the market for an Abstract machine (execution
environment). Do you know any implemented in .Net? There seem to be a java
one http://www.ikvm.net/, but its too early to say if it will work for me.

Unless I can prepare the python environment with some hook into settrace as
was suggested earlier. I think this wilkl be my next research vector.

Alternatively I could build my own Turing machine. But that would miss my
target audience by 15+ years. :)

I know my statemachines. I am already using them in the demo as I have
cooldown on the firing mechanism and the physics engine sets limitations on
how long driving the wheels take.



Tax

P.S:
To give some insight to my setup I have attached the avatar script I am
using. The interresting part is kickstarted from SetCode().


using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using IronPython;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

using System.Threading;


public class Tank : MonoBehaviour
{
Transform body;
 Transform lf, rf, lb, rb;
Transform cannon;
 ScriptEngine engine;
ScriptScope scope;
 Thread PythonThread;
 string code = "";
 float right = 0.0f;
float left = 0.0f;
 bool fire = false;
float fireinterval = 1.0f;
float lastfire = -1.0f;
 //Senses
float distance = 100.0f;
 Vector3 nearest_enemy = new Vector3();
 bool destroyme = false;
 object result = null;
 void Awake()
 {
GameManager.Instance.tanks.Add(this);
 body = transform.FindChild("Body");
cannon = transform.FindChild("Cannon");
 lf = transform.FindChild("LF");
lb = transform.FindChild("LB");
 rf = transform.FindChild("RF");
rb = transform.FindChild("RB");
 Physics.IgnoreCollision(lf.collider, body.collider);
Physics.IgnoreCollision(lb.collider, body.collider);
 Physics.IgnoreCollision(rf.collider, body.collider);
Physics.IgnoreCollision(rb.collider, body.collider);
 Physics.IgnoreCollision(cannon.collider, body.collider);
 Physics.IgnoreCollision(rf.collider, rb.collider);
Physics.IgnoreCollision(lf.collider, lb.collider);
 Physics.IgnoreCollision(lf.collider,
GameObject.Find("/World/maze1").collider);
 Physics.IgnoreCollision(lf.collider,
GameObject.Find("/World/maze2").collider);
Physics.IgnoreCollision(lf.collider,
GameObject.Find("/World/maze3").collider);
 Physics.IgnoreCollision(lf.collider,
GameObject.Find("/World/maze4").collider);
 Physics.IgnoreCollision(lb.collider,
GameObject.Find("/World/maze1").collider);
Physics.IgnoreCollision(lb.collider,
GameObject.Find("/World/maze2").collider);
 Physics.IgnoreCollision(lb.collider,
GameObject.Find("/World/maze3").collider);
Physics.IgnoreCollision(lb.collider,
GameObject.Find("/World/maze4").collider);
 Physics.IgnoreCollision(rf.collider,
GameObject.Find("/World/maze1").collider);
 Physics.IgnoreCollision(rf.collider,
GameObject.Find("/World/maze2").collider);
Physics.IgnoreCollision(rf.collider,
GameObject.Find("/World/maze3").collider);
 Physics.IgnoreCollision(rf.collider,
GameObject.Find("/World/maze4").collider);
 Physics.IgnoreCollision(rb.collider,
GameObject.Find("/World/maze1").collider);
Physics.IgnoreCollision(rb.collider,
GameObject.Find("/World/maze2").collider);
 Physics.IgnoreCollision(rb.collider,
GameObject.Find("/World/maze3").collider);
Physics.IgnoreCollision(rb.collider,
GameObject.Find("/World/maze4").collider);
 }
  public void PythonThreadFunction()
    {
Dictionary<String,System.Object> options = new
Dictionary<string,System.Object>();
    options["DivisionOptions"] = PythonDivisionOptions.New;
    engine = Python.CreateEngine(options);
    scope = engine.CreateScope();
    ScriptSource source = engine.CreateScriptSourceFromString(code,
SourceCodeKind.Statements);
 //Set senses
scope.SetVariable("distance", distance);
 float f = nearest_enemy.x;
scope.SetVariable("nex", (double)f);
 f = nearest_enemy.y;
scope.SetVariable("ney", (double)f);
f = nearest_enemy.z;
 scope.SetVariable("nez", (double)f);
 //Also declare action variables
 scope.SetVariable("left", left);
scope.SetVariable("right", right);
 scope.SetVariable("fire", fire);
 try{
 //Wish I had a source.Execute(scope, [number of IL instructions to
execute])
                    result = source.Execute(scope);
}
 catch (Exception e)
 {
Debug.Log ("Execution error" + e.ToString());
 }
destroyme = true;
    }
 public void SetCode(string c)
{
code = c;
 PythonThread = new Thread(new ThreadStart(PythonThreadFunction));
PythonThread.IsBackground = true;
 PythonThread.Priority = System.Threading.ThreadPriority.Lowest;
        PythonThread.Start();
}
 // Update is called once per frame
void Update ()
 {
JointMotor jm = new JointMotor();
jm.force = 100;
 //Probe senses
RaycastHit hit = new RaycastHit();
 if(Physics.Raycast(new Ray(body.position, body.rotation *
Vector3.forward), out hit, 100.0f))
{
 distance = hit.distance;
}
 nearest_enemy = body.position;
foreach(Tank t in GameManager.Instance.tanks)
 {
if(t != this)
{
 if(nearest_enemy == body.position || (t.body.position -
body.position).magnitude < (nearest_enemy - body.position).magnitude)
{
 nearest_enemy = t.body.position;
}
}
 }
nearest_enemy = body.InverseTransformPoint(nearest_enemy);
  //Fetch output
if(code != "")
 {
try{left = Mathf.Clamp(scope.GetVariable<float>("left"), -1.0f,
1.0f);}catch{left = 0.0f;}
 try{right = Mathf.Clamp(scope.GetVariable<float>("right"), -1.0f,
1.0f);}catch{right = 0.0f;}
try{fire = scope.GetVariable<bool>("fire");}catch{fire = false;}
 }
 jm.targetVelocity = right * 400.0f; //rs * s;
 rf.hingeJoint.motor = jm;
rb.hingeJoint.motor = jm;
 jm.targetVelocity = left * 400.0f; //ls * s;
lf.hingeJoint.motor = jm;
lb.hingeJoint.motor = jm;
 if(Input.GetKeyDown(KeyCode.Space))
{
 fire = true;
}
 //Tick
if(fire && Time.time > lastfire + fireinterval)
{
 lastfire = Time.time;
fire = false;
float force = 10.0f;
 GameObject go =
(GameObject)GameObject.Instantiate((GameObject)Resources.Load("prefabs/Projectile"),
cannon.position + cannon.rotation* (Vector3.up * 0.055f +  Vector3.forward
* 0.4f), cannon.rotation);
 go.rigidbody.AddForce(cannon.rotation * Vector3.forward * force,
ForceMode.Impulse);
cannon.rigidbody.AddForce(cannon.rotation * Vector3.forward * -force,
ForceMode.Impulse);
 Physics.IgnoreCollision(go.collider, cannon.collider);
Physics.IgnoreCollision(go.collider, body.collider);
 }
 if(destroyme)
 {
Destroy(transform.gameObject);
}
 }
 public void hit()
 {
Destroy (transform.gameObject);
}
 void OnGUI()
{
 Vector3 p = Camera.mainCamera.WorldToScreenPoint(body.position);
GUI.Label(new Rect(p.x,Screen.height - p.y,      100,100),
string.Format("left {0:0.00}", left));
 GUI.Label(new Rect(p.x,Screen.height - p.y + 20, 100,100),
string.Format("right {0:0.00}", right));
GUI.Label(new Rect(p.x,Screen.height - p.y + 40, 100,100),
string.Format("distance {0:0.00}", distance));
 GUI.Label(new Rect(p.x,Screen.height - p.y + 60, 100,100),
string.Format("nearest_enemy {0:0.00} {1:0.00} {2:0.00} ", nearest_enemy.x,
nearest_enemy.y, nearest_enemy.z));
 }
 void OnDestroy()
 {
GameManager.Instance.tanks.Remove(this);
 if(PythonThread != null)
{
PythonThread.Abort();
 }
}
}


2012/7/24 Keith Rome <rome at wintellect.com>

>  That is a pretty cool demo already J****
>
> ** **
>
> 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 <rome at wintellect.com>****
>
> 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>****
>
> 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> 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>****
>
> 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 | krome at wintellect.com <rome at wintellect.com>****
>
> www.wintellect.com****
>
>  ****
>
> *From:* ironpython-users-bounces+rome=wintellect.com at python.org [mailto:
> ironpython-users-bounces+rome=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
> *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>****
>
> 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> 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
> http://mail.python.org/mailman/listinfo/ironpython-users****
>
>
>
>
> --
> *W. Kevin Hazzard*****
>
> Consultant, Author, Teacher, Microsoft MVP****
>
> (804) 928-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****
>
> 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/df0358af/attachment-0001.html>


More information about the Ironpython-users mailing list