/// <summary>
        /// Writes the indicated ADF to the file
        /// </summary>
        /// <param name="ADBranch">The ADF to write</param>
        public override bool WriteADF(GPProgramBranchADF adf)
        {
            //
            // Write the function declaration
            m_Writer.WriteLine("int countADF" + adf.WhichFunction + "=0;");
            m_Writer.WriteLine(BuildADFPrototype(adf, "GeneticProgram::"));
            m_Writer.WriteLine("\t{");

            m_Writer.WriteLine("\tif (countADF" + adf.WhichFunction + " >= 1) return 1.0;");
            m_Writer.WriteLine("\tcountADF" + adf.WhichFunction + "++;");
            m_Writer.WriteLine();

            m_Writer.Write("\tdouble Result=");

            WriteProgramBody(adf.Root);

            //
            // Close off the function
            m_Writer.WriteLine(";");
            m_Writer.WriteLine();
            m_Writer.WriteLine("\tcountADF" + adf.WhichFunction + "--;");
            m_Writer.WriteLine();
            m_Writer.WriteLine("return Result;");
            m_Writer.WriteLine("}");
            m_Writer.WriteLine();

            return(true);
        }
        /// <summary>
        /// Writes the indicated ADF to the file
        /// </summary>
        /// <param name="ADBranch">The ADF to write</param>
        public override bool WriteADF(GPProgramBranchADF adf)
        {
            //
            // Write the function declaration
            m_Writer.WriteLine("\tPrivate countADF" + adf.WhichFunction + " As Integer=0");
            m_Writer.Write("\tPrivate Function ADF" + adf.WhichFunction + "(");
            //
            // Write the function parameters
            WriteADFParameters(adf);
            m_Writer.WriteLine(") As Double");
            m_Writer.WriteLine();

            m_Writer.WriteLine("\t\tIf countADF" + adf.WhichFunction + " >= 1");
            m_Writer.WriteLine("\t\t\tReturn 1.0");
            m_Writer.WriteLine("\t\tEnd If");
            m_Writer.WriteLine("\t\tcountADF" + adf.WhichFunction + " = countADF" + adf.WhichFunction + " + 1");
            m_Writer.WriteLine();

            m_Writer.WriteLine("\t\tDim Result As Double");
            m_Writer.Write("\t\tResult= ");

            WriteProgramBody(adf.Root);

            //
            // Close off the function
            m_Writer.WriteLine();
            m_Writer.WriteLine();
            m_Writer.WriteLine("\t\tcountADF" + adf.WhichFunction + " = countADF" + adf.WhichFunction + " - 1");
            m_Writer.WriteLine();
            m_Writer.WriteLine("\t\treturn Result");
            m_Writer.WriteLine("\tEnd Function");
            m_Writer.WriteLine();

            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Writes the indicated ADF to the file
        /// </summary>
        /// <param name="ADBranch">The ADF to write</param>
        public override bool WriteADF(GPProgramBranchADF adf)
        {
            //
            // Write the function declaration
            m_Writer.WriteLine("int countADF" + adf.WhichFunction + "=0;");
            m_Writer.Write("double ADF" + adf.WhichFunction + "(");
            //
            // Write the function parameters
            WriteADFParameters(adf);
            m_Writer.WriteLine(")");
            m_Writer.WriteLine("\t{");

            m_Writer.WriteLine("\tif (countADF" + adf.WhichFunction + " >= 1) return 1.0;");
            m_Writer.WriteLine("\tcountADF" + adf.WhichFunction + "++;");
            m_Writer.WriteLine();

            m_Writer.Write("\tdouble Result=");

            WriteProgramBody(adf.Root);

            //
            // Close off the function
            m_Writer.WriteLine(";");
            m_Writer.WriteLine();
            m_Writer.WriteLine("\tcountADF" + adf.WhichFunction + "--;");
            m_Writer.WriteLine();
            m_Writer.WriteLine("return Result;");
            m_Writer.WriteLine("}");
            m_Writer.WriteLine();

            return(true);
        }
        /// <summary>
        /// Write an ADF prototype
        /// </summary>
        /// <param name="ADBranch"></param>
        /// <param name="ClassRoot"></param>
        /// <returns>String representation of the ADF prototype</returns>
        private String BuildADFPrototype(GPProgramBranchADF adf, String ClassRoot)
        {
            String Prototype = "double " + ClassRoot + "ADF" + adf.WhichFunction + "(";

            Prototype += BuildADFParameters(adf) + ")";

            return(Prototype);
        }
Esempio n. 5
0
        /// <summary>
        /// A Function node really acts as a reference to make a call into
        /// a Function program branch.  What this method does is to first crawl
        /// through each of the Function parameter subtrees and get their values
        /// computed up.  These values are stored internal to the function
        /// to act as a sort of stack, because each of the subtrees could
        /// make calls to the same Function and we don't want the results to get
        /// overwritten.  Once all the subtrees have been evalutated, the
        /// results are written into the Function parameters and the Function program
        /// branch is called.
        /// </summary>
        /// <param name="tree">Program tree this Function belongs to</param>
        /// <param name="execBranch">Program Branch this Function is executing within</param>
        public override double EvaluateAsDouble(GPProgram tree, GPProgramBranch execBranch)
        {
            GPProgramBranchADF adf = tree.ADF[this.WhichFunction];

            base.PrepareFunctionParameters(tree, execBranch, adf);

            //
            // Evalute the Function program branch
            return(adf.EvaluateAsDouble(tree));
        }
 /// <summary>
 /// Declare each of the ADF parameters
 /// </summary>
 /// <param name="ADBranch">Which ADF to work with</param>
 private void WriteADFParameters(GPProgramBranchADF adf)
 {
     for (short nParam = 0; nParam < adf.NumberArgs; nParam++)
     {
         if (nParam != 0)
         {
             m_Writer.Write(",");
         }
         m_Writer.Write("ByVal p" + Convert.ToString(nParam) + " As Double");
     }
 }
        /// <summary>
        /// Declare each of the ADF parameters
        /// </summary>
        /// <param name="ADBranch">Which ADF to work with</param>
        /// <returns>String representation of the ADF parameters</returns>
        private String BuildADFParameters(GPProgramBranchADF adf)
        {
            String Parameters = "";

            for (short nParam = 0; nParam < adf.NumberArgs; nParam++)
            {
                if (nParam != 0)
                {
                    Parameters += ",";
                }
                Parameters += "double p" + Convert.ToString(nParam);
            }

            return(Parameters);
        }
        public override bool WriteADF(GPProgramBranchADF adf)
        {
            m_xmlWriter.WriteStartElement("ADF");

            //
            // Record the number of parameters
            m_xmlWriter.WriteStartElement("ParameterCount");
            m_xmlWriter.WriteValue(adf.NumberArgs);
            m_xmlWriter.WriteEndElement();

            WriteProgramNode(adf.Root);

            m_xmlWriter.WriteEndElement();

            return(true);
        }
Esempio n. 9
0
        /// <summary>
        /// This method directs the construction of an ADL branch
        /// </summary>
        /// <param name="Branch"></param>
        /// <param name="TreeBuild"></param>
        /// <returns></returns>
        public override bool Build(GPProgramBranch Branch, GPEnums.TreeBuild TreeBuild)
        {
            m_Branch = m_BranchADF = (GPProgramBranchADF)Branch;
            //
            // Call the recursive method to create the tree
            m_Branch.Root = BuildInternal(TreeBuild, m_Branch.DepthInitial, false);

            //
            // Update the tree stats
            m_Branch.UpdateStats();

            //
            // Convert the program into array representation
            m_Branch.ConvertToArray(m_Config.FunctionSet);

            return(true);
        }
Esempio n. 10
0
        /// <summary>
        /// Writes the indicated ADF to the file
        /// </summary>
        /// <param name="adf">The ADF to write</param>
        public override bool WriteADF(GPProgramBranchADF adf)
        {
            WriteFunctionPrototype("ADF" + adf.WhichFunction, adf.NumberArgs, "p");
            //
            // Add the common blocks
            WriteFortranString("      REAL Memory(" + m_Program.CountMemory + ")");
            WriteFortranString("      COMMON /idxmemory/ Memory");
            WriteFortranString("      INTEGER CountADF" + adf.WhichFunction);
            WriteFortranString("      COMMON /ADF/ CountADF" + adf.WhichFunction);
            //
            // Declare the function types
            WriteFortranString("      REAL SetMem");
            WriteFortranString("      REAL GetMem");
            foreach (KeyValuePair <String, GPLanguageWriter.tagUserDefinedFunction> kvp in m_FunctionSet)
            {
                if (!IsFortranIntrinsic(kvp.Key))
                {
                    WriteFortranString("      REAL " + ((GPLanguageWriter.tagUserDefinedFunction)kvp.Value).Name);
                }
            }

            WriteFortranString("");
            WriteFortranString("      IF (CountADF" + adf.WhichFunction + " .GE. 1) THEN");
            WriteFortranString("          CountADF" + adf.WhichFunction + "=1");
            WriteFortranString("          RETURN");
            WriteFortranString("      ENDIF");
            WriteFortranString("");
            WriteFortranString("      CountADF" + adf.WhichFunction + "=CountADF" + adf.WhichFunction + "+1");
            WriteFortranString("");

            m_Writer.Write("      ADF" + adf.WhichFunction + "=");

            WriteProgramBody(adf.Root);

            //
            // Close off the function
            WriteFortranString("");
            WriteFortranString("");
            WriteFortranString("      CountADF" + adf.WhichFunction + "=CountADF" + adf.WhichFunction + "-1");
            WriteFortranString("");
            WriteFortranString("      RETURN");
            WriteFortranString("      END");
            WriteFortranString("");

            return(true);
        }
Esempio n. 11
0
        /// <summary>
        /// Cloneable interface
        /// </summary>
        /// <returns>Clone of the object</returns>
        public Object Clone()
        {
            //
            // Clone the RPB
            GPProgramBranchRPB rpb = (GPProgramBranchRPB)m_RPB.Clone();

            rpb.Parent = this;

            //
            // copy the main tree
            GPProgram tree = new GPProgram(rpb);

            m_Memory = new double[this.CountMemory];

            //
            // Handle the ADFs
            tree.m_listADF = new List <GPProgramBranchADF>();
            foreach (GPProgramBranchADF adf in m_listADF)
            {
                GPProgramBranchADF copy = (GPProgramBranchADF)adf.Clone();
                copy.Parent = this;
                tree.m_listADF.Add(copy);
            }

            //
            // Handle the ADLs
            tree.m_listADL = new List <GPProgramBranchADL>();
            foreach (GPProgramBranchADL adl in m_listADL)
            {
                GPProgramBranchADL copy = (GPProgramBranchADL)adl.Clone();
                copy.Parent = this;
                tree.m_listADL.Add(copy);
            }

            //
            // Handle the ADRs
            tree.m_listADR = new List <GPProgramBranchADR>();
            foreach (GPProgramBranchADR adr in m_listADR)
            {
                GPProgramBranchADR copy = (GPProgramBranchADR)adr.Clone();
                copy.Parent = this;
                tree.m_listADR.Add(copy);
            }

            return(tree);
        }
Esempio n. 12
0
        /// <summary>
        /// Create a brand spanking new baby program tree!
        /// </summary>
        /// <param name="Depth">Max depth the tree can be</param>
        /// <param name="treeBuild">Tree building technique</param>
        /// <returns>Newly constructed program</returns>
        public GPProgram Construct(int nDepth, GPEnums.TreeBuild treeBuild)
        {
            //
            // Create an empty program tree
            m_Program = new GPProgram(null);

            //
            // We start by creating the ADFs first, because we want them available
            // to the RPB when it is created.

            //
            // Create some ADF branches
            for (short ADF = 0; ADF < m_Config.ADFSet.Count; ADF++)
            {
                GPProgramBranchADF adfBranch = new GPProgramBranchADF(m_Program, nDepth, ADF, m_Config.ADFSet[ADF]);
                m_OperatorADF.Build(adfBranch, treeBuild);
                m_Program.ADF.Add(adfBranch);
            }

            //
            // Create some ADL branches
            for (short ADL = 0; ADL < m_Config.ADLSet.Count; ADL++)
            {
                GPProgramBranchADL adlBranch = new GPProgramBranchADL(m_Program, nDepth, ADL, m_Config.ADLSet[ADL]);
                m_OperatorADL.Build(adlBranch, treeBuild);
                m_Program.ADL.Add(adlBranch);
            }

            //
            // Create some ADR branches
            for (short ADR = 0; ADR < m_Config.ADRSet.Count; ADR++)
            {
                GPProgramBranchADR adrBranch = new GPProgramBranchADR(m_Program, nDepth, ADR, m_Config.ADRSet[ADR]);
                m_OperatorADR.Build(adrBranch, treeBuild);
                m_Program.ADR.Add(adrBranch);
            }

            //
            // Build the RPB branch
            m_Program.RPB = new GPProgramBranchRPB(m_Program, m_Program.ADF.Count, nDepth, treeBuild);
            m_OperatorRPB.Build(m_Program.RPB, treeBuild);

            return(m_Program);
        }
Esempio n. 13
0
        /// <summary>
        /// Parse an ADF branch
        /// </summary>
        /// <param name="adfNode"></param>
        /// <param name="WhichADF"></param>
        /// <param name="Program"></param>
        /// <returns></returns>
        private GPProgramBranchADF LoadBranchADF(XmlNode adfNode, short WhichADF, GPProgram Program)
        {
            //
            // First step, parse out the number of arguments
            XmlNode xmlNode        = adfNode.SelectSingleNode("ParameterCount");
            byte    ParameterCount = Convert.ToByte(xmlNode.InnerText);

            //
            // Create the Branch tree
            GPProgramBranchADF adf = new GPProgramBranchADF(Program, 0, WhichADF, ParameterCount);

            //
            // Get the root ADF node
            xmlNode  = adfNode.SelectSingleNode("GPNode");
            adf.Root = ReadGPNode(xmlNode);

            adf.UpdateStats();

            return(adf);
        }
Esempio n. 14
0
 public GPProgramBranchFactoryADF(GPProgramBranchADF ADF, GPModelerServer Config)
     : base(ADF, Config)
 {
     m_BranchADF = ADF;
 }
Esempio n. 15
0
        /// <summary>
        /// Build a program based upon the XML description that is provided.
        /// </summary>
        /// <returns></returns>
        public GPProgram Construct()
        {
            //
            // Create a program tree
            GPProgram Program = new GPProgram(null);

            //
            // Start off with the root node
            XmlElement xmlRoot = m_DocProgram.DocumentElement;

            //
            // Read the indexed memory size
            XmlNode xmlHeader = xmlRoot.SelectSingleNode("Header");

            if (xmlHeader != null)
            {
                XmlNode xmlMemory = xmlHeader.SelectSingleNode("MemoryCount");
                Program.CountMemory = Convert.ToInt16(xmlMemory.InnerText);
            }
            else
            {
                Program.CountMemory = 1;                        // Some stupid default
            }

            //
            // Load the ADRs
            List <GPProgramBranchADR> ADRList = new List <GPProgramBranchADR>();
            XmlNodeList xmlListADR            = xmlRoot.SelectNodes("ADR");
            short       WhichADR = 0;

            foreach (XmlNode adrNode in xmlListADR)
            {
                GPProgramBranchADR adrBranch = LoadBranchADR(adrNode, WhichADR, Program);
                ADRList.Add(adrBranch);

                WhichADR++;
            }

            //
            // Add this as the list of ADRs for the program
            Program.ADR = ADRList;

            //
            // Load the ADLs
            List <GPProgramBranchADL> ADLList = new List <GPProgramBranchADL>();
            XmlNodeList xmlListADL            = xmlRoot.SelectNodes("ADL");
            short       WhichADL = 0;

            foreach (XmlNode adlNode in xmlListADL)
            {
                GPProgramBranchADL adlBranch = LoadBranchADL(adlNode, WhichADL, Program);
                ADLList.Add(adlBranch);

                WhichADL++;
            }

            //
            // Add this as the list of ADLs for the program
            Program.ADL = ADLList;

            //
            // Load the ADFs
            List <GPProgramBranchADF> ADFList = new List <GPProgramBranchADF>();
            XmlNodeList xmlListADF            = xmlRoot.SelectNodes("ADF");
            short       WhichADF = 0;

            foreach (XmlNode adfNode in xmlListADF)
            {
                GPProgramBranchADF adfBranch = LoadBranchADF(adfNode, WhichADF, Program);
                ADFList.Add(adfBranch);

                WhichADF++;
            }

            //
            // Don't forget to assign this list of ADFs to the program tree
            Program.ADF = ADFList;

            //
            // Get the RPB node
            XmlNode xmlRPB = xmlRoot.SelectSingleNode("RPB");

            //
            // Create the RPB
            Program.RPB = LoadBranchRPB(xmlRPB, ADFList, Program);

            return(Program);
        }
Esempio n. 16
0
 public abstract bool WriteADF(GPProgramBranchADF adf);
Esempio n. 17
0
        /// <summary>
        /// This method performs crossover on the ADF branch.
        /// 90% of the time internal nodes are selected and 10% of the time
        /// leaf nodes are selected.
        /// TODO: Parameterize those percentages
        ///
        /// If either one of the selected nodes is a function that accepts only
        /// terminals as parameters, no crossover is performed.
        ///
        /// </summary>
        /// <param name="sibling"></param>
        public override void Crossover(GPProgramBranch sibling)
        {
            GPProgramBranchADF rightADF = (GPProgramBranchADF)sibling;

            //
            // Step 1: Find a node in the left tree
            double TypeLeft = GPUtilities.rngNextDouble();

            GPProgramBranchFactory.FindResult findLeft = null;
            bool DoneLeft = false;

            while (!DoneLeft)
            {
                int NodeLeft = GPUtilities.rngNextInt(m_Branch.CountNodes);
                findLeft = FindNode(null, m_Branch.Root, NodeLeft);
                if (TypeLeft < 0.90 && (findLeft.Node is GPNodeFunction))
                {
                    DoneLeft = true;
                }
                else if (TypeLeft >= 0.90 && (findLeft.Node is GPNodeTerminal))
                {
                    DoneLeft = true;
                }
                else if (m_Branch.CountNodes == 1)
                {
                    DoneLeft = true;
                }
            }

            //
            // If the node is a function that only accepts terminal inputs, then crossover is not allowed
            if (findLeft.Node is GPNodeFunction && ((GPNodeFunction)findLeft.Node).TerminalParameters)
            {
                return;
            }
            if (findLeft.Parent != null && findLeft.Parent is GPNodeFunction && ((GPNodeFunction)findLeft.Parent).TerminalParameters)
            {
                return;
            }

            //
            // Step 2: Find a node in the right tree
            double TypeRight = GPUtilities.rngNextDouble();

            GPProgramBranchFactory.FindResult findRight = null;
            bool DoneRight = false;

            while (!DoneRight)
            {
                int NodeRight = GPUtilities.rngNextInt(rightADF.CountNodes);
                findRight = FindNode(null, rightADF.Root, NodeRight);
                if (TypeRight < 0.90 && (findRight.Node is GPNodeFunction))
                {
                    DoneRight = true;
                }
                else if (TypeRight >= 0.90 && (findRight.Node is GPNodeTerminal))
                {
                    DoneRight = true;
                }
                else if (rightADF.CountNodes == 1)
                {
                    DoneRight = true;
                }
            }

            //
            // If the node is a function that only accepts terminal inputs, then crossover is not allowed
            if (findRight.Node is GPNodeFunction && ((GPNodeFunction)findRight.Node).TerminalParameters)
            {
                return;
            }
            if (findRight.Parent != null && findRight.Parent is GPNodeFunction && ((GPNodeFunction)findRight.Parent).TerminalParameters)
            {
                return;
            }

            //
            // Step 3: Swap the references
            if (findLeft.Parent == null)
            {
                m_Branch.Root = findRight.Node;
            }
            else
            {
                findLeft.Parent.Children[findLeft.ChildNumber] = findRight.Node;
            }

            if (findRight.Parent == null)
            {
                rightADF.Root = findLeft.Node;
            }
            else
            {
                findRight.Parent.Children[findRight.ChildNumber] = findLeft.Node;
            }

            //
            // Update the stats for these trees
            m_Branch.UpdateStats();
            rightADF.UpdateStats();
        }