Esempio n. 1
0
        /** Used by the above function */
        public IList GatherNodeString(IEvolutionState state, int threadnum, GPNode node, int index)
        {
            ArrayList list = new ArrayList();

            if (node is ERC)
            {
                // Now, get the "key" from the "node", NOTE: the "node" is inside an ArrayList,
                // since the ERCBank is mapped as key --> ArrayList of GPNodes.
                // The "key" is the corresponding int value for the ERC.
                list.Add(node.Name.Trim()); // add "ERC"
                                            // then add the ERC key (original genome value)
                list.Add(GetKeyFromNode(state, threadnum, node, index).Trim());
            }
            else
            {
                list.Add(node.ToString().Trim());
            }
            if (node.Children.Length > 0)
            {
                for (int i = 0; i < node.Children.Length; i++)
                {
                    index++;
                    IList sublist = GatherNodeString(state, threadnum, node.Children[i], index);
                    list.AddRange(sublist);
                }
            }
            return(list);
        }
Esempio n. 2
0
        static void EvalNode(GPNode node)
        {
            switch (node.Name)
            {
            case "IF":
                code += "if(";
                EvalNode(node.Children[0]);
                code += "){\n";
                EvalNode(node.Children[1]);
                code += "\n}\nelse\n{\n";
                EvalNode(node.Children[2]);
                code += "\n}";
                break;

            case "AND":
                code += "((";
                EvalNode(node.Children[0]);
                code += ") && (";
                EvalNode(node.Children[1]);
                code += "))";
                break;

            case "OR":
                code += "((";
                EvalNode(node.Children[0]);
                code += ") || (";
                EvalNode(node.Children[1]);
                code += "))";
                break;

            case "NOT":
                code += "!(";
                EvalNode(node.Children[0]);
                code += ")";
                break;

            case "<":
                EvalNode(node.Children[0]);
                code += "<";
                EvalNode(node.Children[1]);
                break;

            case ">":
                EvalNode(node.Children[0]);
                code += ">";
                EvalNode(node.Children[1]);
                break;

            case "=":
                EvalNode(node.Children[0]);
                code += "==";
                EvalNode(node.Children[1]);
                break;

            case "ERC":
                if (node.ToString() == "ERC(label)")
                {
                    code += ("return \"" + node.ToStringForHumans() + "\";");
                }
                else
                {
                    code += node.ToStringForHumans();
                }
                break;

            default:
                throw new Exception("Nie zdefiniowano wezla");
            }
        }
        public void TestGPNode()
        {
            //pai: PAI(FILHO,NUMBER)
            GPSemantica semanticaPai  = new GPSemanticaFormula("PAI", 2, 2);
            GPSemantica semanticaPai2 = new GPSemanticaFormula("PAI2", 2, 3);

            Assert.IsFalse(semanticaPai.IsTerminal);

            //filho: TERMINAL

            GPSemantica semanticaNumber = new GPSemanticaNumber("Numero de 0 a 10", 0, 10000);

            GPSemantica semanticaFilho = new GPSemanticaFormula("FILHO", 0, 0);
            //neto: NETO(NUMBER)
            GPSemantica semanticaNeto = new GPSemanticaFormula("NETO", 1, 2);

            // GPSemantica semanticaNumber2 = new GPSemantica("NETO");
            Assert.IsTrue(semanticaFilho.IsTerminal);

            GPAbstractNode nodePai     = new GPNode(semanticaPai);
            GPAbstractNode nodePai2    = new GPNode(semanticaPai2);
            GPAbstractNode nodeFilho   = new GPNode(semanticaFilho);
            GPAbstractNode nodeNeto    = new GPNode(semanticaNeto);
            GPAbstractNode nodeNumber  = new GPNodeNumber(semanticaNumber, 10);
            GPAbstractNode nodeNumber2 = new GPNodeNumber(semanticaNumber, 1050.123f);

            Assert.IsTrue(nodePai.CanAddNode(nodeFilho));
            Assert.IsTrue(nodePai.CanAddNode(nodeNumber));
            nodePai.AddNode(nodeFilho);
            Assert.IsFalse(nodePai.CanAddNode(nodeFilho));//já foi adicionado
            Assert.IsTrue(nodePai.CanAddNode(nodeNumber));
            nodePai.AddNode(nodeNumber);
            Assert.IsFalse(nodePai.CanAddNode(nodeFilho));
            Assert.IsFalse(nodePai.CanAddNode(nodeNumber));

            Assert.IsFalse(nodeNumber.CanAddNode(nodeFilho));
            Assert.IsFalse(nodeNumber.CanAddNode(nodeNumber));
            Assert.IsFalse(nodeFilho.CanAddNode(nodeNumber));
            Assert.IsFalse(nodeFilho.CanAddNode(nodeFilho));
            Assert.IsFalse(nodeFilho.CanAddNode(nodeNeto));

            Assert.IsTrue(nodePai.ToString() == "PAI(FILHO,10)", nodePai.ToString() + "<>" + "PAI(FILHO,10)");

            Assert.IsFalse(nodeNeto.CanAddNode(nodeFilho));

            Assert.IsTrue(nodePai.ContainsNode(nodeFilho));
            Assert.IsFalse(nodePai.ContainsNode(nodeNeto));

            nodePai2.AddNode(nodeNeto);
            Assert.IsTrue(nodePai2.ContainsNode(nodeNeto));
            Assert.IsTrue(nodeNeto.nodePai == nodePai2);

            Assert.IsTrue(nodePai.TransferNode(nodeFilho, nodeNeto));
            Assert.IsFalse(nodePai.ContainsNode(nodeFilho));
            Assert.IsTrue(nodePai.ContainsNode(nodeNeto));
            Assert.IsTrue(nodePai2.ContainsNode(nodeFilho));

            Assert.IsTrue(nodeNeto.nodePai == nodePai, nodeNeto.nodePai + "<>" + nodePai);
            Assert.IsTrue(nodeFilho.nodePai == nodePai2, nodeFilho.nodePai.ToString());
            nodeNeto.AddNode(nodeNumber2);

            Assert.IsTrue(nodePai.ToString() == "PAI(NETO(1050.123),10)", nodePai.ToString() + "<>" + "PAI(NETO(1050.123),10)");
            Assert.IsTrue(nodePai.Size() == 4, nodePai.Size() + "<>" + 4);
            Assert.IsTrue(nodeNumber.Size() == 1, nodeNumber.Size() + "<>" + 1);

            Assert.IsTrue(nodePai.SizeLevel() == 3, nodePai.SizeLevel() + "<>" + 3);
        }