Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/15/08 16:47:45 (15 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)

Location:
trunk/sources/HeuristicLab.Random
Files:
4 edited

Legend:

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

    r719 r763  
    7171      NormalDistributedRandom normal = new NormalDistributedRandom(mt, mu, sigma * factor);
    7272
    73       value.Accept(new RandomAdderVisitor(normal));
    74 
     73      AddNormal(value, normal);
    7574      return null;
    7675    }
    7776
     77    private void AddNormal(IObjectData value, NormalDistributedRandom normal) {
     78      // dispatch manually based on dynamic type
     79      if (value is IntData)
     80        AddNormal((IntData)value, normal);
     81      else if (value is ConstrainedIntData)
     82        AddNormal((ConstrainedIntData)value, normal);
     83      else if (value is ConstrainedDoubleData)
     84        AddNormal((ConstrainedDoubleData)value, normal);
     85      else if (value is DoubleData)
     86        AddNormal((DoubleData)value, normal);
     87      else throw new InvalidOperationException("Can't handle type " + value.GetType().Name);
     88    }
     89    public void AddNormal(DoubleData data, NormalDistributedRandom normal) {
     90      data.Data += normal.NextDouble();
     91    }
    7892
    79     private class RandomAdderVisitor : ObjectDataVisitorBase {
    80       private NormalDistributedRandom normal;
    81       public RandomAdderVisitor(NormalDistributedRandom normal) {
    82         this.normal = normal;
     93    public void AddNormal(ConstrainedDoubleData data, NormalDistributedRandom normal) {
     94      for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
     95        double newValue = data.Data + normal.NextDouble();
     96        if (IsIntegerConstrained(data)) {
     97          newValue = Math.Round(newValue);
     98        }
     99        if (data.TrySetData(newValue)) {
     100          return;
     101        }
    83102      }
     103      throw new InvalidProgramException("Coudn't find a valid value");
     104    }
    84105
    85       public override void Visit(DoubleData data) {
    86         data.Data += normal.NextDouble();
     106    public void AddNormal(IntData data, NormalDistributedRandom normal) {
     107      data.Data = (int)Math.Round(data.Data + normal.NextDouble());
     108    }
     109
     110    public void AddNormal(ConstrainedIntData data, NormalDistributedRandom normal) {
     111      for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
     112        if (data.TrySetData((int)Math.Round(data.Data + normal.NextDouble())))
     113          return;
    87114      }
     115      throw new InvalidProgramException("Couldn't find a valid value.");
     116    }
    88117
    89       public override void Visit(ConstrainedDoubleData data) {
    90         for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
    91           double newValue = data.Data + normal.NextDouble();
    92           if(IsIntegerConstrained(data)) {
    93             newValue = Math.Round(newValue);
    94           }
    95           if(data.TrySetData(newValue)) {
    96             return;
    97           }
     118    private bool IsIntegerConstrained(ConstrainedDoubleData data) {
     119      foreach (IConstraint constraint in data.Constraints) {
     120        if (constraint is IsIntegerConstraint) {
     121          return true;
    98122        }
    99         throw new InvalidProgramException("Coudn't find a valid value");
    100123      }
    101 
    102       public override void Visit(IntData data) {
    103         data.Data = (int)Math.Round(data.Data + normal.NextDouble());
    104       }
    105 
    106       public override void Visit(ConstrainedIntData data) {
    107         for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
    108           if(data.TrySetData((int)Math.Round(data.Data + normal.NextDouble())))
    109             return;
    110         }
    111         throw new InvalidProgramException("Couldn't find a valid value.");
    112       }
    113 
    114       private bool IsIntegerConstrained(ConstrainedDoubleData data) {
    115         foreach(IConstraint constraint in data.Constraints) {
    116           if(constraint is IsIntegerConstraint) {
    117             return true;
    118           }
    119         }
    120         return false;
    121       }
     124      return false;
    122125    }
    123126  }
  • trunk/sources/HeuristicLab.Random/NormalRandomizer.cs

    r719 r763  
    6262      double mu = GetVariableValue<DoubleData>("Mu", scope, true).Data;
    6363      double sigma = GetVariableValue<DoubleData>("Sigma", scope, true).Data;
    64       NormalDistributedRandom normal = new NormalDistributedRandom(mt, mu, sigma);
    6564
    66       value.Accept(new RandomizerVisitor(normal));
    67 
     65      NormalDistributedRandom n = new NormalDistributedRandom(mt, mu, sigma);
     66      RandomizeNormal(value, n);
    6867      return null;
    6968    }
    7069
    71     private class RandomizerVisitor : ObjectDataVisitorBase {
    72       private NormalDistributedRandom normal;
     70    private void RandomizeNormal(IObjectData value, NormalDistributedRandom n) {
     71      // dispatch manually based on dynamic type
     72      if (value is IntData)
     73        RandomizeNormal((IntData)value, n);
     74      else if (value is ConstrainedIntData)
     75        RandomizeNormal((ConstrainedIntData)value, n);
     76      else if (value is DoubleData)
     77        RandomizeNormal((DoubleData)value, n);
     78      else if (value is ConstrainedDoubleData)
     79        RandomizeNormal((ConstrainedDoubleData)value, n);
     80      else throw new InvalidOperationException("Can't handle type " + value.GetType().Name);
     81    }
    7382
    74       public RandomizerVisitor(NormalDistributedRandom normal) {
    75         this.normal = normal;
     83    public void RandomizeNormal(ConstrainedDoubleData data, NormalDistributedRandom normal) {
     84      for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
     85        double r = normal.NextDouble();
     86        if (IsIntegerConstrained(data)) {
     87          r = Math.Round(r);
     88        }
     89        if (data.TrySetData(r)) {
     90          return;
     91        }
    7692      }
     93      throw new InvalidOperationException("Couldn't find a valid value in 100 tries with mu=" + normal.Mu + " sigma=" + normal.Sigma);
     94    }
    7795
    78       public override void Visit(ConstrainedDoubleData data) {
    79         for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
    80           double r = normal.NextDouble();
    81           if(IsIntegerConstrained(data)) {
    82             r = Math.Round(r);
    83           }
    84           if(data.TrySetData(r)) {
    85             return;
    86           }
    87         }
    88         throw new InvalidProgramException("Couldn't find a valid value in 100 tries with mu=" + normal.Mu + " sigma=" + normal.Sigma);
     96    public void RandomizeNormal(ConstrainedIntData data, NormalDistributedRandom normal) {
     97      for (int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
     98        double r = normal.NextDouble();
     99        if (data.TrySetData((int)Math.Round(r))) // since r is a continuous normally distributed random variable rounding should be OK
     100          return;
    89101      }
     102      throw new InvalidOperationException("Couldn't find a valid value");
     103    }
    90104
    91       public override void Visit(ConstrainedIntData data) {
    92         for(int tries = MAX_NUMBER_OF_TRIES; tries >= 0; tries--) {
    93           double r = normal.NextDouble();
    94           if(data.TrySetData((int)Math.Round(r))) // since r is a continuous normally distributed random variable rounding should be OK
    95             return;
    96         }
    97         throw new InvalidProgramException("Couldn't find a valid value");
    98       }
     105    public void RandomizeNormal(DoubleData data, NormalDistributedRandom normal) {
     106      data.Data = normal.NextDouble();
     107    }
    99108
    100       public override void Visit(DoubleData data) {
    101         data.Data = normal.NextDouble();
    102       }
    103 
    104       public override void Visit(IntData data) {
    105         data.Data = (int)Math.Round(normal.NextDouble());
    106       }
     109    public void RandomizeNormal(IntData data, NormalDistributedRandom normal) {
     110      data.Data = (int)Math.Round(normal.NextDouble());
     111    }
    107112
    108113
    109       private bool IsIntegerConstrained(ConstrainedDoubleData data) {
    110         foreach(IConstraint constraint in data.Constraints) {
    111           if(constraint is IsIntegerConstraint) {
    112             return true;
    113           }
     114    private bool IsIntegerConstrained(ConstrainedDoubleData data) {
     115      foreach (IConstraint constraint in data.Constraints) {
     116        if (constraint is IsIntegerConstraint) {
     117          return true;
    114118        }
    115         return false;
    116119      }
     120      return false;
    117121    }
    118122  }
  • 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  }
  • 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.