File size: 4,516 Bytes
b1b3bae |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
/// ------------------------------------------------------
/// 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
{
/// <summary>
/// Variants of crossover operator for Differential Evolution (DE),
/// originally due to Storner and Price (1).
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
public static class DECrossover
{
#region Variants.
/// <summary>
/// Enumeration of DE crossover variants.
/// </summary>
public enum Variant
{
Rand1Bin,
Best1Bin,
}
/// <summary>
/// Name of DE crossover variant.
/// </summary>
/// <param name="kind"></param>
/// <returns></returns>
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
/// <summary>
/// Perform DE crossover.
/// </summary>
/// <param name="crossover">Crossover variant to be performed.</param>
/// <param name="CR">Crossover probability.</param>
/// <param name="n">Dimensionality for problem.</param>
/// <param name="w">Differential weight (vector).</param>
/// <param name="x">Current agent position.</param>
/// <param name="y">Potentially new agent position.</param>
/// <param name="g">Population's best known position.</param>
/// <param name="agents">Entire population.</param>
/// <param name="randomSet">Random-set used for drawing distinct agents.</param>
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
}
} |