Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/15/08 16:47:45 (16 years ago)
Author:
gkronber
Message:

removed visitor interfaces and methods in HeuristicLab.Data and fixed classes in HeuristicLab.Random to work without visitor methods. #343 (Rethink about usefulness of visitors for ObjectData and Constraints)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Random/UniformRandomAdder.cs

    r719 r763  
    3333
    3434    public override string Description {
    35       get { return @"Samples a uniformly distributed random variable 'U' with range = [min,max] and E(u) = (max-min)/2
     35      get {
     36        return @"Samples a uniformly distributed random variable 'U' with range = [min,max] and E(u) = (max-min)/2
    3637and adds the result to the variable 'Value'. ShakingFactor influences the effective range of U.
    3738If r=(max-min) then the effective range of U is [E(u) - shakingFactor * r/2, E(u) + shakingFactor * r/2].
    3839
    3940If a constraint for the allowed range of 'Value' is defined and the result of the operation would be smaller then
    40 the smallest allowed value then 'Value' is set to the lower bound and vice versa for the upper bound."; }
     41the smallest allowed value then 'Value' is set to the lower bound and vice versa for the upper bound.";
     42      }
    4143    }
    4244
     
    6668      max = ex + newRange / 2;
    6769
    68       value.Accept(new RandomAdderVisitor(mt, min, max));
    69 
     70      AddUniform(value, mt, min, max);
    7071      return null;
    7172    }
    7273
     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    }
    7386
    74     private class RandomAdderVisitor : ObjectDataVisitorBase {
    75       private double min;
    76       private double max;
    77       private MersenneTwister mt;
    7887
    79       public RandomAdderVisitor(MersenneTwister mt, double min, double max) {
    80         this.mt = mt;
    81         this.min = min;
    82         this.max = max;
     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);
     93        }
     94        if (data.TrySetData(newValue)) {
     95          return;
     96        }
    8397      }
     98      throw new InvalidProgramException("Couldn't find a valid value");
     99    }
    84100
    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           }
     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;
    94106        }
    95         throw new InvalidProgramException("Couldn't find a valid value");
    96107      }
     108      throw new InvalidProgramException("Couldn't find a valid value");
     109    }
    97110
    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           }
     111    public void AddUniform(DoubleData data, MersenneTwister mt, double min, double max) {
     112      data.Data = data.Data + mt.NextDouble() * (max - min) + min;
     113    }
     114
     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;
    104122        }
    105         throw new InvalidProgramException("Couldn't find a valid value");
    106123      }
    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       }
     124      return false;
    123125    }
    124126  }
Note: See TracChangeset for help on using the changeset viewer.