Exemplo n.º 1
0
        private static HashSet <Curriculum> GetXmlCurricula(XElement element, Dictionary <string, Course> courses)
        {
            var curricula = new HashSet <Curriculum>();

            foreach (var node in element.DescendantNodes())
            {
                if (node.NodeType != XmlNodeType.Element)
                {
                    continue;
                }

                var elem = (XElement)node;
                if (elem.Name.ToString().ToLower() != "curriculum")
                {
                    continue;
                }

                var id = elem.Attribute("id").Value;

                var cur = new Curriculum(id);
                foreach (var xNode in elem.DescendantNodes())
                {
                    if (xNode.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }

                    var courseelem = (XElement)xNode;
                    if (courseelem.Name.ToString().ToLower() != "course")
                    {
                        continue;
                    }

                    var courseid = courseelem.Attribute("ref").Value;

                    cur.AddCourse(courses[courseid]);
                    courses[courseid].Add(cur);
                }
                curricula.Add(cur);
            }

            return(curricula);
        }
Exemplo n.º 2
0
        public int Disrupt(DisruptionTypes disruption, int seed, out string entitiesdisrupted)
        {
            var random    = new Random(seed);
            var disrupted = 0;

            switch (disruption)
            {
            case DisruptionTypes.SecondLargestRoomRemovedOneTimeslot:
            {
                var tsday2 = new List <TimeSlot>()
                {
                    data.TimeSlots[random.Next(data.TimeSlots.Count)]
                };

                var secondlargestroom = data.Rooms.OrderByDescending(r => r.Capacity).ToList()[1];
                disrupted =
                    solutionBefore._assignments.Count(
                        a => a.Room.Equals(secondlargestroom) && tsday2.Contains(a.TimeSlot));
                model.Disrupt(secondlargestroom, tsday2);
                entitiesdisrupted = $"{secondlargestroom} in {string.Join(",", tsday2)}";
                Console.WriteLine($"Removed: {secondlargestroom} in {string.Join(",", tsday2)}\n" +
                                  $"Disrupted: {disrupted} ");
            }
            break;

            case DisruptionTypes.SecondLargestRoomDayRemoved:
            {
                var rday   = random.Next(data.Days);
                var tsday2 = data.TimeSlots.Where(t => t.Day == rday).ToList();

                var secondlargestroom = data.Rooms.OrderByDescending(r => r.Capacity).ToList()[1];
                disrupted =
                    solutionBefore._assignments.Count(
                        a => a.Room.Equals(secondlargestroom) && tsday2.Contains(a.TimeSlot));
                model.Disrupt(secondlargestroom, tsday2);
                entitiesdisrupted = $"{secondlargestroom} in {string.Join(",", tsday2)}";

                Console.WriteLine($"Removed: {secondlargestroom} in {string.Join(",", tsday2)}\n" +
                                  $"Disrupted: {disrupted} ");
            }
            break;

            case DisruptionTypes.RandomRoomRandomdayRemoved:
            {
                Room            room   = null;
                List <TimeSlot> tsday2 = null;
                while (disrupted == 0)
                {
                    var rday = random.Next(data.Days);
                    tsday2 = data.TimeSlots.Where(t => t.Day == rday).ToList();
                    var rroom = random.Next(data.Rooms.Count);

                    room      = data.Rooms[rroom];
                    disrupted =
                        solutionBefore._assignments.Count(
                            a => a.Room.Equals(room) && tsday2.Contains(a.TimeSlot));
                }
                model.Disrupt(room, tsday2);
                entitiesdisrupted = $"{room} in {string.Join(",", tsday2)}";

                Console.WriteLine($"Removed: {room} in {string.Join(",", tsday2)}\n" +
                                  $"Disrupted: {disrupted} ");
            }
            break;

            case DisruptionTypes.OneTimeslotUnavailable:
            {
                TimeSlot timeSlot = null;
                while (disrupted == 0)
                {
                    timeSlot  = data.TimeSlots[random.Next(data.TimeSlots.Count)];
                    disrupted = solutionBefore._assignments.Count(a => timeSlot.Equals(a.TimeSlot));
                }

                model.Disrupt(timeSlot);
                entitiesdisrupted = $"{string.Join(",", timeSlot)}";

                Console.WriteLine($"Removed: {string.Join(",", timeSlot)}\n" +
                                  $"Disrupted: {disrupted} ");
            }
            break;

            case DisruptionTypes.OneAssignmentUnavailbility:
            {
                var assignment =
                    solutionBefore._assignments.ToList()[random.Next(solutionBefore._assignments.Count)];
                disrupted = 1;
                model.Disrupt(assignment.Course, assignment.TimeSlot);
                entitiesdisrupted = $"{string.Join(",", assignment)}";

                Console.WriteLine($"Removed: {string.Join(",", assignment)}\n" +
                                  $"Disrupted: {disrupted} ");
            }
            break;

            case DisruptionTypes.CurriculumFourCoursesInsert:
            {
                Curriculum newcurriclum = null;

                while (disrupted == 0)
                {
                    newcurriclum = new Curriculum("ExtraCur");
                    while (newcurriclum.Courses.Count < 4)
                    {
                        var course = data.Courses[random.Next(data.Courses.Count)];
                        if (newcurriclum.Courses.Contains(course))
                        {
                            continue;
                        }
                        newcurriclum.AddCourse(course);
                    }
                    disrupted = solutionBefore._assignments.GroupBy(a => a.TimeSlot)
                                .Sum(ass =>
                        {
                            var count = ass.Select(a => a.Course).ToList().Intersect(newcurriclum.Courses).Count();
                            return(count < 2 ? 0 : count);
                        });
                }


                model.Disrupt(newcurriclum);
                entitiesdisrupted = $"{string.Join(",", newcurriclum.Courses)}";

                Console.WriteLine($"Added curr: {string.Join(",", newcurriclum.Courses)}\n" +
                                  $"Disrupted: {disrupted} ");
            }
            break;

            case DisruptionTypes.CurriculumFourCoursesInsertWithCC:
            {
                var newcurriclum = new Curriculum("ExtraCur");
                while (newcurriclum.Courses.Count < 4)
                {
                    var course = data.Courses[random.Next(data.Courses.Count)];
                    if (newcurriclum.Courses.Contains(course))
                    {
                        continue;
                    }
                    newcurriclum.AddCourse(course);
                }
                disrupted = solutionBefore._assignments.GroupBy(a => a.TimeSlot).Sum(ass =>
                    {
                        var count = ass.Select(a => a.Course).ToList().Intersect(newcurriclum.Courses).Count();
                        return(count < 2 ? 0 : count);
                    });
                model.Disrupt(newcurriclum, true);
                entitiesdisrupted = $"{string.Join(",", newcurriclum.Courses)}";
                Console.WriteLine($"Added curr: {string.Join(",", newcurriclum.Courses)}\n" +
                                  $"Disrupted: {disrupted} ");
            }
            break;

            default:
                throw new ArgumentException("unknown disruption " + disruption);
            }
            return(disrupted);
        }