Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.Random/NormalRandomAdder.cs @ 2092

Last change on this file since 2092 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: 5.0 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 NormalRandomAdder : OperatorBase {
31    private static int MAX_NUMBER_OF_TRIES = 100;
32
33    public override string Description {
34      get {
35        return @"Samples a normally distributed (mu, sigma * shakingFactor) random variable and adds the result to variable 'Value'.
36       
37If a constraint for the allowed range of 'Value' is defined and the result of the operation would be smaller then
38the smallest allowed value then 'Value' is set to the lower bound and vice versa for the upper bound.";
39      }
40    }
41
42    public double Mu {
43      get { return ((DoubleData)GetVariable("Mu").Value).Data; }
44      set { ((DoubleData)GetVariable("Mu").Value).Data = value; }
45    }
46    public double Sigma {
47      get { return ((DoubleData)GetVariable("Sigma").Value).Data; }
48      set { ((DoubleData)GetVariable("Sigma").Value).Data = value; }
49    }
50
51    public NormalRandomAdder() {
52      AddVariableInfo(new VariableInfo("Mu", "Parameter mu of the normal distribution", typeof(DoubleData), VariableKind.None));
53      GetVariableInfo("Mu").Local = true;
54      AddVariable(new Variable("Mu", new DoubleData(0.0)));
55
56      AddVariableInfo(new VariableInfo("Sigma", "Parameter sigma of the normal distribution", typeof(DoubleData), VariableKind.None));
57      GetVariableInfo("Sigma").Local = true;
58      AddVariable(new Variable("Sigma", new DoubleData(1.0)));
59
60      AddVariableInfo(new VariableInfo("Value", "The value to manipulate (actual type is one of: IntData, DoubleData, ConstrainedIntData, ConstrainedDoubleData)", typeof(IObjectData), VariableKind.In));
61      AddVariableInfo(new VariableInfo("ShakingFactor", "Determines the force of the shaking factor (effective sigma = sigma * shakingFactor)", typeof(DoubleData), VariableKind.In));
62      AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In));
63    }
64
65    public override IOperation Apply(IScope scope) {
66      IObjectData value = GetVariableValue<IObjectData>("Value", scope, false);
67      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
68      double factor = GetVariableValue<DoubleData>("ShakingFactor", scope, true).Data;
69      double mu = GetVariableValue<DoubleData>("Mu", null, false).Data;
70      double sigma = GetVariableValue<DoubleData>("Sigma", null, false).Data;
71      NormalDistributedRandom normal = new NormalDistributedRandom(mt, mu, sigma * factor);
72
73      value.Accept(new RandomAdderVisitor(normal));
74
75      return null;
76    }
77
78
79    private class RandomAdderVisitor : ObjectDataVisitorBase {
80      private NormalDistributedRandom normal;
81      public RandomAdderVisitor(NormalDistributedRandom normal) {
82        this.normal = normal;
83      }
84
85      public override void Visit(DoubleData data) {
86        data.Data += normal.NextDouble();
87      }
88
89      public override void Visit(ConstrainedDoubleData data) {
90        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
91          double newValue = data.Data + normal.NextDouble();
92          if(IsIntegerConstrained(data)) {
93            newValue = Math.Round(newValue);
94          }
95          if(data.TrySetData(newValue)) {
96            return;
97          }
98        }
99        throw new InvalidProgramException("Coudn't find a valid value");
100      }
101
102      public override void Visit(IntData data) {
103        data.Data = (int)Math.Round(data.Data + normal.NextDouble());
104      }
105
106      public override void Visit(ConstrainedIntData data) {
107        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
108          if(data.TrySetData((int)Math.Round(data.Data + normal.NextDouble())))
109            return;
110        }
111        throw new InvalidProgramException("Couldn't find a valid value.");
112      }
113
114      private bool IsIntegerConstrained(ConstrainedDoubleData data) {
115        foreach(IConstraint constraint in data.Constraints) {
116          if(constraint is IsIntegerConstraint) {
117            return true;
118          }
119        }
120        return false;
121      }
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.