#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 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 {
///
/// An operator which creates a new Mersenne Twister pseudo random number generator.
///
[Item("RandomCreator", "An operator which creates a new Mersenne Twister pseudo random number generator.")]
[StorableType("8DDEF6F1-0E26-4B4B-8F42-4B6D27A843A7")]
public sealed class RandomCreator : SingleSuccessorOperator {
#region Parameter Properties
public ValueLookupParameter SetSeedRandomlyParameter {
get { return (ValueLookupParameter)Parameters["SetSeedRandomly"]; }
}
public ValueLookupParameter SeedParameter {
get { return (ValueLookupParameter)Parameters["Seed"]; }
}
public ValueParameter RandomTypeParameter {
get { return (ValueParameter)Parameters["RandomType"]; }
}
public LookupParameter RandomParameter {
get { return (LookupParameter)Parameters["Random"]; }
}
#endregion
#region Properties
public BoolValue SetSeedRandomly {
get { return SetSeedRandomlyParameter.Value; }
set { SetSeedRandomlyParameter.Value = value; }
}
public IntValue Seed {
get { return SeedParameter.Value; }
set { SeedParameter.Value = value; }
}
public IRandom RandomType {
get { return RandomTypeParameter.Value; }
set { RandomTypeParameter.Value = value; }
}
#endregion
[StorableConstructor]
private RandomCreator(bool deserializing) : base(deserializing) { }
private RandomCreator(RandomCreator original, Cloner cloner) : base(original, cloner) { }
public RandomCreator()
: base() {
Parameters.Add(new ValueLookupParameter("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
Parameters.Add(new ValueLookupParameter("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
Parameters.Add(new ValueParameter("RandomType", "The type of pseudo random number generator which is created.", new MersenneTwister()));
Parameters.Add(new LookupParameter("Random", "The new pseudo random number generator which is initialized with the given seed."));
}
// BackwardsCompatibility3.3
#region Backwards compatible code (remove with 3.4)
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
if (!Parameters.ContainsKey("RandomType"))
Parameters.Add(new ValueParameter("RandomType", "The type of pseudo random number generator which is created.", new MersenneTwister()));
}
#endregion
public override IDeepCloneable Clone(Cloner cloner) {
return new RandomCreator(this, cloner);
}
public override IOperation Apply() {
if (SetSeedRandomlyParameter.ActualValue == null) SetSeedRandomlyParameter.ActualValue = new BoolValue(true);
bool setSeedRandomly = SetSeedRandomlyParameter.ActualValue.Value;
if (SeedParameter.ActualValue == null) SeedParameter.ActualValue = new IntValue(0);
IntValue seed = SeedParameter.ActualValue;
if (setSeedRandomly) seed.Value = new System.Random().Next();
IRandom random = (IRandom)RandomType.Clone();
random.Reset(seed.Value);
RandomParameter.ActualValue = random;
return base.Apply();
}
}
}