Example #1
0
        public Solution minimize(ProblemInstance instance, Evaluator evaluator)
        {
            Solution solution = new Solution(instance.m);
            Cost     cost     = evaluator.evaluate(instance, solution);

            int current_worker = 0;

            for (int i = 0; i < instance.n; i++)
            {
                current_worker = MathUtils.argmin(cost.costs);
                solution.job_ids[current_worker].Add(i);
                cost = evaluator.evaluate(instance, solution);
            }
            solution.tag = "贪心算法";
            return(solution);
        }
Example #2
0
        public Cost evaluate(ProblemInstance instance, Solution solution)
        {
            Cost cost = new Cost();

            cost.costs     = new double[instance.m];
            cost.totalCost = 0;

            for (int i = 0; i < instance.m; i++)
            {
                cost.costs[i]   = getDistanceOfWorker(i, instance, solution);
                cost.totalCost += cost.costs[i];
            }

            cost.tag = "总路径长度";
            return(cost);
        }
Example #3
0
        public double getDistanceOfWorker(int workerID, ProblemInstance instance, Solution solution)
        {
            List <int> job_sequence = solution.job_ids[workerID];

            if (job_sequence.Count == 0)
            {
                return(0);
            }
            double sum = distanceToZero(instance.job_details[job_sequence[0]]);

            for (int i = 1; i < job_sequence.Count; i++)
            {
                sum += distance(instance.job_details[job_sequence[i - 1]], instance.job_details[job_sequence[i]]);
            }
            sum += distanceToZero(instance.job_details[job_sequence[job_sequence.Count - 1]]);
            return(sum);
        }
Example #4
0
            public Solution getSolution(ProblemInstance problem)
            {
                int      machine_id = 0;
                Solution solution   = new Solution(problem.m);

                for (int i = 0; i < data.Length; i++)
                {
                    if (data[i] == -1)
                    {
                        machine_id++;
                    }
                    else
                    {
                        solution.job_ids[machine_id].Add(data[i]);
                    }
                }
                return(solution);
            }
        public static ProblemInstance getCustomProblemInstance()
        {
            ProblemInstance problemInstance = new ProblemInstance();

            problemInstance.m = 2;      //有几个骑士
            problemInstance.n = 5;      //有几个地理坐标

            problemInstance.job_details = new JobDetails[problemInstance.n];

            //因为n = 5,所以下面有5个坐标,最好在[-50, 50]之间,保证显示效果
            int pointer = 0;

            problemInstance.job_details[pointer++] = new JobDetails(10, 10);
            problemInstance.job_details[pointer++] = new JobDetails(8, 10);
            problemInstance.job_details[pointer++] = new JobDetails(6, 10);
            problemInstance.job_details[pointer++] = new JobDetails(4, 10);
            problemInstance.job_details[pointer++] = new JobDetails(2, 10);

            return(problemInstance);
        }
Example #6
0
        public static Solution getRandomSolution(ProblemInstance problem)
        {
            Solution   solution  = new Solution(problem.m);
            List <int> order_ids = new List <int>(problem.n);

            for (int i = 0; i < problem.n; i++)
            {
                order_ids.Add(i);
            }

            int selected_id = 0;

            for (int i = 0; i < problem.n; i++)
            {
                selected_id = order_ids[MathUtils.u(0, order_ids.Count)];
                order_ids.Remove(selected_id);
                solution.job_ids[MathUtils.u(0, problem.m)].Add(selected_id);
            }
            return(solution);
        }
Example #7
0
            public static DataPack parseDataPack(ProblemInstance problem, Solution solution)
            {
                DataPack dataPack = new DataPack();

                dataPack.data = new int[problem.m + problem.n - 1];
                int pointer = 0;

                for (int i = 0; i < problem.m; i++)
                {
                    if (i > 0)
                    {
                        dataPack.data[pointer++] = -1;
                    }
                    for (int j = 0; j < solution.job_ids[i].Count; j++)
                    {
                        dataPack.data[pointer++] = solution.job_ids[i][j];
                    }
                }

                return(dataPack);
            }
Example #8
0
        public Solution minimize(ProblemInstance problem, Evaluator evaluator)
        {
            this.problem   = problem;
            this.evaluator = evaluator;
            Solution solution = new Solution(problem.m);

            List <int> order_ids = new List <int>();

            for (int i = 0; i < problem.n; i++)
            {
                order_ids.Add(i);
            }
            order_ids.Sort((x, y) =>
            {
                JobDetails job_x = problem.job_details[x];
                JobDetails job_y = problem.job_details[y];

                double d = angles(job_x) - angles(job_y);
                if (d < 0)
                {
                    return(-1);
                }
                if (d == 0)
                {
                    return(0);
                }
                return(1);
            });

            int jobPerMachine = 1 + problem.n / problem.m, machine_id;

            for (int i = 0; i < problem.n; i++)
            {
                machine_id = i / jobPerMachine;
                solution.job_ids[machine_id].Add(order_ids[i]);
            }
            solution.tag = "扫描算法";
            return(solution);
        }
Example #9
0
        public Solution minimize(ProblemInstance problem, Evaluator evaluator)
        {
            DataPack g_best       = DataPack.parseDataPack(problem, SolutionUtils.getRandomSolution(problem));
            double   g_best_value = g_best.getValue(problem, evaluator);

            double last_value = -1;

            List <Particle> particles = new List <Particle>();

            for (int i = 0; i < population_size; i++)
            {
                particles.Add(new Particle(problem, evaluator));
            }

            Optimizer optimizer = new ImprovedGreedyOptimizer();

            particles.Add(new Particle(problem, evaluator, optimizer.minimize(problem, evaluator)));

            optimizer = new ScannerOptimizer();
            particles.Add(new Particle(problem, evaluator, optimizer.minimize(problem, evaluator)));

            for (int i = 0; i < iteration_times; i++)
            {
                foreach (Particle particle in particles)
                {
                    particle.update(ref g_best, ref g_best_value, particles);
                }
                if (last_value != g_best_value)
                {
                    last_value = g_best_value;
                    Console.WriteLine("OPT = " + last_value.ToString());
                }
            }
            Solution solution = g_best.getSolution(problem);

            solution.tag = "粒子群优化算法";
            return(solution);
        }
Example #10
0
        public SolutionViewer(ProblemInstance problem, Solution solution, Cost cost, String title)
        {
            InitializeComponent();

            WorkCanvas wc = new WorkCanvas(pictureBox1.Width, pictureBox1.Height, problem, solution, cost, title);
        }
Example #11
0
 public double getValue(ProblemInstance problem, Evaluator evaluator)
 {
     return(evaluator.evaluate(problem, getSolution(problem)).totalCost);
 }