コード例 #1
0
ファイル: BasicTests.cs プロジェクト: lulzzz/UniTimetabling
        public void TestSolveComp05ToOptimalityUD2()
        {
            var data        = Data.ReadXml(dataPath + ITC_Comp05, "120", "4");
            var formulation = ProblemFormulation.UD2;


            var sol = new Solution(data, formulation);

            sol.Read(dataPath + @"ITC2007\comp05-UD2.sol");

            var model = new CCTModel(data, formulation);

            model.ModelParameters.SaveBoundInformation = true;
            model.MipStart(sol._assignments.ToList());

            //model.WriteModel(@"c:\temp\model.lp");
            Console.WriteLine("Solving");
            model.Optimize(300);

            sol.SetAssignments(model.GetAssignments());
            Console.WriteLine(sol.AnalyzeSolution());
            Assert.IsTrue(sol.IsFeasible, "solution should be feasible");
            //  Assert.AreEqual(0,sol.Objective);

            Console.WriteLine("MWD;CC;obj;bnd");
            Console.WriteLine(String.Join("\n", model.bounds.Select(t => $"{t.Item1};{t.Item2};{t.Item3};{t.Item4}")));
        }
コード例 #2
0
ファイル: BasicTests.cs プロジェクト: lulzzz/UniTimetabling
        public void TestStageI(string filename)
        {
            var data        = Data.ReadXml(dataPath + filename, "120", "4");
            var formulation = ProblemFormulation.UD2NoOverbook;
            var mippar      = new CCTModel.MIPModelParameters()
            {
                UseStageIandII        = false,
                UseHallsConditions    = true,
                UseRoomHallConditions = true,
                TuneGurobi            = true,
                SaveBoundInformation  = true,
            };
            var model = new CCTModel(data, formulation, mippar);

            //model.WriteModel(@"c:\temp\model.lp");
            Console.WriteLine("Solving");
            model.Optimize(20);
            model.DisplayObjectives();
            var sol = new Solution(data, formulation);

            sol.SetAssignments(model.GetAssignments());
            // model.DisplaySoftConstraintViolations();
            //var sol = new Solution(data,Formulation);
            //sol.SetAssignments(model.GetAssignments());
            //Console.WriteLine(sol.AnalyzeSolution());
            //sol.Save(@"c:\temp\solution.ctt");
            //            File.AppendAllText(@"c:\temp\results.txt", $"30s;{filename};{model.Objective}\n");

            Console.WriteLine("MWD;CC;obj;bnd");
            Console.WriteLine(String.Join("\n", model.bounds.Select(t => $"{t.Item1};{t.Item2};{t.Item3};{t.Item4}")));

            Console.WriteLine(sol.AnalyzeSolution());
        }
コード例 #3
0
ファイル: BasicTests.cs プロジェクト: lulzzz/UniTimetabling
        public void TestRoomUnsuitabilityConstraint()
        {
            var data = Data.ReadXml(dataPath + ITC_Comp05, "120", "4");
            //var formulation = ProblemFormulation.UD2;
            var formulation = ProblemFormulation.UD2;

            formulation.UnsuitableRoomsWeight = 1;
            formulation.BadTimeslotsWeight    = 1;
            //  formulation.StudentMinMaxLoadWeight = 1;
            //  formulation.OverbookingAllowed = 0.15;

            var model = new CCTModel(data, formulation);

            Console.WriteLine("Solving");
            model.WriteModel(@"c:\temp\model.lp");

            model.Optimize(900);
            model.WriteModel(@"c:\temp\solution.sol");
            var sol = new Solution(data, formulation);

            sol.SetAssignments(model.GetAssignments());
            Console.WriteLine(sol.AnalyzeSolution());
            Assert.IsTrue(sol.IsFeasible, "solution should be feasible");
            // Assert.AreEqual(5, sol.Objective);
        }
