/// <summary>
 /// Mutates the endogenous strategy parameters.
 /// </summary>
 /// <param name="random">The random number generator to use.</param>
 /// <param name="vector">The strategy vector to manipulate.</param>
 /// <param name="generalLearningRate">The general learning rate dampens the mutation over all dimensions.</param>
 /// <param name="learningRate">The learning rate dampens the mutation in each dimension.</param>
 public static void Apply(IRandom random, RealVector vector, double generalLearningRate, double learningRate) {
   NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0);
   double generalMultiplier = Math.Exp(generalLearningRate * N.NextDouble());
   for (int i = 0; i < vector.Length; i++) {
     vector[i] *= generalMultiplier * Math.Exp(learningRate * N.NextDouble());
   }
 }
 /// <summary>
 /// Mutates the endogenous strategy parameters.
 /// </summary>
 /// <param name="random">The random number generator to use.</param>
 /// <param name="vector">The strategy vector to manipulate.</param>
 /// <param name="generalLearningRate">The general learning rate dampens the mutation over all dimensions.</param>
 /// <param name="learningRate">The learning rate dampens the mutation in each dimension.</param>
 /// <param name="bounds">The minimal and maximal value for each component, bounds are cycled if the length of bounds is smaller than the length of vector</param>
 public static void Apply(IRandom random, DoubleArray vector, double generalLearningRate, double learningRate, DoubleMatrix bounds) {
   NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0);
   double generalMultiplier = Math.Exp(generalLearningRate * N.NextDouble());
   for (int i = 0; i < vector.Length; i++) {
     double change = vector[i] * generalMultiplier * Math.Exp(learningRate * N.NextDouble());
     if (bounds != null) {
       double min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1];
       if (min == max) vector[i] = min;
       else {
         if (change < min || change > max) change = Math.Max(min, Math.Min(max, change));
         vector[i] = change;
       }
     }
   }
 }
 /// <summary>
 /// Performs an adaptive normally distributed all position manipulation on the given 
 /// <paramref name="vector"/>.
 /// </summary>
 /// <exception cref="InvalidOperationException">Thrown when the strategy vector is not
 /// as long as the vector to get manipulated.</exception>
 /// <param name="sigma">The strategy vector determining the strength of the mutation.</param>
 /// <param name="random">A random number generator.</param>
 /// <param name="vector">The real vector to manipulate.</param>
 /// <returns>The manipulated real vector.</returns>
 public static void Apply(IRandom random, RealVector vector, RealVector sigma) {
   if (sigma == null || sigma.Length == 0) throw new ArgumentException("ERROR: Vector containing the standard deviations is not defined.", "sigma");
   NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0);
   for (int i = 0; i < vector.Length; i++) {
     vector[i] = vector[i] + (N.NextDouble() * sigma[i % sigma.Length]);
   }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Generates a new normally distributed random variable and assigns it to the specified variable.
        /// </summary>
        public override IOperation Apply()
        {
            IRandom random = RandomParameter.ActualValue;
            double  mu     = MuParameter.ActualValue.Value;
            double  sigma  = SigmaParameter.ActualValue.Value;

            NormalDistributedRandom normalRandom = new NormalDistributedRandom(random, mu, sigma);

            ValueParameter.ActualValue = new DoubleValue(normalRandom.NextDouble());
            return(base.Apply());
        }
 public static AdditiveMove[] Apply(IRandom random, RealVector vector, double sigma, int sampleSize, DoubleMatrix bounds) {
   AdditiveMove[] moves = new AdditiveMove[sampleSize];
   NormalDistributedRandom N = new NormalDistributedRandom(random, 0, sigma);
   for (int i = 0; i < sampleSize; i++) {
     int index = random.Next(vector.Length);
     double strength = 0, min = bounds[index % bounds.Rows, 0], max = bounds[index % bounds.Rows, 1];
     do {
       strength = N.NextDouble();
     } while (vector[index] + strength < min || vector[index] + strength > max);
     moves[i] = new AdditiveMove(index, strength);
   }
   return moves;
 }
    /// <summary>
    /// Performs a normally distributed all position manipulation on the given 
    /// <paramref name="vector"/> and rounds the result to the next feasible value.
    /// </summary>
    /// <exception cref="InvalidOperationException">Thrown when the sigma vector is null or of length 0.</exception>
    /// <param name="sigma">The sigma vector determining the strength of the mutation.</param>
    /// <param name="random">A random number generator.</param>
    /// <param name="vector">The integer vector to manipulate.</param>#
    /// <param name="bounds">The bounds and step size for each dimension (will be cycled in case there are less rows than elements in the parent vectors).</param>
    /// <returns>The manipulated integer vector.</returns>
    public static void Apply(IRandom random, IntegerVector vector, IntMatrix bounds, DoubleArray sigma) {
      if (sigma == null || sigma.Length == 0) throw new ArgumentException("RoundedNormalAllPositionsManipulator: Vector containing the standard deviations is not defined.", "sigma");
      if (bounds == null || bounds.Rows == 0 || bounds.Columns < 2) throw new ArgumentException("RoundedNormalAllPositionsManipulator: Invalid bounds specified.", "bounds");
      var N = new NormalDistributedRandom(random, 0.0, 1.0);
      for (int i = 0; i < vector.Length; i++) {
        int min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1], step = 1;
        if (bounds.Columns > 2) step = bounds[i % bounds.Rows, 2];

        int value = (vector[i] + (int)Math.Round((N.NextDouble() * sigma[i % sigma.Length])) - min) / step;
        max = FloorFeasible(min, max, step, max - 1);
        vector[i] = RoundFeasible(min, max, step, value);
      }
    }
