Example #1
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);
              }
        }
        private void bookSlots(Slot s, Gene g)
        {
            if (g.getEvent().getWeekly() && s.getWeek() == "B")
            s = getCorrespondingSlot(s);

              for (int i = 0; i < g.getEvent().getDuration(); i++)
              {
            int slotIndex = Program.slots.IndexOf(s);
            slotIndex += i;
            s = Program.slots[slotIndex];
            chromosome.Add(new Gene(s, g.getEvent(), g.getRoom(), g.getCannotChange()));

            slotIndex = Program.slots.IndexOf(s);
            slotIndex++;
            if (slotIndex < Program.slots.Count)
              s = Program.slots[slotIndex];

            if (g.getEvent().getWeekly())
              chromosome.Add(new Gene(s, g.getEvent(), g.getRoom(), g.getCannotChange()));
              }
        }
 public bool scheduleEventFromList(Slot s, Gene g)
 {
     if (!hasEventBeenScheduled(g.getEvent()))
       {
     bool clashes = doClashesExist(s, g.getEvent(), g.getRoom());
     if (!clashes && g.getEvent().getWeekly())
       clashes = doClashesExist(getCorrespondingSlot(s), g.getEvent(), g.getRoom());
     if (!clashes && enoughTimeInDay(s, g.getEvent()))
     {
       bookSlots(s, g);
       return true;
     }
     else
       return false;
       }
       return true;
 }
 public void scheduleUnscheduableEvents(Slot s)
 {
     Slot originalSlot = s;
       for (int i = unscheduableEvents.Count; i > 0; i--)
       {
     s = originalSlot;
     if (hasEventBeenScheduled(unscheduableEvents[i - 1]))
       unscheduableEvents.Remove(unscheduableEvents[i - 1]);
     else
     {
       if (enoughTimeInDay(s, unscheduableEvents[i - 1]))
       {
     List<Room> possibleRooms = findAvailableRooms(unscheduableEvents[i - 1].getRoomSize(),
                                                   unscheduableEvents[i - 1].getActivity());
     bool stop = false;
     int j = 0;
     while ((j < possibleRooms.Count) && (!stop))
     {
       bool clashes = doClashesExist(s, unscheduableEvents[i - 1], possibleRooms[j]);
       if (!clashes && unscheduableEvents[i-1].getWeekly())
         clashes = doClashesExist(getCorrespondingSlot(s), unscheduableEvents[i-1], possibleRooms[j]);
       if (!clashes)
       {
         stop = true;
         Gene aGene = new Gene(s, unscheduableEvents[i-1], possibleRooms[j], unscheduableEvents[i-1].getWeekly());
         bookSlots(s, aGene);
       }
       j++;
     }
       }
     }
       }
 }
        public void scheduleEvent(Event e)
        {
            /* Choose a random slot to attempt to schedule this event in */
              Slot selectedSlot = Program.slots[random.Next(Program.slots.Count)];
              if (e.getWeekly() && selectedSlot.getWeek() == "B")
            selectedSlot = getCorrespondingSlot(selectedSlot);

              List<Room> availableRooms = findAvailableRooms(e.getRoomSize(), e.getActivity());
              Room selectedRoom = null;

              bool scheduable = false;

              /* If there is enough time remaining in the day to schedule this event
               * at this slot, try to find a room that is available and does not cause
               * a clash */
              if (enoughTimeInDay(selectedSlot, e))
              {
            int i = 0;
            while ((i < availableRooms.Count) && (!scheduable))
            {
              selectedRoom = availableRooms[i];
              bool clashes = doClashesExist(selectedSlot, e, selectedRoom);
              if (!clashes && e.getWeekly())
            clashes = doClashesExist(getCorrespondingSlot(selectedSlot), e, selectedRoom);
              if (!clashes)
            scheduable = true;
              i++;
            }
              }

              /* if this random slot is not feasible then go from this slot + 1 to the end of the
               * slots list trying to schedule this event in each slot */
              if (!scheduable)
              {
            int selectedSlotIndex = Program.slots.IndexOf(selectedSlot);
            int i = selectedSlotIndex + 2;
            while ((i < Program.slots.Count) && (!scheduable))
            {
              selectedSlot = Program.slots[i];
              if (enoughTimeInDay(selectedSlot, e))
              {
            int j = 0;
            while ((j < availableRooms.Count) && (!scheduable))
            {
              selectedRoom = availableRooms[j];
              bool clashes = doClashesExist(selectedSlot, e, selectedRoom);
              if (!clashes && e.getWeekly())
                clashes = doClashesExist(getCorrespondingSlot(selectedSlot), e, selectedRoom);
              if (!clashes)
                scheduable = true;
              j++;
            }
              }
              i += 2;
            }

            /* go from the beginning of the slots to the slot that was initially chosen - 1
             * only if a slot hasn't been found */
            if (!scheduable)
            {
              i = 0;
              while ((i < selectedSlotIndex) && (!scheduable))
              {
            selectedSlot = Program.slots[i];
            if (enoughTimeInDay(selectedSlot, e))
            {
              int j = 0;
              while ((j < availableRooms.Count) && (!scheduable))
              {
                selectedRoom = availableRooms[j];
                bool clashes = doClashesExist(selectedSlot, e, selectedRoom);
                if (!clashes && e.getWeekly())
                  clashes = doClashesExist(getCorrespondingSlot(selectedSlot), e, selectedRoom);
                if (!clashes)
                  scheduable = true;
                j++;
              }
            }
            i += 2;
              }
            }
              }

              if (scheduable)
              {
            Gene aGene = new Gene(selectedSlot, e, selectedRoom, false);
            bookSlots(selectedSlot, aGene);
              }
              else
            unscheduableEvents.Add(e);
        }