Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/01/13 13:55:09 (12 years ago)
Author:
abeham
Message:

#2020: review comments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Creators/NormalDistributedRealVectorCreator.cs

    r9258 r9265  
    4545    }
    4646
     47    public IValueParameter<IntValue> MaximumTriesParameter {
     48      get { return (IValueParameter<IntValue>)Parameters["MaximumTries"]; }
     49    }
     50
    4751    [StorableConstructor]
    4852    protected NormalDistributedRealVectorCreator(bool deserializing) : base(deserializing) { }
     
    5256      Parameters.Add(new ValueLookupParameter<RealVector>("Mean", "The mean vector around which the points will be sampled."));
    5357      Parameters.Add(new ValueLookupParameter<DoubleArray>("Sigma", "The standard deviations for all or for each dimension."));
     58      Parameters.Add(new ValueParameter<IntValue>("MaximumTries", "The maximum number of tries to sample within the specified bounds.", new IntValue(1000)));
    5459    }
    5560
    5661    public override IDeepCloneable Clone(Cloner cloner) {
    5762      return new NormalDistributedRealVectorCreator(this, cloner);
     63    }
     64
     65    [StorableHook(HookType.AfterDeserialization)]
     66    private void AfterDeserialization() {
     67      if (!Parameters.ContainsKey("MaximumTries"))
     68        Parameters.Add(new ValueParameter<IntValue>("MaximumTries", "The maximum number of tries to sample within the specified bounds.", new IntValue(1000)));
    5869    }
    5970
     
    8091    /// <param name="sigma">The vector of standard deviations, must have at least one row.</param>
    8192    /// <param name="bounds">The lower and upper bound (1st and 2nd column) of the positions in the vector. If there are less rows than dimensions, the rows are cycled.</param>
     93    /// <param name="maximumTries">The maximum number of tries to sample a value inside the bounds for each dimension. If a valid value cannot be obtained, the mean will be used.</param>
    8294    /// <returns>The newly created real vector.</returns>
    83     public static RealVector Apply(IRandom random, RealVector mean, DoubleArray sigma, DoubleMatrix bounds) {
    84       if (random == null) throw new ArgumentException("Random is not defined", "random");
    85       if (mean == null || mean.Length == 0) throw new ArgumentException("Mean is not defined", "mean");
    86       if (sigma == null || sigma.Length == 0) throw new ArgumentException("Sigma is not defined.", "sigma");
     95    public static RealVector Apply(IRandom random, RealVector mean, DoubleArray sigma, DoubleMatrix bounds, int maximumTries = 1000) {
     96      if (random == null) throw new ArgumentNullException("Random is not defined", "random");
     97      if (mean == null || mean.Length == 0) throw new ArgumentNullException("Mean is not defined", "mean");
     98      if (sigma == null || sigma.Length == 0) throw new ArgumentNullException("Sigma is not defined.", "sigma");
    8799      if (bounds == null || bounds.Rows == 0) bounds = new DoubleMatrix(new[,] { { double.MinValue, double.MaxValue } });
    88100      var nd = new NormalDistributedRandom(random, 0, 1);
     
    94106        else if (mean[i] > max) result[i] = max;
    95107        else {
     108          int count = 0;
     109          bool inRange;
    96110          do {
    97111            result[i] = mean[i] + sigma[i % sigma.Length] * nd.NextDouble();
    98           } while (result[i] < bounds[i % sigma.Length, 0] || result[i] > bounds[i % sigma.Length, 1]);
     112            inRange = result[i] >= bounds[i % sigma.Length, 0] && result[i] < bounds[i % sigma.Length, 1];
     113            count++;
     114          } while (count < maximumTries && !inRange);
     115          if (count == maximumTries && !inRange)
     116            result[i] = mean[i];
    99117        }
    100118      }
     
    110128    /// <returns>The newly created real vector.</returns>
    111129    protected override RealVector Create(IRandom random, IntValue length, DoubleMatrix bounds) {
    112       return Apply(random, MeanParameter.ActualValue, SigmaParameter.ActualValue, bounds);
     130      return Apply(random, MeanParameter.ActualValue, SigmaParameter.ActualValue, bounds, MaximumTriesParameter.Value.Value);
    113131    }
    114132  }
Note: See TracChangeset for help on using the changeset viewer.