Esempio n. 1
0
        //Apllica l'algoritmo Simulated Annealing per ottenere un'ipotetica soluzione migliore
        public static Plan SA(Instance instance, Plan best_plan, double t_max, int max_it)
        {
            Console.WriteLine("--------------------------------");
            Console.WriteLine("SIMULATED ANNEALING");

            double best_obj = best_plan.QualityPlan().tot_rank;
            double t0       = best_obj * t_max;
            double tf       = t0 * t_max;
            double t        = t0;
            double c        = Math.Pow(tf / t0, 1 / (double)max_it);

            Plan current_plan = Plan.Copy(best_plan);

            for (int i = 0; i < max_it; i++)
            {
                Plan neighbor_plan = Plan.Copy(current_plan);
                neighbor_plan = Heuristics.Ruin(instance, neighbor_plan, k_ruin);
                neighbor_plan = Heuristics.Recreate(instance, neighbor_plan, noise);
                List <Plan> ps = Heuristics.CompareSA(best_plan, neighbor_plan, current_plan, t);

                best_plan    = (ps[0] != null) ? Plan.Copy(ps[0]) : best_plan;
                current_plan = (ps[1] != null) ? Plan.Copy(ps[1]) : current_plan;

                t *= c;
            }
            return(best_plan);
        }
Esempio n. 2
0
        public Plan SA(Instance inst, int k, int noise, double temp = 0.01, int max_it = 100)
        {
            Plan   best_plan = Heuristics.CreateInitialPlan(inst, noise, max_it);
            double best_obj  = best_plan.QualityPlan().tot_rank;
            double t0        = best_obj * temp;
            double tf        = t0 * temp;
            double t         = t0;
            double c         = Math.Pow(tf / t0, 1 / (double)max_it);

            Plan current_plan = Plan.Copy(best_plan);

            for (int i = 0; i < max_it; i++)
            {
                Plan neighbor_plan = Plan.Copy(current_plan);
                neighbor_plan = Heuristics.Ruin(inst, neighbor_plan, k);
                neighbor_plan = Heuristics.Recreate(inst, neighbor_plan, noise);
                List <Plan> ps = Heuristics.CompareSA(best_plan, neighbor_plan, current_plan, t);

                best_plan    = (ps[0] != null) ? Plan.Copy(ps[0]) : best_plan;
                current_plan = (ps[1] != null) ? Plan.Copy(ps[1]) : current_plan;

                t *= c;
            }
            return(best_plan);
        }