示例#1
0
        public void ReplaceRandomBranch(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, int maxTreeLength, int maxTreeDepth)
        {
            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;

            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);
                var 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);

            if (tries < MAX_TRIES)
            {
                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);
                }

                CutPointSymbolParameter.ActualValue = (ISymbol)parent.Symbol.Clone();
                RemovedBranchParameter.ActualValue  = new SymbolicExpressionTree((ISymbolicExpressionTreeNode)parent.GetSubtree(childIndex).Clone());

                parent.RemoveSubtree(childIndex);
                parent.InsertSubtree(childIndex, seedNode);
                ProbabilisticTreeCreator.PTC2(random, seedNode, maxLength, maxDepth);

                AddedBranchParameter.ActualValue = new SymbolicExpressionTree((ISymbolicExpressionTreeNode)parent.GetSubtree(childIndex).Clone());
            }
        }
示例#2
0
        public SymbolicExpressionTreeEncoding(string name, ISymbolicExpressionGrammar grammar, int maximumLength, int maximumDepth)
            : base(name)
        {
            treeLengthParameter          = new FixedValueParameter <IntValue>(Name + ".Maximum Tree Length", "Maximal length of the symbolic expression.", new IntValue(maximumLength));
            treeDepthParameter           = new FixedValueParameter <IntValue>(Name + ".Maximum Tree Depth", "Maximal depth of the symbolic expression. The minimum depth needed for the algorithm is 3 because two levels are reserved for the ProgramRoot and the Start symbol.", new IntValue(maximumDepth));
            grammarParameter             = new ValueParameter <ISymbolicExpressionGrammar>(Name + ".Grammar", "The grammar that should be used for symbolic expression tree.", grammar);
            functionDefinitionsParameter = new FixedValueParameter <IntValue>(Name + ".Function Definitions", "Maximal number of automatically defined functions", new IntValue(0));
            functionArgumentsParameter   = new FixedValueParameter <IntValue>(Name + ".Function Arguments", "Maximal number of arguments of automatically defined functions.", new IntValue(0));

            Parameters.Add(treeLengthParameter);
            Parameters.Add(treeDepthParameter);
            Parameters.Add(grammarParameter);
            Parameters.Add(functionDefinitionsParameter);
            Parameters.Add(functionArgumentsParameter);

            SolutionCreator = new ProbabilisticTreeCreator();
            RegisterParameterEvents();
            DiscoverOperators();
        }
        private static void DeletionByRandomRegeneration(IRandom random, ISymbolicExpressionTree symbolicExpressionTree, DefunTreeNode selectedDefunBranch)
        {
            // find first invocation and replace it with a randomly generated tree
            // can't find all invocations in one step because once we replaced a top level invocation
            // the invocations below it are removed already
            var invocationCutPoint = (from node in symbolicExpressionTree.IterateNodesPrefix()
                                      from subtree in node.Subtrees.OfType <InvokeFunctionTreeNode>()
                                      where subtree.Symbol.FunctionName == selectedDefunBranch.FunctionName
                                      select new CutPoint(node, subtree)).FirstOrDefault();

            while (invocationCutPoint != null)
            {
                // deletion by random regeneration
                ISymbolicExpressionTreeNode replacementTree = null;
                var allowedSymbolsList = invocationCutPoint.Parent.Grammar.GetAllowedChildSymbols(invocationCutPoint.Parent.Symbol, invocationCutPoint.ChildIndex).ToList();
                var weights            = allowedSymbolsList.Select(s => s.InitialFrequency);

#pragma warning disable 612, 618
                var selectedSymbol = allowedSymbolsList.SelectRandom(weights, random);
#pragma warning restore 612, 618


                int minPossibleLength = invocationCutPoint.Parent.Grammar.GetMinimumExpressionLength(selectedSymbol);
                int maxLength         = Math.Max(minPossibleLength, invocationCutPoint.Child.GetLength());
                int minPossibleDepth  = invocationCutPoint.Parent.Grammar.GetMinimumExpressionDepth(selectedSymbol);
                int maxDepth          = Math.Max(minPossibleDepth, invocationCutPoint.Child.GetDepth());
                replacementTree = selectedSymbol.CreateTreeNode();
                if (replacementTree.HasLocalParameters)
                {
                    replacementTree.ResetLocalParameters(random);
                }
                invocationCutPoint.Parent.RemoveSubtree(invocationCutPoint.ChildIndex);
                invocationCutPoint.Parent.InsertSubtree(invocationCutPoint.ChildIndex, replacementTree);

                ProbabilisticTreeCreator.PTC2(random, replacementTree, maxLength, maxDepth);

                invocationCutPoint = (from node in symbolicExpressionTree.IterateNodesPrefix()
                                      from subtree in node.Subtrees.OfType <InvokeFunctionTreeNode>()
                                      where subtree.Symbol.FunctionName == selectedDefunBranch.FunctionName
                                      select new CutPoint(node, subtree)).FirstOrDefault();
            }
        }
 protected ProbabilisticTreeCreator(ProbabilisticTreeCreator original, Cloner cloner) : base(original, cloner) { }
 protected ProbabilisticTreeCreator(ProbabilisticTreeCreator original, Cloner cloner) : base(original, cloner)
 {
 }