public static bool CheckTooClose(MyCase myCase, int[] Choosen, int startAv, MyProgram item) { // check successor program for (int m = startAv; m < Math.Min(startAv + myCase.Delta, myCase.Times.Count); m++) { if (Choosen[m] == item.Id) { return(false); } } var nearest = -1; for (int i = startAv; i >= 0; i--) { if (Choosen[i] == item.Id) { nearest = i; break; } } if (nearest != -1 && startAv - nearest + item.Duration <= myCase.Delta) { return(false); } return(true); }
public static bool CheckAvailableSlot(int[] Choosen, MyProgram item) { for (int i = item.Start; i < item.Start + item.Duration; i++) { if (Choosen[i] != -1) { return(false); } } return(true); }
// change max and groud duration public static void AssignProgramToScheTree(int[] Choosen, int startAv, MyProgram item, MyGroup group) { ////assign program to frame for (int j = 0; j < item.Duration; j++) { Choosen[startAv] = item.Id; startAv++; } //// decrease the maximum show time of this program item.MaxShowTime--; group.TotalTime -= item.Duration; }
private static MyCase InitData(string filename) { IWorkbook wb = null; MyCase data = new MyCase(); data.Theta1 = 1; data.Theta2 = 0; data.Delta = 60; data.Alpha = 0.6; data.Allocates = new List <MyAssignment>(); data.BTGroups = new List <MyBelongToGroup>(); data.Programs = new List <MyProgram>(); #region create time frame using (FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read)) { wb = new XSSFWorkbook(file); } data.Frames = GetTimeFrame(Convert.ToInt32(filename.Split('F')[1].Split('-').FirstOrDefault()), Convert.ToInt32(filename.Split('F')[2].Split('.').FirstOrDefault())); #endregion data.Groups = GetGroup(); data.Times = GetTime(data.Frames.LastOrDefault().End); #region read data to object ISheet sheet = wb.GetSheetAt(0); List <MyProgram> list = new List <MyProgram>(); for (int row = 0; row <= sheet.LastRowNum; row++) { if (sheet.GetRow(row) != null) { if (sheet.GetRow(row).GetCell(5) != null) { MyProgram pr = new MyProgram(); pr.Name = sheet.GetRow(row).GetCell(2).StringCellValue; pr.Live = pr.Name.ToLower().Contains("live") ? true : false; pr.Duration = Convert.ToInt32(sheet.GetRow(row).GetCell(5).NumericCellValue); pr.Efficiency = Convert.ToDouble(sheet.GetRow(row).GetCell(6) != null ? sheet.GetRow(row).GetCell(6).NumericCellValue : 0); pr.Group = sheet.GetRow(row).GetCell(7) != null?sheet.GetRow(row).GetCell(7).StringCellValue : ""; if (pr.Group == "E") { pr.Group = "D"; } if (!string.IsNullOrEmpty(pr.Group)) { list.Add(pr); } } } } #endregion #region create program list int programIndex = 0; foreach (var item in list) { if (data.Programs.Where(x => x.Name == item.Name).FirstOrDefault() == null) { item.Id = programIndex; data.Programs.Add(item); programIndex++; } } #endregion #region calculate group duration var totaltime = data.Frames.LastOrDefault().End; var staticTime = data.Programs.Sum(x => x.Duration); var totalGroup = data.Programs.Select(x => x.Group).Distinct().Count(); var dynamicTime = totaltime - staticTime; double[] ratio = GetTimeRatio(); double totalRatio = 0.0; for (int j = 0; j < totalGroup; j++) { totalRatio += ratio[j]; } int sum = 0; for (int k = totalGroup - 1; k > 0; k--) { data.Groups[k].TotalTime = Convert.ToInt32(data.Programs.Where(x => x.Group == data.Groups[k].Name).Sum(t => t.Duration) + dynamicTime * ratio[k] / totalRatio); sum += data.Groups[k].TotalTime; data.Groups[k].MaxShow = Convert.ToInt32(Math.Ceiling(data.Groups[k].TotalTime / (data.Programs.Where(x => x.Group == data.Groups[k].Name).Sum(t => t.Duration) * data.Alpha))); } data.Groups[0].TotalTime = totaltime - sum; data.Groups[0].MaxShow = Convert.ToInt32(Math.Ceiling(data.Groups[0].TotalTime / (data.Programs.Where(x => x.Group == data.Groups[0].Name).Sum(t => t.Duration) * data.Alpha))); #endregion #region calculate max show time foreach (var pr in data.Programs) { foreach (var gr in data.Groups) { if (pr.Group == gr.Name) { pr.MaxShowTime = gr.MaxShow; } } } #endregion #region assign program to time frame foreach (var fr in data.Frames) { foreach (var pr in data.Programs) { if (fr.Live) { data.Allocates.Add(new MyAssignment() { FrameId = fr.Id, ProgramId = pr.Id, Assignable = 1 }); } // normal frame else { if (pr.Live) { data.Allocates.Add(new MyAssignment() { FrameId = fr.Id, ProgramId = pr.Id, Assignable = 0 }); } else { data.Allocates.Add(new MyAssignment() { FrameId = fr.Id, ProgramId = pr.Id, Assignable = 1 }); } } } } #endregion #region program belong to group foreach (var gr in data.Groups) { foreach (var pr in data.Programs) { if (pr.Group == gr.Name) { data.BTGroups.Add(new MyBelongToGroup() { GroupId = gr.Id, ProgramId = pr.Id, BelongTo = 1 }); } else { data.BTGroups.Add(new MyBelongToGroup() { GroupId = gr.Id, ProgramId = pr.Id, BelongTo = 0 }); } } } #endregion #region add groupId, available time frame foreach (var item in data.Programs) { item.GroupId = data.Groups.Where(x => x.Id == data.BTGroups.Where(y => y.ProgramId == item.Id && y.BelongTo == 1).FirstOrDefault().GroupId).FirstOrDefault().Id; item.FrameList = data.Frames.Where(x => data.Allocates.Where(y => y.ProgramId == item.Id && y.Assignable == 1).Select(z => z.FrameId).ToList().Contains(x.Id)).ToList(); } #endregion #region generate test using (System.IO.StreamWriter file = new System.IO.StreamWriter(filename.Split(new string[] { ".xlsx" }, StringSplitOptions.None).FirstOrDefault() + "_testcase.txt")) { //total time file.WriteLine("T\t" + totaltime); //delay repeat program show file.WriteLine("D\t" + data.Delta); //program foreach (var pr in data.Programs) { file.WriteLine("P\t" + pr.Id + "\t" + pr.Duration + "\t" + pr.Efficiency + "\t" + pr.MaxShowTime + "\t" + pr.Name); } //time frame foreach (var tf in data.Frames) { file.WriteLine("F\t" + tf.Id + "\t" + tf.Start + "\t" + tf.End); } // group foreach (var gr in data.Groups) { file.WriteLine("G\t" + gr.Id + "\t" + gr.TotalTime + "\t" + gr.Name); } // assign foreach (var ass in data.Allocates) { file.WriteLine("A\t" + ass.FrameId + "\t" + ass.ProgramId + "\t" + ass.Assignable); } // belong to foreach (var blt in data.BTGroups) { file.WriteLine("B\t" + blt.GroupId + "\t" + blt.ProgramId + "\t" + blt.BelongTo); } } #endregion return(data); }
// change max and groud duration public static void AssignProgramToSche(MyCase myCase, int[] Choosen, int startAv, MyProgram item) { ////assign program to frame for (int j = 0; j < item.Duration; j++) { Choosen[startAv] = item.Id; startAv++; } //// decrease the maximum show time of this program myCase.Programs.Where(x => x.Id == item.Id).FirstOrDefault().MaxShowTime--; myCase.Groups.Where(x => x.Id == item.GroupId).FirstOrDefault().TotalTime -= item.Duration; }
public static bool CheckAssignableProgram(MyCase myCase, int[] Choosen, int startAv, MyProgram item) { // out bound of array time if (startAv + item.Duration - 1 >= myCase.Times.Count) { return(false); } else { // check space which has enough free space to assign program for (int m = startAv; m < startAv + item.Duration; m++) { if (Choosen[m] != -1) { return(false); } } } return(true); }