コード例 #1
0
ファイル: Solver.cs プロジェクト: tiruns/GAScheduling
        public void PrintResult(Individual individual)
        {
            var datasets = Datasets.Get();

            foreach (var chromo in individual.Genome)
            {
                var day    = chromo.Gene.TimeId / Config.PeriodsPerDay;
                var period = chromo.Gene.TimeId % Config.PeriodsPerDay;

                string clsString = "";

                if (chromo.IsSubClass)
                {
                    var sub = datasets.SubClasses.Find(c => c.Id == chromo.ClassId).DerivingFrom;
                    foreach (var cid in sub)
                    {
                        clsString += datasets.Classes.Find(c => c.Id == cid).Name + " ";
                    }
                }
                else
                {
                    clsString = datasets.Classes.Find(c => c.Id == chromo.ClassId).Name;
                }

                Console.WriteLine("DoW:{0},\tPer:{1},\tCrs:{2},\tRom:{3},\tCls:{4},\tThr:{5}",
                                  day,
                                  period,
                                  datasets.Courses.Find(c => c.Id == chromo.CourseId).Name,
                                  datasets.Rooms.Find(r => r.Id == chromo.Gene.RoomId).Name,
                                  clsString,
                                  datasets.Teachers.Find(t => t.Id == chromo.Gene.TeacherId).Name
                                  );
            }
        }
コード例 #2
0
ファイル: Solver.cs プロジェクト: tiruns/GAScheduling
        public Individual()
        {
            this.Genome = new List <Chromosome>();
            var datasets = Datasets.Get();

            foreach (var cls in datasets.Classes)
            {
                foreach (var course in cls.RequiredCourses)
                {
                    int periods = datasets.Courses.Find(c => c.Id == course).PeriodsPerWeek;
                    for (int i = 0; i < periods; ++i)
                    {
                        this.Genome.Add(new Chromosome(cls, course));
                    }
                }
            }
            foreach (var sub in datasets.SubClasses)
            {
                int periods = datasets.Courses.Find(c => c.Id == sub.RequiredCourses).PeriodsPerWeek;
                for (int i = 0; i < periods; ++i)
                {
                    this.Genome.Add(new Chromosome(sub, sub.RequiredCourses));
                }
            }
        }
コード例 #3
0
ファイル: Solver.cs プロジェクト: tiruns/GAScheduling
        public void WriteCsv(Individual individual)
        {
            string path = "";

            for (int i = 0; i < 32; ++i)
            {
                path = "result_" + RandomEngine.Next(320) + ".csv";
                if (!System.IO.File.Exists(path))
                {
                    var datasets = Datasets.Get();

                    var header = new List <string>
                    {
                        "DayOfWeek",
                        "Period",
                        "Course",
                        "Room",
                        "Classes",
                        "Teacher"
                    };

                    CsvWriter.WriteAll <Chromosome>(path, header, individual.Genome, chromo =>
                    {
                        var list = new List <string>();

                        var day    = chromo.Gene.TimeId / Config.PeriodsPerDay;
                        var period = chromo.Gene.TimeId % Config.PeriodsPerDay;

                        string clsString = "";

                        if (chromo.IsSubClass)
                        {
                            var sub = datasets.SubClasses.Find(c => c.Id == chromo.ClassId).DerivingFrom;
                            foreach (var cid in sub)
                            {
                                clsString += datasets.Classes.Find(c => c.Id == cid).Name + " ";
                            }
                        }
                        else
                        {
                            clsString = datasets.Classes.Find(c => c.Id == chromo.ClassId).Name;
                        }

                        list.Add(day.ToString());
                        list.Add(period.ToString());
                        list.Add(datasets.Courses.Find(c => c.Id == chromo.CourseId).Name);
                        list.Add(datasets.Rooms.Find(r => r.Id == chromo.Gene.RoomId).Name);
                        list.Add(clsString);
                        list.Add(datasets.Teachers.Find(t => t.Id == chromo.Gene.TeacherId).Name);

                        return(list);
                    });

                    Console.WriteLine("File saved: " + path);
                    return;
                }
            }
            throw new Exception("Can't write to csv file: " + path);
        }
コード例 #4
0
ファイル: Solver.cs プロジェクト: tiruns/GAScheduling
        public DNA()
        {
            var datasets = Datasets.Get();

            this.RoomId    = datasets.Rooms[RandomEngine.Next(datasets.Rooms.Count)].Id;
            this.TeacherId = datasets.Teachers[RandomEngine.Next(datasets.Teachers.Count)].Id;
            this.TimeId    = RandomEngine.Next(datasets.TotalPeriods);
        }
コード例 #5
0
        public float Evaluate()
        {
            var datasets = Datasets.Get();

            var violations = BucketCheck(bucket =>
            {
                var dic   = new Dictionary <int, List <Chromosome> >();
                var count = 0;

                foreach (var chromo in bucket)
                {
                    if (!dic.Keys.Contains(chromo.Gene.RoomId))
                    {
                        dic.Add(chromo.Gene.RoomId, new List <Chromosome>());
                    }
                    dic[chromo.Gene.RoomId].Add(chromo);
                }

                foreach (var room in dic)
                {
                    var list   = room.Value;
                    var marked = new List <Chromosome>();

                    var first  = list[0];
                    int number = 0;

                    foreach (var other in list)
                    {
                        //if (other == first)
                        //    continue;

                        if (first.CourseId != other.CourseId ||
                            first.Gene.TeacherId != other.Gene.TeacherId)
                        {
                            count += 1;
                        }
                        else
                        {
                            number += first.IsSubClass ?
                                      datasets.SubClasses.Find(cls => cls.Id == first.ClassId).NumberOfStudents :
                                      datasets.Classes.Find(cls => cls.Id == first.ClassId).NumberOfStudents;
                        }
                    }

                    if (number > datasets.Rooms.Find(r => r.Id == room.Key).Capacity)
                    {
                        count += 1;
                    }
                }
                return(count);
            });
            var maxAllowed = mIndividual.Genome.Count / 6;
            var score      = Math.Max(maxAllowed - violations, 0);

            score *= score;

            return(score);
        }
