예제 #1
0
        public void ArgumentCreaterDistributionsTest()
        {
            var trees     = new List <ISymbolicExpressionTree>();
            var grammar   = Grammars.CreateArithmeticAndAdfGrammar();
            var random    = new MersenneTwister(31415);
            int failedOps = 0;

            for (int i = 0; i < POPULATION_SIZE; i++)
            {
                ISymbolicExpressionTree tree;
                do
                {
                    tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH);
                    SubroutineCreater.CreateSubroutine(random, tree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
                } while (!TreeHasAdfWithParameter(tree, 3));
                var success = ArgumentCreater.CreateNewArgument(random, tree, 60000, 100, 3, 3);
                if (!success)
                {
                    failedOps++;
                }
                Util.IsValid(tree);
                trees.Add(tree);
            }
            // difficult to make sure that create argument operations succeed because trees are macro-expanded can potentially become very big
            // => just test if only a small proportion fails
            Assert.IsTrue(failedOps < POPULATION_SIZE * 0.05); // only 5% may fail
            Console.WriteLine("ArgumentCreator: " + Environment.NewLine +
                              "Failed operations: " + failedOps * 100.0 / POPULATION_SIZE + " %" + Environment.NewLine +
                              Util.GetSizeDistributionString(trees, 200, 20) + Environment.NewLine +
                              Util.GetFunctionDistributionString(trees) + Environment.NewLine +
                              Util.GetNumberOfSubtreesDistributionString(trees) + Environment.NewLine +
                              Util.GetTerminalDistributionString(trees) + Environment.NewLine
                              );
        }
예제 #2
0
        public void ArgumentDuplicaterDistributionsTest()
        {
            var trees   = new List <ISymbolicExpressionTree>();
            var grammar = Grammars.CreateArithmeticAndAdfGrammar();
            var random  = new MersenneTwister(31415);

            for (int i = 0; i < POPULATION_SIZE; i++)
            {
                ISymbolicExpressionTree tree = null;
                do
                {
                    tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH);
                    SubroutineCreater.CreateSubroutine(random, tree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
                } while (!HasAdfWithArguments(tree));
                var success = ArgumentDuplicater.DuplicateArgument(random, tree, 3, 3);
                Assert.IsTrue(success);
                Util.IsValid(tree);
                trees.Add(tree);
            }
            Console.WriteLine("ArgumentDuplicater: " + Environment.NewLine +
                              Util.GetSizeDistributionString(trees, 200, 5) + Environment.NewLine +
                              Util.GetFunctionDistributionString(trees) + Environment.NewLine +
                              Util.GetNumberOfSubtreesDistributionString(trees) + Environment.NewLine +
                              Util.GetTerminalDistributionString(trees) + Environment.NewLine
                              );
        }
        public void ChangeNodeTypeManipulationDistributionsTest()
        {
            SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter();
            var trees        = new List <ISymbolicExpressionTree>();
            var grammar      = Grammars.CreateArithmeticAndAdfGrammar();
            var random       = new MersenneTwister(31415);
            int failedEvents = 0;

            for (int i = 0; i < POPULATION_SIZE; i++)
            {
                var    tree         = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH);
                string originalTree = formatter.Format(tree);
                ChangeNodeTypeManipulation.ChangeNodeType(random, tree);
                string manipulatedTree = formatter.Format(tree);
                if (originalTree == manipulatedTree)
                {
                    failedEvents++;
                }
                Util.IsValid(tree);
                trees.Add(tree);
            }
            Console.WriteLine("ChangeNodeTypeManipulation: " + Environment.NewLine +
                              "Failed events: " + failedEvents * 100.0 / POPULATION_SIZE + " %" + Environment.NewLine +
                              Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
                              Util.GetFunctionDistributionString(trees) + Environment.NewLine +
                              Util.GetNumberOfSubtreesDistributionString(trees) + Environment.NewLine +
                              Util.GetTerminalDistributionString(trees) + Environment.NewLine
                              );
            Assert.IsTrue(failedEvents * 100.0 / POPULATION_SIZE < 5.0); // only max 5% failed mutations are allowed
        }
        public void ReplaceBranchManipulationDistributionsTest()
        {
            SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter();
            var trees   = new List <ISymbolicExpressionTree>();
            var grammar = Grammars.CreateArithmeticAndAdfGrammar();
            var random  = new MersenneTwister(31415);

            for (int i = 0; i < POPULATION_SIZE; i++)
            {
                var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH);
                SubroutineCreater.CreateSubroutine(random, tree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
                string originalTree = formatter.Format(tree);
                ReplaceBranchManipulation.ReplaceRandomBranch(random, tree, MAX_TREE_LENGTH, MAX_TREE_DEPTH);
                string manipulatedTree = formatter.Format(tree);
                Assert.IsFalse(originalTree == manipulatedTree);
                Util.IsValid(tree);
                trees.Add(tree);
            }
            Console.WriteLine("ReplaceBranchManipulation: " + Environment.NewLine +
                              Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
                              Util.GetFunctionDistributionString(trees) + Environment.NewLine +
                              Util.GetNumberOfSubtreesDistributionString(trees) + Environment.NewLine +
                              Util.GetTerminalDistributionString(trees) + Environment.NewLine
                              );
        }
