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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Constraints;
|
---|
28 |
|
---|
29 | namespace 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 (inclusive)", 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 (exclusive)", 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.Floor(mt.NextDouble() * (max - min) + min);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public override void Visit(ConstrainedDoubleData data) {
|
---|
88 | for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
|
---|
89 | double r = mt.NextDouble() * (max - min) + min;
|
---|
90 | if(IsIntegerConstrained(data)) {
|
---|
91 | r = Math.Floor(r);
|
---|
92 | }
|
---|
93 | if(data.TrySetData(r)) {
|
---|
94 | return;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | throw new InvalidProgramException("Couldn't find a valid value");
|
---|
98 | }
|
---|
99 |
|
---|
100 | public override void Visit(ConstrainedIntData data) {
|
---|
101 | for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
|
---|
102 | int r = (int)Math.Floor(mt.NextDouble() * (max - min) + min);
|
---|
103 | if(data.TrySetData(r)) {
|
---|
104 | return;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | throw new InvalidProgramException("Couldn't find a valid value");
|
---|
108 | }
|
---|
109 |
|
---|
110 | private bool IsIntegerConstrained(ConstrainedDoubleData data) {
|
---|
111 | foreach(IConstraint constraint in data.Constraints) {
|
---|
112 | if(constraint is IsIntegerConstraint) {
|
---|
113 | return true;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | return false;
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|