Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.Random/NormalRandomAdder.cs @ 14832

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

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 4.7 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 NormalRandomAdder() {
43      AddVariableInfo(new VariableInfo("Mu", "Parameter mu of the normal distribution", typeof(DoubleData), VariableKind.None));
44      GetVariableInfo("Mu").Local = true;
45      AddVariable(new Variable("Mu", new DoubleData(0.0)));
46
47      AddVariableInfo(new VariableInfo("Sigma", "Parameter sigma of the normal distribution", typeof(DoubleData), VariableKind.None));
48      GetVariableInfo("Sigma").Local = true;
49      AddVariable(new Variable("Sigma", new DoubleData(0.0)));
50
51      AddVariableInfo(new VariableInfo("Value", "The value to manipulate (actual type is one of: IntData, DoubleData, ConstrainedIntData, ConstrainedDoubleData)", typeof(IObjectData), VariableKind.In));
52      AddVariableInfo(new VariableInfo("ShakingFactor", "Determines the force of the shaking factor (effective sigma = sigma * shakingFactor)", typeof(DoubleData), VariableKind.In));
53      AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In));
54    }
55
56    public override IOperation Apply(IScope scope) {
57      IObjectData value = GetVariableValue<IObjectData>("Value", scope, false);
58      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
59      double factor = GetVariableValue<DoubleData>("ShakingFactor", scope, true).Data;
60      double mu = GetVariableValue<DoubleData>("Mu", null, false).Data;
61      double sigma = GetVariableValue<DoubleData>("Sigma", null, false).Data;
62      NormalDistributedRandom normal = new NormalDistributedRandom(mt, mu, sigma * factor);
63
64      value.Accept(new RandomAdderVisitor(normal));
65
66      return null;
67    }
68
69
70    private class RandomAdderVisitor : ObjectDataVisitorBase {
71      private NormalDistributedRandom normal;
72      public RandomAdderVisitor(NormalDistributedRandom normal) {
73        this.normal = normal;
74      }
75
76      public override void Visit(DoubleData data) {
77        data.Data += normal.NextDouble();
78      }
79
80      public override void Visit(ConstrainedDoubleData data) {
81
82        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
83          double newValue = data.Data + normal.NextDouble();
84
85          if(IsIntegerConstrained(data)) {
86            newValue = Math.Round(newValue);
87          }
88          if(data.TrySetData(newValue)) {
89            return;
90          }
91        }
92
93        throw new InvalidProgramException("Coudn't find a valid value");
94      }
95
96      public override void Visit(IntData data) {
97        data.Data = (int)Math.Round(data.Data + normal.NextDouble());
98      }
99
100      public override void Visit(ConstrainedIntData data) {
101        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
102          if(data.TrySetData((int)Math.Round(data.Data + normal.NextDouble())))
103            return;
104        }
105
106        throw new InvalidProgramException("Couldn't find a valid value.");
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.