Ejemplo n.º 1
0
        private static int getValidStartTimeValue(int startValue, double duration)
        {
            startValue = adjustStartValue(startValue);
            int weekDay         = startValue / 10000;
            int startHour       = startValue % 10000;
            int standardDuation = TimeValueInterval.hoursStandardValue(duration);
            int startNow        = startHour + standardDuation;
            int startPM         = MIN_PM_HOUR + standardDuation;
            int startAM         = MIN_AM_HOUR + standardDuation;

            if (startHour >= MIN_AM_HOUR && startNow <= MAX_AM_HOUR)
            {
                return(startValue);
            }
            else if (startHour >= MIN_PM_HOUR && startNow <= MAX_PM_HOUR)
            {
                return(startValue);
            }
            else if (startHour < MIN_PM_HOUR && startPM < MAX_PM_HOUR)
            {
                return(weekDay * 10000 + MIN_PM_HOUR);
            }
            else if (startAM < MAX_AM_HOUR)
            {
                return((weekDay + 1) * 10000 + MIN_AM_HOUR);
            }
            else if (startPM < MAX_PM_HOUR)
            {
                return((weekDay + 1) * 10000 + MIN_PM_HOUR);
            }

            Console.WriteLine("Error!!!!!! Duration is too long!!!!!!!!");
            return(-1);
        }
Ejemplo n.º 2
0
        // return a nonnull value except duration of one exam is too long
        private static TimeValueInterval getEarliest(List <TimeValueInterval> occupied, double duration)
        {
            int startValue = 0;

            startValue = getValidStartTimeValue(startValue, duration);
            if (startValue < 0)
            {
                return(null);
            }

            TimeValueInterval t = new TimeValueInterval(startValue, duration);

            while (true)
            {
                TimeValueInterval tmp = getConflictTimeData(occupied, t);
                if (tmp == null)
                {
                    break;
                }

                startValue = tmp.endValue;
                startValue = getValidStartTimeValue(startValue, duration);
                t.initial(startValue, duration);
            }

            return(t);
        }
Ejemplo n.º 3
0
 public SpecialEntity(int course, int room, int session, int protor, int startValue, double d)
 {
     courseId  = course;
     roomId    = room;
     sessionId = session;
     protorId  = protor;
     timeData  = new TimeValueInterval(startValue, d);
 }
Ejemplo n.º 4
0
 public SpecialEntity(int course, int room, int session, int protor, int week, int hour, double d)
 {
     courseId  = course;
     roomId    = room;
     sessionId = session;
     protorId  = protor;
     timeData  = new TimeValueInterval(week * 10000 + hour * 100, d);
 }
Ejemplo n.º 5
0
        public bool isConflict(TimeValueInterval another)
        {
            if (startValue < another.startValue)
            {
                return(endValue > another.startValue);
            }

            return(startValue < another.endValue);
        }
Ejemplo n.º 6
0
        public SpecialEntity(string course, string room, int session, string protor, string week, string hour, string d)
        {
            courseId  = string2Int(course);
            roomId    = string2Int(room);
            sessionId = session;
            protorId  = string2Int(protor);
            int    weekDay   = string2Int(week);
            double startHour = Convert.ToDouble(hour);
            double duration  = Convert.ToDouble(d);

            timeData = new TimeValueInterval(weekDay * 10000 + TimeValueInterval.hoursStandardValue(startHour), duration);
        }
Ejemplo n.º 7
0
        public void arrangeExam(TimeValueInterval data)
        {
            bool contain = false;

            foreach (TimeValueInterval tv in occupied)
            {
                if (tv.SameWith(data))
                {
                    contain = true;
                    break;
                }
            }

            if (!contain)
            {
                occupied.Add(data);
            }
        }
