Free cookie consent management tool by TermsFeed Policy Generator

Changeset 763


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)

Location:
trunk/sources
Files:
2 deleted
20 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Data/BoolArrayData.cs

    r737 r763  
    9595      Data = data;
    9696    }
    97     /// <summary>
    98     /// The point of intersection where an <see cref="IObjectDataVisitor"/>
    99     /// can change the elements of the array.
    100     /// </summary>
    101     /// <param name="visitor">The visitor that changes the element.</param>
    102     public override void Accept(IObjectDataVisitor visitor) {
    103       visitor.Visit(this);
    104     }
    10597  }
    10698}
  • trunk/sources/HeuristicLab.Data/BoolData.cs

    r737 r763  
    9898      Data = bool.Parse(node.InnerText);
    9999    }
    100 
    101     /// <summary>
    102     /// The point of intersection where an <see cref="IObjectDataVisitor"/>
    103     /// can change the boolean element.
    104     /// </summary>
    105     /// <param name="visitor">The visitor that changes the element.</param>
    106     public override void Accept(IObjectDataVisitor visitor) {
    107       visitor.Visit(this);
    108     }
    109100  }
    110101}
  • trunk/sources/HeuristicLab.Data/BoolMatrixData.cs

    r737 r763  
    122122      return builder.ToString();
    123123    }
    124     /// <summary>
    125     /// The point of intersection where an <see cref="IObjectDataVisitor"/>
    126     /// can change the elements of the matrix.
    127     /// </summary>
    128     /// <param name="visitor">The visitor that changes the elements.</param>
    129     public override void Accept(IObjectDataVisitor visitor) {
    130       visitor.Visit(this);
    131     }
    132124  }
    133125}
  • trunk/sources/HeuristicLab.Data/ConstrainedDoubleData.cs

    r737 r763  
    110110      base.Data = (DoubleData)PersistenceManager.Restore(node.SelectSingleNode("Value"), restoredObjects);
    111111    }
    112 
    113     /// <summary>
    114     /// The point of intersection where an <see cref="IObjectDataVisitor"/>
    115     /// can change the double value.
    116     /// </summary>
    117     /// <param name="visitor">The visitor that changes the element.</param>
    118     public override void Accept(IObjectDataVisitor visitor) {
    119       visitor.Visit(this);
    120     }
    121112  }
    122113}
  • trunk/sources/HeuristicLab.Data/ConstrainedIntData.cs

    r737 r763  
    110110      base.Data = (IntData)PersistenceManager.Restore(node.SelectSingleNode("Value"), restoredObjects);
    111111    }
    112 
    113     /// <summary>
    114     /// The point of intersection where an <see cref="IObjectDataVisitor"/>
    115     /// can change the int element.
    116     /// </summary>
    117     /// <param name="visitor">The visitor that changes the element.</param>
    118     public override void Accept(IObjectDataVisitor visitor) {
    119       visitor.Visit(this);
    120     }
    121112  }
    122113}
  • trunk/sources/HeuristicLab.Data/ConstrainedObjectData.cs

    r737 r763  
    137137      return Data.ToString();
    138138    }
    139 
    140     /// <summary>
    141     /// The point of intersection where an <see cref="IObjectDataVisitor"/>
    142     /// can change the element.
    143     /// </summary>
    144     /// <param name="visitor">The visitor that changes the element.</param>
    145     public virtual void Accept(IObjectDataVisitor visitor) {
    146       visitor.Visit(this);
    147     }
    148139  }
    149140}
  • trunk/sources/HeuristicLab.Data/DoubleArrayData.cs

    r737 r763  
    117117      return builder.ToString();
    118118    }
    119 
    120     /// <summary>
    121     /// The point of intersection where an <see cref="IObjectDataVisitor"/>
    122     /// can change the elements of the array.
    123     /// </summary>
    124     /// <param name="visitor">The visitor that changes the elements.</param>
    125     public override void Accept(IObjectDataVisitor visitor) {
    126       visitor.Visit(this);
    127     }
    128119  }
    129120}
  • trunk/sources/HeuristicLab.Data/DoubleData.cs

    r737 r763  
    109109      }
    110110    }
    111 
    112     /// <summary>
    113     /// The point of intersection where an <see cref="IObjectDataVisitor"/>
    114     /// can change the elements of the matrix.
    115     /// </summary>
    116     /// <param name="visitor">The visitor that changes the elements.</param>
    117     public override void Accept(IObjectDataVisitor visitor) {
    118       visitor.Visit(this);
    119     }
    120111  }
    121112}
  • trunk/sources/HeuristicLab.Data/HeuristicLab.Data.csproj

    r737 r763  
    33    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    44    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    5     <ProductVersion>9.0.21022</ProductVersion>
     5    <ProductVersion>9.0.30729</ProductVersion>
    66    <SchemaVersion>2.0</SchemaVersion>
    77    <ProjectGuid>{F473D9AF-3F09-4296-9F28-3C65118DAFFA}</ProjectGuid>
     
    180180    </Compile>
    181181    <Compile Include="IObjectData.cs" />
    182     <Compile Include="IObjectDataVisitor.cs" />
    183182    <Compile Include="ItemList.cs" />
    184183    <Compile Include="ItemListView.cs">
     
    200199    </Compile>
    201200    <Compile Include="ObjectData.cs" />
    202     <Compile Include="ObjectDataVisitorBase.cs" />
    203201    <Compile Include="StringDataView.cs">
    204202      <SubType>UserControl</SubType>
  • trunk/sources/HeuristicLab.Data/IObjectData.cs

    r737 r763  
    3434    /// </summary>
    3535    object Data { get; set; }
    36 
    37     /// <summary>
    38     /// The point of intersection where an <see cref="IObjectDataVisitor"/>
    39     /// can change the object.
    40     /// </summary>
    41     /// <param name="visitor">The visitor that changes the element.</param>
    42     void Accept(IObjectDataVisitor visitor);
    4336  }
    4437}
  • trunk/sources/HeuristicLab.Data/IntData.cs

    r737 r763  
    103103      Data = int.Parse(node.InnerText, CultureInfo.InvariantCulture.NumberFormat);
    104104    }
    105 
    106     /// <summary>
    107     /// The point of intersection where an <see cref="IObjectDataVisitor"/>
    108     /// can change the int value.
    109     /// </summary>
    110     /// <param name="visitor">The visitor that changes the element.</param>
    111     public override void Accept(IObjectDataVisitor visitor) {
    112       visitor.Visit(this);
    113     }
    114105  }
    115106}
  • trunk/sources/HeuristicLab.Data/IntMatrixData.cs

    r737 r763  
    139139      return builder.ToString();
    140140    }
    141 
    142     /// <summary>
    143     /// The point of intersection where an <see cref="IObjectDataVisitor"/>
    144     /// can change the elements of the matrix.
    145     /// </summary>
    146     /// <param name="visitor">The visitor that changes the elements.</param>
    147     public override void Accept(IObjectDataVisitor visitor) {
    148       visitor.Visit(this);
    149     }
    150141  }
    151142}
  • trunk/sources/HeuristicLab.Data/NullData.cs

    r737 r763  
    5858      return clone;
    5959    }
    60 
    61     /// <summary>
    62     /// The point of intersection where an <see cref="IObjectDataVisitor"/>
    63     /// can change the value.
    64     /// </summary>
    65     /// <param name="visitor">The visitor that changes the element.</param>
    66     public override void Accept(IObjectDataVisitor visitor) {
    67       visitor.Visit(this);
    68     }
    6960  }
    7061}
  • trunk/sources/HeuristicLab.Data/ObjectData.cs

    r737 r763  
    120120        return Data.ToString();
    121121    }
    122 
    123     /// <summary>
    124     /// The point of intersection where an <see cref="IObjectDataVisitor"/>
    125     /// can change the object.
    126     /// </summary>
    127     /// <param name="visitor">The visitor that changes the element.</param>
    128     public virtual void Accept(IObjectDataVisitor visitor) {
    129       visitor.Visit(this);
    130     }
    131122  }
    132123}
  • trunk/sources/HeuristicLab.Data/StringData.cs

    r737 r763  
    109109      return Data;
    110110    }
    111 
    112     /// <summary>
    113     /// The point of intersection where an <see cref="IObjectDataVisitor"/>
    114     /// can change the string.
    115     /// </summary>
    116     /// <param name="visitor">The visitor that changes the element.</param>
    117     public override void Accept(IObjectDataVisitor visitor) {
    118       visitor.Visit(this);
    119     }
    120111  }
    121112}
  • trunk/sources/HeuristicLab.GP.Boolean/FunctionLibraryInjector.cs

    r722 r763  
    7373      And and = new And();
    7474      Or or = new Or();
    75       Not not = new Not();
     75      //Not not = new Not();
    7676      Nand nand = new Nand();
    7777      Nor nor = new Nor();
    78       Xor xor = new Xor();
     78      //Xor xor = new Xor();
    7979      variable = new HeuristicLab.GP.Boolean.Variable();
    8080
     
    8282        and,
    8383        or,
    84         not,
     84        //not,
    8585        nand,
    8686        nor,
    87         xor,
     87        //xor,
    8888        variable
    8989      };
     
    9191      SetAllowedSubOperators(and, allFunctions);
    9292      SetAllowedSubOperators(or, allFunctions);
    93       SetAllowedSubOperators(not, allFunctions);
     93      //SetAllowedSubOperators(not, allFunctions);
    9494      SetAllowedSubOperators(nand, allFunctions);
    9595      SetAllowedSubOperators(nor, allFunctions);
    96       SetAllowedSubOperators(xor, allFunctions);
     96      //SetAllowedSubOperators(xor, allFunctions);
    9797
    9898      operatorLibrary = new GPOperatorLibrary();
    9999      operatorLibrary.GPOperatorGroup.AddOperator(and);
    100100      operatorLibrary.GPOperatorGroup.AddOperator(or);
    101       operatorLibrary.GPOperatorGroup.AddOperator(not);
     101      //operatorLibrary.GPOperatorGroup.AddOperator(not);
    102102      operatorLibrary.GPOperatorGroup.AddOperator(nand);
    103103      operatorLibrary.GPOperatorGroup.AddOperator(nor);
    104       operatorLibrary.GPOperatorGroup.AddOperator(xor);
     104      //operatorLibrary.GPOperatorGroup.AddOperator(xor);
    105105      operatorLibrary.GPOperatorGroup.AddOperator(variable);
    106106    }
  • 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.