Esempio n. 1
0
        public Solution minimize(ProblemInstance problem, Evaluator evaluator)
        {
            List <DataPack> chrs = new List <DataPack>();

            for (int i = 0; i < population_size; i++)
            {
                chrs.Add(DataPack.parseDataPack(problem, SolutionUtils.getRandomSolution(problem)));
            }

            Optimizer optimizer = new ImprovedGreedyOptimizer();

            chrs.Add(DataPack.parseDataPack(problem, optimizer.minimize(problem, evaluator)));

            optimizer = new ScannerOptimizer();
            chrs.Add(DataPack.parseDataPack(problem, optimizer.minimize(problem, evaluator)));

            sort(chrs, problem, evaluator);

            double last_value = -1, g_best_value;

            for (int i = 0; i < iteration_times; i++)
            {
                for (int j = 0; j < population_size; j++)
                {
                    DataPack dataPack = chrs[j];
                    if (MathUtils.probability(cross_over_rate))
                    {
                        chrs.Add(crossover(dataPack, chrs[MathUtils.u(0, chrs.Count)]));
                    }
                    else
                    {
                        chrs.Add(mutation(dataPack));
                    }
                }
                sort(chrs, problem, evaluator);
                chrs         = chrs.GetRange(0, population_size);
                g_best_value = chrs[0].getValue(problem, evaluator);

                if (last_value != g_best_value)
                {
                    last_value = g_best_value;
                    Console.WriteLine("OPT = " + last_value.ToString());
                }
            }

            sort(chrs, problem, evaluator);
            Solution solution = chrs[0].getSolution(problem);

            solution.tag = "遗传算法";
            return(solution);
        }
Esempio n. 2
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);
        }