Exemplo n.º 1
0
 public void LinkageTreeTestAdd() {
   MersenneTwister rand = new MersenneTwister();
   LinkageTree tree = new LinkageTree(Length, rand);
   tree.Add(solutions[0]);
   tree.Add(solutions[1]);
   PrivateObject hidden = new PrivateObject(tree);
   int[][][] result = (int[][][])hidden.GetField("occurances");
   Assert.AreEqual(1, result[1][0][0]); // Positions 0 and 1 had value 00 exactly once
   Assert.AreEqual(2, result[Length - 1][Length - 2][0]); // Positions 0 and 1 had value 00 exactly once
   Assert.AreEqual(0, result[Length - 1][Length - 2][1]); // Positions 7 and 8 never had value 10
   Assert.AreEqual(1, result[1][0][3]); // Positions 0 and 1 had value 11 exactly once
 }
    private void TestCalculatorPerfomance(CalcateFunc calculateFunc) {
      var twister = new MersenneTwister(31415);
      var dataset = CreateRandomDataset(twister, Rows, Columns);
      OnlineCalculatorError errorState = OnlineCalculatorError.None; ;

      Stopwatch watch = new Stopwatch();
      watch.Start();
      for (int i = 0; i < Repetitions; i++) {
        double value = calculateFunc(dataset.GetDoubleValues("y"), dataset.GetDoubleValues("x0"), out errorState);
      }
      Assert.AreEqual(errorState, OnlineCalculatorError.None);
      watch.Stop();

      TestContext.WriteLine("");
      TestContext.WriteLine("Calculated Rows per milisecond: {0}.", Rows * Repetitions * 1.0 / watch.ElapsedMilliseconds);
    }
Exemplo n.º 3
0
    public void LinkageTreeTestEntropyDistance() {
      MersenneTwister rand = new MersenneTwister();
      LinkageTree tree = new LinkageTree(Length, rand);
      PrivateObject hidden = new PrivateObject(tree);
      // No information should result in a distance of 0
      Assert.AreEqual((double)0, hidden.Invoke("EntropyDistance", new object[] { 0, 1 }));
      foreach (var solution in solutions) {
        tree.Add(solution);
      }
      // Check that 0 and 1 are closer than 0 and 2
      var linked = (double)hidden.Invoke("EntropyDistance", new object[] { 0, 1 });
      var unlinked = (double)hidden.Invoke("EntropyDistance", new object[] { 0, 2 });
      Assert.IsTrue(linked < unlinked);

      // Reversing the arguments should not change the result
      var forward = hidden.Invoke("EntropyDistance", new object[] { Length - 1, Length - 2 });
      var backward = hidden.Invoke("EntropyDistance", new object[] { Length - 2, Length - 1 });
      Assert.AreEqual(forward, backward);
    }