예제 #5
0
        public void ProbabilisticTreeCreatorSpecificTreeSizesTest()
        {
            var trees           = new List <ISymbolicExpressionTree>();
            var grammar         = Grammars.CreateSimpleArithmeticGrammar();
            var random          = new MersenneTwister(31415);
            var treeGrammarType = SymbolicExpressionTreeGrammar_Accessor.ShadowedType.ReferencedType;


            for (int targetTreeSize = 1; targetTreeSize <= 100; targetTreeSize++)
            {
                var tree     = new SymbolicExpressionTree();
                var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
                rootNode.SetGrammar((ISymbolicExpressionTreeGrammar)Activator.CreateInstance(treeGrammarType, grammar));
                if (rootNode.HasLocalParameters)
                {
                    rootNode.ResetLocalParameters(random);
                }
                var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
                startNode.SetGrammar((ISymbolicExpressionTreeGrammar)Activator.CreateInstance(treeGrammarType, grammar));
                if (startNode.HasLocalParameters)
                {
                    startNode.ResetLocalParameters(random);
                }
                rootNode.AddSubtree(startNode);

                ProbabilisticTreeCreator_Accessor.TryCreateFullTreeFromSeed(random, startNode, targetTreeSize, ((int)Math.Log(targetTreeSize, 2)) + 1);
                tree.Root = rootNode;
                //mkommend: commented due to performance issues on the builder
                // Assert.AreEqual(targetTreeSize + 2, tree.Length);  //the root and start node must be additionally added
            }
        }
예제 #6
0
        public void ProbabilisticTreeCreatorSpecificTreeSizesTest()
        {
            var grammar = Grammars.CreateSimpleArithmeticGrammar();
            var random  = new MersenneTwister(31415);

            for (int targetTreeSize = 3; targetTreeSize <= 100; targetTreeSize++)
            {
                var tree = ProbabilisticTreeCreator.CreateExpressionTree(random, grammar, targetTreeSize, ((int)Math.Log(targetTreeSize, 2)) + 2);
                Assert.AreEqual(targetTreeSize, tree.Length);
            }
        }
