Exemple #1
0
        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
        }
Exemple #2
0
        public void SubroutineDeleterDistributionsTest()
        {
            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);
                    SubroutineCreater.CreateSubroutine(random, tree, MAX_TREE_LENGTH, MAX_TREE_DEPTH, 3, 3);
                } while (!HasAtLeastOneAdf(tree));
                var success = SubroutineDeleter.DeleteSubroutine(random, tree, 3, 3);
                Assert.IsTrue(success);
                Util.IsValid(tree);
                trees.Add(tree);
            }
            Console.WriteLine("SubroutineDeleter: " + Environment.NewLine +
                              Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
                              Util.GetFunctionDistributionString(trees) + Environment.NewLine +
                              Util.GetNumberOfSubtreesDistributionString(trees) + Environment.NewLine +
                              Util.GetTerminalDistributionString(trees) + Environment.NewLine
                              );
        }
Exemple #3
0
        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
                              );
        }
Exemple #4
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
                              );
        }
        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
        }
Exemple #6
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
        }