Пример #1
0
        private void Method2(int length, IScriptReader scriptReader)
        {
            var generator = new GenerateName();

            var able = new Able {IsIdle = true};
            var baker = new Baker {IsIdle = true};
            var charlie = new Charlie {IsIdle = true};

            _carhopses.Add(able);
            _carhopses.Add(baker);
            _carhopses.Add(charlie);

            var fel = new FutureEventList();
            var customersQueue = new Queue<ICustomer>(length);
            int totalTime = 0;

            for (int i = 0; i < length; i++)
            {
                int randomNumber = i == 0 ? 0 : Math.Abs(scriptReader.GetNormal(5, 5));
                totalTime += randomNumber;

                var customer = new Customer(totalTime, Math.Abs(scriptReader.GetNormal(10, 6)), generator)
                                   {ArrivalTimeNumber = randomNumber};

                var felItem = new FutureEventListRow
                                  {
                                      Time = totalTime,
                                      Customer = customer,
                                      EventType = EventType.Arrival
                                  };

                fel.PushEventRow(felItem);
                _globalList.PushEventRow(felItem);
            }

            while (fel.Length > 0)
            {
                FutureEventListRow nearestEvent = fel.GetNearestEvent();
                int currentTime = nearestEvent.Time;

                if (nearestEvent.EventType == EventType.Arrival)
                {
                    var nState = new State(!able.IsIdle, !baker.IsIdle, !charlie.IsIdle);
                    nearestEvent.Customer.OnArrivalSystemState = nState;

                    if (nState.IsAnyAvailable())
                    {
                        var availables = new List<Carhops>();
                        if (!nState.GetFirstStatus())
                            availables.Add(able);
                        if (!nState.GetSecondStatus())
                            availables.Add(baker);
                        if (!nState.GetThirdStatus())
                            availables.Add(charlie);

                        Carhops selectedCarhops = null;

                        int numb = 0;
                        switch (availables.Count)
                        {
                            case 1:
                                selectedCarhops = availables[0];
                                break;
                            case 2:
                                numb = _randomGenerator.Next(0, 20)/10;
                                if (numb >= 0 && numb < 1)
                                    selectedCarhops = availables[0];
                                else if (numb >= 1 && numb < 2)
                                    selectedCarhops = availables[1];
                                break;
                            case 3:
                                numb = _randomGenerator.Next(0, 30)/10;
                                if (numb >= 0 && numb < 1)
                                    selectedCarhops = availables[0];
                                else if (numb >= 1 && numb < 2)
                                    selectedCarhops = availables[1];
                                else if (numb >= 2 && numb < 3)
                                    selectedCarhops = availables[2];
                                break;
                            default:
                                break;
                        }
                        if (selectedCarhops != null)
                        {
                            nearestEvent.Customer.ServiceProvider = selectedCarhops;
                            selectedCarhops.CurrentCustomer = nearestEvent.Customer;

                            var felItem = new FutureEventListRow
                                              {
                                                  Time = nearestEvent.Customer.ServiceTime + currentTime,
                                                  Customer = nearestEvent.Customer,
                                                  EventType = EventType.Departure,
                                              };

                            fel.PushEventRow(felItem);
                            _globalList.PushEventRow(felItem);
                            selectedCarhops.IsIdle = false;
                        }
                        else
                        {
                            customersQueue.Enqueue(nearestEvent.Customer);
                        }
                    }
                    else
                    {
                        customersQueue.Enqueue(nearestEvent.Customer);
                    }
                }
                else if (nearestEvent.EventType == EventType.Departure)
                {
                    Carhops currentCarhop = nearestEvent.Customer.ServiceProvider;
                    currentCarhop.IsIdle = true;

                    var enCustomer = new Customer(nearestEvent.Customer.ArrivalTime, nearestEvent.Customer.ServiceTime,
                                                  generator)
                                         {
                                             ServiceProvider = nearestEvent.Customer.ServiceProvider,
                                             DepartureTime = currentTime,
                                             ArrivalTimeNumber = nearestEvent.Customer.ArrivalTimeNumber,
                                             OnArrivalSystemState = nearestEvent.Customer.OnArrivalSystemState
                                         };
                    enCustomer.WaitTime = enCustomer.DepartureTime - (enCustomer.ArrivalTime + enCustomer.ServiceTime);

                    var nState = new State(!able.IsIdle, !baker.IsIdle, !charlie.IsIdle);
                    enCustomer.AfterDepartureState = nState;

                    if (enCustomer.WaitTime < 0)
                        throw new InvalidDataException();

                    _solvedData.Add(enCustomer);

                    if (customersQueue.Count > 0)
                    {
                        // his work is done!
                        ICustomer firstInQueue = customersQueue.Dequeue();

                        firstInQueue.ServiceProvider = currentCarhop;

                        currentCarhop.CurrentCustomer = firstInQueue;
                        currentCarhop.IsIdle = false;

                        var felItem = new FutureEventListRow
                                          {
                                              Time = firstInQueue.ServiceTime + currentTime,
                                              Customer = firstInQueue,
                                              EventType = EventType.Departure
                                          };
                        fel.PushEventRow(felItem);
                        _globalList.PushEventRow(felItem);
                    }
                    else
                    {
                        if (nearestEvent.Customer.ServiceProvider is Able)
                        {
                            able.IsIdle = true;
                            able.CurrentCustomer = null;
                        }
                        else if (nearestEvent.Customer.ServiceProvider is Baker)
                        {
                            baker.IsIdle = true;
                            baker.CurrentCustomer = null;
                        }
                        else if (nearestEvent.Customer.ServiceProvider is Charlie)
                        {
                            charlie.IsIdle = true;
                            charlie.CurrentCustomer = null;
                        }
                    }
                }
                fel.RemoveFirstElement();
            }
        }
