Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.Random/UniformRandomizer.cs @ 12448

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

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 4.2 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 UniformRandomizer : OperatorBase {
31    private static int MAX_NUMBER_OF_TRIES = 100;
32    public override string Description {
33      get { return "Initializes the value of variable 'Value' to a random value uniformly distributed between 'Min' and 'Max' (exclusive)"; }
34    }
35
36    public UniformRandomizer() {
37      AddVariableInfo(new VariableInfo("Value", "The value to manipulate (type is one of: IntData, ConstrainedIntData, DoubleData, ConstrainedDoubleData)", typeof(IObjectData), VariableKind.In));
38      AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In));
39      AddVariableInfo(new VariableInfo("Min", "Lower bound of the uniform distribution", typeof(DoubleData), VariableKind.None));
40      GetVariableInfo("Min").Local = true;
41      AddVariable(new Variable("Min", new DoubleData(0.0)));
42
43      AddVariableInfo(new VariableInfo("Max", "Upper bound of the uniform distribution", typeof(DoubleData), VariableKind.None));
44      GetVariableInfo("Max").Local = true;
45      AddVariable(new Variable("Max", new DoubleData(1.0)));
46    }
47
48    public override IOperation Apply(IScope scope) {
49      IObjectData value = GetVariableValue<IObjectData>("Value", scope, false);
50      MersenneTwister mt = GetVariableValue<MersenneTwister>("Random", scope, true);
51      double min = GetVariableValue<DoubleData>("Min", null, false).Data;
52      double max = GetVariableValue<DoubleData>("Max", null, false).Data;
53
54      value.Accept(new RandomVisitor(mt, min, max));
55
56      return null;
57    }
58
59    private class RandomVisitor : ObjectDataVisitorBase {
60      private MersenneTwister mt;
61      private double min;
62      private double max;
63
64      public RandomVisitor(MersenneTwister mt, double min, double max) {
65        this.mt = mt;
66        this.min = min;
67        this.max = max;
68      }
69
70      public override void Visit(DoubleData data) {
71        data.Data = mt.NextDouble() * (max - min) + min;
72      }
73
74      public override void Visit(IntData data) {
75        data.Data = (int)Math.Round(mt.NextDouble() * (max - min) + min);
76      }
77
78      public override void Visit(ConstrainedDoubleData data) {
79
80        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
81          double r = mt.NextDouble() * (max - min) + min;
82
83          if(IsIntegerConstrained(data)) {
84            r = Math.Round(r);
85          }
86
87          if(data.TrySetData(r)) {
88            return;
89          }
90        }
91
92        throw new InvalidProgramException("Couldn't find a valid value");
93      }
94
95      public override void Visit(ConstrainedIntData data) {
96
97        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
98          int r = (int)Math.Round(mt.NextDouble() * (max - min) + min);
99
100          if(data.TrySetData(r)) {
101            return;
102          }
103        }
104        throw new InvalidProgramException("Couldn't find a valid value");
105      }
106
107      private bool IsIntegerConstrained(ConstrainedDoubleData data) {
108        foreach(IConstraint constraint in data.Constraints) {
109          if(constraint is IsIntegerConstraint) {
110            return true;
111          }
112        }
113        return false;
114      }
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.