private void AllocateSlot(Air air, Maintainance nearestSchedule)
        {
            int airInSchedule = nearestSchedule.Slot.Where(f => f != null).Count();
            int nextIndex     = Matrix.MaintainanceCollection.LastIndexOf(nearestSchedule);

            Trace.WriteLine("airInSchedule " + airInSchedule);
            if (airInSchedule < 5)
            {
                while (true)
                {
                    int random = new Random().Next(nearestSchedule.Slot.Length - 1);
                    //Trace.WriteLine("Random " + random);
                    if (nearestSchedule.Slot[random] == null)
                    {
                        nearestSchedule.Slot[random] = air;
                        break;
                    }
                }
                Trace.WriteLine("Maintainance Start Time : " + nearestSchedule.MaintainanceStartedTime);
                Trace.WriteLine("Slot Capacity : " + nearestSchedule.Slot.Where(f => f != null).Count());
            }
            else if (nextIndex + 1 != Matrix.MaintainanceCollection.Count)
            {
                AllocateSlot(air, Matrix.MaintainanceCollection[nextIndex + 1]);
                Trace.WriteLine("Maintainance Start Time : " + Matrix.MaintainanceCollection[nextIndex + 1].MaintainanceStartedTime);
                Trace.WriteLine("Slot Capacity : " + Matrix.MaintainanceCollection[nextIndex + 1].Slot.Where(f => f != null).Count());
            }
        }
Example #2
0
 public static Maintainance NearestSchedule(Air air)
 {
     foreach (var i in MaintainanceCollection.Where(f => f.MaintainanceStartedTime >= air.ArrivalTime))
     {
         return(i);
     }
     return(null);
 }
Example #3
0
        public static Maintainance AddAirToMaintain(Air air)
        {
            Maintainance m = NearestSchedule(air);
            Maintainance alcocatedSchdule = null;

            try
            {
                alcocatedSchdule = m.AddAir(air);
            }
            catch (Exception)
            {            /*An exception will be occured when it's run to latest maintaince.*/
            }
            return(alcocatedSchdule);
        }
        public Maintainance AddAir(Air air)
        {
            Maintainance nearestSchedule = Matrix.NearestSchedule(air);

            if (nearestSchedule == null)
            {
                return(null);
            }
            else
            {
                //Try first nearest schedule.
                this.AllocateSlot(air, nearestSchedule);
            }

            return(nearestSchedule);
        }
Example #5
0
        private static Maintainance SwapSlot(Maintainance schedule, List <ColumnRank> improveColumns, List <ColumnRank> allRankedColumns)
        {
            foreach (var i in improveColumns)
            {
                List <ColumnRank> destCols = allRankedColumns.FindAll(f => {
                    return(!improveColumns.Exists(g => g.ColumnIndex == f.ColumnIndex));
                });



                if (destCols.Count > 0)
                {
                    int selectedPoorIndex = new Random(Guid.NewGuid().GetHashCode()).Next(0, destCols.Count - 1);
                    if (destCols.Count == 2)
                    {
                        selectedPoorIndex = Convert.ToInt32(Math.Abs(Guid.NewGuid().GetHashCode() % 2));
                    }
                    else if (destCols.Count == 1)
                    {
                    }
                    ColumnRank swapCol = destCols.ElementAt(selectedPoorIndex);


                    Air iDestAir = schedule.Slot[swapCol.ColumnIndex];
                    if (iDestAir != null)
                    {
                        //Clone Air object from destination array then remove it.
                        Air sourceAir = schedule.Slot[swapCol.ColumnIndex];
                        schedule.Slot[swapCol.ColumnIndex] = null;

                        //Assign cloning object to improve index.
                        schedule.Slot[i.ColumnIndex] = sourceAir;
                    }
                }
                else
                {
                    Trace.WriteLine("Poor column were not found!");
                }
            }


            return(schedule);
        }