コード例 #6
0
        private Buckets GenerateBueckets(Individual individual)
        {
            var datasets = Datasets.Get();

            var buckets = new Buckets(datasets.TotalPeriods);

            for (int i = 0; i < datasets.TotalPeriods; ++i)
            {
                buckets.Add(new List <Chromosome>());
            }
            foreach (var chromo in individual.Genome)
            {
                buckets[chromo.Gene.TimeId].Add(chromo);
            }

            return(buckets);
        }
コード例 #7
0
        public float BucketCheck(Func <List <Chromosome>, int> check)
        {
            int violations = 0;
            var datasets   = Datasets.Get();

            foreach (var bucket in mBuckets)
            {
                if (bucket.Count == 0)
                {
                    continue;
                }

                violations += check(bucket);
            }

            return(violations);
        }
コード例 #8
0
        public float Evaluate()
        {
            var datasets   = Datasets.Get();
            var violations = 0;

            foreach (var chromo in mIndividual.Genome)
            {
                var teacher = datasets.Teachers.Find(t => t.Id == chromo.Gene.TeacherId);
                if (!teacher.ProvidedCourse.Contains(chromo.CourseId))
                {
                    violations += 1;
                }
            }
            var error      = Convert.ToSingle(violations) / mIndividual.Genome.Count;
            var maxAllowed = mIndividual.Genome.Count / 6;
            var score      = maxAllowed * (1 - error);

            score *= score;
            return(score);
        }
コード例 #9
0
ファイル: Solver.cs プロジェクト: tiruns/GAScheduling
        public DNA(DNA left, DNA right)
        {
            this.RoomId    = RandomEngine.NextDouble() < 0.5 ? left.RoomId : right.RoomId;
            this.TeacherId = RandomEngine.NextDouble() < 0.5 ? left.TeacherId : right.TeacherId;
            this.TimeId    = RandomEngine.NextDouble() < 0.5 ? left.TimeId : right.TimeId;

            var datasets = Datasets.Get();

            if (RandomEngine.NextDouble() < Config.MutationRate)
            {
                if (RandomEngine.NextDouble() < 0.33)
                {
                    this.RoomId = datasets.Rooms[RandomEngine.Next(datasets.Rooms.Count)].Id;
                }
                if (RandomEngine.NextDouble() < 0.33)
                {
                    this.TeacherId = datasets.Teachers[RandomEngine.Next(datasets.Teachers.Count)].Id;
                }
                if (RandomEngine.NextDouble() < 0.33)
                {
                    this.TimeId = RandomEngine.Next(datasets.TotalPeriods);
                }
            }
        }
コード例 #10
0
        public float Evaluate()
        {
            var datasets = Datasets.Get();

            var violations = BucketCheck(bucket =>
            {
                var marked = new List <Chromosome>();
                var count  = 0;

                foreach (var chromo in bucket)
                {
                    if (marked.Contains(chromo))
                    {
                        continue;
                    }

                    marked.Add(chromo);

                    if (chromo.IsSubClass)
                    {
                        var rawClasses = datasets.SubClasses.Find(x => x.Id == chromo.ClassId).DerivingFrom;
                        foreach (var other in bucket)
                        {
                            if (marked.Contains(other))
                            {
                                continue;
                            }

                            if ((!other.IsSubClass && rawClasses.Contains(other.ClassId)) ||
                                (other.IsSubClass && other.ClassId == chromo.ClassId))
                            {
                                count += 1;
                                marked.Add(other);
                            }
                        }
                    }
                    else
                    {
                        foreach (var other in bucket)
                        {
                            if (marked.Contains(other))
                            {
                                continue;
                            }

                            if (!other.IsSubClass && other.ClassId == chromo.ClassId)
                            {
                                count += 1;
                                marked.Add(other);
                            }
                        }
                    }
                }

                return(count);
            });
            var maxAllowed = mIndividual.Genome.Count / 6;
            var score      = Math.Max(maxAllowed - violations, 0);

            score *= score;

            return(score);
        }
コード例 #11
0
        public float Evaluate()
        {
            var datasets   = Datasets.Get();
            int violations = 0;

            foreach (var course in datasets.Courses)
            {
                var progress = new Dictionary <int, int>();

                var list = new List <Chromosome>();
                foreach (var chromo in mIndividual.Genome)
                {
                    if (chromo.CourseId == course.Id)
                    {
                        list.Add(chromo);
                    }
                }
                list.Sort();

                int time    = -1;
                var current = new List <Chromosome>();

                foreach (var chromo in list)
                {
                    if (!progress.Keys.Contains(chromo.ClassId))
                    {
                        progress.Add(chromo.ClassId, 1);
                    }
                    else
                    {
                        progress[chromo.ClassId]++;
                    }

                    if (time < chromo.Gene.TimeId)
                    {
                        time = chromo.Gene.TimeId;
                        current.Clear();
                        current.Add(chromo);
                        continue;
                    }
                    else if (time == chromo.Gene.TimeId)
                    {
                        foreach (var other in current)
                        {
                            if (chromo.Gene.RoomId != other.Gene.RoomId ||
                                progress[chromo.ClassId] != progress[other.ClassId])
                            {
                                violations += 1;
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Time mismatch.");
                    }
                }
            }

            var maxAllowed = mIndividual.Genome.Count / 6;
            var score      = Math.Max(maxAllowed - violations, 0);

            score *= score;

            return(score);
        }