Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Random/UniformRandomAdder.cs @ 143

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

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 5.1 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 UniformRandomAdder : OperatorBase {
31
32    private static int MAX_NUMBER_OF_TRIES = 100;
33
34    public override string Description {
35      get { return @"Samples a uniformly distributed random variable 'U' with range = [min,max] and E(u) = (max-min)/2
36and adds the result to the variable 'Value'. ShakingFactor influences the effective range of U.
37If r=(max-min) then the effective range of U is [E(u) - shakingFactor * r/2, E(u) + shakingFactor * r/2].
38
39If a constraint for the allowed range of 'Value' is defined and the result of the operation would be smaller then
40the smallest allowed value then 'Value' is set to the lower bound and vice versa for the upper bound."; }
41    }
42
43    public UniformRandomAdder() {
44      AddVariableInfo(new VariableInfo("Value", "The value to manipulate (type is one of: IntData, ConstrainedIntData, DoubleData, ConstrainedDoubleData)", typeof(IObjectData), VariableKind.In));
45      AddVariableInfo(new VariableInfo("ShakingFactor", "Determines the force of the shaking factor.", typeof(DoubleData), VariableKind.In));
46      AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In));
47      AddVariableInfo(new VariableInfo("Min", "Lower bound of the uniform distribution.", typeof(DoubleData), VariableKind.None));
48      GetVariableInfo("Min").Local = true;
49      AddVariable(new Variable("Min", new DoubleData(-1.0)));
50
51      AddVariableInfo(new VariableInfo("Max", "Upper bound of the uniform distribution", typeof(DoubleData), VariableKind.None));
52      GetVariableInfo("Max").Local = true;
53      AddVariable(new Variable("Max", new DoubleData(1.0)));
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 min = GetVariableValue<DoubleData>("Min", null, false).Data;
61      double max = GetVariableValue<DoubleData>("Max", null, false).Data;
62
63      double ex = (max - min) / 2.0 + min;
64      double newRange = (max - min) * factor;
65      min = ex - newRange / 2;
66      max = ex + newRange / 2;
67
68      value.Accept(new RandomAdderVisitor(mt, min, max));
69
70      return null;
71    }
72
73
74    private class RandomAdderVisitor : ObjectDataVisitorBase {
75      private double min;
76      private double max;
77      private MersenneTwister mt;
78
79      public RandomAdderVisitor(MersenneTwister mt, double min, double max) {
80        this.mt = mt;
81        this.min = min;
82        this.max = max;
83      }
84
85      public override void Visit(ConstrainedDoubleData data) {
86
87        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
88
89          double newValue = data.Data + mt.NextDouble() * (max - min) + min;
90
91          if(IsIntegerConstrained(data)) {
92            newValue = Math.Round(newValue);
93          }
94
95          if(data.TrySetData(newValue)) {
96            return;
97          }
98        }
99
100        throw new InvalidProgramException("Couldn't find a valid value");
101      }
102
103      public override void Visit(ConstrainedIntData data) {
104        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
105          int newValue = (int)Math.Round(data.Data + mt.NextDouble() * (max - min) + min);
106
107          if(data.TrySetData(newValue)) {
108            return;
109          }
110        }
111        throw new InvalidProgramException("Couldn't find a valid value");
112      }
113
114      public override void Visit(DoubleData data) {
115        data.Data = data.Data + mt.NextDouble() * (max - min) + min;
116      }
117
118      public override void Visit(IntData data) {
119        data.Data = (int)Math.Round(data.Data + mt.NextDouble() * (max - min) + min);
120      }
121      private bool IsIntegerConstrained(ConstrainedDoubleData data) {
122        foreach(IConstraint constraint in data.Constraints) {
123          if(constraint is IsIntegerConstraint) {
124            return true;
125          }
126        }
127        return false;
128      }
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.