Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Random/NormalRandomizer.cs @ 6575

Last change on this file since 6575 was 469, checked in by gkronber, 16 years ago

fixed #238 by using floor instead of round when we use a uniform-distribution in combination with integer variables

File size: 4.6 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 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
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;
53      AddVariable(new Variable("Sigma", new DoubleData(1.0)));
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          if(IsIntegerConstrained(data)) {
82            r = Math.Round(r);
83          }
84          if(data.TrySetData(r)) {
85            return;
86          }
87        }
88        throw new InvalidProgramException("Couldn't find a valid value in 100 tries with mu=" + normal.Mu + " sigma=" + normal.Sigma);
89      }
90
91      public override void Visit(ConstrainedIntData data) {
92        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
93          double r = normal.NextDouble();
94          if(data.TrySetData((int)Math.Round(r))) // since r is a continuous normally distributed random variable rounding should be OK
95            return;
96        }
97        throw new InvalidProgramException("Couldn't find a valid value");
98      }
99
100      public override void Visit(DoubleData data) {
101        data.Data = normal.NextDouble();
102      }
103
104      public override void Visit(IntData data) {
105        data.Data = (int)Math.Round(normal.NextDouble());
106      }
107
108
109      private bool IsIntegerConstrained(ConstrainedDoubleData data) {
110        foreach(IConstraint constraint in data.Constraints) {
111          if(constraint is IsIntegerConstraint) {
112            return true;
113          }
114        }
115        return false;
116      }
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.