/// <summary> /// Writes the specified RPB to the stream /// </summary> /// <param name="rpb">RPB tree to translate</param> /// <returns>True/False upon success or failure</returns> public override bool WriteRPB(GPProgramBranchRPB rpb) { // // Write the function declaration m_Writer.Write("double Compute("); // // Write the input parameters WriteProgramParameters(); m_Writer.WriteLine(")"); m_Writer.WriteLine("{"); // // Reset the memory cells m_Writer.WriteLine("\n\tint Cell;"); m_Writer.WriteLine("\n\tfor (Cell=0; Cell<" + m_Program.CountMemory + "; Cell++)"); m_Writer.WriteLine("\t\tm_Memory[Cell]=0;"); m_Writer.WriteLine(); m_Writer.Write("return "); WriteProgramBody(rpb.Root); // // Close off the function m_Writer.WriteLine(";"); m_Writer.WriteLine("}"); m_Writer.WriteLine(); return(true); }
/// <summary> /// Writes the specified RPB to the stream /// </summary> /// <param name="rpb">RPB tree to translate</param> /// <returns>True/False upon success or failure</returns> public override bool WriteRPB(GPProgramBranchRPB rpb) { WriteFunctionPrototype("Compute", m_Program.InputDimension, "t"); 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); } // // Declare any ADLs foreach (GPProgramBranchADL adl in m_Program.ADL) { WriteFortranString(" REAL ADL" + adl.WhichFunction); } WriteFortranString(" INTEGER Cell"); WriteFortranString(""); // // Reset the memory cells WriteFortranString(" DO 10 Cell=1," + m_Program.CountMemory); WriteFortranString(" Memory(Cell)=0"); WriteFortranString("10 CONTINUE"); WriteFortranString(""); m_Writer.Write(" Compute= "); WriteProgramBody(rpb.Root); // // Close off the function WriteFortranString(""); WriteFortranString(""); WriteFortranString(" RETURN"); WriteFortranString(" END"); WriteFortranString(""); return(true); }
/// <summary> /// Writes the specified RPB to the stream /// </summary> /// <param name="rpb">RPB tree to translate</param> /// <returns>True/False upon success or failure</returns> public override bool WriteRPB(GPProgramBranchRPB rpb) { // // Write the function declaration m_Writer.Write("\tpublic double Compute("); // // Write the input parameters WriteProgramParameters(); m_Writer.WriteLine(")"); m_Writer.WriteLine("\t{"); // // Time series programs initialize data before the computation if (m_TimeSeries) { WriteTimeSeriesInit(m_Program.InputDimension); } // // Reset the memory cells m_Writer.WriteLine("\n\t\tfor (int Cell=0; Cell<" + m_Program.CountMemory + "; Cell++)"); m_Writer.WriteLine("\t\t\tm_Memory[Cell]=0;"); m_Writer.WriteLine(); // // Compute the result m_Writer.Write("\t\tdouble Result="); WriteProgramBody(rpb.Root); // // Close off the result computation m_Writer.WriteLine(";"); m_Writer.WriteLine(); // // Update the input history if (!m_TimeSeries) { WriteInputHistoryUpdate(m_Program.InputDimension); } // // Close off the function and get the result returned m_Writer.WriteLine(); m_Writer.WriteLine("\treturn Result;"); m_Writer.WriteLine("\t}"); m_Writer.WriteLine(); return(true); }
/// <summary> /// Writes the specified RPB to the stream /// </summary> /// <param name="rpb">RPB tree to translate</param> /// <returns>True/False upon success or failure</returns> public override bool WriteRPB(GPProgramBranchRPB rpb) { // // Write the function declaration m_Writer.Write("\tPublic Function Compute("); // // Write the input parameters WriteProgramParameters(); m_Writer.WriteLine(") As Double"); m_Writer.WriteLine(); // // Time series programs initialize data before the computation if (m_TimeSeries) { WriteTimeSeriesInit(m_Program.InputDimension); } // // Reset the memory cells m_Writer.WriteLine("\t\tDim Cell As Integer"); m_Writer.WriteLine("\t\tFor Cell = 0 To " + m_Program.CountMemory); m_Writer.WriteLine("\t\t\tm_Memory(Cell) = 0"); m_Writer.WriteLine("\t\tNext"); m_Writer.WriteLine(); m_Writer.WriteLine("\t\tDim Result As Double"); m_Writer.Write("\t\tResult = "); WriteProgramBody(rpb.Root); m_Writer.WriteLine(); m_Writer.WriteLine(); // // Update the input history if (!m_TimeSeries) { WriteInputHistoryUpdate(m_Program.InputDimension); } // // Close off the function m_Writer.WriteLine(); m_Writer.WriteLine("\tReturn Result"); m_Writer.WriteLine("\tEnd Function"); m_Writer.WriteLine(); return(true); }
/// <summary> /// This constructor is only used when a clone() of the object /// is being made. This keeps a new program from being generated. /// </summary> /// <param name="root"></param> /// <param name="InputDimension"></param> public GPProgram(GPProgramBranchRPB root) { m_RPB = root; // // Allocate the ADF List structure m_listADF = new List <GPProgramBranchADF>(); // // Allocate the ADL List structure m_listADL = new List <GPProgramBranchADL>(); // // Allocate the ADR List structure m_listADR = new List <GPProgramBranchADR>(); }
public override bool WriteRPB(GPProgramBranchRPB rpb) { m_xmlWriter.WriteStartElement("RPB"); // // Record the number of parameters m_xmlWriter.WriteStartElement("ParameterCount"); m_xmlWriter.WriteValue(m_Program.InputDimension); m_xmlWriter.WriteEndElement(); WriteProgramNode(rpb.Root); m_xmlWriter.WriteEndElement(); return(true); }
/// <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> /// Parse the RPB branch /// </summary> /// <param name="rpbNode"></param> /// <param name="ADFList"></param> /// <param name="Program"></param> /// <returns></returns> private GPProgramBranchRPB LoadBranchRPB(XmlNode rpbNode, List <GPProgramBranchADF> ADFList, GPProgram Program) { // // Start out with the input dimension XmlNode xmlNode = rpbNode.SelectSingleNode("ParameterCount"); Program.InputDimension = Convert.ToInt16(xmlNode.InnerText); // // Create the Branch tree GPProgramBranchRPB rpb = new GPProgramBranchRPB(Program, ADFList.Count, 0, GPEnums.TreeBuild.Undef); // // Get the root of the branch xmlNode = rpbNode.SelectSingleNode("GPNode"); rpb.Root = ReadGPNode(xmlNode); rpb.UpdateStats(); return(rpb); }
public GPProgramBranchFactoryRPB(GPProgramBranchRPB RPB, GPModelerServer Config) : base(RPB, Config) { }
/// <summary> /// This method performs crossover on the result producing 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) { GPProgramBranchRPB rightRPB = (GPProgramBranchRPB)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 (TypeLeft >= 0.90 && (findLeft.Node is GPNodeFunction) && ((GPNodeFunction)findLeft.Node).Children.Count == 0) { // // This if statement accounts for the possibility of functions that // are constant values...because I didn't use a terminal type for them 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(rightRPB.CountNodes); findRight = FindNode(null, rightRPB.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 (TypeRight >= 0.90 && (findRight.Node is GPNodeFunction) && ((GPNodeFunction)findRight.Node).Children.Count == 0) { // // This if statement accounts for the possibility of functions that // are constant values...because I didn't use a terminal type for them DoneRight = true; } else if (rightRPB.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) { rightRPB.Root = findLeft.Node; } else { findRight.Parent.Children[findRight.ChildNumber] = findLeft.Node; } // // Update the stats for these trees m_Branch.UpdateStats(); rightRPB.UpdateStats(); }
public abstract bool WriteRPB(GPProgramBranchRPB rpb);