It is All about Traffic, and Programming
Search
Archives
0 visitors online now
0 guests, 0 members

Matlab

Running VISSIM from within MATLAB

Vissim has a very nice COM-based programming interface – that a user can use any COM-compliant languages (as COM client)  to programmingly invoke Vissim (as out-of-proc COM server),  and performs some customized task.  Typically,  a user can use Excel/VBA, C#, Java,  Fortran, Delphi, C++ etc as his preferred language.

Matlab is a comprehensive computation platform with a lot of readily available numerical algorithms (e.g., Genetic Algorithms,  Support Vector Machine, Wavelet, Kalman Filtering etc.)  that can be very useful in simulation studies, research and development on customized and sophisticated simulation experiments.

Matlab provides very good support for COM – hence it is, in my opinion, probably the best Vissim COM programming environment if you have an academic minds and wish to do some cool prototypes and interesting simulation experiments.

  • For industry applications,  it is probably an overkill,  and the performance hit is a problem – Matlab essentially works the same way as Excel/VBA – this means all method invocations are “interpreted” – the stack frame is created by first querying a dictionary of method names to get the right function pointer,  amongst the mess of the inter-process marshaling.   C++, C# or any compiled language would be much faster as the stack frame is created directly from function pointer without the  querying step.  Wait – strictly speaking, C# is a bit different from other native language like C++, Delphi, or Fortran, as .NET/COM inter-op is via Common Language Runtime (CLR)’s P/Invoke which is another layer, rather than the pure native way.

Vissim User Manual doesn’t mention how to use Vissim COM wih Matlab.  It is not complicated but there is some catch to make it work, so I just put the procedures here – and,  if you find it helpful,  a loud “Thank You” would make me HAPPY!

First thing first,  in order to run Vissim COM,  you would need to know Vissim’s ProgID, which is a string-based name saved in Windows system registry, together with some other information (such as the path to Vissim.exe).  Here is an excerpt you need to read carefully from Vissim COM manual:

When working with Vissim  manually it is possible to start several instances of Vissim. You
can  also  start  several  different  versions  (e.g.  Vissim  5.40  and  Vissim  6.0).  The  same
applies  when  working  with  COM:  If  the  version  number  is  not  specified  (i.e.
CreateObject (“Vissim.Vissim”)),  the  Vissim  version  which  has  been  registered  as  COM
server most recently is started.  Specifying the version number during the call allows you to
start  a  specific  version  of  Vissim:  CreateObject (“Vissim.Vissim.540”)  for  Vissim  5.40  or
CreateObject (“Vissim.Vissim.600”)  for Vissim 6. If you have installed a 32-bit and  a  64-bit
edition,  you  can  use  “Vissim.Vissim-32.600”  or  “Vissim.Vissim-64.600”  to  start  a  specific
edition. A Vissim instance started via COM is automatically closed as soon all COM objects
are set to nothing.

This is kind of twisty, so let me rephrase it, with some simplifications:

  • If you only have one Vissim version installed, ProgID = “Vissim.Vissim”
  • If you have multiple Vissim version installed, ProgID = “Vissim.Vissim.XXX”, where XXX is the version number, e.g., 540 for version 5.40, or 600 for version 6.00. Please note,  service packs doesn’t count for version number, so service pack 1, 2, … n for the same version have the same ProgID
  • If you have multiple Vissim versions,  for 32bit and 64bit, then the ProgID = “Vissim.Vissim-YY.XXX”, where YY is 32, or 64, corresponding to 32bit or 64bit edition, XXX is the version number such as 540, or 600.

With ProgID in mind, and a Vissim COM interface online manual in front of you,  now you need to

  • Launch Matlab (in my example I am using Matlab 2013b), but I believe any Matlab version after 7.0 will do;
  • In the Matlab Command Window, type
    vissim = actxserver('Vissim.Vissim.600')

    Note here a ProgID string ‘Vissim.Vissim.600’,  with an explicit version number 600 is used. This is because on my computer I have two Vissim versions (540 and 600) installed. actxserver is a Matlab command that returns the default interface of the COM server, which in our case,  is IVissim interface.

  • Immediately you will see a Vissim 600 instance being launched automatically, and the main GUI shows up;
  • The Matlab variable, “vissim” is a handle to IVissim interface. From this point on,  you can use “vissim” to call various other interfaces, for example,
    vissim.LoadNet(‘whatever.inpx’), vissim.Simulation.RunContinuous, simulation = vissim.Simulation
  • You can do whatever you like now with Matlab,  including invoke  Genetic Algorithm routines or something else;
  • Finally, when you are done,  to destroy “vissim” (which is a reference-counted handle of IVissim interface),  simply put this line
    vissim = 0,  or alternatively,  vissim.Release

    then the vissim instance will be destroyed (to be more accurate  – released to decrement ref_count,  and destroyed if ref_count drops to zero) and the main GUI disappears from the screen.

Last, there are two little tricks working Vissim in Matlab environment , you can use .get to list all properties of an interface, and .methods to list all methods of an interface, for example:

You can immediately recognize that these are all listed in Vissim COM Manual – but it is good to verify by using .get  and .methods.

