Disim Highway Simulator/Creating an infrastructure controller

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Simple Example[edit | edit source]

Disim allows the creation of totally arbitrary infrastructure control without having to modify its internal code. To illustrate this, let us start by a simple example: imagine that a ramp meter is present on the network and we want to increase the entry rate on the corresponding ramp when the density of the traffic measured by a sensor on the same highway is too low. The script would look like this:

-- Actuator
ramp_meter = nil
-- Sensors
density_sensor = {}
-- Variables
K = 0.1
optimal_density = 60
tprev = 1
last_change = 0
dt_red = 5
dt_green = 3

function init(self)
  ramp_meter = self:getRoadActuator("rampmeter")
  density_sensor[0] = self:getRoadSensor("densitysensor1")
  density_sensor[1] = self:getRoadSensor("densitysensor2")

function update(self, t, dt)
  -- Setting the ramp meter light
  last_change = last_change + dt
  if (ramp_meter:getColor() == RED and last_change > dt_red) then
    last_change = 0
  elseif (ramp_meter:getColor() == GREEN and last_change > dt_green) then
    last_change = 0

  -- Aggregation of data happens every minute
  if (t - tprev < 60) then
  tprev = t

  density = density_sensor[0]:getValue() + density_sensor[1]:getValue()
  dt_red = dt_red + (density - optimal_density)*K
  if (dt_red < 0) then dt_red = 0; end

function destroy(self)
  -- Nothing to do here

What is the scripting language?[edit | edit source]

This script was made using LUA (http://www.lua.org/manual/5.1/manual.html): Lua is an extension programming language designed to support general procedural programming with data description facilities. It also offers good support for object-oriented programming, functional programming, and data-driven programming. Lua is intended to be used as a powerful, light-weight scripting language for any program that needs one. Lua is implemented as a library, written in clean C (that is, in the common subset of ANSI C and C++).

Disim allows anyone to create and redefine the infrastructure behavior using LUA as a scripting language. Creating your infrastructure controller in LUA has several advantages: compilation is not needed, a simplified API is available and you benefit from all the LUA capabilities (tables, anonymous functions, dictionaries, ...). As we can observe from the example above, each script should provide the following 3 functions:

function init(self)

This function takes as input a single variable. This variable is a LuaInfrastructure pointer on the current highway network. This function does not return any values, but it might be useful to save some pointers on the different sensors and actuators in the network that we plan to use.

function update(self, t, dt)

This function takes three variables as input:

  • self: Just like the input function, this is a pointer to the infrastructure controlled.
  • t: This is the absolute time elapsed since the beginning of the simulation.
  • dt: This is the time-step duration in seconds since the last call to that function.

This function does not return any values.

function destroy(self)

This function takes as input a single variable. This variable is a pointer on the current infrastructure. In this function you should clean up any variable that has been initialized by init.

The API[edit | edit source]

As seen above, all the functions provided take as an argument a pointer to the current infrastructure being controlled. This pointer describes an object in LUA: the LuaInfrastructure. To see what you can do with this object, check out the LUA API of Disim available here.

What's next?[edit | edit source]

In the next section, we are going to see how one can combine all that we have seen so far to gather data from a fictitious highway.