development.gif
Address

Passeig de Gràcia 12
08007 Barcelona (Spain)
Tel. (+34) 933 171 693
Fax. (+34) 933 171 716

Script Doctor 1: Barcelona, Python

Python and Barcelona are cool. Really. I read about it on an ACM Queue article where Kode Vicious recommends both, although for different reasons. This column is more about Python and less about Barcelona. The later requires a first hand experience so stay tuned for our training course offers.

Moving back to coding, AIMSUN NG offers full access to its innermost core using Python. That means that any operation that you can do using the User Interface (and others that are not possible) can be done with a program. Today I will introduce you to this particular feature and, in follow-up articles, I will show you some of the amazing things that you can accomplish with a few lines of code.

The Basics

Let’s do the classical first step: the Hello World example. To do so, create a new Python Script from the Site menu. Then edit it and write the following line in the code area:

To execute the code press the Execute button (or, after closing the dialog, the Execute option in the script’s context menu).

AIMSUN NG Script Editor
AIMSUN NG Script Editor

Now it’s a question of reading tons of documentation on Python and about AIMSUN NG to do whatever you want. This column, nevertheless, will help you in the digging process offering ready to use samples and showing the best methods to accomplish the more usual tasks.

A Catalog of Everything

The current model, that is, the currently open network, contains a catalog that holds, sorted by type, all the objects in the network (either graphical or not). Lots of operations require either to access all the objects of a type (e.g. to modify all the section speeds) or to find an object using its identifier (e.g. to find experiment 100 in order to change its parameters).

Let’s see a simple example that shows the catalog usage:

First, we will get the section type, called GKSection. Note that the model variable is automatically initialized with the model where the script is being executed. In a loop we will iterate over all the sections in the model and we will print their identifier and name.

The getObjectsByType method returns a dictionary so we have to add the extra call itervalues to iterate over the object in the map instead of the keys.

If you want to reduce the speed of all the sections by a 10%, then, instead of the print line, you should write:

The other possible use of the catalog is to locate an object either by name or by identifier. For example, to get section 200:

And to find it by name:

What’s coming next?

In the next articles you will learn how to export and import O/D matrices, how to simulate lots of replications changing the experiment parameters and how to automatize the creation of visualization styles. Feel free to with your doubts and suggestions. In the meanwhile review the scripting documentation and run for a good Python book.

References


© 1998-2008 TSS-Transport Simulation Systems