Ejemplo n.º 8
0
        private List <ExamEntity> normalArrange(CourseEntity ce, List <SessionEntity> remainSessions, TimeValueInterval timeData)
        {
            List <ExamEntity> ret = new List <ExamEntity>();
            int sessionCt         = remainSessions.Count;
            int sessionIndex      = 0;

            foreach (RoomEntity r in allRoom)
            {
                if (r.roomType == ce.requiredRoomType && getConflictTimeData(r.occupied, timeData) == null)
                {
                    ExamEntity entity = new ExamEntity();
                    entity.courseId    = ce.courseId;
                    entity.roomId      = r.roomId;
                    entity.roomType    = r.roomType;
                    entity.startMinute = TimeValueInterval.value2Min(timeData.startValue);
                    entity.startHour   = TimeValueInterval.value2Hour(timeData.startValue);
                    entity.weekDay     = TimeValueInterval.value2Weekday(timeData.startValue);

                    entity.endMinute = TimeValueInterval.value2Min(timeData.endValue);
                    entity.endHour   = TimeValueInterval.value2Hour(timeData.endValue);

                    r.arrangeExam(timeData);

                    // Jane: added null check 20180426 if (allLevelTimeLine[ce.level] != null)
                    if (allLevelTimeLine[ce.level] != null)
                    {
                        allLevelTimeLine[ce.level].Add(timeData);
                    }

                    int sessionsThisRoom = r.roomCapacity / SESSION_CAPACITY;
                    for (int i = 0; i < sessionsThisRoom; ++i, ++sessionIndex)
                    {
                        if (sessionIndex >= remainSessions.Count)
                        {
                            break;
                        }

                        entity.sessionIds.Add(remainSessions[sessionIndex]);
                        //if (protorId <= 0) protorId = remainSessions[sessionIndex].facultyId;
                    }

                    bool         hasProtor      = false;
                    ProtorEntity sessionFaculty = null;
                    foreach (SessionEntity se in entity.sessionIds)
                    {
                        int protorId = se.facultyId;
                        foreach (ProtorEntity pe in allProtor)
                        {
                            if (pe.protorId == protorId)
                            {
                                sessionFaculty = pe;
                                break;
                            }
                        }

                        if (sessionFaculty != null)
                        {
                            if (getConflictTimeData(sessionFaculty.occupied, timeData) == null)
                            {
                                entity.protorId = sessionFaculty.protorId;
                                sessionFaculty.arrangeExam(timeData);
                                hasProtor = true;
                                break;
                            }
                        }
                    }

                    if (!hasProtor)
                    {
                        bool valid = true;
                        foreach (ProtorEntity pe in allProtor)
                        {
                            valid = true;
                            foreach (SessionEntity ss in ce.sessionIds)
                            {
                                if (pe.protorId == ss.sessionId && pe.protorId > 0)
                                {
                                    valid = false;
                                    break;
                                }
                            }

                            if (!valid)
                            {
                                continue;
                            }

                            if (getConflictTimeData(pe.occupied, timeData) == null)
                            {
                                entity.protorId = pe.protorId;
                                pe.arrangeExam(timeData);
                                break;
                            }
                        }

                        Console.WriteLine("DOES NOT use teacher as protor!!!");
                    }

                    ret.Add(entity);
                }

                if (sessionCt <= sessionIndex)
                {
                    break;
                }
            }

            remainSessions.Clear();
            return(ret);
        }
