コード例 #1
0
        public static double Solve(int population_size, int dimension_count, CostEvaluationMethod evaluator, CreateParticleMethod generator, out Particle global_best_solution, double[] lower_bounds = null, double[] upper_bounds = null, object constraints = null, double tolerance = 0.000001, int maxIterations = 100000)
        {
            ParticleSwarm <Particle> solver = new ParticleSwarm <Particle>(population_size, dimension_count, evaluator, generator);

            solver.LowerBounds = lower_bounds;
            solver.UpperBounds = upper_bounds;

            solver.Initialize(constraints);
            int    iteration                      = 0;
            double cost_reduction                 = tolerance;
            double global_best_solution_cost      = solver.GlobalBestSolutionCost;
            double prev_global_best_solution_cost = global_best_solution_cost;

            while (cost_reduction >= tolerance && iteration < maxIterations)
            {
                prev_global_best_solution_cost = global_best_solution_cost;
                solver.Iterate();
                global_best_solution_cost = solver.GlobalBestSolutionCost;
                cost_reduction            = prev_global_best_solution_cost - global_best_solution_cost;
                iteration++;
            }

            global_best_solution = solver.GlobalBestSolution.Clone() as Particle;

            return(global_best_solution_cost);
        }
コード例 #2
0
        public static double Solve(int population_size, int dimension_count, CostEvaluationMethod evaluator, double[] lower_bounds, double[] upper_bounds, out Particle global_best_solution, int maxIterations = 100000, int displayEvery = 100)
        {
            ParticleSwarm <Particle> solver = new ParticleSwarm <Particle>(population_size, dimension_count, evaluator, lower_bounds, upper_bounds);

            solver.Initialize();
            int    iteration = 0;
            double global_best_solution_cost     = solver.GlobalBestSolutionCost;
            double prev_global_best_soution_cost = global_best_solution_cost;

            while (iteration < maxIterations)
            {
                prev_global_best_soution_cost = global_best_solution_cost;
                solver.Iterate();
                global_best_solution_cost = solver.GlobalBestSolutionCost;
                //cost_reduction = prev_global_best_soution_cost - global_best_solution_cost;
                if (iteration % displayEvery == 0)
                {
                    Console.WriteLine("Generation: {0}, Best Cost: {1}", iteration, global_best_solution_cost);
                }
                iteration++;
            }

            global_best_solution = solver.GlobalBestSolution.Clone() as Particle;

            return(global_best_solution_cost);
        }