[2] | 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 UniformRandomAdder : OperatorBase {
|
---|
| 31 |
|
---|
| 32 | private static int MAX_NUMBER_OF_TRIES = 100;
|
---|
| 33 |
|
---|
| 34 | public override string Description {
|
---|
[763] | 35 | get {
|
---|
| 36 | return @"Samples a uniformly distributed random variable 'U' with range = [min,max] and E(u) = (max-min)/2
|
---|
[2] | 37 | and adds the result to the variable 'Value'. ShakingFactor influences the effective range of U.
|
---|
| 38 | If r=(max-min) then the effective range of U is [E(u) - shakingFactor * r/2, E(u) + shakingFactor * r/2].
|
---|
| 39 |
|
---|
| 40 | If a constraint for the allowed range of 'Value' is defined and the result of the operation would be smaller then
|
---|
[763] | 41 | the smallest allowed value then 'Value' is set to the lower bound and vice versa for the upper bound.";
|
---|
| 42 | }
|
---|
[2] | 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));
|
---|
[469] | 47 | AddVariableInfo(new VariableInfo("ShakingFactor", "Determines the force of the shaking factor", typeof(DoubleData), VariableKind.In));
|
---|
[2] | 48 | AddVariableInfo(new VariableInfo("Random", "The random generator to use", typeof(MersenneTwister), VariableKind.In));
|
---|
[469] | 49 | AddVariableInfo(new VariableInfo("Min", "Lower bound of the uniform distribution (inclusive)", typeof(DoubleData), VariableKind.None));
|
---|
[2] | 50 | GetVariableInfo("Min").Local = true;
|
---|
| 51 | AddVariable(new Variable("Min", new DoubleData(-1.0)));
|
---|
| 52 |
|
---|
[469] | 53 | AddVariableInfo(new VariableInfo("Max", "Upper bound of the uniform distribution (exclusive)", typeof(DoubleData), VariableKind.None));
|
---|
[2] | 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;
|
---|
[719] | 62 | double min = GetVariableValue<DoubleData>("Min", scope, true).Data;
|
---|
| 63 | double max = GetVariableValue<DoubleData>("Max", scope, true).Data;
|
---|
[2] | 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 |
|
---|
[763] | 70 | AddUniform(value, mt, min, max);
|
---|
[2] | 71 | return null;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[763] | 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 | }
|
---|
[2] | 86 |
|
---|
| 87 |
|
---|
[763] | 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);
|
---|
[2] | 93 | }
|
---|
[763] | 94 | if (data.TrySetData(newValue)) {
|
---|
| 95 | return;
|
---|
| 96 | }
|
---|
[2] | 97 | }
|
---|
[763] | 98 | throw new InvalidProgramException("Couldn't find a valid value");
|
---|
| 99 | }
|
---|
[2] | 100 |
|
---|
[763] | 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;
|
---|
[2] | 106 | }
|
---|
| 107 | }
|
---|
[763] | 108 | throw new InvalidProgramException("Couldn't find a valid value");
|
---|
| 109 | }
|
---|
[2] | 110 |
|
---|
[763] | 111 | public void AddUniform(DoubleData data, MersenneTwister mt, double min, double max) {
|
---|
| 112 | data.Data = data.Data + mt.NextDouble() * (max - min) + min;
|
---|
| 113 | }
|
---|
[2] | 114 |
|
---|
[763] | 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;
|
---|
[2] | 122 | }
|
---|
| 123 | }
|
---|
[763] | 124 | return false;
|
---|
[2] | 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|