コード例 #4
0
        private void RunWieghtedSumMetod(int minSoftConsViol, int maxSoftConsViol, double minCost, double maxCost)
        {
            List <MultiResult> pareto;

            var queue = new Queue <Tuple <int, int, int, int> >();

            queue.Enqueue(Tuple.Create(minSoftConsViol, maxSoftConsViol, (int)minCost, (int)maxCost));
            for (int i = 0; i < Steps; i++)
            {
                if (queue.Count == 0)
                {
                    Console.WriteLine("Queue empty.. breaking");
                    break;
                }
                var point = queue.Dequeue();
                Console.WriteLine($"Point: {point}");

                var softconstweight = point.Item4 - point.Item3;
                var costweight      = point.Item2 - point.Item1;

                if (costweight < 0.1 || softconstweight < 0.1)
                {
                    Console.WriteLine("no slope.. skipping");
                    continue;
                }

                _model.SetObjective(softconstweight, costweight);

                Console.WriteLine($"Slope set to: {(double) softconstweight/costweight : #.##}");
                var solfound = _model.Optimize(Timelimit, MIPGap);
                Console.WriteLine($"LastSolution:\n(cost,softobj) = {_model.ObjCost} , {_model.ObjSoftCons}");
                if (solfound)
                {
                    queue.Enqueue(Tuple.Create(point.Item1, _model.ObjSoftCons, (int)_model.ObjCost, point.Item4));
                    queue.Enqueue(Tuple.Create(_model.ObjSoftCons, point.Item2, point.Item3, (int)_model.ObjCost));
                    // Maybe remove dominated solutions.
                    // _model.ObjBound <= cost*costweight + softobj*softobjweigh
                    var costBound    = (int)Math.Floor(_model.ObjBound / costweight);
                    var softobjBound = (int)Math.Floor(_model.ObjBound / softconstweight);

                    _multiResults.Add(new MultiResult(_model.ObjCost, _model.ObjSoftCons, costBound, softobjBound,
                                                      _model.GetUsedRooms(),
                                                      _model.GetAssignments(), null, null, (int)_stopwatch.Elapsed.TotalSeconds));
                }
                AddAllSolutions(_model);
            }
        }
コード例 #5
0
        public Solution SolveStageII()
        {
            var model = new CCTModel(data, formulation, new CCTModel.MIPModelParameters()
            {
                UseStageIandII = true,
            });

            model.SetAndFixSolution(_solution._assignments.ToList());
            //model.MipStart(_solution._assignments.ToList());
            model.Optimize(1200);
            _solution.SetAssignments(model.GetAssignments());
            return(_solution);
        }
コード例 #6
0
        public Solution RunLargeCoursesFirst()
        {
            var stopwatch = Stopwatch.StartNew();

            var cutlimit   = 0.1;
            var ncourses   = 10;//(int) 0.1*data.Courses.Count;
            var newcourses = data.Courses.OrderByDescending(c => c.NumberOfStudents).Take(ncourses).ToList();
            var remcourse  = data.Courses.Except(newcourses).ToList();

            var newdata = new Data(data.Courses, data.Lecturers, data.Rooms, data.Curricula, data.TimeSlots, data.Days, data.Periods, data.MinimumPeriodsPerDay, data.MaximumPeriodsPerDay);

            newdata.CleanRemovedCourses(remcourse);

            Console.WriteLine($"Solving with {newcourses.Count} ({(double) newcourses.Count/data.Courses.Count : 0.00%})");
            var modelpre = new CCTModel(newdata, formulation, MIPmodelParameters);

            modelpre.Optimize();
            var ass = modelpre.GetAssignments();

            var model = new CCTModel(data, formulation, MIPmodelParameters);

            // model.SetAndFixSolution(ass);

            model.Optimize();

            //    model.Optimize(timetotighten+50,cutoff:500);
            //     model.ModelParameters.AbortSolverOnZeroPenalty = true;
            var tighttimer = Stopwatch.StartNew();

            model.Optimize(Timetotighten + 900);
            tighttimer.Stop();
            model.DisplayObjectives();
            _solution = new Solution(data, formulation);
            _solution.SetAssignments(model.GetAssignments());

            Console.WriteLine($"Unavailableused: {model.GetUnavailableused()} ({(double)model.GetUnavailableused() / data.Courses.Sum(c => c.Lectures):0.00%})");

            //timers
            stopwatch.Stop();
            tighttimer.Stop();
            totalseconds     = (int)stopwatch.Elapsed.TotalSeconds;
            roomtightseconds = (int)tighttimer.Elapsed.TotalSeconds;

            return(_solution);
        }
