Ejemplo n.º 1
0
        private int W; // Length of an intersection

        #endregion Fields

        #region Constructors

        public Network(InputParameters ip, Random _rN, Algorithm _algorithm)
        {
            ns = new NetworkStatistics();

            this.rN = _rN;
            this.algo = _algorithm;
            numHRoads = ip.numHRoads;
            numVRoads = ip.numVRoads;

            Road r = null;					// Build roads as elements in array list
            vertRoads = new ArrayList();
            horRoads = new ArrayList();

            R = ip.lengthR;
            W = ip.lengthW;
            speedLimit = ip.speedLimit;

            for(int i = 0; i < numVRoads; i++)
            {
                // Create vertical roads
                r = new Road(i, RoadOrientation.NS, ip, rN, algo, this);
            //	r = new Road(i + 1, Orientation.VERTICAL, R, W, numHRoads, speedLimit, toggleInterval, num);
                vertRoads.Add(r);
            }
            for(int j = 0; j < numHRoads; j++)
            {
                // Create horizontal roads
                r = new Road(j, RoadOrientation.EW, ip, rN, algo, this);
            //	r = new Road(j + 1, Orientation.HORIZONTAL, R, W, numVRoads, speedLimit, toggleInterval, num);
                horRoads.Add(r);
            }

            string fileName;
            writer = new StreamWriter("arrivalLog.txt");
        }
        private int platoonSize; // Size of platoon - fixed for all roads

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Class constructor
        /// </summary>
        /// <param name="ip">InputParameters instance</param>
        public FixedIntersectionTiming(InputParameters _ip)
            : base(_ip)
        {
            /**
             * This is the most basic algorithm where there's exactly one vehicle per platoon and fixed green time interval of 20
             * per phase. Moreover, it's possible to create a new platoon each time instant - thus we set H = 1.
             */
            ip = _ip;
            platoonSize = 1;
            greenPhaseInterval = 20;
            H = 1;
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            //
            // TODO: Add code to start application here
            //
            int seed = Environment.TickCount;

            seed = 2951;
            Random rN = new Random(seed);

            Console.WriteLine("Seed = " + seed);
            InputParameters ip = new InputParameters("input.txt", null);

            //	Algorithm algo = new FixedIntersectionTiming(ip);
            Algorithm  algo = new FixedPeriodVariableSize(ip);
            //    Algorithm algo = new FixedSizeVariablePeriod(ip);

            Network ns = new Network(ip, rN, algo);
            ns.runSimulation(7200);

             //           Console.WriteLine("Num Cars = " + ip.getNumCars(0, Direction.EW, 16));

            //			PlatoonPerfCounter p1 = new PlatoonPerfCounter(1516, 16, 1500, 1, 3040, 0);
            //			PlatoonPerfCounter p2 = new PlatoonPerfCounter(1500, 10, 1500, 1, 3040, 0);
            //			PlatoonPerfCounter p3 = new PlatoonPerfCounter(1516, 16, 1500, 1, 3040, 0);
            //			PlatoonPerfCounter p4 = new PlatoonPerfCounter(1510, 10, 1500, 1, 3040, 0);
            //
            //
            //			RoadPerfCounter rpf = new RoadPerfCounter();
            //
            //			rpf.addPlatoonCounter(p1);
            //			rpf.addPlatoonCounter(p2);
            //			rpf.addPlatoonCounter(p3);
            //			rpf.addPlatoonCounter(p4);
            //
            //			for(int i = 0; i < 50; i++)
            //			{
            //				rpf.platoonCreated();
            //			}
            //
            //			for(int i = 0; i < 4; i++)
            //			{
            //				rpf.platoonDeparted();
            //			}
            //
            //			rpf.getStatistics();
        }
		private InputParameters ip;		// Reference to input parameter object

		/// <summary>
		/// Class constructor
		/// </summary>
		/// <param name="ip">InputParameters instance</param>
		public FixedPeriodVariableSize(InputParameters _ip) : base(_ip)
		{
			double []temp1, temp2;
			int []temp3;
			int tempVar;
			int idx;

			this.ip = _ip;

			#region Code to normalize input arrival rates
			ip.getArrivalRateVRoads(out temp1);
			ip.getArrivalRateHRoads(out temp2);

			this.normalizeInput(temp1, temp2);
			#endregion

			#region Code to compute hyperperiod
			temp3 = new Int32[arrivalHRoads.Length + arrivalVRoads.Length];

			idx = 0;

			for(int i = 0; i < arrivalHRoads.Length; i++)
				temp3[idx++] = arrivalHRoads[i];
			for(int i = 0; i < arrivalVRoads.Length; i++)
				temp3[idx++] = arrivalVRoads[i];

			H = ComputeLCM.LCM(temp3);
			#endregion

			Console.WriteLine("Hyperperiod = " + H);

			/**
			 * We compute execution time along each intersection as the fraction of the traffic
			 * flowing through that intersection in the direction under consideration. Then, for
			 * each intersection along the road, we find the minimum value of execution time along
			 * that road in the specified direction. That value is the execution time (platoon length)
			 * along that road.
			 */

			#region Code to compute execution time on NS roads

			eVRoads = new int[arrivalVRoads.Length];

			for(int i = 0; i < arrivalVRoads.Length; i++)
			{
				tempVar = Convert.ToInt32(Math.Floor((1.0 * arrivalVRoads[i] / (arrivalVRoads[i] + arrivalHRoads[0]) * H)));
				eVRoads[i] = tempVar;
				
				for(int j = 1; j < arrivalHRoads.Length; j++)
				{
					tempVar = Convert.ToInt32(Math.Floor((1.0 * arrivalVRoads[i] / (arrivalVRoads[i] + arrivalHRoads[j]) * H)));

					if(tempVar < eVRoads[i])
						eVRoads[i] = tempVar;
				}
			}
			#endregion
//#if DDEBUG
			Console.WriteLine("Printing list of E along NS roads");

			for(int i = 0; i < eVRoads.Length; i++)
			{
				Console.WriteLine(eVRoads[i]);
			}
//#endif
			#region Code to compute execution time on EW roads

			eHRoads = new int[arrivalHRoads.Length];

			for(int i = 0; i < arrivalHRoads.Length; i++)
			{
				eHRoads[i] = Convert.ToInt32(Math.Floor((1.0 * arrivalHRoads[i] / (arrivalHRoads[i] + arrivalVRoads[0]) * H)));

				for(int j = 1; j < arrivalVRoads.Length; j++)
				{
					tempVar = Convert.ToInt32(Math.Floor((1.0 * arrivalHRoads[i] / (arrivalHRoads[i] + arrivalVRoads[j]) * H)));

						if(tempVar < eHRoads[i])
							eHRoads[i] = tempVar;
				}
			}
			#endregion
//#if DDEBUG
			Console.WriteLine("Printing list of E along EW roads");

			for(int i = 0; i < eHRoads.Length; i++)
			{
				Console.WriteLine(eHRoads[i]);
			}
//#endif

			/**
			 * From the execution time at each intersection, we can determine the platoon lengths and hence the 
			 * number of vehicles in each platoon.
			 */
			pLenVRoads = new int[eVRoads.Length];
			pLenHRoads = new int[eHRoads.Length];
			platoonSizeVRoads = new int[eVRoads.Length];
			platoonSizeHRoads = new int[eHRoads.Length];

			for(int i = 0; i < eVRoads.Length; i++)
			{
				/**
				 * Compute platoon length as
				 * SpeedLimit * E - W
				 */
				pLenVRoads[i] = ip.speedLimit * eVRoads[i] - ip.lengthW;

				/**
				 * Compute how many vehicles can fit in this platoon
				 */
				tempVar = pLenVRoads[i] / (ip.intraPlatoonDist + ip.vehicleLen);
				platoonSizeVRoads[i] = tempVar;

				/**
				 * Check if one more vehicle can be accomodated in this platoon. We do not need
				 * to consider intra-platoon spacing as it is already considered in the above
				 * expression.
				 */
				if((tempVar = pLenVRoads[i] % (ip.intraPlatoonDist + ip.vehicleLen)) >= ip.vehicleLen)
					platoonSizeVRoads[i] += 1;
//#if DDEBUG
				Console.WriteLine("Platoon Length on NS{0} Road = {1} Number of Vehicles in Platoon = {2}", i, pLenVRoads[i], platoonSizeVRoads[i]);
//#endif
			}
			for(int i = 0; i < eHRoads.Length; i++)
			{
				pLenHRoads[i] = ip.speedLimit * eHRoads[i] - ip.lengthW;

				/**
				 * Compute how many vehicles can fit in this platoon
				 */
				tempVar = pLenHRoads[i] / (ip.intraPlatoonDist + ip.vehicleLen);
				platoonSizeHRoads[i] = tempVar;

				if(pLenHRoads[i] % (ip.intraPlatoonDist + ip.vehicleLen) >= ip.vehicleLen)
					platoonSizeHRoads[i] += 1;
#if DDEBUG
                Console.WriteLine("Platoon Length on EW{0} Road = {1} Number of Vehicles in Platoon = {2}", i, pLenHRoads[i], platoonSizeHRoads[i]);
#endif
			}
			/**
			* Now we compute the time instants when NS (SN) and EW (WE) platoons cross the intersections.
			* Each time instant is the offset from the start of hyperperiod.
			*/

			crossingTimeNS = new int[ip.numHRoads * ip.numVRoads];
			crossingTimeEW = new int[ip.numHRoads * ip.numVRoads];

			Console.WriteLine("Schedule");
			for(int i = 0, j = 0; i < crossingTimeNS.Length; i++)
			{
				crossingTimeNS[i] = 0;
				crossingTimeEW[i] = eVRoads[j];
				Console.WriteLine("Intersection = {0} NS Crossing Time = {1} EW Crossing Time = {2}", i, crossingTimeNS[i], crossingTimeEW[i]);

				if(j < eVRoads.Length - 1)
					j++;
				else
					j = 0;
			}
		}
		private InputParameters ip;				// Reference to input parameter object

		/// <summary>
		/// Class Constructor
		/// </summary>
		/// <param name="_ip">InputParameters instance</param>
		public FixedSizeVariablePeriod(InputParameters _ip) : base(_ip)
		{
			double []temp1;		// Temporary variable
			this.ip = _ip;

			/**
			 * NOTE: We are setting 10 vehicles per platoon in the algorithm
			 */
			platoonSize = 10;
			platoonLen = platoonSize * ip.vehicleLen + (platoonSize - 1) * ip.intraPlatoonDist;

			/**
			 * The first step is to compute the execution time for the platoons.
			 * Execution time is computed as the total distance that a platoon must travel at speed limit on the intersection
			 * such that it's trailing end just clears the intersection. We take ceiling of that value for safety considerations.
			 */
			executionTime = (int) Math.Ceiling((ip.lengthW + platoonLen) * 1.0 / ip.speedLimit);
						
			/**
			 * Obtain arrival rate on NS and EW roads.
			 */
			ip.getArrivalRateVRoads(out temp1);

			/**
			 * Obtain the maximum arrival rate among all NS roads.
			 */
            maxArrivalRateVRoads = temp1[0];

			for(int i = 1; i < temp1.Length; i++)
			{
				if(maxArrivalRateVRoads < temp1[i])
					maxArrivalRateVRoads = temp1[i];
			}

			/**
			 * Repeat the same to get maximum arrival rate among all EW roads.
			 */
			ip.getArrivalRateHRoads(out temp1);
			maxArrivalRateHRoads = temp1[0];

			for(int i = 1; i < temp1.Length; i++)
			{
				if(maxArrivalRateHRoads < temp1[i])
					maxArrivalRateHRoads = temp1[i];
			}

			/**
			 * Now compute K and hence obtain period for NS platoons and period for EW platoons
			 */
			if(maxArrivalRateVRoads >= maxArrivalRateHRoads)
			{
				k = maxArrivalRateVRoads * 1.0 / maxArrivalRateHRoads;
				periodNS = (int) Math.Ceiling(executionTime * 1.0 * (1 + k) / k);
				periodEW = (int) Math.Ceiling(executionTime * 1.0 * (1 + k));
			}
			else
			{
				k = maxArrivalRateHRoads * 1.0 / maxArrivalRateVRoads;
				periodNS = (int) Math.Ceiling(executionTime * 1.0 * (1 + k));
				periodEW = (int) Math.Ceiling(executionTime * 1.0 * (1 + k) / k);
			}
			/**
			 * Compute hyperperiod now.
			 */
			int []tempArray = new int[2];
			tempArray[0] = periodNS;
			tempArray[1] = periodEW;
			H = ComputeLCM.LCM(tempArray);

			Console.WriteLine("Period NS = {0} Period EW = {1} H = {2} e = {3}", periodNS, periodEW, H, executionTime);

			double util; // utilization of intersection
			util = executionTime * 1.0 / periodNS + executionTime * 1.0 / periodEW;

			if(util > 1.0)
			{
				Console.WriteLine("Utilization EXCEEDS one. Actual Value = {0}", util);
			}
			computeClockSchedule();
		}
