In addition to this, there is a third variety of variables called a table field. This can hold anything except nil including functions. It is not likely to be used however in cost functions.
Comments
Comments can be added by using –[[text]] instead of ‘’’ OR – instead of #
e.g. Lua
–[[This is a comment]]
e.g Python
#This is a comment
In Lua # instead gives the length of a variable e.g. #comment -> 7
Arrays
In Python, the position of objects in a list starts at 0. However, in Lua the convention is that arrays start with index 1.
e.g. Lua
list=[0,1,2]
list[1] = 0
Python
list = [0,1,2]
list[1] = 1
Functions
In Lua to define a function you also need to add an end statement at the end of the function. E.g.
function cost(turn)
calculatedcost = turn:length2D()
return calculatedcost
end
Note that when a cost function is coded in Lua language, Lua does not allow a variable name to be the same as the name of any function inside the cost function.
Classes
In Python classes are used to define a group of functions which act on a particular object. For instance turn.getName() runs the function getName for the object turn for which the function has been defined. In Lua, you need to instead use a : e.g. turn:getName().
Example of a Lua Dynamic Cost Function for Aimsun Next
Let’s now put all the above in practice by writing a full cost function that computes a generalised cost by taking into account travel time, tolls and operating cost.
<<code>>
function distance(context,manager,section,turn,travelTime)
linkdistance = turn:length2D()+section:length2D()
return linkdistance
end
function cost(context, manager, section, turn, travelTime)
— link travel time
linkcost = turn:getStaTravelT(manager)
— add attractiveness term
linkcost = linkcost * (1+manager:getAttractivenessWeight()*(1-turn:getAttractiveness()/manager:getMaxTurnAttractiveness()))
— add user defined term
linkcost = linkcost + manager:getUserDefinedCostWeight()*section:getUserDefinedCost()
VOT = 30 –pence per minute
VOC = 20 — pence per km
linkdistance=distance(context, manager, section, turn, travelTime)
— make generalised cost
linkcost = linkcost + (linkdistance *VOC/VOT) +(section:getUserDefinedCost()/VOT)
return linkcost
end
<<code>>