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