Ejemplo n.º 1
0
        public void TestCurriculaConstraint()
        {
            var data = Data.ReadXml(dataPath + ITC_Comp05, "120", "4");
            // var formulation = ProblemFormulation.UD2;
            //Formulation.AvailabilityHardConstraint = false;
            //Formulation.OverbookingAllowed = 0.35;
            var ModelParameters = new CCTModel.MIPModelParameters()
            {
                UseStageIandII           = false,
                ConstraintPenalty        = 50,
                AbortSolverOnZeroPenalty = true,
            };
            var model = new CCTModel(data, formulation, ModelParameters);
            var watch = Stopwatch.StartNew();

            model.Optimize(30, 0.99);
            var obj = model.ObjSoftCons;

            while (watch.Elapsed.TotalSeconds < TimelimitStageI * 2)
            {
                model.SetQualConstraint(obj - 5);
                model.Optimize(120);
                obj = model.ObjSoftCons;
                Console.WriteLine($"Objective: {obj}");
            }
        }
Ejemplo n.º 2
0
        public void TestObjective()
        {
            var data        = Data.ReadXml(dataPath + ITC_Comp01, "120", "4");
            var formulation = ProblemFormulation.UD2;
            //Formulation.AvailabilityHardConstraint = false;
            //Formulation.OverbookingAllowed = 0.35;
            var model = new CCTModel(data, formulation);

            model.Optimize(900);
            model.SetObjective(0.95, 0.5);
            model.Optimize(60);
            model.SetObjective(0.5, 0.5);
            model.Optimize(60);
            model.SetObjective(0.2, 0.8);
        }
Ejemplo n.º 3
0
        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}")));
        }
        public void ThresholdBehvaiour(string filename)
        {
            var data        = Data.ReadXml(dataPath + filename, "120", "4");
            var formulation = ProblemFormulation.UD2NoOverbook;
            var newrooms    = CreateRoomsFixedSize(data, 25, 1);

            data.SetRoomList(newrooms);

            var model = new CCTModel(data, formulation, new CCTModel.MIPModelParameters()
            {
                UseRoomsAsTypes = true,
                UseStageIandII  = false,
                //   TuneGurobi = true,
                //TuneGurobi = false,
                SaveBoundInformation = true,
                //FocusOnlyOnBounds = true,
            });
            var seatscal = new RoomCombinations(data, 25);

            var CriticalSeats = seatscal._minCost;

            // model.SetObjective(0, 1, 0);
            //model.Optimize();
            // model.SetObjective(8, 1, 0); // Weighted sum
            model.SetObjective(1, 0, 0);
            model.SetBudgetConstraint(CriticalSeats);
            model.Optimize(300);


            Console.WriteLine();
            Console.WriteLine("time;MWD;CC;softobj;obj;cost;bnd;");
            Console.WriteLine(String.Join("\n",
                                          model.bounds.Select(t => $"{t.Item6};{t.Item1};{t.Item2};{t.Item1+t.Item2};{t.Item3};{t.Item5};{t.Item4}")));
            Console.WriteLine($"Last bound: {model.ObjBound}");
        }
Ejemplo n.º 5
0
        public void Relaxunavailability(string filename)
        {
            var algname = nameof(Relaxunavailability);
            var data    = Data.ReadXml(dataPath + filename, "120", "4");

            for (int i = 0; i < 1; i++)
            {
                //var formulation = ProblemFormulation.UD2NoOverbook;
                //Formulation.AvailabilityHardConstraint = false;
                //Formulation.OverbookingAllowed = 0.35;
                var ModelParameters = new CCTModel.MIPModelParameters()
                {
                    UseStageIandII        = false,
                    TuneGurobi            = true,
                    UseHallsConditions    = true,
                    UseRoomHallConditions = true,
                    Seed = seeds[i],
                };
                formulation.AvailabilityHardConstraint = false;
                var model = new CCTModel(data, formulation, ModelParameters);

                var stopwatch = Stopwatch.StartNew();

                model.Optimize(30);
                stopwatch.Stop();
                var objective = (int)Math.Round(model.Objective);
                var dataname  = System.IO.Path.GetFileNameWithoutExtension(filename);

                File.AppendAllText(@"c:\temp\heuristic.txt",
                                   $"{algname};{dataname};{DateTime.Now};{i};{objective};{(int) stopwatch.Elapsed.TotalSeconds};\n");
            }
        }
