public AdaptiveRandomSearch(int search_space_size, CreateNeighborMethod generator) { mSearchSpaceSize = search_space_size; mSolutionGenerator = generator; if (mSolutionGenerator == null) { mSolutionGenerator = (x, step_size, constraints) => { 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 < x.Length) { throw new ArgumentOutOfRangeException(); } if (upper_bounds.Length < x.Length) { throw new ArgumentOutOfRangeException(); } for (int i = 0; i < x.Length; ++i) { // must check boundary ... double min = System.Math.Max(lower_bounds[i], x[i] - step_size); double max = System.Math.Min(upper_bounds[i], x[i] + step_size); double new_val = min + (max - min) * RandomEngine.NextDouble(); x[i] = new_val; } return(x); }; } }
public double[] TakeStep(CreateNeighborMethod generator, double[] x, double step_size, object constraints) { return(generator(x, step_size, constraints)); }