コード例 #7
0
        public Solution Run()
        {
            var stopwatch = Stopwatch.StartNew();

            var model = new CCTModel(data, formulation, MIPmodelParameters);

            model.Optimize(TimelimitStageI - Timetotighten);
            var userooms = model.GetUsedRooms();

            Console.WriteLine($"Used rooms: {userooms.Sum(kv => kv.Value)} over {userooms.Count} ");
            Console.WriteLine($"OveruseOnTimeslots: {model.GetOverUseOfRooms()} ");

            //  model.PenalizeRoomUsedMoreThanOnce(100);
            model.PenalizeRoomUsedMoreThanOnce(10, true);

            model.SetObjective(0, 0, 0.01);
            //model.SetObjective(0, 0, 0);
            //model.SetMipHintToCurrent();
            model.SetProximityOrigin();
            model.SetQualConstraint(model.ObjSoftCons);

            model.Fixsol(true);
            model.Optimize();
            model.Fixsol(false);

            //    model.Optimize(timetotighten+50,cutoff:500);
            model.ModelParameters.AbortSolverOnZeroPenalty = true;
            var tighttimer = Stopwatch.StartNew();

            model.Optimize(Timetotighten + 300);
            tighttimer.Stop();
            model.DisplayObjectives();
            _solution = new Solution(data, formulation);
            _solution.SetAssignments(model.GetAssignments());

            //timers
            stopwatch.Stop();
            tighttimer.Stop();
            totalseconds     = (int)stopwatch.Elapsed.TotalSeconds;
            roomtightseconds = (int)tighttimer.Elapsed.TotalSeconds;
            return(_solution);
        }
コード例 #8
0
ファイル: BasicTests.cs プロジェクト: lulzzz/UniTimetabling
        public void TestSolve()
        {
            var data        = Data.ReadXml(dataPath + ITC_Comp04, "120", "4");
            var formulation = ProblemFormulation.UD2;

            formulation.StudentMinMaxLoadWeight = 1;
            formulation.RoomStabilityWeight     = 0;
            formulation.OverbookingAllowed      = 0.5;
            var model = new CCTModel(data, formulation);

            //model.WriteModel(@"c:\temp\model.lp");
            Console.WriteLine("Solving");
            model.Optimize(300, 0.40);
            // model.DisplaySoftConstraintViolations();
            var sol = new Solution(data, formulation);

            sol.SetAssignments(model.GetAssignments());
            Console.WriteLine(sol.AnalyzeSolution());
            sol.Save(@"c:\temp\solution.ctt");
        }
コード例 #9
0
        public Solution RunUnavailbility()
        {
            var stopwatch = Stopwatch.StartNew();

            var model = new CCTModel(data, formulation, MIPmodelParameters);

            model.Optimize(TimelimitStageI - Timetotighten);
            Console.WriteLine($"Unavailableused: {model.GetUnavailableused()} ({(double) model.GetUnavailableused()/data.Courses.Sum(c => c.Lectures):0.00%})");

            model.PenalizeUnavailability(10);

            // model.SetObjective(0, 0, 0.01);
            // model.SetObjective(0, 0, 0);
            //model.SetMipHintToCurrent();
            model.SetProximityOrigin();
            // model.SetQualConstraint(model.ObjSoftCons);

            model.Fixsol(true);
            model.Optimize();
            model.Fixsol(false);

            //    model.Optimize(timetotighten+50,cutoff:500);
            //     model.ModelParameters.AbortSolverOnZeroPenalty = true;
            var tighttimer = Stopwatch.StartNew();

            model.Optimize(Timetotighten + 900);
            tighttimer.Stop();
            model.DisplayObjectives();
            _solution = new Solution(data, formulation);
            _solution.SetAssignments(model.GetAssignments());

            Console.WriteLine($"Unavailableused: {model.GetUnavailableused()} ({(double)model.GetUnavailableused() / data.Courses.Sum(c => c.Lectures):0.00%})");

            //timers
            stopwatch.Stop();
            tighttimer.Stop();
            totalseconds     = (int)stopwatch.Elapsed.TotalSeconds;
            roomtightseconds = (int)tighttimer.Elapsed.TotalSeconds;

            return(_solution);
        }
