/// <summary> /// Performs a crossover operation with this program and the sibling program. /// Crossover is performed on like branches, i.e. RCBs only crossover with sibling /// RCBs. /// </summary> /// <param name="sibling">Which sibling to crossover with</param> public override void Crossover(GPProgramBranch sibling) { GPProgramBranchADR rightADR = (GPProgramBranchADR)sibling; // // Select a branch byte WhichBranch = (byte)GPUtilities.rngNextInt(4); switch (WhichBranch) { case 0: Crossover(ref m_BranchADR.m_RCB, m_BranchADR.NodeCountRCB, ref rightADR.m_RCB, rightADR.NodeCountRCB); break; case 1: Crossover(ref m_BranchADR.m_RBB, m_BranchADR.NodeCountRBB, ref rightADR.m_RBB, rightADR.NodeCountRBB); break; case 2: Crossover(ref m_BranchADR.m_RUB, m_BranchADR.NodeCountRUB, ref rightADR.m_RUB, rightADR.NodeCountRUB); break; case 3: Crossover(ref m_BranchADR.m_RGB, m_BranchADR.NodeCountRGB, ref rightADR.m_RGB, rightADR.NodeCountRGB); break; } // // Make sure the program stats get updated for both programs m_Branch.UpdateStats(); sibling.UpdateStats(); }
/// <summary> /// Parse an ADR branch /// </summary> /// <param name="adrNode"></param> /// <param name="WhichADR"></param> /// <param name="Program"></param> /// <returns>ADR program branch</returns> private GPProgramBranchADR LoadBranchADR(XmlNode adrNode, short WhichADR, GPProgram Program) { // // First step, parse out the number of arguments XmlNode xmlNode = adrNode.SelectSingleNode("ParameterCount"); byte ParameterCount = Convert.ToByte(xmlNode.InnerText); // // Create the Branch tree GPProgramBranchADR adr = new GPProgramBranchADR(Program, 0, WhichADR, ParameterCount); // // There are four branches to read, do each of them separately xmlNode = adrNode.SelectSingleNode("RCB"); adr.RCB = ReadGPNode(xmlNode.SelectSingleNode("GPNode")); xmlNode = adrNode.SelectSingleNode("RBB"); adr.RBB = ReadGPNode(xmlNode.SelectSingleNode("GPNode")); xmlNode = adrNode.SelectSingleNode("RUB"); adr.RUB = ReadGPNode(xmlNode.SelectSingleNode("GPNode")); xmlNode = adrNode.SelectSingleNode("RGB"); adr.RGB = ReadGPNode(xmlNode.SelectSingleNode("GPNode")); adr.UpdateStats(); return(adr); }
public override bool WriteADR(GPProgramBranchADR adr) { m_xmlWriter.WriteStartElement("ADR"); // // Record the number of parameters m_xmlWriter.WriteStartElement("ParameterCount"); m_xmlWriter.WriteValue(adr.NumberArgs); m_xmlWriter.WriteEndElement(); // // Write each of the loop branches m_xmlWriter.WriteStartElement("RCB"); WriteProgramNode(adr.RCB); m_xmlWriter.WriteEndElement(); m_xmlWriter.WriteStartElement("RBB"); WriteProgramNode(adr.RBB); m_xmlWriter.WriteEndElement(); m_xmlWriter.WriteStartElement("RUB"); WriteProgramNode(adr.RUB); m_xmlWriter.WriteEndElement(); m_xmlWriter.WriteStartElement("RGB"); WriteProgramNode(adr.RGB); m_xmlWriter.WriteEndElement(); m_xmlWriter.WriteEndElement(); return(true); }
/// <summary> /// An ADR node really acts as a reference to make a call into /// an ADR program branch. What this method does is to first crawl /// through each of the ADR 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 ADR and we don't want the results to get /// overwritten. Once all the subtrees have been evalutated, the /// results are written into the ADR parameters and the ADR program /// branch is called. /// </summary> /// <param name="tree">Program tree this ADR belongs to</param> /// <param name="execBranch">Program Branch this ADR is executing within</param> public override double EvaluateAsDouble(GPProgram tree, GPProgramBranch execBranch) { GPProgramBranchADR adr = tree.ADR[this.WhichFunction]; base.PrepareFunctionParameters(tree, execBranch, adr); // // Evalute the Function program branch return(adr.EvaluateAsDouble(tree)); }
/// <summary> /// Makes a clone of the ADR /// </summary> public new Object Clone() { GPProgramBranchADR obj = (GPProgramBranchADR)base.Clone(); // // Clone each of the branches obj.RCB = (GPNode)this.RCB.Clone(); obj.RBB = (GPNode)this.RBB.Clone(); obj.RUB = (GPNode)this.RUB.Clone(); obj.RGB = (GPNode)this.RGB.Clone(); return(obj); }
/// <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> /// Construct the four branching structures /// </summary> /// <param name="Branch">Reference to the ADR 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_BranchADR = (GPProgramBranchADR)Branch; // // We create 4 different branching structures: RCB, RBB, RUB, RGB m_BranchADR.RCB = BuildInternal(TreeBuild, m_Branch.DepthInitial, false); m_BranchADR.RBB = BuildInternal(TreeBuild, m_Branch.DepthInitial, true); m_BranchADR.RUB = BuildInternal(TreeBuild, m_Branch.DepthInitial, false); m_BranchADR.RGB = 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> /// TODO: Not implemented, because I haven't yet got the ADR code working /// </summary> /// <param name="adr"></param> /// <returns></returns> public override bool WriteADR(GPProgramBranchADR adr) { return(true); }
public abstract bool WriteADR(GPProgramBranchADR adr);
/// <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); }
public GPProgramBranchFactoryADR(GPProgramBranchADR ADR, GPModelerServer Config) : base(ADR, Config) { m_BranchADR = ADR; }