Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Random/NormalRandomizer.cs @ 440

Last change on this file since 440 was 426, checked in by gkronber, 16 years ago

added properties to access parameters of randomizers (must be combined with r425) (ticket #225)

File size: 4.5 KB
RevLine 
[2]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
[426]37    public double Mu {
38      get { return ((DoubleData)GetVariable("Mu").Value).Data; }
39      set { ((DoubleData)GetVariable("Mu").Value).Data = value; }
40    }
41    public double Sigma {
42      get { return ((DoubleData)GetVariable("Sigma").Value).Data; }
43      set { ((DoubleData)GetVariable("Sigma").Value).Data = value; }
44    }
45
[2]46    public NormalRandomizer() {
47      AddVariableInfo(new VariableInfo("Mu", "Parameter mu of the normal distribution", typeof(DoubleData), VariableKind.None));
48      GetVariableInfo("Mu").Local = true;
49      AddVariable(new Variable("Mu", new DoubleData(0.0)));
50
51      AddVariableInfo(new VariableInfo("Sigma", "Parameter sigma of the normal distribution", typeof(DoubleData), VariableKind.None));
52      GetVariableInfo("Sigma").Local = true;
[183]53      AddVariable(new Variable("Sigma", new DoubleData(1.0)));
[2]54
55      AddVariableInfo(new VariableInfo("Value", "The value to manipulate (actual type is one of: IntData, DoubleData, ConstrainedIntData, ConstrainedDoubleData)", typeof(IObjectData), VariableKind.In));
56      AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In));
57    }
58
59    public override IOperation Apply(IScope scope) {
60      IObjectData value = GetVariableValue<IObjectData>("Value", scope, false);
61      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
62      double mu = GetVariableValue<DoubleData>("Mu", null, false).Data;
63      double sigma = GetVariableValue<DoubleData>("Sigma", null, false).Data;
64      NormalDistributedRandom normal = new NormalDistributedRandom(mt, mu, sigma);
65
66      value.Accept(new RandomizerVisitor(normal));
67
68      return null;
69    }
70
71    private class RandomizerVisitor : ObjectDataVisitorBase {
72      private NormalDistributedRandom normal;
73
74      public RandomizerVisitor(NormalDistributedRandom normal) {
75        this.normal = normal;
76      }
77
78      public override void Visit(ConstrainedDoubleData data) {
79        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
80          double r = normal.NextDouble();
81
82          if(IsIntegerConstrained(data)) {
83            r = Math.Round(r);
84          }
85
86          if(data.TrySetData(r)) {
87            return;
88          }
89        }
90        throw new InvalidProgramException("Couldn't find a valid value in 100 tries with mu=" + normal.Mu + " sigma=" + normal.Sigma);
91      }
92
93      public override void Visit(ConstrainedIntData data) {
94
95        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
96          double r = normal.NextDouble();
97          if(data.TrySetData((int)Math.Round(r)))
98            return;
99        }
100
101        throw new InvalidProgramException("Couldn't find a valid value");
102      }
103
104      public override void Visit(DoubleData data) {
105        data.Data = normal.NextDouble();
106      }
107
108      public override void Visit(IntData data) {
109        data.Data = (int)Math.Round(normal.NextDouble());
110      }
111
112
113      private bool IsIntegerConstrained(ConstrainedDoubleData data) {
114        foreach(IConstraint constraint in data.Constraints) {
115          if(constraint is IsIntegerConstraint) {
116            return true;
117          }
118        }
119        return false;
120      }
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.