Ejemplo n.º 7
0
    protected override List<List<double>> GenerateValues() {
      List<List<double>> data = new List<List<double>>();

      var nrand = new NormalDistributedRandom(random, 0, 1);
      for (int c = 0; c < numberOfFeatures; c++) {
        var datai = Enumerable.Range(0, TestPartitionEnd).Select(_ => nrand.NextDouble()).ToList();
        data.Add(datai);
      }
      var y = GenerateRandomFunction(random, data);

      var targetSigma = y.StandardDeviation();
      var noisePrng = new NormalDistributedRandom(random, 0, targetSigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio)));

      data.Add(y.Select(t => t + noisePrng.NextDouble()).ToList());

      return data;
    }
 /// <summary>
 /// Generates a new random real vector normally distributed around the given mean with the given <paramref name="length"/> and in the interval [min,max).
 /// </summary>
 /// <exception cref="ArgumentException">
 /// Thrown when <paramref name="random"/> is null.<br />
 /// Thrown when <paramref name="mean"/> is null or of length 0.<br />
 /// Thrown when <paramref name="sigma"/> is null or of length 0.<br />
 /// </exception>
 /// <remarks>
 /// If no bounds are given the bounds will be set to (double.MinValue;double.MaxValue).
 /// 
 /// If dimensions of the mean do not lie within the given bounds they're set to either to the min or max of the bounds depending on whether the given dimension
 /// for the mean is smaller or larger than the bounds. If min and max for a certain dimension are almost the same the resulting value will be set to min.
 /// 
 /// However, please consider that such static bounds are not really meaningful to optimize.
 /// 
 /// The sigma vector can contain 0 values in which case the dimension will be exactly the same as the given mean.
 /// </remarks>
 /// <param name="random">The random number generator.</param>
 /// <param name="means">The mean vector around which the resulting vector is sampled.</param>
 /// <param name="sigmas">The vector of standard deviations, must have at least one row.</param>
 /// <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>
 /// <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>
 /// <returns>The newly created real vector.</returns>
 public static RealVector Apply(IntValue lengthValue, IRandom random, RealVector means, DoubleArray sigmas, DoubleMatrix bounds, int maximumTries = 1000) {
   if (lengthValue == null || lengthValue.Value == 0) throw new ArgumentException("Length is not defined or zero");
   if (random == null) throw new ArgumentNullException("Random is not defined", "random");
   if (means == null || means.Length == 0) throw new ArgumentNullException("Mean is not defined", "mean");
   if (sigmas == null || sigmas.Length == 0) throw new ArgumentNullException("Sigma is not defined.", "sigma");
   if (bounds == null || bounds.Rows == 0) bounds = new DoubleMatrix(new[,] { { double.MinValue, double.MaxValue } });
   var length = lengthValue.Value;
   var nd = new NormalDistributedRandom(random, 0, 1);
   var result = new RealVector(length);
   for (int i = 0; i < result.Length; i++) {
     var min = bounds[i % bounds.Rows, 0];
     var max = bounds[i % bounds.Rows, 1];
     var mean = means[i % means.Length];
     var sigma = sigmas[i % sigmas.Length];
     if (min.IsAlmost(max) || mean < min) result[i] = min;
     else if (mean > max) result[i] = max;
     else {
       int count = 0;
       bool inRange;
       do {
         result[i] = mean + sigma * nd.NextDouble();
         inRange = result[i] >= min && result[i] < max;
         count++;
       } while (count < maximumTries && !inRange);
       if (count == maximumTries && !inRange)
         result[i] = mean;
     }
   }
   return result;
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Generates a new double random number.
 /// </summary>
 /// <returns>A double random number.</returns>
 public double NextDouble()
 {
     return(NormalDistributedRandom.NextDouble(uniform, mu, sigma));
 }
Ejemplo n.º 10
0
    protected override List<List<double>> GenerateValues() {
      List<List<double>> data = new List<List<double>>();
      for (int i = 0; i < AllowedInputVariables.Count(); i++) {
        data.Add(Enumerable.Range(0, TestPartitionEnd)
          .Select(_ => xRandom.NextDouble())
          .ToList());
      }

      var random = new MersenneTwister();
      var selectedFeatures =
        Enumerable.Range(0, AllowedInputVariables.Count())
        .Where(_ => random.NextDouble() < selectionProbability)
        .ToArray();

      w = selectedFeatures.Select(_ => weightRandom.NextDouble()).ToArray();
      var target = new List<double>();
      for (int i = 0; i < data[0].Count; i++) {
        var s = selectedFeatures
          .Select(index => data[index][i])
          .ToArray();
        target.Add(ScalarProd(s, w));
      }
      var targetSigma = target.StandardDeviation();
      var noisePrng = new NormalDistributedRandom(random, 0, targetSigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio)));

      data.Add(target.Select(t => t + noisePrng.NextDouble()).ToList());

      // set property listing the selected features as string[]
      this.selectedFeatures = selectedFeatures.Select(i => AllowedInputVariables[i]).ToArray();
      optimalRSquared = 1 - noiseRatio;
      return data;
    }
