public static CalucationData Simulate(string name, int iteration, List <CalucationData> cCalculation, params Air[] airs) { CalucationData prevCalculation = cCalculation.LastOrDefault(); List <MatrixCompare> checkList = new List <MatrixCompare>(); //Init matrix. List <Maintainance> cMaintain = null; CalucationData cal = null; if (iteration == 0) { Program.Reset(); Program.TestNo = DateTime.Now.ToString("ddMMyyyy_hhmmss"); List <Air> cAir = new List <Air>(airs); int countTick = 0; while ((_EndTime - Program.CurrentTime).TotalMinutes > -1) { cAir.ForEach(f => { if (f.DepartureTime >= Program.CurrentTime && f.Status != AirStatus.M ) { f.UpdateStatus(); } }); Program.CurrentTime = Program.CurrentTime.AddMinutes(_Tick); countTick++; } cMaintain = Matrix.Fixation(Matrix.MaintainanceCollection); cal = ComputationUnit.GetRawCalculationData(iteration, cMaintain); cal.StartCalculation(0); cCalculation.Add(cal); FileHelper.AppendText(name.Replace(".", iteration + "."), "First Matrix", cal, checkList); } else { cMaintain = Program.Clone(prevCalculation.Schedules); int count = 0; foreach (var i in cMaintain) { for (int j = 0; j < Program.MAX_AIR; j++) { if (i.Slot[j] != null) { checkList.Add(new MatrixCompare() { Before = i.Slot[j].WaitingDurationLog, Position = string.Format("({0},{1})", count, j) }); } } count++; } List <ColumnRank> cPoorRank = Matrix.GetRankedColumns((cMaintain)); //cMaintain = Matrix.FixPoorRank(cMaintain, cPoorRank); count = 0; foreach (var i in cMaintain) { for (int j = 0; j < Program.MAX_AIR; j++) { if (i.Slot[j] != null) { MatrixCompare iCompare = checkList.Find(f => f.Position == string.Format("({0},{1})", count, j)); if (iCompare != null) { iCompare.After = i.Slot[j].WaitingDurationLog; } } } count++; } Console.WriteLine("========================================"); Console.WriteLine("Iteration No.{0}", iteration); foreach (var i in checkList.Where(f => f.Changed)) { Console.WriteLine(string.Format( "{3}\r\nBefore = {0}, After = {1}, Changed = {2}", i.Before, i.After, i.Changed, i.Position )); } cal = ComputationUnit.GetRawCalculationData(iteration, cMaintain); string msg = ""; if (prevCalculation.TotalWatingTime < cal.TotalWatingTime) { cal = prevCalculation; } else { msg = "New matrix was better!"; Console.WriteLine(msg); } cal.StartCalculation(prevCalculation.SigmaT.Value); cCalculation.Add(cal); FileHelper.AppendText(name.Replace(".", iteration + "."), msg, cal, checkList); } return(cal); }
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;'> </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); }