Exemplo n.º 1
0
        private void Iteration(Label label)
        {
            for (int i = this.currentModellingDay; i < this.modelingDays; i++)
            {
                this.currentModellingDay = i;
                if (this.stopFlag == true)
                {
                    pauseDone.Set();
                    break;
                }

                DateTime workdayStartTime = startTime.AddDays(i);
                modelTime = workdayStartTime;

                int[] materialsNumToday = new int[12];
                for (int j=0;j<12;j++)
                {
                    //***materialsNumToday[j]=this.storage.GetMaterialNumberFromID(j+1);
                    //возвращает со склада в materialsNumToday[j] количество материала с номером (j + 1)
                    this.storage.Materials.GetMaterial(j + 1, out materialsNumToday[j]);
                }
                this.storage.AddMaterialsStatisticDay(materialsNumToday);
                Demand[] newDemands = this.generator.GenerateDemands(workdayStartTime);
                int[] modifyDemandsTime = this.generator.GenerateModifyTime();

                ////////////////////////////////////////////////// Обращение к back-office
                this.storage.ClearAllPlan();
                this.storage.AddDailyPlan(this.backOffice.GetDailyPlan(modelTime,ref this.storage));

                int rem = -1;
                int span = (int)(modelTime-startTime).TotalDays;
                Math.DivRem(span,Params.DELIVERY_PERIOD,out rem);
                if ((rem == 0)&&(span!=0))
                {
                    DeliveryDemand delivery = this.backOffice.GetDeliveryDemands(modelTime);
                    if (delivery != null)
                    {
                        DeliveryDemand[] dlvr = new DeliveryDemand[1] { delivery }; // это мне было лень generator.modifyDeliveries() переписывать
                        this.storage.AddDeliveryDemand(this.generator.ModifyDeliveries(dlvr).ElementAt(0));
                    }
                }
                //////////////////////////////////////////////////

                int runtimeModifyDemandsSumTime = 0;
                int newDemandInd = 0;
                int modifyDemandInd = 0;
                int nextPlanElemEndTime = 0;
                int todayWorkTime = 0;
                PlanReportElement curPlanElem = new PlanReportElement();
                try
                {
                    if (this.storage.GetFirstPlanElement().DemandID != 0)
                    {
                        int prodId = this.storage.GetFirstPlanElement().ProductID;
                        nextPlanElemEndTime = Params.Products[prodId].Time;
                        bool canDo = this.storage.Materials.TakeAwayMaterialCluster(Params.Products[prodId].Materials);
                        if (canDo == false) throw new Exception("Не достаточно материалов для производства товара");
                        /*
                        for (int j = 1; j <= CParams.MATERIALS_NUMBER; j++)
                        {
                            //***int materialNum = CParams.m_products[this.storage.GetFirstPlanElement().m_iProductID].m_iMaterials[j - 1];
                            int materialNum = 0;
                            //получает в materialNum количество материала № j нужное для производства продукта
                            CParams.m_products[this.storage.GetFirstPlanElement().m_iProductID].m_materials.GetMaterial(j, out materialNum);

                            if (materialNum > 0)
                            {
                                //***this.storage.DeleteMaterialNumber(j, materialNum);
                                //удаление со склада от материала с номером j количества materialNum
                                this.storage.m_materials.TakeAwayMaterial(j, materialNum);
                            }
                        }
                        */
                    }
                    else
                    {
                        nextPlanElemEndTime = Params.RetargetTimes[this.storage.GetFirstPlanElement().ProductID-1];
                    }
                    curPlanElem.StartExecuteDate=modelTime;
                }
                catch
                {
                    nextPlanElemEndTime = -1;
                }
                if (nextPlanElemEndTime != -1) todayWorkTime += nextPlanElemEndTime;

                bool endOfDayFlag = false;

                int k = 0;
                while (endOfDayFlag == false)
                {
                    k++;

                    int newDemandNextTime = -1;
                    int modifyDemandNextTime = -1;
                    int nextDeliveryDemandTime = this.storage.GetNextDeliveryDemandTime(modelTime);

                    if (newDemandInd < newDemands.Length)
                    {
                        newDemandNextTime = (int)(newDemands[newDemandInd].GettingDate - modelTime).TotalMinutes;
                    }
                    if (modifyDemandInd < modifyDemandsTime.Length)
                    {
                        runtimeModifyDemandsSumTime = modifyDemandsTime.Take(modifyDemandInd).Sum() + modifyDemandsTime[modifyDemandInd];
                        modifyDemandNextTime = runtimeModifyDemandsSumTime -(int)(modelTime - workdayStartTime).TotalMinutes;
                    }

                    if ((nextPlanElemEndTime == -1) && (newDemandNextTime == -1) &&
                        (modifyDemandNextTime == -1) && (nextDeliveryDemandTime == -1))
                        endOfDayFlag = true;

                    else
                    {
                        List<int> nextTimes = new List<int>();
                        if (nextPlanElemEndTime != -1) nextTimes.Add(nextPlanElemEndTime);
                        if (newDemandNextTime != -1) nextTimes.Add(newDemandNextTime);
                        if (modifyDemandNextTime != -1) nextTimes.Add(modifyDemandNextTime);
                        if (nextDeliveryDemandTime != -1) nextTimes.Add(nextDeliveryDemandTime);

                        modelTime = modelTime.AddMinutes(nextTimes.Min());
                        if (modelTime.Day != workdayStartTime.Day) break;
                        setLabelText(label, "Модельное время: " + modelTime.ToString());

                        if (nextTimes.Min() == newDemandNextTime)
                        {
                            bool approved = backOffice.ApproveDemand(ref newDemands[newDemandInd]);
                            if (approved == true) storage.AddAcceptedDemand(newDemands[newDemandInd]);
                            else storage.AddDeclinedDemand(newDemands[newDemandInd]);
                            newDemandInd++;
                        }

                        if (nextTimes.Min() == modifyDemandNextTime)
                        {
                            var notFinishedDemands = this.storage.GetNotFinishedDemands();
                            if (notFinishedDemands.Count() > 0)
                            {
                                Demand modifiedDemand = this.generator.ModifyDemand(notFinishedDemands.ToArray(), modelTime);
                                Demand demand;
                                this.storage.GetAcceptedDemand(modifiedDemand.ID, out demand);
                                bool approved = backOffice.ApproveModifyDemand(modelTime, ref modifiedDemand, demand);
                                if (approved == true) this.storage.ModifyDemand(modifiedDemand);
                                this.storage.AddModifyStatistic(approved);
                            }
                            modifyDemandInd++;
                        }

                        if ((nextTimes.Min() != nextPlanElemEndTime) && (nextPlanElemEndTime != -1))
                            nextPlanElemEndTime = nextPlanElemEndTime - nextTimes.Min();

                        if (nextTimes.Min() == nextPlanElemEndTime)
                        {
                            PlanElement planElem = this.storage.GetFirstPlanElementAndDelete();
                            curPlanElem.PlanElement = planElem;
                            curPlanElem.EndExecuteDate = modelTime;
                            this.storage.AddPlanReportElement(curPlanElem);
                           // backOffice.reportPlanElem(curPlanElem);   отчёт в бекофис не надо
                            if ((planElem.DemandID!=0)&&(this.storage.IsDemandDone(planElem.DemandID) == true))
                                this.storage.FinishDemand(planElem.DemandID, modelTime);

                            curPlanElem = new PlanReportElement();
                            try
                            {
                                if (this.storage.GetFirstPlanElement().DemandID != 0)
                                {
                                    int prodId = this.storage.GetFirstPlanElement().ProductID;
                                    nextPlanElemEndTime = Params.Products[prodId].Time;
                                    bool canDo = this.storage.Materials.TakeAwayMaterialCluster(Params.Products[prodId].Materials);
                                    if (canDo == false) throw new Exception("Не достаточно материалов для производства товара");
                                    /*
                                    for (int j = 1; j <= CParams.MATERIALS_NUMBER; j++)
                                    {
                                        //***int materialNum = CParams.m_products[this.storage.GetFirstPlanElement().m_iProductID + 1].m_iMaterials[j - 1];
                                        int materialNum = 0;
                                        //получает в materialNum количество материала № j нужное для производства продукта
                                        CParams.m_products[this.storage.GetFirstPlanElement().m_iProductID].m_materials.GetMaterial(j, out materialNum);

                                        if (materialNum > 0)
                                        {
                                            //***this.storage.DeleteMaterialNumber(j, materialNum);
                                            //удаление со склада от материала с номером j количества materialNum
                                            this.storage.m_materials.TakeAwayMaterial(j, materialNum);
                                        }
                                    }
                                    */
                                }
                                else
                                {
                                    nextPlanElemEndTime = Params.RetargetTimes[this.storage.GetFirstPlanElement().ProductID-1];
                                }
                                curPlanElem.StartExecuteDate = modelTime;

                            }
                            catch
                            {
                                nextPlanElemEndTime = -1;
                            }
                            if (nextPlanElemEndTime != -1) todayWorkTime += nextPlanElemEndTime;
                            if (nextPlanElemEndTime != -1)    // костыль для реализации круглосуточной работы
                            {
                                TimeSpan nextPlanElemEndTimeSpan = new TimeSpan(0,nextPlanElemEndTime,0);
                                DateTime nextWorkDayStartTime = workdayStartTime + new TimeSpan(1,0,0,0);
                                if (modelTime + nextPlanElemEndTimeSpan > nextWorkDayStartTime)
                                {
                                    nextPlanElemEndTime = (int)(nextWorkDayStartTime - modelTime).TotalMinutes - this.storage.GetPlanElementsToGo();
                                }
                            }
                        }

                        if (nextTimes.Min() == nextDeliveryDemandTime)
                        {
                            DeliveryDemand[] deliveryDemands = this.storage.GetDeliveryDemand(modelTime);
                            if (deliveryDemands.Length > 0)
                            {
                                foreach (DeliveryDemand d in deliveryDemands)
                                {
                                    this.storage.Materials.AddMaterialCluster(d.MaterialsDemand);
                                    /*
                                    for (int j = 1; j <= CParams.MATERIALS_NUMBER; j++)
                                    {
                                        //***int materialNum = d.m_materialsDemand[j];
                                        int materialNum = 0;
                                        d.m_materialsDemand.GetMaterial(j, out materialNum);

                                        //if (materialNum > 0)
                                        //{
                                            //***this.storage.AddMaterialNumber(j, materialNum);
                                            //добавление на склад количества materialNum материала номером j
                                            this.storage.m_materials.AddMaterial(j, materialNum);
                                        //}
                                    }
                                    */
                                    backOffice.ReportDeliveryDemand(d);
                                    d.IsDone = true;
                                }
                            }
                        }

                        //// Не нужно если план не меняется в течение дня

                        //if (nextPlanElemEndTime == -1)
                        //{
                        //    curPlanElem = new CPlanReportElement();
                        //    try
                        //    {
                        //        int prodId = this.storage.GetFirstPlanElement().m_iProductID;
                        //        nextPlanElemEndTime = CParams.m_products[prodId].m_iTime;
                        //        this.storage.m_materials.TakeAwayMaterialCluster(CParams.m_products[prodId].m_materials);
                        //        /*
                        //        for (int j = 1; j <= CParams.MATERIALS_NUMBER; j++)
                        //        {
                        //            //***int materialNum = CParams.m_products[this.storage.GetFirstPlanElement().m_iProductID + 1].m_iMaterials[j - 1];
                        //            int materialNum = 0;
                        //            //получает в materialNum количество материала № j нужное для производства продукта
                        //            CParams.m_products[this.storage.GetFirstPlanElement().m_iProductID].m_materials.GetMaterial(j, out materialNum);

                        //            if (materialNum > 0)
                        //            {
                        //                //***this.storage.DeleteMaterialNumber(j, materialNum);
                        //                //удаление со склада от материала с номером j количества materialNum
                        //                this.storage.m_materials.TakeAwayMaterial(j, materialNum);
                        //            }
                        //        }
                        //        */
                        //        curPlanElem.m_dtStartExecute = modelTime;
                        //    }
                        //    catch
                        //    {
                        //        nextPlanElemEndTime = -1;
                        //    }
                        //}

                    }

                }

                //this.storage.SaveIdleStatistic((double)(CParams.WORKDAY_MINUTES_NUMBER - todayWorkTime) / CParams.WORKDAY_MINUTES_NUMBER);
                this.storage.SaveDemandAverageDelayStatistic();
                this.storage.SaveFinishedDemandsPerDayStatistic();
                this.storage.SaveCanceledDemandsPerDayStatistic();
            }
            if (stopFlag!=true) setLabelText(label, "Модельное время: " + modelTime.ToString() + "\nМоделирование завершено");
            return;
        }
Exemplo n.º 2
0
 /// <summary>
 /// Добавить элемент выполнения плана
 /// </summary>
 /// <param name="planReportElement"></param>
 /// <returns></returns>
 public bool AddPlanReportElement(PlanReportElement planReportElement)
 {
     planReport.Add(planReportElement.EndExecuteDate, planReportElement);
     //!!! Здесь при добавлении еще нужно упорядочивать элементы
     return true;
 }