示例#1
0
 public Gene(Slot slot, Event evt, Room room, bool cannotChange)
 {
     this.slot = slot;
       this.evt = evt;
       this.room = room;
       this.cannotChange = cannotChange;
 }
示例#2
0
 public Gene()
 {
     this.slot = new Slot();
       this.evt = new Event();
       this.room = new Room();
       cannotChange = false;
 }
        /* Checks for any clashes between groups and rooms for a particular slot */
        public bool doClashesExist(Slot s, Event e, Room r)
        {
            int indexOfSelectedSlot = Program.slots.IndexOf(s);
              bool clashes = false;
              int i = 0;

              // ensure there are no index out of bound errors
              if ((indexOfSelectedSlot + e.getDuration()) >= Program.slots.Count)
            clashes = true;

              /* Check that there are no clashes for any hours of the event */
              while ((i < e.getDuration()) && (!clashes))
              {
            List<Gene> eventsAtThisTime = new List<Gene>();

            foreach (Gene g in chromosome)
            {
              if (g.getSlot() == s)
            eventsAtThisTime.Add(g);
            }

            int j = 0;
            while ((j < eventsAtThisTime.Count) && (!clashes))
            {
              if ((eventsAtThisTime[j].getEvent().getGroups().Intersect(e.getGroups()).Count() > 0) ||
              (eventsAtThisTime[j].getRoom() == r && r != Program.lunchRoom))
            clashes = true;
              j++;
            }

            s = Program.slots[++indexOfSelectedSlot];
            i++;
              }
              return clashes;
        }
