Changeset 3269 for trunk/sources/HeuristicLab.Random
- Timestamp:
- 04/05/10 18:52:23 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Random/3.3
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Random/3.3/HeuristicLab.Random-3.3.csproj
r2900 r3269 93 93 <SubType>Code</SubType> 94 94 </Compile> 95 <Compile Include="NormalRandomizer.cs" /> 95 96 <Compile Include="Properties\AssemblyInfo.cs" /> 96 97 <Compile Include="RandomCreator.cs" /> 98 <Compile Include="UniformRandomizer.cs" /> 97 99 </ItemGroup> 98 100 <ItemGroup> -
trunk/sources/HeuristicLab.Random/3.3/NormalRandomizer.cs
r2524 r3269 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-20 08Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 26 26 using HeuristicLab.Data; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Operators; 29 using HeuristicLab.Parameters; 28 30 29 31 namespace HeuristicLab.Random { … … 31 33 /// Normally distributed random number generator. 32 34 /// </summary> 33 [EmptyStorableClass] 34 public class NormalRandomizer : OperatorBase { 35 private static int MAX_NUMBER_OF_TRIES = 100; 36 37 /// <inheritdoc select="summary"/> 38 public override string Description { 39 get { return "Initializes the value of variable 'Value' to a random value normally distributed with 'Mu' and 'Sigma'."; } 35 [StorableClass] 36 [Item("NormalRandomizer", "Initializes the value of variable 'Value' to a random value normally distributed with parameters 'Mu' and 'Sigma'")] 37 public class NormalRandomizer : SingleSuccessorOperator { 38 #region parameter properties 39 public ILookupParameter<IRandom> RandomParameter { 40 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 40 41 } 41 42 /// <summary> 43 /// Gets or sets the value for µ. 44 /// </summary> 45 /// <remarks>Gets or sets the variable with the name <c>Mu</c> through the method 46 /// <see cref="OperatorBase.GetVariable"/> of class <see cref="OperatorBase"/>.</remarks> 47 public double Mu { 48 get { return ((DoubleData)GetVariable("Mu").Value).Data; } 49 set { ((DoubleData)GetVariable("Mu").Value).Data = value; } 42 public IValueLookupParameter<DoubleValue> MuParameter { 43 get { return (IValueLookupParameter<DoubleValue>)Parameters["Mu"]; } 50 44 } 51 /// <summary> 52 /// Gets or sets the value for sigma. 53 /// </summary> 54 /// <remarks>Gets or sets the variable with the name <c>Sigma</c> through the method 55 /// <see cref="OperatorBase.GetVariable"/> of class <see cref="OperatorBase"/>.</remarks> 56 public double Sigma { 57 get { return ((DoubleData)GetVariable("Sigma").Value).Data; } 58 set { ((DoubleData)GetVariable("Sigma").Value).Data = value; } 45 public IValueLookupParameter<DoubleValue> SigmaParameter { 46 get { return (IValueLookupParameter<DoubleValue>)Parameters["Sigma"]; } 59 47 } 60 48 public ILookupParameter<DoubleValue> ValueParameter { 49 get { return (ILookupParameter<DoubleValue>)Parameters["Value"]; } 50 } 51 #endregion 52 #region Properties 53 public DoubleValue Mu { 54 get { return MuParameter.ActualValue; } 55 set { MuParameter.ActualValue = value; } 56 } 57 public DoubleValue Max { 58 get { return SigmaParameter.ActualValue; } 59 set { SigmaParameter.ActualValue = value; } 60 } 61 #endregion 61 62 /// <summary> 62 63 /// Initializes a new instance of <see cref="NormalRandomizer"/> with four variable infos … … 64 65 /// </summary> 65 66 public NormalRandomizer() { 66 AddVariableInfo(new VariableInfo("Mu", "Parameter mu of the normal distribution", typeof(DoubleData), VariableKind.None)); 67 GetVariableInfo("Mu").Local = true; 68 AddVariable(new Variable("Mu", new DoubleData(0.0))); 69 70 AddVariableInfo(new VariableInfo("Sigma", "Parameter sigma of the normal distribution", typeof(DoubleData), VariableKind.None)); 71 GetVariableInfo("Sigma").Local = true; 72 AddVariable(new Variable("Sigma", new DoubleData(1.0))); 73 74 AddVariableInfo(new VariableInfo("Value", "The value to manipulate (actual type is one of: IntData, DoubleData, ConstrainedIntData, ConstrainedDoubleData)", typeof(IObjectData), VariableKind.In)); 75 AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In)); 67 Parameters.Add(new LookupParameter<IRandom>("Random", "A random generator that supplies uniformly distributed values.")); 68 Parameters.Add(new ValueLookupParameter<DoubleValue>("Mu", "Mu parameter of the normal distribution (N(mu,sigma)).")); 69 Parameters.Add(new ValueLookupParameter<DoubleValue>("Sigma", "Sigma parameter of the normal distribution (N(mu,sigma)).")); 70 Parameters.Add(new LookupParameter<DoubleValue>("Value", "The value that should be set to a random value.")); 76 71 } 77 72 78 73 /// <summary> 79 /// Generates a new normally distributed random variable and assigns it to the specified variable 80 /// in the given <paramref name="scope"/>. 74 /// Generates a new normally distributed random variable and assigns it to the specified variable. 81 75 /// </summary> 82 /// <param name="scope">The scope where to assign the new random value to.</param> 83 /// <returns><c>null</c>.</returns> 84 public override IOperation Apply(IScope scope) { 85 IObjectData value = GetVariableValue<IObjectData>("Value", scope, false); 86 MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true); 87 double mu = GetVariableValue<DoubleData>("Mu", scope, true).Data; 88 double sigma = GetVariableValue<DoubleData>("Sigma", scope, true).Data; 76 public override IOperation Apply() { 77 IRandom random = RandomParameter.ActualValue; 78 double mu = MuParameter.ActualValue.Value; 79 double sigma = SigmaParameter.ActualValue.Value; 89 80 90 NormalDistributedRandom n = new NormalDistributedRandom(mt, mu, sigma);91 RandomizeNormal(value, n);81 NormalDistributedRandom normalRandom = new NormalDistributedRandom(random, mu, sigma); 82 ValueParameter.ActualValue = new DoubleValue(normalRandom.NextDouble()); 92 83 return null; 93 }94 95 private void RandomizeNormal(IObjectData value, NormalDistributedRandom n) {96 // dispatch manually based on dynamic type97 if (value is IntData)98 RandomizeNormal((IntData)value, n);99 else if (value is DoubleData)100 RandomizeNormal((DoubleData)value, n);101 else throw new InvalidOperationException("Can't handle type " + value.GetType().Name);102 }103 104 /// <summary>105 /// Generates a new double random number based on a continuous, normally distributed random number106 /// generator <paramref name="normal"/>.107 /// </summary>108 /// <param name="data">The double object where to assign the new value to.</param>109 /// <param name="normal">The continuous, normally distributed random variable.</param>110 public void RandomizeNormal(DoubleData data, NormalDistributedRandom normal) {111 data.Data = normal.NextDouble();112 }113 114 /// <summary>115 /// Generates a new int random number based on a continuous, normally distributed random number116 /// generator <paramref name="normal"/>.117 /// </summary>118 /// <param name="data">The int object where to assign the new value to.</param>119 /// <param name="normal">The continuous, normally distributed random variable.</param>120 public void RandomizeNormal(IntData data, NormalDistributedRandom normal) {121 data.Data = (int)Math.Round(normal.NextDouble());122 84 } 123 85 } -
trunk/sources/HeuristicLab.Random/3.3/UniformRandomizer.cs
r2524 r3269 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-20 08Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 26 26 using HeuristicLab.Data; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Operators; 29 using HeuristicLab.Parameters; 28 30 29 31 namespace HeuristicLab.Random { … … 31 33 /// Uniformly distributed random number generator. 32 34 /// </summary> 33 [ EmptyStorableClass]34 public class UniformRandomizer : OperatorBase {35 private static int MAX_NUMBER_OF_TRIES = 100;36 /// <inheritdoc select="summary"/>37 public override string Description{38 get { return "Initializes the value of variable 'Value' to a random value uniformly distributed between 'Min' and 'Max' (exclusive)"; }35 [StorableClass] 36 [Item("UniformRandomizer", "Initializes the value of variable 'Value' to a random value uniformly distributed between 'Min' and 'Max'")] 37 public class UniformRandomizer : SingleSuccessorOperator { 38 #region parameter properties 39 public ILookupParameter<IRandom> RandomParameter { 40 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 39 41 } 40 41 /// <summary> 42 /// Gets or sets the maximum value of the random number generator (exclusive). 43 /// </summary> 44 /// <remarks>Gets or sets the variable with name <c>Max</c> through the 45 /// <see cref="OperatorBase.GetVariable"/> method of class <see cref="OperatorBase"/>.</remarks> 46 public double Max { 47 get { return ((DoubleData)GetVariable("Max").Value).Data; } 48 set { ((DoubleData)GetVariable("Max").Value).Data = value; } 42 public IValueLookupParameter<DoubleValue> MinParameter { 43 get { return (IValueLookupParameter<DoubleValue>)Parameters["Min"]; } 49 44 } 50 /// <summary> 51 /// Gets or sets the minimum value of the random number generator. 52 /// </summary> 53 /// <remarks>Gets or sets the variable with name <c>Min</c> through the 54 /// <see cref="OperatorBase.GetVariable"/> method of class <see cref="OperatorBase"/>.</remarks> 55 public double Min { 56 get { return ((DoubleData)GetVariable("Min").Value).Data; } 57 set { ((DoubleData)GetVariable("Min").Value).Data = value; } 45 public IValueLookupParameter<DoubleValue> MaxParameter { 46 get { return (IValueLookupParameter<DoubleValue>)Parameters["Max"]; } 58 47 } 48 public ILookupParameter<DoubleValue> ValueParameter { 49 get { return (ILookupParameter<DoubleValue>)Parameters["Value"]; } 50 } 51 #endregion 52 #region Properties 53 public DoubleValue Min { 54 get { return MinParameter.ActualValue; } 55 set { MinParameter.ActualValue = value; } 56 } 57 public DoubleValue Max { 58 get { return MaxParameter.ActualValue; } 59 set { MaxParameter.ActualValue = value; } 60 } 61 #endregion 59 62 60 63 /// <summary> … … 63 66 /// between 0.0 and 1.0. 64 67 /// </summary> 65 public UniformRandomizer() { 66 AddVariableInfo(new VariableInfo("Value", "The value to manipulate (type is one of: IntData, ConstrainedIntData, DoubleData, ConstrainedDoubleData)", typeof(IObjectData), VariableKind.In)); 67 AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In)); 68 AddVariableInfo(new VariableInfo("Min", "Lower bound of the uniform distribution (inclusive)", typeof(DoubleData), VariableKind.None)); 69 GetVariableInfo("Min").Local = true; 70 AddVariable(new Variable("Min", new DoubleData(0.0))); 71 72 AddVariableInfo(new VariableInfo("Max", "Upper bound of the uniform distribution (exclusive)", typeof(DoubleData), VariableKind.None)); 73 GetVariableInfo("Max").Local = true; 74 AddVariable(new Variable("Max", new DoubleData(1.0))); 68 public UniformRandomizer() 69 : base() { 70 Parameters.Add(new LookupParameter<IRandom>("Random", "A random generator that supplies uniformly distributed values.")); 71 Parameters.Add(new ValueLookupParameter<DoubleValue>("Min", "The minimal allowed value (inclusive)")); 72 Parameters.Add(new ValueLookupParameter<DoubleValue>("Max", "The maximal allowed value (exclusive)")); 73 Parameters.Add(new LookupParameter<DoubleValue>("Value", "The value that should be set to a random value.")); 75 74 } 76 75 … … 78 77 /// Generates a new uniformly distributed random variable. 79 78 /// </summary> 80 /// <param name="scope">The scope where to apply the random number generator.</param> 81 /// <returns><c>null</c>.</returns> 82 public override IOperation Apply(IScope scope) { 83 IObjectData value = GetVariableValue<IObjectData>("Value", scope, false); 84 MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true); 85 double min = GetVariableValue<DoubleData>("Min", scope, true).Data; 86 double max = GetVariableValue<DoubleData>("Max", scope, true).Data; 79 public override IOperation Apply() { 80 IRandom random = RandomParameter.ActualValue; 81 double min = MinParameter.ActualValue.Value; 82 double max = MaxParameter.ActualValue.Value; 87 83 88 RandomizeUniform(value, mt, min, max);84 ValueParameter.ActualValue = new DoubleValue(random.NextDouble() * (max - min) + min); 89 85 return null; 90 86 } 91 92 /// <summary> 93 /// Generates a new random number depending on the type of the <paramref name="value"/>. 94 /// </summary> 95 /// <exception cref="ArgumentException">Thrown when an unhandleable type appears.</exception> 96 /// <param name="value">The object whose data should be a new randomly generated number.</param> 97 /// <param name="mt">The MersenneTwister to generate a new random number.</param> 98 /// <param name="min">The left border of the interval in which the next random number has to lie.</param> 99 /// <param name="max">The right border (exclusive) of the interval in which the next random number 100 /// has to lie.</param> 101 private void RandomizeUniform(IObjectData value, MersenneTwister mt, double min, double max) { 102 // Dispatch manually based on dynamic type, 103 // a bit awkward but necessary until we create a better type hierarchy for numeric types (gkronber 15.11.2008). 104 if (value is DoubleData) 105 RandomizeUniform((DoubleData)value, mt, min, max); 106 else if (value is IntData) 107 RandomizeUniform((IntData)value, mt, min, max); 108 else throw new ArgumentException("Can't handle type " + value.GetType().Name); 109 } 110 111 /// <summary> 112 /// Generates a new double random number. 113 /// </summary> 114 /// <param name="data">The double object which the new value is assigned to.</param> 115 /// <param name="mt">The random number generator.</param> 116 /// <param name="min">The left border of the interval in which the next random number has to lie.</param> 117 /// <param name="max">The right border (exclusive) of the interval in which the next random number 118 /// has to lie.</param> 119 public void RandomizeUniform(DoubleData data, MersenneTwister mt, double min, double max) { 120 data.Data = mt.NextDouble() * (max - min) + min; 121 } 122 123 /// <summary> 124 /// Generates a new int random number. 125 /// </summary> 126 /// <param name="data">The int object which the new value is assigned to.</param> 127 /// <param name="mt">The random number generator.</param> 128 /// <param name="min">The left border of the interval in which the next random number has to lie.</param> 129 /// <param name="max">The right border (exclusive) of the interval in which the next random number 130 /// has to lie.</param> 131 public void RandomizeUniform(IntData data, MersenneTwister mt, double min, double max) { 132 data.Data = (int)Math.Floor(mt.NextDouble() * (max - min) + min); 133 } 134 } 87 } 135 88 }
Note: See TracChangeset
for help on using the changeset viewer.