Example #6
0
        public static List <Maintainance> Fixation(List <Maintainance> schedules)
        {
            //List<ColumnRank> cImproved = null;
            //while (cImproved == null || cImproved.Count > 0)
            //{
            //    cImproved = GetRankedColumns(schedules).Where(f => f.MustImprove).ToList();
            //    if (cImproved.Count != 0)
            //    {
            //        for (int i = 0; i < schedules.Count; i++)
            //        {
            //            List<ColumnRank> iColRank = GetRankedColumns(schedules);
            //            schedules[i] = SwapSlot(schedules[i], cImproved, iColRank);
            //        }
            //    }

            //}

            List <KeyValuePair <int, Air> > cLog = new List <KeyValuePair <int, Air> >();
            int count = 0;

            foreach (Maintainance i in schedules)
            {
                for (int j = 0; j < i.Slot.Length; j++)
                {
                    if (IndexedColumns.Contains(j) && i.Slot[j] != null)
                    {
                        IndexedColumns.Remove(j);
                    }
                    else
                    {
                        if (i.Slot[j] != null)
                        {
                            cLog.Add(new KeyValuePair <int, Air>(
                                         count,
                                         Program.Clone(i.Slot[j])
                                         ));
                            i.Slot[j] = null;
                        }
                    }
                }
                count++;
            }

            while (IndexedColumns.Count != 0 || cLog.Count != 0)
            {
                if (IndexedColumns.Count == 0)
                {
                    break;
                }
                int ran = IndexedColumns[0];
                foreach (var i in cLog)
                {
                    schedules[i.Key].Slot[ran] = i.Value;
                    IndexedColumns.Remove(ran);
                    break;
                }
                if (cLog.Count > 0)
                {
                    cLog.RemoveAt(0);
                }
            }


            List <int> cRandom = new List <int>();

            for (int i = 0; i < Program.MAX_AIR; i++)
            {
                cRandom.Add(i);
            }
            cRandom = new List <int>(RandomizeInt(cRandom.ToArray()));


            foreach (var i in schedules)
            {
                if (cRandom.Count != 0)
                {
                    for (int j = 0; j < i.Slot.Length; j++)
                    {
                        Air jAir            = i.Slot[j];
                        int jNewPosition    = cRandom[0];
                        Air jNewAirPosition = i.Slot[jNewPosition];
                        if (jAir != null && j != jNewPosition && jNewAirPosition != null)
                        {
                            i.Slot[jNewPosition] = Program.Clone(jAir);
                            i.Slot[j]            = null;
                            cRandom.Remove(jNewPosition);
                            break;
                        }
                        else if (j == jNewPosition)
                        {
                            cRandom.Remove(j);
                            break;
                        }
                    }
                }
            }



            return(schedules);
        }