Пример #2
0
        private void Method1(int length, IScriptReader scriptReader)
        {
            var generator = new GenerateName();

            var able = new Able {IsIdle = true};
            var baker = new Baker {IsIdle = true};
            var charlie = new Charlie {IsIdle = true};

            _carhopses.Add(able);
            _carhopses.Add(baker);
            _carhopses.Add(charlie);

            var fel = new FutureEventList();
            var customersQueue = new Queue<ICustomer>(length);
            int totalTime = 0;

            for (int i = 0; i < length; i++)
            {
                int randomNumber = i == 0 ? 0 : Math.Abs(scriptReader.GetNormal(5, 5));
                totalTime += randomNumber;

                var customer = new Customer(totalTime, Math.Abs(scriptReader.GetNormal(10, 6)), generator)
                                   {ArrivalTimeNumber = randomNumber};

                var felItem = new FutureEventListRow
                                  {
                                      Time = totalTime,
                                      Customer = customer,
                                      EventType = EventType.Arrival
                                  };

                fel.PushEventRow(felItem);
                _globalList .PushEventRow(felItem);
            }

            while (fel.Length > 0)
            {
                FutureEventListRow nearestEvent = fel.GetNearestEvent();
                int currentTime = nearestEvent.Time;

                if (nearestEvent.EventType == EventType.Arrival)
                {
                    var nState = new State(!able.IsIdle, !baker.IsIdle, !charlie.IsIdle);
                    nearestEvent.Customer.OnArrivalSystemState = nState;
                    if (able.IsIdle)
                    {
                        nearestEvent.Customer.ServiceProvider = able;
                        able.CurrentCustomer = nearestEvent.Customer;

                        var felItem = new FutureEventListRow
                                          {
                                              Time = nearestEvent.Customer.ServiceTime + currentTime,
                                              Customer = nearestEvent.Customer,
                                              EventType = EventType.Departure,
                                          };

                        fel.PushEventRow(felItem);
                        _globalList.PushEventRow(felItem);

                        able.IsIdle = false;
                    }
                    else if (baker.IsIdle)
                    {
                        nearestEvent.Customer.ServiceProvider = baker;
                        baker.CurrentCustomer = nearestEvent.Customer;

                        var felItem = new FutureEventListRow
                                          {
                                              Time = nearestEvent.Customer.ServiceTime + currentTime,
                                              Customer = nearestEvent.Customer,
                                              EventType = EventType.Departure
                                          };

                        fel.PushEventRow(felItem);
                        _globalList.PushEventRow(felItem);
                        baker.IsIdle = false;
                    }
                    else if (charlie.IsIdle)
                    {
                        nearestEvent.Customer.ServiceProvider = charlie;
                        charlie.CurrentCustomer = nearestEvent.Customer;

                        var felItem = new FutureEventListRow
                                          {
                                              Time = nearestEvent.Customer.ServiceTime + currentTime,
                                              Customer = nearestEvent.Customer,
                                              EventType = EventType.Departure
                                          };
                        fel.PushEventRow(felItem);
                        _globalList.PushEventRow(felItem);
                        charlie.IsIdle = false;
                    }
                    else
                    {
                        customersQueue.Enqueue(nearestEvent.Customer);
                    }
                }
                else if (nearestEvent.EventType == EventType.Departure)
                {
                    Carhops currentCarhop = nearestEvent.Customer.ServiceProvider;
                    currentCarhop.IsIdle = true;

                    var enCustomer = new Customer(nearestEvent.Customer.ArrivalTime, nearestEvent.Customer.ServiceTime,
                                                  generator)
                                         {
                                             ServiceProvider = nearestEvent.Customer.ServiceProvider,
                                             DepartureTime = currentTime,
                                             ArrivalTimeNumber = nearestEvent.Customer.ArrivalTimeNumber,
                                             OnArrivalSystemState = nearestEvent.Customer.OnArrivalSystemState
                                         };
                    enCustomer.WaitTime = enCustomer.DepartureTime - (enCustomer.ArrivalTime + enCustomer.ServiceTime);

                    var nState = new State(!able.IsIdle, !baker.IsIdle, !charlie.IsIdle);
                    enCustomer.AfterDepartureState = nState;

                    if (enCustomer.WaitTime < 0)
                        throw new InvalidDataException();

                    _solvedData.Add(enCustomer);

                    if (customersQueue.Count > 0)
                    {
                        // his work is done!
                        ICustomer firstInQueue = customersQueue.Dequeue();

                        firstInQueue.ServiceProvider = currentCarhop;

                        currentCarhop.CurrentCustomer = firstInQueue;
                        currentCarhop.IsIdle = false;

                        var felItem = new FutureEventListRow
                                          {
                                              Time = firstInQueue.ServiceTime + currentTime,
                                              Customer = firstInQueue,
                                              EventType = EventType.Departure
                                          };
                        fel.PushEventRow(felItem);
                        _globalList.PushEventRow(felItem);
                    }
                    else
                    {
                        if (nearestEvent.Customer.ServiceProvider is Able)
                        {
                            able.IsIdle = true;
                            able.CurrentCustomer = null;
                        }
                        else if (nearestEvent.Customer.ServiceProvider is Baker)
                        {
                            baker.IsIdle = true;
                            baker.CurrentCustomer = null;
                        }
                        else if (nearestEvent.Customer.ServiceProvider is Charlie)
                        {
                            charlie.IsIdle = true;
                            charlie.CurrentCustomer = null;
                        }
                    }
                }
                fel.RemoveFirstElement();
            }
        }