Beispiel #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);
        }
        public override ContinuousSolution Minimize(CostEvaluationMethod evaluate, GradientEvaluationMethod calc_grad, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            double?improvement = null;
            int    iteration   = 0;

            if (mLocalSearch == null)
            {
                throw new ArgumentNullException();
            }

            mLocalSearch.LowerBounds = mLowerBounds;
            mLocalSearch.UpperBounds = mUpperBounds;

            int problem_size = mDimension;
            ContinuousSolution best_solution = new ContinuousSolution();

            while (!should_terminate(improvement, iteration))
            {
                double[] x = GreedyConstructRandomSolution(evaluate, constraints, problem_size);

                ContinuousSolution x_pi_refined = mLocalSearch.Minimize(x, evaluate, calc_grad, mLocalSearchShouldTerminate, constraints);

                if (best_solution.TryUpdateSolution(x_pi_refined.Values, x_pi_refined.Cost, out improvement))
                {
                    OnSolutionUpdated(best_solution, iteration);
                }

                OnStepped(best_solution, iteration);
                iteration++;
            }

            return(best_solution);
        }
Beispiel #3
0
        public static double SolveByAntSystem(int population_size, int state_count, CostEvaluationMethod evaluator,
                                              HeuristicValueEvaluationMethod heuristicEvaluator, int displayEvery, out Ant global_best_solution, CreateAntMethod generator = null, int maxIterations = 100000)
        {
            AntSystem <Ant> solver = new AntSystem <Ant>(population_size, state_count, evaluator, generator);

            solver.HeuristicValueEvaluator = heuristicEvaluator;
            solver.Initialize();
            int    iteration = 0;
            double global_best_solution_cost      = solver.GlobalBestSolutionCost;
            double prev_global_best_solution_cost = global_best_solution_cost;

            while (iteration < maxIterations)
            {
                prev_global_best_solution_cost = global_best_solution_cost;
                solver.Iterate();
                global_best_solution_cost = solver.GlobalBestSolutionCost;
                iteration++;

                if (iteration % displayEvery == 0)
                {
                    Console.WriteLine("Iteration: {0} Cost: {1}", iteration, global_best_solution_cost);
                }
            }

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

            return(global_best_solution_cost);
        }
        public static double Solve(int population_size, int dimension_count, CostEvaluationMethod evaluator, CreateBeeMethod generator, out Bee global_best_solution, double[] lower_bounds = null, double[] upper_bounds = null, object constraints = null, double tolerance = 0.000001, int maxIterations = 100000)
        {
            BeeSwarm <Bee> solver = new BeeSwarm <Bee>(60, 15, 30, 20, 10, evaluator, generator);

            solver.LowerBounds       = lower_bounds;
            solver.UpperBounds       = upper_bounds;
            solver.Constraints       = constraints;
            solver.LocalSearchRegion = new double[dimension_count];
            for (int i = 0; i < dimension_count; ++i)
            {
                solver.LocalSearchRegion[i] = (upper_bounds[i] - lower_bounds[i]) / 1000;
            }

            solver.Initialize();
            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 Bee;

            return(global_best_solution_cost);
        }
Beispiel #5
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);
        }
Beispiel #6
0
        public ParticleSwarm(int population_size, int dimension_count, CostEvaluationMethod evaluator, double[] lower_bounds, double[] upper_bounds)
        {
            mDimensionCount = dimension_count;

            mLowerBounds = lower_bounds;
            mUpperBounds = upper_bounds;

            mParticleGenerator = (constraints) =>
            {
                SimpleParticle p = new SimpleParticle(this, mDimensionCount, constraints);
                double         lower_bound;
                double         upper_bound;
                double         range;
                for (int d = 0; d < mDimensionCount; ++d)
                {
                    lower_bound = lower_bounds[d];
                    upper_bound = upper_bounds[d];
                    range       = upper_bound - lower_bound;
                    p[d, ParticleVectorType.Position] = lower_bound + range * RandomEngine.NextDouble();
                    p[d, ParticleVectorType.Velocity] = 0;
                }
                Particle cast_p = p as Particle;
                return(cast_p);
            };

            mParticles          = new Particle[population_size];
            mLocalBestParticles = new Particle[population_size];
            mEvaluator          = evaluator;
        }
