コード例 #1
0
        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);
        }
コード例 #2
0
 //H3
 public static bool ValidateProgramLength(MyCase myCase, int[] Choosen)
 {
     foreach (var pro in myCase.Programs)
     {
         int mark = 0;
         for (int i = 0; i < Choosen.Count(); i++)
         {
             if (Choosen[i] == pro.Id)
             {
                 mark++;
             }
             if (Choosen[i] != -1 && Choosen[i] != pro.Id)
             {
                 if (mark != 0)
                 {
                     if (mark != pro.Duration)
                     {
                         return(false);
                     }
                     mark = 0;
                 }
             }
         }
     }
     return(true);
 }
コード例 #3
0
 //H4
 public static bool ValidateMaxShowTime(MyCase myCase, int[] Choosen)
 {
     foreach (var pro in myCase.Programs)
     {
         int count = 0;
         int mark  = -1;
         for (int i = 0; i < Choosen.Count(); i++)
         {
             if (Choosen[i] == pro.Id && pro.Id != mark)
             {
                 count++;
                 mark = pro.Id;
             }
             if (Choosen[i] != -1 && Choosen[i] != pro.Id)
             {
                 mark = Choosen[i];
             }
         }
         if (count > pro.MaxShowTime)
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #4
0
        public static List <MyTimeFrame> OrderOccupiedFrame(MyCase myCase, int[] Choosen, List <int> frameList)
        {
            int[] Cache = new int[frameList.Count];
            for (int i = 0; i < frameList.Count; i++)
            {
                Cache[i] = myCase.Frames.Where(x => x.Id == frameList[i]).FirstOrDefault().Unoccupate;
            }
            List <MyTimeFrame> result = new List <MyTimeFrame>();

            for (int i = 0; i < frameList.Count; i++)
            {
                int max = Cache.Max();
                for (int j = 0; j < frameList.Count; j++)
                {
                    if (max == -1)
                    {
                        break;
                    }
                    if (Cache[j] == max)
                    {
                        result.Add(myCase.Frames.Where(x => x.Id == frameList[j]).FirstOrDefault());
                        Cache[j] = -1;
                        break;
                    }
                }
            }
            return(result);
        }
コード例 #5
0
 static void Main(string[] args)
 {
     foreach (var filename in fileList)
     {
         MyCase input = InitData(filename);
         Gen(500, 500, 0.4, 360, 1000, 0.01, input);
     }
 }
コード例 #6
0
        private static bool CheckValidFrame(int start, MyCase myCase, int proId)
        {
            int FrameId = myCase.Frames.Where(x => x.Start <= start && x.End >= start).FirstOrDefault().Id;

            if (myCase.Allocates.Where(x => x.FrameId == FrameId && x.ProgramId == proId && x.Assignable == 1).Count() > 0)
            {
                return(true);
            }
            return(false);
        }
コード例 #7
0
 public static int GetFirstAvailableSlotInFrame(MyCase myCase, int[] Choosen, MyTimeFrame frame)
 {
     for (int i = frame.Start; i <= frame.End; i++)
     {
         if (Choosen[i - 1] == -1)
         {
             return(i - 1);
         }
     }
     return(-1);
 }
コード例 #8
0
 //H2
 public static bool ValidateMinShowTime(MyCase myCase, int[] Choosen)
 {
     foreach (var pro in myCase.Programs)
     {
         if (Choosen.Where(x => x == pro.Id).Count() <= 0)
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #9
0
 // 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;
 }
コード例 #10
0
 //H6
 public static bool ValidateGrouptime(MyCase myCase, int[] Choosen)
 {
     foreach (var gr in myCase.Groups)
     {
         List <int> ProgramId = myCase.BTGroups.Where(x => x.GroupId == gr.Id && x.BelongTo == 1).Select(x => x.ProgramId).ToList();
         int        TotalTime = Choosen.Where(x => ProgramId.Contains(x)).Count();
         if (TotalTime > gr.TotalTime)
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #11
0
        public static List <MyProgram> ConvertToProgram(MyCase myCase, int[] array)
        {
            int mark = -1;
            List <MyProgram> proList = new List <MyProgram>();

            for (int i = 0; i < array.Count(); i++)
            {
                if (array[i] != -1 && array[i] != mark)
                {
                    proList.Add(myCase.Programs.Where(x => x.Id == array[i]).FirstOrDefault());
                    mark = array[i];
                }
            }
            return(proList);
        }
コード例 #12
0
 //H1
 public static bool ValidateTimeFrame(MyCase myCase, int[] Choosen)
 {
     foreach (var pro in myCase.Programs)
     {
         var startPoints = GetShowTime(Choosen, pro.Id);
         foreach (var point in startPoints)
         {
             if (!CheckValidFrame(point + 1, myCase, pro.Id))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
コード例 #13
0
 // change unoccupate
 public static void UpdateUnOccupiedFrameTime(MyCase myCase, int[] Choosen)
 {
     //// update frame unoccupate
     for (int i = 0; i < myCase.Frames.Count; i++)
     {
         var unocc = 0;
         for (int j = myCase.Frames[i].Start; j <= myCase.Frames[i].End; j++)
         {
             if (Choosen[j - 1] == -1)
             {
                 unocc += 1;
             }
         }
         myCase.Frames[i].Unoccupate = unocc;
     }
 }
コード例 #14
0
        public static List <Chromosome> Repopulate(MyCase input, List <Chromosome> initPopulation, int populationSize)
        {
            var children = new List <MyStatictis>();

            for (int i = 0; i < initPopulation.Count; i++)
            {
                var rev = strategy(input, initPopulation[i]);
                rev.Ratio = i;
                children.Add(rev);
            }
            var choosen           = children.OrderByDescending(x => x.Revenue).Take(populationSize).ToList();
            var choosenChromosome = new List <Chromosome>();

            choosen.ForEach(x => choosenChromosome.Add(initPopulation[(int)x.Ratio]));
            return(choosenChromosome);
        }
コード例 #15
0
        public static List <MySchedule> GetSchedule(MyCase myCase, int[] Choosen)
        {
            List <MySchedule> sche = new List <MySchedule>();
            int anker = -1;

            for (int j = 0; j < myCase.Times.Count; j++)
            {
                if (Choosen[j] != -1 && anker != Choosen[j])
                {
                    anker = Choosen[j];
                    MySchedule p = new MySchedule();
                    p.Program = myCase.Programs.Where(x => x.Id == anker).FirstOrDefault();
                    p.Start   = j;
                    sche.Add(p);
                }
            }
            return(sche);
        }
コード例 #16
0
        public static List <Chromosome> CreateInitPopulation(int initSize, MyCase input)
        {
            var initPopulation = new List <Chromosome>();

            for (int i = 0; i < initSize; i++)
            {
                var individual = new Chromosome();
                for (int j = 0; j < input.Programs.Count; j++)
                {
                    individual.ATP.Add(new ATP()
                    {
                        RandomNo  = GetRandom(input.Programs[j].MaxShowTime),
                        ProgramId = input.Programs[j].Id
                    });
                }
                initPopulation.Add(individual);
            }
            return(initPopulation);
        }
コード例 #17
0
        public static double CalculateRevenue(MyCase myCase, int[] Choosen)
        {
            List <TempResult> results = new List <TempResult>();
            var resultIds             = Choosen.Distinct().Where(x => x != -1).ToList();

            foreach (var id in resultIds)
            {
                var pr = new TempResult();
                pr.proId   = id;
                pr.numShow = Choosen.Where(x => x == id).ToList().Count / myCase.Programs.Where(x => x.Id == id).FirstOrDefault().Duration;
                results.Add(pr);
            }
            double revenue = 0;

            foreach (var i in results)
            {
                revenue += myCase.Programs.Where(x => x.Id == i.proId).FirstOrDefault().RevenuePerShow *i.numShow;
            }
            return(revenue);
        }
コード例 #18
0
 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);
 }
コード例 #19
0
 //H5
 public static bool ValidateTooClose(MyCase myCase, int[] Choosen)
 {
     foreach (var pro in myCase.Programs)
     {
         List <List <int> > p = new List <List <int> >();
         List <int>         o = new List <int>();
         int mark             = -1;
         for (int i = 0; i < Choosen.Count(); i++)
         {
             if (Choosen[i] == pro.Id && pro.Id != mark)
             {
                 mark = pro.Id;
                 o.Add(i);
             }
             if (Choosen[i] != -1 && Choosen[i] != pro.Id)
             {
                 if (mark == pro.Id)
                 {
                     o.Add(i - 1);
                     p.Add(o);
                     o = new List <int>();
                 }
                 mark = Choosen[i];
             }
         }
         for (int k = 0; k < p.Count - 1; k++)
         {
             for (int l = p[k][1] + 1; l < p[k][0] + myCase.Delta; l++)
             {
                 if (l < Choosen.Count())
                 {
                     if (Choosen[l] == pro.Id)
                     {
                         return(false);
                     }
                 }
             }
         }
     }
     return(true);
 }
コード例 #20
0
        public static void ShiftRight(MyCase myCase, int[] Choosen, int k, int FrameId, List <int> Unoccupate = null)
        {
            int start = -1;

            for (int i = myCase.Frames[FrameId].Start - 1; i < myCase.Frames[FrameId].End; i++)
            {
                if (Choosen[i] == -1)
                {
                    start = i;
                    break;
                }
            }
            for (int i = start - 1; i >= myCase.Frames[FrameId].Start - 1; i--)
            {
                Choosen[i + k] = Choosen[i];
            }
            for (int i = myCase.Frames[FrameId].Start - 1; i < myCase.Frames[FrameId].Start - 1 + k; i++)
            {
                Choosen[i] = -1;
            }
        }
コード例 #21
0
        public static List <MyTimeFrame> OrderMinimumPro(MyCase myCase, int[] Choosen, List <int> frameList)
        {
            int[] Cache = new int[frameList.Count];
            for (int i = 0; i < frameList.Count; i++)
            {
                Cache[i] = 0;
                int check = -1;
                for (int j = myCase.Frames.Where(x => x.Id == frameList[i]).FirstOrDefault().Start - 1; j < myCase.Frames.Where(x => x.Id == frameList[i]).FirstOrDefault().End; j++)
                {
                    if (Choosen[j] != check)
                    {
                        Cache[i]++;
                        check = Choosen[j];
                    }
                }
            }
            List <MyTimeFrame> result = new List <MyTimeFrame>();

            for (int i = 0; i < frameList.Count; i++)
            {
                int max = Cache.Max();
                for (int j = 0; j < frameList.Count; j++)
                {
                    if (max == -1)
                    {
                        break;
                    }
                    if (Cache[j] == max)
                    {
                        result.Add(myCase.Frames.Where(x => x.Id == frameList[j]).FirstOrDefault());
                        Cache[j] = -1;
                        break;
                    }
                }
            }
            return(result);
        }
コード例 #22
0
 public static bool CheckOneShow(int[] Choosen, MyCase myCase)
 {
     foreach (var pro in myCase.Programs)
     {
         bool flag2 = false;
         for (int i = 0; i < Choosen.Count(); i++)
         {
             if (Choosen[i] == pro.Id)
             {
                 flag2 = true;
                 break;
             }
         }
         if (flag2)
         {
             break;
         }
         else
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #23
0
        public static int[] ValidateResult(MyCase myCase, int[] Choosen)
        {
            int[] result = new int[7];
            if (!ValidateContinuePrograms(myCase, Choosen))
            {
                result[6] = -7;
            }
            else
            {
                result[6] = 0;
            }
            if (!ValidateTimeFrame(myCase, Choosen))
            {
                result[0] = -1;
            }
            else
            {
                result[0] = 0;
            }

            if (!ValidateMinShowTime(myCase, Choosen))
            {
                result[1] = -2;
            }
            else
            {
                result[1] = 0;
            }

            if (!ValidateProgramLength(myCase, Choosen))
            {
                result[2] = -3;
            }
            else
            {
                result[2] = 0;
            }

            if (!ValidateMaxShowTime(myCase, Choosen))
            {
                result[3] = -4;
            }
            else
            {
                result[3] = 0;
            }

            if (!ValidateTooClose(myCase, Choosen))
            {
                result[4] = -5;
            }
            else
            {
                result[4] = 0;
            }

            if (!ValidateGrouptime(myCase, Choosen))
            {
                result[5] = -6;
            }
            else
            {
                result[5] = 0;
            }
            return(result);
        }
コード例 #24
0
 public static MyTimeFrame FindTimeFrame(int start, MyCase myCase)
 {
     return(myCase.Frames.Where(x => x.Start - 1 <= start && x.End - 1 >= start).FirstOrDefault());
 }
コード例 #25
0
        public static MyStatictis strategy(MyCase input, Chromosome chromosome)
        {
            var programs = input.Programs.DeepCopyByExpressionTree();

            for (int i = 0; i < programs.Count; i++)
            {
                programs[i].MaxShowTime = chromosome.ATP[i].RandomNo;
            }
            var myGroups = input.Groups.DeepCopyByExpressionTree();
            var myFrame  = input.Frames.DeepCopyByExpressionTree();

            //var maxNos = chromosome.DeepCopyByExpressionTree();
            #region init array
            int[] Choosen = new int[input.Times.Count];
            for (int i = 0; i < input.Times.Count; i++)
            {
                Choosen[i] = -1;
            }
            #endregion
            // check ability of assignment
            var  watch  = System.Diagnostics.Stopwatch.StartNew();
            bool change = false;
            #region assign program to frame
            // assign one program one time
            var listProgram = programs.Where(x => x.MaxShowTime > 0).OrderByDescending(x => x.Efficiency).ToList();
            foreach (var item in listProgram)
            {
                var gr = myGroups.Where(x => x.Id == item.GroupId).FirstOrDefault();
                if (gr.TotalTime >= item.Duration)
                {
                    foreach (var frame in item.FrameList)
                    {
                        int startAv = Utility.GetFirstAvailableSlotInFrame(input, Choosen, frame);
                        #region find a empty space to assign
                        if (startAv != -1)
                        {
                            if (Utility.CheckAssignableProgram(input, Choosen, startAv, item))
                            {
                                if (Utility.CheckTooClose(input, Choosen, startAv, 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
                                    item.MaxShowTime--;
                                    gr.TotalTime -= item.Duration;
                                    change        = true;
                                    break;
                                }
                            }
                        }
                        #endregion
                    }
                }
            }

            while (true)
            {
                //find special program first
                var listSpecial = programs.Where(x => x.MaxShowTime > 0 && x.FrameList.Count < 11).OrderByDescending(x => x.Efficiency).ToList();
                if (listSpecial.Count > 0)
                {
                    foreach (var item in listSpecial)
                    {
                        var gr = myGroups.Where(x => x.Id == item.GroupId).FirstOrDefault();
                        #region group still has cota
                        if (gr.TotalTime >= item.Duration)
                        {
                            foreach (var frame in item.FrameList)
                            {
                                int startAv = Utility.GetFirstAvailableSlotInFrame(input, Choosen, frame);
                                #region find a empty space to assign
                                if (startAv != -1)
                                {
                                    if (Utility.CheckAssignableProgram(input, Choosen, startAv, item))
                                    {
                                        if (Utility.CheckTooClose(input, Choosen, startAv, 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
                                            item.MaxShowTime--;
                                            gr.TotalTime -= item.Duration;
                                            change        = true;
                                            break;
                                        }
                                    }
                                }
                                #endregion
                            }
                        }
                        #endregion
                    }
                }
                else
                {
                    var listUsual = programs.Where(x => x.MaxShowTime > 0 && x.FrameList.Count == 11).OrderByDescending(x => x.Efficiency).ToList();
                    foreach (var item in listUsual)
                    {
                        var gr = myGroups.Where(x => x.Id == item.GroupId).FirstOrDefault();
                        #region group still has cota
                        if (gr.TotalTime >= item.Duration)
                        {
                            foreach (var frame in item.FrameList)
                            {
                                int startAv = Utility.GetFirstAvailableSlotInFrame(input, Choosen, frame);
                                #region find a empty space to assign
                                if (startAv != -1)
                                {
                                    if (Utility.CheckAssignableProgram(input, Choosen, startAv, item))
                                    {
                                        if (Utility.CheckTooClose(input, Choosen, startAv, 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
                                            item.MaxShowTime--;
                                            gr.TotalTime -= item.Duration;
                                            change        = true;
                                            break;
                                        }
                                    }
                                }
                                #endregion
                            }
                        }
                        #endregion
                    }
                }
                #region if nothing changes, stop loop
                if (change)
                {
                    change = false;
                }
                else
                {
                    break;
                }
                #endregion
            }
            #endregion

            #region try to assign programe between two frames
            change = false;
            // calculate the unoccupate of two continue frame
            while (true)
            {
                //// update frame unoccupate
                for (int i = 0; i < myFrame.Count; i++)
                {
                    var unocc = 0;
                    for (int j = myFrame[i].Start; j <= myFrame[i].End; j++)
                    {
                        if (Choosen[j - 1] == -1)
                        {
                            unocc += 1;
                        }
                    }
                    myFrame[i].Unoccupate = unocc;
                }
                List <int> Unoccupate = new List <int>();
                for (int i = 0; i < myFrame.Count - 1; i++)
                {
                    Unoccupate.Add(myFrame[i].Unoccupate + myFrame[i + 1].Unoccupate);
                }
                var list2 = programs.Where(x => x.MaxShowTime > 0).OrderByDescending(x => x.Efficiency).ToList();
                var item  = list2.FirstOrDefault();

                var gr = myGroups.Where(x => x.Id == item.GroupId).FirstOrDefault();
                if (gr.TotalTime >= item.Duration)
                {
                    for (int i = 0; i < Unoccupate.Count; i++)
                    {
                        if (item.Duration <= Unoccupate[i])
                        {
                            //check allowed frames
                            var FrameIdList = item.FrameList.Select(x => x.Id).ToList();
                            //add program to time frame
                            if (FrameIdList.Contains(i))
                            {
                                ////check available slot
                                int startAv = myFrame[i].End - myFrame[i].Unoccupate;
                                if (startAv != myFrame[i].End)
                                {
                                    if (Utility.CheckTooClose(input, Choosen, startAv, item))
                                    {
                                        int shift = item.Duration - myFrame[i].Unoccupate;
                                        // ShiftRight
                                        Utility.ShiftRight(input, Choosen, shift, i + 1, Unoccupate);
                                        ////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--;
                                        gr.TotalTime -= item.Duration;
                                        change        = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if (change)
                    {
                        break;
                    }
                }
                if (change)
                {
                    change = false;
                }
                else
                {
                    break;
                }
            }
            #endregion

            watch.Stop();
            var         elapsedH1 = watch.ElapsedMilliseconds;
            MyStatictis result    = new MyStatictis();
            result.Choosen      = Choosen;
            result.Elapsed      = elapsedH1;
            result.Revenue      = Utility.CalculateRevenue(input, Choosen);
            chromosome.Solution = Choosen;
            return(result);
        }
コード例 #26
0
        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);
        }
コード例 #27
0
        public static void Mutate(List <Chromosome> population, double mutationPercent, MyCase input)
        {
            var listIds = new List <int>();

            for (int i = 0; i < population.Count; i++)
            {
                listIds.Add(i);
            }
            var index = 0;

            while (index < population.Count() * mutationPercent)
            {
                var randomIndex   = r.Next(0, listIds.Count);
                var chromosome    = population[randomIndex];
                var indexMutation = r.Next(0, chromosome.ATP.Count);
                chromosome.ATP[indexMutation].RandomNo = r.Next(1, input.Programs[indexMutation].MaxShowTime + 1);
                listIds.RemoveAt(randomIndex);
                index++;
            }
        }
コード例 #28
0
        public static void Gen(int initSize, int populationSize, double percentCrossOver, int nochange, int noloop, double mutationPercent, MyCase input)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
            List <Chromosome> initPopulation = CreateInitPopulation(initSize, input);

            for (int i = 0; i < initPopulation.Count; i++)
            {
                var rev = strategy(input, initPopulation[i]);
                var avl = Validate.ValidateResult(input, (int[])initPopulation[i].Solution);
                var id  = "";
                foreach (var a in initPopulation[i].ATP)
                {
                    id += a.RandomNo.ToString();
                }
                if (rev.Revenue > revenue)
                {
                    revenue = rev.Revenue;
                }
            }

            for (var lop = 0; lop < noloop; lop++)
            {
                var parents  = SelectParents(initPopulation, percentCrossOver);
                var children = MakeChildren(parents);
                initPopulation = initPopulation.Concat(children).ToList();
                initPopulation = Repopulate(input, initPopulation, populationSize);
                Mutate(initPopulation, mutationPercent, input);
                /*double re = initPopulation.OrderByDescending(x => x.revenue).FirstOrDefault().revenue;*/
                double eachRev = 0;
                for (int i = 0; i < initPopulation.Count; i++)
                {
                    var rev = strategy(input, initPopulation[i]);
                    if (rev.Revenue > revenue)
                    {
                        eachRev = rev.Revenue;
                    }
                }
                if (eachRev > revenue)
                {
                    revenue = eachRev;
                    count   = 0;
                }
                count++;

                if (count > nochange)
                {
                    break;
                }
            }

            watch.Stop();
            var elapsedH1 = watch.ElapsedMilliseconds;
            //return result;
        }