Ejemplo n.º 6
0
        protected int[] arrivalVRoads; // Arrival rate on NS roads (normalized to have integral values)

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Class constructor
        /// </summary>
        /// <param name="ip">Inputparameters instance</param>
        public Algorithm(InputParameters ip)
        {
            arrivalVRoads = arrivalHRoads = null;
        }
Ejemplo n.º 7
0
        RoadPerfCounter roadCntr; // Performance counter for the road

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Class constructor.
        /// </summary>
        /// <param name="_roadNum">A number for the road</param>
        /// <param name="_orientation">Road's orientation</param>
        /// <param name="_ip">Reference of InputParameters object</param>
        /// <param name="_rN">Reference of Random class object</param>
        /// <param name="_algo">Reference of Algorithm class object</param>
        /// <param name="ns">Reference of network simulator</param>
        public Road(int _roadNum, int _orientation, InputParameters _ip, Random _rN, Algorithm _algo, Network ns)
        {
            /**
             * Set the values of the parameters
             */
            roadNum = _roadNum;
            orient = _orientation;
            this.ip = _ip;
            this.rN = _rN;
            this.schedule = _algo;

            this.ns = ns;
            //			totalDelay = totalStopCount = 0;
            //			numPlatoonArrivals = numVehDepartures = numPlatoonDepartures = 0;

            R = ip.lengthR;
            W = ip.lengthW;

            lastArrivalDownLane = lastArrivalUpLane = 0;

            /**
             * Instantiate poisson generator object.
             */
            pG = new PoissonGenerator();

            /**
             * Instantiate Road Perf Counter object.
             */
            roadCntr = new RoadPerfCounter();

            /**
             * Calculate the total number of intersections
             */
            if(orient == RoadOrientation.NS)
                numIntersection = ip.numHRoads;
            else
                numIntersection = ip.numVRoads;

            /**
             * Compute road's start and end coordinates based on the number of intersecting roads. The road is one
             * segment longer than the last intersection.
             */
            startPos = 0;
            endPos = numIntersection * (R + W) + R;

            /**
             * Compute the start position of each intersection
             */
            posnIntersection = new int[numIntersection];

            posnIntersection[0] = R;	// Position of first intersection
            // position of all subsequent intersections
            for(int i = 1; i < numIntersection; i++)
                posnIntersection[i] = (i + 1)* (R + W) - W;

            upLane = new  ArrayList();
            downLane = new ArrayList();
            downQueue = new Queue();
            upQueue = new Queue();

            /**
             * Obtain platoon generation characteristics from the algorithm object
             */
            obtainPlatoonCreationParameters(0);
        }