Beispiel #7
0
        public ParticleSwarm(int population_size, int dimension_count, CostEvaluationMethod evaluator, CreateParticleMethod generater = null)
        {
            mDimensionCount    = dimension_count;
            mParticleGenerator = generater;
            mEvaluator         = evaluator;

            mParticles          = new Particle[population_size];
            mLocalBestParticles = new Particle[population_size];
            mEvaluator          = evaluator;
        }
        protected ContinuousSolution DoLocalSearch(double[] x, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, object constraints)
        {
            if (mLocalSearch != null)
            {
                return(mLocalSearch.Minimize(x, evaluate, calc_gradient, mLocalSearchTerminationCondition, constraints));
            }
            double fx = evaluate(x, mLowerBounds, mUpperBounds, constraints);

            return(new ContinuousSolution(x, fx));
        }
        public BeeSwarm(int _n, int _n1, int _n2, int _m, int _e, CostEvaluationMethod evaluator, CreateBeeMethod generator)
        {
            mScoutBeeCount         = _n;
            mBeeCount_BestPatches  = _n1;
            mBeeCount_ElitePatches = _n2;

            mBestPatchCount  = _m;
            mElitePatchCount = _e;

            mBeeGenerator = generator;
            mEvaluator    = evaluator;
        }
Beispiel #10
0
        //calculate gradient
        public static void CalcGradient(double[] solution, double[] gradf, CostEvaluationMethod evaluate)
        {
            double xi, delta;
            double udelta = 0.0;

            int dimension_count = solution.Length;

            double rteps = Math.Sqrt(small());

            for (int i = 0; i < dimension_count; i++)
            {
                xi = solution[i];

                double tmp = 1.0e0;
                if (1.0e0 > Math.Abs(xi))
                {
                    tmp = 1.0e0;
                }
                else
                {
                    tmp = Math.Abs(xi);
                }
                if (udelta > rteps * tmp)
                {
                    delta = udelta;
                }
                else
                {
                    delta = rteps * tmp;
                }
                if (xi < 0.0)
                {
                    delta = -delta;
                }

                solution[i] = xi + delta;
                double f2 = evaluate(solution);
                solution[i] = xi;
                double f1 = evaluate(solution);
                if (double.IsInfinity(f1) && double.IsInfinity(f2))
                {
                    gradf[i] = 0;
                }
                else
                {
                    gradf[i] = (f2 - f1) / delta;
                }
            }
            return;
        }
Beispiel #11
0
        public override ContinuousSolution Minimize(double[] x_0, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_grad, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            double?improvement = null;
            int    iteration   = 0;

            double             fx_0          = evaluate(x_0, mLowerBounds, mUpperBounds, constraints);
            ContinuousSolution best_solution = new ContinuousSolution(x_0, fx_0);

            int dimension = x_0.Length;

            while (!should_terminate(improvement, iteration))
            {
                double[] best_x_in_neighborhood    = null;
                double   best_x_in_neighborhood_fx = double.MaxValue;
                int      move_id = -1;
                for (int i = 0; i < dimension; ++i)
                {
                    if (!IsMoveTabu(i))
                    {
                        double[] x_pi  = GetNeighbor(best_solution.Values, i, constraints);
                        double   fx_pi = evaluate(x_pi, mLowerBounds, mUpperBounds, constraints);

                        if (fx_pi < best_x_in_neighborhood_fx)
                        {
                            best_x_in_neighborhood    = x_pi;
                            best_x_in_neighborhood_fx = fx_pi;
                            move_id = i;
                        }
                    }
                }

                if (best_x_in_neighborhood != null)
                {
                    if (best_solution.TryUpdateSolution(best_x_in_neighborhood, best_x_in_neighborhood_fx, out improvement))
                    {
                        TabuMove(move_id);
                        OnSolutionUpdated(best_solution, iteration);
                    }
                }

                LowerTabuList();

                OnStepped(best_solution, iteration);
                iteration++;
            }

            return(best_solution);
        }
        public override ContinuousSolution Minimize(double[] x_0, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_grad, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            double?improvement = null;
            int    iteration   = 0;

            if (mNeighborhoods.Count == 0)
            {
                throw new ArgumentNullException();
            }

            mNeighborhoods.LowerBounds = mLowerBounds;
            mNeighborhoods.UpperBounds = mUpperBounds;

            double[] x  = (double[])x_0.Clone();
            double   fx = evaluate(x, mLowerBounds, mUpperBounds, constraints);

            ContinuousSolution best_solution = new ContinuousSolution(x, fx);

            int neighborhood_count = mNeighborhoods.Count;

            ContinuousSolution current_best = null;

            while (!should_terminate(improvement, iteration))
            {
                for (int l = 0; l < neighborhood_count; ++l)
                {
                    SingleTrajectoryContinuousSolver local_search = mNeighborhoods.GetLocalSearchAt(l);
                    SingleTrajectoryContinuousSolver.TerminationEvaluationMethod termination_condition = mNeighborhoods.GetTerminationConditionAt(l);

                    current_best = local_search.Minimize(x, evaluate, calc_grad, termination_condition, constraints);

                    x  = current_best.Values;
                    fx = current_best.Cost;

                    if (best_solution.TryUpdateSolution(x, fx, out improvement))
                    {
                        OnSolutionUpdated(best_solution, iteration);
                    }

                    OnStepped(best_solution, iteration);
                    iteration++;
                }
            }

            return(best_solution);
        }
        public BeeSwarm(int _n, int _n1, int _n2, int _m, int _e, CostEvaluationMethod evaluator)
        {
            mScoutBeeCount         = _n;
            mBeeCount_BestPatches  = _n1;
            mBeeCount_ElitePatches = _n2;

            mBestPatchCount  = _m;
            mElitePatchCount = _e;

            mBeeGenerator = () =>
            {
                SimpleBee p = new SimpleBee(this, mLowerBounds.Length, mLowerBounds, mUpperBounds);
                p.Initialize(Constraints);
                return(p as Bee);
            };
            mEvaluator = evaluator;
        }
