Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Random/NormalRandomAdder.cs @ 834

Last change on this file since 834 was 763, checked in by gkronber, 16 years ago

removed visitor interfaces and methods in HeuristicLab.Data and fixed classes in HeuristicLab.Random to work without visitor methods. #343 (Rethink about usefulness of visitors for ObjectData and Constraints)

File size: 5.4 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 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
[426]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
[2]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;
[183]58      AddVariable(new Variable("Sigma", new DoubleData(1.0)));
[2]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;
[719]69      double mu = GetVariableValue<DoubleData>("Mu", scope, true).Data;
70      double sigma = GetVariableValue<DoubleData>("Sigma", scope, true).Data;
[2]71      NormalDistributedRandom normal = new NormalDistributedRandom(mt, mu, sigma * factor);
72
[763]73      AddNormal(value, normal);
[2]74      return null;
75    }
76
[763]77    private void AddNormal(IObjectData value, NormalDistributedRandom normal) {
78      // dispatch manually based on dynamic type
79      if (value is IntData)
80        AddNormal((IntData)value, normal);
81      else if (value is ConstrainedIntData)
82        AddNormal((ConstrainedIntData)value, normal);
83      else if (value is ConstrainedDoubleData)
84        AddNormal((ConstrainedDoubleData)value, normal);
85      else if (value is DoubleData)
86        AddNormal((DoubleData)value, normal);
87      else throw new InvalidOperationException("Can't handle type " + value.GetType().Name);
88    }
89    public void AddNormal(DoubleData data, NormalDistributedRandom normal) {
90      data.Data += normal.NextDouble();
91    }
[2]92
[763]93    public void AddNormal(ConstrainedDoubleData data, NormalDistributedRandom normal) {
94      for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
95        double newValue = data.Data + normal.NextDouble();
96        if (IsIntegerConstrained(data)) {
97          newValue = Math.Round(newValue);
[2]98        }
[763]99        if (data.TrySetData(newValue)) {
100          return;
101        }
[2]102      }
[763]103      throw new InvalidProgramException("Coudn't find a valid value");
104    }
[2]105
[763]106    public void AddNormal(IntData data, NormalDistributedRandom normal) {
107      data.Data = (int)Math.Round(data.Data + normal.NextDouble());
108    }
[2]109
[763]110    public void AddNormal(ConstrainedIntData data, NormalDistributedRandom normal) {
111      for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
112        if (data.TrySetData((int)Math.Round(data.Data + normal.NextDouble())))
113          return;
[2]114      }
[763]115      throw new InvalidProgramException("Couldn't find a valid value.");
116    }
[2]117
[763]118    private bool IsIntegerConstrained(ConstrainedDoubleData data) {
119      foreach (IConstraint constraint in data.Constraints) {
120        if (constraint is IsIntegerConstraint) {
121          return true;
[2]122        }
123      }
[763]124      return false;
[2]125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.