Ejemplo n.º 11
0
    /// <summary>
    /// Generates a new normally distributed random variable and assigns it to the specified variable.
    /// </summary>
    public override IOperation Apply() {
      IRandom random = RandomParameter.ActualValue;
      double mu = MuParameter.ActualValue.Value;
      double sigma = SigmaParameter.ActualValue.Value;

      NormalDistributedRandom normalRandom = new NormalDistributedRandom(random, mu, sigma);
      ValueParameter.ActualValue = new DoubleValue(normalRandom.NextDouble());
      return base.Apply();
    }
Ejemplo n.º 12
0
    public override IOperation Apply() {
      var maxTries = MaxTriesParameter.Value.Value;
      var truncateAtBounds = TruncateAtBoundsParameter.Value.Value;
      var random = RandomParameter.ActualValue;
      var lambda = PopulationSizeParameter.ActualValue.Value;
      var xmean = MeanParameter.ActualValue;
      var arx = RealVectorParameter.ActualValue;
      var sp = StrategyParametersParameter.ActualValue;
      var iterations = IterationsParameter.ActualValue.Value;
      var initialIterations = sp.InitialIterations;
      var bounds = BoundsParameter.ActualValue;

      if (arx == null || arx.Length == 0) {
        arx = new ItemArray<RealVector>(lambda);
        for (int i = 0; i < lambda; i++) arx[i] = new RealVector(xmean.Length);
        RealVectorParameter.ActualValue = arx;
      }
      var nd = new NormalDistributedRandom(random, 0, 1);

      var length = arx[0].Length;

      for (int i = 0; i < lambda; i++) {
        int tries = 0;
        bool inRange;
        if (initialIterations > iterations) {
          for (int k = 0; k < length; k++) {
            do {
              arx[i][k] = xmean[k] + sp.Sigma * sp.D[k] * nd.NextDouble();
              inRange = bounds[k % bounds.Rows, 0] <= arx[i][k] && arx[i][k] <= bounds[k % bounds.Rows, 1];
              if (!inRange) tries++;
            } while (!inRange && tries < maxTries);
            if (!inRange && truncateAtBounds) {
              if (bounds[k % bounds.Rows, 0] > arx[i][k]) arx[i][k] = bounds[k % bounds.Rows, 0];
              else if (bounds[k % bounds.Rows, 1] < arx[i][k]) arx[i][k] = bounds[k % bounds.Rows, 1];
            }
          }
        } else {
          var B = sp.B;
          do {
            tries++;
            inRange = true;
            var artmp = new double[length];
            for (int k = 0; k < length; ++k) {
              artmp[k] = sp.D[k] * nd.NextDouble();
            }

            for (int k = 0; k < length; k++) {
              var sum = 0.0;
              for (int j = 0; j < length; j++)
                sum += B[k, j] * artmp[j];
              arx[i][k] = xmean[k] + sp.Sigma * sum; // m + sig * Normal(0,C)
              if (bounds[k % bounds.Rows, 0] > arx[i][k] || arx[i][k] > bounds[k % bounds.Rows, 1])
                inRange = false;
            }
          } while (!inRange && tries < maxTries);
          if (!inRange && truncateAtBounds) {
            for (int k = 0; k < length; k++) {
              if (bounds[k % bounds.Rows, 0] > arx[i][k]) arx[i][k] = bounds[k % bounds.Rows, 0];
              else if (bounds[k % bounds.Rows, 1] < arx[i][k]) arx[i][k] = bounds[k % bounds.Rows, 1];
            }
          }
        }
      }
      return base.Apply();
    }
