Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Random/UniformRandomAdder.cs @ 1058

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