Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.Random/NormalRandomizer.cs @ 5155

Last change on this file since 5155 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Constraints;
28
29namespace HeuristicLab.Random {
30  public class NormalRandomizer : OperatorBase {
31    private static int MAX_NUMBER_OF_TRIES = 100;
32
33    public override string Description {
34      get { return "Initializes the value of variable 'Value' to a random value normally distributed with 'Mu' and 'Sigma'."; }
35    }
36
37    public NormalRandomizer() {
38      AddVariableInfo(new VariableInfo("Mu", "Parameter mu of the normal distribution", typeof(DoubleData), VariableKind.None));
39      GetVariableInfo("Mu").Local = true;
40      AddVariable(new Variable("Mu", new DoubleData(0.0)));
41
42      AddVariableInfo(new VariableInfo("Sigma", "Parameter sigma of the normal distribution", typeof(DoubleData), VariableKind.None));
43      GetVariableInfo("Sigma").Local = true;
44      AddVariable(new Variable("Sigma", new DoubleData(0.0)));
45
46      AddVariableInfo(new VariableInfo("Value", "The value to manipulate (actual type is one of: IntData, DoubleData, ConstrainedIntData, ConstrainedDoubleData)", typeof(IObjectData), VariableKind.In));
47      AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In));
48    }
49
50    public override IOperation Apply(IScope scope) {
51      IObjectData value = GetVariableValue<IObjectData>("Value", scope, false);
52      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
53      double mu = GetVariableValue<DoubleData>("Mu", null, false).Data;
54      double sigma = GetVariableValue<DoubleData>("Sigma", null, false).Data;
55      NormalDistributedRandom normal = new NormalDistributedRandom(mt, mu, sigma);
56
57      value.Accept(new RandomizerVisitor(normal));
58
59      return null;
60    }
61
62    private class RandomizerVisitor : ObjectDataVisitorBase {
63      private NormalDistributedRandom normal;
64
65      public RandomizerVisitor(NormalDistributedRandom normal) {
66        this.normal = normal;
67      }
68
69      public override void Visit(ConstrainedDoubleData data) {
70        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
71          double r = normal.NextDouble();
72
73          if(IsIntegerConstrained(data)) {
74            r = Math.Round(r);
75          }
76
77          if(data.TrySetData(r)) {
78            return;
79          }
80        }
81        throw new InvalidProgramException("Couldn't find a valid value in 100 tries with mu=" + normal.Mu + " sigma=" + normal.Sigma);
82      }
83
84      public override void Visit(ConstrainedIntData data) {
85
86        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
87          double r = normal.NextDouble();
88          if(data.TrySetData((int)Math.Round(r)))
89            return;
90        }
91
92        throw new InvalidProgramException("Couldn't find a valid value");
93      }
94
95      public override void Visit(DoubleData data) {
96        data.Data = normal.NextDouble();
97      }
98
99      public override void Visit(IntData data) {
100        data.Data = (int)Math.Round(normal.NextDouble());
101      }
102
103
104      private bool IsIntegerConstrained(ConstrainedDoubleData data) {
105        foreach(IConstraint constraint in data.Constraints) {
106          if(constraint is IsIntegerConstraint) {
107            return true;
108          }
109        }
110        return false;
111      }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.