Free cookie consent management tool by TermsFeed Policy Generator

Last change on this file since 719 was 719, checked in by abeham, 16 years ago

[TICKET #341] Changed HeuristicLab.Random operators to also take min/max, mu/sigma from scope

File size: 5.1 KB
Line
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
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
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 (inclusive)", typeof(DoubleData), VariableKind.None));
48      GetVariableInfo("Min").Local = true;
50
51      AddVariableInfo(new VariableInfo("Max", "Upper bound of the uniform distribution (exclusive)", typeof(DoubleData), VariableKind.None));
52      GetVariableInfo("Max").Local = true;
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", scope, true).Data;
61      double max = GetVariableValue<DoubleData>("Max", scope, true).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
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        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
87          double newValue = data.Data + mt.NextDouble() * (max - min) + min;
88          if(IsIntegerConstrained(data)) {
89            newValue = Math.Floor(newValue);
90          }
91          if(data.TrySetData(newValue)) {
92            return;
93          }
94        }
95        throw new InvalidProgramException("Couldn't find a valid value");
96      }
97
98      public override void Visit(ConstrainedIntData data) {
99        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
100          int newValue = (int)Math.Floor(data.Data + mt.NextDouble() * (max - min) + min);
101          if(data.TrySetData(newValue)) {
102            return;
103          }
104        }
105        throw new InvalidProgramException("Couldn't find a valid value");
106      }
107
108      public override void Visit(DoubleData data) {
109        data.Data = data.Data + mt.NextDouble() * (max - min) + min;
110      }
111
112      public override void Visit(IntData data) {
113        data.Data = (int)Math.Floor(data.Data + mt.NextDouble() * (max - min) + min);
114      }
115      private bool IsIntegerConstrained(ConstrainedDoubleData data) {
116        foreach(IConstraint constraint in data.Constraints) {
117          if(constraint is IsIntegerConstraint) {
118            return true;
119          }
120        }
121        return false;
122      }
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.