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.Parameters;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Operators {
|
---|
28 | [Item("SubScopesRandomCreator", "Creates pseudo random number generators for each subscope.")]
|
---|
29 | [StorableClass]
|
---|
30 | public sealed class SubScopesRandomCreator : SingleSuccessorOperator {
|
---|
31 | #region Parameter Properties
|
---|
32 | private LookupParameter<IRandom> GlobalRandomParameter {
|
---|
33 | get { return (LookupParameter<IRandom>)Parameters["GlobalRandom"]; }
|
---|
34 | }
|
---|
35 | private ScopeParameter CurrentScopeParameter {
|
---|
36 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
37 | }
|
---|
38 | private IScope CurrentScope {
|
---|
39 | get { return CurrentScopeParameter.ActualValue; }
|
---|
40 | }
|
---|
41 | #endregion
|
---|
42 |
|
---|
43 | [StorableConstructor]
|
---|
44 | private SubScopesRandomCreator(bool deserializing) : base(deserializing) { }
|
---|
45 | private SubScopesRandomCreator(SubScopesRandomCreator original, Cloner cloner)
|
---|
46 | : base(original, cloner) {
|
---|
47 | }
|
---|
48 | public SubScopesRandomCreator()
|
---|
49 | : base() {
|
---|
50 | #region Create parameters
|
---|
51 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope into which the random number generator is cloned."));
|
---|
52 | Parameters.Add(new LookupParameter<IRandom>("GlobalRandom", "A pseudo random number generator.", "Random"));
|
---|
53 | #endregion
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
57 | return new SubScopesRandomCreator(this, cloner);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public override IOperation Apply() {
|
---|
61 | IRandom random = GlobalRandomParameter.ActualValue;
|
---|
62 | IRandom subScopesRandom = (IRandom)random.Clone();
|
---|
63 | subScopesRandom.Reset(random.Next());
|
---|
64 | CurrentScope.Variables.Add(new Variable("Random", "A pseudo random number generator.", subScopesRandom));
|
---|
65 | return base.Apply();
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|