********************

UPDATE –

I found another good source of information on this subject is a white paper of Dr. Tamas Tettamanti, which provides a more detailed step-by-step document for Vissim-Matlab integration –

http://www.researchgate.net/publication/258513223_The_beginning_steps_of_VISSIM_COM_programming_with_MATLAB_script

 

Aimsun COM Interface (3) – Matlab example

Matlab is a very powerful computational platform that runs on multi-platforms, including Windows, Mac-OS, Linux… you name it.

On Microsoft Windows, Matlab provides pretty good support for COM automation. So today, let me illustrates a little Aimsun COM sample using Matlab.

When using Aimsun COM under Matlab environment, all the automation objects are accessed by the relevant IDispatch interface.

Here is the first line that creates a reference to the Aimsun Application object. ‘angcomsvr.AimsunServer’ is the progID, also in Matlab the string is single quoted, just like Pascal.

aimsunApp = actxserver(‘angcomsvr.AimsunServer’);

After creating the reference, we need to initialize the object by:.

aimsunApp.InitAimsunApp(0);

The integer parameter 0 will be marshalled as a boolean false, indicating that Aimsun is going to be started as “non-console”, i.e., GUI model.

Now that we have successfully initialized Aimsun App, we can now to whateve we like, e.g., open an network, generate new demand or run the simulation. All of these through line by line Matlab code, just like what you can usually do with Aimsun’s default Python scripting.

aimsunApp.OpenNetwork(‘D:\Network\whatever.ang’);

Or you can have a little fun by maximizing, minimizing, reposition the main Aimsun window:

aimsunApp.ShowMaximized;
aimsunApp.ShowMinimized;
aimsunApp.SetWindowPosition(0, 0, 150, 150);
aimsunApp.Run;

In order to close the Aimsun Application, use

aimsunApp.release

This will decrement the reference count and since the Aimsun COM is implemented as multi-instance COM server, decrementing the reference count by 1 will immediately drop it to 0, hence deleting the object from memory.

That is it. Next I will illustrate an Excel sample. Then I’ll show more complicated simulation scenario using Pascal.

Aimsun COM Interface (1)

As I have pointed out in a previous article, Aimsun is a cross-platform application that runs on Linux, MacOS and Windows. COM is a Windows platform-specific technology, so naturally Aimsun itself does not have COM/Automation support. Nor I think TSS ever wants to put that in their list of buzz words.

But sometimes, we do find COM/Automation convenient, especially when we have the need to interface Aimsun with other programming languages such as VB/VBScript, Excel, MATLAB, Delphi, FORTRAN, C#/.NET etc.

Hence the start of the development to implement a COM interface for Aimsun.

It is not a simple wrap-up of Aimsun SDK functions.

The challenge is I don’t have access to Aimsun source code, neither does Aimsun’s native development language, i.e., Qt/C++ seem to have a stable support for COM/Active X development, at least with ActiveQt current release.

So finally I chose to resort to Windows Operating System API and COM SDK directly. Therefore, this Aimsun COM interface is purely Window specific, and based on the following packages:

  1. Aimsun SDK (needs special license from TSS)
  2. Windows Operating System APIs (WIN32 API)
  3. Windows COM SDK
  4. A little tweak of the compiler generated C++ code and manipulation of the v-table of some C++ classes.

Currently this Aimsun COM interface has the following specifics:

  1. Dual Interface supporting both IUnknown and IDispatch, with the latter allowing using popular scripting language VBScript, JavaScript, MatLab etc.
  2. Earlier binding so strong typed languages such C++, Delphi, FORTRAN languages can interface Aimsun kernel without runtime performance loss.
  3. Interop with .NET by COM-Interop/P-Invoke

This Aimsun-COM implements the following custom Interfaces, and I plan to add more if I have time:

  • IAimsunInstance
  • IAimsunDemand
  • IAimsunExperiment
  • IAimsunMicroSimulator
  • IAimsunMOE
  • IAimsunNetwork
  • IAimsunScenario
  • IAimsunSignalControl

Anyone with some experienced with VISSIM COM might find the above a little familiar.

That is right. The idea is the same, i.e., to hide un-necessary class information and publish everything as interfaces and properties, while allowing the user to access the functionality using his favorite language, be it VB, Delphi, Java, .NET or whatever language that is COM-compliant.

Another good manifestation of this idea is ArcGIS’s ArcObjects.

But be reminded that .NET(/C#) interop with Aimsun-COM is via COM-interop/P-Invoke, the internal mechanism is a little different from native COM language such as C++ or Delphi.

After registration of new Aimsun COM server, the system Registry has the following added entry (Double click the figure to see):

As you can see, this Aimsun COM server has a ProgID “angcomsvr.AimsunServer”, and a GUID is {04B0631C-0A6A-49E6-AB17-845A31F79B74}. The default interface is IAimsunInstance.

Keep this information in mind, because we are going to use it later with various examples.

In the following days I will demo this Aimsun COM interface with Delphi, C# and MATLAB samples. And if I have time, I am also going to show the interaction of Aimsun COM with Internet Explorer, and demonstrate some interesting (toy) web applications with it.