// To compile this in the CLAMV computers simply write "g++ -o Nagel-Schreckenberg_TrafficModel Nagel-Schreckenberg_TrafficModel.cpp" // // main.cpp // trafficflow // // Created by Alexandru Mihai on 10/19/14. // Copyright (c) 2014 JacobsUniversity. All rights reserved. // #include #include #include using namespace std; int street1[100]; int street2[100]; //Here a random starting point is chosen for the 6 car traffic jam int r = arc4random()%100; int trafficjam[6] = {r,r+1,r+2,r+3,r+4,r+5}; //Here the dawdle probability is given int probability = 250; //This function give the starting points of the cars using a recursive function based on a random number generator, to ensure that the traffic jam is not overwritten int initializing() { int position = (arc4random() % 100); if (position == trafficjam[0] || position == trafficjam[1] || position == trafficjam[2] || position == trafficjam[3] || position == trafficjam[4] || position == trafficjam[5]) { return initializing(); } else { return position; } } // This function is the one that assigns the random initial velocities to the starting points using the previous function. void startingpositions() { street1[trafficjam[5]] = (arc4random() % 4) + 3; for( int k = 0; k < 5; k++) { street1[trafficjam[k]] = 1; } for( int i = 1; i < 25; i++) { street1[initializing()] = (arc4random() % 6) + 1; } } // This is the collision step, meaning the step in which the cars change the velocity with with they are traveling(excluding the dawdle prob.) // Within teach larger If loop is a check that the max velocity is not surpassing the space the car has to move, therby ensuring nu accidents void collision_step() { for(int k=0;k<100;k++){street2[k] = 0;} for( int j = 0; j < 100; j++) { if(street1[j] == 6) { if(street1[(j+1)%100] != 0) { street2[j] = 1; } else if(street1[(j+2)%100] != 0) { street2[j] = 2; } else if(street1[(j+3)%100] != 0) { street2[j] = 3; } else if(street1[(j+4)%100] != 0) { street2[j] = 4; } else if(street1[(j+5)%100] != 0) { street2[j] = 5; } else { street2[j] = 6; } } if(street1[j] == 5) { if(street1[(j+1)%100] != 0) { street2[j] = 1; } else if(street1[(j+2)%100] != 0) { street2[j] = 2; } else if(street1[(j+3)%100] != 0) { street2[j] = 3; } else if(street1[(j+4)%100] != 0) { street2[j] = 4; } else if(street1[(j+5)%100] != 0) { street2[j] = 5; } else { street2[j] = 6; } } if(street1[j] == 4) { if(street1[(j+1)%100] != 0) { street2[j] = 1; } else if(street1[(j+2)%100] != 0) { street2[j] = 2; } else if(street1[(j+3)%100] != 0) { street2[j] = 3; } else if(street1[(j+4)%100] != 0) { street2[j] = 4; } else { street2[j] = 5; } } if(street1[j] == 3) { if(street1[(j+1)%100] != 0) { street2[j] = 1; } else if(street1[(j+2)%100] != 0) { street2[j] = 2; } else if(street1[(j+3)%100] != 0) { street2[j] = 3; } else { street2[j] = 4; } } if(street1[j] == 2) { if(street1[(j+1)%100] != 0) { street2[j] = 1; } else if(street1[(j+2)%100] != 0) { street2[j] = 2; } else { street2[j] = 3; } } if(street1[j] == 1) { if(street1[(j+1)%100] != 0) { street2[j] = 1; } else { street2[j] = 2; } } if(street2[j] == 0) { street2[j] = 0; } } } // In this function the dawdle probability of called and changes the velocities of the respective cars void dawdle() { for( int j = 0; j < 100; j++) { if(street2[j] == 2) { if(arc4random()%1000 < probability) { street2[j] = 1; } else { street2[j] = 2; } } if(street2[j] == 3) { if(arc4random()%1000 < probability) { street2[j] = 2; } else { street2[j] = 3; } } if(street2[j] == 4) { if(arc4random()%1000 < probability) { street2[j] = 3; } else { street2[j] = 4; } } if(street2[j] == 5) { if(arc4random()%1000 < probability) { street2[j] = 4; } else { street2[j] = 5; } } if(street2[j] == 6) { if(arc4random()%1000 < probability) { street2[j] = 5; } else { street2[j] = 6; } } } } // In the stream_step function we propegate the velocities we calculated in the collision function so that the iteration can continue void stream_step() { for(int k=0;k<100;k++){street1[k] = 0;} for( int j = 0; j < 100; j++) { //street1[j] = 0; if(street2[j] == 6) { street1[(j+5)%100] = 6; } if(street2[j] == 5) { street1[(j+4)%100] = 5; } if(street2[j] == 4) { street1[(j+3)%100] = 4; } if(street2[j] == 3) { street1[(j+2)%100] = 3; } if(street2[j] == 2) { street1[(j+1)%100] = 2; } if(street2[j] == 1) { street1[j] = 1; } } } // And here the final iteration with respect to time takes place, putting together all of the previous functions. void iteration( int iter, ostream& output) { startingpositions(); for(int t = 0; t < iter; t++) { for( int k = 0; k < 100; k++) { if(street1[k] != 0) { street1[k] = 1; } else { street1[k] = 0; } output << street1[k]; } output << "\n"; collision_step(); if(probability != 0) { dawdle(); } stream_step(); } } int main() { ofstream myFileStream("street3"); iteration(500,myFileStream); myFileStream.close(); return 0; }