MATLAB AIMSUN

download MATLAB AIMSUN

of 12

Transcript of MATLAB AIMSUN

  • 8/10/2019 MATLAB AIMSUN

    1/12

    Department of Adaptive Systems

    UTIA, AV CRP.O.Box 18, 182 08 Prague 8,Czech Republic

    RESEARCH REPORT

    Petr Gebousky

    AIMSUN MATLAB

    Interface Toolbox

    Users Guide

    Version 1.0 November, 2005

    MD CR 1F43A/003/120, MSMT 1M0572

    Tel: +420-266052061 Fax: +420-266052068 E-mail: [email protected]

  • 8/10/2019 MATLAB AIMSUN

    2/12

  • 8/10/2019 MATLAB AIMSUN

    3/12

    Abstract

    This text is the basic users guide for the manipulation with the toolbox implementing inter-

    face b etween AIMSUN and MATLAB. Besides the basic instructions, some technical details of

    implementation are outlined.

    1 Aim of Toolbox

    The aim of AIMSUN MATLAB interface toolbox is to implement the interface between MATLABenvironment and AIMSUN simulator for the purpose of testing of designed traffic control of specialtype.

    The AIMSUN simulator [2] is powerful tool for simulation of traffic flow while the MATLAB en-vironment [1] provides the rich set of functions that simplify the process of design, optimization andtesting of developed traffic control. Toolbox combines together advantages of both of them.

    This text presume the knowledge of AIMSUN and TEDI manipulation.

    Acknowledgment: Creation of this toolbox was partially supported by the grants RIZDOP03 (MDCR 1F43A/003/120) and DAR (MSMT 1M0572).

    2 Specificity of Toolbox

    Toolbox allows to realize special type of macroscopic control with the fixed control/sampling period.It comes out from the goals of the project. Its aim is to develop coordinated traffic control on the widearea with tens of controlled junctions. Basic prerequisite is relatively long control/sampling period(about minutes) for gathering the data on the level of network of junctions and subsequent applicationof control actions.

    The simple loop detectors are considered to exist in the traffic network only. Therefore counts andoccupancy from the detectors can be gathered only.

    The chosen sampling period of coordinated traffic control is equivalent to the period of switching(synchronization) of software control between MATLAB and AIMSUN. Therefore switching of controlbetween them are not realized in each simulation step, but only periodically determined by the samplingperiod. It helps to minimize an increase of time necessary for simulation run spent on synchronization.

    Counts of passing vehicles and occupancies on detectors in each sampling/control period are accessi-ble from MATLAB environment. Functions of the toolbox then allow to change timing of phases of thepredefined control plan on all controlled junctions in the network. Allowed control actions are only thechanges of time of phases. It means that both the rates of phases and the length of cycle of control plancan be changed. But it is not possible to switch the order of phases. It correspondes to reality, wherethe optimal ordering of phases is determined, timing of phases is varying according the conditions only.

    A special complement of the toolbox is possibility to acquire average and maximal queues per lanein the sections of traffic network. It is implemented for the purpose of testing of queue length estimator

    from basic detectors data. Estimates of queue length serve as input to the controller under developmentin this main project.The latest version of toolbox also offers the possibility to insert number of vehicles on entrances of

    network directly from MATLAB, when the so called AIMSUN results containers are used. Generally,both results containers (vehicle entrance numbers + turning rates on junctions) and OD matrices canbe employed in AIMSUN together with this toolbox. Though, the specification of number vehicles onentrances directly from MATLAB works only for result containers now. The reason is preffered resultcontainers for definition of traffic flow.

    2

  • 8/10/2019 MATLAB AIMSUN

    4/12

    3 Contents of Toolbox

    Toolbox contains two main parts necessary to implement the connection between MATLAB environmentand AIMSUN: general shared dynamic library and set of MATLAB functions.

    3.1 General Dynamic Library

    The dynamic library forms the core of the implemented interface. It has to be linked both to theAIMSUN and MATLAB before running simulation.

    Interface and synchronization between MATLAB and AIMSUN are implemented by means of sharedfiles in the Windows pagefile. The shared data are represented by the C structures, where all the usedarrays are static. The bounds are fixed and sufficiently high (see Table 1). If it would not suffice, it ispossible to recompile the library with the new bounds.

    Detectors Junctions Sections Phases of Junctions Vehicle types100 100 100 16 10

    Table 1: Fixed limits for number of elements in traffic network

    3.2 MATLAB functions

    Toolbox includes set of MATLAB functions that realize link and unlink of general dynamic library,synchronization with and access function for obtaining and changing the information in AIMSUN. Allfunctions are listed bellow:

    after error export phases export vehicle flows

    function info generate input flows get actual time

    get data get detectors get entrance sections

    get junctions get lock get sampling time

    get sections get simulation time get statistics time

    get total statistics get veh types load final information

    load ini information load library load step information

    return activity select random generator set vehicle rates

    sort hash start AIMSUN unload library

    Detailed information about functions is given in the Appendix A.In addition to the functions directly used in interface, the toolbox contains also few functions that

    simplify and automate building of ASCII file, that defines the result container. Such created file can beimported to the TEDI editor of AIMSUN package thereafter. These functions are:

    create area inflow create junction create vehicle

    generate traffic result

    4 Usage of Toolbox

    Manipulation with the prepared interface toolbox and AIMSUN simulator has to follow some basicrules described hereafter. Set of necessary instructions is divided into three groups according the placeof their application.

    3

  • 8/10/2019 MATLAB AIMSUN

    5/12

    4.1 Before the First Usage of Toolbox

    After instalation of the toolbox (directory), the path has to be set to this directory in MATLAB. Besidesthe path to the AIMSUN directory (which contains file aimsun*.exe) has to be modified in function

    start_AIMSUNto the actual one.

    4.2 Preparation of Simulation Experiment in AIMSUN

    Before the running simulation in AIMSUN with the realized MATLAB interface, it is necessary toprepare simulation experiment in AIMSUN. Such experiment has to be open and ready in AIMSUN oreven better, saved as AIMSUN scenario. The saved scenario can be then simply called directly fromMATLAB using function start_AIMSUN. Such simulation experiment (scenario) comprehends:

    Loaded traffic network.

    Set simulation time of experiment.

    Loaded traffic result or O/D matrices. Suitable generator of vehicle entrances for both of themhas to be selected. In case of employment of ASCII files representing the result container, it hasto be imported in TEDI editor first of all. It is visible from AIMSUN then. It is recommended todelete result container of the same name before importing new one from ASCII file.

    If it is planned to use the possibility to generate vehicles directly from MATLAB , all states ofresult container should define only turning rates on junctions. Vehicle entrances should stay zero.In this case, the random generator is selected from MATLAB.

    Loaded predefined control plan with described phases on each junction. The control plan has toapplied during the whole time of simulation. The control on individual junctions in this controlplan has to be set to external(if external control is supposed) and the lower and upper boundsof times of phases selected reasonably, as only then it is possible to change these times fromMATLAB within these bounds.

    Loaded dynamic library (*.dll), that is a part of toolbox, by means of extensions.

    Set the aggregated detection time to the desired sampling/control period. It corresponds theperiod of synchronization with MATLAB.

    If measurement of queue lengths are in demand, time interval of the periodic statistics gatheringhas to be set to the same value as sampling/control period and items determining that statisticsdata from all sections selected.

    4.3 Structure of MATLAB Script

    Basic structure of a m-script that employs developed interface should follow the blueprint saved in_default.m listed bellow. Starting of such script realizes the simulation experiment with conected

    interface.The blueprint contains instructions in the form of comments. Positions, where the modifications

    according the actual conditions and simulation experiment have to be applied are labelled (OPTIONAL)or (SWITCH). Further information about individual functions can be found in function references, seeSectionA.

    % basic blueprint of m-script realizing the AIMSUN simulation with employed interface toolbox

    %####################################

    4

  • 8/10/2019 MATLAB AIMSUN

    6/12

    % Preliminary block

    %####################################

    disp(AIMSUN simulation can start...);

    %***************************************************%** **% (OPTIONAL)% calling of saved AIMSUN scenario from MATLAB% (1- console, 2- GUI with visualization 3- GUI without visualization)% (the alternative is to run scenario directly from AIMSUN. If so, after% running the m-script, the simulation has to be started in AIMSUN)

    start_AIMSUN(C:\AIMSUNpr\smichovNEW.sce,1);%** **%***************************************************

    load_library;disp(Waiting for AIMSUN simulation initialization...);while (get_lock == 0)end

    %+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++% AIMSUN initialization information load and first prior setting% of junction phase timing

    if (get_lock ==1),load_ini_information;

    sim_time=get_simulation_time;sam_time=get_sampling_time;[n_detectors, ID_detectors] = get_detectors;[n_sections, ID_sections] = get_sections;[n_junctions, JunctionNphases] = get_junctions;[n_vehicle_types] = get_veh_types;

    % use ONLY if vehicles will be entered from MATLAB[n_entr_sections, ID_entr_sections]= get_entrance_sections;

    %***************************************************

    %** **%(OPTIONAL)% selection of detectors from which the counts and occupancy will be% gained in this order. Each row of matrix define, data of which% detector will be on this position. If the rows of matrix contain more numbers of% detectors the data will be aggregated. Superfluous positions should contain NaNdetectors=ID_detectors;%** **%***************************************************

    %***************************************************%** **%(OPTIONAL)% selection of sections from which the queues will be% gained in this order. Each row of matrix define, data of which% section will be on this position. If the rows of matrix contain more numbers of% sections the data will be aggregated. Superfluous positions should cantain NaN

    % sekce=[ 4 42; % Zborovska vstup

    % 40 NaN; % Korenskeho% 1 NaN; % Zborovska (Vbotanice)% 2 3; % V Botanice% 8 NaN; % Zborovska (Matousova)% 5 12; % Matousova% 37 NaN; % Zborovska (Lesnicka)% 13 NaN; % Zborovska (Matousova) -protismer% 33 NaN; % Lesnicka% 38 NaN; % Zborovska (Pechackova)% 36 NaN; % Pechackova% 9 NaN; % Zborovska (Lidicka)% 16 NaN; % Lidicka

    5

  • 8/10/2019 MATLAB AIMSUN

    7/12

    % 21 NaN; % Lidicka (od Motola)% 32 NaN; % Svornosti (Na belidle)% 14 NaN; % Svornosti (Lidicka) -protismer% 31 NaN; % Na belidle% 29 NaN; % Svornosti (Vrazova)

    % 28 NaN; % Vrazova% 25 NaN; % Svornosti(Jindricha Plachty)% 10 NaN; % Svornosti (Vltavska)% 6 22]; % Vltavska

    sections=ID_sections;%** **%***************************************************

    %***************************************************%** **% (OPTIONAL)% the choice of the order of junctions, for which phase timing will be set% vectorjunctions = JunctionNphases(:,1);%** **%***************************************************

    %***************************************************%** **% (OPTIONAL)% IF vehicles will be entered from MATLAB% the choice of the order of entrance section, for which the number of vehicles% will be inserted in this order% vectorentrance_sections = ID_entr_sections;%** **%***************************************************

    % (SWITCH)% 1. if vehicles are not entered in MATLAB[seq_det, seq_sec, seq_jun]=sort_hash(detectors, sections, junctions);

    % 2. if vehicles are entered in MATLAB[seq_det, seq_sec, seq_jun, seq_esec]=sort_hash(detectors, sections, junctions, entrance_sections);

    % (OPTIONAL)%??? Generate vehicles from MATLAB? 0/1generate_input_flows(0);

    % (SWITCH)%??? Which random generator to use to enter vehicles (0-normal, 1-constant, 2-uniform, 3-%exponential (only when vehicles are entered from MATLAB)select_random_generator(0);

    %####################################% initialization block

    %####################################

    % (OPTIONAL)% IF the vehicles are generated from MATLAB

    %??the rate of vehicle types in all flows in network

    %the vehicle types are listed in AIMSUN (X)console% (the vehicle types has to be specified in result container)vehicle_rate=ones(1,n_vehicle_types)/n_vehicle_types;set_vehicle_rates(vehicle_rate);

    % ??number of vehicles entering the entrance sections in selected order% during the (detector) sampling periodvehicles =zeros(1,n_entr_sections);export_vehicle_flows(vehicles, seq_esec);

    6

  • 8/10/2019 MATLAB AIMSUN

    8/12

    % (SWITCH)% if to set the junction phase timing

    % 1. setting the length (seconds) of phases on individual controlled% junctions

    %***************************************************%** **% timing for individual junctions (rows) in selected order of junctions% the order of phases (and interphases) have to be read from AIMSUN.% The matrix should have at least number of columns = maximal number of% phases for all junctions

    phase=[ 20 4 52 4 0 0;20 32 4 20 4 0;27 5 26 4 13 5;31 5 40 4 0 0];

    %** **%***************************************************export_phases(phase, seq_jun);

    % 2. return control to AIMSUN without phase timing settingreturn_activity;

    while (get_lock == 0)end

    end% initialization end%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    %+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++% simulation stepdisp(AIMSUN simulation is running...);while (get_lock ~= 2)

    load_step_information;%

    %ziskani pozadovanych dat v maticich prislusnem poradi%pro detektory jsou v prvnim sloupci pocty projetych aut, v druhem

    %obsazenosti v procentech, ve tretim rychlosti%pro delky front pak prvni sloupec vraci prumernou druhy pak maximalni delku kolony na pruh[data_detectors,data_sections] = get_data(seq_det,seq_sec);

    %####################################% simulation step block

    %####################################

    % (OPTIONAL)% IF the vehicles are generated from MATLAB

    %??the rate of vehicle types in all flows in network%the vehicle types are listed in AIMSUN (X)consolevehicle_rate=ones(1,n_vehicle_types)/n_vehicle_types;set_vehicle_rates(vehicle_rate);

    % ??number of vehicles entering the entrance sections in selected order% during the (detector) sampling periodvehicles =zeros(1,n_entr_sections);export_vehicle_flows(vehicles, seq_esec);

    % (SWITCH)% if to set the junction phase timing

    % 1. setting the length (seconds) of phases on individual controlled% junctions

    7

  • 8/10/2019 MATLAB AIMSUN

    9/12

    %***************************************************%** **% timing for individual junctions (rows) in selected order of junctions% the order of phases (and interphases) have to be read from AIMSUN.% The matrix should have at least number of columns = maximal number of

    % phases for all junctions

    phase=[ 20 4 52 4 0 0;20 32 4 20 4 0;27 5 26 4 13 5;31 5 40 4 0 0];

    %** **%***************************************************export_phases(phase, seq_jun);

    % 2. return control to AIMSUN without phase timing settingreturn_activity;

    while (get_lock == 0)end

    end% simulation step enddisp(AIMSUN simulation is finished.);load_final_information;[flow,travel_timeA,travel_timeD,delay_timeA, delay_timeD,speedA, speedD, ...

    density, stop_timeA, stop_timeD,n_stops,total_travel]=get_total_statistics;%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    %####################################% finalization block

    %####################################

    % unload libraryunload_library;

    4.4 Repeated Simulations

    The simulation without the changes to the AIMSUN scenario can be run repeatedly. Then it is necessaryto rewind the simulation in AIMSUN environmnent to its beginning before each simulation. If theAIMSUN console is used no actions are necessary.

    4.5 Exceptional Events

    4.5.1 Stopping the Simulation from AIMSUN

    It is possible to stop the simulation in an arbitrary moment from AIMSUN. Then for the repeatedsimulation it is necessary to rewind the simulation to its beginning and run the MATLAB m-scriptagain.

    If the function start_AIMSUN with option to run AIMSUN enviroment is used in m-script, new

    instance of AIMSUN is opened every time the function is called.If the AIMSUN console is used for the simulation, it can be stopped by pressing Ctrl+C. After that,

    the m-script in MATLAB has to be stopped using again Ctrl+Cand calling function after_error.

    4.5.2 Stopping the Simulation from MATLAB

    If the simulation is interrupted from MATLAB (e.g. due to the error in the code) the simulation halts.Then it is necessary to call the function after_error. It causes that the action is returned back to

    8

  • 8/10/2019 MATLAB AIMSUN

    10/12

    AIMSUN. Then the simulation in AIMSUN follows but without the synchronization and control fromMATLAB. Finally stopping the simulation from AIMSUN has to be applied.

    If AIMSUN console is used, it has to be stopped by pressing Ctrl+Ctoo.

    4.6 Preparation of Result Containers

    The toolbox allows automated creation of two versions of ASCII files representing result container atpresent.

    The first one supposes that the vehicle entrances are controlled from AIMSUN and turning rateson all junctions are fixed during the whole simulation. Also rates of vehicle types can be definedonly constant for the whole network and simulation time.

    The second one allows to define varying turning rates during simulation, while the vehicle entrancesare supposed to be controlled from MATLAB. Rates of vehicle types are considered same for thewhole network, but variation during simulation time can be generated again from MATLAB.

    Simple illustrative example scripts of the generation of traffic results for both versions are listed bellow.These examples form the contents of two scripts named _exampleTR1 and _exampleTR2, saved in thetoolbox directory.

    Example of simple script generating result container ASCII for fixed turning rates andvehicle entrance control done totally in AIMSUN

    % Example of constructor of result container (ASCII file) with fixed turning rates and non-zero entrances (generated in AIMSUN)

    %suppose matrix DATA exists and contains number of vehicles entering the%network. Rows correspond to sampling periods, while the columns various%entrances

    % starting time of simulation (vector of dimension 3 [hours minutes seconds])t_init = [0 0 0];

    % fixed sampling period (in seconds) of gathered data and generated statest_sample = 90;

    % name of the generated file (it coincides with result container name)file_name = realBezUt;

    % relate IDs of entrance sections with columns of DATA matrixIDinpSec = [9 7 1]; % IDs of entrance sections

    inflow = create_area_inflow(IDinpSec, DATA);

    % define the vehicle types in network and their rates% such vehicle types have to be predefined in TEDIvehicles(1) = create_vehicle(car,0.9);vehicles(2) = create_vehicle(truck,.1);

    % definition of junctions and fixed turning rates on themturnings = [.4 .6; .1 .9];junctions(1) = create_junction(1,2,2, [1 6], [2 12], turnings);

    turnings = [.80 .2; .23 .77];junctions(2) = create_junction(2,2,2, [7 11], [6 8], turnings);

    % NOT MODIFY HEREAFTER

    % construction of MATLAB structure, necessary to generate traffic result% ASCII filetr.file_name = file_name;tr.t_sample = t_sample;tr.t_init = t_init;tr.inflow = inflow;

    9

  • 8/10/2019 MATLAB AIMSUN

    11/12

    tr.junctions = junctions;tr.vehicles = vehicles;

    generate_traffic_result(tr,1);

    Example of simple script generating result container ASCII for varying turning rates andvehicle entrance control done only in MATLAB

    % Example of constructor of result container (ASCII file) with varible turning rates and zero entrances (entrances generated from MATLAB)

    % starting time of simulation (vector of dimension 3 [hours minutes seconds])t_init = [0 0 0];

    % name of the generated file (it coincides with result container name)file_name=realBezUt;

    % defines the IDs of input sectionsIDinpSec=[9 7 1]; % IDs of entrance sections

    inflow=create_area_inflow(IDinpSec);

    % define the vehicle types in network% such vehicle types have to be predefined in TEDIvehicles(1)=create_vehicle(car);vehicles(2)=create_vehicle(truck);

    % definition of junctions and varying turning rates on themturnings{1}=[.4 .6; .1 .9];turnings{2}=[.5 .5; .2 .8];junctions(1)=create_junction(1,2,2, [1 6], [2 12], turnings);

    turnings{1}=[.8 .2; .23 .77];turnings{2}=[.7 .3; .23 .77];junctions(2)=create_junction(2,2,2, [7 11], [6 8], turnings);

    % definition of times, where turning rates change% matrix having 3 columns for determination of time [hours minutes seconds]% number of rows corresponds with dimension of cell vectors turning for all junctionst_turning = [0 0 0; 10 0 0];

    % NOT MODIFY HEREAFTER

    % construction of MATLAB structure, necessary to generate traffic result% ASCII filetr.file_name = file_name;tr.t_init = t_init;tr.junctions = junctions;tr.vehicles = vehicles;tr.t_turning = t_turning;tr.inflow = inflow;

    generate_traffic_result(tr,1);

    A MATLAB Function References

    In order to simplify maintenance of the toolbox documentation, the function reference list is not includeddirectly in the document. Only the reference to the html version at http://marabu.utia.cas.cz:1800/svn/doprava/AIMSUN-MATLAB/toolbox/htmldoc/index.html is mentioned here.

    References

    [1] The MathWorks, Inc. MATLAB Users Guide, 1992.

    10

    http://marabu.utia.cas.cz:1800/svn/doprava/AIMSUN-MATLAB/toolbox/htmldoc/index.htmlhttp://marabu.utia.cas.cz:1800/svn/doprava/AIMSUN-MATLAB/toolbox/htmldoc/index.htmlhttp://marabu.utia.cas.cz:1800/svn/doprava/AIMSUN-MATLAB/toolbox/htmldoc/index.htmlhttp://marabu.utia.cas.cz:1800/svn/doprava/AIMSUN-MATLAB/toolbox/htmldoc/index.html
  • 8/10/2019 MATLAB AIMSUN

    12/12

    [2] TSS - Transport Simulation Systems. AIMSUN User Manual, version 4.2, 2004.

    11