コード例 #10
0
ファイル: BasicTests.cs プロジェクト: lulzzz/UniTimetabling
        public void TestSolveComp01ToOptimalityUD2()
        {
            var data        = Data.ReadXml(dataPath + ITC_Comp01, "120", "4");
            var formulation = ProblemFormulation.UD2;



            var model = new CCTModel(data, formulation);

            Console.WriteLine("Solving");
            model.WriteModel(@"c:\temp\model.lp");

            model.Optimize();
            model.WriteModel(@"c:\temp\solution.sol");
            var sol = new Solution(data, formulation);

            sol.SetAssignments(model.GetAssignments());
            Console.WriteLine(sol.AnalyzeSolution());
            Assert.IsTrue(sol.IsFeasible, "solution should be feasible");
            Assert.AreEqual(5, sol.Objective);
        }
コード例 #11
0
        public List <Tuple <int, Solution, int> > Run()
        {
            var before = (int)solutionBefore.Objective;

            model.SetObjective(0, 0, 1);
            //model.Fixsol(false);
            //model.SetProxConstraint(10);
            var feasible = model.Optimize(Timelimit);

            if (!feasible)
            {
                return(new List <Tuple <int, Solution, int> >());
            }
            var minperb = (int)model.Objective;

            //Find best objective
            model.SetObjective(1, 0, 0);
            var sol = new List <Tuple <int, Solution, int> >();
            var currentObjective = 0;

            for (var pertubations = minperb; pertubations <= minperb + ExtraPerubations && pertubations <= MaxTotalPertubations; pertubations++)
            {
                Console.WriteLine($"pertubations = {pertubations}");

                model.SetProxConstraint(pertubations);
                if (PerturbationObjectEqual)
                {
                    model.SetProcConstraintSenseEqual(true);
                }
                // if (RemoveCurrentSolutionAfteritt) model.b
                //model.Optimize();
                int sols;
                for (sols = 0; sols < SolPerPertubation; sols++)
                {
                    if (!model.Optimize(Timelimit))
                    {
                        if (AbortSolverWhenNoImprovement)
                        {
                            break;                                           //infeasibles
                        }
                        else
                        {
                            continue;
                        }
                    }

                    Console.WriteLine("new solution");
                    //constraint on objective function


                    // if (model.ObjSoftCons == prev) break;
                    currentObjective = model.ObjSoftCons;
                    model.SetQualConstraint(currentObjective);
                    var solututionAfter = new Solution(data, formulation);
                    solututionAfter.SetAssignments(model.GetAssignments());
                    Console.WriteLine($"Before: {before}\n" +
                                      $"After: {model.ObjSoftCons}\n" +
                                      $"Perbs: {pertubations}");
                    Console.WriteLine(solutionBefore.AnalyzeSolution());
                    Console.WriteLine(solututionAfter.AnalyzeSolution());
                    var solution = new Solution(data, formulation);
                    solution.SetAssignments(model.GetAssignments());
                    solution.AnalyzeSolution();
                    sol.Add(Tuple.Create(pertubations, solution, model.RunTime));
                    DisplayPertubations(solutionBefore._assignments.ToList(), solututionAfter._assignments.ToList());
                    //to pertubate more.
                    model.RemoveCurrentSolution();
                }
                if (sols == 0)
                {
                    break;                //previous pertubations didnt find anything
                }
                model.SetQualConstraint(currentObjective - 1);
            }
            return(sol);
        }
