Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Random/UniformRandomizer.cs @ 430

Last change on this file since 430 was 426, checked in by gkronber, 16 years ago

added properties to access parameters of randomizers (must be combined with r425) (ticket #225)

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