Ejemplo n.º 13
0
    protected override List<List<double>> GenerateValues() {
      // variable names are shuffled in the beginning (and sorted at the end)
      variableNames = variableNames.Shuffle(random).ToArray();

      // a third of all variables are independent vars
      List<List<double>> lvl0 = new List<List<double>>();
      int numLvl0 = (int)Math.Ceiling(numberOfFeatures * 0.33);

      List<string> description = new List<string>(); // store information how the variable is actually produced
      List<string[]> inputVarNames = new List<string[]>(); // store information to produce graphviz file

      var nrand = new NormalDistributedRandom(random, 0, 1);
      for (int c = 0; c < numLvl0; c++) {
        var datai = Enumerable.Range(0, TestPartitionEnd).Select(_ => nrand.NextDouble()).ToList();
        inputVarNames.Add(new string[] { });
        description.Add("~ N(0, 1)");
        lvl0.Add(datai);
      }

      // lvl1 contains variables which are functions of vars in lvl0 (+ noise)
      List<List<double>> lvl1 = new List<List<double>>();
      int numLvl1 = (int)Math.Ceiling(numberOfFeatures * 0.33);
      for (int c = 0; c < numLvl1; c++) {
        string[] selectedVarNames;
        var x = GenerateRandomFunction(random, lvl0, out selectedVarNames);
        var sigma = x.StandardDeviation();
        var noisePrng = new NormalDistributedRandom(random, 0, sigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio)));
        lvl1.Add(x.Select(t => t + noisePrng.NextDouble()).ToList());

        inputVarNames.Add(selectedVarNames);
        var desc = string.Format("f({0})", string.Join(",", selectedVarNames));
        description.Add(string.Format(" ~ N({0}, {1:N3})", desc, noisePrng.Sigma));
      }

      // lvl2 contains variables which are functions of vars in lvl0 and lvl1 (+ noise)
      List<List<double>> lvl2 = new List<List<double>>();
      int numLvl2 = (int)Math.Ceiling(numberOfFeatures * 0.2);
      for (int c = 0; c < numLvl2; c++) {
        string[] selectedVarNames;
        var x = GenerateRandomFunction(random, lvl0.Concat(lvl1).ToList(), out selectedVarNames);
        var sigma = x.StandardDeviation();
        var noisePrng = new NormalDistributedRandom(random, 0, sigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio)));
        lvl2.Add(x.Select(t => t + noisePrng.NextDouble()).ToList());

        inputVarNames.Add(selectedVarNames);
        var desc = string.Format("f({0})", string.Join(",", selectedVarNames));
        description.Add(string.Format(" ~ N({0}, {1:N3})", desc, noisePrng.Sigma));
      }

      // lvl3 contains variables which are functions of vars in lvl0, lvl1 and lvl2 (+ noise)
      List<List<double>> lvl3 = new List<List<double>>();
      int numLvl3 = numberOfFeatures - numLvl0 - numLvl1 - numLvl2;
      for (int c = 0; c < numLvl3; c++) {
        string[] selectedVarNames;
        var x = GenerateRandomFunction(random, lvl0.Concat(lvl1).Concat(lvl2).ToList(), out selectedVarNames);
        var sigma = x.StandardDeviation();
        var noisePrng = new NormalDistributedRandom(random, 0, sigma * Math.Sqrt(noiseRatio / (1.0 - noiseRatio)));
        lvl3.Add(x.Select(t => t + noisePrng.NextDouble()).ToList());

        inputVarNames.Add(selectedVarNames);
        var desc = string.Format("f({0})", string.Join(",", selectedVarNames));
        description.Add(string.Format(" ~ N({0}, {1:N3})", desc, noisePrng.Sigma));
      }

      networkDefinition = string.Join(Environment.NewLine, variableNames.Zip(description, (n, d) => n + d));
      // for graphviz
      networkDefinition += Environment.NewLine + "digraph G {";
      foreach (var t in variableNames.Zip(inputVarNames, Tuple.Create).OrderBy(t => t.Item1)) {
        var name = t.Item1;
        var selectedVarNames = t.Item2;
        foreach (var selectedVarName in selectedVarNames) {
          networkDefinition += Environment.NewLine + selectedVarName + " -> " + name;
        }
      }
      networkDefinition += Environment.NewLine + "}";

      // return a random permutation of all variables
      var allVars = lvl0.Concat(lvl1).Concat(lvl2).Concat(lvl3).ToList();
      var orderedVars = allVars.Zip(variableNames, Tuple.Create).OrderBy(t => t.Item2).Select(t => t.Item1).ToList();
      variableNames = variableNames.OrderBy(n => n).ToArray();
      return orderedVars;
    }
 /// <summary>
 /// Performs an adaptive normally distributed all position manipulation on the given 
 /// <paramref name="vector"/>.
 /// </summary>
 /// <exception cref="InvalidOperationException">Thrown when the strategy vector is not
 /// as long as the vector to get manipulated.</exception>
 /// <param name="strategyParameters">The strategy vector determining the strength of the mutation.</param>
 /// <param name="random">A random number generator.</param>
 /// <param name="vector">The real vector to manipulate.</param>
 /// <returns>The manipulated real vector.</returns>
 public static void Apply(IRandom random, RealVector vector, RealVector strategyParameters) {
   NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0);
   if (strategyParameters != null) {
     for (int i = 0; i < vector.Length; i++) {
       vector[i] = vector[i] + (N.NextDouble() * strategyParameters[i % strategyParameters.Length]);
     }
   } else {
     // BackwardsCompatibility3.3
     #region Backwards compatible region (remove with 3.4)
     // when upgrading to 3.4 remove the whole condition and just put the if-branch in place (you should then also throw an ArgumentException when strategyParameters is null or has length 0)
     for (int i = 0; i < vector.Length; i++) {
       vector[i] = vector[i] + N.NextDouble();
     }
     #endregion
   }
 }