예제 #7
0
        public void SubtreeCrossoverDistributionsTest()
        {
            int    generations = 5;
            var    trees       = new List <ISymbolicExpressionTree>();
            var    grammar     = Grammars.CreateArithmeticAndAdfGrammar();
            var    random      = new MersenneTwister(31415);
            double msPerCrossoverEvent;

            for (int i = 0; i < POPULATION_SIZE; i++)
            {
                trees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10));
                for (int j = random.Next(3); j < 3; j++)
                {
                    SubroutineCreater.CreateSubroutine(random, trees[i], 100, 10, 3, 3);
                }
            }
            var children = new List <ISymbolicExpressionTree>(trees);

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            for (int gCount = 0; gCount < generations; gCount++)
            {
                for (int i = 0; i < POPULATION_SIZE; i++)
                {
                    var par0 = (ISymbolicExpressionTree)trees.SampleRandom(random).Clone();
                    var par1 = (ISymbolicExpressionTree)trees.SampleRandom(random).Clone();
                    children[i] = SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10);
                }
                trees = children;
            }
            stopwatch.Stop();
            foreach (var tree in trees)
            {
                Util.IsValid(tree);
            }

            msPerCrossoverEvent = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE / (double)generations;

            Console.WriteLine("SubtreeCrossover: " + Environment.NewLine +
                              msPerCrossoverEvent + " ms per crossover event (~" + Math.Round(1000.0 / (msPerCrossoverEvent)) + "crossovers / s)" + Environment.NewLine +
                              Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
                              Util.GetFunctionDistributionString(trees) + Environment.NewLine +
                              Util.GetNumberOfSubtreesDistributionString(trees) + Environment.NewLine +
                              Util.GetTerminalDistributionString(trees) + Environment.NewLine
                              );

            //mkommend: commented due to performance issues on the builder
            //Assert.IsTrue(Math.Round(1000.0 / (msPerCrossoverEvent)) > 2000); // must achieve more than 2000 x-overs/s
        }
예제 #8
0
        public void FullTreeCreatorDistributionsTest()
        {
            var randomTrees = new List <ISymbolicExpressionTree>();
            var grammar     = Grammars.CreateSimpleArithmeticGrammar();
            var random      = new MersenneTwister(31415);
            var stopwatch   = new Stopwatch();

            stopwatch.Start();
            for (int i = 0; i != POPULATION_SIZE; i++)
            {
                randomTrees.Add(FullTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH));
            }
            stopwatch.Stop();
            double msPerRandomTreeCreation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE;
            int    count     = 0;
            int    depth     = 0;
            int    maxLength = 0;

            foreach (var tree in randomTrees)
            {
                Util.IsValid(tree);
                Assert.IsTrue(tree.Depth - 1 == MAX_TREE_DEPTH);
                if (maxLength < tree.Length)
                {
                    maxLength = tree.Length;
                }
                count += tree.Length;
                depth += tree.Depth;
            }

            Console.WriteLine("FullTreeCreator: " + Environment.NewLine +
                              msPerRandomTreeCreation + " ms per random tree (~" +
                              Math.Round(1000.0 / (msPerRandomTreeCreation)) + "random trees / s)" + Environment.NewLine +
                              Util.GetSizeDistributionString(randomTrees, maxLength, 5) + Environment.NewLine +
                              Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine +
                              Util.GetNumberOfSubtreesDistributionString(randomTrees) + Environment.NewLine +
                              Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine +
                              "Average tree depth: " + depth / POPULATION_SIZE + Environment.NewLine +
                              "Average tree length: " + count / POPULATION_SIZE + Environment.NewLine +
                              "Maximum tree length: " + maxLength + Environment.NewLine +
                              "Total nodes created: " + count + Environment.NewLine
                              );
            //mkommend: commented due to performance issues on the builder
            //Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 300); // must achieve more than 300 random trees / s
        }
