Skip to content

Aimsun Next Micro API Example 5

Example of Transit Management

This example shows how to manage transit. It generates a new bus one minute after the simulation start time and disables its first bus stop. At the same time, the Aimsun Next API Module stores its distance traveled.

C++ Version

#include "AKIProxie.h"
#include "CIProxie.h"
#include "ANGConProxie.h"
#include "AAPI.h"
#include <fstream>

char            astring[128];
std::ofstream   fileFloatCar;
int         idveh = 0;
int         busVehicleTypeId = 79;
bool        timeChanged = false;
// Procedures could be modified by the user
int AAPILoad()
{   
    return 0;
}

int AAPIInit()
{   
    AKIPrintString("Init....");
    fileFloatCar.open("C:/tmp/BusyPy.txt", std::ios::out);
    idveh = 0;
    return 0;
}

int AAPISimulationReady()
{
    return 0;
}

int AAPIManage(double time, double timeSta, double timeTrans, double acycle)
{
    double tempsEntrada = AKIGetIniSimTime() + 60;
    if (timeSta == tempsEntrada) {
        AKIPrintString("Entering a Vehicle");
        //getting the bus vehicle type position
        int busPosition = AKIVehGetVehTypeInternalPosition(busVehicleTypeId);
        idveh = AKIPTEnterVeh( 1, busPosition, 1 );
        sprintf(astring, " idveh = %d", idveh);
        AKIPrintString(astring);
    }
    if (timeSta >= tempsEntrada + 1 && timeChanged == false) {
        AKIPrintString("Modifying Stop Time of first bus stop");
        double res = 0;
        res = AKIPTVehModifyStopTime( idveh, 0, 0 );
        sprintf(astring, " res = %d", res);
        AKIPrintString(astring);
        timeChanged = true;
    }
    return 0;
}

int AAPIPostManage(double time, double timeSta, double timeTrans, double acycle)
{ 
    if (idveh > 0) {
        InfVeh infveh = AKIVehTrackedGetInf(idveh);
        if (infveh.report == 0) {
            sprintf(astring, "%f %f\n", timeSta, infveh.TotalDistance);
            fileFloatCar << astring;
        } else {
            idveh = 0;
            fileFloatCar.close();
        }
    }
    return 0;
}

int AAPIFinish()
{
    AKIPrintString("...Finish");
    return 0;
}

int AAPIUnLoad()
{
    return 0;
}

Python Version

from AAPI import *

#global variables
fileFloatCar = 0
idveh=0
busVehicleTypeId = 79
timeChanged = False

def AAPILoad():
    return 0
def AAPIInit():
    AKIPrintString("Init....")
    global idveh, fileFloatCar
    fileFloatCar=open('C:/tmp/BusPy.txt', 'w');
    idveh = 0
    return 0

def AAPISimulationReady():
    return 0

def AAPIManage(time, timeSta, timeTrans, acycle):
    global idveh
    global busVehicleTypeId
    global timeChanged

    tempsEntrada = AKIGetIniSimTime()+60
    if timeSta == tempsEntrada:
        AKIPrintString("Entering a Vehicle")
        #getting the bus vehicle type position
        busPosition = AKIVehGetVehTypeInternalPosition(busVehicleTypeId)
        idveh = AKIPTEnterVeh( 1418, busPosition, True )
        astring = ' idveh = %d' %(idveh)
        AKIPrintString(astring)


    if timeSta >= tempsEntrada + 1 and timeChanged == False:
        AKIPrintString("Modifying Stop Time of first bus stop")     
        res = AKIPTVehModifyStopTime( idveh, 0, 0 )
        astring = ' res = %d' %(res)
        AKIPrintString(astring)
        timeChanged = True
    return 0

def AAPIPostManage(time, timeSta, timeTrans, acycle):
    global idveh, fileFloatCar
    if idveh > 0 :
        infveh = AKIVehTrackedGetInf(idveh)
        if infveh.report == 0:
            astring = "%f %f\n" %(timeSta, infveh.TotalDistance)
            fileFloatCar.write(astring)
        else:
            idveh = 0
            fileFloatCar.close()
    return 0

def AAPIFinish():
    AKIPrintString("...Finish")
    return 0

def AAPIUnLoad():
    return 0