private void RunEpsilonMethod(int minSoftConsViol, int maxSoftConsViol, double minCost, double maxCost) { List <MultiResult> pareto; var stepsize = (double)1 / (Steps); //(double)1 / (Steps + 1); var proxobj = 0; if (EpsilonOnQuality) { _model.SetObjective(0, 1, proxobj); } else { _model.SetObjective(1, 0, proxobj); } if (LocalBranching > 0) { _model.SetProxConstraint(LocalBranching); } for (var alpha = EpsilonRelaxing ? stepsize : 1 - stepsize; alpha >= 0 && alpha <= 1; //maybe we maybe should solve the last one again.. this seems to give improvementts. alpha += (EpsilonRelaxing ? 1 : -1) * stepsize) { Console.WriteLine($"Elapsed time: {_stopwatch.Elapsed} -- {_stopwatch.ElapsedMilliseconds}ms"); OptimizeSubProblem(minSoftConsViol, maxSoftConsViol, minCost, maxCost, alpha); } if (DoubleSweep) { pareto = GetParetoPoints(_multiResults); var beforepoints = pareto.Count; DisplaySolutions(pareto); //Check if new corner points minCost = pareto.Min(r => r.Cost); maxCost = pareto.Max(r => r.Cost); minSoftConsViol = pareto.Min(r => r.SoftObjective); maxSoftConsViol = pareto.Max(r => r.SoftObjective); Console.WriteLine("Running opposite direction"); if (LocalBranching > 0) { _model.SetProxConstraint(LocalBranching); } for (var alpha = !EpsilonRelaxing ? stepsize : 1 - stepsize; alpha > 0 && alpha < 1; //maybe we maybe should solve the last one again.. this seems to give improvementts. alpha += (!EpsilonRelaxing ? 1 : -1) * stepsize) { Console.WriteLine($"Elapsed time: {_stopwatch.Elapsed} -- {_stopwatch.ElapsedMilliseconds}ms"); OptimizeSubProblem(minSoftConsViol, maxSoftConsViol, minCost, maxCost, alpha); } pareto = GetParetoPoints(_multiResults); Console.WriteLine($"Done with oposite. New points added: {pareto.Count - beforepoints}"); } }
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); } }
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); }
// [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 } }