![]() |
| Script Doctor 2: Barcelona, Beckham |
|
It seems that Barcelona is cool since long time ago: when an 11-year-old David Beckham won a contest organized by Bobby Charltons Soccer Schools, the prize was a two weeks stay in Barcelona. If its good for Mr. Beckham, it should be good for you too! I have tried to mix David story with Python with no success, the closer possibility could be Victoria Beckham due to the garlic story (look in the References section), but we are too glamorous to do that. Todays example will show how to split an O/D matrix in two, modifying the trips by applying a multiplier factor. This can be easily done using the O/D matrix editor but the script will show you how to access the matrix data and how to create new objects. Matrix SplitMatrices are organized in Centroids Configurations and they hold the number of trips between origins and destinations for one vehicle type and for a time period (for example car from 8:00 AM to 9:00 AM). The example is divided in three parts:
Declaring variables at the top of the code is a good idea so you can easily apply the script to other matrices. The code is: The code to find the matrix and to call the split function is: We look in the catalog for the matrix and, in the if line, we test that we have an object (matrix != None) and that it is really a matrix (matrix.isA( "GKODMatrix" )). None will be returned if no object exists with the supplied identifier. Even if the object is not None this doesnt mean that it is a matrix so we have to test it to be sure. The Split FunctionThe last part of the script contains the function that does the split. Well, in fact, it is not the last part of the code as the function will be written between the variable declaration and the code that calls the function. The function header is: The split function is divided in two: it creates both matrices and it puts the trips on them. The code to create the matrices is: It looks for the centroid configuration that holds the original matrix as it will be the one where we will add the new matrices. The code is easy to read. The only strange point is the way used to create objects in AIMSUN NG, we use the newObject method found in the GKModel class. We cannot create an object directly as some pre and post actions are needed. My name is Slim ShadyThe newObject method requires the name of the class that we want to instantiate. If the answer is GKODMatrix the question is, how do I know the name of the class for a particular type? This information is found in the Types Window (look in the Window menu, then in the Windows submenu). ![]() Types Browser The Types Window lists the class names both as presented to the user (OD Matrix) and as used when coding in AIMSUN NG (GKODMatrix). Now that we have the matrices created and added in the proper centroid configuration we will set the trips for each one. In the References you will find the complete code. Remember that you can paste the code in the Script Editor either using the context menu or pressing Control+V shortcut. References |