Ejemplo n.º 6
0
        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());
        }
Ejemplo n.º 7
0
        public void TestSolveStageI(string filename)
        {
            var algname = nameof(TestSolveStageI);
            var data    = Data.ReadXml(dataPath + filename, "120", "4");

            for (int i = 0; i < ntests; i++)
            {
                //var formulation = ProblemFormulation.UD2NoOverbook;
                //Formulation.AvailabilityHardConstraint = false;
                //Formulation.OverbookingAllowed = 0.35;
                var ModelParameters = new CCTModel.MIPModelParameters()
                {
                    UseStageIandII        = false,
                    TuneGurobi            = true,
                    UseHallsConditions    = true,
                    UseRoomHallConditions = true,
                    Seed = seeds[i],
                };
                var model = new CCTModel(data, formulation, ModelParameters);

                model.Optimize(TimelimitStageI);
                var objective = (int)Math.Round(model.Objective);

                File.AppendAllText(@"c:\temp\heuristic.txt",
                                   $"{algname};{filename};{DateTime.Now.ToString()};{i};{objective}\n");
            }
        }
Ejemplo n.º 8
0
        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);
        }
        public void TestBudgetRelaxations(string filename, double stepsize)
        {
            var data        = Data.ReadXml(dataPath + filename, "120", "4");
            var formulation = ProblemFormulation.UD2NoOverbook;
            var newrooms    = CreateRoomsFixedSize(data, 25, 1);

            data.SetRoomList(newrooms);


            var model = new CCTModel(data, formulation, new CCTModel.MIPModelParameters()
            {
                UseRoomsAsTypes      = true,
                UseStageIandII       = false,
                TuneGurobi           = true,
                SaveBoundInformation = true,
            });

            model.RelaxModel();

            model.SetObjective(0, 1, 0);
            model.Optimize();
            var min = model.Objective; // Math.Floor(model.Objective*100)/100;

            Console.WriteLine($"Minimum budget for relaxed: {min}");
            model = new CCTModel(data, formulation, new CCTModel.MIPModelParameters()
            {
                UseRoomsAsTypes      = true,
                UseStageIandII       = false,
                TuneGurobi           = true,
                SaveBoundInformation = true,
            });

            //model.WriteModel(@"c:\temp\model.lp");
            var    bounds  = new List <Tuple <double, double, double, double> >();
            double prevobj = double.PositiveInfinity;
            int    lasts   = 5;

            for (double budget = min; budget < min * 2; budget += stepsize)
            {
                model.RelaxModel();
                Console.WriteLine($"Budget:{budget : 0.00} ");
                model.SetBudgetConstraint(budget);
                if (model.Optimize(300))
                {
                    bounds.Add(Tuple.Create(budget, model.Objective, formulation.MinimumWorkingDaysWeight * model.ObjMWD, formulation.CurriculumCompactnessWeight * model.ObjCC)); //Save each objective? MWD CC
                    if (prevobj - model.Objective < 1e-6)
                    {
                        lasts--;
                    }
                    if (lasts == 0)
                    {
                        break;
                    }
                    prevobj = model.Objective;
                }
            }
            Console.WriteLine("Budget;Obj;MWD;CC");
            Console.WriteLine(string.Join("\n", bounds.Select(b => $"{b.Item1 : #.###};{b.Item2: #.###};{b.Item3: #.###};{b.Item4: #.###}")));
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
0
        public void TestQualConstraint()
        {
            var data        = Data.ReadXml(dataPath + ITC_Comp08, "120", "4");
            var formulation = ProblemFormulation.UD2;
            var model       = new CCTModel(data, formulation);

            //model.WriteModel(@"c:\temp\model.lp");
            model.SetQualConstraint(5);
            model.Optimize();
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
0
        public void TestLocalBranchingConstraint(bool usecurr, int N)
        {
            var data = Data.ReadXml(dataPath + ITC_Comp05, "120", "4");
            //var formulation = ProblemFormulation.UD2;
            //Formulation.AvailabilityHardConstraint = false;
            //Formulation.OverbookingAllowed = 0.35;
            var modelParameters = new CCTModel.MIPModelParameters()
            {
                UseStageIandII = false
            };
            var model = new CCTModel(data, formulation, modelParameters);

            model.Optimize(30 * 3, 0.99);
            model.SetProxConstraint(N);
            var watch = Stopwatch.StartNew();

            while (watch.Elapsed.TotalSeconds < TimelimitStageI)
            {
                model.SetProximityOrigin(usecurr);
                model.Optimize(15);
            }
        }
Ejemplo n.º 16
0
        public void TestSolutionread()
        {
            var data        = Data.ReadXml(dataPath + ITC_Comp01, "120", "4");
            var formulation = ProblemFormulation.UD2;
            var sol         = new Solution(data, formulation);

            sol.Read(dataPath + @"ITC2007\sol\comp01-UD2.sol");
            sol.AnalyzeSolution();
            Assert.AreEqual(0, sol.UnscheduledLectures);
            var model = new CCTModel(data, ProblemFormulation.UD2);

            model.SetAndFixSolution(sol._assignments.ToList());
            model.Optimize();
            Assert.AreEqual(5, model.Objective);
        }
Ejemplo n.º 17
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);
            }
        }
