#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Random {
///
/// Uniformly distributed random number generator.
///
[StorableClass]
[Item("UniformRandomizer", "Initializes the value of variable 'Value' to a random value uniformly distributed between 'Min' and 'Max'")]
public class UniformRandomizer : SingleSuccessorOperator {
#region Parameter Properties
public ILookupParameter RandomParameter {
get { return (ILookupParameter)Parameters["Random"]; }
}
public IValueLookupParameter MinParameter {
get { return (IValueLookupParameter)Parameters["Min"]; }
}
public IValueLookupParameter MaxParameter {
get { return (IValueLookupParameter)Parameters["Max"]; }
}
public ILookupParameter ValueParameter {
get { return (ILookupParameter)Parameters["Value"]; }
}
#endregion
#region Properties
public DoubleValue Min {
get { return MinParameter.ActualValue; }
set { MinParameter.ActualValue = value; }
}
public DoubleValue Max {
get { return MaxParameter.ActualValue; }
set { MaxParameter.ActualValue = value; }
}
#endregion
[StorableConstructor]
protected UniformRandomizer(bool deserializing) : base(deserializing) { }
protected UniformRandomizer(UniformRandomizer original, Cloner cloner) : base(original, cloner) { }
///
/// Initializes a new instance of with four variable infos
/// (Value, Random, Max and Min), being a random number generator
/// between 0.0 and 1.0.
///
public UniformRandomizer()
: base() {
Parameters.Add(new LookupParameter("Random", "A random generator that supplies uniformly distributed values."));
Parameters.Add(new ValueLookupParameter("Min", "The minimal allowed value (inclusive)"));
Parameters.Add(new ValueLookupParameter("Max", "The maximal allowed value (exclusive)"));
Parameters.Add(new LookupParameter("Value", "The value that should be set to a random value."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new UniformRandomizer(this, cloner);
}
///
/// Generates a new uniformly distributed random variable.
///
public override IOperation Apply() {
IRandom random = RandomParameter.ActualValue;
double min = MinParameter.ActualValue.Value;
double max = MaxParameter.ActualValue.Value;
ValueParameter.ActualValue = new DoubleValue(random.NextDouble() * (max - min) + min);
return null;
}
}
}