Exemplo n.º 4
0
    public void LinkageTreeTestRebuild() {
      // The seed matters as equal sized clusters can appear in any order
      MersenneTwister rand = new MersenneTwister(123);
      LinkageTree tree = new LinkageTree(Length, rand);
      foreach (var solution in solutions) {
        tree.Add(solution);
      }

      // Check if the clusters created contain the expected variables.
      var found = tree.Clusters.ToArray();
      Assert.AreEqual(correctClusters.Length, found.Length);
      for (int i = 0; i < found.Length; i++) {
        found[i].Sort();
        Assert.IsTrue(found[i].SequenceEqual(correctClusters[i]), string.Format("Failed On Cluster {0}", i));
      }
    }
 private void randFill(IList<bool> data, MersenneTwister rand) {
   for (int i = 0; i < data.Count; i++) {
     data[i] = rand.Next(2) == 1;
   }
 }
    private void Initialize(IEnumerable<string> variableNames, int nConstants) {
      #region symbol declaration
      var add = new Addition();
      var sub = new Subtraction();
      var mul = new Multiplication();
      var div = new Division();
      var mean = new Average();
      var log = new Logarithm();
      var pow = new Power();
      var square = new Square();
      var root = new Root();
      var sqrt = new SquareRoot();
      var exp = new Exponential();

      // we use our own random number generator here because we assume 
      // that grammars are only initialized once when setting the grammar in the problem.
      // This means everytime the grammar parameter in the problem is changed
      // we initialize the constants to new values
      var rand = new MersenneTwister();
      // warm up
      for (int i = 0; i < 1000; i++) rand.NextDouble();

      var constants = new List<Constant>(nConstants);
      for (int i = 0; i < nConstants; i++) {
        var constant = new Constant();
        do {
          var constVal = rand.NextDouble() * 20.0 - 10.0;
          constant.Name = string.Format("{0:0.000}", constVal);
          constant.MinValue = constVal;
          constant.MaxValue = constVal;
          constant.ManipulatorSigma = 0.0;
          constant.ManipulatorMu = 0.0;
          constant.MultiplicativeManipulatorSigma = 0.0;
        } while (constants.Any(c => c.Name == constant.Name)); // unlikely, but it could happen that the same constant value is sampled twice. so we resample if necessary.
        constants.Add(constant);
      }

      var variables = new List<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>();
      foreach (var variableName in variableNames) {
        var variableSymbol = new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable();
        variableSymbol.Name = variableName;
        variableSymbol.WeightManipulatorMu = 0.0;
        variableSymbol.WeightManipulatorSigma = 0.0;
        variableSymbol.WeightMu = 1.0;
        variableSymbol.WeightSigma = 0.0;
        variableSymbol.MultiplicativeWeightManipulatorSigma = 0.0;
        variableSymbol.AllVariableNames = new[] { variableName };
        variableSymbol.VariableNames = new[] { variableName };
        variables.Add(variableSymbol);
      }

      #endregion

      AddSymbol(add);
      AddSymbol(sub);
      AddSymbol(mul);
      AddSymbol(div);
      AddSymbol(mean);
      AddSymbol(log);
      AddSymbol(pow);
      AddSymbol(square);
      AddSymbol(root);
      AddSymbol(sqrt);
      AddSymbol(exp);
      constants.ForEach(AddSymbol);
      variables.ForEach(AddSymbol);

      #region subtree count configuration
      SetSubtreeCount(add, 2, 2);
      SetSubtreeCount(sub, 2, 2);
      SetSubtreeCount(mul, 2, 2);
      SetSubtreeCount(div, 2, 2);
      SetSubtreeCount(mean, 2, 2);
      SetSubtreeCount(log, 1, 1);
      SetSubtreeCount(pow, 2, 2);
      SetSubtreeCount(square, 1, 1);
      SetSubtreeCount(root, 2, 2);
      SetSubtreeCount(sqrt, 1, 1);
      SetSubtreeCount(exp, 1, 1);
      constants.ForEach((c) => SetSubtreeCount(c, 0, 0));
      variables.ForEach((v) => SetSubtreeCount(v, 0, 0));
      #endregion

      var functions = new ISymbol[] { add, sub, mul, div, mean, log, pow, root, square, sqrt };
      var terminalSymbols = variables.Concat<ISymbol>(constants);
      var allSymbols = functions.Concat(terminalSymbols);

      #region allowed child symbols configuration
      foreach (var s in allSymbols) {
        AddAllowedChildSymbol(StartSymbol, s);
      }
      foreach (var parentSymb in functions)
        foreach (var childSymb in allSymbols) {
          AddAllowedChildSymbol(parentSymb, childSymb);
        }

      #endregion
    }
 public static Dataset CreateRandomDataset(MersenneTwister twister, int rows, int columns) {
   double[,] data = new double[rows, columns];
   for (int i = 0; i < rows; i++) {
     for (int j = 0; j < columns; j++) {
       data[i, j] = twister.NextDouble() * 2.0 - 1.0;
     }
   }
   IEnumerable<string> variableNames = new string[] { "y" }.Concat(Enumerable.Range(0, columns - 1).Select(x => "x" + x.ToString()));
   Dataset ds = new Dataset(variableNames, data);
   return ds;
 }