示例#4
0
        private void loadXML()
        {
            try
              {
            XmlTextReader reader = new XmlTextReader("data.xml");

            string groupRecord = "";
            Event eventToAdd = new Event();
            List<string> eventGroups = new List<string>();
            Gene fixedEventGene = new Gene();
            Room roomToAdd = new Room();
            string feDay = "";
            string feTime = "";
            while (reader.Read())
            {
              reader.MoveToContent();

              // groups
              string text = reader.Name.ToString();
              if (text == "GroupYear")
            groupRecord += reader.ReadString();
              else if (text == "GroupName")
            groupRecord += reader.ReadString();
              else if (text == "GroupDescription")
              {
            groupRecord += " - " + reader.ReadString();
            groups.Add(groupRecord);
            groupRecord = "";
              }

              // events
              else if (text == "CourseCode")
              {
            eventToAdd = new Event();
            eventToAdd.setCourse(reader.ReadString());
              }
              else if (text == "Activity")
            eventToAdd.setActivity(reader.ReadString());
              else if (text == "Duration")
            eventToAdd.setDuration(Int32.Parse(reader.ReadString()));
              else if (text == "Weekly")
              {
            eventToAdd.setWeekly(Boolean.Parse(reader.ReadString()));
            eventGroups = new List<string>();
              }
              else if (text == "EventGroup")
            eventGroups.Add(reader.ReadString());
              else if (text == "EventRoomSize")
              {
            eventToAdd.setGroups(eventGroups);
            eventToAdd.setRoomSize(reader.ReadString());
            events.Add(eventToAdd);
              }

              // fixed events
              else if (text == "FEDay")
              {
            fixedEventGene = new Gene();
            feDay = reader.ReadString();
            fixedEventGene.setCannotChange(true);
              }
              else if (text == "FETime")
            feTime = reader.ReadString();
              else if (text == "FEWeek")
              {
            fixedEventGene.setSlot(Genetic_Algorithms.Program.findSlot(getDayNum(feDay),
                                                                       Int32.Parse(feTime),
                                                                       reader.ReadString()));
              }
              else if (text == "FEDuration")
            fixedEventGene.getEvent().setDuration(Int32.Parse(reader.ReadString()));

              else if (text == "FECourseCode")
            fixedEventGene.getEvent().setCourse(reader.ReadString());
              else if (text == "FEActivity")
              {
            fixedEventGene.getEvent().setActivity(reader.ReadString());
            eventGroups = new List<string>();
              }
              else if (text == "FEGroup")
            eventGroups.Add(reader.ReadString());
              else if (text == "FERoom")
              {
            fixedEventGene.getEvent().setGroups(eventGroups);

            fixedEventGene.getRoom()._name = reader.ReadString();
            fixedEventGene.getRoom()._size = "";
            fixedEventGene.getRoom()._type = "";

            fixedEvents.Add(fixedEventGene);
              }

              // rooms
              else if (text == "RoomName")
              {
            roomToAdd = new Room();
            roomToAdd._name = reader.ReadString();
              }
              else if (text == "RoomSize")
            roomToAdd._size = reader.ReadString();
              else if (text == "RoomType")
              {
            roomToAdd._type = reader.ReadString();
            rooms.Add(roomToAdd);
              }

              // courses
              else if (text == "CourseName")
            courses.Add(reader.ReadString());

              // external rooms
              else if (text == "ExternalRoomName")
            externalRooms.Add(reader.ReadString());
            }
              }
              catch (Exception exp)
              {
            Console.WriteLine(exp);
              }
        }
        static void Main(string[] args)
        {
            groups = new List<string>();
              groups.Add("w");
              groups.Add("x");
              groups.Add("y");
              groups.Add("z");
              groups.Add("m");
              groups.Add("a");
              string[] allGroups = new string[] { groups[0], groups[1], groups[2], groups[3], groups[4] };

              rooms = new List<Room>();
              rooms.Add(new Room("1.1", "Lecture", "Large"));
              rooms.Add(new Room("1.3", "Lecture", "Medium"));
              rooms.Add(new Room("1.4", "Lecture", "Medium"));
              rooms.Add(new Room("1.5", "Lecture", "Medium"));
              rooms.Add(new Room("3rd Year Lab", "Lab", "Large"));
              rooms.Add(new Room("Unix", "Lab", "Large"));
              rooms.Add(new Room("Eng", "Lab", "Large"));
              rooms.Add(new Room("Tutorial Room", "Tutorial", "Large"));
              rooms.Add(new Room("LF15", "Examples Class", "Large"));
              rooms.Add(new Room("1.5", "Workshop", "Large"));

              lunchRoom = new Room("Lunch Room", "Lunch", "Large");

              // pain value array
              int[] painValues = {9, 8, 7, 3, 5, 3, 2, 4, // monday
                          7, 6, 5, 0, 3, 0, 1, 2, // tuesday
                          7, 5, 0, 0, 3, 0, 0, 3, // thursday
                          8, 7, 5, 3, 4, 5, 7, 8 // friday
                         };

              //0900-1700 4x = 8 x 4 = 32 + 4 hours on Wed = 36 hours
              slots = new List<Slot>();
              string[] weeks = { "A", "B" };
              int[] fullDaysID = { 1, 2, 4, 5 };

              foreach (string week in weeks)
              {
            int j = 0;
            foreach (int dayID in fullDaysID)
            {
              for (int time = 900; time < 1700; time += 100)
              {
            slots.Add(new Slot(dayID, time, week, painValues[j]));
            j++;
              }
            }

            int[] painValuesWed = { 7, 5, 2, 0 };
            int p = 0;
            for (int i = 900; i < 1300; i += 100)
            {
              slots.Add(new Slot(3, i, week, painValuesWed[p]));
              p++;
            }
              }

              slots = (from s in slots
               orderby s.getDay(), s.getTime()
               select s).ToList();

              // 22 events
              events = new List<Event>();

              /* ------------------- Lectures --------------------------- */
              events.Add(new Event("COMP10412", "Lecture", 1, true, new List<string>(new string[] { groups[0], groups[1], groups[2], groups[3], groups[4]}), "Large"));
              events.Add(new Event("COMP10092", "Lecture", 1, true, new List<string>(new string[] { groups[0], groups[1], groups[2], groups[3], groups[4], groups[5]}), "Large"));
              events.Add(new Event("COMP10020", "Lecture", 1, true, new List<string>(new string[] { groups[0], groups[1], groups[2], groups[3], groups[5]}), "Large"));
              events.Add(new Event("COMP10052", "Lecture", 1, true, new List<string>(new string[] { groups[0], groups[1], groups[2], groups[3], groups[4], groups[5]}), "Large"));
              events.Add(new Event("COMP10052", "Lecture", 1, true, new List<string>(new string[] { groups[0], groups[1], groups[2], groups[3], groups[4], groups[5]}), "Large"));
              events.Add(new Event("COMP10092", "Lecture", 1, true, new List<string>(new string[] { groups[0], groups[1], groups[2], groups[3], groups[4], groups[5]}), "Large"));
              events.Add(new Event("COMP10020", "Lecture", 1, true, new List<string>(new string[] { groups[0], groups[1], groups[2], groups[3], groups[5] }), "Large"));
              events.Add(new Event("COMP10042", "Lecture", 1, true, new List<string>(new string[] { groups[0], groups[1], groups[2], groups[3], groups[4]}), "Large"));
              events.Add(new Event("COMP10900", "Lecture", 1, true, new List<string>(new string[] { groups[0], groups[1], groups[2], groups[3], groups[4], groups[5]}), "Large"));
              events.Add(new Event("COMP10092", "Lecture", 1, true, new List<string>(new string[] { groups[0], groups[1], groups[2], groups[3], groups[4], groups[5]}), "Large"));
              events.Add(new Event("COMP10042", "Lecture", 1, true, new List<string>(new string[] { groups[0], groups[1], groups[2], groups[3], groups[4] }), "Large"));

              /* ------------------------ Group W events --------------------------- */
              events.Add(new Event("COMP10052", "Lab", 2, false, new List<string>(new string[] { groups[0], groups[4] }), "Large"));
              events.Add(new Event("COMP10412", "Lab", 2, false, new List<string>(new string[] { groups[0], groups[4] }), "Large"));
              events.Add(new Event("COMP10092", "Lab", 2, false, new List<string>(new string[] { groups[0], groups[4] }), "Large"));
              events.Add(new Event("COMP10900", "Lab", 1, true, new List<string>(new string[] { groups[0], groups[4] }), "Large"));
              events.Add(new Event("COMP10092", "Lab", 2, true, new List<string>(new string[] { groups[0], groups[4] }), "Large"));
              events.Add(new Event("COMP10052", "Examples Class", 1, false, new List<string>(new string[] { groups[0], groups[4] }), "Large"));
              events.Add(new Event("COMP10020", "Examples Class", 1, true, new List<string>(new string[] { groups[0] }), "Large"));
              events.Add(new Event("COMP10042", "Examples Class", 1, true, new List<string>(new string[] { groups[0], groups[4] }), "Large"));
              events.Add(new Event("COMP10412", "Examples Class", 1, true, new List<string>(new string[] { groups[0], groups[4] }), "Large"));
              events.Add(new Event("Tutorial", "Tutorial", 1, true, new List<string>(new string[] { groups[0] }), "Large"));

              /* ------------------------ Group X events --------------------------- */

              events.Add(new Event("COMP10412", "Lab", 2, false, new List<string>(new string[] { groups[1] }), "Large"));
              events.Add(new Event("COMP10092", "Lab", 2, false, new List<string>(new string[] { groups[1] }), "Large"));
              events.Add(new Event("COMP10900", "Lab", 1, true, new List<string>(new string[] { groups[1] }), "Large"));
              events.Add(new Event("COMP10052", "Lab", 2, false, new List<string>(new string[] { groups[1] }), "Large"));
              events.Add(new Event("COMP10092", "Lab", 2, true, new List<string>(new string[] { groups[1] }), "Large"));
              events.Add(new Event("COMP10042", "Examples Class", 1, true, new List<string>(new string[] { groups[1] }), "Large"));
              events.Add(new Event("COMP10020", "Examples Class", 1, true, new List<string>(new string[] { groups[1] }), "Large"));
              events.Add(new Event("COMP10412", "Examples Class", 1, true, new List<string>(new string[] { groups[1] }), "Large"));
              events.Add(new Event("COMP10052", "Examples Class", 1, false, new List<string>(new string[] { groups[1] }), "Large"));
              events.Add(new Event("Tutorial", "Tutorial", 1, true, new List<string>(new string[] { groups[1] }), "Large"));

              /* ------------------------ Group Y events --------------------------- */

              events.Add(new Event("COMP10092", "Lab", 2, false, new List<string>(new string[] { groups[2] }), "Large"));
              events.Add(new Event("COMP10412", "Lab", 2, false, new List<string>(new string[] { groups[2] }), "Large"));
              events.Add(new Event("COMP10052", "Lab", 2, false, new List<string>(new string[] { groups[2] }), "Large"));
              events.Add(new Event("COMP10092", "Lab", 2, true, new List<string>(new string[] { groups[2] }), "Large"));
              events.Add(new Event("COMP10900", "Lab", 1, true, new List<string>(new string[] { groups[2] }), "Large"));
              events.Add(new Event("COMP10020", "Examples Class", 1, true, new List<string>(new string[] { groups[2] }), "Large"));
              events.Add(new Event("COMP10052", "Examples Class", 1, false, new List<string>(new string[] { groups[2] }), "Large"));
              events.Add(new Event("COMP10042", "Examples Class", 1, true, new List<string>(new string[] { groups[2] }), "Large"));
              events.Add(new Event("COMP10412", "Examples Class", 1, true, new List<string>(new string[] { groups[2] }), "Large"));
              events.Add(new Event("Tutorial", "Tutorial", 1, true, new List<string>(new string[] { groups[2] }), "Large"));

              /* ------------------------ Group Z events --------------------------- */
              events.Add(new Event("COMP10052", "Lab", 2, false, new List<string>(new string[] { groups[3], groups[5] }), "Large"));
              events.Add(new Event("COMP10412", "Lab", 2, false, new List<string>(new string[] { groups[3] }), "Large"));
              events.Add(new Event("COMP10092", "Lab", 2, false, new List<string>(new string[] { groups[3], groups[5] }), "Large"));
              events.Add(new Event("COMP10900", "Lab", 1, true, new List<string>(new string[] { groups[3], groups[5] }), "Large"));
              events.Add(new Event("COMP10092", "Lab", 2, true, new List<string>(new string[] { groups[3], groups[5] }), "Large"));
              events.Add(new Event("COMP10042", "Examples Class", 1, true, new List<string>(new string[] { groups[3] }), "Large"));
              events.Add(new Event("COMP10052", "Examples Class", 1, false, new List<string>(new string[] { groups[3], groups[5] }), "Large"));
              events.Add(new Event("COMP10412", "Examples Class", 1, true, new List<string>(new string[] { groups[3] }), "Large"));
              events.Add(new Event("COMP10020", "Examples Class", 1, true, new List<string>(new string[] { groups[3], groups[5] }), "Large"));
              events.Add(new Event("Tutorial", "Tutorial", 1, true, new List<string>(new string[] { groups[3] }), "Large"));

              /* ------------------------ Group A events --------------------------- */
              events.Add(new Event("Tutorial", "Tutorial", 1, true, new List<string>(new string[] { groups[5] }), "Large"));

              /* ---------------------- Lunch Events ---------------------------- */
              lunchEvents = new List<Event>();
              lunchEvents.Add(new Event("Lunch", "Lunch", 1, true, new List<string>(new string[] { groups[0] }), "Large"));
              lunchEvents.Add(new Event("Lunch", "Lunch", 1, true, new List<string>(new string[] { groups[1] }), "Large"));
              lunchEvents.Add(new Event("Lunch", "Lunch", 1, true, new List<string>(new string[] { groups[2] }), "Large"));
              lunchEvents.Add(new Event("Lunch", "Lunch", 1, true, new List<string>(new string[] { groups[3] }), "Large"));
              lunchEvents.Add(new Event("Lunch", "Lunch", 1, true, new List<string>(new string[] { groups[4] }), "Large"));
              lunchEvents.Add(new Event("Lunch", "Lunch", 1, true, new List<string>(new string[] { groups[5] }), "Large"));

              /* -------------------------- Fixed events -----------------------*/
              List<Slot> allSlotsForEvent = new List<Slot>();
              fixedEvents = new List<Gene>();

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s) { return s.getDay() == 1 && s.getTime() == 1200; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("MATH10232", "Lecture", 1, true, new List<string>(new string[] { groups[4] }), "Large"),
                                 new Room("SCH RUTH", "Lecture", "Large"),
                                 true));
              }

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s) { return s.getDay() == 1 && s.getTime() == 1600; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("MATH10212", "Lecture", 1, true, new List<string>(new string[] { groups[4] }), "Large"),
                                 new Room("CHEM G.51", "Lecture", "Large"),
                                 true));
              }

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s) { return s.getDay() == 2 && s.getTime() == 900; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("MATH10232", "Lecture", 1, true, new List<string>(new string[] { groups[4] }), "Large"),
                                 new Room("SCH RUTH", "Lecture", "Large"),
                                 true));
              }

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s) { return s.getDay() == 3 && s.getTime() == 1200; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("MATH10212", "Lecture", 1, true, new List<string>(new string[] { groups[4] }), "Large"),
                                 new Room("SCH RUTH", "Lecture", "Large"),
                                 true));
              }

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s) { return s.getDay() == 5 && s.getTime() == 900; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("MATH10212", "Lecture", 1, true, new List<string>(new string[] { groups[4] }), "Large"),
                                 new Room("ALEX TH", "Lecture", "Large"),
                                 true));
              }

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s) { return s.getDay() == 5 && s.getTime() == 1300; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("MATH10232", "Lecture", 1, true, new List<string>(new string[] { groups[4] }), "Large"),
                                 new Room("SCH RUTH", "Lecture", "Large"),
                                 true));
              }

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s){ return s.getDay() == 2 && s.getTime() == 1000; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("BMAN10252", "Lecture", 1, true, new List<string>(new string[] {groups[5]}), "Large"),
                                 new Room("Ell Wilk A3.7", "Lecture", "Large"),
                                 true));
              }

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s){ return s.getDay() == 2 && s.getTime() == 1200; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("BMAN10552", "Lecture", 1, true, new List<string>(new string[] {groups[5]}), "Large"),
                                 new Room("CHAP", "Lecture", "Large"),
                                 true));
              }

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s) { return s.getDay() == 4 && s.getTime() == 1000; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("BMAN10252", "Lecture", 1, true, new List<string>(new string[] { groups[5] }), "Large"),
                                 new Room("MBS EAST F20", "Lecture", "Large"),
                                 true));
              }

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s) { return s.getDay() == 4 && s.getTime() == 1200; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("BMAN10552", "Workshop", 1, true, new List<string>(new string[] { groups[5] }), "Large"),
                                 new Room("1.5", "Workshop", "Large"),
                                 true));
              }

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s) { return s.getDay() == 4 && s.getTime() == 1400; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("BMAN10652", "Lecture", 1, true, new List<string>(new string[] { groups[5] }), "Large"),
                                 new Room("1.1", "Lecture", "Large"),
                                 true));
              }

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s) { return s.getDay() == 4 && s.getTime() == 1500; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("BMAN10252", "Lecture", 1, true, new List<string>(new string[] { groups[5] }), "Large"),
                                 new Room("MBS EAST F20", "Lecture", "Large"),
                                 true));
              }

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s) { return s.getDay() == 4 && s.getTime() == 1600; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("BMAN10552", "Lecture", 1, true, new List<string>(new string[] { groups[5] }), "Large"),
                                 new Room("CHAP", "Lecture", "Large"),
                                 true));
              }

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s) { return s.getDay() == 5 && s.getTime() == 1100; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("BMAN10612", "Lecture", 1, true, new List<string>(new string[] { groups[5] }), "Large"),
                                 new Room("CRAW TH.2", "Lecture", "Large"),
                                 true));
              }

              allSlotsForEvent = Program.slots.FindAll(delegate(Slot s) { return s.getDay() == 5 && s.getTime() == 1200; });
              foreach (Slot s in allSlotsForEvent)
              {
            fixedEvents.Add(new Gene(s,
                                 new Event("BMAN10612", "Lecture", 1, true, new List<string>(new string[] { groups[5] }), "Large"),
                                 new Room("CRAW TH.2", "Lecture", "Large"),
                                 true));
              }

              for (int repeating = 0; repeating < 5; repeating++)
              {
            // create initial population
            Population population = new Population();
            population.createInitialPopulation();
            //Console.WriteLine(population);

            //population.selection();
            population.crossover();
              }
              //population.createXML();

              Console.ReadLine();
        }
 /* this method is required as some places the chromosome is viewed
  * as a Chromosome instead of a List<Gene> */
 public void add(Slot s, Event e, Room r, bool cannotChange)
 {
     chromosome.Add(new Gene(s, e, r, cannotChange));
 }
        public void scheduleLunch()
        {
            /* For every event in the lunch events list, for every day that ought
               * to have lunch scheduled, choose either 1200 or 1300 to allocate
               * lunch to for that particular group */
              Room lunchRoom = new Room("Lunch Room", "Lunch", "Large");
              foreach (Event e in Program.lunchEvents)
              {
            List<Slot> tempLunchSlots = new List<Slot>();

            int[] days = { 1, 2, 4, 5 };
            foreach (int day in days)
            {
              tempLunchSlots = (from s in Program.slots
                            where ((s.getTime() == 1200 ||
                                    s.getTime() == 1300) &&
                                    s.getDay() == day)
                            select s).ToList();

              Slot selectedSlot = tempLunchSlots[random.Next(tempLunchSlots.Count)];
              chromosome.Add(new Gene(Program.slots[Program.slots.IndexOf(selectedSlot)],
                                  e , lunchRoom, false));
              chromosome.Add(new Gene(Program.slots[Program.slots.IndexOf(getCorrespondingSlot(selectedSlot))],
                                  e, lunchRoom, false));
            }
              }
        }