protected ContinuousSolution[] Diversify(List <ContinuousSolution> diverse_set) { ContinuousSolution[] ref_set = new ContinuousSolution[mReferenceSetSize]; for (int i = 0; i < mNumElite; ++i) { ref_set[i] = diverse_set[i]; } int remainder_set_size = mDiverseSetSize - mNumElite; ContinuousSolution[] remainder_set = new ContinuousSolution[remainder_set_size]; for (int i = mNumElite; i < mDiverseSetSize; ++i) { remainder_set[i - mNumElite] = diverse_set[i]; } Dictionary <ContinuousSolution, double> remainder_distance_set = new Dictionary <ContinuousSolution, double>(); for (int i = 0; i < remainder_set_size; ++i) { double distance = 0; for (int j = 0; j < mNumElite; ++j) { distance += remainder_set[i].GetDistance2(ref_set[j]); } remainder_distance_set[remainder_set[i]] = distance; } remainder_set = remainder_set.OrderBy(s => remainder_distance_set[s]).ToArray(); for (int i = mNumElite; i < mReferenceSetSize; ++i) { ref_set[i] = remainder_set[i - mNumElite]; } return(ref_set); }
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(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); }