Ejemplo n.º 18
0
        public void PenaltiesScaledByCourseSizes(string filename)
        {
            var data = Data.ReadXml(dataPath + filename, "120", "4");
            //var formulation = ProblemFormulation.UD2NoOverbook;
            //Formulation.AvailabilityHardConstraint = false;
            //Formulation.OverbookingAllowed = 0.35;
            var ModelParameters = new CCTModel.MIPModelParameters()
            {
                UseStageIandII             = false,
                TuneGurobi                 = true,
                UseHallsConditions         = true,
                UseRoomHallConditions      = true,
                ScalePenaltyWithCourseSize = true,
            };
            var model = new CCTModel(data, formulation, ModelParameters);

            model.Optimize(TimelimitStageI * 2);
            File.AppendAllText(@"c:\temp\heuristic.txt", $"{nameof(PenaltiesScaledByCourseSizes)};{filename};{model.Objective}\n");
        }
Ejemplo n.º 19
0
        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");
        }
Ejemplo n.º 20
0
        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);
        }
Ejemplo n.º 21
0
        public void TestSolveStageIandII(string filename)
        {
            var data = Data.ReadXml(dataPath + filename, "120", "4");
            //var formulation = ProblemFormulation.UD2NoOverbook;
            //Formulation.AvailabilityHardConstraint = false;
            //Formulation.OverbookingAllowed = 0.35;
            var ModelParameters = new CCTModel.MIPModelParameters()
            {
                UseStageIandII        = false,
                TuneGurobi            = true,
                UseHallsConditions    = true,
                UseRoomHallConditions = true,
            };
            var model = new CCTModel(data, formulation, ModelParameters);

            model.Optimize(TimelimitStageI);

            //todo make test. use fixing as in multi

            File.AppendAllText(@"c:\temp\heuristic.txt", $"{nameof(TestSolveStageI)};{filename};{model.Objective}\n");
        }
Ejemplo n.º 22
0
        public void TestBudgetConstraintWithEnumeration(string filename, int budget, int seed)
        {
            var data        = Data.ReadXml(dataPath + filename, "120", "4");
            var formulation = ProblemFormulation.UD2NoOverbook;

            var comb = new RoomCombinations(data, 25);

            Console.WriteLine($"Minimum room is: {comb._minCost}");
            var newrooms = CreateRoomsFixedSize(data, 25, 1);

            data.SetRoomList(newrooms);


            var model = new CCTModel(data, formulation, new CCTModel.MIPModelParameters()
            {
                UseRoomsAsTypes = true,
                UseStageIandII  = false,
                TuneGurobi      = true,
                Seed            = seed,
            });

            //model.WriteModel(@"c:\temp\model.lp");
            //var budget = comb._minCost+0;


            Console.WriteLine($"Budget: {budget}");

            model.SetPossibleRoomCombinations(budget);
            model.Optimize(900);


            var sol = new Solution(data, formulation);

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

            Console.WriteLine($"Room Cost: {model.GetUsedRooms().Sum(kv => kv.Key.Cost*kv.Value)}");
            File.AppendAllText(@"c:\temp\enumtest.txt", $"{nameof(TestBudgetConstraintWithEnumeration)};{filename};{seed};{model.Objective}\n");
        }
