# Sunday, June 12, 2005

I have created a project on http://projectdistributor.net for NSteer and will soon upload a drop to the server. If you are interrested by this project, suscribe to the RSS:

 

posted on Sunday, June 12, 2005 11:17:38 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [0]
# Friday, June 10, 2005

NSteer 2: Laying down some interfaces

In the previous episode, we laid down the concepts and “lexical” language about autonomous agents. We can start to design interfaces for each concept identified previously.  Here’s a concept summary to get started:

Concept summary

  • Dynamic concepts
    • Agent, the canonical autonomous agent,
    • Behavior, the canonical behavior,
    • Body, an instance defining the kinetic dynamics of the agent,
    • Vision, what the agent sees,
    • Integrators integrate the motion dynamics,
    • Saturators, saturate scalars and vectors,
  • Visualization concepts
    • Visualizable: can be visible/hidden, can be rendered,
    • Sprite: a graphical element with a background and a border
    • Artifact, a dynamic or visual artifact that can be attached to an agent (e.g. smoke trail, hallow, etc…). An artifact is owned by agent.
    • A scene is the graphical layer abstraction.
    • A world provides dimensions and projection methods,
  • Service concepts
    • Agent service, maintains a list of active actions,
    • World service, accesses world instance
    • Integrator service, manages agent movement integration and force resolution,
    • Behavior service, manages local and global behaviors,

On top of those concepts, remember that we are going to work with the System.Components model-container-services interfaces: IComponent, IContainer.

Dynamic concepts

An agent has a body, a behavior, a vision and can be visualized.
public interface IAgent : IComponent, IVisualizable
{
    IBody Body { get;}
    IBehavior Behavior { get;}
    IVision Vision { get;}
}
A behavior computes the steering force and is also considered as an artifact.
public interface IBehavior : IArtifact
{
    PointF ComputeSteering();
}
A body implements the kinetics of the agent and is as well an artifact.
public interface IBody : IArtifact
{
    float Mass { get;}
    PointF Position { get;}
    float Direction { get;}
    Referential Anchor { get;}
    PointF Velocity { get;}
    PointF Acceleration { get;}
    IPointSaturator VelocitySaturator { get; set;}
    IPointSaturator AccelerationSaturator { get; set;}
    void Update(
        PointF acceleration,
        PointF velocity,
        PointF position);
}

There are some comments to do about this interface.
1. IPointSaturator is the interface defining PointF saturator. In fact, it is very important to make sure that the agents have a maximum speed!
2. Referential is the local referential attached to the agent.
3. There is one major lack in this interface, the inertia, the rotation velocity and acceleration. For the sake of simplicity, I decided to leave those aside for now.

A vision determines if an agent can see another, it has a maximum range (and is an artifact)
public interface IVision : IArtifact
{
    float MaxRange { get;}
    bool CanSee(IAgent agent);
}
An integrator integrates the motion dynamics,
public interface IIntegrator
{
    void Integrate(IBody body, PointF steering);
}
A saturator will rescale a vector or a scalar depending on the body state.

It also provides a way to determine the maximum vector norm (norm-2) depending on the “attack” angle.

public interface IScalarSaturator
{
    float Saturate(IBody body, float value);
}
public interface IPointSaturator
{
    PointF Saturate(IBody body, PointF vector);
    float GetMaxNormFromAngle(float value);
}

Visualization concepts


A scene provides the methods to render lines, circles, etc…
public interface IScene : IComponent
{
    void Prepare();
    void Flush();
    void DrawLine(Pen pen, PointF start, PointF end);
    ...  
    void PushTranslateTransform(float dx, float dy);
    ...
    void PopTransform();
}

A visualizable element can be visible/hidden and rendered,
public interface IVisualizable
{
    bool Visible { get;set;}
    void Render(IScene scene);
}
A sprite is a graphical element with a background and a border
public interface ISprite : IVisualizable, IDisposable
{
    string Name { get;}
    Font Font { get;set;}
    Color BackgroundColor { get;set;}
    Color StrokeColor { get;set;}
    float StrokeWidth { get;set;}
}

