Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14648 was 14185, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 4.4 KB
RevLine 
[2773]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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
[4722]22using HeuristicLab.Common;
[2773]23using HeuristicLab.Core;
24using HeuristicLab.Data;
[3368]25using HeuristicLab.Operators;
[2773]26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
[3368]29namespace HeuristicLab.Random {
[2773]30  /// <summary>
[2924]31  /// An operator which creates a new Mersenne Twister pseudo random number generator.
[2773]32  /// </summary>
[2924]33  [Item("RandomCreator", "An operator which creates a new Mersenne Twister pseudo random number generator.")]
[3017]34  [StorableClass]
[2794]35  public sealed class RandomCreator : SingleSuccessorOperator {
[4722]36    #region Parameter Properties
[3048]37    public ValueLookupParameter<BoolValue> SetSeedRandomlyParameter {
38      get { return (ValueLookupParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
[2773]39    }
[3048]40    public ValueLookupParameter<IntValue> SeedParameter {
41      get { return (ValueLookupParameter<IntValue>)Parameters["Seed"]; }
[2773]42    }
[4258]43    public ValueParameter<IRandom> RandomTypeParameter {
44      get { return (ValueParameter<IRandom>)Parameters["RandomType"]; }
45    }
[2773]46    public LookupParameter<IRandom> RandomParameter {
47      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
48    }
[4722]49    #endregion
50
51    #region Properties
[3048]52    public BoolValue SetSeedRandomly {
[2773]53      get { return SetSeedRandomlyParameter.Value; }
54      set { SetSeedRandomlyParameter.Value = value; }
55    }
[3048]56    public IntValue Seed {
[2773]57      get { return SeedParameter.Value; }
58      set { SeedParameter.Value = value; }
59    }
[4258]60    public IRandom RandomType {
61      get { return RandomTypeParameter.Value; }
62      set { RandomTypeParameter.Value = value; }
63    }
[4722]64    #endregion
[2773]65
[4722]66    [StorableConstructor]
67    private RandomCreator(bool deserializing) : base(deserializing) { }
68    private RandomCreator(RandomCreator original, Cloner cloner) : base(original, cloner) { }
[2794]69    public RandomCreator()
[2773]70      : base() {
[3048]71      Parameters.Add(new ValueLookupParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
72      Parameters.Add(new ValueLookupParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
[4258]73      Parameters.Add(new ValueParameter<IRandom>("RandomType", "The type of pseudo random number generator which is created.", new MersenneTwister()));
[2794]74      Parameters.Add(new LookupParameter<IRandom>("Random", "The new pseudo random number generator which is initialized with the given seed."));
[2773]75    }
76
[4258]77    // BackwardsCompatibility3.3
78    #region Backwards compatible code (remove with 3.4)
79    [StorableHook(HookType.AfterDeserialization)]
[4722]80    private void AfterDeserialization() {
[4258]81      if (!Parameters.ContainsKey("RandomType"))
82        Parameters.Add(new ValueParameter<IRandom>("RandomType", "The type of pseudo random number generator which is created.", new MersenneTwister()));
83    }
84    #endregion
85
[4722]86    public override IDeepCloneable Clone(Cloner cloner) {
87      return new RandomCreator(this, cloner);
88    }
89
[2834]90    public override IOperation Apply() {
[3048]91      if (SetSeedRandomlyParameter.ActualValue == null) SetSeedRandomlyParameter.ActualValue = new BoolValue(true);
[2818]92      bool setSeedRandomly = SetSeedRandomlyParameter.ActualValue.Value;
[3048]93      if (SeedParameter.ActualValue == null) SeedParameter.ActualValue = new IntValue(0);
94      IntValue seed = SeedParameter.ActualValue;
[2773]95
96      if (setSeedRandomly) seed.Value = new System.Random().Next();
[4258]97      IRandom random = (IRandom)RandomType.Clone();
98      random.Reset(seed.Value);
99      RandomParameter.ActualValue = random;
[2773]100
101      return base.Apply();
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.