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/UniformRandomizer.cs

    r719 r763  
    6161      double max = GetVariableValue<DoubleData>("Max", scope, true).Data;
    6262
    63       value.Accept(new RandomVisitor(mt, min, max));
    64 
     63      RandomizeUniform(value, mt, min, max);
    6564      return null;
    6665    }
    6766
    68     private class RandomVisitor : ObjectDataVisitorBase {
    69       private MersenneTwister mt;
    70       private double min;
    71       private double max;
     67    private void RandomizeUniform(IObjectData value, MersenneTwister mt, double min, double max) {
     68      // Dispatch manually based on dynamic type,
     69      // a bit awkward but necessary until we create a better type hierarchy for numeric types (gkronber 15.11.2008).
     70      if (value is DoubleData)
     71        RandomizeUniform((DoubleData)value, mt, min, max);
     72      else if (value is ConstrainedDoubleData)
     73        RandomizeUniform((ConstrainedDoubleData)value, mt, min, max);
     74      else if (value is IntData)
     75        RandomizeUniform((IntData)value, mt, min, max);
     76      else if (value is ConstrainedIntData)
     77        RandomizeUniform((ConstrainedIntData)value, mt, min, max);
     78      else throw new ArgumentException("Can't handle type " + value.GetType().Name);
     79    }
    7280
    73       public RandomVisitor(MersenneTwister mt, double min, double max) {
    74         this.mt = mt;
    75         this.min = min;
    76         this.max = max;
    77       }
    7881
    79       public override void Visit(DoubleData data) {
     82      public void RandomizeUniform(DoubleData data, MersenneTwister mt, double min, double max) {
    8083        data.Data = mt.NextDouble() * (max - min) + min;
    8184      }
    8285
    83       public override void Visit(IntData data) {
     86      public void RandomizeUniform(IntData data, MersenneTwister mt, double min, double max) {
    8487        data.Data = (int)Math.Floor(mt.NextDouble() * (max - min) + min);
    8588      }
    8689
    87       public override void Visit(ConstrainedDoubleData data) {
     90      public void RandomizeUniform(ConstrainedDoubleData data, MersenneTwister mt, double min, double max) {
    8891        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
    8992          double r = mt.NextDouble() * (max - min) + min;
     
    9598          }
    9699        }
    97         throw new InvalidProgramException("Couldn't find a valid value");
     100        throw new InvalidOperationException("Couldn't find a valid value");
    98101      }
    99102
    100       public override void Visit(ConstrainedIntData data) {
     103      public void RandomizeUniform(ConstrainedIntData data, MersenneTwister mt, double min, double max) {
    101104        for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
    102105          int r = (int)Math.Floor(mt.NextDouble() * (max - min) + min);
     
    105108          }
    106109        }
    107         throw new InvalidProgramException("Couldn't find a valid value");
     110        throw new InvalidOperationException("Couldn't find a valid value");
    108111      }
    109112
     
    117120      }
    118121    }
    119   }
    120122}
Note: See TracChangeset for help on using the changeset viewer.