Ejemplo n.º 23
0
        public void MaximizeRoomUtilization(string filename)
        {
            var data = Data.ReadXml(dataPath + filename, "120", "4");

            Console.WriteLine($"Dataset: {data.Name}");
            Console.WriteLine($"Courses: {data.Courses.Count} Lectures: {data.Courses.Sum(c => c.Lectures)}");



            //DisplayRoomStats("original",data.Rooms.ToDictionary(r => r, r => 1), data);

            //  var newrooms = CreateRooms(data);
            var newrooms = CreateRoomsFixedSize(data, 25, 1);

            data.SetRoomList(newrooms);


            Console.WriteLine("\nLectures:\n");
            Console.WriteLine("size,hours");

            foreach (var coursesizes in data.Courses.GroupBy(c => c.NumberOfStudents).OrderBy(c => c.Key))
            {
                Console.WriteLine($"{coursesizes.Key},{coursesizes.Sum(s => s.Lectures)}");
            }

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

            model.Optimize();
            DisplayRoomStats("optimized", model.GetUsedRooms(), data);
        }
Ejemplo n.º 24
0
        public void TestBudgetConstraint(string filename, int budget, int seed)
        {
            var data        = Data.ReadXml(dataPath + filename, "120", "4");
            var formulation = ProblemFormulation.UD2NoOverbook;
            var newrooms    = CreateRoomsFixedSize(data, 25, 1);

            data.SetRoomList(newrooms);


            var model = new CCTModel(data, formulation, new CCTModel.MIPModelParameters()
            {
                UseRoomsAsTypes = true,
                UseStageIandII  = false,
                TuneGurobi      = true,
                Seed            = seed,
            });

            //model.WriteModel(@"c:\temp\model.lp");
            model.SetBudgetConstraint(budget);
            model.Optimize(900);
            Console.WriteLine($"Room Cost: {model.GetUsedRooms().Sum(kv => kv.Key.Cost * kv.Value)}");
            File.AppendAllText(@"c:\temp\enumtest.txt", $"{nameof(TestBudgetConstraint)};{filename};{seed};{model.Objective}\n");
        }
Ejemplo n.º 25
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);
        }
        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);
        }
Ejemplo n.º 27
0
        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");
        }
        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);
        }
Ejemplo n.º 29
0
        public List <Tuple <int, int, int, int> > Run()
        {
            if (formulationtype < 0 || formulationtype > 1)
            {
                throw new ArgumentException("No formulationtype set");
            }
            var stopwatch = Stopwatch.StartNew();
            var solutions = new List <Tuple <int, int, int, int> >();


            //  var newrooms = CreateRoomsFixedSize(data, 25, 1);
            //  data.SetRoomList(newrooms);

            _model = new CCTModel(data, formulation, MIPmodelParameters);

            _model.Optimize(Timelimit);
            var minobj = (int)Math.Round(_model.Objective);

            solutions.Add(Tuple.Create(data.TimeSlots.Count, (int)Math.Round(_model.Objective),
                                       (int)Math.Ceiling(_model.ObjBound), (int)stopwatch.Elapsed.TotalSeconds));
            //Minimize timeslots
            int maxtimeslots = 0;

            if (formulationtype == 0)
            {
                _model.SetQualConstraint(minobj);
                _model.SetObjective(0, 0, totaltimeslots: 1);
                _model.Optimize(Timelimit);
                _model.SetQualConstraint();
                maxtimeslots = (int)Math.Round(_model.Objective);
                _model.SetObjective(1, 0);
            }
            else if (formulationtype == 1)
            {
                _model.SetBudgetConstraint(minobj);
                _model.SetObjective(0, 0, totaltimeslots: 1);
                _model.Optimize(Timelimit);
                _model.SetBudgetConstraint();
                maxtimeslots = (int)Math.Round(_model.Objective);
                _model.SetObjective(0, 1);
            }


            for (int nincluded = 0; nincluded < data.TimeSlots.Count; nincluded++)
            {
                if (nincluded > maxtimeslots)
                {
                    break;
                }

                Console.WriteLine($"Nincluded: {nincluded} ({(double)nincluded / data.TimeSlots.Count: 0.00%})");

                _model.SetTimeslotsUsedConstraint(nincluded);
                var sol = _model.Optimize(Timelimit);
                if (sol)
                {
                    solutions.Add(Tuple.Create(nincluded, (int)Math.Round(_model.Objective), (int)Math.Ceiling(_model.ObjBound),
                                               (int)stopwatch.Elapsed.TotalSeconds));
                    if ((int)Math.Round(_model.Objective) <= minobj)
                    {
                        break;
                    }
                }
            }
            //Tighten bounds.
            for (int i = 0; i < solutions.Count; i++)
            {
                var item3 = solutions.Where(s => s.Item1 >= solutions[i].Item1).Max(s => s.Item3);
                solutions[i] = Tuple.Create(solutions[i].Item1, solutions[i].Item2, item3, solutions[i].Item4);
            }


            return(solutions);
        }
