Beispiel #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
 }
Beispiel #2
0
 // In the GECCO paper, Figure 3
 public static double ImproveUsingTree(LinkageTree tree, IList<BinaryVector> donors, BinaryVector solution, double fitness, BinaryProblem problem, IRandom rand) {
   var options = Enumerable.Range(0, donors.Count).ToArray();
   foreach (var cluster in tree.Clusters) {
     // Find a donor which has at least one gene value different
     // from the current solution for this cluster of genes
     bool donorFound = false;
     foreach (var donorIndex in options.ShuffleList(rand)) {
       // Attempt the donation
       fitness = Donate(solution, fitness, donors[donorIndex], cluster, problem, rand, out donorFound);
       if (donorFound) break;
     }
   }
   return fitness;
 }
Beispiel #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);
    }
Beispiel #4
0
        protected LinkageTree(LinkageTree original, Cloner cloner) : base(original, cloner)
        {
            occurances = new int[original.occurances.Length][][];
            //mkommend: first entry is not used, cf. ctor line 83
            for (int i = 1; i < original.occurances.Length; i++)
            {
                occurances[i] = new int[original.occurances[i].Length][];
                for (int j = 0; j < original.occurances[i].Length; j++)
                {
                    occurances[i][j] = original.occurances[i][j].ToArray();
                }
            }

            clusters        = original.clusters.Select(c => c.ToList()).ToArray();
            clusterOrdering = new List <int>(original.clusterOrdering);
            length          = original.length;
            rand            = cloner.Clone(original.rand);
            rebuildRequired = original.rebuildRequired;
        }
        // In the GECCO paper, Figure 3
        public static double ImproveUsingTree(LinkageTree tree, IList <BinaryVector> donors, BinaryVector solution, double fitness, BinaryProblem problem, IRandom rand)
        {
            var options = Enumerable.Range(0, donors.Count).ToArray();

            foreach (var cluster in tree.Clusters)
            {
                // Find a donor which has at least one gene value different
                // from the current solution for this cluster of genes
                bool donorFound = false;
                foreach (var donorIndex in options.ShuffleList(rand))
                {
                    // Attempt the donation
                    fitness = Donate(solution, fitness, donors[donorIndex], cluster, problem, rand, out donorFound);
                    if (donorFound)
                    {
                        break;
                    }
                }
            }
            return(fitness);
        }
Beispiel #6
0
 public Population(int length, IRandom rand) {
   Solutions = new List<BinaryVector>();
   Tree = new LinkageTree(length, rand);
 }
Beispiel #7
0
 public Population(int length, IRandom rand)
 {
     Solutions = new List <BinaryVector>();
     Tree      = new LinkageTree(length, rand);
 }
Beispiel #8
0
 protected Population(Population original, Cloner cloner) : base(original, cloner)
 {
     Solutions = original.Solutions.Select(cloner.Clone).ToList();
     Tree      = cloner.Clone(original.Tree);
 }
Beispiel #9
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));
      }
    }