Ejemplo n.º 1
0
void TAToQueues(Road& R, int timestep) {
	vector<int> carsToErase;
	for(vector<Car>::iterator it = R.getTA().begin() ; it != R.getTA().end() ; it++) {
		it->iterProg(0);
		if(it->distanceInTA() < R.distanceToTravelInTA((*it)))		// The car has to reach its queue (Individual or common)
			it->iterDistanceInTA(R.speedMax()*timestep);
		// !!! SOME TIME IS ADDED TO FAKE THE FACT THAT THE CAR WAS NOT TRAVELING AT VMAX 
		// !!! BUT, BESIDES THE TIME ADDED< THE CAR SHOULD REALLY WAIT THIS TIME AND NOT BE IN THE SYSTEM ANYMORE
		else {																// The car has reached its queue (Individual or common)
			it->initDistanceInTA();															//The traveled distance is initialized
			int addedTime = timeToAdd(R.speedMax(), it->accMax(), it->distanceInTA() );		//Some time is added to the cars 
			for(int i = 0; i<addedTime ; i++) { it->iterProg(0); }
			carsToErase.push_back(it->number());
			R.addCarToQueue((*it));
		}
	}
	R.removeCarsFromTA(carsToErase);
}
Ejemplo n.º 2
0
vector<Event> Intersection::runEvent(Event ev, double time, double dt)
{
    vector<Event> eventsToReschedule(0);
    EventType type = ev.getType();
    switch(type)
    {
    case enteringRoad:
    {
        Road* road = ev.getRoad();
        bool hasMoved = road->moveCarsInJA();
        if(hasMoved) //if at least one car has moved
        {
            Intersection* previousIntersection = road->getIntersectionA();
            //Reschedule event running the TravelingArea and CommonQueue in current road
            eventsToReschedule.push_back(Event(outgoingRoad,road,previousIntersection));
        }
        break;
    }
    case outgoingRoad:
    {
        Road* road = ev.getRoad();
        bool hasMoved = road->moveCarsInCQandTA(dt);
        if(hasMoved) //if at least one car has moved
        {
            //Rescheduling the entrance of the car that enter the network
            eventsToReschedule.push_back(Event(enteringNetwork,NULL,this));
            //Rescheduling entry of cars from the road entering the intersection
            for(vector<Road*>::iterator it = enteringRoads.begin(); it != enteringRoads.end(); it++)
            {
                eventsToReschedule.push_back(Event(enteringRoad,*it,this));
            }
        }
        break;
    }
    case enteringNetwork:
    {
        EnterCarIt it = enteringCars.begin();
        while(it != enteringCars.end()) //We go through every entering car
        {
            EnterCarIt it2 = next(it);
            Car* currentCar = it->first;
            Road* targetRoad = it->second;
            if(currentCar->getEnteringTime() <= time) //If it is time for the current car to enter the network
            {
                if(targetRoad->getRoomLeftInTravelingArea() >= currentCar->getLength()) //If there is room for the car
                {
                    MoveResult result = currentCar->tryToEnterRoad(targetRoad); //Run the moving function in the car
                    targetRoad->getTA()->addCar(currentCar); //Add the car to the new traveling area
                    enteringCars.erase(it); //erasing the car
                    it = it2; //updating the iterator
                }
                else
                {
                    currentCar->postponeEnteringTime(dt); //We postpone the car's entering time
                    it++; //updating the iterator
                }
            }
            else
                it++;
        }
        break;
    }
    }
    return eventsToReschedule;
}