Changeset 3123
- Timestamp:
- 03/19/10 15:59:37 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/BoundsChecker.cs
r3060 r3123 29 29 namespace HeuristicLab.Encodings.RealVectorEncoding { 30 30 /// <summary> 31 /// Checks if all elements of a real vector are inside a given minimum and maximum value.31 /// Checks if all elements of a real vector are inside the bounds. 32 32 /// If not, the elements are corrected. 33 33 /// </summary> 34 [Item("BoundsChecker", "Checks if all elements of a real vector are inside a given minimum and maximum value. If not, elements are corrected.")]34 [Item("BoundsChecker", "Checks if all elements of a real vector are inside the bounds. If not, elements are corrected.")] 35 35 [StorableClass] 36 36 public class BoundsChecker : SingleSuccessorOperator { … … 38 38 get { return (LookupParameter<RealVector>)Parameters["RealVector"]; } 39 39 } 40 public ValueLookupParameter<DoubleValue> MinimumParameter { 41 get { return (ValueLookupParameter<DoubleValue>)Parameters["Minimum"]; } 42 } 43 public ValueLookupParameter<DoubleValue> MaximumParameter { 44 get { return (ValueLookupParameter<DoubleValue>)Parameters["Maximum"]; } 40 public ValueLookupParameter<DoubleMatrix> BoundsParameter { 41 get { return (ValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 45 42 } 46 43 47 44 /// <summary> 48 /// Initializes a new instance of <see cref="BoundsChecker"/> with t hreeparameters49 /// (<c>RealVector</c>, <c> Minimum</c> and <c>Maximum</c>).45 /// Initializes a new instance of <see cref="BoundsChecker"/> with two parameters 46 /// (<c>RealVector</c>, <c>Bounds</c>). 50 47 /// </summary> 51 48 public BoundsChecker() 52 49 : base() { 53 50 Parameters.Add(new LookupParameter<RealVector>("RealVector", "The real-valued vector for which the bounds should be checked.")); 54 Parameters.Add(new ValueLookupParameter<DoubleValue>("Minimum", "The lower bound for each element in the vector.")); 55 Parameters.Add(new ValueLookupParameter<DoubleValue>("Maximum", "The upper bound for each element in the vector.")); 51 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.")); 56 52 } 57 53 58 54 /// <summary> 59 /// Checks if all elements of the given <paramref name="vector"/> are inside the given minimum 60 /// and maximum value and if not they are corrected. 55 /// Checks if all elements of the given <paramref name="vector"/> are inside the bounds and if not they are corrected. 61 56 /// </summary> 62 /// <param name="min">The minimum value of the range (inclusive).</param> 63 /// <param name="max">The maximum value of the range (inclusive).</param> 57 /// <param name="bounds">The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.</param> 64 58 /// <param name="vector">The vector to check.</param> 65 59 /// <returns>The corrected real vector.</returns> 66 public static void Apply(RealVector vector, Double Value min, DoubleValue max) {60 public static void Apply(RealVector vector, DoubleMatrix bounds) { 67 61 for (int i = 0; i < vector.Length; i++) { 68 if (vector[i] < min.Value) vector[i] = min.Value; 69 if (vector[i] > max.Value) vector[i] = max.Value; 62 double min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1]; 63 if (vector[i] < min) vector[i] = min; 64 if (vector[i] > max) vector[i] = max; 70 65 } 71 66 } 72 67 73 68 /// <summary> 74 /// Checks if all elements of the given <paramref name="vector"/> are inside the given minimum 75 /// and maximum value and if not they are corrected. 69 /// Checks if all elements of the given <paramref name="vector"/> are inside the bounds and if not they are corrected. 76 70 /// </summary> 77 /// <exception cref="InvalidOperationException">Thrown when one of the three parameters (vector, minimum, or maximum)could not be found.</exception>78 /// <remarks>Calls <see cref="Apply( double, double, double[])"/>.</remarks>71 /// <exception cref="InvalidOperationException">Thrown when either vector or bounds could not be found.</exception> 72 /// <remarks>Calls <see cref="Apply(RealVector, DoubleMatrix)"/>.</remarks> 79 73 /// <inheritdoc select="returns" /> 80 74 public override IOperation Apply() { 81 75 if (RealVectorParameter.ActualValue == null) throw new InvalidOperationException("BoundsChecker: Parameter " + RealVectorParameter.ActualName + " could not be found."); 82 if (MinimumParameter.ActualValue == null) throw new InvalidOperationException("BoundsChecker: Parameter " + MinimumParameter.ActualName + " could not be found."); 83 if (MaximumParameter.ActualValue == null) throw new InvalidOperationException("BoundsChecker: Parameter " + MaximumParameter.ActualName + " could not be found."); 84 Apply(RealVectorParameter.ActualValue, MinimumParameter.ActualValue, MaximumParameter.ActualValue); 76 if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("BoundsChecker: Parameter " + BoundsParameter.ActualName + " could not be found."); 77 Apply(RealVectorParameter.ActualValue, BoundsParameter.ActualValue); 85 78 return base.Apply(); 86 79 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Creators/UniformRandomRealVectorCreator.cs
r3060 r3123 48 48 /// <param name="random">The random number generator.</param> 49 49 /// <param name="length">The length of the real vector.</param> 50 /// <param name="min">The minimum value of the sampling range for each vector element (inclusive).</param> 51 /// <param name="max">The maximum value of the sampling range for each vector element (exclusive).</param> 50 /// <param name="bounds">The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.</param> 52 51 /// <returns>The newly created real vector.</returns> 53 public static RealVector Apply(IRandom random, int length, double min, double max) { 54 if (length <= 0) throw new ArgumentException("UniformRandomRealVectorCreator: Length is smaller or equal to 0.", "length"); 55 if (min > max) throw new ArgumentException("UniformRandomRealVectorCreator: Minimum is greater than Maximum.", "min"); 52 public static RealVector Apply(IRandom random, int length, DoubleMatrix bounds) { 56 53 RealVector result = new RealVector(length); 57 for (int i = 0; i < length; i++) 58 result[i] = min + random.NextDouble() * (max - min); 54 result.Randomize(random, bounds); 59 55 return result; 60 56 } 61 57 62 58 /// <summary> 63 /// Forwards the call to <see cref="Apply(IRandom, int, double, double)"/>.59 /// Forwards the call to <see cref="Apply(IRandom, int, DoubleMatrix)"/>. 64 60 /// </summary> 65 61 /// <param name="random">The pseudo random number generator to use.</param> 66 62 /// <param name="length">The length of the real vector.</param> 67 /// <param name="minimum">The minimum value of the sampling range for each vector element (inclusive).</param> 68 /// <param name="maximum">The maximum value of the sampling range for each vector element (exclusive).</param> 63 /// <param name="bounds">The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.</param> 69 64 /// <returns>The newly created real vector.</returns> 70 protected override RealVector Create(IRandom random, IntValue length, Double Value minimum, DoubleValue maximum) {71 return Apply(random, length.Value, minimum.Value, maximum.Value);65 protected override RealVector Create(IRandom random, IntValue length, DoubleMatrix bounds) { 66 return Apply(random, length.Value, bounds); 72 67 } 73 68 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Interfaces/IRealVectorCreator.cs
r3060 r3123 30 30 public interface IRealVectorCreator : IRealVectorOperator, ISolutionCreator { 31 31 IValueLookupParameter<IntValue> LengthParameter { get; } 32 IValueLookupParameter<DoubleValue> MinimumParameter { get; } 33 IValueLookupParameter<DoubleValue> MaximumParameter { get; } 32 IValueLookupParameter<DoubleMatrix> BoundsParameter { get; } 34 33 ILookupParameter<RealVector> RealVectorParameter { get; } 35 34 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Manipulators/BreederGeneticAlgorithmManipulator.cs
r3060 r3123 38 38 public class BreederGeneticAlgorithmManipulator : RealVectorManipulator { 39 39 private static readonly double[] powerOfTwo = new double[] { 1, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125, 0.00390625, 0.001953125, 0.0009765625, 0.00048828125, 0.000244140625, 0.0001220703125, 0.00006103515625, 0.000030517578125 }; 40 public ValueLookupParameter<DoubleValue> MinimumParameter { 41 get { return (ValueLookupParameter<DoubleValue>)Parameters["Minimum"]; } 42 } 43 public ValueLookupParameter<DoubleValue> MaximumParameter { 44 get { return (ValueLookupParameter<DoubleValue>)Parameters["Maximum"]; } 40 public ValueLookupParameter<DoubleMatrix> BoundsParameter { 41 get { return (ValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 45 42 } 46 43 public ValueLookupParameter<DoubleValue> SearchIntervalFactorParameter { … … 48 45 } 49 46 /// <summary> 50 /// Initializes a new instance of <see cref="BreederGeneticAlgorithmManipulator"/> with t hree variable51 /// infos (<c>Minimum</c>, <c>Maximum</c> and <c>SearchIntervalFactor</c>).47 /// Initializes a new instance of <see cref="BreederGeneticAlgorithmManipulator"/> with two 48 /// parameters (<c>Bounds</c> and <c>SearchIntervalFactor</c>). 52 49 /// </summary> 53 50 public BreederGeneticAlgorithmManipulator() 54 51 : base() { 55 Parameters.Add(new ValueLookupParameter<DoubleValue>("Minimum", "The lower bound for each element in the vector.")); 56 Parameters.Add(new ValueLookupParameter<DoubleValue>("Maximum", "The upper bound for each element in the vector.")); 52 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each element in the vector.")); 57 53 Parameters.Add(new ValueLookupParameter<DoubleValue>("SearchIntervalFactor", "The factor determining the size of the search interval, that will be added/removed to/from the allele selected for manipulation.", new DoubleValue(0.1))); 58 54 } … … 63 59 /// <param name="random">A random number generator.</param> 64 60 /// <param name="vector">The real vector to manipulate.</param> 65 /// <param name="min">The minimum number of the sampling range for the vector element (inclusive).</param> 66 /// <param name="max">The maximum number of the sampling range for the vector element (exclusive).</param> 61 /// <param name="bounds">The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.</param> 67 62 /// <param name="searchIntervalFactor">The factor determining the size of the search interval.</param> 68 public static void Apply(IRandom random, RealVector vector, Double Value min, DoubleValue max, DoubleValue searchIntervalFactor) {63 public static void Apply(IRandom random, RealVector vector, DoubleMatrix bounds, DoubleValue searchIntervalFactor) { 69 64 int length = vector.Length; 70 65 double prob, value; … … 72 67 value = Sigma(random); 73 68 } while (value == 0); 74 value *= searchIntervalFactor.Value * (max.Value - min.Value);75 69 76 70 prob = 1.0 / (double)length; … … 79 73 for (int i = 0; i < length; i++) { 80 74 if (random.NextDouble() < prob) { 75 double range = bounds[i % bounds.Rows, 1] - bounds[i % bounds.Rows, 0]; 81 76 if (random.NextDouble() < 0.5) { 82 vector[i] = vector[i] + value ;77 vector[i] = vector[i] + value * searchIntervalFactor.Value * range; 83 78 } else { 84 vector[i] = vector[i] - value ;79 vector[i] = vector[i] - value * searchIntervalFactor.Value * range; 85 80 } 86 81 wasMutated = true; … … 91 86 if (!wasMutated) { 92 87 int pos = random.Next(length); 88 double range = bounds[pos % bounds.Rows, 1] - bounds[pos % bounds.Rows, 0]; 93 89 if (random.NextDouble() < 0.5) { 94 vector[pos] = vector[pos] + value ;90 vector[pos] = vector[pos] + value * searchIntervalFactor.Value * range; 95 91 } else { 96 vector[pos] = vector[pos] - value ;92 vector[pos] = vector[pos] - value * searchIntervalFactor.Value * range; 97 93 } 98 94 } … … 114 110 115 111 /// <summary> 116 /// Checks the parameters Minimum, Maximum, and SearchIntervalFactor and forwards the call to <see cref="Apply(IRandom, RealVector, DoubleValue, DoubleValue, DoubleValue)"/>.112 /// Checks the parameters Bounds, and SearchIntervalFactor and forwards the call to <see cref="Apply(IRandom, RealVector, DoubleValue, DoubleValue, DoubleValue)"/>. 117 113 /// </summary> 118 114 /// <param name="random">A random number generator.</param> 119 115 /// <param name="realVector">The real vector to manipulate.</param> 120 116 protected override void Manipulate(IRandom random, RealVector realVector) { 121 if (MinimumParameter.ActualValue == null) throw new InvalidOperationException("BreederGeneticAlgorithmManipulator: Parameter " + MinimumParameter.ActualName + " could not be found."); 122 if (MaximumParameter.ActualValue == null) throw new InvalidOperationException("BreederGeneticAlgorithmManipulator: Paraemter " + MaximumParameter.ActualName + " could not be found."); 117 if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("BreederGeneticAlgorithmManipulator: Parameter " + BoundsParameter.ActualName + " could not be found."); 123 118 if (SearchIntervalFactorParameter.ActualValue == null) throw new InvalidOperationException("BreederGeneticAlgorithmManipulator: Paraemter " + SearchIntervalFactorParameter.ActualName + " could not be found."); 124 Apply(random, realVector, MinimumParameter.ActualValue, MaximumParameter.ActualValue, SearchIntervalFactorParameter.ActualValue);119 Apply(random, realVector, BoundsParameter.ActualValue, SearchIntervalFactorParameter.ActualValue); 125 120 } 126 121 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Manipulators/MichalewiczNonUniformAllPositionsManipulator.cs
r3060 r3123 38 38 public class MichalewiczNonUniformAllPositionsManipulator : RealVectorManipulator { 39 39 /// <summary> 40 /// The lower bound of the values in the real vector.40 /// The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled. 41 41 /// </summary> 42 public ValueLookupParameter<DoubleValue> MinimumParameter { 43 get { return (ValueLookupParameter<DoubleValue>)Parameters["Minimum"]; } 44 } 45 /// <summary> 46 /// The upper bound of the values in the real vector. 47 /// </summary> 48 public ValueLookupParameter<DoubleValue> MaximumParameter { 49 get { return (ValueLookupParameter<DoubleValue>)Parameters["Maximum"]; } 42 public ValueLookupParameter<DoubleMatrix> BoundsParameter { 43 get { return (ValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 50 44 } 51 45 /// <summary> … … 70 64 /// <summary> 71 65 /// Initializes a new instance of <see cref="MichalewiczNonUniformAllPositionsManipulator"/> with 72 /// f ive parameters (<c>Minimum</c>, <c>Maximum</c>, <c>CurrentGeneration</c>,66 /// four parameters (<c>Bounds</c>, <c>CurrentGeneration</c>, 73 67 /// <c>MaximumGenerations</c> and <c>GenerationDependency</c>). 74 68 /// </summary> 75 69 public MichalewiczNonUniformAllPositionsManipulator() 76 70 : base() { 77 Parameters.Add(new ValueLookupParameter<DoubleValue>("Minimum", "Minimum of the sampling range for the vector element (included)")); 78 Parameters.Add(new ValueLookupParameter<DoubleValue>("Maximum", "Maximum of the sampling range for the vector element (excluded)")); 71 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.")); 79 72 Parameters.Add(new LookupParameter<IntValue>("Generation", "Current generation of the algorithm")); 80 73 Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "Maximum number of generations")); … … 89 82 /// <param name="random">The random number generator.</param> 90 83 /// <param name="vector">The real vector to manipulate.</param> 91 /// <param name="min">The minimum value of the sampling range for the vector element (inclusive).</param> 92 /// <param name="max">The maximum value of the sampling range for the vector element (exclusive).</param> 84 /// <param name="bounds">The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.</param> 93 85 /// <param name="currentGeneration">The current generation of the algorithm.</param> 94 86 /// <param name="maximumGenerations">Maximum number of generations.</param> 95 87 /// <param name="generationsDependency">Specifies the degree of dependency on the number of generations.</param> 96 88 /// <returns>The manipulated real vector.</returns> 97 public static void Apply(IRandom random, RealVector vector, Double Value min, DoubleValue max, IntValue currentGeneration, IntValue maximumGenerations, DoubleValue generationsDependency) {89 public static void Apply(IRandom random, RealVector vector, DoubleMatrix bounds, IntValue currentGeneration, IntValue maximumGenerations, DoubleValue generationsDependency) { 98 90 if (currentGeneration.Value > maximumGenerations.Value) throw new ArgumentException("MichalewiczNonUniformAllPositionManipulator: CurrentGeneration must be smaller or equal than MaximumGeneration", "currentGeneration"); 99 91 int length = vector.Length; … … 102 94 103 95 for (int i = 0; i < length; i++) { 96 double min = bounds[i % bounds.Rows, 0]; 97 double max = bounds[i % bounds.Rows, 1]; 104 98 if (random.NextDouble() < 0.5) { 105 vector[i] = vector[i] + (max .Value- vector[i]) * (1 - Math.Pow(random.NextDouble(), prob));99 vector[i] = vector[i] + (max - vector[i]) * (1 - Math.Pow(random.NextDouble(), prob)); 106 100 } else { 107 vector[i] = vector[i] - (vector[i] - min .Value) * (1 - Math.Pow(random.NextDouble(), prob));101 vector[i] = vector[i] - (vector[i] - min) * (1 - Math.Pow(random.NextDouble(), prob)); 108 102 } 109 103 } … … 116 110 /// <param name="realVector">The real vector that should be manipulated.</param> 117 111 protected override void Manipulate(IRandom random, RealVector realVector) { 118 if (MinimumParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformAllPositionManipulator: Parameter " + MinimumParameter.ActualName + " could not be found."); 119 if (MaximumParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformAllPositionManipulator: Parameter " + MaximumParameter.ActualName + " could not be found."); 112 if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformAllPositionManipulator: Parameter " + BoundsParameter.ActualName + " could not be found."); 120 113 if (GenerationParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformAllPositionManipulator: Parameter " + GenerationParameter.ActualName + " could not be found."); 121 114 if (MaximumGenerationsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformAllPositionManipulator: Parameter " + MaximumGenerationsParameter.ActualName + " could not be found."); 122 115 if (GenerationDependencyParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformAllPositionManipulator: Parameter " + GenerationDependencyParameter.ActualName + " could not be found."); 123 Apply(random, realVector, MinimumParameter.ActualValue, MaximumParameter.ActualValue, GenerationParameter.ActualValue, MaximumGenerationsParameter.ActualValue, GenerationDependencyParameter.ActualValue);116 Apply(random, realVector, BoundsParameter.ActualValue, GenerationParameter.ActualValue, MaximumGenerationsParameter.ActualValue, GenerationDependencyParameter.ActualValue); 124 117 } 125 118 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Manipulators/MichalewiczNonUniformOnePositionManipulator.cs
r3060 r3123 38 38 public class MichalewiczNonUniformOnePositionManipulator : RealVectorManipulator { 39 39 /// <summary> 40 /// The lower bound of the values in the real vector.40 /// The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled. 41 41 /// </summary> 42 public ValueLookupParameter<DoubleValue> MinimumParameter { 43 get { return (ValueLookupParameter<DoubleValue>)Parameters["Minimum"]; } 44 } 45 /// <summary> 46 /// The upper bound of the values in the real vector. 47 /// </summary> 48 public ValueLookupParameter<DoubleValue> MaximumParameter { 49 get { return (ValueLookupParameter<DoubleValue>)Parameters["Maximum"]; } 42 public ValueLookupParameter<DoubleMatrix> BoundsParameter { 43 get { return (ValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 50 44 } 51 45 /// <summary> … … 69 63 70 64 /// <summary> 71 /// Initializes a new instance of <see cref="MichalewiczNonUniformOnePositionManipulator"/> with f ive72 /// parameters (<c> Minimum</c>, <c>Maximum</c>, <c>CurrentGeneration</c>, <c>MaximumGenerations</c>65 /// Initializes a new instance of <see cref="MichalewiczNonUniformOnePositionManipulator"/> with four 66 /// parameters (<c>Bounds</c>, <c>CurrentGeneration</c>, <c>MaximumGenerations</c> 73 67 /// and <c>GenerationDependency</c>). 74 68 /// </summary> 75 69 public MichalewiczNonUniformOnePositionManipulator() 76 70 : base() { 77 Parameters.Add(new ValueLookupParameter<DoubleValue>("Minimum", "Minimum of the sampling range for the vector element (included)")); 78 Parameters.Add(new ValueLookupParameter<DoubleValue>("Maximum", "Maximum of the sampling range for the vector element (excluded)")); 71 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.")); 79 72 Parameters.Add(new LookupParameter<IntValue>("Generation", "Current generation of the algorithm")); 80 73 Parameters.Add(new LookupParameter<IntValue>("MaximumGenerations", "Maximum number of generations")); … … 89 82 /// <param name="random">The random number generator.</param> 90 83 /// <param name="vector">The real vector to manipulate.</param> 91 /// <param name="min">The minimum value of the sampling range for the vector element (inclusive).</param> 92 /// <param name="max">The maximum value of the sampling range for the vector element (exclusive).</param> 84 /// <param name="bounds">The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.</param> 93 85 /// <param name="currentGeneration">The current generation of the algorithm.</param> 94 86 /// <param name="maximumGenerations">Maximum number of generations.</param> 95 87 /// <param name="generationsDependency">Specifies the degree of dependency on the number of generations.</param> 96 88 /// <returns>The manipulated real vector.</returns> 97 public static void Apply(IRandom random, RealVector vector, Double Value min, DoubleValue max, IntValue currentGeneration, IntValue maximumGenerations, DoubleValue generationsDependency) {89 public static void Apply(IRandom random, RealVector vector, DoubleMatrix bounds, IntValue currentGeneration, IntValue maximumGenerations, DoubleValue generationsDependency) { 98 90 if (currentGeneration.Value > maximumGenerations.Value) throw new ArgumentException("MichalewiczNonUniformOnePositionManipulator: CurrentGeneration must be smaller or equal than MaximumGeneration", "currentGeneration"); 99 91 int length = vector.Length; … … 102 94 double prob = (1 - Math.Pow(random.NextDouble(), Math.Pow(1 - currentGeneration.Value / maximumGenerations.Value, generationsDependency.Value))); 103 95 96 double min = bounds[index % bounds.Rows, 0]; 97 double max = bounds[index % bounds.Rows, 1]; 98 104 99 if (random.NextDouble() < 0.5) { 105 vector[index] = vector[index] + (max .Value- vector[index]) * prob;100 vector[index] = vector[index] + (max - vector[index]) * prob; 106 101 } else { 107 vector[index] = vector[index] - (vector[index] - min .Value) * prob;102 vector[index] = vector[index] - (vector[index] - min) * prob; 108 103 } 109 104 } … … 115 110 /// <param name="realVector">The real vector that should be manipulated.</param> 116 111 protected override void Manipulate(IRandom random, RealVector realVector) { 117 if (MinimumParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + MinimumParameter.ActualName + " could not be found."); 118 if (MaximumParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + MaximumParameter.ActualName + " could not be found."); 112 if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + BoundsParameter.ActualName + " could not be found."); 119 113 if (GenerationParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + GenerationParameter.ActualName + " could not be found."); 120 114 if (MaximumGenerationsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + MaximumGenerationsParameter.ActualName + " could not be found."); 121 115 if (GenerationDependencyParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + GenerationDependencyParameter.ActualName + " could not be found."); 122 Apply(random, realVector, MinimumParameter.ActualValue, MaximumParameter.ActualValue, GenerationParameter.ActualValue, MaximumGenerationsParameter.ActualValue, GenerationDependencyParameter.ActualValue);116 Apply(random, realVector, BoundsParameter.ActualValue, GenerationParameter.ActualValue, MaximumGenerationsParameter.ActualValue, GenerationDependencyParameter.ActualValue); 123 117 } 124 118 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Manipulators/UniformOnePositionManipulator.cs
r3060 r3123 33 33 /// It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg. 34 34 /// </remarks> 35 [Item("UniformOnePositionManipulator", "Changes a single position in the vector by sampling uniformly from the interval [Minimum , Maximum). It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.")]35 [Item("UniformOnePositionManipulator", "Changes a single position in the vector by sampling uniformly from the interval [Minimum_i, Maximum_i) in dimension i. It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.")] 36 36 [StorableClass] 37 37 public class UniformOnePositionManipulator : RealVectorManipulator { 38 38 /// <summary> 39 /// The lower boundof the values in the real vector.39 /// The bounds of the values in the real vector. 40 40 /// </summary> 41 public ValueLookupParameter<DoubleValue> MinimumParameter { 42 get { return (ValueLookupParameter<DoubleValue>)Parameters["Minimum"]; } 43 } 44 /// <summary> 45 /// The upper bound of the values in the real vector. 46 /// </summary> 47 public ValueLookupParameter<DoubleValue> MaximumParameter { 48 get { return (ValueLookupParameter<DoubleValue>)Parameters["Maximum"]; } 41 public ValueLookupParameter<DoubleMatrix> BoundsParameter { 42 get { return (ValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 49 43 } 50 44 51 45 /// <summary> 52 /// Initializes a new instance of <see cref="UniformOnePositionManipulator"/> with two parameters53 /// (<c> Minimum</c> and <c>Maximum</c>).46 /// Initializes a new instance of <see cref="UniformOnePositionManipulator"/> with one parameter 47 /// (<c>Bounds</c>). 54 48 /// </summary> 55 49 public UniformOnePositionManipulator() { 56 Parameters.Add(new ValueLookupParameter<DoubleValue>("Minimum", "Minimum of the sampling range for the vector element (included)")); 57 Parameters.Add(new ValueLookupParameter<DoubleValue>("Maximum", "Maximum of the sampling range for the vector element (excluded)")); 50 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "Lower and upper bound of the positions in the vector.")); 58 51 } 59 52 … … 63 56 /// <param name="random">A random number generator.</param> 64 57 /// <param name="vector">The real vector to manipulate.</param> 65 /// <param name="min">The minimum value of the sampling range for 66 /// the vector element to change (inclusive).</param> 67 /// <param name="max">The maximum value of the sampling range for 68 /// the vector element to change (exclusive).</param> 69 public static void Apply(IRandom random, RealVector vector, DoubleValue min, DoubleValue max) { 58 /// <param name="bounds">The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.</param> 59 public static void Apply(IRandom random, RealVector vector, DoubleMatrix bounds) { 70 60 int index = random.Next(vector.Length); 71 vector[index] = min.Value + random.NextDouble() * (max.Value - min.Value); 61 double min = bounds[index % bounds.Rows, 0]; 62 double max = bounds[index % bounds.Rows, 1]; 63 vector[index] = min + random.NextDouble() * (max - min); 72 64 } 73 65 74 66 /// <summary> 75 /// Checks if the minimum and maximum parameters are available and forwards the call to <see cref="Apply(IRandom, RealVector, DoubleValue, DoubleValue)"/>.67 /// Checks if the bounds parameters is available and forwards the call to <see cref="Apply(IRandom, RealVector, DoubleMatrix)"/>. 76 68 /// </summary> 77 69 /// <param name="random">The random number generator to use.</param> 78 70 /// <param name="realVector">The real vector to manipulate.</param> 79 71 protected override void Manipulate(IRandom random, RealVector realVector) { 80 if (MinimumParameter.ActualValue == null) throw new InvalidOperationException("UniformOnePositionManipulator: Parameter " + MinimumParameter.ActualName + " could not be found."); 81 if (MaximumParameter.ActualValue == null) throw new InvalidOperationException("UniformOnePositionManipulator: Parameter " + MaximumParameter.ActualName + " could not be found."); 82 Apply(random, realVector, MinimumParameter.ActualValue, MaximumParameter.ActualValue); 72 if (BoundsParameter.ActualValue == null) throw new InvalidOperationException("MichalewiczNonUniformOnePositionManipulator: Parameter " + BoundsParameter.ActualName + " could not be found."); 73 Apply(random, realVector, BoundsParameter.ActualValue); 83 74 } 84 75 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVector.cs
r3054 r3123 56 56 } 57 57 } 58 59 public virtual void Randomize(IRandom random, int startIndex, int length, DoubleMatrix bounds) { 60 if (length > 0) { 61 for (int i = startIndex; i < startIndex + length; i++) { 62 double min = bounds[i % bounds.Rows, 0]; 63 double max = bounds[i % bounds.Rows, 1]; 64 array[i] = min + (max - min) * random.NextDouble(); 65 } 66 OnReset(); 67 } 68 } 69 58 70 public void Randomize(IRandom random, double min, double max) { 59 71 Randomize(random, 0, Length, min, max); 60 72 } 73 74 public void Randomize(IRandom random, DoubleMatrix bounds) { 75 Randomize(random, 0, Length, bounds); 76 } 77 61 78 } 62 79 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorCreator.cs
r3060 r3123 47 47 get { return (IValueLookupParameter<IntValue>)Parameters["Length"]; } 48 48 } 49 public IValueLookupParameter<DoubleValue> MinimumParameter { 50 get { return (IValueLookupParameter<DoubleValue>)Parameters["Minimum"]; } 51 } 52 public IValueLookupParameter<DoubleValue> MaximumParameter { 53 get { return (IValueLookupParameter<DoubleValue>)Parameters["Maximum"]; } 49 public IValueLookupParameter<DoubleMatrix> BoundsParameter { 50 get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; } 54 51 } 55 52 … … 59 56 Parameters.Add(new LookupParameter<RealVector>("RealVector", "The vector which should be manipulated.")); 60 57 Parameters.Add(new ValueLookupParameter<IntValue>("Length", "The length of the vector.")); 61 Parameters.Add(new ValueLookupParameter<DoubleValue>("Minimum", "The lower bound for each element in the vector.")); 62 Parameters.Add(new ValueLookupParameter<DoubleValue>("Maximum", "The upper bound for each element in the vector.")); 58 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "A 2 column matrix specifying the lower and upper bound for each dimension. If there are less rows than dimension the bounds vector is cycled.")); 63 59 } 64 60 65 61 public sealed override IOperation Apply() { 66 RealVectorParameter.ActualValue = Create(RandomParameter.ActualValue, LengthParameter.ActualValue, MinimumParameter.ActualValue, MaximumParameter.ActualValue);62 RealVectorParameter.ActualValue = Create(RandomParameter.ActualValue, LengthParameter.ActualValue, BoundsParameter.ActualValue); 67 63 return base.Apply(); 68 64 } 69 65 70 protected abstract RealVector Create(IRandom random, IntValue length, Double Value minimum, DoubleValue maximum);66 protected abstract RealVector Create(IRandom random, IntValue length, DoubleMatrix bounds); 71 67 } 72 68 } -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Tests/MichalewiczNonUniformAllPositionsManipulatorTest.cs
r3061 r3123 86 86 TestRandom random = new TestRandom(); 87 87 RealVector parent, expected; 88 DoubleValue min, max, generationsDependency; 88 DoubleValue generationsDependency; 89 DoubleMatrix bounds; 89 90 IntValue currentGeneration, maximumGenerations; 90 91 bool exceptionFired; … … 94 95 parent = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 }); 95 96 expected = new RealVector(new double[] { 0.45, 0.22, 0.3, 0.6, 0.14 }); 96 min = new DoubleValue(0.3); 97 max = new DoubleValue(0.7); 97 bounds = new DoubleMatrix(new double[,] { { 0.3, 0.7 } }); 98 98 generationsDependency = new DoubleValue(0.1); 99 99 currentGeneration = new IntValue(1); 100 100 maximumGenerations = new IntValue(4); 101 MichalewiczNonUniformAllPositionsManipulator.Apply(random, parent, min, max, currentGeneration, maximumGenerations, generationsDependency);101 MichalewiczNonUniformAllPositionsManipulator.Apply(random, parent, bounds, currentGeneration, maximumGenerations, generationsDependency); 102 102 Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(expected, parent)); 103 103 // The following test is not based on published examples … … 106 106 random.DoubleNumbers = new double[] { 0.2, 0.5, 0.7, 0.8, 0.9, 0.5, 0.2, 0.5, 0.7, 0.8 }; 107 107 parent = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 }); 108 min = new DoubleValue(0.3); 109 max = new DoubleValue(0.7); 108 bounds = new DoubleMatrix(new double[,] { { 0.3, 0.7 } }); 110 109 generationsDependency = new DoubleValue(0.1); 111 110 currentGeneration = new IntValue(5); //current generation > max generation 112 111 maximumGenerations = new IntValue(4); 113 112 try { 114 MichalewiczNonUniformAllPositionsManipulator.Apply(random, parent, min, max, currentGeneration, maximumGenerations, generationsDependency);113 MichalewiczNonUniformAllPositionsManipulator.Apply(random, parent, bounds, currentGeneration, maximumGenerations, generationsDependency); 115 114 } 116 115 catch (System.ArgumentException) { -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Tests/MichalewiczNonUniformOnePositionManipulatorTest.cs
r3061 r3123 86 86 TestRandom random = new TestRandom(); 87 87 RealVector parent, expected; 88 DoubleValue min, max, generationsDependency; 88 DoubleValue generationsDependency; 89 DoubleMatrix bounds; 89 90 IntValue currentGeneration, maximumGenerations; 90 91 bool exceptionFired; … … 95 96 parent = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 }); 96 97 expected = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.34, 0.1 }); 97 min = new DoubleValue(0.3); 98 max = new DoubleValue(0.7); 98 bounds = new DoubleMatrix(new double[,] { { 0.3, 0.7 } }); 99 99 generationsDependency = new DoubleValue(0.1); 100 100 currentGeneration = new IntValue(1); 101 101 maximumGenerations = new IntValue(4); 102 MichalewiczNonUniformOnePositionManipulator.Apply(random, parent, min, max, currentGeneration, maximumGenerations, generationsDependency);102 MichalewiczNonUniformOnePositionManipulator.Apply(random, parent, bounds, currentGeneration, maximumGenerations, generationsDependency); 103 103 Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(expected, parent)); 104 104 // The following test is not based on published examples … … 108 108 random.DoubleNumbers = new double[] { 0.2, 0.7 }; 109 109 parent = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 }); 110 min = new DoubleValue(0.3); 111 max = new DoubleValue(0.7); 110 bounds = new DoubleMatrix(new double[,] { { 0.3, 0.7 } }); 112 111 generationsDependency = new DoubleValue(0.1); 113 112 currentGeneration = new IntValue(5); //current generation > max generation 114 113 maximumGenerations = new IntValue(4); 115 114 try { 116 MichalewiczNonUniformOnePositionManipulator.Apply(random, parent, min, max, currentGeneration, maximumGenerations, generationsDependency);115 MichalewiczNonUniformOnePositionManipulator.Apply(random, parent, bounds, currentGeneration, maximumGenerations, generationsDependency); 117 116 } 118 117 catch (System.ArgumentException) { -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Tests/UniformOnePositionManipulatorTest.cs
r3061 r3123 86 86 TestRandom random = new TestRandom(); 87 87 RealVector parent, expected; 88 Double Value min, max;88 DoubleMatrix bounds; 89 89 // The following test is not based on published examples 90 90 random.Reset(); … … 93 93 parent = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.5, 0.1 }); 94 94 expected = new RealVector(new double[] { 0.2, 0.2, 0.3, 0.3, 0.1 }); 95 min = new DoubleValue(0.2); 96 max = new DoubleValue(0.7); 97 UniformOnePositionManipulator.Apply(random, parent, min, max); 95 bounds = new DoubleMatrix(new double[,] { { 0.2, 0.7 } }); 96 UniformOnePositionManipulator.Apply(random, parent, bounds); 98 97 Assert.IsTrue(Auxiliary.RealVectorIsAlmostEqualByPosition(expected, parent)); 99 98 }
Note: See TracChangeset
for help on using the changeset viewer.