Changeset 1218 for trunk/sources/HeuristicLab.RealVector
- Timestamp:
- 02/16/09 01:21:53 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.RealVector
- Files:
-
- 4 deleted
- 11 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.RealVector/BlendAlphaBetaCrossover.cs
r1184 r1218 34 34 /// solution by the factor alpha, into the direction of the 'worse' solution by the factor beta. 35 35 /// </summary> 36 public class BlendAlphaBetaCrossover : CrossoverBase {36 public class BlendAlphaBetaCrossover : RealVectorCrossoverBase { 37 37 /// <inheritdoc select="summary"/> 38 38 public override string Description { … … 45 45 /// <summary> 46 46 /// Initializes a new instance of <see cref="BlendAlphaBetaCrossover"/> with five variable infos 47 /// (<c>Maximization</c>, <c>Quality</c>, <c> RealVector</c>, <c>Alpha</c> and <c>Beta</c>).47 /// (<c>Maximization</c>, <c>Quality</c>, <c>Alpha</c> and <c>Beta</c>). 48 48 /// </summary> 49 49 public BlendAlphaBetaCrossover() … … 51 51 AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In)); 52 52 AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In)); 53 AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));54 53 VariableInfo alphaVarInfo = new VariableInfo("Alpha", "Value for alpha", typeof(DoubleData), VariableKind.In); 55 54 alphaVarInfo.Local = true; … … 110 109 111 110 /// <summary> 112 /// Performs a blend alpha beta crossover o f tworeal vectors.111 /// Performs a blend alpha beta crossover operation for two given parent real vectors. 113 112 /// </summary> 113 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 114 114 /// <param name="scope">The current scope.</param> 115 /// <param name="random"> Therandom number generator.</param>116 /// <param name="parent 1">The first parent for the crossover.</param>117 /// < param name="parent2">The second parent for the crossover.</param>118 /// <param name="child">The created child generated through the blend alpha beta crossover.</param>119 protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {115 /// <param name="random">A random number generator.</param> 116 /// <param name="parents">An array containing the two real vectors that should be crossed.</param> 117 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 118 protected override double[] Cross(IScope scope, IRandom random, double[][] parents) { 119 if (parents.Length != 2) throw new InvalidOperationException("ERROR in BlendAlphaBetaCrossover: The number of parents is not equal to 2"); 120 120 bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data; 121 DoubleArrayData vector1 = parent1.GetVariableValue<DoubleArrayData>("RealVector", false); 122 DoubleData quality1 = parent1.GetVariableValue<DoubleData>("Quality", false); 123 DoubleArrayData vector2 = parent2.GetVariableValue<DoubleArrayData>("RealVector", false); 124 DoubleData quality2 = parent2.GetVariableValue<DoubleData>("Quality", false); 121 double quality1 = scope.SubScopes[0].GetVariableValue<DoubleData>("Quality", false).Data; 122 double quality2 = scope.SubScopes[1].GetVariableValue<DoubleData>("Quality", false).Data; 125 123 double alpha = GetVariableValue<DoubleData>("Alpha", scope, true).Data; 126 124 double beta = GetVariableValue<DoubleData>("Beta", scope, true).Data; 127 125 128 if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to real vectors of different length."); 129 130 double[] result = Apply(random, maximization, vector1.Data, quality1.Data, vector2.Data, quality2.Data, alpha, beta); 131 child.AddVariable(new Variable(child.TranslateName("RealVector"), new DoubleArrayData(result))); 126 return Apply(random, maximization, parents[0], quality1, parents[1], quality2, alpha, beta); 132 127 } 133 128 } -
trunk/sources/HeuristicLab.RealVector/BlendAlphaCrossover.cs
r1184 r1218 78 78 79 79 /// <summary> 80 /// Performs a blend alpha crossover o f tworeal vectors.80 /// Performs a blend alpha crossover operation for two given parent real vectors. 81 81 /// </summary> 82 /// < remarks>Calls <see cref="Apply"/>.</remarks>82 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 83 83 /// <param name="scope">The current scope.</param> 84 /// <param name="random"> Therandom number generator.</param>85 /// <param name="parent 1">The first parent for the crossover operation.</param>86 /// < param name="parent2">The second parent for the crossover operation.</param>87 /// <returns>The newly created real vector, resulting from the blend alpha crossover.</returns>88 protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {84 /// <param name="random">A random number generator.</param> 85 /// <param name="parents">An array containing the two real vectors that should be crossed.</param> 86 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 87 protected override double[] Cross(IScope scope, IRandom random, double[][] parents) { 88 if (parents.Length != 2) throw new InvalidOperationException("ERROR in BlendAlphaCrossover: The number of parents is not equal to 2"); 89 89 double alpha = GetVariableValue<DoubleData>("Alpha", scope, true).Data; 90 return Apply(random, parent 1, parent2, alpha);90 return Apply(random, parents[0], parents[1], alpha); 91 91 } 92 92 } -
trunk/sources/HeuristicLab.RealVector/CompleteContinuousCrossover.cs
r1184 r1218 29 29 /// <summary> 30 30 /// Complete continuous crossover for real vectors: for each element of the new vector the average 31 /// of bothparents is calculated.31 /// of all parents is calculated. 32 32 /// </summary> 33 33 public class CompleteContinuousCrossover : RealVectorCrossoverBase { … … 38 38 39 39 /// <summary> 40 /// Performs a complete continuous crossover of the twogiven real vectors.40 /// Performs a complete continuous crossover of multiple given real vectors. 41 41 /// </summary> 42 42 /// <param name="random">The random number generator.</param> 43 /// <param name="parent1">The first parent for the crossover.</param> 44 /// <param name="parent2">The second parent for the crossover.</param> 43 /// <param name="parents">An array containing the parents that should be crossed.</param> 45 44 /// <returns>The newly created real vector, resulting from the complete continuous crossover.</returns> 46 public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {47 int length = parent 1.Length;45 public static double[] Apply(IRandom random, double[][] parents) { 46 int length = parents[0].Length; 48 47 double[] result = new double[length]; 49 50 for (int i = 0; i < length; i++) 51 result[i] = (parent1[i] + parent2[i]) / 2; 48 for (int i = 0; i < length; i++) { 49 double sum = 0.0; 50 for (int j = 0; j < parents.Length; j++) 51 sum += parents[j][i]; 52 result[i] = sum / parents.Length; 53 } 52 54 return result; 53 55 } 54 56 55 57 /// <summary> 56 /// Performs a complete continuous crossover o f the two givenreal vectors.58 /// Performs a complete continuous crossover operation for multiple given parent real vectors. 57 59 /// </summary> 58 /// < remarks>Calls <see cref="Apply"/>.</remarks>60 /// <exception cref="InvalidOperationException">Thrown if there are less than two parents.</exception> 59 61 /// <param name="scope">The current scope.</param> 60 /// <param name="random"> Therandom number generator.</param>61 /// <param name="parent 1">The first parent for the crossover.</param>62 /// < param name="parent2">The second parent for the crossover.</param>63 /// <returns>The newly created real vector, resulting from the complete continuous crossover.</returns>64 protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {65 return Apply(random, parent 1, parent2);62 /// <param name="random">A random number generator.</param> 63 /// <param name="parents">An array containing the real vectors that should be crossed.</param> 64 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 65 protected override double[] Cross(IScope scope, IRandom random, double[][] parents) { 66 if (parents.Length < 2) throw new InvalidOperationException("ERROR in CompleteContinuousCrossover: The number of parents is less than 2"); 67 return Apply(random, parents); 66 68 } 67 69 } -
trunk/sources/HeuristicLab.RealVector/ContinuousCrossover.cs
r1184 r1218 28 28 namespace HeuristicLab.RealVector { 29 29 /// <summary> 30 /// Continuous crossover for real vectors: for each element randomlyeither the element of the first31 /// parent or the average of both parents is selected.30 /// Continuous crossover for real vectors: for each element either the element of the first 31 /// parent or the average of all parents is selected randomly. 32 32 /// </summary> 33 33 public class ContinuousCrossover : RealVectorCrossoverBase { … … 38 38 39 39 /// <summary> 40 /// Performs a continuous crossover of the twogiven real vectors.40 /// Performs a continuous crossover of multiple given real vectors. 41 41 /// </summary> 42 42 /// <param name="random">The random number generator.</param> 43 /// <param name="parent1">The first parent for the crossover.</param> 44 /// <param name="parent2">The second parent for the crossover.</param> 43 /// <param name="parents">An array containing the parents that should be crossed.</param> 45 44 /// <returns>The newly created real vector, resulting from the continuous crossover.</returns> 46 public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {47 int length = parent 1.Length;45 public static double[] Apply(IRandom random, double[][] parents) { 46 int length = parents[0].Length; 48 47 double[] result = new double[length]; 49 48 50 49 for (int i = 0; i < length; i++) { 51 50 if (random.NextDouble() < 0.5) { 52 result[i] = (parent1[i] + parent2[i]) / 2; 51 double sum = 0.0; 52 for (int j = 0; j < parents.Length; j++) 53 sum += parents[j][i]; 54 result[i] = sum / parents.Length; 53 55 } else { 54 result[i] = parent 1[i];56 result[i] = parents[0][i]; 55 57 } 56 58 } … … 59 61 60 62 /// <summary> 61 /// Performs a continuous crossover o f the two givenreal vectors.63 /// Performs a continuous crossover operation for multiple given parent real vectors. 62 64 /// </summary> 63 /// < remarks>Calls <see cref="Apply"/>.</remarks>65 /// <exception cref="InvalidOperationException">Thrown if there are less than two parents.</exception> 64 66 /// <param name="scope">The current scope.</param> 65 /// <param name="random"> Therandom number generator.</param>66 /// <param name="parent 1">The first parent for the crossover.</param>67 /// < param name="parent2">The second parent for the crossover.</param>68 /// <returns>The newly created real vector, resulting from the continuous crossover.</returns>69 protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {70 return Apply(random, parent 1, parent2);67 /// <param name="random">A random number generator.</param> 68 /// <param name="parents">An array containing the real vectors that should be crossed.</param> 69 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 70 protected override double[] Cross(IScope scope, IRandom random, double[][] parents) { 71 if (parents.Length < 2) throw new InvalidOperationException("ERROR in ContinuousCrossover: The number of parents is less than 2"); 72 return Apply(random, parents); 71 73 } 72 74 } -
trunk/sources/HeuristicLab.RealVector/DiscreteCrossover.cs
r1184 r1218 27 27 namespace HeuristicLab.RealVector { 28 28 /// <summary> 29 /// Discrete crossover for real vectors: Selects randomly either the value of the first or the30 /// second parent.29 /// Discrete crossover for real vectors: For each position in the new vector an allele 30 /// of one of the parents is randomly selected. 31 31 /// </summary> 32 32 public class DiscreteCrossover : RealVectorCrossoverBase { 33 33 /// <inheritdoc select="summary"/> 34 34 public override string Description { 35 get { return "Discrete crossover for real vectors."; }35 get { return @"Discrete crossover for real vectors: creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent."; } 36 36 } 37 37 38 38 /// <summary> 39 /// Performs a discrete crossover operation o f the twogiven parents.39 /// Performs a discrete crossover operation on multiple given parents. 40 40 /// </summary> 41 41 /// <param name="random">A random number generator.</param> 42 /// <param name="parent1">The first parent for the crossover operation.</param> 43 /// <param name="parent2">The second parent for the crossover operation.</param> 42 /// <param name="parents">An array containing the parents that should be crossed.</param> 44 43 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 45 public static double[] Apply(IRandom random, double[] parent1, double[] parent2) {46 int length = parent 1.Length;44 public static double[] Apply(IRandom random, double[][] parents) { 45 int length = parents[0].Length; 47 46 double[] result = new double[length]; 48 49 47 for (int i = 0; i < length; i++) { 50 if (random.NextDouble() < 0.5) 51 result[i] = parent1[i]; 52 else 53 result[i] = parent2[i]; 48 result[i] = parents[random.Next(parents.Length)][i]; 54 49 } 55 50 return result; … … 57 52 58 53 /// <summary> 59 /// Performs a discrete crossover operation of the two given parents.54 /// Performs a discrete crossover operation for multiple given parent real vectors. 60 55 /// </summary> 56 /// <exception cref="InvalidOperationException">Thrown if there are less than two parents.</exception> 61 57 /// <param name="scope">The current scope.</param> 62 58 /// <param name="random">A random number generator.</param> 63 /// <param name="parent1">The first parent for the crossover operation.</param> 64 /// <param name="parent2">The second parent for the crossover operation.</param> 59 /// <param name="parents">An array containing the real vectors that should be crossed.</param> 65 60 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 66 protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) { 67 return Apply(random, parent1, parent2); 61 protected override double[] Cross(IScope scope, IRandom random, double[][] parents) { 62 if (parents.Length < 2) throw new InvalidOperationException("ERROR in DiscreteCrossover: The number of parents is less than 2"); 63 return Apply(random, parents); 68 64 } 69 65 } -
trunk/sources/HeuristicLab.RealVector/HeuristicCrossover.cs
r1184 r1218 32 32 /// of the two parents times a randomly chosen factor. 33 33 /// </summary> 34 public class HeuristicCrossover : CrossoverBase {34 public class HeuristicCrossover : RealVectorCrossoverBase { 35 35 /// <summary> 36 /// Initializes a new instance of <see cref="HeuristicCrossover"/> with t hreevariable infos37 /// (<c>Maximization</c> , <c>Quality</c> and <c>RealVector</c>).36 /// Initializes a new instance of <see cref="HeuristicCrossover"/> with two variable infos 37 /// (<c>Maximization</c> and <c>Quality</c>). 38 38 /// </summary> 39 39 public HeuristicCrossover() … … 41 41 AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In)); 42 42 AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In)); 43 AddVariableInfo(new VariableInfo("RealVector", "Parent and child real vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New));44 43 } 45 44 … … 74 73 75 74 /// <summary> 76 /// Perfo mrs a heuristic crossover on the two given parents.75 /// Performs a heuristic crossover operation for two given parent real vectors. 77 76 /// </summary> 78 /// <exception cref="InvalidOperationException">Thrown when the parent vectors have different lengths.</exception>77 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 79 78 /// <param name="scope">The current scope.</param> 80 /// <param name="random"> Therandom number generator.</param>81 /// <param name="parent 1">The first parent for the crossover operation.</param>82 /// < param name="parent2">The second parent for the crossover operation.</param>83 /// <param name="child">The newly created real vector, resulting from the heuristic crossover.</param>84 protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {79 /// <param name="random">A random number generator.</param> 80 /// <param name="parents">An array containing the two real vectors that should be crossed.</param> 81 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 82 protected override double[] Cross(IScope scope, IRandom random, double[][] parents) { 83 if (parents.Length != 2) throw new InvalidOperationException("ERROR in HeuristicCrossover: The number of parents is not equal to 2"); 85 84 bool maximization = GetVariableValue<BoolData>("Maximization", scope, true).Data; 86 DoubleArrayData vector1 = parent1.GetVariableValue<DoubleArrayData>("RealVector", false); 87 DoubleData quality1 = parent1.GetVariableValue<DoubleData>("Quality", false); 88 DoubleArrayData vector2 = parent2.GetVariableValue<DoubleArrayData>("RealVector", false); 89 DoubleData quality2 = parent2.GetVariableValue<DoubleData>("Quality", false); 85 double quality1 = scope.SubScopes[0].GetVariableValue<DoubleData>("Quality", false).Data; 86 double quality2 = scope.SubScopes[1].GetVariableValue<DoubleData>("Quality", false).Data; 90 87 91 if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to real vectors of different length."); 92 93 double[] result = Apply(random, maximization, vector1.Data, quality1.Data, vector2.Data, quality2.Data); 94 child.AddVariable(new Variable(child.TranslateName("RealVector"), new DoubleArrayData(result))); 88 return Apply(random, maximization, parents[0], quality1, parents[1], quality2); 95 89 } 96 90 } -
trunk/sources/HeuristicLab.RealVector/HeuristicLab.RealVector.csproj
r1159 r1218 65 65 <Compile Include="ContinuousManipulator.cs" /> 66 66 <Compile Include="DiscreteCrossover.cs" /> 67 <Compile Include="DiscreteMultiCrossover.cs" />68 67 <Compile Include="HeuristicCrossover.cs" /> 69 <Compile Include="IntermediateMultiCrossover.cs" />70 68 <Compile Include="MichalewiczNonUniformAllPositionsManipulator.cs" /> 71 69 <Compile Include="MichalewiczNonUniformOnePositionManipulator.cs" /> 72 70 <Compile Include="RandomConvexCrossover.cs" /> 73 71 <Compile Include="LocalCrossover.cs" /> 74 <Compile Include="RealVectorMultiCrossoverBase.cs" /> 75 <Compile Include="RealVectorSelfAdaptiveMultiCrossoverBase.cs" /> 76 <Compile Include="SelfAdaptiveDiscreteMultiCrossover.cs" /> 72 <Compile Include="SelfAdaptiveDiscreteCrossover.cs" /> 77 73 <Compile Include="SelfAdaptiveNormalAllPositionsManipulator.cs" /> 78 74 <Compile Include="UniformAllPositionsManipulator.cs" /> -
trunk/sources/HeuristicLab.RealVector/LocalCrossover.cs
r1184 r1218 57 57 58 58 /// <summary> 59 /// Performs a local crossover o n the two given parentvectors.59 /// Performs a local crossover operation for two given parent real vectors. 60 60 /// </summary> 61 /// < remarks>Calls <see cref="Apply"/>.</remarks>61 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 62 62 /// <param name="scope">The current scope.</param> 63 /// <param name="random"> Therandom number generator.</param>64 /// <param name="parent 1">The first parent for the crossover operation.</param>65 /// < param name="parent2">The second parent for the crossover operation.</param>66 /// <returns>The newly created real vector, resulting from the local crossover.</returns>67 protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {68 return Apply(random, parent 1, parent2);63 /// <param name="random">A random number generator.</param> 64 /// <param name="parents">An array containing the two real vectors that should be crossed.</param> 65 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 66 protected override double[] Cross(IScope scope, IRandom random, double[][] parents) { 67 if (parents.Length != 2) throw new InvalidOperationException("ERROR in LocalCrossover: The number of parents is not equal to 2"); 68 return Apply(random, parents[0], parents[1]); 69 69 } 70 70 } -
trunk/sources/HeuristicLab.RealVector/RandomConvexCrossover.cs
r1184 r1218 56 56 57 57 /// <summary> 58 /// Performs a random convex crossover o n the two given parents.58 /// Performs a random convex crossover operation for two given parent real vectors. 59 59 /// </summary> 60 /// < remarks>Calls <see cref="Apply"/>.</remarks>60 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 61 61 /// <param name="scope">The current scope.</param> 62 /// <param name="random"> Therandom number generator.</param>63 /// <param name="parent 1">The first parent vector for the crossover.</param>64 /// < param name="parent2">The second parent vector for the crossover.</param>65 /// <returns>The newly created real vector, resulting from the random convex crossover.</returns>66 protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {67 return Apply(random, parent 1, parent2);62 /// <param name="random">A random number generator.</param> 63 /// <param name="parents">An array containing the two real vectors that should be crossed.</param> 64 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 65 protected override double[] Cross(IScope scope, IRandom random, double[][] parents) { 66 if (parents.Length != 2) throw new InvalidOperationException("ERROR in RandomConvexCrossover: The number of parents is not equal to 2"); 67 return Apply(random, parents[0], parents[1]); 68 68 } 69 69 } -
trunk/sources/HeuristicLab.RealVector/RealVectorCrossoverBase.cs
r1184 r1218 42 42 43 43 /// <summary> 44 /// Performs a crossover of two given parents. 44 /// Performs a crossover by calling <see cref="Cross(HeuristicLab.Core.IScope, HeuristicLab.Core.IRandom, double[][]"/> 45 /// and adds the created real vector to the current scope. 46 /// </summary> 47 /// <exception cref="InvalidOperationException">Thrown if the parents have different lengths.</exception> 48 /// <param name="scope">The current scope which represents a new child.</param> 49 /// <param name="random">A random number generator.</param> 50 protected sealed override void Cross(IScope scope, IRandom random) { 51 double[][] parents = new double[scope.SubScopes.Count][]; 52 int length = -1; 53 for (int i = 0; i < scope.SubScopes.Count; i++) { 54 parents[i] = scope.SubScopes[i].GetVariableValue<DoubleArrayData>("RealVector", false).Data; 55 if (i == 0) length = parents[i].Length; 56 else if (parents[i].Length != length) throw new InvalidOperationException("ERROR in RealVectorCrossoverBase: Cannot apply crossover to real vectors of different length"); 57 } 58 59 double[] result = Cross(scope, random, parents); 60 scope.AddVariable(new Variable(scope.TranslateName("RealVector"), new DoubleArrayData(result))); 61 } 62 63 /// <summary> 64 /// Performs a crossover of multiple real vectors. 45 65 /// </summary> 46 66 /// <param name="scope">The current scope.</param> 47 67 /// <param name="random">A random number generator.</param> 48 /// <param name="parent1">The first parent for crossover.</param> 49 /// <param name="parent2">The second parent for crossover.</param> 50 /// <param name="child">The resulting child scope.</param> 51 protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) { 52 DoubleArrayData vector1 = parent1.GetVariableValue<DoubleArrayData>("RealVector", false); 53 DoubleArrayData vector2 = parent2.GetVariableValue<DoubleArrayData>("RealVector", false); 54 55 if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to real vectors of different length."); 56 57 double[] result = Cross(scope, random, vector1.Data, vector2.Data); 58 child.AddVariable(new Variable(child.TranslateName("RealVector"), new DoubleArrayData(result))); 59 } 60 61 /// <summary> 62 /// Performs a crossover of two given parents. 63 /// </summary> 64 /// <param name="scope">The current scope.</param> 65 /// <param name="random">A random number generator.</param> 66 /// <param name="parent1">The first parent for crossover.</param> 67 /// <param name="parent2">The second parent for crossover.</param> 68 /// <param name="parents">An array containing all parent real vectors.</param> 68 69 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 69 protected abstract double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2);70 protected abstract double[] Cross(IScope scope, IRandom random, double[][] parents); 70 71 } 71 72 } -
trunk/sources/HeuristicLab.RealVector/SelfAdaptiveDiscreteCrossover.cs
r1217 r1218 28 28 namespace HeuristicLab.RealVector { 29 29 /// <summary> 30 /// Creates a new offspring by combining the alleles in the parents such that each allele is30 /// Discrete crossover for real vectors: creates a new offspring by combining the alleles in the parents such that each allele is 31 31 /// randomly selected from one parent. It will also use the same strategy to combine the endogenous 32 /// strategy parameter vector. 32 /// strategy parameter vector. 33 33 /// </summary> 34 public class SelfAdaptiveDiscrete MultiCrossover : RealVectorSelfAdaptiveMultiCrossoverBase {34 public class SelfAdaptiveDiscreteCrossover : RealVectorCrossoverBase { 35 35 /// <inheritdoc select="summary"/> 36 36 public override string Description { 37 37 get { 38 return @" Thiscreates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent. It will also use the same strategy to combine the endogenous strategy parameter vector.";38 return @"Discrete crossover for real vectors: creates a new offspring by combining the alleles in the parents such that each allele is randomly selected from one parent. It will also use the same strategy to combine the endogenous strategy parameter vector."; 39 39 } 40 40 } 41 41 42 42 /// <summary> 43 /// Performs a self adaptive discrete multiple crossover on the given list of <paramref name="parents"/>. 43 /// Initializes a new instance of <see cref="SelfAdaptiveDiscreteCrossover"/> with one additional 44 /// variable infos (<c>StrategyVector</c>). 44 45 /// </summary> 45 /// <exception cref="InvalidOperationException">Thrown when the parent vectors have different lengths.</exception> 46 /// <param name="random">The random number generator.</param> 47 /// <param name="parents">The list of parents to crossover.</param> 48 /// <param name="strategyParametersList">The strategy parameter list.</param> 49 /// <param name="childIndividual">Output parameter; the created child.</param> 50 /// <param name="strategyParameters">Output parameter; endogenous strategy parameters.</param> 51 public static void Apply(IRandom random, IList<double[]> parents, IList<double[]> strategyParametersList, out double[] childIndividual, out double[] strategyParameters) { 52 childIndividual = new double[parents[0].Length]; 53 strategyParameters = new double[strategyParametersList[0].Length]; 54 try { 55 for (int i = 0; i < childIndividual.Length; i++) { 56 int nextParent = random.Next(parents.Count); 57 childIndividual[i] = parents[nextParent][i]; 58 strategyParameters[i] = strategyParametersList[nextParent][i]; 59 } 60 } catch (IndexOutOfRangeException) { 61 throw new InvalidOperationException("Cannot apply self adaptive multicrossover to real vectors of different length."); 46 public SelfAdaptiveDiscreteCrossover() 47 : base() { 48 AddVariableInfo(new VariableInfo("StrategyVector", "Endogenous strategy parameter vector", typeof(DoubleArrayData), VariableKind.In | VariableKind.New)); 49 } 50 51 /// <summary> 52 /// Performs a discrete crossover operation for multiple given parent real vectors that combines the strategy vectors in the same way. 53 /// </summary> 54 /// <param name="random">A random number generator.</param> 55 /// <param name="parents">An array containing the parents that should be crossed.</param> 56 /// <param name="strategies">An array containing the strategy vectors of the parents.</param> 57 /// <param name="child">The newly created real vector, resulting from the crossover operation (output parameter).</param> 58 /// <param name="strategy">The newly created strategy vector, resulting from the crossover operation (output parameter).</param> 59 public static void Apply(IRandom random, double[][] parents, double[][] strategies, out double[] child, out double[] strategy) { 60 child = new double[parents[0].Length]; 61 strategy = new double[strategies[0].Length]; 62 for (int i = 0; i < child.Length; i++) { 63 int nextParent = random.Next(parents.Length); 64 child[i] = parents[nextParent][i]; 65 strategy[i] = strategies[nextParent][i]; 62 66 } 63 67 } 64 68 65 69 /// <summary> 66 /// Performs a self adaptive discrete multiple crossover on the given list of <paramref name="parents"/>.70 /// Performs a discrete crossover operation for multiple given parent real vectors that combines the strategy vectors in the same way. 67 71 /// </summary> 68 /// < remarks>Calls <see cref="Apply"/>.</remarks>72 /// <exception cref="InvalidOperationException">Thrown if there are less than two parents or if the length of the strategy vectors is not the same as the length of the real vectors.</exception> 69 73 /// <param name="scope">The current scope.</param> 70 /// <param name="random">The random number generator.</param> 71 /// <param name="parents">The list of parents to crossover.</param> 72 /// <param name="strategyParametersList">The strategy parameter list.</param> 73 /// <param name="childIndividual">Output parameter; the created child.</param> 74 /// <param name="strategyParameters">Output parameter; endogenous strategy parameters.</param> 75 protected override void Cross(IScope scope, IRandom random, IList<double[]> parents, IList<double[]> strategyParametersList, out double[] childIndividual, out double[] strategyParameters) { 76 Apply(random, parents, strategyParametersList, out childIndividual, out strategyParameters); 74 /// <param name="random">A random number generator.</param> 75 /// <param name="parents">An array containing the real vectors that should be crossed.</param> 76 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 77 protected override double[] Cross(IScope scope, IRandom random, double[][] parents) { 78 if (parents.Length < 2) throw new InvalidOperationException("ERROR in SelfAdaptiveDiscreteCrossover: The number of parents is less than 2"); 79 double[][] strategies = new double[scope.SubScopes.Count][]; 80 int length = parents[0].Length; 81 for (int i = 0; i < scope.SubScopes.Count; i++) { 82 strategies[i] = scope.SubScopes[i].GetVariableValue<DoubleArrayData>("StrategyVector", false).Data; 83 if (strategies[i].Length != length) throw new InvalidOperationException("ERROR in SelfAdaptiveDiscreteCrossover: Strategy vectors do not have the same length as the real vectors"); 84 } 85 86 double[] child, strategy; 87 Apply(random, parents, strategies, out child, out strategy); 88 scope.AddVariable(new Variable(scope.TranslateName("StrategyVector"), new DoubleArrayData(strategy))); 89 return child; 77 90 } 78 91 } -
trunk/sources/HeuristicLab.RealVector/SinglePointCrossover.cs
r1184 r1218 57 57 58 58 /// <summary> 59 /// Performs a single point crossover at a randomly chosen position of the two 60 /// given parent real vectors. 59 /// Performs a single point crossover operation for two given parent real vectors. 61 60 /// </summary> 61 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 62 62 /// <param name="scope">The current scope.</param> 63 63 /// <param name="random">A random number generator.</param> 64 /// <param name="parent 1">The first parent for crossover.</param>65 /// < param name="parent2">The second parent for crossover.</param>66 /// <returns>The newly created real vector, resulting from the single point crossover.</returns>67 protected override double[] Cross(IScope scope, IRandom random, double[] parent1, double[] parent2) {68 return Apply(random, parent 1, parent2);64 /// <param name="parents">An array containing the two real vectors that should be crossed.</param> 65 /// <returns>The newly created real vector, resulting from the crossover operation.</returns> 66 protected override double[] Cross(IScope scope, IRandom random, double[][] parents) { 67 if (parents.Length != 2) throw new InvalidOperationException("ERROR in SinglePointCrossover: The number of parents is not equal to 2"); 68 return Apply(random, parents[0], parents[1]); 69 69 } 70 70 }
Note: See TracChangeset
for help on using the changeset viewer.