예제 #1
0
        /// <summary>
        /// Generate Tree structure for chromosome representation
        /// </summary>
        /// <param name="levels"></param>
        /// <returns></returns>
        public GPNode GenerateTreeStructure(int levels)
        {
            //Create the first node
            GPNode root = GPNode.NewNode();

            root.value = 0;

            //Collection holds tree nodes
            Queue <GPNode> dataTree = new Queue <GPNode>();
            GPNode         node     = null;


            //Add to list and ncrease index
            dataTree.Enqueue(root);

            while (dataTree.Count > 0)
            {
                node = dataTree.Dequeue();
                int level = node.value;

                //Generate Chromosome node value
                if (node.value < levels)
                {
                    node.value = Globals.GenerateNodeValue(true);
                }
                else
                {
                    node.value = Globals.GenerateNodeValue(false);
                };

                //Node children generatio
                if (node.value > 1999)
                {
                    int aritry = functionSet.GetFunctions()[node.value - 2000].Aritry;
                    //Create children of node
                    node.children = new GPNode[aritry];

                    for (int i = 0; i < aritry; i++)
                    {
                        GPNode child = GPNode.NewNode();
                        child.value = level + 1;

                        node.children[i] = child;
                        dataTree.Enqueue(node.children[i]);
                    }
                }
            }

            return(root);
        }
예제 #2
0
        /// <summary>
        /// Converts treeDrawer in to GPNode
        /// </summary>
        /// <returns> GPnode tree structure</returns>
        public GPNode TreeNodeToGPNode()
        {
            if (treeCtrlDrawer1.RootNode == null)
            {
                return(null);
            }
            //Create list of terminals
            FromDirToFuns();

            //Collection holds tree nodes
            Queue <GPNode>         dataTree = new Queue <GPNode>();
            Queue <TreeNodeDrawer> ctrls    = new Queue <TreeNodeDrawer>();

            //current node
            GPNode         root     = GPNode.NewNode();
            TreeNodeDrawer treeCtrl = null;

            ctrls.Enqueue(treeCtrlDrawer1.RootNode);
            dataTree.Enqueue(root);

            while (ctrls.Count > 0)
            {
                //get next node
                var node = dataTree.Dequeue();
                treeCtrl   = ctrls.Dequeue();
                node.value = GetNodeValue(treeCtrl.Content);

                if (treeCtrl.TreeChildren != null && treeCtrl.TreeChildren.Count > 0)
                {
                    node.children = new GPNode[treeCtrl.TreeChildren.Count];
                    for (int i = 0; i < treeCtrl.TreeChildren.Count; i++)
                    {
                        node.children[i] = GPNode.NewNode();
                        dataTree.Enqueue(node.children[i]);

                        ctrls.Enqueue((TreeNodeDrawer)treeCtrl.TreeChildren[i]);
                    }
                }
            }

            return(root);
        }
예제 #3
0
        /// <summary>
        /// Converts treeDrawer in to GPNode
        /// </summary>
        /// <returns> GPnode tree structure</returns>
        public GPNode ToGPNode(bool tag = false)
        {
            if (this.RootNode == null)
            {
                return(null);
            }

            //Collection holds tree nodes
            Queue <GPNode>         dataTree = new Queue <GPNode>();
            Queue <TreeNodeDrawer> ctrls    = new Queue <TreeNodeDrawer>();

            //current node
            GPNode         root     = GPNode.NewNode();
            TreeNodeDrawer treeCtrl = null;

            ctrls.Enqueue(this.RootNode);
            dataTree.Enqueue(root);

            while (ctrls.Count > 0)
            {
                //get next node
                var node = dataTree.Dequeue();
                treeCtrl    = ctrls.Dequeue();
                node.value  = (int)treeCtrl.Content;
                node.marked = tag;

                if (treeCtrl.TreeChildren != null && treeCtrl.TreeChildren.Count > 0)
                {
                    node.children = new GPNode[treeCtrl.TreeChildren.Count];
                    for (int i = 0; i < treeCtrl.TreeChildren.Count; i++)
                    {
                        node.children[i] = GPNode.NewNode();
                        dataTree.Enqueue(node.children[i]);

                        ctrls.Enqueue((TreeNodeDrawer)treeCtrl.TreeChildren[i]);
                    }
                }
            }

            return(root);
        }
예제 #4
0
        public void Crossover(GPNode ch1, GPNode ch2, int index1, int index2)
        {
            //We dont want to crossover root
            if (index1 == 1 || index2 == 1)
            {
                throw new Exception("Wrong index number for Crossover operation!");
            }

            //start counter from 0
            int count = 0;

            //Collection holds tree nodes
            Queue <GPNode> dataTree = new Queue <GPNode>();
            GPNode         node     = null;

            //Add tail recursion
            dataTree.Enqueue(ch1);

            while (dataTree.Count > 0)
            {
                //get next node
                node = dataTree.Dequeue();

                //count node
                count++;

                //when the counter is equel to index return curretn node
                if (count == index1)
                {
                    break;
                }

                if (node.children != null)
                {
                    for (int i = node.children.Length - 1; i >= 0; i--)
                    {
                        dataTree.Enqueue(node.children[i]);
                    }
                }
            }

            var node1 = node;


            //Add tail recursion
            count = 0;
            dataTree.Clear();
            dataTree.Enqueue(ch2);

            while (dataTree.Count > 0)
            {
                //get next node
                node = dataTree.Dequeue();

                //count node
                count++;

                //when the counter is equel to index return curretn node
                if (count == index2)
                {
                    break;
                }

                if (node.children != null)
                {
                    for (int i = node.children.Length - 1; i >= 0; i--)
                    {
                        dataTree.Enqueue(node.children[i]);
                    }
                }
            }


            //Exchange  nodes
            GPNode tempNode = GPNode.NewNode();

            tempNode.value = node1.value;
            if (node1.children != null)
            {
                tempNode.children = new GPNode[node1.children.Length];
                for (int i = 0; i < tempNode.children.Length; i++)
                {
                    tempNode.children[i] = node1.children[i];
                }
            }

            //
            node1.value    = node.value;
            node1.children = null;
            if (node.children != null)
            {
                node1.children = null;
                node1.children = new GPNode[node.children.Length];
                for (int i = 0; i < node.children.Length; i++)
                {
                    node1.children[i] = node.children[i];
                }
            }
            //
            node.value    = tempNode.value;
            node.children = tempNode.children;
        }