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);
                };
            }
        }
Example #2
0
 protected virtual double[] MutateStrategy(double[] stddevs)
 {
     double[] child_stddevs = new double[stddevs.Length];
     for (int i = 0; i < stddevs.Length; ++i)
     {
         child_stddevs[i] = stddevs[i] + RandomEngine.Gauss(0, System.Math.Sqrt(System.Math.Abs(stddevs[i])));
     }
     return(child_stddevs);
 }
        public StochasticHillClimber(double[] masks, CreateRandomNeighborhoodMethod generator = null)
        {
            mMasks             = (double[])masks.Clone();
            mSolutionGenerator = generator;
            if (mSolutionGenerator == null)
            {
                mSolutionGenerator = (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);
                };
            }
        }
        protected virtual double[] MutateStrategy(double[] stddevs)
        {
            double tau   = 1.0 / System.Math.Sqrt(2.0 * stddevs.Length);
            double tau_p = 1.0 / System.Math.Sqrt(2.0 * System.Math.Sqrt(stddevs.Length));

            double[] child_stddevs = new double[stddevs.Length];
            for (int i = 0; i < stddevs.Length; ++i)
            {
                child_stddevs[i] = stddevs[i] * System.Math.Exp(RandomEngine.Gauss(0, tau_p) + RandomEngine.Gauss(0, tau));
            }
            return(child_stddevs);
        }
Example #5
0
        protected virtual ContinuousSolution MutateVector(ContinuousSolution solution, double[] lower_bounds, double[] upper_bounds, object constraints)
        {
            double[] x        = new double[mDimension];
            double[] strategy = solution.GetMutationStrategy();
            for (int i = 0; i < mDimension; ++i)
            {
                x[i] = solution[i] + RandomEngine.Gauss(0, strategy[i]);
                x[i] = System.Math.Max(lower_bounds[i], x[i]);
                x[i] = System.Math.Min(upper_bounds[i], x[i]);
            }

            return(new ContinuousSolution(x, double.MaxValue));
        }