Changeset 1218 for trunk/sources
- Timestamp:
- 02/16/09 01:21:53 (16 years ago)
- Location:
- trunk/sources
- Files:
-
- 5 deleted
- 30 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.BitVector/BitVectorCrossoverBase.cs
r1176 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, bool[][]"/> 45 /// and adds the created bit vector to the current scope. 45 46 /// </summary> 46 /// <exception cref="InvalidOperationException">Thrown whenthe parents have different lengths.</exception>47 /// <param name="scope">The current scope .</param>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> 48 49 /// <param name="random">A random number generator.</param> 49 /// <param name="parent1">The first parent for crossover.</param> 50 /// <param name="parent2">The second parent for crossover.</param> 51 /// <param name="child">The resulting child scope.</param> 52 protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) { 53 IVariableInfo bitVectorInfo = GetVariableInfo("BitVector"); 54 BoolArrayData vector1 = parent1.GetVariableValue<BoolArrayData>(bitVectorInfo.FormalName, false); 55 BoolArrayData vector2 = parent2.GetVariableValue<BoolArrayData>(bitVectorInfo.FormalName, false); 50 protected sealed override void Cross(IScope scope, IRandom random) { 51 bool[][] parents = new bool[scope.SubScopes.Count][]; 52 int length = -1; 53 for (int i = 0; i < scope.SubScopes.Count; i++) { 54 parents[i] = scope.SubScopes[i].GetVariableValue<BoolArrayData>("BitVector", false).Data; 55 if (i == 0) length = parents[i].Length; 56 else if (parents[i].Length != length) throw new InvalidOperationException("ERROR in BitVectorCrossoverBase: Cannot apply crossover to bit vectors of different length"); 57 } 56 58 57 if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to bit vectors of different length."); 58 59 bool[] result = Cross(scope, random, vector1.Data, vector2.Data); 60 child.AddVariable(new Variable(child.TranslateName(bitVectorInfo.FormalName), new BoolArrayData(result))); 59 bool[] result = Cross(scope, random, parents); 60 scope.AddVariable(new Variable(scope.TranslateName("BitVector"), new BoolArrayData(result))); 61 61 } 62 62 63 63 /// <summary> 64 /// Performs a crossover of two given parents.64 /// Performs a crossover of multiple bit vectors. 65 65 /// </summary> 66 66 /// <param name="scope">The current scope.</param> 67 67 /// <param name="random">A random number generator.</param> 68 /// <param name="parent1">The first parent for crossover.</param> 69 /// <param name="parent2">The second parent for crossover.</param> 68 /// <param name="parents">An array containing all parent bit vectors.</param> 70 69 /// <returns>The newly created bit vector, resulting from the crossover operation.</returns> 71 protected abstract bool[] Cross(IScope scope, IRandom random, bool[] parent1, bool[] parent2);70 protected abstract bool[] Cross(IScope scope, IRandom random, bool[][] parents); 72 71 } 73 72 } -
trunk/sources/HeuristicLab.BitVector/SinglePointCrossover.cs
r1176 r1218 57 57 58 58 /// <summary> 59 /// Performs a single point crossover at a randomly chosen position of t he two59 /// Performs a single point crossover at a randomly chosen position of two 60 60 /// given parent bit vectors. 61 61 /// </summary> 62 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 62 63 /// <param name="scope">The current scope.</param> 63 64 /// <param name="random">A random number generator.</param> 64 /// <param name="parent1">The first parent for crossover.</param> 65 /// <param name="parent2">The second parent for crossover.</param> 65 /// <param name="parents">An array containing the two bit vectors that should be crossed.</param> 66 66 /// <returns>The newly created bit vector, resulting from the single point crossover.</returns> 67 protected override bool[] Cross(IScope scope, IRandom random, bool[] parent1, bool[] parent2) { 68 return Apply(random, parent1, parent2); 67 protected override bool[] Cross(IScope scope, IRandom random, bool[][] parents) { 68 if (parents.Length != 2) throw new InvalidOperationException("ERROR in SinglePointCrossover: The number of parents is not equal to 2"); 69 return Apply(random, parents[0], parents[1]); 69 70 } 70 71 } -
trunk/sources/HeuristicLab.Evolutionary/CrossoverBase.cs
r1159 r1218 28 28 namespace HeuristicLab.Evolutionary { 29 29 /// <summary> 30 /// Base class for cross ingover operators.30 /// Base class for crossover operators. 31 31 /// </summary> 32 32 public abstract class CrossoverBase : OperatorBase { … … 40 40 41 41 /// <summary> 42 /// Replaces the parents (the sub scopes of the current <paramref name="scope"/>) with created children 43 /// by crossing over of two adjacent sub scopes. 42 /// Retrieves the random number generator and calls <see cref="Cross(HeuristicLab.Core.IScope, HeuristicLab.Core.IRandom)"/>. 44 43 /// </summary> 45 /// <exception cref="InvalidOperationException">Thrown when the size of the mating pool 46 /// is not even.</exception> 47 /// <param name="scope">The current scope whose sub scopes shall be parents.</param> 44 /// <param name="scope">The current scope which represents a new child.</param> 48 45 /// <returns><c>null</c>.</returns> 49 46 public override IOperation Apply(IScope scope) { 50 47 IRandom random = GetVariableValue<IRandom>("Random", scope, true); 51 52 if (scope.SubScopes.Count != 2) 53 throw new InvalidOperationException("ERROR: Number of parents is != 2"); 54 55 IScope parent1 = scope.SubScopes[0]; 56 IScope parent2 = scope.SubScopes[1]; 57 IScope child = scope; 58 Cross(scope, random, parent1, parent2, child); 59 48 Cross(scope, random); 60 49 return null; 61 50 } 62 51 63 52 /// <summary> 64 /// Performs a cross over of <paramref name="parent1"/> and <paramref name="parent2"/>65 /// to create a new <paramref name="child"/>.53 /// Performs a crossover of all parents (sub-scopes) of <paramref name="scope"/> to create a new child. 54 /// Note that all children have to be prepared using the <see cref="HeuristicLab.Evolutionary.ChildrenInitializer"/>. 66 55 /// </summary> 67 /// <param name="scope">The current scope .</param>56 /// <param name="scope">The current scope which represents a new child.</param> 68 57 /// <param name="random">A random number generator.</param> 69 /// <param name="parent1">The parent scope 1 to cross over.</param> 70 /// <param name="parent2">The parent scope 2 to cross over.</param> 71 /// <param name="child">The resulting child of the cross over.</param> 72 protected abstract void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child); 58 protected abstract void Cross(IScope scope, IRandom random); 73 59 } 74 60 } -
trunk/sources/HeuristicLab.Evolutionary/HeuristicLab.Evolutionary.csproj
r1159 r1218 61 61 <ItemGroup> 62 62 <Compile Include="ChildrenInitializer.cs" /> 63 <Compile Include="MultiCrossoverBase.cs" />64 63 <Compile Include="SASEGASAReunificator.cs" /> 65 64 <Compile Include="CrossoverBase.cs" /> -
trunk/sources/HeuristicLab.IntVector/DiscreteCrossover.cs
r1157 r1218 56 56 57 57 /// <summary> 58 /// Performs a discrete crossover operation of the two given parents.58 /// Performs a discrete crossover operation for two given parent integer vectors. 59 59 /// </summary> 60 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 60 61 /// <param name="scope">The current scope.</param> 61 62 /// <param name="random">A random number generator.</param> 62 /// <param name="parent1">The first parent for the crossover operation.</param> 63 /// <param name="parent2">The second parent for the crossover operation.</param> 63 /// <param name="parents">An array containing the two integer vectors that should be crossed.</param> 64 64 /// <returns>The newly created integer vector, resulting from the crossover operation.</returns> 65 protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) { 66 return Apply(random, parent1, parent2); 65 protected override int[] Cross(IScope scope, IRandom random, int[][] parents) { 66 if (parents.Length != 2) throw new InvalidOperationException("ERROR in DiscreteCrossover: The number of parents is not equal to 2"); 67 return Apply(random, parents[0], parents[1]); 67 68 } 68 69 } -
trunk/sources/HeuristicLab.IntVector/IntVectorCrossoverBase.cs
r1157 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, int[][]"/> 45 /// and adds the created integer 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 int[][] parents = new int[scope.SubScopes.Count][]; 52 int length = -1; 53 for (int i = 0; i < scope.SubScopes.Count; i++) { 54 parents[i] = scope.SubScopes[i].GetVariableValue<IntArrayData>("IntVector", false).Data; 55 if (i == 0) length = parents[i].Length; 56 else if (parents[i].Length != length) throw new InvalidOperationException("ERROR in IntVectorCrossoverBase: Cannot apply crossover to integer vectors of different length"); 57 } 58 59 int[] result = Cross(scope, random, parents); 60 scope.AddVariable(new Variable(scope.TranslateName("IntVector"), new IntArrayData(result))); 61 } 62 63 /// <summary> 64 /// Performs a crossover of multiple integer 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 IVariableInfo intVectorInfo = GetVariableInfo("IntVector"); 53 IntArrayData vector1 = parent1.GetVariableValue<IntArrayData>(intVectorInfo.FormalName, false); 54 IntArrayData vector2 = parent2.GetVariableValue<IntArrayData>(intVectorInfo.FormalName, false); 55 56 if (vector1.Data.Length != vector2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to integer vectors of different length."); 57 58 int[] result = Cross(scope, random, vector1.Data, vector2.Data); 59 child.AddVariable(new Variable(child.TranslateName(intVectorInfo.FormalName), new IntArrayData(result))); 60 } 61 62 /// <summary> 63 /// Performs a crossover of two given parents. 64 /// </summary> 65 /// <param name="scope">The current scope.</param> 66 /// <param name="random">A random number generator.</param> 67 /// <param name="parent1">The first parent for crossover.</param> 68 /// <param name="parent2">The second parent for crossover.</param> 68 /// <param name="parents">An array containing all parent integer vectors.</param> 69 69 /// <returns>The newly created integer vector, resulting from the crossover operation.</returns> 70 protected abstract int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2);70 protected abstract int[] Cross(IScope scope, IRandom random, int[][] parents); 71 71 } 72 72 } -
trunk/sources/HeuristicLab.IntVector/SinglePointCrossover.cs
r1157 r1218 57 57 58 58 /// <summary> 59 /// Performs a single point crossover at a randomly chosen position of t he two59 /// Performs a single point crossover at a randomly chosen position of two 60 60 /// given parent integer vectors. 61 61 /// </summary> 62 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 62 63 /// <param name="scope">The current scope.</param> 63 64 /// <param name="random">A random number generator.</param> 64 /// <param name="parent1">The first parent for crossover.</param> 65 /// <param name="parent2">The second parent for crossover.</param> 65 /// <param name="parents">An array containing the two integer vectors that should be crossed.</param> 66 66 /// <returns>The newly created integer vector, resulting from the single point crossover.</returns> 67 protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) { 68 return Apply(random, parent1, parent2); 67 protected override int[] Cross(IScope scope, IRandom random, int[][] parents) { 68 if (parents.Length != 2) throw new InvalidOperationException("ERROR in SinglePointCrossover: The number of parents is not equal to 2"); 69 return Apply(random, parents[0], parents[1]); 69 70 } 70 71 } -
trunk/sources/HeuristicLab.Permutation/AbsolutePositionTopologicalCrossover.cs
r850 r1218 75 75 76 76 /// <summary> 77 /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/> 78 /// by taking the values from both parents one by one with the same index. 77 /// Performs an absolute position topological crossover operation for two given parent permutations. 79 78 /// </summary> 80 /// < remarks>Calls <see cref="Apply"/>.</remarks>79 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 81 80 /// <param name="scope">The current scope.</param> 82 81 /// <param name="random">A random number generator.</param> 83 /// <param name="parent 1">The parent scope 1 to cross over.</param>84 /// < param name="parent2">The parent scope 2 to cross over.</param>85 /// <returns>The created cross over permutation as int array.</returns>86 protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {87 return Apply(random, parent 1, parent2);82 /// <param name="parents">An array containing the two permutations that should be crossed.</param> 83 /// <returns>The newly created permutation, resulting from the crossover operation.</returns> 84 protected override int[] Cross(IScope scope, IRandom random, int[][] parents) { 85 if (parents.Length != 2) throw new InvalidOperationException("ERROR in AbsolutePositionTopologicalCrossover: The number of parents is not equal to 2"); 86 return Apply(random, parents[0], parents[1]); 88 87 } 89 88 } -
trunk/sources/HeuristicLab.Permutation/CosaCrossover.cs
r850 r1218 86 86 87 87 /// <summary> 88 /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/> 89 /// by taking first the reverse elements of a randomly chosen interval of parent1 90 /// and inserting it in the result at a position specified by the permutation of parent2. 91 /// The remaining elements to be inserted are taken again from parent1 in the forward direction. 88 /// Performs a COSA crossover operation for two given parent permutations. 92 89 /// </summary> 93 /// < remarks>Calls <see cref="Apply"/>.</remarks>90 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 94 91 /// <param name="scope">The current scope.</param> 95 /// <param name="random"> Therandom number generator.</param>96 /// <param name="parent 1">The parent scope 1 to cross over.</param>97 /// < param name="parent2">The parent scope 2 to cross over.</param>98 /// <returns>The created cross over permutation as int array.</returns>99 protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {100 return Apply(random, parent 1, parent2);92 /// <param name="random">A random number generator.</param> 93 /// <param name="parents">An array containing the two permutations that should be crossed.</param> 94 /// <returns>The newly created permutation, resulting from the crossover operation.</returns> 95 protected override int[] Cross(IScope scope, IRandom random, int[][] parents) { 96 if (parents.Length != 2) throw new InvalidOperationException("ERROR in CosaCrossover: The number of parents is not equal to 2"); 97 return Apply(random, parents[0], parents[1]); 101 98 } 102 99 } -
trunk/sources/HeuristicLab.Permutation/CyclicCrossover.cs
r850 r1218 80 80 81 81 /// <summary> 82 /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/> 83 /// by copying a whole cycle starting at a randomly chosen position in parent1 and taking the rest 84 /// from parent2. 82 /// Performs a cyclic crossover operation for two given parent permutations. 85 83 /// </summary> 86 /// < remarks>Calls <see cref="Apply"/>.</remarks>84 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 87 85 /// <param name="scope">The current scope.</param> 88 /// <param name="random"> Therandom number generator.</param>89 /// <param name="parent 1">The parent scope 1 to cross over.</param>90 /// < param name="parent2">The parent scope 2 to cross over.</param>91 /// <returns>The created cross over permutation as int array.</returns>92 protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {93 return Apply(random, parent 1, parent2);86 /// <param name="random">A random number generator.</param> 87 /// <param name="parents">An array containing the two permutations that should be crossed.</param> 88 /// <returns>The newly created permutation, resulting from the crossover operation.</returns> 89 protected override int[] Cross(IScope scope, IRandom random, int[][] parents) { 90 if (parents.Length != 2) throw new InvalidOperationException("ERROR in CyclicCrossover: The number of parents is not equal to 2"); 91 return Apply(random, parents[0], parents[1]); 94 92 } 95 93 } -
trunk/sources/HeuristicLab.Permutation/EdgeRecombinationCrossover.cs
r850 r1218 139 139 140 140 /// <summary> 141 /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="2"/> 142 /// by calculating the edges of each element. Starts at a randomly chosen position, 143 /// the next element is a neighbour with the least 144 /// number of neighbours, the next again a neighbour and so on. 141 /// Performs an edge recombination crossover operation for two given parent permutations. 145 142 /// </summary> 146 /// <exception cref="InvalidOperationException">Thrown when the permutation lacks a number. 147 /// </exception> 148 /// <remarks>Calls <see cref="Apply"/>.</remarks> 143 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 149 144 /// <param name="scope">The current scope.</param> 150 /// <param name="random"> Therandom number generator.</param>151 /// <param name="parent 1">The parent scope 1 to cross over.</param>152 /// < param name="parent2">The parent scope 2 to cross over.</param>153 /// <returns>The created cross over permutation as int array.</returns>154 protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {155 return Apply(random, parent 1, parent2);145 /// <param name="random">A random number generator.</param> 146 /// <param name="parents">An array containing the two permutations that should be crossed.</param> 147 /// <returns>The newly created permutation, resulting from the crossover operation.</returns> 148 protected override int[] Cross(IScope scope, IRandom random, int[][] parents) { 149 if (parents.Length != 2) throw new InvalidOperationException("ERROR in EdgeRecombinationCrossover: The number of parents is not equal to 2"); 150 return Apply(random, parents[0], parents[1]); 156 151 } 157 152 } -
trunk/sources/HeuristicLab.Permutation/MaximalPreservativeCrossover.cs
r850 r1218 78 78 79 79 /// <summary> 80 /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/> 81 /// by preserving a big randomly chosen region of one permutation and taking the missing ones from the other 82 /// permuation array. 80 /// Performs a maximal preservative crossover operation for two given parent permutations. 83 81 /// </summary> 84 /// < remarks>Calls <see cref="Apply"/>.</remarks>82 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 85 83 /// <param name="scope">The current scope.</param> 86 /// <param name="random"> Therandom number generator.</param>87 /// <param name="parent 1">The permutation array of parent 1.</param>88 /// < param name="parent2">The permutation array of parent 2.</param>89 /// <returns>The created cross over permutation as int array.</returns>90 protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {91 return Apply(random, parent 1, parent2);84 /// <param name="random">A random number generator.</param> 85 /// <param name="parents">An array containing the two permutations that should be crossed.</param> 86 /// <returns>The newly created permutation, resulting from the crossover operation.</returns> 87 protected override int[] Cross(IScope scope, IRandom random, int[][] parents) { 88 if (parents.Length != 2) throw new InvalidOperationException("ERROR in MaximalPreservativeCrossover: The number of parents is not equal to 2"); 89 return Apply(random, parents[0], parents[1]); 92 90 } 93 91 } -
trunk/sources/HeuristicLab.Permutation/OrderBasedCrossover.cs
r850 r1218 92 92 93 93 /// <summary> 94 /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/> by 95 /// randomly selecting some values from the first permutation that will be inserted one after each 96 /// other; the missing ones are picked in the correct order from the second permutation. 94 /// Performs an order-based crossover operation for two given parent permutations. 97 95 /// </summary> 98 /// < remarks>Calls <see cref="Apply"/>.</remarks>96 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 99 97 /// <param name="scope">The current scope.</param> 100 /// <param name="random"> Therandom number generator.</param>101 /// <param name="parent 1">The parent scope 1 to cross over.</param>102 /// < param name="parent2">The parent scope 2 to cross over.</param>103 /// <returns>The created cross over permutation as int array.</returns>104 protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {105 return Apply(random, parent 1, parent2);98 /// <param name="random">A random number generator.</param> 99 /// <param name="parents">An array containing the two permutations that should be crossed.</param> 100 /// <returns>The newly created permutation, resulting from the crossover operation.</returns> 101 protected override int[] Cross(IScope scope, IRandom random, int[][] parents) { 102 if (parents.Length != 2) throw new InvalidOperationException("ERROR in OrderBasedCrossover: The number of parents is not equal to 2"); 103 return Apply(random, parents[0], parents[1]); 106 104 } 107 105 } -
trunk/sources/HeuristicLab.Permutation/OrderCrossover.cs
r850 r1218 74 74 75 75 /// <summary> 76 /// Performs a cross over permuation of <paramref name="parent1"/> and <paramref name="parent2"/> 77 /// by taking a randomly chosen interval from <paramref name="parent1"/> and the rest from 78 /// <paramref name="parent2"/>. 76 /// Performs an order crossover operation for two given parent permutations. 79 77 /// </summary> 80 /// < remarks>Calls <see cref="Apply"/>.</remarks>78 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 81 79 /// <param name="scope">The current scope.</param> 82 /// <param name="random"> Therandom number generator.</param>83 /// <param name="parent 1">The parent scope 1 to cross over.</param>84 /// < param name="parent2">The parent scope 2 to cross over.</param>85 /// <returns>The created cross over permutation as int array.</returns>86 protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {87 return Apply(random, parent 1, parent2);80 /// <param name="random">A random number generator.</param> 81 /// <param name="parents">An array containing the two permutations that should be crossed.</param> 82 /// <returns>The newly created permutation, resulting from the crossover operation.</returns> 83 protected override int[] Cross(IScope scope, IRandom random, int[][] parents) { 84 if (parents.Length != 2) throw new InvalidOperationException("ERROR in OrderCrossover: The number of parents is not equal to 2"); 85 return Apply(random, parents[0], parents[1]); 88 86 } 89 87 } -
trunk/sources/HeuristicLab.Permutation/PartiallyMatchedCrossover.cs
r850 r1218 87 87 88 88 /// <summary> 89 /// Performs a cross over permuation of <paramref name="parent1"/> and <paramref name="parent2"/> 90 /// by taking a randomly chosen interval from <paramref name="parent1"/>, preserving the position, 91 /// then all positions from <paramref name="parent2"/> which are still free in the child 92 /// (the position is free and the value is "free") 93 /// and then missing ones from <paramref name="parent2"/> in the order they occur 94 /// in <paramref name="parent2"/>. 89 /// Performs a partially matched crossover operation for two given parent permutations. 95 90 /// </summary> 96 /// < remarks>Calls <see cref="Apply"/>.</remarks>91 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 97 92 /// <param name="scope">The current scope.</param> 98 /// <param name="random"> Therandom number generator.</param>99 /// <param name="parent 1">The parent scope 1 to cross over.</param>100 /// < param name="parent2">The parent scope 2 to cross over.</param>101 /// <returns>The created cross over permutation as int array.</returns>102 protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {103 return Apply(random, parent 1, parent2);93 /// <param name="random">A random number generator.</param> 94 /// <param name="parents">An array containing the two permutations that should be crossed.</param> 95 /// <returns>The newly created permutation, resulting from the crossover operation.</returns> 96 protected override int[] Cross(IScope scope, IRandom random, int[][] parents) { 97 if (parents.Length != 2) throw new InvalidOperationException("ERROR in PartiallyMatchedCrossover: The number of parents is not equal to 2"); 98 return Apply(random, parents[0], parents[1]); 104 99 } 105 100 } -
trunk/sources/HeuristicLab.Permutation/PermutationCrossoverBase.cs
r850 r1218 28 28 namespace HeuristicLab.Permutation { 29 29 /// <summary> 30 /// Base class for cross over permutations.30 /// Base class for all permutation crossover operators. 31 31 /// </summary> 32 32 public abstract class PermutationCrossoverBase : CrossoverBase { … … 41 41 42 42 /// <summary> 43 /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/> with 44 /// the given random number generator (<paramref name="random"/>) to create a new 45 /// <paramref name="child"/>. 43 /// Performs a crossover by calling <see cref="Cross(HeuristicLab.Core.IScope, HeuristicLab.Core.IRandom, int[][]"/> 44 /// and adds the created permutation to the current scope. 46 45 /// </summary> 47 /// <exception cref="InvalidOperationException">Thrown when the two permutations have a different48 /// length.</exception>49 /// < remarks>Calls <see cref="Cross(HeuristicLab.Core.IScope, HeuristicLab.Core.IRandom, int[], int[])"/>.</remarks>50 /// <param name="scope">The scope where to get the actual child variable name.</param>51 /// <param name="random">The random number generator.</param>52 /// <param name="parent1">The parent scope 1 to cross over.</param>53 /// <param name="parent2">The parent scope 2 to cross over.</param>54 /// <param name="child">The child scope which to assign the permutated data.</param>55 protected sealed override void Cross(IScope scope, IRandom random, IScope parent1, IScope parent2, IScope child) {56 Permutation perm1 = parent1.GetVariableValue<Permutation>("Permutation", false);57 Permutation perm2 = parent2.GetVariableValue<Permutation>("Permutation", false);46 /// <exception cref="InvalidOperationException">Thrown if the parents have different lengths.</exception> 47 /// <param name="scope">The current scope which represents a new child.</param> 48 /// <param name="random">A random number generator.</param> 49 protected sealed override void Cross(IScope scope, IRandom random) { 50 int[][] parents = new int[scope.SubScopes.Count][]; 51 int length = -1; 52 for (int i = 0; i < scope.SubScopes.Count; i++) { 53 parents[i] = scope.SubScopes[i].GetVariableValue<Permutation>("Permutation", false).Data; 54 if (i == 0) length = parents[i].Length; 55 else if (parents[i].Length != length) throw new InvalidOperationException("ERROR in PermutationCrossoverBase: Cannot apply crossover to permutations of different length"); 56 } 58 57 59 if (perm1.Data.Length != perm2.Data.Length) throw new InvalidOperationException("Cannot apply crossover to permutations of different length."); 60 61 int[] result = Cross(scope, random, perm1.Data, perm2.Data); 62 child.AddVariable(new Variable(scope.TranslateName("Permutation"), new Permutation(result))); 58 int[] result = Cross(scope, random, parents); 59 scope.AddVariable(new Variable(scope.TranslateName("Permutation"), new Permutation(result))); 63 60 } 64 61 65 62 /// <summary> 66 /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/> with 67 /// the given random number generator (<paramref name="random"/>) . 63 /// Performs a crossover of multiple permutations. 68 64 /// </summary> 69 /// <param name="scope">The scope of the variables.</param> 70 /// <param name="random">The random number generator.</param> 71 /// <param name="parent1">The parent scope 1 to cross over.</param> 72 /// <param name="parent2">The parent scope 2 to cross over.</param> 73 /// <returns>The created cross over permutation as int array.</returns> 74 protected abstract int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2); 65 /// <param name="scope">The current scope.</param> 66 /// <param name="random">A random number generator.</param> 67 /// <param name="parents">An array containing all parent permutations.</param> 68 /// <returns>The newly created permutation, resulting from the crossover operation.</returns> 69 protected abstract int[] Cross(IScope scope, IRandom random, int[][] parents); 75 70 } 76 71 } -
trunk/sources/HeuristicLab.Permutation/PositionBasedCrossover.cs
r850 r1218 78 78 79 79 /// <summary> 80 /// Performs a cross over permutation of <paramref name="parent1"/> and <paramref name="parent2"/> 81 /// based on randomly chosen positions to define which position to take from where. 80 /// Performs a position-based crossover operation for two given parent permutations. 82 81 /// </summary> 83 /// < remarks>Calls <see cref="Apply"/>.</remarks>82 /// <exception cref="InvalidOperationException">Thrown if there are not exactly two parents.</exception> 84 83 /// <param name="scope">The current scope.</param> 85 /// <param name="random"> Therandom number generator.</param>86 /// <param name="parent 1">The permutation array of parent 1.</param>87 /// < param name="parent2">The permutation array of parent 2.</param>88 /// <returns>The created cross over permutation as int array.</returns>89 protected override int[] Cross(IScope scope, IRandom random, int[] parent1, int[] parent2) {90 return Apply(random, parent 1, parent2);84 /// <param name="random">A random number generator.</param> 85 /// <param name="parents">An array containing the two permutations that should be crossed.</param> 86 /// <returns>The newly created permutation, resulting from the crossover operation.</returns> 87 protected override int[] Cross(IScope scope, IRandom random, int[][] parents) { 88 if (parents.Length != 2) throw new InvalidOperationException("ERROR in PositionBasedCrossover: The number of parents is not equal to 2"); 89 return Apply(random, parents[0], parents[1]); 91 90 } 92 91 } -
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 } -
trunk/sources/HeuristicLab.SimOpt/SimOptDiscreteMultiCrossover.cs
r637 r1218 29 29 30 30 namespace HeuristicLab.SimOpt { 31 public class SimOptDiscreteMultiCrossover : MultiCrossoverBase {31 public class SimOptDiscreteMultiCrossover : CrossoverBase { 32 32 33 33 public override string Description { … … 40 40 } 41 41 42 protected override void Cross(IScope scope, IRandom random , IScope[] parents, IScope child) {42 protected override void Cross(IScope scope, IRandom random) { 43 43 ICollection<IConstraint> violated; 44 44 45 ConstrainedItemList[] p = new ConstrainedItemList[ parents.Length];45 ConstrainedItemList[] p = new ConstrainedItemList[scope.SubScopes.Count]; 46 46 for (int i = 0; i < p.Length; i++) { 47 p[i] = parents[i].GetVariableValue<ConstrainedItemList>("Item", false);47 p[i] = scope.SubScopes[i].GetVariableValue<ConstrainedItemList>("Item", false); 48 48 if (i > 0 && p[i].Count != p[i-1].Count) throw new InvalidOperationException("ERROR: the lists do not contain the same number of items"); 49 49 } … … 55 55 childList.BeginCombinedOperation(); 56 56 for (int i = 0; i < childList.Count; i++) { 57 int nextParent = random.Next(0, parents.Length);57 int nextParent = random.Next(0, scope.SubScopes.Count); 58 58 if (nextParent > 0) childList.TrySetAt(i, (IItem)p[nextParent].Clone(), out violated); 59 59 } 60 60 } while (!childList.EndCombinedOperation(out violated) && ++iter < 100); 61 61 if (violated.Count == 0) { 62 child.AddVariable(new Variable(parents[0].TranslateName("Item"), childList));62 scope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("Item"), childList)); 63 63 } 64 64 } -
trunk/sources/HeuristicLab.SimOpt/SimOptSinglePointCrossover.cs
r637 r1218 40 40 } 41 41 42 protected override void Cross(IScope scope, IRandom random , IScope parent1, IScope parent2, IScope child) {42 protected override void Cross(IScope scope, IRandom random) { 43 43 ICollection<IConstraint> violated; 44 ConstrainedItemList p1 = parent1.GetVariableValue<ConstrainedItemList>("Item", false);45 ConstrainedItemList p2 = parent2.GetVariableValue<ConstrainedItemList>("Item", false);44 ConstrainedItemList p1 = scope.SubScopes[0].GetVariableValue<ConstrainedItemList>("Item", false); 45 ConstrainedItemList p2 = scope.SubScopes[1].GetVariableValue<ConstrainedItemList>("Item", false); 46 46 47 47 if (p1.Count != p2.Count) throw new InvalidOperationException("ERROR: the lists do not contain the same number of items"); … … 58 58 } while (!childList.EndCombinedOperation(out violated) && ++iter < 100); 59 59 if (violated.Count == 0) { 60 child.AddVariable(new Variable(parent1.TranslateName("Item"), childList));60 scope.AddVariable(new Variable(scope.SubScopes[0].TranslateName("Item"), childList)); 61 61 } 62 62 }
Note: See TracChangeset
for help on using the changeset viewer.