コード例 #12
0
        public List <Tuple <int, Solution, int, int> > Run()
        {
            var starttime = DateTime.Now;
            var before    = (int)solutionBefore.Objective;

            model.SetObjective(0, 0, 1);
            //model.Fixsol(false);
            //model.SetProxConstraint(10);
            var feasible = model.Optimize(Timelimit);

            if (!feasible)
            {
                return(new List <Tuple <int, Solution, int, int> >());
            }
            var minperb = (int)model.Objective;

            //Find best objective
            model.SetObjective(1, 0, 0);
            var sol = new List <Tuple <int, Solution, int, int> >();
            var currentObjective = 0;
            var maxPerturbations = Math.Max(minperb + ExtraPerubations, MaxTotalPertubations);

            for (var pertubations = minperb; pertubations <= maxPerturbations; pertubations++)
            {
                Console.WriteLine($"pertubations = {pertubations}");

                model.SetProxConstraint(pertubations);
                if (PerturbationObjectEqual)
                {
                    model.SetProcConstraintSenseEqual(true);
                }
                // if (RemoveCurrentSolutionAfteritt) model.b
                //model.Optimize();
                int sols;
                for (sols = 0; sols < SolPerPertubation; sols++)
                {
                    var timeleft = TotalTimelimit - (int)(DateTime.Now - starttime).TotalSeconds;
                    if (!model.Optimize(timeleft))
                    {
                        if (AbortSolverWhenNoImprovement)
                        {
                            break;                                           //infeasibles
                        }
                        else
                        {
                            continue;
                        }
                    }

                    Console.WriteLine("new solution");
                    //constraint on objective function


                    // if (model.ObjSoftCons == prev) break;
                    currentObjective = model.ObjSoftCons;
                    model.SetQualConstraint(currentObjective);
                    var solututionAfter = new Solution(data, formulation);
                    solututionAfter.SetAssignments(model.GetAssignments());
                    Console.WriteLine($"Before: {before}\n" +
                                      $"After: {model.ObjSoftCons}\n" +
                                      $"Perbs: {pertubations}");
                    Console.WriteLine(solutionBefore.AnalyzeSolution());
                    Console.WriteLine(solututionAfter.AnalyzeSolution());
                    var solution = new Solution(data, formulation);
                    solution.SetAssignments(model.GetAssignments());
                    solution.AnalyzeSolution();
                    sol.Add(Tuple.Create(pertubations, solution, model.RunTime, (int)Math.Ceiling(model.ObjBound)));
                    DisplayPertubations(solutionBefore._assignments.ToList(), solututionAfter._assignments.ToList());
                    //to pertubate more.
                    model.RemoveCurrentSolution();
                }
                if (sols == 0)
                {
                    break;                //previous pertubations didnt find anything
                }
                if (AbortWhenPreviousSolutionValueIsObtained && currentObjective == (int)solutionBefore.Objective)
                {
                    break;
                }
                if ((int)(DateTime.Now - starttime).TotalSeconds > TotalTimelimit)
                {
                    Console.WriteLine("total timelimit reached");
                    break;
                }

                //overall timelimit?
                model.SetQualConstraint(currentObjective - 1);
            }
            LastRuntimeSeconds = (int)(DateTime.Now - starttime).TotalSeconds;
            return(sol);
        }
