Exemplo n.º 1
0
 public void AddNode(basicNode node)
 {
     //this.variables.Add(node.Name);
     this.name2Node.Add(node.Name, node);
 }
Exemplo n.º 2
0
        private static Network CreateNetwork(string filePath)
        {
            string[] lines = File.ReadAllLines(filePath);
            int      n     = int.Parse(lines[0].Split(' ')[1]);

            lines = lines.Skip(1).ToArray();
            Network network = new Network();

            for (int i = 0; i < n; i++)
            {
                checkValitidy(lines);
                var    parts = lines[0].Split(' ');
                string type  = parts.Last();
                string name  = parts[1];
                parts = lines[1].Split(' ');
                List <string> parents = parts.Skip(1).ToList();

                basicNode node = null;
                if (type == "boolean")
                {
                    node        = new regularNode();
                    node.values = new List <object> {
                        "true", "false"
                    };
                }
                if (type == "multie")
                {
                    node        = new regularNode();
                    parts       = lines[2].Split(' ');
                    node.values = parts.Skip(1).Select(x => (object)x).ToList();
                }
                if (type == "noisyOR")
                {
                    node        = new noisyORNode();
                    node.values = new List <object> {
                        "true", "false"
                    };
                }

                node.Name         = name;
                node.ParentsNames = parents;
                network.AddNode(node);

                if (type == "noisyOR")
                {
                    var disturbution = lines[3].Split(' ').Skip(1).Select(x => double.Parse(x));
                    ((noisyORNode)node).AddProbabilities(disturbution.ToArray());
                    lines = lines.Skip(5).ToArray();
                }
                else
                {
                    int distLines = int.Parse(lines[3].Split(' ')[1]);
                    ((regularNode)node).CreateProbabiltyTable();
                    for (int j = 0; j < distLines; j++)
                    {
                        parts = lines[4 + j].Split(' ');
                        if (type == "boolean")
                        {
                            double p = double.Parse(parts.Last());
                            Dictionary <string, object> parentsVals = getParentsVals(parts.Take(parts.Length - 1));
                            ((regularNode)node).AddProbabiltyRow("true", p, parentsVals);
                            ((regularNode)node).AddProbabiltyRow("false", 1 - p, parentsVals);
                        }
                        if (type == "multie")
                        {
                            double p = double.Parse(parts.Last());
                            Dictionary <string, object> parentsVals = getParentsVals(parts.Take(parts.Length - 2));
                            ((regularNode)node).AddProbabiltyRow(parts[parts.Length - 2], p, parentsVals);
                        }
                    }

                    lines = lines.Skip(4 + distLines + 1).ToArray();
                }
            }
            return(network);
        }