Ejemplo n.º 9
0
        private ExamEntity specialArrange(CourseEntity course, SpecialEntity special, TimeValueInterval timeData, List <SessionEntity> remainSessions, List <SpecialEntity> specialsOfThisCourse)
        {
            int        sessionCt    = remainSessions.Count;
            int        sessionIndex = 0;
            RoomEntity room         = null;

            foreach (RoomEntity r in allRoom)
            {
                if (special.roomId > 0)
                {
                    if (r.roomId == special.roomId)
                    {
                        room = r;
                        break;
                    }
                }
                else
                {
                    if (r.roomType == course.requiredRoomType && getConflictTimeData(r.occupied, timeData) == null)
                    {
                        room = r;
                        foreach (SpecialEntity sot in specialsOfThisCourse)
                        {
                            if (sot.roomId <= 0)
                            {
                                continue;
                            }
                            if (r.roomId == sot.roomId)
                            {
                                room = null;
                                break;
                            }
                        }

                        if (room != null)
                        {
                            break;
                        }
                    }
                }
            }

            if (room == null)
            {
                return(null);
            }

            bool       hasSpecialProtor = special.protorId > 0;
            ExamEntity exam             = new ExamEntity();

            exam.courseId    = special.courseId;
            exam.roomId      = special.roomId;
            exam.roomType    = room.roomType;
            exam.startMinute = TimeValueInterval.value2Min(timeData.startValue);
            exam.startHour   = TimeValueInterval.value2Hour(timeData.startValue);
            exam.weekDay     = TimeValueInterval.value2Weekday(timeData.startValue);

            exam.endMinute = TimeValueInterval.value2Min(timeData.endValue);
            exam.endHour   = TimeValueInterval.value2Hour(timeData.endValue);

            int sessionsThisRoom = room.roomCapacity / SESSION_CAPACITY;

            if (special.sessionId > 0)
            {
                SessionEntity target = null;
                foreach (SessionEntity s in remainSessions)
                {
                    if (s.sessionId == special.sessionId)
                    {
                        target = s;
                        break;
                    }
                }

                if (target != null)
                {
                    exam.sessionIds.Add(target);
                    remainSessions.Remove(target);
                }
                --sessionsThisRoom;
            }

            for (int i = 0; i < sessionsThisRoom; ++sessionIndex)
            {
                if (sessionIndex >= remainSessions.Count)
                {
                    break;
                }

                bool sessionInSpecial = false;
                foreach (SpecialEntity e in specialsOfThisCourse)
                {
                    if (e.courseId == course.courseId && e.roomId != room.roomId && e.sessionId == remainSessions[sessionIndex].sessionId)
                    {
                        sessionInSpecial = true;
                        break;
                    }
                }

                if (!sessionInSpecial)
                {
                    exam.sessionIds.Add(remainSessions[sessionIndex]);
                    ++i;
                }
            }

            remainSessions.RemoveRange(0, sessionIndex);

            if (hasSpecialProtor)
            {
                exam.protorId = special.protorId;
                ProtorEntity pe = null;
                foreach (ProtorEntity p in allProtor)
                {
                    if (p.protorId == special.protorId)
                    {
                        pe = p;
                        break;
                    }
                }

                if (pe != null)
                {
                    pe.arrangeExam(timeData);
                }
            }
            else
            {
                ProtorEntity sessionFaculty = null;
                foreach (SessionEntity ss in exam.sessionIds)
                {
                    foreach (ProtorEntity pe in allProtor)
                    {
                        if (ss.facultyId == pe.protorId)
                        {
                            sessionFaculty = pe;
                            break;
                        }
                    }

                    if (sessionFaculty != null)
                    {
                        if (getConflictTimeData(sessionFaculty.occupied, timeData) == null)
                        {
                            break;
                        }
                        else
                        {
                            sessionFaculty = null;
                        }
                    }
                }

                if (sessionFaculty != null)
                {
                    exam.protorId = sessionFaculty.protorId;
                    sessionFaculty.arrangeExam(timeData);
                }
                else
                {
                    foreach (ProtorEntity pe in allProtor)
                    {
                        sessionFaculty = pe;
                        foreach (SpecialEntity e in specialsOfThisCourse)
                        {
                            if (sessionFaculty.protorId == e.protorId)
                            {
                                sessionFaculty = null;
                                break;
                            }
                        }

                        if (sessionFaculty == null)
                        {
                            continue;
                        }

                        if (getConflictTimeData(sessionFaculty.occupied, timeData) == null && sessionFaculty.protorId != special.protorId)
                        {
                            exam.protorId = sessionFaculty.protorId;
                            sessionFaculty.arrangeExam(timeData);
                            break;
                        }
                    }
                }
            }

            room.arrangeExam(timeData);

            // Jane: added null check 20180426 if (allLevelTimeLine[ce.level] != null)
            if (allLevelTimeLine[course.level] != null)
            {
                allLevelTimeLine[course.level].Add(timeData);
            }

            return(exam);
        }
Ejemplo n.º 10
0
        private TimeValueInterval getEarlistTimeData(CourseEntity ce)
        {
            TimeValueInterval earlist = getEarliest(allLevelTimeLine[ce.level], ce.duration);
            TimeValueInterval ret = null;
            int count = 0, sessionCt = ce.sessionIds.Count, roomCt = 0;

            for (int value = earlist.startValue; ; value += 100)
            {
                TimeValueInterval t = new TimeValueInterval(value, ce.duration);
                if (!t.isValid())
                {
                    continue;
                }

                count = 0;
                foreach (RoomEntity re in allRoom)
                {
                    if (re.roomType == ce.requiredRoomType && getConflictTimeData(re.occupied, t) == null)
                    {
                        count += re.roomCapacity / SESSION_CAPACITY;
                        ++roomCt;
                        if (count >= sessionCt)
                        {
                            ret = t;
                            break;
                        }
                    }
                }

                if (ret == null)
                {
                    continue;
                }

                count = 0;
                foreach (SessionEntity se in ce.sessionIds)
                {
                    if (se.facultyId <= 0)
                    {
                        continue;
                    }

                    ProtorEntity specialProtor = null;
                    foreach (ProtorEntity pe in allProtor)
                    {
                        if (pe.protorId == se.facultyId)
                        {
                            specialProtor = pe;
                            break;
                        }
                    }

                    if (specialProtor != null)
                    {
                        if (getConflictTimeData(specialProtor.occupied, t) == null)
                        {
                            ++count;
                        }
                        else
                        {
                            count = -1;
                            break;
                        }
                    }
                }

                if (count < 0)
                {
                    continue;
                }

                if (count < roomCt)
                {
                    foreach (ProtorEntity pe in allProtor)
                    {
                        if (getConflictTimeData(pe.occupied, t) == null)
                        {
                            ++count;
                            if (count >= roomCt)
                            {
                                break;
                            }
                        }
                    }
                }

                if (count < roomCt)
                {
                    continue;
                }

                if (getConflictTimeData(allLevelTimeLine[ce.level], ret) == null)
                {
                    break;
                }

                if (value >= MAX_EXAM_DAYS * 10000)
                {
                    ret = null;
                    break;
                }
            }

            return(ret);
        }