コード例 #13
0
ファイル: DataAnalysis.cs プロジェクト: lulzzz/UniTimetabling
        public void TopSolutionsAnalysis(string filename)
        {
            var name = Path.GetFileNameWithoutExtension(filename);

            var data             = Data.ReadXml(dataPath + filename, "120", "4");
            var formulation      = ProblemFormulation.UD2NoOverbook;
            var courestime       = new Dictionary <Course, Dictionary <TimeSlot, int> >();
            var penaltyforcourse = new Dictionary <Course, int>();

            foreach (var course in data.Courses)
            {
                courestime.Add(course, new Dictionary <TimeSlot, int>());
                foreach (var timeSlot in data.TimeSlots)
                {
                    courestime[course].Add(timeSlot, 0);
                }
                penaltyforcourse[course] = 0;
            }


            var bestsol = new List <Assignment>();
            var penMWD  = new List <int>();
            var penCC   = new List <int>();

            for (var i = 0; i < 10; i++)
            {
                var sol = new Solution(data, formulation);
                sol.Read(dataPath + @"ITC2007\sol\" + name + "-UD2-" + i + ".sol");
                sol.AnalyzeSolution();
                foreach (var assignment in sol._assignments)
                {
                    courestime[assignment.Course][assignment.TimeSlot]++;
                }

                foreach (var course in data.Courses)
                {
                    penaltyforcourse[course] += sol.PenaltyForCourse[course];
                }
                penMWD.Add((int)sol.MinimumWorkingDays);
                penCC.Add((int)sol.CurriculumCompactness);

                if (i == 0)
                {
                    bestsol = sol._assignments.ToList();
                }
            }

            const int minhint = 7;
            var       maxhint = 7;
            var       noise   = 0.5;

            const bool fixhints = true;

            var random = new Random(62);

            foreach (var course in data.Courses)
            {
                foreach (var ts in data.TimeSlots)
                {
                    //     if (random.Next(10) < courestime[course][ts]) courestime[course][ts] = 1;
                    //    else courestime[course][ts] = 0;

                    if (courestime[course][ts] < minhint)
                    {
                        courestime[course][ts] = 0;
                    }
                    if (courestime[course][ts] > maxhint)
                    {
                        courestime[course][ts] = 0;
                    }
                    //result should be made to double.
                    // if(courestime[course][ts] > 0) courestime[course][ts] +=  random.Next(-3,3);
                    //courestime[course][ts] *= 1 + (int) Math.Round(noise*(2*random.NextDouble()-1));
                }
            }

            var hints    = courestime.SelectMany(c => c.Value.Where(cv => cv.Value > 0).Select(cv => Tuple.Create(c.Key, cv.Key, cv.Value))).ToList();
            var currtime = new Dictionary <Curriculum, Dictionary <TimeSlot, int> >();

            foreach (var curr in data.Curricula)
            {
                currtime.Add(curr, new Dictionary <TimeSlot, int>());
                foreach (var timeSlot in data.TimeSlots)
                {
                    currtime[curr].Add(timeSlot, courestime.Where(c => curr.Courses.Contains(c.Key)).Sum(kv => kv.Value[timeSlot]));
                }
            }
            var chints = currtime.SelectMany(c => c.Value.Select(cv => Tuple.Create(c.Key, cv.Key, cv.Value))).ToList();


            Console.WriteLine($"Timeslots: {data.TimeSlots.Count}");
            Console.WriteLine($"MWD: avg: {penMWD.Average()} - {string.Join(", ", penMWD)} ");
            Console.WriteLine($"CC: avg: {penCC.Average()} - {string.Join(", ", penCC)} ");
            foreach (var course in data.Courses.OrderByDescending(c => penaltyforcourse[c]))
            {
                Console.WriteLine($"\nCourse: {course}\n" +
                                  $"Size: {course.NumberOfStudents}\n" +
                                  $"Lectures: {course.Lectures}\n" +
                                  $"MWD: {course.MinimumWorkingDays}\n" +
                                  $"TSAvaialble:{data.TimeSlots.Count - course.UnavailableTimeSlots.Count}\n" +
                                  $"Unavail: {string.Join(",", course.UnavailableTimeSlots)}\n" +
                                  $"TotalPenalty: {penaltyforcourse[course]}");
                foreach (var valuePair in courestime[course].Where(kv => kv.Value > 0).OrderByDescending(kv => kv.Value))
                {
                    Console.WriteLine(valuePair.Key + ": " + valuePair.Value);
                }
            }

            Console.WriteLine("\n\nBy prio: ");
            foreach (var hint in hints.Where(h => h.Item3 > 0).OrderByDescending(h => h.Item3))
            {
                Console.WriteLine(hint.Item1.ID + "_" + hint.Item2 + ": " + hint.Item3);
            }

            Console.WriteLine("\n\nHistogram: ");
            Console.WriteLine("total lectures: " + data.Courses.Sum(c => c.Lectures));
            for (var i = 1; i <= 10; i++)
            {
                Console.WriteLine($"{i}\t{hints.Count(h => h.Item3 == i)}");
            }

            Console.WriteLine("Unavailabilites\tSame");
            foreach (var course in data.Courses)
            {
                Console.WriteLine($"{course.UnavailableTimeSlots.Count}\t{(double) courestime[course].Sum(t => t.Value)/course.Lectures : 0.00}");
            }
            Console.WriteLine("Penal\tSame");
            foreach (var course in data.Courses)
            {
                Console.WriteLine($"{penaltyforcourse[course]}\t{(double) courestime[course].Sum(t => t.Value)/course.Lectures : 0.00}");
            }
            Console.WriteLine("Size\tSame");
            foreach (var course in data.Courses)
            {
                Console.WriteLine($"{course.NumberOfStudents}\t{(double) courestime[course].Sum(t => t.Value)/course.Lectures : 0.00}");
            }
            // return;
            //Console.WriteLine("\n\nCurriculumPatterns");

            //foreach (var hint in chints.Where(h => h.Item3 > 0).OrderByDescending(h => h.Item3))
            //{
            //    Console.WriteLine(hint.Item1.ID + "_" + hint.Item2 + ": " + hint.Item3);
            //}
            //Console.WriteLine("\n\nHistogram: ");
            //Console.WriteLine("total curr_lectures: " + data.Curricula.Sum(c => c.Lectures));
            //for (var i = 1; i <= 10; i++)
            //{
            //    Console.WriteLine($"{i}\t{chints.Count(h => h.Item3 == i)}");

            //}


            var ModelParameters = new CCTModel.MIPModelParameters()
            {
                UseStageIandII        = false,
                TuneGurobi            = true,
                UseHallsConditions    = true,
                UseRoomHallConditions = true,
                //  Seed = seeds[i],
            };

            var model = new CCTModel(data, formulation, ModelParameters);

            // model.SetMipHints(chints);
            Console.WriteLine($"Adding {hints.Count} hints. Total letures: {data.Courses.Sum(c => c.Lectures)} ({(double) hints.Count / data.Courses.Sum(c => c.Lectures) :0.00%})");
            model.SetMipHints(hints, fixhints);
            // model.MipStart(bestsol);
            var r = model.Optimize(300);
            // model.Optimize(60*60*3);

            var ass      = model.GetAssignments();
            var finalsol = new Solution(data, formulation);

            finalsol.SetAssignments(ass);
            finalsol.Save(@"C:\temp\finalsolStageI_" + name + ".sol");
            var ModelParameters2 = new CCTModel.MIPModelParameters()
            {
                UseStageIandII        = true,
                TuneGurobi            = true,
                UseHallsConditions    = true,
                UseRoomHallConditions = true,
                //  Seed = seeds[i],
            };

            var model2 = new CCTModel(data, formulation, ModelParameters2);

            model2.SetAndFixSolution(ass);
            model2.Optimize(300);
            ass = model2.GetAssignments();
            finalsol.SetAssignments(ass);
            finalsol.Save(@"C:\temp\finalsolStageII_" + name + ".sol");
        }
コード例 #14
0
        public void MultiObjectiveRoomStabilityFixedTime(string filename)
        {
            var algoname = nameof(MultiObjectiveRoomStabilityFixedTime);

            if (ConsoleToFile)
            {
                SetConsoleOutputToFile(@"c:\temp\multiobjective\\logs\\" + filename + "\\" + algoname + ".log");
            }


            var data = Data.ReadXml(dataPath + filename, "120", "4");

            var newrooms = CreateRoomsFixedSize(data, 25, 3);

            data.SetRoomList(newrooms);

            var formulation = ProblemFormulation.MinimizeRoomCost;
            // formulation.OverbookingAllowed = 0.25;

            var startmodel = new CCTModel(data, formulation, new CCTModel.MIPModelParameters()
            {
                UseStageIandII        = false,
                TuneGurobi            = true,
                UseHallsConditions    = true,
                UseRoomHallConditions = true,
            });

            startmodel.Optimize(300);
            var fixedTimeSlotAssignments = startmodel.GetAssignments();

            var stageIsol = new Solution(data, formulation);

            stageIsol.SetAssignments(fixedTimeSlotAssignments);
            Console.WriteLine("Assignments of timeslots:");
            Console.WriteLine(stageIsol.AnalyzeSolution());

            //  var newrooms = CreateRooms(data,5);
            // var newrooms = CreateRoomsFixedSize(data, 20);fr


            var problemFormulation = ProblemFormulation.EverythingZero;

            problemFormulation.RoomStabilityWeight = 1;
            //problemFormulation.OverbookingAllowed = 1;
            var mipModelParameters = new CCTModel.MIPModelParameters()
            {
                TuneGurobi     = true,
                UseStageIandII = true,
                // UseStageIandII = true,
                UseHallsConditions    = false,
                UseRoomHallConditions = false,
                NSols             = 100,
                ConstraintPenalty = 50,
                UseRoomsAsTypes   = false,
            };
            var solver = new MultiObjectiveSolver(data, problemFormulation, mipModelParameters)
            {
                Timelimit                = Timelimit,
                MIPGap                   = 0.0,
                Steps                    = 10,
                LocalBranching           = 0,
                EpsilonOnQuality         = false,
                FixedTimeSlotAssignments = fixedTimeSlotAssignments
            };

            Console.WriteLine("Algorithm: " + algoname);
            DisplayParameterObject(problemFormulation);
            DisplayParameterObject(solver);
            DisplayParameterObject(solver.MipParameters);

            var pareto = solver.Run();

            WriteSol(@"c:\temp\multiobjective\", filename, data, problemFormulation, algoname, DateTime.Now.ToString(), pareto);
        }
コード例 #15
0
        public void Lex_soft_cost_withTimeslot(string filename)
        {
            var algname = nameof(Lex_soft_cost_withTimeslot);
            //  formulation.AvailabilityHardConstraint = false;
            var data     = Data.ReadXml(dataPath + filename, "120", "4");
            var newrooms = CreateRoomsFixedSize(data, 25, 5);

            data.SetRoomList(newrooms);

            var par = new CCTModel.MIPModelParameters()
            {
                UseStageIandII        = false,
                UseHallsConditions    = true,
                UseRoomHallConditions = true,
                TuneGurobi            = false,
                UseRoomsAsTypes       = false,
            };
            var costmodel = new CCTModel(data, ProblemFormulation.MinimizeRoomCost, par);

            costmodel.Optimize();


            data.SetRoomList(costmodel.GetUsedRooms().Where(kv => kv.Value > 0).Select(kv => kv.Key).ToList());


            //var formulation = ProblemFormulation.UD2NoOverbook;
            // formulation.AvailabilityHardConstraint = false;
            //   formulation.OverbookingAllowed = 0.35;
            var ModelParameters = new CCTModel.MIPModelParameters()
            {
                UseStageIandII        = false,
                TuneGurobi            = true,
                UseHallsConditions    = true,
                UseRoomHallConditions = true,
                // UseRoomsAsTypes = true,
                UseRoomsAsTypes          = false,
                RoomBoundForEachTimeSlot = true,
            };
            var model = new CCTModel(data, _problemFormulation, ModelParameters);

            model.Optimize(300);
            var userooms = model.GetUsedRooms();

            Console.WriteLine($"Used rooms: {userooms.Sum(kv => kv.Value)} over {userooms.Count} ");

            // model.PenalizeRoomUsedMoreThanOnce(100);
            model.PenalizeRoomUsedMoreThanOnce(1, true);
            model.SetObjective(0, 0, 0.001);
            model.SetProximityOrigin();
            model.SetQualConstraint(model.ObjSoftCons);
            // model.SetProxConstraint(20);
            model.Fixsol(true);
            model.Optimize();
            model.Fixsol(false);


            model.Optimize(900);
            model.DisplayObjectives();
            var sol = new Solution(data, ProblemFormulation.MinimizeRoomCost);

            sol.SetAssignments(model.GetAssignments());
            Console.WriteLine(sol.AnalyzeSolution());

            File.AppendAllText(@"c:\temp\heuristic.txt", $"{algname};{filename};{model.ObjSoftCons}\n");
        }