//to do here: implement the methods we need
        public static void TestAlgorithm()
        {
            // mock constraints
            var constraints = new ConstraintsCollection();

            constraints.Add(new MinimumFreeDaysConstraint(3));
            constraints.Add(new ClashesConstraint(true));

            // create courses

            var i       = 0;
            var courses = AllCoursesData.TakeWhile(x => i++ < 6).ToList();

            var scheduler    = new Scheduler();
            var allSolutions = scheduler.SolveSssp(courses, constraints);

            Console.WriteLine(allSolutions.Count());
            foreach (var solution in allSolutions)
            {
                solution.ExportToJson();
                if (constraints.IsConsistent(solution, solution.First()) == false)
                {
                    throw new Exception();
                }
                //PrintSolution(solution);
            }
        }
        public double Rate(ConstraintsCollection constraints)
        {
            double timetableRate = 0;

            foreach (var constraint in constraints)
            {
                timetableRate += constraint.RatePenalty * constraint.GetRateFactor(this);
            }

            return(Rating = timetableRate); // update Rating and return it
        }
示例#3
0
        public void BacktrackingAllSolutions(List <ClassType> variables, Timetable instantiation, IPriorityQueue <Timetable> allSolutions, int index, ConstraintsCollection constraints, CancellationToken cancellationToken)
        {
            if (variables.Count == index)
            {
                // check constraints who can be calculated only after timetable is fully instantiated
                if (!constraints.IsConsistent(instantiation, true))
                {
                    return;
                }

                var rate = instantiation.Rate(constraints);
                if (allSolutions.Count < 200)
                {
                    allSolutions.Add(instantiation);
                    return;
                }

                if (rate < allSolutions.FindMax().Rating)
                {
                    allSolutions.DeleteMax();
                    allSolutions.Add(instantiation);
                }

                return;
            }

            var variable = variables[index++];
            var domain   = variable.Groups;

            foreach (var value in domain)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                var tempTimetable = instantiation.Copy();
                tempTimetable.Add(value);
                if (constraints.IsConsistent(tempTimetable, false))
                {
                    BacktrackingAllSolutions(variables, tempTimetable, allSolutions, index, constraints, cancellationToken);
                }
            }
        }
示例#4
0
        public IEnumerable <Timetable> SolveSssp(System.Collections.Generic.IList <Course> courses, ConstraintsCollection constraints)
        {
            var allClassTypes = GetAllClassTypes(courses);
            var variables     = ApplyMrvHeuristic(allClassTypes);

            //allSolutions = MaxHeap
            var backtrackingSolutions = new IntervalHeap <Timetable>(200);

            // BT(Variables, {}, Domains, allSolutions)
            var cancellationTokenSource = new CancellationTokenSource();

            cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(20));
            var cancellationToken = cancellationTokenSource.Token;
            var backtrackingTask  = Task.Factory.StartNew(
                () =>
            {
                BacktrackingAllSolutions(variables, new Timetable(), backtrackingSolutions, 0, constraints, cancellationToken);
                return(backtrackingSolutions);
            }, cancellationToken);

            // Generic algorithm
            var geneticAlgorithmRunner = new TimetablerGeneticAlgorithmRunner(variables, constraints);
            var geneticTask            = geneticAlgorithmRunner.RunAsync();

            Task.WaitAll(backtrackingTask);

            var timetables = new List <Timetable>();

            if (cancellationToken.IsCancellationRequested)
            {
                geneticTask.Wait();
                timetables.Add(geneticAlgorithmRunner.GeneticSolution);
            }

            if (backtrackingSolutions.Any())
            {
                timetables.AddRange(backtrackingSolutions);
            }

            var sortedSolutions = SortByRate(timetables);

            //Return allSolutions
            return(sortedSolutions);
        }
示例#5
0
 public TimetablerGeneticAlgorithmRunner(IList <ClassType> variables, ConstraintsCollection constraints)
 {
     Variables   = variables;
     Constraints = constraints;
 }