Ejemplo n.º 11
0
        private static TimeValueInterval getConflictTimeData(List <TimeValueInterval> occupied, TimeValueInterval one)
        {
            // Jane: added null check 20180426 if (occupied != null)
            if (occupied != null)
            {
                foreach (TimeValueInterval ti in occupied)
                {
                    if (ti != null && ti.isConflict(one))
                    {
                        return(ti);
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 12
0
 public bool SameWith(TimeValueInterval another)
 {
     return(startValue == another.startValue && endValue == another.endValue);
 }
Ejemplo n.º 13
0
        public ExamTables arrangeExam()
        {
            ExamTables ret = new ExamTables();

            maxLevel = -1;
            sortExamCourse();
            getProtorRoomAndSpecial();
            allLevelTimeLine = new List <TimeValueInterval> [maxLevel + 1];
            for (int i = 0; i < maxLevel; ++i)
            {
                allLevelTimeLine[i] = new List <TimeValueInterval>();
            }

            while (allSpecial.Count > 0)
            {
                int courseId            = allSpecial[0].courseId;
                List <SpecialEntity> s  = checkInSpecial(courseId);
                CourseEntity         ce = null;
                foreach (CourseEntity c in allExamCourse)
                {
                    if (c.courseId == courseId)
                    {
                        ce = c;
                        break;
                    }
                }

                if (ce == null)
                {
                    foreach (SpecialEntity se in s)
                    {
                        allSpecial.Remove(se);
                    }
                    continue;
                }

                List <SessionEntity> remainSessions = new List <SessionEntity>(ce.sessionIds);
                TimeValueInterval    timeData       = allSpecial[0].timeData;

                if (s.Count > 0) // special arrangement(course with a specified room an start time)
                {
                    foreach (SpecialEntity se in s)
                    {
                        ExamEntity e = specialArrange(ce, se, timeData, remainSessions, s);
                        ret.addExam(e);
                    }
                }

                if (remainSessions.Count > 0)
                {
                    List <ExamEntity> el = normalArrange(ce, remainSessions, timeData);
                    foreach (ExamEntity exam in el)
                    {
                        ret.addExam(exam);
                    }
                }

                foreach (SpecialEntity se in s)
                {
                    allSpecial.Remove(se);
                }

                allExamCourse.Remove(ce);
            }

            while (allExamCourse.Count > 0)
            {
                CourseEntity         ce             = allExamCourse[0];
                List <SessionEntity> remainSessions = new List <SessionEntity>(ce.sessionIds);
                TimeValueInterval    timeData       = getEarlistTimeData(ce);
                if (timeData == null)
                {
                    allExamCourse.Remove(ce);
                    Console.WriteLine("Course " + ce.courseId + " required impoosible resourses");
                    continue;
                }

                if (remainSessions.Count > 0)
                {
                    List <ExamEntity> el = normalArrange(ce, remainSessions, timeData);
                    foreach (ExamEntity exam in el)
                    {
                        ret.addExam(exam);
                    }
                }

                allExamCourse.Remove(ce);
            }

            foreach (CourseEntity ce in nonExamCourse)
            {
                ExamEntity ex = new ExamEntity();
                ex.courseId   = ce.courseId;
                ex.protorId   = -1;
                ex.sessionIds = ce.sessionIds;
                ex.roomType   = ce.requiredRoomType;
                ret.addExam(ex);
            }

            return(ret);
        }