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);
                };
            }
        }
Esempio n. 2
0
 public Glm(GlmDistributionFamily distribution, double[][] A, double[] b, SingleTrajectoryContinuousSolver solver)
 {
     this.solver = solver;
     this.mDistributionFamily = distribution;
     this.mLinkFunc           = GetLinkFunction(distribution);
     this.A      = A;
     this.b      = b;
     this.mStats = new Statistics.GlmStatistics(A[0].Length, b.Length);
 }
        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 void AddNeighborhood(SingleTrajectoryContinuousSolver local_search, SingleTrajectoryContinuousSolver.TerminationEvaluationMethod termination_condition)
 {
     local_search.Stepped += (solution, iteration) =>
     {
         OnStepped(solution, iteration);
     };
     local_search.SolutionUpdated += (solution, iteration) =>
     {
         OnSolutionUpdated(solution, iteration);
     };
     mNeighborhoods.Add(local_search, termination_condition);
 }
        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);
        }
Esempio n. 6
0
 public Glm(GlmDistributionFamily distribution, double[,] A, double[] b, SingleTrajectoryContinuousSolver solver, int maxIters = -1)
 {
     this.solver = solver;
     this.mDistributionFamily = distribution;
     this.mLinkFunc           = GetLinkFunction(distribution);
     this.A = new double[A.GetLength(0)][];
     for (int i = 0; i < A.GetLength(0); i++)
     {
         this.A[i] = new double[A.GetLength(1)];
         for (int j = 0; j < A.GetLength(1); j++)
         {
             this.A[i][j] = A[i, j];
         }
     }
     this.b = b;
     if (maxIters > 0)
     {
         this.mMaxIters = maxIters;
     }
     this.mStats = new Statistics.GlmStatistics(A.GetLength(1), b.Length);
 }
Esempio n. 7
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);
        }
Esempio n. 8
0
 public void Add(SingleTrajectoryContinuousSolver local_search, SingleTrajectoryContinuousSolver.TerminationEvaluationMethod termination_condition)
 {
     mLocalSearches.Add(local_search);
     mTerminationConditions.Add(termination_condition);
 }