Ejemplo n.º 30
0
        //   [TestCase(ITC_Comp05,1)]

        /*
         * [TestCase(ITC_Comp05, 2)]
         * [TestCase(ITC_Comp05, 3)]
         * [TestCase(ITC_Comp05, 4)]
         * [TestCase(ITC_Comp05, 5)]
         * [TestCase(ITC_Comp05, 6)]
         * [TestCase(ITC_Comp05, 7)]
         * [TestCase(ITC_Comp05, 8)]
         * [TestCase(ITC_Comp05, 11)]
         * [TestCase(ITC_Comp05, 12)]
         * [TestCase(ITC_Comp05, 13)]
         * [TestCase(ITC_Comp05, 14)]
         * [TestCase(ITC_Comp05, 15)]
         * [TestCase(ITC_Comp05, 16)]
         * [TestCase(ITC_Comp05, 17)]
         */
        public void CCfirstHeuristic30s(string filename, int seed) //
        {
            //  var seed = 0;
            for (int ia = 1; ia < 5; ia++)
            {
                seed = ia;

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

                var problemFormulation = ProblemFormulation.UD2NoOverbook;
                problemFormulation.MinimumWorkingDaysWeight = 0;
                //    problemFormulation.CurriculumCompactnessWeight = 0;
                var par = new CCTModel.MIPModelParameters()
                {
                    TuneGurobi            = true,
                    UseStageIandII        = false,
                    UseHallsConditions    = true,
                    UseRoomHallConditions = true,
                    UseRoomsAsTypes       = false,
                    Seed = seed,
                };
                var model = new CCTModel(data, problemFormulation, par);
                model.SetObjective(1, 0, 0);
                model.Optimize(TimelimitStageI);
                model.DisplayObjectives();
                var result = new Dictionary <double, double>();
                //maybe find better proximity that reflects currciulumcompactness better.
                //Check which curriculum that can be moved without introducting penalties.
                model.SetProximityOrigin(true);
                problemFormulation.MinimumWorkingDaysWeight = 5;
                for (var i = 15; i <= 15; i++)
                {
//                model.SetObjective(0, 0, i);
                    model.SetProxConstraint(i);
                    model.AddMinimumWorkingDaysCost();
                    // model.FixCurricula(true);
                    model.Optimize();
                    model.DisplayObjectives();
                    model.SetObjective(1, 0, 0);
                    model.Fixsol(true);
                    model.Optimize();
                    model.Fixsol(false);
                    model.DisplayObjectives();
                    result[i] = model.Objective;
                }

                Console.WriteLine("Results:");
                Console.WriteLine("i;obj");
                foreach (var r in result)
                {
                    Console.WriteLine($"{r.Key};{r.Value}");
                }

                var best = result.Min(r => r.Value);

                File.AppendAllText(@"c:\temp\heuristic.txt", $"{nameof(CCfirstHeuristic30s)};{filename};{best}\n");
                //  model.FixCurricula(false);

                // maybe also save multiple minimum curriculum solutions
            }
        }