예제 #1
0
        private void CreateNode_Multistate(XmlNode theXmlNode)
        {
            XmlAttributeCollection attr = theXmlNode.Attributes;
            int range = Convert.ToInt32(attr.GetNamedItem("Range").Value);

            // 新建节点并添加值
            int   nid      = m_bnNodes.Count;
            BNode newBNode = new BNode(theXmlNode.Name.ToLower(), nid, range);

            //标注出所有节点的父节点以及子节点
            XmlNodeList xmlNodes = theXmlNode.SelectNodes("Parents/*");

            foreach (XmlNode xml_node in xmlNodes)
            {
                foreach (BNode bn_node in m_bnNodes)
                {
                    if (xml_node.Name.ToLower() == bn_node.Name)
                    {
                        bn_node.children.Add(newBNode);
                        newBNode.Parents.Add(bn_node);
                        break;
                    }
                }
            }

            // 对每个节点新建一个条件概率表
            int table_rows = 1;

            xmlNodes = theXmlNode.SelectNodes("CPT_Col");
            //if (range != xmlNodes.Count + 1) throw new Exception("CPT cols mismatch");
            int cond_rows = 1;

            if (newBNode.Parents.Count != 0)
            {
                foreach (BNode bn_node in newBNode.Parents)
                {
                    table_rows *= bn_node.Range;
                    cond_rows  *= bn_node.Range;
                }
            }
            table_rows  *= newBNode.Range;
            newBNode.CPT = new double[cond_rows, range];

            //为条件概率表添加通过学习已得到的参数

            for (int i = 0; i < xmlNodes.Count; ++i)
            {
                XmlNodeList cpNodes = xmlNodes[i].SelectNodes("CP");

                if (cpNodes.Count != table_rows)
                {
                    throw new Exception("CPT Rows mismatch");
                }

                //所有情况按字典序进行排序
                int now = 0;
                for (int j = 0; j < cond_rows; ++j)
                {
                    double pr = 0;
                    for (int k = 0; k < range; ++k)
                    {
                        newBNode.CPT[j, k] = Convert.ToDouble(cpNodes[now].InnerText);
                        now += 1;
                        pr  += newBNode.CPT[j, k];
                    }
                    if (pr > 1.0 && pr < 1.0)
                    {
                        throw new Exception("Probability does not normalize");
                    }
                }
            }

            m_bnNodes.Add(newBNode);
        }
        protected double Sampling(int prob, int probcon)
        {
            mq = 0;
            int T = 10000;            //采样个数

            First_Sampling();         //第一次随机抽样

            Random random = new Random(GetRandomSeedbyGuid());
            int    sample = random.Next();

            if (the_sample[prob] == probcon)
            {
                mq = mq + 1;
            }

            double pr = 0.0;
            double this_pr, multipr1, multipr2;

            nxt_sample = the_sample;

            for (int i = 2; i < T; i++)
            {
                this_pr  = 1;
                multipr1 = 1;
                multipr2 = 1;

                the_sample = nxt_sample;

                //确定抽样时所需要的近似概率
                for (int j = 0; j < m_net.Nodes.Count; j++)
                {
                    BNode theNode = (BNode)m_net.Nodes[j];
                    int   cond    = 0;
                    multipr1 = 1;
                    multipr2 = 1;
                    int flag = 0;


                    if (theNode.Evidence == -1)
                    {
                        if (theNode.Parents.Count != 0)
                        {
                            foreach (BNode node in theNode.Parents)
                            {
                                cond = cond * 2 + the_sample[node.ID];
                            }
                            multipr1 = multipr1 * theNode.CPT[cond, 0];
                            multipr2 = multipr2 * theNode.CPT[cond, 1];
                        }
                        else
                        {
                            multipr1 = multipr1 * theNode.CPT[0, 0];
                            multipr2 = multipr2 * theNode.CPT[0, 1];
                        }

                        foreach (string item in strs)
                        {
                            if (theNode.Name.Contains(item))
                            {
                                this_pr = theNode.CPT[cond, 0];
                                random  = new Random(GetRandomSeedbyGuid());
                                sample  = random.Next();
                                if (sample % 10000000 > this_pr * 10000000)
                                {
                                    nxt_sample[j] = 1;
                                }
                                else
                                {
                                    nxt_sample[j] = 0;
                                }
                                flag = 1;
                            }
                        }
                        if (flag == 1)
                        {
                            continue;
                        }

                        if (theNode.children.Count != 0)
                        {
                            int cond1 = 0;
                            int cond2 = 0;
                            foreach (BNode node in theNode.children)
                            {
                                cond1 = 0;
                                cond2 = 0;
                                foreach (BNode parent in node.Parents)
                                {
                                    if (parent.ID != theNode.ID)
                                    {
                                        cond1 = cond1 * parent.Range + the_sample[parent.ID];
                                        cond2 = cond2 * parent.Range + the_sample[parent.ID];
                                    }
                                    else
                                    {
                                        cond1 = cond1 * theNode.Range + 0;
                                        cond2 = cond2 * theNode.Range + 1;
                                    }
                                }
                                multipr1 = multipr1 * node.CPT[cond1, the_sample[node.ID]];
                                multipr2 = multipr2 * node.CPT[cond2, the_sample[node.ID]];
                            }
                        }
                        this_pr = multipr1 / (multipr1 + multipr2);

                        random = new Random(GetRandomSeedbyGuid());
                        sample = random.Next();

                        if (sample % 10000000 > this_pr * 10000000)
                        {
                            nxt_sample[j] = 1;
                        }
                        else
                        {
                            nxt_sample[j] = 0;
                        }
                    }
                }

                if (nxt_sample[prob] == probcon)
                {
                    mq = mq + 1;
                }
            }

            pr = (double)mq / T;
            return(pr);
        }