예제 #9
0
        public void AllArchitectureAlteringOperatorsDistributionTest()
        {
            var trees    = new List <ISymbolicExpressionTree>();
            var newTrees = new List <ISymbolicExpressionTree>();
            var grammar  = Grammars.CreateArithmeticAndAdfGrammar();
            var random   = new MersenneTwister(31415);
            SymbolicExpressionTreeStringFormatter formatter = new SymbolicExpressionTreeStringFormatter();
            IntValue maxTreeSize   = new IntValue(MAX_TREE_LENGTH);
            IntValue maxTreeHeigth = new IntValue(MAX_TREE_DEPTH);
            IntValue maxDefuns     = new IntValue(3);
            IntValue maxArgs       = new IntValue(3);

            for (int i = 0; i < POPULATION_SIZE; i++)
            {
                var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH);
                Util.IsValid(tree);
                trees.Add(tree);
            }
            Stopwatch stopwatch    = new Stopwatch();
            int       failedEvents = 0;

            for (int g = 0; g < N_ITERATIONS; g++)
            {
                for (int i = 0; i < POPULATION_SIZE; i++)
                {
                    if (random.NextDouble() < 0.5)
                    {
                        // manipulate
                        stopwatch.Start();
                        var  selectedTree = (ISymbolicExpressionTree)trees.SampleRandom(random).Clone();
                        var  oldTree      = (ISymbolicExpressionTree)selectedTree.Clone();
                        bool success      = false;
                        int  sw           = random.Next(6);
                        switch (sw)
                        {
                        case 0: success = ArgumentCreater.CreateNewArgument(random, selectedTree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); break;

                        case 1: success = ArgumentDeleter.DeleteArgument(random, selectedTree, 3, 3); break;

                        case 2: success = ArgumentDuplicater.DuplicateArgument(random, selectedTree, 3, 3); break;

                        case 3: success = SubroutineCreater.CreateSubroutine(random, selectedTree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3); break;

                        case 4: success = SubroutineDuplicater.DuplicateSubroutine(random, selectedTree, 3, 3); break;

                        case 5: success = SubroutineDeleter.DeleteSubroutine(random, selectedTree, 3, 3); break;
                        }
                        stopwatch.Stop();
                        if (!success)
                        {
                            failedEvents++;
                        }
                        Util.IsValid(selectedTree);
                        newTrees.Add(selectedTree);
                    }
                    else
                    {
                        stopwatch.Start();
                        // crossover
                        SymbolicExpressionTree par0 = null;
                        SymbolicExpressionTree par1 = null;
                        do
                        {
                            par0 = (SymbolicExpressionTree)trees.SampleRandom(random).Clone();
                            par1 = (SymbolicExpressionTree)trees.SampleRandom(random).Clone();
                        } while (par0.Length > MAX_TREE_LENGTH || par1.Length > MAX_TREE_LENGTH);
                        var newTree = SubtreeCrossover.Cross(random, par0, par1, 0.9, MAX_TREE_LENGTH, MAX_TREE_DEPTH);
                        stopwatch.Stop();
                        Util.IsValid(newTree);
                        newTrees.Add(newTree);
                    }
                }
                trees = new List <ISymbolicExpressionTree>(newTrees);
                newTrees.Clear();
            }
            var msPerOperation = stopwatch.ElapsedMilliseconds / ((double)POPULATION_SIZE * (double)N_ITERATIONS);

            Console.WriteLine("AllArchitectureAlteringOperators: " + Environment.NewLine +
                              "Operations / s: ~" + Math.Round(1000.0 / (msPerOperation)) + "operations / s)" + Environment.NewLine +
                              "Failed events: " + failedEvents * 100.0 / (double)(POPULATION_SIZE * N_ITERATIONS / 2.0) + "%" + Environment.NewLine +
                              Util.GetSizeDistributionString(trees, 200, 5) + Environment.NewLine +
                              Util.GetFunctionDistributionString(trees) + Environment.NewLine +
                              Util.GetNumberOfSubtreesDistributionString(trees) + Environment.NewLine +
                              Util.GetTerminalDistributionString(trees) + Environment.NewLine
                              );

            Assert.IsTrue(failedEvents * 100.0 / (POPULATION_SIZE * N_ITERATIONS / 2.0) < 75.0); // 25% of architecture operations must succeed
            //mkommend: commented due to performance issues on the builder
            // Assert.IsTrue(Math.Round(1000.0 / (msPerOperation)) > 800); // must achieve more than 800 ops per second
        }