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 void CalculateCentroid(List <ContinuousSolution> solutions, ContinuousSolution centroid) { int solution_count = solutions.Count - 1; int dimension = solutions[0].Values.Length; double[] x_centroid = new double[dimension]; for (int d = 0; d < dimension; ++d) { x_centroid[d] = 0; } for (int i = 0; i < solution_count; ++i) { ContinuousSolution s = solutions[i]; double[] x = s.Values; for (int d = 0; d < dimension; ++d) { x_centroid[d] += x[d]; } } for (int d = 0; d < dimension; ++d) { x_centroid[d] /= solution_count; } centroid.Values = x_centroid; }
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); }
public static double[] GetMutationStrategy(this ContinuousSolution s) { if (s.HasAttribute("MutationStrategy")) { return(s["MutationStrategy"] as double[]); } return(null); }
public static int GetWins(this ContinuousSolution s) { if (s.HasAttribute("Wins")) { return((int)s["Wins"]); } return(0); }
protected ContinuousSolution Mutate(ContinuousSolution solution, double[] lower_bounds, double[] upper_bounds, object constraints) { ContinuousSolution child = MutateVector(solution, lower_bounds, upper_bounds, constraints); double[] child_strategy = MutateStrategy(solution.GetMutationStrategy()); child.SetMutationStrategy(child_strategy); return(child); }
public ContinuousSolution[] Recombine(ContinuousSolution[] subset, object constraints) { ContinuousSolution a = subset[0]; ContinuousSolution b = subset[1]; ContinuousSolution d = (a - b) / 2; double[] lower_bounds = null; double[] upper_bounds = null; if (mLowerBounds == null || mUpperBounds == null) { if (constraints != null && 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 < d.Length) { throw new IndexOutOfRangeException(); } if (upper_bounds.Length < d.Length) { throw new IndexOutOfRangeException(); } ContinuousSolution[] children = new ContinuousSolution[subset.Length]; for (int i = 0; i < subset.Length; ++i) { double[] x = new double[d.Length]; for (int j = 0; j < d.Length; ++j) { int direction = RandomEngine.NextDouble() < 0.5 ? 1 : -1; double r = RandomEngine.NextDouble(); x[j] = subset[i][j] + d[j] * direction * r; x[j] = System.Math.Max(lower_bounds[j], x[j]); x[j] = System.Math.Min(upper_bounds[j], x[j]); } children[i] = new ContinuousSolution(x, double.MaxValue); } return(children); }
public static bool IsNew(this ContinuousSolution s) { if (s.HasAttribute("IsNew")) { return(Convert.ToInt32(s["IsNew"]) == 1); } else { return(false); } }
public override void Train(List <T> records) { HashSet <string> class_labels = new HashSet <string>(); foreach (T rec in records) { class_labels.Add(rec.Label); } mClassFieldLabels = class_labels.ToList(); mTheta.Clear(); int sample_count = records.Count; if (sample_count == 0) { throw new ArgumentException("sample_count==0"); } int dimension = records[0].Dimension; double[,] X = new double[sample_count, dimension]; int[] Y = new int[sample_count]; for (int i = 0; i < sample_count; ++i) { T rec = records[i]; for (int d = 0; d < dimension; ++d) { X[i, d] = rec[d]; } } double[] theta_0 = new double[dimension]; foreach (string class_label in mClassFieldLabels) { for (int i = 0; i < sample_count; ++i) { T rec = records[i]; Y[i] = rec.Label == class_label ? 1 : 0; } for (int d = 0; d < dimension; ++d) { theta_0[d] = 0; } LinearSVMCostFunction f = new LinearSVMCostFunction(X, Y, dimension, sample_count); f.C = mC; ContinuousSolution solution = mLocalSearcher.Minimize(theta_0, f, mMaxSolverIteration); mTheta[class_label] = solution.Values; } }
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)); }
protected virtual ContinuousSolution Recombine(ContinuousSolution[] pop, double[] lower_bounds, double[] upper_bounds, object constraints) { ContinuousSolution[] parents = new ContinuousSolution[mSelectedParentCount_rho]; for (int i = 0; i < mSelectedParentCount_rho; ++i) { parents[i] = BinaryTournamentSelection(pop); } HashSet <int> intersection_points = new HashSet <int>(); for (int i = 0; i < mSelectedParentCount_rho - 1; ++i) { int index = RandomEngine.NextInt(mDimension); while (intersection_points.Contains(index)) { index = RandomEngine.NextInt(mDimension); } intersection_points.Add(index); } int[] intersect_pts = intersection_points.OrderBy(s => s).ToArray(); int start_pt = 0; double[] x = new double[mDimension]; double[] child_strategy = new double[mDimension]; double[] parent_strategy = null; for (int i = 0; i < intersect_pts.Length; ++i) { int end_pt = intersect_pts[i]; parent_strategy = parents[i].GetMutationStrategy(); for (int j = start_pt; j < end_pt; ++j) { x[j] = parents[i][j]; child_strategy[j] = parent_strategy[j]; } } parent_strategy = parents[mSelectedParentCount_rho - 1].GetMutationStrategy(); for (int i = start_pt; i < mDimension; ++i) { x[i] = parents[mSelectedParentCount_rho - 1][i]; child_strategy[i] = parent_strategy[i]; } ContinuousSolution child = new ContinuousSolution(x, double.MaxValue); child.SetMutationStrategy(child_strategy); return(child); }
public static void RunMain(string[] args) { CostFunction_RosenbrockSaddle f = new CostFunction_RosenbrockSaddle(); int maxIterations = 200; DifferentialEvolution s = new DifferentialEvolution(f); s.SolutionUpdated += (best_solution, step) => { Console.WriteLine("Step {0}: Fitness = {1}", step, best_solution.Cost); }; ContinuousSolution finalSolution = s.Minimize(f, maxIterations); }
public override void Train(List <T> records) { HashSet <string> class_labels = new HashSet <string>(); foreach (T rec in records) { class_labels.Add(rec.Label); } mClassFieldLabels = class_labels.ToList(); mTheta.Clear(); mKernelCentroids.Clear(); int m = records.Count; if (m == 0) { throw new ArgumentException("sample count==0"); } int dimension = records[0].Dimension; int[] Y = new int[m]; for (int i = 0; i < m; ++i) { T rec = records[i]; mKernelCentroids.Add((double[])rec.Data.Clone()); } double[] theta_0 = new double[m + 1]; foreach (string class_label in mClassFieldLabels) { for (int i = 0; i < m; ++i) { T rec = records[i]; Y[i] = rec.Label == class_label ? 1 : 0; } for (int d = 0; d < dimension; ++d) { theta_0[d] = 0; } KernelSVMCostFunction <T> f = new KernelSVMCostFunction <T>(records, Y, m, mKernel); f.C = mC; ContinuousSolution solution = mLocalSearcher.Minimize(theta_0, f, mMaxSolverIteration); mTheta[class_label] = solution.Values; } }
public static void RunMain(string[] args) { CostFunction_RosenbrockSaddle f = new CostFunction_RosenbrockSaddle(); int maxIterations = 200; int popSize = 100; // population Size EvolutionaryProgramming s = new EvolutionaryProgramming(f, popSize); s.SolutionUpdated += (best_solution, step) => { Console.WriteLine("Step {0}: Fitness = {1}", step, best_solution.Cost); }; ContinuousSolution finalSolution = s.Minimize(f, maxIterations); }
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 void Reflect(List <ContinuousSolution> solutions, ContinuousSolution centroid, double alpha, out double[] x_r) { int dimension = centroid.Values.Length; double[] x_centroid = centroid.Values; int solution_count = solutions.Count - 1; ContinuousSolution last_solution = solutions[solution_count]; double[] x_last = last_solution.Values; x_r = new double[dimension]; for (int d = 0; d < dimension; ++d) { x_r[d] = x_centroid[d] + alpha * (x_centroid[d] - x_last[d]); } }
public void Expand(List <ContinuousSolution> solutions, ContinuousSolution centroid, double gamma, out double[] x_e) { int dimension = centroid.Values.Length; double[] x_centroid = centroid.Values; int solution_count = solutions.Count - 1; ContinuousSolution last_solution = solutions[solution_count]; double[] x_last = last_solution.Values; x_e = new double[dimension]; for (int d = 0; d < dimension; ++d) { x_e[d] = x_centroid[d] + gamma * (x_centroid[d] - x_last[d]); } }
public static void RunMain(string[] args) { CostFunction_RosenbrockSaddle f = new CostFunction_RosenbrockSaddle(); int maxIterations = 200; int mu = 30; int lambda = 20; EvolutionStrategy s = new EvolutionStrategy(f, mu, lambda); s.SolutionUpdated += (best_solution, step) => { Console.WriteLine("Step {0}: Fitness = {1}", step, best_solution.Cost); }; ContinuousSolution finalSolution = s.Minimize(f, maxIterations); }
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); }
protected bool ExploreSubsets(ref ContinuousSolution[] ref_set, CostEvaluationMethod evaluate, GradientEvaluationMethod calc_grad, object constraints) { bool is_improved = false; List <ContinuousSolution[]> subsets = SelectSubsets(ref_set); for (int i = 0; i < ref_set.Length; ++i) { ref_set[i].SetIsNew(false); } for (int i = 0; i < subsets.Count; ++i) { ContinuousSolution[] subset = subsets[i]; ContinuousSolution[] candidate_solutions = Recombine(subset, constraints); ContinuousSolution[] improvements = new ContinuousSolution[candidate_solutions.Length]; for (int j = 0; j < improvements.Length; ++j) { //double fx = evaluate(candidate_solutions[j].Values, mLowerBounds, mUpperBounds, constraints); ContinuousSolution s = mLocalSearch.Minimize(candidate_solutions[j].Values, evaluate, calc_grad, mLocalSearchTerminationCondition, constraints); //double improvement = fx - s.Cost; improvements[j] = s; } for (int j = 0; j < improvements.Length; ++j) { if (ref_set.ContainsItem(improvements[j])) { improvements[j].SetIsNew(false); } else { improvements[j].SetIsNew(true); ref_set = ref_set.OrderBy(s => s.Cost).ToArray(); if (ref_set[mReferenceSetSize - 1].Cost > improvements[j].Cost) { ref_set[mReferenceSetSize - 1] = improvements[j]; is_improved = true; } } } } return(is_improved); }
public virtual double[] Solve() { int n = A[0].Length; Random random = new Random(); double[] x_0 = new double[n]; for (int i = 0; i < n; ++i) { x_0[i] = random.NextDouble(); } ContinuousSolution s = solver.Minimize(x_0, EvaluateCost, EvaluateGradient, ShouldTerminate); mX = s.Values; UpdateStatistics(); return(X); }
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 void Reduce(List <ContinuousSolution> solutions, double sigma) { int solution_count = solutions.Count; ContinuousSolution best_solution = solutions[0]; double[] x_0 = best_solution.Values; int dimension = x_0.Length; for (int i = 1; i < solution_count; ++i) { ContinuousSolution s = solutions[i]; double[] x = s.Values; for (int d = 0; d < dimension; ++d) { x[d] = x_0[d] + sigma * (x[d] - x_0[d]); } s.Values = x; } }
protected ContinuousSolution[] SelectParents(ContinuousSolution[] pop, ContinuousSolution p0) { ContinuousSolution p1, p2, p3; do { p1 = pop[RandomEngine.NextInt(pop.Length)]; } while (p1 == p0); do { p2 = pop[RandomEngine.NextInt(pop.Length)]; } while (p2 == p1 || p2 == p0); do { p3 = pop[RandomEngine.NextInt(pop.Length)]; } while (p3 == p2 || p3 == p1 || p3 == p0); return(new ContinuousSolution[] { p1, p2, p3 }); }
protected List <ContinuousSolution[]> SelectSubsets(ContinuousSolution[] ref_set) { List <ContinuousSolution> additions = new List <ContinuousSolution>(); List <ContinuousSolution> remainders = new List <ContinuousSolution>(); for (int i = 0; i < mReferenceSetSize; ++i) { if (ref_set[i].IsNew()) { additions.Add(ref_set[i]); } else { remainders.Add(ref_set[i]); } } if (remainders.Count == 0) { foreach (ContinuousSolution s in additions) { remainders.Add(s); } } List <ContinuousSolution[]> subsets = new List <ContinuousSolution[]>(); for (int i = 0; i < additions.Count; ++i) { ContinuousSolution addition = additions[i]; for (int j = 0; j < remainders.Count; ++j) { ContinuousSolution remainder = remainders[j]; if (addition != remainder) { subsets.Add(new ContinuousSolution[] { remainder, addition }); } } } return(subsets); }
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(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); }
public void Minimize(double x_0, CostFunction f, int max_iterations) { double x = x_0; double fx = f.Evaluate(x); double newtonX = 0; double denominator = 0; ContinuousSolution best_solution = new ContinuousSolution(new double[] { x }, fx); double?improvement = null; for (int k = 0; k < max_iterations; ++k) { f.CalcGradient(x, out denominator); if (Math.Abs(denominator) < ZERO) { break; } newtonX = x - fx / denominator; if (Math.Abs(newtonX - x) < SIGMA) { break; } x = newtonX; fx = f.Evaluate(x); if (best_solution.TryUpdateSolution(new double[] { x }, fx, out improvement)) { OnSolutionUpdated(best_solution, k); } OnStepped(new ContinuousSolution(new double[] { x }, fx), k); } }
protected ContinuousSolution Reproduce(ContinuousSolution[] pop, ContinuousSolution p0, double[] lower_bounds, double[] upper_bounds) { ContinuousSolution[] parents = SelectParents(pop, p0); ContinuousSolution p1 = parents[0]; ContinuousSolution p2 = parents[1]; ContinuousSolution p3 = parents[2]; int cut_point = RandomEngine.NextInt(mDimension); ContinuousSolution child = new ContinuousSolution(mDimension); for (int i = 0; i < mDimension; ++i) { child[i] = p0[i]; if (i == cut_point || RandomEngine.NextDouble() < mCrossoverRate) { child[i] = p1[i] + mWeightingFactor * (p2[i] - p3[i]); child[i] = System.Math.Max(lower_bounds[i], child[i]); child[i] = System.Math.Min(upper_bounds[i], child[i]); } } return(child); }
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); }