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 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 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 ); }
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 ); }
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); } }
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 }
public static ISymbolicExpressionTree[] CreateRandomTrees(MersenneTwister twister, Dataset dataset, ISymbolicExpressionGrammar grammar, int popSize, int minSize, int maxSize, int maxFunctionDefinitions, int maxFunctionArguments) { foreach (Variable variableSymbol in grammar.Symbols.OfType <Variable>()) { variableSymbol.VariableNames = dataset.VariableNames.Skip(1); } ISymbolicExpressionTree[] randomTrees = new ISymbolicExpressionTree[popSize]; for (int i = 0; i < randomTrees.Length; i++) { randomTrees[i] = ProbabilisticTreeCreator.Create(twister, grammar, maxSize, 10); } return(randomTrees); }
protected static ISymbolicExpressionTreeNode GenerateAndInsertNewSubtree(IRandom random, ISymbolicExpressionTreeNode parent, List <ISymbol> allowedSymbols, int childIndex, int maxLength, int maxDepth) { var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList(); #pragma warning disable 612, 618 var seedSymbol = allowedSymbols.SelectRandom(weights, random); #pragma warning restore 612, 618 // replace the old node with the new node var seedNode = seedSymbol.CreateTreeNode(); if (seedNode.HasLocalParameters) { seedNode.ResetLocalParameters(random); } parent.RemoveSubtree(childIndex); parent.InsertSubtree(childIndex, seedNode); ProbabilisticTreeCreator.PTC2(random, seedNode, maxLength, maxDepth); return(seedNode); }
public void ProbabilisticTreeCreaterDistributionsTest() { 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(ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_LENGTH, MAX_TREE_DEPTH)); } stopwatch.Stop(); int count = 0; int depth = 0; foreach (var tree in randomTrees) { Util.IsValid(tree); count += tree.Length; depth += tree.Depth; } double msPerRandomTreeCreation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE; Console.WriteLine("ProbabilisticTreeCreator: " + Environment.NewLine + msPerRandomTreeCreation + " ms per random tree (~" + Math.Round(1000.0 / (msPerRandomTreeCreation)) + "random trees / s)" + Environment.NewLine + Util.GetSizeDistributionString(randomTrees, 105, 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 + "Total nodes created: " + count + Environment.NewLine ); //mkommend: commented due to performance issues on the builder // Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 250); // must achieve more than 250 random trees / s }
public override void ReplaceBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, ICFGPythonProblemData problemData, ItemArray <PythonStatementSemantic> semantics, PythonProcess pythonProcess, double timeout, int maxTreeLength, int maxTreeDepth, int maximumSemanticTries) { if (semantics == null || semantics.Length == 0) { ReplaceBranchManipulation.ReplaceRandomBranch(random, symbolicExpressionTree, maxTreeLength, maxTreeDepth); SemanticallyEquivalentMutationParameter.ActualValue = new IntValue(NoSemantics); MutationTypeParameter.ActualValue = new IntValue(RandomMutation); return; } var allowedSymbols = new List <ISymbol>(); ISymbolicExpressionTreeNode parent; int childIndex; int maxLength; int maxDepth; // repeat until a fitting parent and child are found (MAX_TRIES times) int tries = 0; ISymbolicExpressionTreeNode child; do { #pragma warning disable 612, 618 parent = symbolicExpressionTree.Root.IterateNodesPrefix().Skip(1).Where(n => n.SubtreeCount > 0).SelectRandom(random); #pragma warning restore 612, 618 childIndex = random.Next(parent.SubtreeCount); child = parent.GetSubtree(childIndex); maxLength = maxTreeLength - symbolicExpressionTree.Length + child.GetLength(); maxDepth = maxTreeDepth - symbolicExpressionTree.Root.GetBranchLevel(child); allowedSymbols.Clear(); foreach (var symbol in parent.Grammar.GetAllowedChildSymbols(parent.Symbol, childIndex)) { // check basic properties that the new symbol must have if ((symbol.Name != child.Symbol.Name || symbol.MinimumArity > 0) && symbol.InitialFrequency > 0 && parent.Grammar.GetMinimumExpressionDepth(symbol) <= maxDepth && parent.Grammar.GetMinimumExpressionLength(symbol) <= maxLength) { allowedSymbols.Add(symbol); } } tries++; } while (tries < MAX_TRIES && allowedSymbols.Count == 0); NumberOfTriesParameter.ActualValue = new IntValue(tries); //normal tries. not semantic tries if (tries < MAX_TRIES) { var statementProductionNames = SemanticOperatorHelper.GetSemanticProductionNames(symbolicExpressionTree.Root.Grammar); var variables = problemData.Variables.GetVariableNames().ToList(); #region calculate original json output ISymbolicExpressionTreeNode statement = SemanticOperatorHelper.GetStatementNode(child, statementProductionNames); string variableSettings = problemData.VariableSettings.Count == 0 ? String.Empty : String.Join(Environment.NewLine, problemData.VariableSettings.Select(x => x.Value)); var statementPos0 = symbolicExpressionTree.IterateNodesPrefix().ToList().IndexOf(statement); if (String.IsNullOrEmpty(variableSettings)) { variableSettings = SemanticOperatorHelper.SemanticToPythonVariableSettings(semantics.First(x => x.TreeNodePrefixPos == statementPos0).Before, problemData.Variables.GetVariableTypes()); } var jsonOriginal = SemanticOperatorHelper.EvaluateStatementNode(statement, pythonProcess, random, problemData, variables, variableSettings, timeout); #endregion var weights = allowedSymbols.Select(s => s.InitialFrequency).ToList(); #pragma warning disable 612, 618 var seedSymbol = allowedSymbols.SelectRandom(weights, random); #pragma warning restore 612, 618 // replace the old node with the new node var seedNode = seedSymbol.CreateTreeNode(); if (seedNode.HasLocalParameters) { seedNode.ResetLocalParameters(random); } parent.RemoveSubtree(childIndex); parent.InsertSubtree(childIndex, seedNode); ProbabilisticTreeCreator.PTC2(random, seedNode, maxLength, maxDepth); JObject jsonReplaced; if (child == statement) { // child is executable, so is the new child jsonReplaced = SemanticOperatorHelper.EvaluateStatementNode(seedNode, pythonProcess, random, problemData, variables, variableSettings, timeout); } else { jsonReplaced = SemanticOperatorHelper.EvaluateStatementNode(statement, pythonProcess, random, problemData, variables, variableSettings, timeout); } if (JToken.EqualityComparer.Equals(jsonOriginal, jsonReplaced)) { SemanticallyEquivalentMutationParameter.ActualValue = new IntValue(Equvivalent); } else { SemanticallyEquivalentMutationParameter.ActualValue = new IntValue(Different); } MutationTypeParameter.ActualValue = new IntValue(RandomMutation); } else { SemanticallyEquivalentMutationParameter.ActualValue = new IntValue(NoMutation); MutationTypeParameter.ActualValue = new IntValue(NoMutation); } }
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 }