Beispiel #14
0
        public AntSystem(int population_size, int state_count, CostEvaluationMethod evaluator, CreateAntMethod generator = null)
        {
            mEvaluator  = evaluator;
            mStateCount = state_count;
            if (generator == null)
            {
                mAntGenerator = (info) =>
                {
                    SimpleAnt p      = new SimpleAnt(this, info);
                    Ant       cast_p = p as Ant;
                    return(cast_p);
                };
            }

            mAnts       = new Ant[population_size];
            mPheromones = new double[mStateCount, mStateCount];
            mTau0       = 1.0 / state_count;
        }
        protected bool ExploreSubsets(ref ContinuousSolution[] ref_set, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_grad, object constraints)
        {
            bool is_improved = false;

            List <ContinuousSolution[]> subsets = SelectSubsets(ref_set);

            for (int i = 0; i < ref_set.Length; ++i)
            {
                ref_set[i].SetIsNew(false);
            }

            for (int i = 0; i < subsets.Count; ++i)
            {
                ContinuousSolution[] subset = subsets[i];
                ContinuousSolution[] candidate_solutions = Recombine(subset, constraints);
                ContinuousSolution[] improvements        = new ContinuousSolution[candidate_solutions.Length];
                for (int j = 0; j < improvements.Length; ++j)
                {
                    //double fx = evaluate(candidate_solutions[j].Values, mLowerBounds, mUpperBounds, constraints);
                    ContinuousSolution s = mLocalSearch.Minimize(candidate_solutions[j].Values, evaluate, calc_grad, mLocalSearchTerminationCondition, constraints);
                    //double improvement = fx - s.Cost;
                    improvements[j] = s;
                }
                for (int j = 0; j < improvements.Length; ++j)
                {
                    if (ref_set.ContainsItem(improvements[j]))
                    {
                        improvements[j].SetIsNew(false);
                    }
                    else
                    {
                        improvements[j].SetIsNew(true);
                        ref_set = ref_set.OrderBy(s => s.Cost).ToArray();
                        if (ref_set[mReferenceSetSize - 1].Cost > improvements[j].Cost)
                        {
                            ref_set[mReferenceSetSize - 1] = improvements[j];
                            is_improved = true;
                        }
                    }
                }
            }

            return(is_improved);
        }
        public override ContinuousSolution Minimize(double[] x_0, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            double?improvement = null;
            int    iteration   = 0;

            double             fx_0          = evaluate(x_0, mLowerBounds, mUpperBounds, constraints);
            ContinuousSolution best_solution = new ContinuousSolution(x_0, fx_0);

            if (mSearchSpaceSize == -1)
            {
                mSearchSpaceSize = x_0.Length;
            }

            while (!should_terminate(improvement, iteration))
            {
                double[] best_x_in_neighborhood    = null;
                double   best_x_in_neighborhood_fx = double.MaxValue;
                for (int i = 0; i < mSearchSpaceSize; ++i)
                {
                    double[] x_pi  = CreateRandomSolution(mSolutionGenerator, i, constraints);
                    double   fx_pi = evaluate(x_pi, mLowerBounds, mUpperBounds, constraints);

                    if (fx_pi < best_x_in_neighborhood_fx)
                    {
                        best_x_in_neighborhood    = x_pi;
                        best_x_in_neighborhood_fx = fx_pi;
                    }
                }

                if (best_x_in_neighborhood != null)
                {
                    if (best_solution.TryUpdateSolution(best_x_in_neighborhood, best_x_in_neighborhood_fx, out improvement))
                    {
                        OnSolutionUpdated(best_solution, iteration);
                    }
                }

                OnStepped(best_solution, iteration);
                iteration++;
            }

            return(best_solution);
        }
        public override ContinuousSolution Minimize(double[] x_0, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            double?improvement = null;
            int    iteration   = 0;

            if (mLocalSearch == null)
            {
                throw new ArgumentNullException();
            }

            mLocalSearch.LowerBounds = mLowerBounds;
            mLocalSearch.UpperBounds = mUpperBounds;

            double[] x = (double[])x_0.Clone();

            double fx = evaluate(x, mLowerBounds, mUpperBounds, constraints);

            ContinuousSolution best_solution = new ContinuousSolution(x, fx);

            while (!should_terminate(improvement, iteration))
            {
                int      r_index = (int)(RandomEngine.NextDouble() * x.Length);
                double[] x_pi    = GetNeighbor(x, r_index, constraints);
                double   fx_pi   = evaluate(x_pi, mLowerBounds, mUpperBounds, constraints);

                ContinuousSolution x_pi_refined = mLocalSearch.Minimize(x_pi, evaluate, calc_gradient, mLocalSearchTerminationCondition, constraints);

                if (best_solution.TryUpdateSolution(x_pi_refined.Values, x_pi_refined.Cost, out improvement))
                {
                    OnSolutionUpdated(best_solution, iteration);
                }

                x  = best_solution.Values;
                fx = best_solution.Cost;

                OnStepped(best_solution, iteration);
                iteration++;
            }

            return(best_solution);
        }
        public override ContinuousSolution Minimize(CostEvaluationMethod evaluate, GradientEvaluationMethod calc_grad, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            double?improvement = null;
            int    iteration   = 0;

            mLocalSearch.LowerBounds = mLowerBounds;
            mLocalSearch.UpperBounds = mUpperBounds;

            List <ContinuousSolution> diverse_set = ConstructDiverseSet(evaluate, calc_grad, constraints);

            diverse_set = diverse_set.OrderBy(s => s.Cost).ToList();

            ContinuousSolution[] ref_set = Diversify(diverse_set);

            foreach (ContinuousSolution s in ref_set)
            {
                s.SetIsNew(true);
            }

            ContinuousSolution best_solution = ref_set[0].Clone() as ContinuousSolution;

            while (!should_terminate(improvement, iteration))
            {
                bool was_changed = ExploreSubsets(ref ref_set, evaluate, calc_grad, constraints);

                if (!was_changed)
                {
                    break;
                }

                if (best_solution.TryUpdateSolution(ref_set[0].Values, ref_set[0].Cost, out improvement))
                {
                    OnSolutionUpdated(best_solution, iteration);
                }

                OnStepped(best_solution, iteration);
                iteration++;
            }

            return(best_solution);
        }
        public override ContinuousSolution Minimize(double[] x_0, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            int dimension = x_0.Length;

            double[] x  = (double[])x_0.Clone();
            double   fx = evaluate(x, mLowerBounds, mUpperBounds, constraints);

            double[] Vfx = new double[dimension];

            ContinuousSolution best_solution = new ContinuousSolution(x, fx);

            double?improvement = null;
            int    iteration   = 0;

            while (!should_terminate(improvement, iteration))
            {
                calc_gradient(x, Vfx, mLowerBounds, mUpperBounds, constraints);

                for (int d = 0; d < dimension; ++d)
                {
                    x[d] = x[d] - mAlpha * Vfx[d];
                }

                fx = evaluate(x, mLowerBounds, mUpperBounds, constraints);

                if (best_solution.TryUpdateSolution(x, fx, out improvement))
                {
                    //Console.WriteLine("called");
                    OnSolutionUpdated(best_solution, iteration);
                }

                OnStepped(new ContinuousSolution(x, fx), iteration);
                iteration++;
            }

            return(best_solution);
        }
        public static double Solve(int dimension_count, CostEvaluationMethod evaluator, out Bee global_best_solution, double[] lower_bounds = null, double[] upper_bounds = null, int maxIterations = 100000, int displayEvery = 100, object constraints = null)
        {
            int            scoutBeeCount = 60;
            int            bestPatch     = 15;
            int            elitePatch    = 30;
            BeeSwarm <Bee> solver        = new BeeSwarm <Bee>(scoutBeeCount, bestPatch, elitePatch, 20, 10, evaluator);

            solver.LowerBounds       = lower_bounds;
            solver.UpperBounds       = upper_bounds;
            solver.Constraints       = constraints;
            solver.LocalSearchRegion = new double[dimension_count];
            for (int i = 0; i < dimension_count; ++i)
            {
                solver.LocalSearchRegion[i] = (upper_bounds[i] - lower_bounds[i]) / bestPatch;
            }

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

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

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

            return(global_best_solution_cost);
        }
        protected List <ContinuousSolution> ConstructDiverseSet(CostEvaluationMethod evaluate, GradientEvaluationMethod calc_grad, object constraints)
        {
            double[]           x             = null;
            ContinuousSolution best_solution = new ContinuousSolution();

            double?improvement2 = null;
            List <ContinuousSolution> diverse_set = new List <ContinuousSolution>();

            while (diverse_set.Count < mReferenceSetSize)
            {
                x = CreateRandomSolution(evaluate, mLowerBounds, mUpperBounds, constraints);

                ContinuousSolution xs = DoLocalSearch(x, evaluate, calc_grad, constraints);

                double minDistanceSq = double.MaxValue;
                double distanceSq    = 0;
                bool   contains_item = false;
                foreach (ContinuousSolution s in diverse_set)
                {
                    distanceSq = s.GetDistanceSq2(xs);
                    if (distanceSq.Equals(minDistanceSq))
                    {
                        contains_item = true;
                        break;
                    }
                }

                if (!contains_item)
                {
                    diverse_set.Add(xs);
                }

                best_solution.TryUpdateSolution(xs.Values, xs.Cost, out improvement2);
            }

            return(diverse_set);
        }
 public abstract ContinuousSolution Minimize(CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, TerminationEvaluationMethod should_terminate, object constraints = null);