예제 #3
0
        private void CreateNode(XmlNode theXmlNode)        //对每个节点的数据进行储存操作
        {
            //Attributes是对于每个节点的属性和值进行获取的函数
            XmlAttributeCollection attr = theXmlNode.Attributes;
            int range = Convert.ToInt32(attr.GetNamedItem("Range").Value);

            // 新建节点并添加值
            int   nid      = m_bnNodes.Count;
            BNode newBNode = new BNode(theXmlNode.Name.ToLower(), nid, range);

            //标注出所有节点的父节点以及子节点
            XmlNodeList xmlNodes = theXmlNode.SelectNodes("Parents/*");

            foreach (XmlNode xml_node in xmlNodes)
            {
                foreach (BNode bn_node in m_bnNodes)
                {
                    if (xml_node.Name.ToLower() == bn_node.Name)
                    {
                        bn_node.children.Add(newBNode);
                        newBNode.Parents.Add(bn_node);
                        break;
                    }
                }
            }

            // 对每个节点新建一个条件概率表
            int table_rows = 1;

            xmlNodes = theXmlNode.SelectNodes("CPT_Col");
            if (range != xmlNodes.Count + 1)
            {
                throw new Exception("CPT cols mismatch");
            }
            foreach (BNode bn_node in newBNode.Parents)
            {
                table_rows *= bn_node.Range;
            }


            newBNode.CPT = new double[table_rows, range];

            //为条件概率表添加通过学习已得到的参数
            for (int i = 0; i < xmlNodes.Count; ++i)
            {
                XmlNodeList cpNodes = xmlNodes[i].SelectNodes("CP");

                if (cpNodes.Count != table_rows)
                {
                    throw new Exception("CPT Rows mismatch");
                }

                for (int j = 0; j < table_rows; ++j)
                {
                    newBNode.CPT[j, i] = Convert.ToDouble(cpNodes[j].InnerText);
                }
            }

            for (int i = 0; i < table_rows; ++i)
            {
                double pr = 1.0;
                for (int j = 0; j < range - 1; ++j)
                {
                    pr -= newBNode.CPT[i, j];
                }
                if (pr < 0)
                {
                    throw new Exception("Probability does not normalize");
                }
                newBNode.CPT[i, range - 1] = pr;
            }

            m_bnNodes.Add(newBNode);
        }
        protected double Sum_Multistate(int nid, int para)
        {
            BNode  theNode = (BNode)m_net.Nodes[nid];
            Bucket theBuck = (Bucket)m_buckets[nid];

            int p_cnt = 1;

            foreach (BNode node in theNode.Parents)
            {
                p_cnt *= node.Range;
            }

            int cond = 0;

            if (p_cnt != 1)
            {
                cond = para % p_cnt;                 //防止溢出
            }
            else
            {
                cond = 0;
            }

            double pr = 0.0;

            for (int e = 0; e < theNode.Range; ++e)
            {
                if (theNode.Evidence != -1 && theNode.Evidence != e)
                {
                    continue;
                }

                double tmpPr = theNode.CPT[cond, e];

                foreach (Bucket nxtBuck in theBuck.childBuckets)
                {
                    int next_para = 0;
                    int cond_new  = 1;
                    int cond_pos  = 1;
                    for (int j = 0; j < nxtBuck.parentNodes.Count; ++j)
                    {
                        BNode pnode = (BNode)nxtBuck.parentNodes[j];

                        int pos = theBuck.parentNodes.IndexOf(pnode);

                        int k;
                        cond_new = 1;

                        for (k = 0; k < j; ++k)
                        {
                            BNode panode = (BNode)nxtBuck.parentNodes[k];
                            cond_new *= panode.Range;
                        }

                        if (pos >= 0)
                        {
                            cond_pos = 1;
                            for (k = 0; k < pos; ++k)
                            {
                                BNode panode = (BNode)theBuck.parentNodes[k];
                                cond_pos *= panode.Range;
                            }
                            next_para += para / cond_pos * cond_new;
                        }
                        else
                        {
                            next_para += e * cond_new;
                        }
                    }

                    tmpPr *= Sum_Multistate(nxtBuck.id, next_para);
                }

                pr += tmpPr;
            }

            return(pr);
        }