Exemplo de API Mesoscópica do Aimsun Next 1¶
Exemplo de Entrada de Veículo Mesoscópico¶
Este exemplo lê uma lista de veículos de um arquivo externo que fornece seu id externo, horário de entrada, tipo de veículo, origem, destino e valor do tempo. Em seguida, ele cria esses veículos na simulação.
Versão C++¶
#include "AMesoAPIHelper.h"
#include "CIProxie.h"
#include "AAPI.h"
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <istream>
#include <sstream>
#include <string>
#include <map>
// Os procedimentos podem ser modificados pelo usuário
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, '
') ) {
std::cout << qLine << '
';
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 << "CARREGAR
";
AMesoDisableGeneration(simhandler);
return 0;
}
int MesoAPIUnLoad(void * simhandler)
{
AMesoPrintString(simhandler, "DESCARREGAR");
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 << "Não foi possível adicionar a viagem do veículo com meu id " << vehInfo.myVehId << '
';
}
}
}
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("Veículo rede 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];
snprintf(auxstring, 512, "Finalizar Veículo %d da seção %d no tempo %f", vehInf.id, vehInf.currentSection, currentTime);
std::cout << auxstring << '
';
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("Veículo entra 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("Veículo sai 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 << "Gerenciar Controle Pós" << '
';
return 0;
}
int MesoAPIManageCycleEvent(void * simhandler, int cycleEventId)
{
return 0;
}
int MesoAPIPreManageRouteChoice(void * simhandler)
{
std::cout << "Gerenciar Controle Pré" << '
';
return 0;
}
int MesoAPINewStatisticalInterval(void * simhandler)
{
std::cout << "Novo Intervalo Estatístico" << '
';
return 0;
}
int MesoAPINewDetectorStatisticalInterval(void * simhandler)
{
std::cout << "Novo Intervalo Estatístico do Detector" << '
';
return 0;
}