I’ve added the name for debugging reasons.

An artifact is a sprite attached to an agent (e.g. smoke trail, hallow, etc…).
public interface IArtifact : ISprite, IOwned<IAgent>
{}
public interface IOwned<T>
{
    event EventHandler OwnerChanged;
    T Owner { get;set;}
}
The world contains world dimensions and projection from/to screen.
public interface IWorld
{
    Size WorldSize { get;}
    Size ScreenSize { get;}
    PointF ScreenToWorld(PointF point);
    PointF WorldToScreen(PointF point);
    PointF ToWorldRatio{get;}
   PointF ToScreenRatio {get;}
}

Service concepts

I’ll skip the service concepts for now. They basically provide access to the different instances of neighborhood, obstacle manager, agent list, etc... For now, it’s to time have a break and a well-deserved cool beer.

posted on Friday, June 10, 2005 5:03:26 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [0]
# Thursday, June 09, 2005

   Diagnosys: In the background, ngraphviz generates a temporary dot file for each graph that gets rendered and shreds it afterwards. If you are running Ngraphviz from a folder that does not have write support, you get something like the exception below.

   Workaround: Do not run reflector from a share!

Access to the path "\\xxxxxxxx\a26d8897-1b11-4f40-8d50-301cc6503ba2.dot" is denied.

System.UnauthorizedAccessException

  at System.IO.__Error.WinIOError(Int32 errorCode, String str)
  at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share,
Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy)
  at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share,
Int32 bufferSize)
  at System.IO.StreamWriter.CreateFile(String path, Boolean append)
  at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
  at System.IO.StreamWriter..ctor(String path)
  at NGraphviz.Helpers.Dot.Run(GraphvizImageType imageType, String dotCode, String outputFileName)
  at QuickGraph.Algorithms.Graphviz.GraphvizAlgorithm.Write(String outputFileName)
  at Reflector.Graph.Graphs.InstructionGraphRenderer.Render(String fileName)
  at Reflector.Graph.IlGraphControl.Translate()
  at Reflector.Framework.InternetExplorerControl.OnParentChanged(EventArgs e)
  at System.Windows.Forms.Control.AssignParent(Control value)
  at System.Windows.Forms.ControlCollection.Add(Control value)
  at _134._1(IWindow )
  at _121._1(String )
  at _4.set_Visible(Boolean value)
  at Reflector.Framework.PackageBase.commandBar_Click(Object sender, EventArgs args)
  at _78.OnClick(EventArgs e)
  at _78.PerformClick()
  at _1.OnClick(EventArgs e)
  at System.Windows.Forms.MenuItemData.Execute()
  at System.Windows.Forms.Command.Invoke()
  at System.Windows.Forms.Control.WmCommand(Message& m)
  at System.Windows.Forms.Control.WndProc(Message& m)
  at System.Windows.Forms.TreeView .WndProc(Message& m)
  at _2.WndProc(Message& message)
  at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
  at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
  at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

 

posted on Thursday, June 09, 2005 9:11:15 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [1]
# Wednesday, June 08, 2005

The SVG viewer and NGraphviz can causes some common failure that are easy to workaround, i.e. I don't plan to fix them soon ;)

1.

 Diagnosys: The first time you run the Adobe SVG viewer, a copyright dialog pops up. After that, you will hit this failure.
 Workaround: Close Reflector and rerun. The dialog is gone and everything works fine.

Object reference not set to an instance of an object.

System.NullReferenceException

  at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
  at System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+
IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
  at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
  at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
  at System.Windows.Forms.Application.DoEvents()
  at _121._1(String )
  at _4.set_Visible(Boolean value)
  at Reflector.Framework.PackageBase.commandBar_Click(Object sender, EventArgs args)
  at _78.OnClick(EventArgs e)
  at _78.PerformClick()
  at _1.OnClick(EventArgs e)
  at System.Windows.Forms.MenuItemData.Execute()
  at System.Windows.Forms.Command.Invoke()
  at System.Windows.Forms.Control.WmCommand(Message& m)
  at System.Windows.Forms.Control.WndProc(Message& m)
  at _76.WndProc(Message& message)
  at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
  at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
  at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

2.

    Diagnosys: You don't have the necessary C++ libraries on your machine to run NGraphviz.
    Workaround: make sure msvcp71.dll and msvcr71.dll are available.

The specified module could not be found. (Exception from HRESULT: 0x8007007E)

System.IO.FileNotFoundException

  at NGraphviz.Helpers.Dot.Run(GraphvizImageType imageType, String dotCode, String outputFileName)
  at QuickGraph.Algorithms.Graphviz.GraphvizAlgorithm.Write(String outputFileName)
  at Reflector.Graph.Graphs.StatementGraphRenderer.Render(String fileName)
  at Reflector.Graph.StatementGraphControl.Translate()
  at Reflector.Framework.InternetExplorerControl.OnParentChanged(EventArgs e)
  at System.Windows.Forms.Control.AssignParent(Control value)
  at System.Windows.Forms.Control.ControlCollection.Add(Control value)
  at _134._1(IWindow )
  at _121._1(String )
  at _121._4.set_Visible(Boolean value)
  at Reflector.Framework.PackageBase.commandBar_Click(Object sender, EventArgs args)
  at _78.OnClick(EventArgs e)
  at _78.PerformClick()
  at _83._1.OnClick(EventArgs e)
  at System.Windows.Forms.MenuItem.MenuItemData.Execute()
  at System.Windows.Forms.Command.Invoke()
  at System.Windows.Forms.Command.DispatchID(Int32 id)
  at System.Windows.Forms.Control.WmCommand(Message& m)
  at System.Windows.Forms.Control.WndProc(Message& m)
  at System.Windows.Forms.TreeView.WndProc(Message& m)
  at _2.WndProc(Message& message)
  at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
  at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
  at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
posted on Wednesday, June 08, 2005 12:41:39 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [0]
# Monday, June 06, 2005
Just updated to dasBlog...
posted on Monday, June 06, 2005 4:23:26 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [1]
# Wednesday, May 25, 2005

A while ago, I mentioned I was planning to build an autonomous agent framework on .NET (this was in fact a long time ago). It’s time to wake this project up with a shiny description:

NSteer is a framework for simulating of autonomous agents for .NET.

Until we get down to the code, here is a little snapshot to get you hooked:

 

A bit of history: boids, flocks, herds, etc…

In 1986, Craig Reynolds wrote a little application that simulated bird group behaviors. The term Boids was born. The beauty of his approach was that the “bird” behavior was not created from some complicated mathematical equation, it all boiled down to 3 simple rules that each agent would follow:

  1. Separation: avoid neighbors,
  2. Cohesion: go towards the group center,
  3. Alignment: align with group velocity.

Since then there has been a lot action in this field and you can see applications of this approach in a lot of the blockbuster hits. Check out Craig’s web site for an impressive list of links to papers (I love this one), samples, frameworks and movies…. It was about time to bring .NET into the play.

Laying down concepts

Let’s start by identifying the concepts in our problem that we can later translate into interfaces:

  • An Agent describes an independent entity that lives in a World. An Agent has a Vision, a Body and a Behavior
  • A Body defines the cinematic properties of the agent (position, acceleration, etc…)
  • A Vision defines the region where the Agent can “see” obstacles or neighbors
  • The Behavior of the Agent will yield a steering force that will move the Agent. This is the “brain” of the agent.
  • Agents are confined in a World.
  • A Simulator that uses an Integrator to integrate the dynamics of the system.

