Skip to content

Aimsun Next Meso API Example 1

Example of a Mesoscopic Vehicle Entrance

This example reads a list of vehicles from an external file which supplies their external id, entrance time, vehicle type, origin, destination, and value of time. It then creates these vehicles in the simulation.

C++ Version

#include "AMesoAPIHelper.h"
#include "CIProxie.h"


#include "AAPI.h"
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <istream>
#include <sstream>
#include <string>
#include <map>


// Procedures could be modified by the user

struct MyVehInfo {
    MyVehInfo() {myVehId=0; aimsunId=0;time=0; vehType=0; origin=0; destination=0; valueOfTime=0;}
    MyVehInfo(int amyId, int atime, int avehType, int aorigin, int adestination, double avalueOfTime) {myVehId=amyId; aimsunId=0; time=atime; vehType=avehType; origin=aorigin; destination=adestination; valueOfTime=avalueOfTime;}
    MyVehInfo(const MyVehInfo & other) {myVehId=other.myVehId; aimsunId=other.aimsunId, time=other.time; vehType=other.vehType; origin=other.origin; destination=other.destination; valueOfTime=other.valueOfTime;}
    int myVehId;
    int aimsunId;
    double time;
    int vehType;
    int origin;
    int destination;
    double valueOfTime;
};

std::map<int, MyVehInfo> myVehContainer;
std::map<int, int> AimsunIdsToMyIds;

int readData(const std::string & inFileName)
{
    std::ifstream qFile(inFileName, std::ios_base::in);
    std::string qLine;
    int lineNumber = 0;
    while( std::getline(qFile, qLine, '\n') ) {
        std::cout << qLine << '\n';
        if ( lineNumber>0 ) {
            std::stringstream  lineStream(qLine);
            std::string        cell;
            int                 cellNumber = 0;
            MyVehInfo vehInfo;
            while(std::getline(lineStream,cell,',') ) {
                if ( cellNumber == 0 ) {
                    vehInfo.myVehId = atoi(cell.c_str());
                }
                if ( cellNumber == 1 ) {
                    vehInfo.time = atof(cell.c_str());  
                }
                if ( cellNumber == 2 ) {
                    vehInfo.vehType = atoi(cell.c_str());
                }
                if ( cellNumber == 3 ) {
                    vehInfo.origin = atoi(cell.c_str());
                }
                if ( cellNumber == 4 ) {
                    vehInfo.destination = atoi(cell.c_str());
                }
                if ( cellNumber == 5 ) {
                    vehInfo.valueOfTime = atof(cell.c_str());   
                }
                cellNumber++;
            }
            myVehContainer.insert(std::map<int, MyVehInfo>::value_type(vehInfo.myVehId, vehInfo));
        }
        lineNumber++;
    }
    return lineNumber-1;
}


int MesoAPILoad()
{
    std::cout << "LOAD\n";
    AMesoDisableGeneration(simhandler);
    return 0;
}

int MesoAPIUnLoad(void * simhandler)
{   
    AMesoPrintString(simhandler, "UNLOAD");
    return 0;
}

int MesoAPIInit(void * simhandler, int iterationNumber, bool /*statisticsAllowed*/)
{   
    if ( iterationNumber == 1 ) {
        std::string inFileName= "c:/temp/generation.csv";

        int numberVehiclesRead = readData(inFileName);

        std::map<int, MyVehInfo>::iterator it = myVehContainer.begin();
        std::map<int, MyVehInfo>::iterator itend = myVehContainer.end();
        for(; it!=itend; ++it) {
            MyVehInfo & vehInfo = (*it).second;
            int aimsunId = AMesoAddTrip(simhandler, vehInfo.vehType, vehInfo.time, vehInfo.origin, vehInfo.destination, -1, -1, vehInfo.valueOfTime);
            if ( aimsunId>0 ) {
                AimsunIdsToMyIds[aimsunId] = vehInfo.myVehId;
                vehInfo.aimsunId = aimsunId;
            }else{
                std::cout << "Could not add vehicle trip with my id " << vehInfo.myVehId << '\n';
            }
        }
    }
    return 0;
}

int MesoAPISimulationReady(void * handler)

{ return 0; }

int MesoAPIFinish(void * /*simhandler*/)
{
    return 0;
}

int MesoAPINewVehicleSystem(void * /*simhandler*/, void * /*vehhandler*/)
{
    return 0;
}

int MesoAPINewVehicleNetwork(void * /*simhandler*/, void * /*vehhandler*/)
{
    //AMesoVehicleInfo vehInf = AMesoGetVehicleInfo(simhandler, vehhandler);
    //qInfo("Veh network id %d %f", vehInf.id, AMesoGetCurrentTime(simhandler));
    return 0;
}

int MesoAPIFinishVehicleNetwork(void * simhandler, void * vehhandler, bool normalOut)
{
    (void)normalOut;
    AMesoVehicleInfo vehInf = AMesoGetVehicleInfo(simhandler, vehhandler);
    double currentTime = AMesoGetCurrentTime(simhandler);
    char auxstring[512];
    sprintf(auxstring, "Finish Vehicle %d from section %d at time %f", vehInf.id, vehInf.currentSection, currentTime);
    std::cout << auxstring << '\n'; 
    AMesoUpdateTripTime(simhandler, vehInf.id, currentTime+1);


    //AMesoPrintString(simhandler, auxstring);
    return 0;
}

int MesoAPIEnterVehicleSection(void * /*simhandler*/, void * /*vehhandler*/, int /*fromSection*/, int /*toSection*/)
{
    //AMesoVehicleInfo vehInf = AMesoGetVehicleInfo(simhandler, vehhandler);
    //qInfo("Veh enter id %d %f %d %d", vehInf.id, AMesoGetCurrentTime(simhandler), fromSection, toSection);
    return 0;
}

int MesoAPIExitVehicleSection(void * /*simhandler*/, void * /*vehhandler*/, int /*section*/)
{
    //AMesoVehicleInfo vehInf = AMesoGetVehicleInfo(simhandler, vehhandler);
    //qInfo("Veh exit id %d %f %d %d", vehInf.id, AMesoGetCurrentTime(simhandler), section);
    return 0;
}

int MesoAPIVehicleReadyForSection(void * /*simhandler*/, void * /*vehhandler*/, int /*section*/)
{
    return 0;
}

int MesoAPIPostManageControl(void * simhandler)
{
    std::cout << "Post Manage Control" << '\n';
    return 0;
}

int MesoAPIManageCycleEvent(void * simhandler, int cycleEventId)
{
    return 0;
}

int MesoAPIPreManageRouteChoice(void * simhandler)
{
    std::cout << "Post Manage Control" << '\n';
    return 0;
}

int MesoAPINewStatisticalInterval(void * simhandler)
{
    std::cout << "New Statistics Interval" << '\n';
    return 0;
}

int MesoAPINewDetectorStatisticalInterval(void * simhandler)
{
    std::cout << "New Detector Statistics Interval" << '\n';
    return 0;
}