コード例 #1
0
ファイル: Driver.cs プロジェクト: iveyas/iveyas.github.io
        static void Main(string[] args)
        {
            Queue <Registrants>         line1    = new Queue <Registrants>();
            Queue <Registrants>         line2    = new Queue <Registrants>();
            List <Queue <Registrants> > listOfQs = new List <Queue <Registrants> >();

            listOfQs.Add(line1);
            listOfQs.Add(line2);

            nextInLine = new Registrants[(listOfQs.Count)];

            TimeSpan timeOpen = new TimeSpan(8, 0, 0);
            //double time = 360000;                               //in ticks or 1/10 of a sec

            int expected = 1000;//Poisson(1000);      //TODO test

            patronIdListFromPoisson = new List <int>(expected);
            for (int i = 0; i < expected; i++)
            {
                patronIdListFromPoisson.Add(i);
            }
            RandomizeList(patronIdListFromPoisson);
            //foreach (var item in fishNum)
            //{
            //    Console.WriteLine(item);
            //}

            //TimeSpan enqueueRate = time / expected;

            openTime    = new DateTime(2016, 11, 1, 7, 0, 0, 0);
            closingTime = new DateTime(2016, 11, 1, 17, 0, 0, 0);
            //TimeSpan timeOpen = (closingTime - openTime);

            TimeSpan tick = new TimeSpan(1000000);                  //tick = .1 sec
            //TimeSpan tick = new TimeSpan(1000000 * 1000);               //tick = 100 sec
            TimeSpan enqueueRate = new TimeSpan(timeOpen.Ticks / expected);


            currentTime = openTime;                                 //set initial current time
            TimeSpan patronEntranceThreshold = new TimeSpan();      //increases until this >= the enqueue rate
            int      patronCounter           = 0;                   //used for index of the randomized patronNumber list

            //continue simulation until
            while (currentTime < closingTime)
            {
                //Console.WriteLine(enqueueRate);
                //Console.WriteLine(patronEntranceThreshold);

                if (patronCounter < patronIdListFromPoisson.Count && enqueueRate <= patronEntranceThreshold)
                {
                    //Console.WriteLine(enqueueRate);
                    //Console.WriteLine(patronEntranceThreshold);

                    //patron comes in, they choose the shortest line

                    Registrants newGuy = new Registrants(patronIdListFromPoisson[patronCounter]);
                    newGuy.lineChoice = ConventionRegistration.ShortestLine(listOfQs);
                    listOfQs[ConventionRegistration.ShortestLine(listOfQs)].Enqueue(newGuy);

                    patronCounter++;                  //enqueue the next person next time
                    patronEntranceThreshold = new TimeSpan(0);
                }

                int lineChoice = -1;
                if (windows.Count > 0)
                {
                    //Console.WriteLine(windows.Peek().Arrival.Time + windows.Peek().windowTime);
                    //Console.WriteLine(currentTime);

                    //while there are patrons in the PQ and they have waited (since they arrived at window + windowTime) until currentTime is <=
                    //(while) instead of (if) incase multiple patron's wait time is up in same tick
                    while (windows.Count > 0 && (windows.Peek().Arrival.Time + windows.Peek().Arrival.windowTime) <= currentTime)
                    {
                        lineChoice = windows.Peek().lineChoice;                     //remember which queue the patron is leaving from

                        Console.WriteLine(windows.Dequeue().ToString());
                        //Console.WriteLine(windows.Peek().ToString());
                        foreach (var item in listOfQs[lineChoice].ToArray())
                        {
                            //if()
                            //Console.WriteLine(item.PatronNum);
                        }

                        //this is the only way patrons are dequeued
                        windows.Dequeue();                                          //the patron leaves the PQ (window)
                        listOfQs[lineChoice].Dequeue();                             //the patron leaves the queue (line they were waiting in)

                        //if there is someone next in that line, they enter the PQ (approach the window) and are assigned a wait time
                        if (listOfQs[lineChoice].Count > 0)
                        {
                            windows.Enqueue(listOfQs[lineChoice].Peek());                           //new patron enters PQ (approaches window)
                            listOfQs[lineChoice].Peek().Arrival = new Evnt(currentTime);            //assign wait time
                        }
                        //Console.WriteLine(107);
                    }
                }

                //set nextInLine array
                //this is currently unused
                for (int i = 0; i < listOfQs.Count; i++)
                {
                    if (listOfQs[i].Count > 0)
                    {
                        nextInLine[i] = listOfQs[i].Peek();
                    }
                }

                //This fills the PQ initially, otherwise they are
                //pulled into the PQ when someone leaves.
                if (windows.Count < listOfQs.Count)                     //if the PQ (windows) are not full
                {
                    for (int i = 0; i < listOfQs.Count; i++)            //for every queue
                    {
                        if (listOfQs[i].Count > 0)
                        {
                            windows.Enqueue(listOfQs[i].Peek());                    //First in line enters PQ
                            listOfQs[i].Peek().Arrival = new Evnt(currentTime);     //and gets window wait time
                        }
                    }
                }

                currentTime             += tick;
                patronEntranceThreshold += tick;

                //for (int i = 0; i < listOfQs.Count; i++)
                //{
                //    Registrants[] tempArr = listOfQs[i].ToArray();

                //    List<int> intList = new List<int>(tempArr.Length);
                //    for (int j = 0; j < tempArr.Length; j++)
                //    {
                //        //intList.Add(100);
                //        intList.Add(tempArr[j].PatronNum);
                //    }
                //    listToPrint.Add(intList);
                //}
                //string listDisplay = "";

                //listDisplay = $"\t\tRegistration Windows\n"
                //            + "\t\t--------------------\n";

                //for(int i = 0; i < listOfQs.Count; i++)
                //{
                //    listDisplay += $"\t W {i}";
                //}

                //listDisplay += "\n";
                //int max = 0;
                //for (int i = 0; i < listOfQs.Count; i++)
                //{
                //    if (listOfQs[i].Count> max)
                //    {
                //        max = listOfQs[i].Count;
                //    }
                //}

                //for (int i = 0; i < max; i++)
                //{
                //    for (int j = 0; j < listOfQs.Count; j++)
                //    {
                //        try
                //        {
                //            listDisplay += "\t" + listToPrint[j][i];
                //        }
                //        catch
                //        {
                //            listDisplay+= "\t    ";
                //        }
                //    }
                //}

                //Console.WriteLine(156);
                //Thread.Sleep(20);
                //    Console.WriteLine("its time");

                ListOfQueues print = new ListOfQueues(listOfQs);
                Console.WriteLine(print.ToString());
                //Console.WriteLine(listDisplay);
            }//end while

            Console.ReadLine();
        }
コード例 #2
0
        public int CompareTo(object registrant)
        {
            Registrants e = (Registrants)registrant;

            return(e.windowTime.CompareTo(windowTime));
        }