Beispiel #23
0
        public override ContinuousSolution Minimize(double[] x_0, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            double?improvement = null;
            int    iteration   = 0;

            double[] x = (double[])x_0.Clone();

            double             fx            = evaluate(x, mLowerBounds, mUpperBounds, constraints);
            ContinuousSolution best_solution = new ContinuousSolution(x, fx);

            double current_step_size = 0;
            double step_size_large   = 0;

            int no_change_count = 0;

            while (!should_terminate(improvement, iteration))
            {
                double[] x1  = TakeStep(mSolutionGenerator, x, current_step_size, constraints);
                double   fx1 = evaluate(x1, mLowerBounds, mUpperBounds, constraints);

                step_size_large = CalcStepSizeLarge(current_step_size, iteration);

                double[] x2  = TakeStep(mSolutionGenerator, x, current_step_size, constraints);
                double   fx2 = evaluate(x1, mLowerBounds, mUpperBounds, constraints);

                if (fx1 < fx || fx2 < fx)
                {
                    if (fx1 < fx2)
                    {
                        x  = x1;
                        fx = fx1;
                        current_step_size = step_size_large;
                    }
                    else
                    {
                        x  = x2;
                        fx = fx2;
                    }

                    no_change_count = 0;

                    if (best_solution.TryUpdateSolution(x, fx, out improvement))
                    {
                        OnSolutionUpdated(best_solution, iteration);
                    }
                }
                else
                {
                    no_change_count++;

                    if (no_change_count > mMaxNoChangeIterationCount)
                    {
                        no_change_count   = 0;
                        current_step_size = current_step_size / mStepSizeFactor_Small;
                    }
                }

                OnStepped(best_solution, iteration);
                iteration++;
            }

            return(best_solution);
        }
 public AntColonySystem(int population_size, int state_count, CostEvaluationMethod evaluator, CreateAntMethod generator = null)
     : base(population_size, state_count, evaluator, generator)
 {
 }
        public override ContinuousSolution Minimize(double[] x_0, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            ContinuousSolution best_solution = new ContinuousSolution();

            int dimension = x_0.Length;

            double[] x  = (double[])x_0.Clone();
            double   fx = evaluate(x, mLowerBounds, mUpperBounds, constraints);

            double?improvement = null;
            int    iteration   = 0;

            while (!should_terminate(improvement, iteration))
            {
                double[] fx_candidate = new double[dimension];
                double[] h            = new double[dimension];
                for (int d = 0; d < dimension; ++d)
                {
                    x[d]           += mDeltaX;
                    fx_candidate[d] = evaluate(x, mLowerBounds, mUpperBounds, constraints);
                    h[d]            = fx - fx_candidate[d];
                    x[d]           -= mDeltaX;
                }

                int    max_d = -1;
                double h_max = 0;
                for (int d = 0; d < dimension; ++d)
                {
                    if (h_max < h[d])
                    {
                        h_max = h[d];
                        max_d = d;
                    }
                }

                if (max_d != -1)
                {
                    fx        = fx_candidate[max_d];
                    x[max_d] += mDeltaX;
                }
                else
                {
                    if (mDeltaX > ZERO)
                    {
                        mDeltaX *= (-BETA);
                    }
                    else
                    {
                        break;
                    }
                }

                if (best_solution.TryUpdateSolution(x, fx, out improvement))
                {
                    OnSolutionUpdated(best_solution, iteration);
                }
                OnStepped(new ContinuousSolution(x, fx), iteration);
                iteration++;
            }

            return(best_solution);
        }
        public override ContinuousSolution Minimize(double[] x_0, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            ContinuousSolution best_solution = new ContinuousSolution();

            int dimension = x_0.Length;

            double[] x  = (double[])x_0.Clone();
            double   fx = evaluate(x, mLowerBounds, mUpperBounds, constraints);

            double[] Vfx = new double[dimension];
            calc_gradient(x, Vfx, mLowerBounds, mUpperBounds, constraints);

            double[] deltaX      = new double[dimension];
            double[] deltaX_prev = new double[dimension];


            for (int d = 0; d < dimension; ++d)
            {
                deltaX[d] = -Vfx[d];
            }

            double alpha;

            double[] x_next;
            double   fx_next;

            LineSearch(x, fx, deltaX, out x_next, out fx_next, out alpha, evaluate, calc_gradient, mLowerBounds, mUpperBounds, constraints);

            double beta = 0;

            double[] s = new double[dimension];

            for (int d = 0; d < dimension; ++d)
            {
                s[d] = deltaX[d];
            }

            double?improvement = null;
            int    iteration   = 0;

            while (!should_terminate(improvement, iteration))
            {
                for (int d = 0; d < dimension; ++d)
                {
                    deltaX_prev[d] = deltaX[d];
                    x[d]           = x_next[d];
                }

                calc_gradient(x, Vfx, mLowerBounds, mUpperBounds, constraints);
                for (int d = 0; d < dimension; ++d)
                {
                    deltaX[d] = -Vfx[d];
                }

                beta = ComputeBeta(deltaX, deltaX_prev, s);

                for (int d = 0; d < dimension; ++d)
                {
                    s[d] = deltaX[d] + beta * s[d];
                }

                LineSearch(x, fx, s, out x_next, out fx_next, out alpha, evaluate, calc_gradient, mLowerBounds, mUpperBounds, constraints);

                if (best_solution.TryUpdateSolution(x_next, fx_next, out improvement))
                {
                    OnSolutionUpdated(best_solution, iteration);
                }
                OnStepped(new ContinuousSolution(x_next, fx_next), iteration);
                iteration++;
            }

            return(best_solution);
        }