We will also need ways to access the list of Agents, Obstacles, etc… To do so, I chose to use the Service – Component – Container pattern where different services give access to the agents, obstacles, etc...

In .Net, this pattern is already part of the framework. The IComponent, IContainer interfaces are defined in System.ComponentModel and there already exists base class such as Container, Component, ServiceContainer to get started.

For a start, we will define the following services:

  • AgentService, gives access to the Agents,
  • WorldService, gives access to the world dimension and properties,
  • ObstacleService, gives access to the obstacles in the world,
  • NeighorhoodService, let agent query about neighboring agents

At last, we need some concepts for visualization:

  • A Sprite is a 2D drawing with background color, foreground color, etc… It can be hidden or made visible.
  • A Scene is an abstraction of GDI+,(just in case I had the courage to implement the framework using DirectX or XAML).

There might be a couple more going down the road, but this looks like a good starting point. I’ll see on the next post how we lay down the interface and maybe get ready for testing.

posted on Wednesday, May 25, 2005 8:35:00 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [0]
# Sunday, May 22, 2005

All downloads recompiled against Reflector 4.1.52.0 available at http://www.projectdistributor.net/Groups/Group.aspx?groupId=34 .

Enjoy :)

posted on Sunday, May 22, 2005 7:49:00 PM (Pacific Daylight Time, UTC-07:00)  #    Comments [2]
# Friday, April 22, 2005

Andrew Stopford has agreed to take over the MbUnit and keep it running. This is a very good news for the project. Hopefully, MbUnit can recover a short bug fix cycle and shape up its integration with CruiseControl.NET or Draco.

Good luck Andrew !

 

posted on Friday, April 22, 2005 11:25:00 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [2]
# Friday, April 15, 2005

To whom it may concern:


As many of you probably know, I am now working at Microsoft in the CLR JIT testing team.  As my job load and duties are filling up more and more, I am unable to continue to contribute at a rate that best serves this project.  From this point forward please consider Jamie Cansdale (www.testdriven.net) as the sole owner (work/responsibilities/fun) for the MbUnit project.  I’ll of course be around occasionally.


Cheers,
Peli

posted on Friday, April 15, 2005 9:30:00 AM (Pacific Daylight Time, UTC-07:00)  #    Comments [15]
# Sunday, January 09, 2005

This is a rather funny application of unit testing. Let me state the problem:

We need to write a wrapper for every type that inherits from C. To make sure we don't miss any, we start by writing a combinatorial test that makes sure every type is mapped with a wrapper type*.

[TestFixture]
public class WrapperTest
{
    // get all the types assignable to C, from the assembly SomeAssembly
    [Factory(typeof(Type))]
    public IEnumerable<Type> Types()
    {
        foreach(Type type in SomeAssembly.GetExportedTypes())
        { 
            if (typeof(C).IsAssignableFrom(type))
               yield return type;
        }
    } 

    // tests that all types have a wrapper
    [CombinatorialTest]
    public void EnsureWrapper(
        [UsingFactories("Types")]Type type
    )
    {
        Assert.IsTrue( IsWrapped(type) );
    }
    
    // returns true of the wrapper exists
    private bool IsWrapped(Type type)
    {...}
}

Ok, we can run this test and see a lot of failures. So now we know wich wrapper has to be written.... but wait, why not making the test generate the wrapper as well.

    [CombinatorialTest]
    public void EnsureWrapper(
        [UsingFactories("Types")]Type type
    )
    {
        if (IsWrapped(type))
            return;

        // wrapper not found, 
        // generating the code and dumping it to Console.Out
        ...

        Assert.Fail("Wrapper not found");
    }

Now, we have test cases that give us the fix in case of a failure :)

*: this could be done by a FxCop rule as well since it is pure static analysis.

posted on Sunday, January 09, 2005 4:13:00 PM (Pacific Standard Time, UTC-08:00)  #    Comments [5]