Skip to content

Instantly share code, notes, and snippets.

@cvson
Created September 9, 2014 03:48
Show Gist options
  • Save cvson/75e111d5596344729b6c to your computer and use it in GitHub Desktop.
Save cvson/75e111d5596344729b6c to your computer and use it in GitHub Desktop.
//
// trafficModeling.C
////////////////////////////////////////////////////
//
// Simple Monte Carlo for traffic modeling
//
///////////////////////////////////////////////////
// Created by @visionlib.postach.io
void titleStyle(TH2* h1){
h1->GetYaxis()->CenterTitle();
h1->GetXaxis()->CenterTitle();
h1->GetXaxis()->SetLabelSize(h1->GetXaxis()->GetTitleSize()*1.4);
h1->GetYaxis()->SetLabelSize(h1->GetYaxis()->GetTitleSize()*1.4);
h1->GetXaxis()->SetTitleSize(h1->GetXaxis()->GetLabelSize()*1.2);
h1->GetYaxis()->SetTitleSize(h1->GetYaxis()->GetLabelSize()*1.2);
h1->GetYaxis()->SetTitleOffset(0.9);
h1->GetXaxis()->SetTitleOffset(0.9);
}
void trafficModeling(){
const Int_t nVehicle = 100;
const Int_t nSpace = 1000;
const Int_t speedMax = 5;
const Float_t probRandomSlow = 0.33;//1/3
//initialize position and velocity
Int_t pos_t0[nVehicle]={};
Int_t vel_t0[nVehicle]={};
Int_t disNext_t0[nVehicle]={};
Int_t time_t0[nVehicle]={0};
for (int ivehicle=0; ivehicle<nVehicle; ivehicle++) {
pos_t0[ivehicle] = gRandom->Uniform(ivehicle*10,(ivehicle+1)*10);
cout<<" postion "<<pos_t0[ivehicle]<<endl;
vel_t0[ivehicle] = gRandom->Uniform(1,speedMax);
}
//reinitialize velocity to avoid collision
for (int ivehicle=0; ivehicle<nVehicle; ivehicle++) {
if (ivehicle<nVehicle-1) {
disNext_t0[ivehicle] = (pos_t0[ivehicle+1] - pos_t0[ivehicle])%nSpace;
}
//circle condition
else disNext_t0[ivehicle] = (pos_t0[0]+nSpace-pos_t0[ivehicle])%nSpace;
//avoid collision at the beginning
if (disNext_t0[ivehicle]<vel_t0[ivehicle]) {
vel_t0[ivehicle] = disNext_t0[ivehicle]-1;
}
cout <<"vehical "<<ivehicle<<" pos "<<pos_t0[ivehicle]<<" dis "<<disNext_t0[ivehicle]<<" vel "<<vel_t0[ivehicle]<<endl;
}//end for
const Int_t kIteration =500;
TH2F *hempty = new TH2F("hempty","hempty",nSpace,0,nSpace,kIteration,0,kIteration);
hempty->GetXaxis()->SetTitle("Car position");
hempty->GetYaxis()->SetTitle("Time evolution");
titleStyle(hempty);
TCanvas *c1 = new TCanvas("c1","c1",1250,1000);
gStyle->SetOptStat(0);
gStyle->SetLineWidth(2);
hempty->SetTitle();
hempty->Draw();
TGraph *pgraph_t0 = new TGraph(nVehicle,pos_t0,time_t0);
pgraph_t0->SetMarkerStyle(8);
Int_t ci;
//navy blue
ci = TColor::GetColor("#0B3861");
pgraph_t0->SetTitle("");
pgraph_t0->SetMarkerColor(ci);
pgraph_t0->SetMarkerSize(0.4);
pgraph_t0->Draw("P");
//time goes by
Int_t time_i[nVehicle]={};
TGraph *pgraph_i;
for (Int_t i=1; i<kIteration;i++) {
if (i%100==0) {
cout<<"Processing "<<i<<"/"<<kIteration<<endl;
}
//calculate speed
for (int ivehicle=0; ivehicle<nVehicle; ivehicle++) {
time_i[ivehicle]=i;
//if velocity smaller than max, then increase by 1
if (vel_t0[ivehicle]<speedMax) {
vel_t0[ivehicle] +=1;
}
//avoid collision
if (vel_t0[ivehicle]>disNext_t0[ivehicle]) {
vel_t0[ivehicle] = disNext_t0[ivehicle]-1;
}
//reduce speed randomly
double randomSpeedSlow = gRandom->Uniform(0.0,1.0);
if (randomSpeedSlow<0.33) {
vel_t0[ivehicle] -=1;
}
//newlocation
pos_t0[ivehicle] = (pos_t0[ivehicle]+vel_t0[ivehicle])%nSpace;
}//end for ivehicle
//recalculate distance, NOT so good way
for (int ivehicle=0; ivehicle<nVehicle; ivehicle++) {
if (ivehicle < (nVehicle-1)) {
disNext_t0[ivehicle] = (pos_t0[ivehicle+1]+nSpace - pos_t0[ivehicle])%nSpace;
}
//circle condition
else disNext_t0[ivehicle] = (pos_t0[0]+nSpace-pos_t0[ivehicle])%nSpace;
}
pgraph_i = new TGraph(nVehicle,pos_t0,time_i);
pgraph_i->SetMarkerColor(ci);
pgraph_i->SetMarkerSize(0.4);
pgraph_i->SetTitle("");
pgraph_i->Draw("P same");
c1->Update();
}//end for kIteration
c1->Print("trafficModeling.png");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment