Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Random/3.3/RandomCreator.cs @ 4682

Last change on this file since 4682 was 4258, checked in by swagner, 14 years ago

Worked in integration of FastRandom PRNG (#1114)

File size: 4.1 KB
RevLine 
[2773]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2773]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Core;
23using HeuristicLab.Data;
[3368]24using HeuristicLab.Operators;
[2773]25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
[3368]28namespace HeuristicLab.Random {
[2773]29  /// <summary>
[2924]30  /// An operator which creates a new Mersenne Twister pseudo random number generator.
[2773]31  /// </summary>
[2924]32  [Item("RandomCreator", "An operator which creates a new Mersenne Twister pseudo random number generator.")]
[3017]33  [StorableClass]
[2794]34  public sealed class RandomCreator : SingleSuccessorOperator {
[3048]35    public ValueLookupParameter<BoolValue> SetSeedRandomlyParameter {
36      get { return (ValueLookupParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
[2773]37    }
[3048]38    public ValueLookupParameter<IntValue> SeedParameter {
39      get { return (ValueLookupParameter<IntValue>)Parameters["Seed"]; }
[2773]40    }
[4258]41    public ValueParameter<IRandom> RandomTypeParameter {
42      get { return (ValueParameter<IRandom>)Parameters["RandomType"]; }
43    }
[2773]44    public LookupParameter<IRandom> RandomParameter {
45      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
46    }
[3048]47    public BoolValue SetSeedRandomly {
[2773]48      get { return SetSeedRandomlyParameter.Value; }
49      set { SetSeedRandomlyParameter.Value = value; }
50    }
[3048]51    public IntValue Seed {
[2773]52      get { return SeedParameter.Value; }
53      set { SeedParameter.Value = value; }
54    }
[4258]55    public IRandom RandomType {
56      get { return RandomTypeParameter.Value; }
57      set { RandomTypeParameter.Value = value; }
58    }
[2773]59
[2794]60    public RandomCreator()
[2773]61      : base() {
[3048]62      Parameters.Add(new ValueLookupParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
63      Parameters.Add(new ValueLookupParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
[4258]64      Parameters.Add(new ValueParameter<IRandom>("RandomType", "The type of pseudo random number generator which is created.", new MersenneTwister()));
[2794]65      Parameters.Add(new LookupParameter<IRandom>("Random", "The new pseudo random number generator which is initialized with the given seed."));
[2773]66    }
[4258]67    [StorableConstructor]
68    private RandomCreator(bool deserializing) : base(deserializing) { }
[2773]69
[4258]70    // BackwardsCompatibility3.3
71    #region Backwards compatible code (remove with 3.4)
72    [StorableHook(HookType.AfterDeserialization)]
73    private void AfterDeserializationHook() {
74      if (!Parameters.ContainsKey("RandomType"))
75        Parameters.Add(new ValueParameter<IRandom>("RandomType", "The type of pseudo random number generator which is created.", new MersenneTwister()));
76    }
77    #endregion
78
[2834]79    public override IOperation Apply() {
[3048]80      if (SetSeedRandomlyParameter.ActualValue == null) SetSeedRandomlyParameter.ActualValue = new BoolValue(true);
[2818]81      bool setSeedRandomly = SetSeedRandomlyParameter.ActualValue.Value;
[3048]82      if (SeedParameter.ActualValue == null) SeedParameter.ActualValue = new IntValue(0);
83      IntValue seed = SeedParameter.ActualValue;
[2773]84
85      if (setSeedRandomly) seed.Value = new System.Random().Next();
[4258]86      IRandom random = (IRandom)RandomType.Clone();
87      random.Reset(seed.Value);
88      RandomParameter.ActualValue = random;
[2773]89
90      return base.Apply();
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.