public IteratedLocalSearch(double[] masks, SingleTrajectoryContinuousSolver local_search, TerminationEvaluationMethod local_search_termination_condition, CreateRandomNeighborhoodMethod generator = null)
        {
            mLocalSearch = local_search;
            mLocalSearchTerminationCondition = local_search_termination_condition;


            mMasks = (double[])masks.Clone();
            mNeighborSolutionProvider = generator;
            if (mNeighborSolutionProvider == null)
            {
                mNeighborSolutionProvider = (x, index, constraints) =>
                {
                    double[] lower_bounds = null;
                    double[] upper_bounds = null;

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

                        lower_bounds = bounds.Item1;
                        upper_bounds = bounds.Item2;
                    }
                    else
                    {
                        lower_bounds = mLowerBounds;
                        upper_bounds = mUpperBounds;
                    }

                    if (lower_bounds.Length < x.Length)
                    {
                        throw new ArgumentOutOfRangeException();
                    }
                    if (upper_bounds.Length < x.Length)
                    {
                        throw new ArgumentOutOfRangeException();
                    }

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

                    for (int i = 0; i < mMasks.Length; ++i)
                    {
                        int    mindex  = (index + i) % x_p.Length;
                        double new_val = mMasks[i] > 0 ? x[(index + i)] + RandomEngine.Gauss(0, mMasks[i]) : x[(index + i) % x.Length];
                        new_val = System.Math.Max(lower_bounds[mindex], new_val);
                        new_val = System.Math.Min(upper_bounds[mindex], new_val);

                        x_p[mindex] = new_val;
                    }
                    return(x_p);
                };
            }
        }
        public ScatterSearch(GenerateSolutionMethod solution_generator, SingleTrajectoryContinuousSolver local_search, TerminationEvaluationMethod local_search_termination_condition)
        {
            mSolutionGenerator = solution_generator;
            mLocalSearch       = local_search;
            mLocalSearchTerminationCondition = local_search_termination_condition;

            if (mSolutionGenerator == null || mLocalSearch == null || mLocalSearchTerminationCondition == null)
            {
                throw new NullReferenceException();
            }
        }
        public GRASP(int[] masks, int dimension, TerminationEvaluationMethod local_search_should_terminate, GreedyConstructSolutionMethod solution_constructor)
        {
            mDimension = dimension;

            mLocalSearchShouldTerminate = local_search_should_terminate;

            mMasks = (int[])masks.Clone();
            mGreedySolutionConstructor = solution_constructor;
            if (mGreedySolutionConstructor == null)
            {
                mGreedySolutionConstructor = (evaluate, constraints, problem_size) =>
                {
                    double[] solution = new double[problem_size];
                    for (int i = 0; i < solution.Length; ++i)
                    {
                        solution[i] = -1;
                    }
                    for (int i = 0; i < problem_size; ++i)
                    {
                        int    best_feature_value = -1;
                        double min_cost           = double.MaxValue;
                        for (int bit_value = 0; bit_value < 2; ++bit_value)
                        {
                            solution[i] = bit_value;

                            double cost = evaluate(solution, mLowerBounds, mUpperBounds, constraints);     //evaluate partial solution
                            if (min_cost > cost)
                            {
                                min_cost           = cost;
                                best_feature_value = bit_value;
                            }
                        }
                    }
                    return(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;

            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_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);
        }
예제 #6
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);
        }
        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);
        }
예제 #8
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);
        }
예제 #9
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_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);
        }
예제 #11
0
 public override ContinuousSolution Minimize(double[] x_0, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, TerminationEvaluationMethod should_terminate, object constraints = null)
 {
     throw new NotImplementedException();
 }
