Ejemplo n.º 1
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);
        }