//Establish the connections a node has to other nodes private void SetHierarchy(Node node) { //Add actual nodes for parents and children from node hash to replace placeholders for(int i = 0; i < node.Parents.Count; i++) { //Console.WriteLine("Adding parent: " + node.Parents[i].Name + " to: " + node.Name); node.Parents[i] = (Node)nodeHash[node.Parents[i].Name]; nodeList[node.Id].Parents[i] = (Node)nodeHash[node.Parents[i].Name]; } for (int i = 0; i < node.Children.Count; i++) { //Console.WriteLine("Adding child: " + node.Children[i].Name + " to: " + node.Name); node.Children[i] = (Node)nodeHash[node.Children[i].Name]; nodeList[node.Id].Children[i] = (Node)nodeHash[node.Children[i].Name]; } }
//Get properties of node private void CreateNode(XElement node) { //Name of node XElement name = node.Element("Name"); //Each row of a nodes CPT List<XElement> CPT_rows = node.Elements("CPT").Elements("Row").ToList(); //Each row should have the same amount of probabilities int probabilitiesPerRow = CPT_rows[0].Elements("Probability").ToList().Count; //Create CPT with matching rows and columns to the XML file double[,] tempCPT = new double[CPT_rows.Count, probabilitiesPerRow]; //Registering probabilities of each CPT row for(int i = 0; i < CPT_rows.Count; i++) { List<XElement> probabilities = CPT_rows[i].Elements("Probability").ToList(); for (int j = 0; j < probabilities.Count; j++) { //Fill CPT row with probabilities, change column for each new row number tempCPT[i, j] = Convert.ToDouble(probabilities[j].Value); } } double probability; //Check CPT rows normalize by rule of Σ(P)=1.0 for (int i = 0; i < tempCPT.GetLength(0); i++) { probability = 0; for (int j = 0; j < tempCPT.GetLength(1); j++) { Console.WriteLine("CPT at [" + i + ", " + j + "] : " + tempCPT[i, j]); probability += tempCPT[i,j]; } if (probability != 1) Console.WriteLine("WARNING! Probabilities don't normalize!"+probability); } Node newNode = new Node(name.Value.ToLower(), tempCPT); //Add the node to the hash if it doesnt already exist, if it does just update the existing instantiation if(!nodeHash.ContainsKey(newNode.Name)) { nodeHash.Add(newNode.Name.ToLower(), newNode); } else { nodeHash[newNode.Name] = newNode; } //Get parents IEnumerable<XElement> parents = node.Element("Parents").Elements("Parent"); foreach (XElement parent in parents) { newNode.Parents.Add(new Node(parent.Value.ToLower())); } //Get children IEnumerable<XElement> children = node.Element("Children").Elements("Child"); foreach (XElement child in children) { newNode.Children.Add(new Node(child.Value.ToLower())); } newNode.Id = NodeHash.Count-1; nodeList.Add(newNode); }