예제 #1
0
        private bool IsValid(OfferedCourse offeredCourse, ReductionStepAction action, OfferedCourseRow offeredCourseRow = null)
        {
            if (action == ReductionStepAction.WHITE_ONE_RED_ROW)
            {
                return true;
            }
            else if (action == ReductionStepAction.WHITE_ALL_COURSES_ROWS)
            {
                return true;
            }
            else if (action == ReductionStepAction.RED_ALL_COURSES_ROWS)
            {
                if (offeredCourse.Color == ReductionStep2ColorStatus.Green)
                {
                    return false;
                }
                else if (offeredCourse.Color == ReductionStep2ColorStatus.WHITE)
                {
                    return true;
                }
                else throw new Exception();
            }
            else if (action == ReductionStepAction.UNCHECK_COURSE_MUST_BE_TAKE)
            {
                List<int> lst = new List<int>();
                foreach (var o in greenCourses)
                    if (o != offeredCourse)
                        lst.Add(o.Course.Id);
                //is valid state must be checked
                return MainCurriculumSateValidator.IsValidState(mainCurriculum, lst);
            }
            else if (action == ReductionStepAction.RED_ONE_WHITE_ROW)
            {
                if (offeredCourse.Color == ReductionStep2ColorStatus.WHITE)
                {
                    return true;
                }
                else if (offeredCourse.Color == ReductionStep2ColorStatus.Green)
                {
                    //temorary change the actual current value
                    offeredCourseRow.Color = ReductionStep2ColorStatus.RED;

                    List<Box> boxes = new List<Box>
                    {
                        ReductionStep2ServiceProvider.CreateBoxForOfferedCourse(offeredCourse)
                    };

                    //restore actual value
                    offeredCourseRow.Color = ReductionStep2ColorStatus.WHITE;

                    foreach (var item in greenCourses)
                    {
                        if (item != offeredCourse)
                            boxes.Add(ReductionStep2ServiceProvider.CreateBoxForOfferedCourse(item));
                    }

                    List<Box> res = null;
                    var task = Task.Run(() => ReductionStep2ServiceProvider.Validate(boxes, exampCollideChecking));
                    if (task.Wait(TimeSpan.FromMilliseconds(timeoutMs)))
                        res = task.Result;
                    else
                    {
                        res = null;
                    }



                    if (res == null) return false;
                    return true;

                }
                else
                    throw new Exception();
            }
            else if (action == ReductionStepAction.CHECK_COURSE_MUST_BE_TAKE)
            {
                if (offeredCourse.Course.Units + currentMustUnits > maxUnits)
                {
                    return false;
                }

                List<int> lst = new List<int>();
                foreach (var o in greenCourses)
                    lst.Add(o.Course.Id);
                lst.Add(offeredCourse.Course.Id);
                //is valid state must be checked
                bool output = MainCurriculumSateValidator.IsValidState(mainCurriculum, lst);
                if (!output) return false;

                output = false;
                for (int a = 0; a < offeredCourse.OfferedCourseRows.Count; a++)
                {
                    if (offeredCourse.OfferedCourseRows[a].Color == ReductionStep2ColorStatus.WHITE)
                    {
                        output = true;
                        break;
                    }
                }
                if (!output) return false;

                Box b1 = ReductionStep2ServiceProvider.CreateBoxForOfferedCourse(offeredCourse);

                List<Box> boxes = new List<Box>
                {
                    b1
                };
                foreach (var item in greenCourses)
                {
                    boxes.Add(ReductionStep2ServiceProvider.CreateBoxForOfferedCourse(item));
                }

                List<Box> res = null;
                var task = Task.Run(() => ReductionStep2ServiceProvider.Validate(boxes, exampCollideChecking));
                if (task.Wait(TimeSpan.FromMilliseconds(timeoutMs)))
                    res = task.Result;
                else
                {
                    res = null;
                }

                if (res == null) return false;

                return true;
            }

            throw new Exception();
            //return false;
        }
예제 #2
0
        public void Run(double[] courseScore, List <OfferedCourse> whiteCourses, List <OfferedCourse> greenCourses, int minUnits, int maxUnits, int greenCoursesUnits, double whiteCoursesTotalScore, MainCurriculum mainCurriculum, List <Box> GreenCoursesBoxes, bool examCollideCheck, AlgorithmTopWeeklyProgramManager ChoosedWeeklyProgram, int timeout)
        {
            bool x = false;

            Task.Run(() =>
            {
                Thread.Sleep(timeout);
                x = true;
            });
            List <int> takenCoursesId = new List <int>();

            while (true)
            {
                if (x)
                {
                    return;
                }
                while (true)
                {
                    if (x)
                    {
                        return;
                    }
                    do
                    {
                        if (x)
                        {
                            return;
                        }

                        Selected             = new bool[whiteCourses.Count];
                        Score                = 0;
                        CurrentSelectedUnits = 0;

                        takenCoursesId.Clear();

                        //init and assigning values to Selected array
                        for (int i = 0; i < Selected.Length; i++)
                        {
                            var offeredCourse = whiteCourses[i];
                            var course        = whiteCourses[i].Course;

                            var pc = random.Next(100);

                            if (CurrentSelectedUnits + course.Units + greenCoursesUnits > maxUnits)
                            {
                                continue;
                            }

                            if (pc <= courseScore[course.Id] * 100.0 / whiteCoursesTotalScore)
                            {
                                Selected[i]           = true;
                                CurrentSelectedUnits += course.Units;
                                takenCoursesId.Add(course.Id);
                            }
                        }

                        if (minUnits > CurrentSelectedUnits + greenCoursesUnits)
                        {
                            //no valid
                        }
                    }while (minUnits > CurrentSelectedUnits + greenCoursesUnits);
                    greenCourses.ForEach(gc => takenCoursesId.Insert(0, gc.Course.Id));
                    //two step validation
                    bool o = MainCurriculumSateValidator.IsValidState(mainCurriculum, takenCoursesId);
                    if (o)
                    {
                        break;
                    }
                }

                List <Box> boxes = new List <Box>();
                GreenCoursesBoxes.ForEach(b => boxes.Add(b));
                for (int i = 0; i < Selected.Length; i++)
                {
                    if (Selected[i])
                    {
                        Box b = ReductionStep2ServiceProvider.CreateBoxForOfferedCourse(whiteCourses[i]);
                        boxes.Add(b);
                    }
                }

                List <Box> res = ReductionStep2ServiceProvider.Validate(boxes, examCollideCheck);

                if (res != null)
                {
                    //break;
                    ChoosedWeeklyProgram.AddUpdateRangeData(res[0].Rows);
                }
            }
        }//end method Run