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/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  }
Note: See TracChangeset for help on using the changeset viewer.