/// <summary> /// Parse an ADL branch /// </summary> /// <param name="adlNode"></param> /// <param name="WhichADL"></param> /// <param name="Program"></param> /// <returns></returns> private GPProgramBranchADL LoadBranchADL(XmlNode adlNode, short WhichADL, GPProgram Program) { // // First step, parse out the number of arguments XmlNode xmlNode = adlNode.SelectSingleNode("ParameterCount"); byte ParameterCount = Convert.ToByte(xmlNode.InnerText); // // Create the Branch tree GPProgramBranchADL adl = new GPProgramBranchADL(Program, 0, WhichADL, ParameterCount); // // There are four branches to read, do each of them separately xmlNode = adlNode.SelectSingleNode("LIB"); adl.LIB = ReadGPNode(xmlNode.SelectSingleNode("GPNode")); xmlNode = adlNode.SelectSingleNode("LCB"); adl.LCB = ReadGPNode(xmlNode.SelectSingleNode("GPNode")); xmlNode = adlNode.SelectSingleNode("LBB"); adl.LBB = ReadGPNode(xmlNode.SelectSingleNode("GPNode")); xmlNode = adlNode.SelectSingleNode("LUB"); adl.LUB = ReadGPNode(xmlNode.SelectSingleNode("GPNode")); adl.UpdateStats(); return(adl); }
/// <summary> /// Writes the indicated ADL to the file /// </summary> /// <param name="adl">The ADL to write</param> /// <returns></returns> public override bool WriteADL(GPProgramBranchADL adl) { // // Write the function declaration m_Writer.Write("\tPrivate Function ADL" + adl.WhichFunction + "("); // // Write the function parameters WriteADLParameters(adl, true); m_Writer.WriteLine(") As Double"); m_Writer.WriteLine(); // // Call the LIB m_Writer.Write("\t\tLIB" + adl.WhichFunction + "("); WriteADLParameters(adl, false); m_Writer.WriteLine(")"); m_Writer.WriteLine(); // // Initialize the loop m_Writer.WriteLine("\t\tDim LoopIndex As Integer = 0"); m_Writer.WriteLine("\t\tDim Result As Double = 0.0"); // // Prepare the condition m_Writer.Write("\t\tWhile ((LoopIndex < 25) And (LCB" + adl.WhichFunction + "("); WriteADLParameters(adl, false); m_Writer.WriteLine(") > 0.0))"); // // Write the loop body m_Writer.WriteLine(); m_Writer.Write("\t\t\tResult = LBB" + adl.WhichFunction + "("); WriteADLParameters(adl, false); m_Writer.WriteLine(")"); m_Writer.Write("\t\t\tLUB" + adl.WhichFunction + "("); WriteADLParameters(adl, false); m_Writer.WriteLine(")"); m_Writer.WriteLine("\t\t\tLoopIndex = LoopIndex + 1"); m_Writer.WriteLine(); m_Writer.WriteLine("\t\tEnd While"); // // Close off the function m_Writer.WriteLine(); m_Writer.WriteLine("\t\treturn Result"); m_Writer.WriteLine("\tEnd Function"); m_Writer.WriteLine(); // // Write each of the four supporting methods for the loop WriteADLBranch(adl, "LIB", adl.LIB); WriteADLBranch(adl, "LCB", adl.LCB); WriteADLBranch(adl, "LBB", adl.LBB); WriteADLBranch(adl, "LUB", adl.LUB); return(true); }
/// <summary> /// Performs a crossover operation with this program and the sibling program. /// Crossover is performed on like branches, i.e. LIBs only crossover with sibling /// LIBs. /// </summary> /// <param name="sibling">Which sibling to crossover with</param> public override void Crossover(GPProgramBranch sibling) { GPProgramBranchADL rightADL = (GPProgramBranchADL)sibling; // // Select a branch byte WhichBranch = (byte)GPUtilities.rngNextInt(4); switch (WhichBranch) { case 0: Crossover(ref m_BranchADL.m_LIB, m_BranchADL.NodeCountLIB, ref rightADL.m_LIB, rightADL.NodeCountLIB); break; case 1: Crossover(ref m_BranchADL.m_LCB, m_BranchADL.NodeCountLCB, ref rightADL.m_LCB, rightADL.NodeCountLCB); break; case 2: Crossover(ref m_BranchADL.m_LBB, m_BranchADL.NodeCountLBB, ref rightADL.m_LBB, rightADL.NodeCountLBB); break; case 3: Crossover(ref m_BranchADL.m_LUB, m_BranchADL.NodeCountLUB, ref rightADL.m_LUB, rightADL.NodeCountLUB); break; } // // Make sure the program stats get updated for both programs m_Branch.UpdateStats(); sibling.UpdateStats(); }
public override bool WriteADL(GPProgramBranchADL adl) { m_xmlWriter.WriteStartElement("ADL"); // // Record the number of parameters m_xmlWriter.WriteStartElement("ParameterCount"); m_xmlWriter.WriteValue(adl.NumberArgs); m_xmlWriter.WriteEndElement(); // // Write each of the loop branches m_xmlWriter.WriteStartElement("LIB"); WriteProgramNode(adl.LIB); m_xmlWriter.WriteEndElement(); m_xmlWriter.WriteStartElement("LCB"); WriteProgramNode(adl.LCB); m_xmlWriter.WriteEndElement(); m_xmlWriter.WriteStartElement("LBB"); WriteProgramNode(adl.LBB); m_xmlWriter.WriteEndElement(); m_xmlWriter.WriteStartElement("LUB"); WriteProgramNode(adl.LUB); m_xmlWriter.WriteEndElement(); m_xmlWriter.WriteEndElement(); return(true); }
/// <summary> /// Writes the indicated ADL to the file /// </summary> /// <param name="adl">The ADL to write</param> /// <returns></returns> public override bool WriteADL(GPProgramBranchADL adl) { // // Write the function declaration m_Writer.Write("double ADL" + adl.WhichFunction + "("); // // Write the function parameters WriteADLParameters(adl, true); m_Writer.WriteLine(")"); m_Writer.WriteLine("{"); // // Call the LIB m_Writer.Write("\tLIB" + adl.WhichFunction + "("); WriteADLParameters(adl, false); m_Writer.WriteLine(");"); m_Writer.WriteLine(); // // Initialize the loop m_Writer.WriteLine("\tint LoopIndex = 0;"); m_Writer.WriteLine("\tdouble Result = 0.0;"); // // Prepare the condition m_Writer.Write("\twhile ((LoopIndex < 25) && (LCB" + adl.WhichFunction + "("); WriteADLParameters(adl, false); m_Writer.WriteLine(") > 0.0))"); // // Write the loop body m_Writer.WriteLine("\t{"); m_Writer.Write("\t\tResult = LBB" + adl.WhichFunction + "("); WriteADLParameters(adl, false); m_Writer.WriteLine(");"); m_Writer.Write("\t\tLUB" + adl.WhichFunction + "("); WriteADLParameters(adl, false); m_Writer.WriteLine(");"); m_Writer.WriteLine("\t\tLoopIndex++;"); m_Writer.WriteLine("\t}"); // // Close off the function m_Writer.WriteLine(); m_Writer.WriteLine("return Result;"); m_Writer.WriteLine("}"); m_Writer.WriteLine(); // // Write each of the four supporting methods for the loop WriteADLBranch(adl, "LIB", adl.LIB); WriteADLBranch(adl, "LCB", adl.LCB); WriteADLBranch(adl, "LBB", adl.LBB); WriteADLBranch(adl, "LUB", adl.LUB); return(true); }
/// <summary> /// Write an ADL prototype /// </summary> /// <param name="adl"></param> /// <param name="ADLBranch"></param> /// <param name="ClassRoot"></param> /// <returns>String representation of the ADL prototype</returns> private String BuildADLPrototype(GPProgramBranchADL adl, String ADLBranch, String ClassRoot) { String Prototype = "double " + ClassRoot + ADLBranch + adl.WhichFunction + "("; Prototype += BuildADLParameters(adl, true) + ")"; return(Prototype); }
/// <summary> /// Writes the indicated ADL to the file /// </summary> /// <param name="adl">The ADL to write</param> /// <returns></returns> public override bool WriteADL(GPProgramBranchADL adl) { WriteFunctionPrototype("ADL" + adl.WhichFunction, adl.NumberArgs, "p"); // // Delcare the loop branch functions WriteFortranString(" REAL LIB" + adl.WhichFunction); WriteFortranString(" REAL LCB" + adl.WhichFunction); WriteFortranString(" REAL LBB" + adl.WhichFunction); WriteFortranString(" REAL LUB" + adl.WhichFunction); // // Declare the loop variables WriteFortranString(" INTEGER LoopIndex"); WriteFortranString(" REAL Result"); WriteFortranString(" REAL Dummy"); WriteFortranString(""); // // Call the LIB WriteFortranString(" Dummy = LIB" + adl.WhichFunction + "(" + ConstructADParameters(adl) + ")"); WriteFortranString(""); // // Initialize the loop WriteFortranString(" LoopIndex = 0"); WriteFortranString(" Result = 0.0"); // // Prepare the condition WriteFortranString(" DO WHILE ((LoopIndex < 25) .AND. (LCB" + adl.WhichFunction + "(" + ConstructADParameters(adl) + ") .GT. 0.0))"); // // Write the loop body WriteFortranString(" Result = LBB" + adl.WhichFunction + "(" + ConstructADParameters(adl) + ")"); WriteFortranString(" Dummy = LUB" + adl.WhichFunction + "(" + ConstructADParameters(adl) + ")"); WriteFortranString(" LoopIndex=LoopIndex+1"); WriteFortranString(" ENDDO"); // // Close off the function WriteFortranString(""); WriteFortranString(" ADL" + adl.WhichFunction + "=Result"); WriteFortranString(""); WriteFortranString(" RETURN"); WriteFortranString(" END"); WriteFortranString(""); // // Write each of the four supporting methods for the loop WriteADLBranch(adl, "LIB", adl.LIB); WriteADLBranch(adl, "LCB", adl.LCB); WriteADLBranch(adl, "LBB", adl.LBB); WriteADLBranch(adl, "LUB", adl.LUB); return(true); }
/// <summary> /// An ADL node really acts as a reference to make a call into /// an ADL program branch. What this method does is to first crawl /// through each of the ADL 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 ADL and we don't want the results to get /// overwritten. Once all the subtrees have been evalutated, the /// results are written into the ADL parameters and the ADL program /// branch is called. /// </summary> /// <param name="tree">Program tree this ADL belongs to</param> /// <param name="execBranch">Program Branch this ADL is executing within</param> public override double EvaluateAsDouble(GPProgram tree, GPProgramBranch execBranch) { GPProgramBranchADL adl = tree.ADL[this.WhichFunction]; base.PrepareFunctionParameters(tree, execBranch, adl); // // Evalute the Function program branch return(adl.EvaluateAsDouble(tree)); }
/// <summary> /// Makes a clone of the ADL /// </summary> public new Object Clone() { GPProgramBranchADL obj = (GPProgramBranchADL)base.Clone(); // // Clone each of the branches obj.LIB = (GPNode)this.LIB.Clone(); obj.LCB = (GPNode)this.LCB.Clone(); obj.LBB = (GPNode)this.LBB.Clone(); obj.LUB = (GPNode)this.LUB.Clone(); return(obj); }
/// <summary> /// Declare each of the ADL parameters /// </summary> /// <param name="adl"></param> /// <param name="WriteType"></param> private void WriteADLParameters(GPProgramBranchADL adl, bool WriteType) { for (short nParam = 0; nParam < adl.NumberArgs; nParam++) { if (nParam != 0) { m_Writer.Write(","); } if (WriteType) { m_Writer.Write("double "); } m_Writer.Write("p" + Convert.ToString(nParam)); } }
/// <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); }
/// <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); }
/// <summary> /// Declare each of the ADL parameters /// </summary> /// <param name="adl"></param> /// <param name="WriteType"></param> /// <returns>String representation of the ADL parameters</returns> private String BuildADLParameters(GPProgramBranchADL adl, bool WriteType) { String Parameters = ""; for (short nParam = 0; nParam < adl.NumberArgs; nParam++) { if (nParam != 0) { Parameters += ","; } if (WriteType) { Parameters += "double "; } Parameters += "p" + Convert.ToString(nParam); } return(Parameters); }
/// <summary> /// Writes a function for the passed in loop branch /// </summary> /// <param name="adl">Parent ADL tree</param> /// <param name="ADLBranch">Root name for the function</param> /// <param name="BranchRoot">Root GPNode of the loop branch</param> private void WriteADLBranch(GPProgramBranchADL adl, String ADLBranch, GPNode BranchRoot) { // // Write the function declaration m_Writer.WriteLine(BuildADLPrototype(adl, ADLBranch, "GeneticProgram::")); m_Writer.WriteLine("{"); m_Writer.Write("\tdouble Result="); WriteProgramBody(BranchRoot); m_Writer.WriteLine(";"); // // Close the function m_Writer.WriteLine(); m_Writer.WriteLine("return Result;"); m_Writer.WriteLine("}"); m_Writer.WriteLine(); }
/// <summary> /// Construct the four branching structures of an ADL /// </summary> /// <param name="Branch">Reference to the ADL branch</param> /// <param name="TreeBuild">Tree building technqiue</param> /// <returns>True if the branch was correctly constructed</returns> public override bool Build(GPProgramBranch Branch, GPEnums.TreeBuild TreeBuild) { m_Branch = m_BranchADL = (GPProgramBranchADL)Branch; // // We create 4 different branching structures: LIB, LCB, LBB, LUB m_BranchADL.LIB = BuildInternal(TreeBuild, m_Branch.DepthInitial, false); m_BranchADL.LCB = BuildInternal(TreeBuild, m_Branch.DepthInitial, false); m_BranchADL.LBB = BuildInternal(TreeBuild, m_Branch.DepthInitial, false); m_BranchADL.LUB = 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); }
/// <summary> /// Writes a function for the passed in loop branch /// </summary> /// <param name="adl">Parent ADL tree</param> /// <param name="BranchName">Root name for the function</param> /// <param name="BranchRoot">Root GPNode of the loop branch</param> private void WriteADLBranch(GPProgramBranchADL adl, String BranchName, GPNode BranchRoot) { WriteFunctionPrototype(BranchName + adl.WhichFunction, adl.NumberArgs, "p"); // // Add the common blocks WriteFortranString(" REAL Memory(" + m_Program.CountMemory + ")"); WriteFortranString(" COMMON /idxmemory/ Memory"); // // 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); } } // // Declare any ADFs foreach (GPProgramBranchADF adf in m_Program.ADF) { WriteFortranString(" REAL ADF" + adf.WhichFunction); } m_Writer.Write(" " + BranchName + adl.WhichFunction + "="); WriteFortranString(""); WriteProgramBody(BranchRoot); WriteFortranString(""); WriteFortranString(""); // // Close the function WriteFortranString(""); WriteFortranString(" RETURN"); WriteFortranString(" END"); WriteFortranString(""); }
/// <summary> /// Writes a function for the passed in loop branch /// </summary> /// <param name="adl">Parent ADL tree</param> /// <param name="BranchName">Root name for the function</param> /// <param name="BranchRoot">Root GPNode of the loop branch</param> private void WriteADLBranch(GPProgramBranchADL adl, String BranchName, GPNode BranchRoot) { // // Write the function declaration m_Writer.Write("double " + BranchName + adl.WhichFunction + "("); // // Write the function parameters WriteADLParameters(adl, true); m_Writer.WriteLine(")"); m_Writer.WriteLine("{"); m_Writer.Write("\tdouble Result="); WriteProgramBody(BranchRoot); m_Writer.WriteLine(";"); // // Close the function m_Writer.WriteLine(); m_Writer.WriteLine("return Result;"); m_Writer.WriteLine("}"); m_Writer.WriteLine(); }
/// <summary> /// Writes a function for the passed in loop branch /// </summary> /// <param name="adl">Parent ADL tree</param> /// <param name="BranchName">Root name for the function</param> /// <param name="BranchRoot">Root GPNode of the loop branch</param> private void WriteADLBranch(GPProgramBranchADL adl, String BranchName, GPNode BranchRoot) { // // Write the function declaration m_Writer.Write("\tPrivate Function " + BranchName + adl.WhichFunction + "("); // // Write the function parameters WriteADLParameters(adl, true); m_Writer.WriteLine(") As Double"); m_Writer.WriteLine(); m_Writer.WriteLine("\t\tDim Result As Double"); m_Writer.Write("\t\tResult="); WriteProgramBody(BranchRoot); m_Writer.WriteLine(); // // Close the function m_Writer.WriteLine(); m_Writer.WriteLine("\t\tReturn Result"); m_Writer.WriteLine("\tEnd Function"); m_Writer.WriteLine(); }
public GPProgramBranchFactoryADL(GPProgramBranchADL ADL, GPModelerServer Config) : base(ADL, Config) { m_BranchADL = ADL; }
public abstract bool WriteADL(GPProgramBranchADL adl);
/// <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); }