Beispiel #27
0
        public override ContinuousSolution Minimize(double[] x_0, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            int dimension = x_0.Length;

            double[] x      = (double[])x_0.Clone();
            double[] x_prev = (double[])x.Clone();
            double   fx     = evaluate(x, mLowerBounds, mUpperBounds, constraints);

            ContinuousSolution best_solution = new ContinuousSolution(x, fx);

            double[] Vfx      = new double[dimension];
            double[] Vfx_prev = new double[dimension];

            double[] y = new double[dimension];

            double[] p = new double[dimension];
            double[] s = new double[dimension];

            double[] x_out;
            double   fx_out = 0;
            double   alpha  = 1;

            calc_gradient(x, Vfx, mLowerBounds, mUpperBounds, constraints);

            Matrix <double> B_matrix = SparseMatrix.Identity(dimension);

            ContinuousSolution solution = new ContinuousSolution(x, fx);

            int    iteration   = 0;
            double?improvement = null;

            while (!should_terminate(improvement, iteration))
            {
                Matrix <double> Vfx_matrix = new SparseMatrix(dimension, 1, 0);

                for (int d = 0; d < dimension; ++d)
                {
                    Vfx_matrix[d, 0] = -Vfx[d];
                }

                Matrix <double> p_matrix = B_matrix.Inverse().Multiply(Vfx_matrix);

                for (int d = 0; d < dimension; ++d)
                {
                    p[d] = p_matrix[d, 0];
                }

                fx = evaluate(x, mLowerBounds, mUpperBounds, constraints);
                //Console.WriteLine("before");
                SingleTrajectoryContinuousSolver.LineSearch(x, fx, p, out x_out, out fx_out, out alpha, evaluate, calc_gradient, mLowerBounds, mUpperBounds, constraints);
                //Console.WriteLine("after");
                if (best_solution.TryUpdateSolution(x, fx, out improvement))
                {
                    OnSolutionUpdated(best_solution, iteration);
                }

                for (int d = 0; d < dimension; ++d)
                {
                    s[d] = alpha * p[d];
                }
                Matrix <double> s_matrix   = new SparseMatrix(dimension, 1, s);
                Matrix <double> s_matrix_t = s_matrix.Transpose();

                for (int d = 0; d < dimension; ++d)
                {
                    x_prev[d] = x[d];
                }

                for (int d = 0; d < dimension; ++d)
                {
                    Vfx_prev[d] = Vfx[d];
                }

                for (int d = 0; d < dimension; ++d)
                {
                    x[d] += alpha * p[d];
                }

                calc_gradient(x, Vfx, mLowerBounds, mUpperBounds, constraints);

                for (int d = 0; d < dimension; ++d)
                {
                    y[d] = Vfx[d] - Vfx_prev[d];
                }

                Matrix <double> y_matrix   = new SparseMatrix(dimension, 1, y);
                Matrix <double> y_matrix_t = y_matrix.Transpose();

                Matrix <double> yts_matrix = y_matrix_t.Multiply(s_matrix);
                double          yts        = yts_matrix[0, 0];

                Matrix <double> sBs_matrix = s_matrix_t.Multiply(B_matrix).Multiply(s_matrix);
                double          sBs        = sBs_matrix[0, 0];

                B_matrix = B_matrix + y_matrix.Multiply(y_matrix_t).Divide(yts) - B_matrix.Multiply(s_matrix).Multiply(s_matrix_t).Multiply(B_matrix).Divide(sBs);

                OnStepped(new ContinuousSolution(x, fx), iteration);
                iteration++;
            }

            return(best_solution);
        }
