Exemple #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
                              );
        }
Exemple #2
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
        }