Example #7
0
        public static void AppendText(string fileName, string msg, CalucationData cal, List <MatrixCompare> cCompare)
        {
            if (!Directory.Exists("c:/simu"))
            {
                Directory.CreateDirectory("c:/simu");
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("<html><head>{0}</head><body>", CSS);

            sb.AppendLine(string.Format("<h2>{0}</h2>", fileName));

            sb.AppendLine("<div style='float:left'><table class='sample'>");

            sb.AppendFormat(BODY, "T", cal.T);
            sb.AppendFormat(BODY, "TT", cal.TT);
            sb.AppendFormat(BODY, "Iteration", cal.Iteration);
            sb.AppendFormat(BODY, "SigmaT", cal.SigmaT);
            sb.AppendFormat(BODY, "DeltaT", cal.DeltaT);
            sb.AppendFormat(BODY, "TZero", cal.TZero);
            sb.AppendFormat(BODY, "AlphaExpoential", cal.AlphaExpoential);
            sb.AppendFormat(BODY, "K", cal.K);
            sb.AppendFormat(BODY, "Gramma", cal.Gramma);
            sb.AppendFormat(BODY, "CurrentT", cal.CurrentT);
            sb.AppendFormat(BODY, "PreviouseT", cal.PreviouseT);
            sb.AppendFormat(BODY, "TotalWatingTime", cal.TotalWatingTime);



            sb.AppendLine("</table></div>");


            sb.AppendLine("<div style='float:left'><table class='sample'>");

            sb.AppendFormat(BODY, "Adaptive1", cal.Adaptive1);
            sb.AppendFormat(BODY, "Adaptive2", cal.Adaptive2);
            sb.AppendFormat(BODY, "Adaptive3", cal.Adaptive3);
            sb.AppendFormat(BODY, "Adaptive4", cal.Adaptive4);
            sb.AppendFormat(BODY, "Exponential", cal.Exponential);
            sb.AppendFormat(BODY, "Geometric", cal.Geometric);
            sb.AppendFormat(BODY, "Linear", cal.Linear);
            sb.AppendFormat(BODY, "Logarithmic", cal.Logarithmic);

            sb.AppendLine("</table></div>");

            sb.AppendLine("<div style='float:left'><table class='sample'>");

            sb.AppendFormat(BODY, "ProbAdaptive1", cal.ProbAdaptive1);
            sb.AppendFormat(BODY, "ProbAdaptive2", cal.ProbAdaptive2);
            sb.AppendFormat(BODY, "ProbAdaptive3", cal.ProbAdaptive3);
            sb.AppendFormat(BODY, "ProbAdaptive4", cal.ProbAdaptive4);
            sb.AppendFormat(BODY, "ProbExponential", cal.ProbExponential);
            sb.AppendFormat(BODY, "ProbGeometric", cal.ProbGeometric);
            sb.AppendFormat(BODY, "ProbLinear", cal.ProbLinear);
            sb.AppendFormat(BODY, "ProbLogarithmic", cal.ProbLogarithmic);

            sb.AppendLine("</table></div>");


            sb.AppendLine("<br />");
            sb.AppendLine("<br />");

            sb.AppendLine("<br />");
            sb.AppendLine("<div style='clear:both;'>&nbsp;</div>");
            sb.AppendLine("<hr />");
            sb.AppendLine("<div style='float:left'><table  class='sample'>");
            int count = 0;

            foreach (var i in cal.Schedules)
            {
                sb.AppendLine("<tr>");

                for (int j = 0; j < i.Slot.Length; j++)
                {
                    MatrixCompare jCompare = cCompare.Find(f => f.Position == string.Format("({0},{1})", count, j));

                    string iColor = "green";
                    if (jCompare != null)
                    {
                        iColor = jCompare.Changed ? "red" : "green";
                    }


                    Air jAir = i.Slot[j];
                    if (jAir == null)
                    {
                        sb.AppendFormat("<td>({0},{1})</td>", count, j);
                    }
                    else
                    {
                        sb.AppendFormat(
                            "<td style='background-color:green;'>" +
                            "({4},{5})<br />" +
                            "WT : {2}<br />" +
                            "AC : {6}<br />" +
                            //"D : {1}<br />" +
                            //"No : {3}<br />" +
                            "</td>",
                            jAir.DisplayArrivalTime,
                            jAir.DisplayDepartureTime,
                            jAir.WaitingDurationLog,
                            jAir.No,
                            count,
                            j,
                            jAir.AccumulateTimeLog
                            );
                    }
                }
                count++;
                sb.AppendLine("</tr>");
            }
            sb.AppendLine("</table></div>");
            sb.AppendLine("</html>");
            //Keep data to text file
            Console.WriteLine("Trying to save file.");
            string iFolder      = string.Format("c:/simu/log{0:ddMMyyyy}/", DateTime.Now);
            string iExcelName   = string.Format("c:/simu/log{0:ddMMyyyy}/{1}", DateTime.Now, "calculation.xls");
            string iDestination = string.Format("c:/simu/log{0:ddMMyyyy}/{1}.html", DateTime.Now, fileName);

            CreateFile(iDestination, sb.ToString());


            try
            {
                AppendExcel(iExcelName, msg, cal, cCompare);
            }
            catch (Exception)
            {
            }

            //Process.Start(ConfigurationManager.AppSettings["IE"], "-private " + iDestination);
        }