Beispiel #28
0
        public override ContinuousSolution Minimize(CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            double?improvement = null;
            int    iteration   = 0;

            ContinuousSolution[] pop = new ContinuousSolution[mPopSize];

            double[] lower_bounds = null;
            double[] upper_bounds = null;
            if (mLowerBounds == null || mUpperBounds == null)
            {
                if (constraints is Tuple <double[], double[]> )
                {
                    Tuple <double[], double[]> bounds = constraints as Tuple <double[], double[]>;
                    lower_bounds = bounds.Item1;
                    upper_bounds = bounds.Item2;
                }
                else
                {
                    throw new InvalidCastException();
                }
            }
            else
            {
                lower_bounds = mLowerBounds;
                upper_bounds = mUpperBounds;
            }

            if (lower_bounds.Length < mDimension)
            {
                throw new IndexOutOfRangeException();
            }
            if (upper_bounds.Length < mDimension)
            {
                throw new IndexOutOfRangeException();
            }

            double[,] init_strategy_bounds = new double[mDimension, 2];
            for (int j = 0; j < mDimension; ++j)
            {
                init_strategy_bounds[j, 0] = 0;
                init_strategy_bounds[j, 1] = (upper_bounds[j] - lower_bounds[j]) * 0.05;
            }


            ContinuousSolution best_solution = null;

            for (int i = 0; i < mPopSize; ++i)
            {
                double[]           x        = mSolutionGenerator(mDimension, constraints);
                double             fx       = evaluate(x, mLowerBounds, mUpperBounds, constraints);
                ContinuousSolution s        = new ContinuousSolution(x, fx);
                double[]           strategy = new double[mDimension];
                for (int j = 0; j < mDimension; ++j)
                {
                    strategy[j] = init_strategy_bounds[j, 0] + (init_strategy_bounds[j, 1] - init_strategy_bounds[j, 0]) * RandomEngine.NextDouble();
                }
                s.SetMutationStrategy(strategy);
                pop[i] = s;
            }

            pop = pop.OrderBy(s => s.Cost).ToArray();

            best_solution = pop[0].Clone() as ContinuousSolution;

            ContinuousSolution[] children   = new ContinuousSolution[mPopSize];
            ContinuousSolution[] generation = new ContinuousSolution[mPopSize * 2];

            while (!should_terminate(improvement, iteration))
            {
                for (int i = 0; i < mPopSize; ++i)
                {
                    children[i]      = Mutate(pop[i], lower_bounds, upper_bounds, constraints);
                    children[i].Cost = evaluate(children[i].Values, mLowerBounds, mUpperBounds, constraints);
                }

                children = children.OrderBy(s => s.Cost).ToArray();
                if (best_solution.TryUpdateSolution(children[0].Values, children[0].Cost, out improvement))
                {
                    OnSolutionUpdated(best_solution, iteration);
                }


                for (int i = 0; i < mPopSize; ++i)
                {
                    generation[i] = pop[i];
                }
                for (int i = 0; i < mPopSize; ++i)
                {
                    generation[i + mPopSize] = children[i];
                }


                for (int i = 0; i < generation.Length; ++i)
                {
                    int wins = 0;
                    ContinuousSolution si = generation[i];
                    for (int j = 0; j < mBoutSize; ++j)
                    {
                        ContinuousSolution sj = generation[RandomEngine.NextInt(generation.Length)];
                        if (si.Cost < sj.Cost)
                        {
                            wins++;
                        }
                    }
                    si.SetWins(wins);
                }

                generation = generation.OrderByDescending(s => s.GetWins()).ToArray();

                for (int i = 0; i < mPopSize; ++i)
                {
                    pop[i] = generation[i];
                }


                OnStepped(best_solution, iteration);
                iteration++;
            }

            return(best_solution);
        }
        public static bool LineSearch(double[] x_0, double fx_0, double[] direction, out double[] x_out, out double fx_out, out double alpha, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, double[] lower_bounds, double[] upper_bounds, object constraints = null)
        {
            int dimension = x_0.Length;

            double[] Vfx = new double[dimension];
            x_out  = new double[dimension];
            alpha  = 1.0;
            fx_out = double.MaxValue;

            calc_gradient(x_0, Vfx, lower_bounds, upper_bounds, constraints);

            double direction_length = 0;

            for (int d = 0; d < dimension; ++d)
            {
                direction_length += direction[d] * direction[d];
            }
            direction_length = Math.Sqrt(direction_length);

            if (direction_length > 0)
            {
                for (int d = 0; d < dimension; ++d)
                {
                    direction[d] /= direction_length;
                }
            }

            double p = 0.0;

            for (int d = 0; d < dimension; ++d)
            {
                p += (direction[d] * Vfx[d]);
            }

            //Console.WriteLine("p: {0}", p);


            if (double.IsNaN(p))
            {
                return(false);
            }

            if (p >= 0.0) // not in the descending direction return false;
            {
                return(false);
            }


            for (int k = 0; ; ++k)
            {
                for (int d = 0; d < dimension; ++d)
                {
                    x_out[d] = x_0[d] + alpha * direction[d];
                }
                fx_out = evaluate(x_out, lower_bounds, upper_bounds, constraints);

                if (fx_out < fx_0 + SIGMA * alpha * p)
                {
                    return(true);
                }
                else
                {
                    if (k == 0)
                    {
                        double enumerator = (p + fx_0 - fx_out);
                        if (enumerator == 0)
                        {
                            alpha = 0.5 * p / enumerator;
                        }
                        else
                        {
                            alpha = 0.5 * p;
                        }

                        //Console.WriteLine("alpha: {0}", alpha);
                    }
                    else
                    {
                        alpha *= BETA;
                    }
                }

                //Console.WriteLine("alpha: {0}", alpha);

                if (alpha < ZERO)
                {
                    if (fx_out > fx_0)
                    {
                        for (int d = 0; d < dimension; ++d)
                        {
                            x_out[d] = x_0[d];
                        }

                        fx_out = fx_0;
                        return(true);
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
        }
Beispiel #30
0
        public override ContinuousSolution Minimize(CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            double?improvement = null;
            int    iteration   = 0;

            ContinuousSolution[] pop      = new ContinuousSolution[mPopSize];
            ContinuousSolution[] children = new ContinuousSolution[mPopSize];

            double[] lower_bounds = null;
            double[] upper_bounds = null;

            if (mLowerBounds == null || mUpperBounds == null)
            {
                if (constraints is Tuple <double[], double[]> )
                {
                    Tuple <double[], double[]> bounds = constraints as Tuple <double[], double[]>;
                    lower_bounds = bounds.Item1;
                    upper_bounds = bounds.Item2;
                }
                else
                {
                    throw new InvalidCastException();
                }
            }
            else
            {
                lower_bounds = mLowerBounds;
                upper_bounds = mUpperBounds;
            }

            if (lower_bounds.Length < mDimension)
            {
                throw new IndexOutOfRangeException();
            }
            if (upper_bounds.Length < mDimension)
            {
                throw new IndexOutOfRangeException();
            }

            double[] best_x0  = null;
            double   best_fx0 = double.MaxValue;

            for (int i = 0; i < mPopSize; ++i)
            {
                double[]           x  = mSolutionGenerator(mDimension, constraints);
                double             fx = evaluate(x, mLowerBounds, mUpperBounds, constraints);
                ContinuousSolution s  = new ContinuousSolution(x, fx);
                pop[i] = s;
                if (fx < best_fx0)
                {
                    best_fx0 = fx;
                    best_x0  = x;
                }
            }

            pop = pop.OrderBy(s => s.Cost).ToArray();
            ContinuousSolution best_solution = new ContinuousSolution(best_x0, best_fx0);

            while (!should_terminate(improvement, iteration))
            {
                for (int i = 0; i < mPopSize; ++i)
                {
                    ContinuousSolution child = Reproduce(pop, pop[i], lower_bounds, upper_bounds);
                    child.Cost  = evaluate(child.Values, mLowerBounds, mUpperBounds, constraints);
                    children[i] = child;
                }

                for (int i = 0; i < mPopSize; ++i)
                {
                    if (pop[i].Cost > children[i].Cost)
                    {
                        pop[i] = children[i];
                    }
                }

                pop = pop.OrderBy(s => s.Cost).ToArray();

                if (best_solution.TryUpdateSolution(pop[0].Values, pop[0].Cost, out improvement))
                {
                    OnSolutionUpdated(best_solution, iteration);
                }

                OnStepped(best_solution, iteration);
                iteration++;
            }

            return(best_solution);
        }