예제 #12
0
        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;
            int vertex_count = dimension + 1;

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


            List <ContinuousSolution> solutions = new List <ContinuousSolution>();

            for (int i = 0; i < vertex_count; ++i)
            {
                double[] temp = (double[])x_0.Clone();
                if (i == vertex_count - 1)
                {
                    temp[0] -= 1.0;
                }
                else
                {
                    temp[i] += 1.0;
                }

                solutions.Add(new ContinuousSolution(temp, evaluate(temp, mLowerBounds, mUpperBounds, constraints)));
            }

            ContinuousSolution centroid = new ContinuousSolution();

            double?improvement = null;
            int    iteration   = 0;

            while (!should_terminate(improvement, iteration))
            {
                Sort(solutions);
                CalculateCentroid(solutions, centroid);

                double[] x_r;
                Reflect(solutions, centroid, mAlpha, out x_r);
                double fx_r = evaluate(x_r, mLowerBounds, mUpperBounds, constraints);
                if (fx_r >= solutions[0].Cost && fx_r < solutions[solutions.Count - 2].Cost)
                {
                    ContinuousSolution last_solution = solutions[solutions.Count - 1];
                    last_solution.Values = x_r;
                    last_solution.Cost   = fx_r;
                }
                else if (fx_r < solutions[0].Cost)
                {
                    double[] x_e;
                    Expand(solutions, centroid, mGamma, out x_e);
                    double fx_e = evaluate(x_e, mLowerBounds, mUpperBounds, constraints);
                    if (fx_e < fx_r)
                    {
                        ContinuousSolution last_solution = solutions[solutions.Count - 1];
                        last_solution.Values = x_e;
                        last_solution.Cost   = fx_e;
                    }
                    else
                    {
                        ContinuousSolution last_solution = solutions[solutions.Count - 1];
                        last_solution.Values = x_r;
                        last_solution.Cost   = fx_r;
                    }
                }
                else
                {
                    double[] x_c;
                    Contract(solutions, centroid, mRho, out x_c);
                    double fx_c = evaluate(x_c, mLowerBounds, mUpperBounds, constraints);
                    if (fx_c < solutions[solutions.Count - 1].Cost)
                    {
                        ContinuousSolution last_solution = solutions[solutions.Count - 1];
                        last_solution.Values = x_c;
                        last_solution.Cost   = fx_c;
                    }
                    else
                    {
                        Reduce(solutions, mSigma);
                        int solution_count = solutions.Count;
                        for (int i = 1; i < solution_count; ++i)
                        {
                            ContinuousSolution s = solutions[i];
                            s.Cost = evaluate(s.Values, mLowerBounds, mUpperBounds, constraints);
                        }
                    }
                }

                if (best_solution.TryUpdateSolution(solutions[0].Values, solutions[0].Cost, out improvement))
                {
                    OnSolutionUpdated(best_solution, iteration);
                }
                OnStepped(solutions[0], 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(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);
        }
예제 #15
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 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);
        }
예제 #17
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);
        }
        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);
        }
 public abstract ContinuousSolution Minimize(CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, TerminationEvaluationMethod should_terminate, object constraints = null);
        public override ContinuousSolution Minimize(double[] x_0, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_gradient, TerminationEvaluationMethod should_terminate, object constraints = null)
        {
            int dimension = x_0.Length;

            double[][] h      = new double[dimension][];
            double[]   lambda = new double[dimension];
            for (int i = 0; i < dimension; ++i)
            {
                h[i] = new double[dimension];
                for (int j = 0; j < dimension; ++j)
                {
                    h[i][j] = i == j ? 1.0 : 0.0;
                }
            }

            double[][] x  = new double[dimension + 1][];
            double[]   fx = new double[dimension + 1];

            for (int i = 0; i <= dimension; ++i)
            {
                x[i] = new double[dimension];
            }
            for (int d = 0; d < dimension; ++d)
            {
                x[0][d] = x_0[d];
                fx[0]   = evaluate(x[0], mLowerBounds, mUpperBounds, constraints);
            }

            ContinuousSolution best_solution = new ContinuousSolution(x[0], fx[0]);

            double?improvement = null;
            int    iteration   = 0;

            while (!should_terminate(improvement, iteration))
            {
                for (int i = 1; i <= dimension; ++i)
                {
                    LineSearch(x[i - 1], fx[i - 1], h[i - 1], out x[i], out fx[i], out lambda[i - 1], evaluate, calc_gradient, mLowerBounds, mUpperBounds, constraints);
                    for (int j = 0; j < dimension - 1; ++j)
                    {
                        for (int d = 0; d < dimension; ++d)
                        {
                            h[j][d] = h[j + 1][d];
                        }
                    }
                    for (int d = 0; d < dimension; ++d)
                    {
                        h[dimension - 1][d] = x[dimension][d] - x[0][d];
                    }
                    LineSearch(x[0], fx[0], h[dimension - 1], out x[0], out fx[0], out lambda[dimension - 1], evaluate, calc_gradient, mLowerBounds, mUpperBounds, constraints);
                    if (best_solution.TryUpdateSolution(x[0], fx[0], out improvement))
                    {
                        OnSolutionUpdated(best_solution, iteration);
                    }
                }
                OnStepped(new ContinuousSolution(x[0], fx[0]), iteration);
                iteration++;
            }

            return(best_solution);
        }