Пример #1
0
        //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);
            }
        }
Пример #2
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);
                }
            }
        }
Пример #3
0
        private double TimetablerFitnessFunction(IChromosome c)
        {
            var timetable = ChromosomeToTimetable(c);

            if (Constraints.IsConsistent(timetable, true))
            {
                return(timetable.Rate(Constraints) * -1);
            }

            return(double.MinValue);
        }