/// ------------------------------------------------------ /// SwarmOps - Numeric and heuristic optimization for C# /// Copyright (C) 2003-2011 Magnus Erik Hvass Pedersen. /// Please see the file license.txt for license details. /// SwarmOps on the internet: http://www.Hvass-Labs.org/ /// ------------------------------------------------------ using System.Diagnostics; namespace SwarmOps.Optimizers { /// /// Variants of crossover operator for Differential Evolution (DE), /// originally due to Storner and Price (1). /// /// /// References: /// (1) R. Storn and K. Price. Differential evolution - a simple /// and efficient heuristic for global optimization over /// continuous spaces. Journal of Global Optimization, /// 11:341-359, 1997. /// public static class DECrossover { #region Variants. /// /// Enumeration of DE crossover variants. /// public enum Variant { Rand1Bin, Best1Bin, } /// /// Name of DE crossover variant. /// /// /// public static string Name(Variant variant) { string s; switch (variant) { case Variant.Best1Bin: s = "Best1Bin"; break; case Variant.Rand1Bin: s = "Rand1Bin"; break; default: s = "Unknown"; break; } return s; } #endregion #region Crossover /// /// Perform DE crossover. /// /// Crossover variant to be performed. /// Crossover probability. /// Dimensionality for problem. /// Differential weight (vector). /// Current agent position. /// Potentially new agent position. /// Population's best known position. /// Entire population. /// Random-set used for drawing distinct agents. public static void DoCrossover(Variant crossover, double CR, int n, double[] w, double[] x, ref double[] y, double[] g, double[][] agents, RandomOps.Set randomSet) { // Agents used in crossover. double[] a, b, c; switch (crossover) { case Variant.Best1Bin: { // The first agent used in crossover is g. a = g; // Pick random and distinct agent-indices. // Also distinct from agent x. int R1 = randomSet.Draw(); int R2 = randomSet.Draw(); b = agents[R1]; c = agents[R2]; } break; case Variant.Rand1Bin: default: { // Pick random and distinct agent-indices. // Also distinct from agent x. int R1 = randomSet.Draw(); int R2 = randomSet.Draw(); int R3 = randomSet.Draw(); // Refer to the randomly picked agents as a and b. a = agents[R1]; b = agents[R2]; c = agents[R3]; } break; } // Pick a random dimension. int R = Globals.Random.Index(n); // Compute potentially new position. for (int k = 0; k < n; k++) { if (k == R || Globals.Random.Bool(CR)) { y[k] = a[k] + w[k] * (b[k] - c[k]); } else { y[k] = x[k]; } } } #endregion } }