Esempio n. 1
0
        public List<Node[]> combination(Node[] a, int m)
        {
            Combination c = new Combination();
            List<Node[]> list = new List<Node[]>();
            int n = a.Length;
            if (m == 0)
            {
                list.Add(new Node[] { });
                return list;
            }
            else if (m == n)
            {
                Node[] temp = new Node[a.Length];
                a.CopyTo(temp, 0);
                list.Add(a);
                return list;
            }

            bool end = false; // 是否是最后一种组合的标记
            // 生成辅助数组。首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。
            int[] tempNum = new int[n];
            for (int i = 0; i < n; i++)
            {
                if (i < m)
                {
                    tempNum[i] = 1;

                }
                else
                {
                    tempNum[i] = 0;
                }
            }
            //printVir(tempNum);// 打印首个辅助数组
            list.Add(c.createResult(a, tempNum, m));// 打印第一种默认组合
            int k = 0;//标记位
            while (!end)
            {
                bool findFirst = false;
                bool swap = false;
                // 然后从左到右扫描数组元素值的"10"组合,找到第一个"10"组合后将其变为"01"
                for (int i = 0; i < n; i++)
                {
                    int l = 0;
                    if (!findFirst && tempNum[i] == 1)
                    {
                        k = i;
                        findFirst = true;
                    }
                    if (tempNum[i] == 1 && tempNum[i + 1] == 0)
                    {
                        tempNum[i] = 0;
                        tempNum[i + 1] = 1;
                        swap = true;
                        for (l = 0; l < i - k; l++)
                        { // 同时将其左边的所有"1"全部移动到数组的最左端。
                            tempNum[l] = tempNum[k + l];
                        }
                        for (l = i - k; l < i; l++)
                        {
                            tempNum[l] = 0;
                        }
                        if (k == i && i + 1 == n - m)
                        {//假如第一个"1"刚刚移动到第n-m+1个位置,则终止整个寻找
                            end = true;
                        }
                    }
                    if (swap)
                    {
                        break;
                    }
                }
                //printVir(tempNum);// 打印辅助数组
                list.Add(c.createResult(a, tempNum, m));// 添加下一种默认组合
            }
            return list;
        }
Esempio n. 2
0
        public Deduce2Result DeduceA2(Node[] reportNodes, bool isDeduceStep2, int suspectedNodeId, Dictionary<Node, MODEventTrustResult> cachedresults, 
            int deduceNodeId, double pDrop, Dictionary<Node, double> pNormal, Dictionary<Node, double> localNodeTrustWeights,
            Dictionary<Node, double> localNodeTrustWeightsLastUpdate, List<IteratorType> iteractions)
        {
            string pkgIdent = cachedresults.First().Value.eventIdent;

            foreach (Node node in reportNodes)
            {
                if (!localNodeTrustWeights.ContainsKey(node))//如果没有记录,则设其初始权重为1
                {
                    localNodeTrustWeights.Add(node, 1f);
                    localNodeTrustWeightsLastUpdate.Add(node, scheduler.currentTime);
                }
                Organization org = global.orgs[((Reader)node).OrgId];
                if (!localNodeTrustWeights.ContainsKey(org))
                {
                    localNodeTrustWeights.Add(org, 1f);
                    localNodeTrustWeightsLastUpdate.Add(org, scheduler.currentTime);
                }
            }
            foreach (Organization org in global.orgs)
            {
                if (!localNodeTrustWeights.ContainsKey(org))
                {
                    localNodeTrustWeights.Add(org, 1f);
                    localNodeTrustWeightsLastUpdate.Add(org, scheduler.currentTime);
                }
            }

            double supportMWeight = 0, nonsupportMWeight = 0;
            double supportMNodes = 0, nonsupportMNodes = 0;
            bool isReportSupportM = false;
            //计算报告是support还是nonsupport
            foreach (KeyValuePair<Node, MODEventTrustResult> k in cachedresults)
            {
                Node node = k.Key;
                MODEventTrustResult result = k.Value;

                if (MODEventTrust.getDist(result.ds) < global.ReportMinDist)
                    continue;

                Organization org = global.orgs[((Reader)node).OrgId];
                if (result.supportDroppingMalicious > 0)
                {
                    supportMWeight += localNodeTrustWeights[node] * localNodeTrustWeights[org];
                    supportMNodes++;
                }
                else
                {
                    nonsupportMWeight += localNodeTrustWeights[node] * localNodeTrustWeights[org];
                    nonsupportMNodes++;
                }
            }

            if (supportMWeight > nonsupportMWeight)
                isReportSupportM = true;
            else
                isReportSupportM = false;

            //double pGroupNormalBySupportM = 0, pGroupMaliciousBySupportM = 0;
            //double pGroupNormalByNonsupportM = 0, pGroupMaliciousByNonsupportM = 0;
            double pGroupNormal = 0;
            Combination c = new Combination();
            //只计算占优势的一方,temp是优势方
            //for (int m = reportNodes.Length / 2 + 1; m < reportNodes.Length; m++)
            for (int m = 0; m <= reportNodes.Length; m++)
            {
                List<Node[]> lists = c.combination(reportNodes, m);

                //求pGroupNormal
                for (int i = 0; i < lists.Count; i++)
                {
                    Node[] list = (Node[])lists[i];
                    double w1 = 0, w2 = 0;
                    HashSet<Node> set1 = new HashSet<Node>();
                    HashSet<Node> set2 = new HashSet<Node>();

                    foreach (Node node in list)
                    {
                        Organization org = global.orgs[((Reader)node).OrgId];
                        set1.Add(node);
                        w1 += localNodeTrustWeights[node]*localNodeTrustWeights[org];
                    }

                    foreach (Node node in reportNodes)//找出相反的节点
                    {
                        if (set1.Contains(node))
                            continue;
                        Organization org = global.orgs[((Reader)node).OrgId];
                        set2.Add(node);
                        w2 += localNodeTrustWeights[node] * localNodeTrustWeights[org];
                    }
                    if (w1 < w2)//如果权重和不够,则skip
                        continue;
                    //剩下的都是set1权值大于set2的情况

                    double p1 = 1;
                    foreach (Node node in set1)
                    {
                        p1 = p1 * pNormal[node];
                    }
                    foreach (Node node in set2)//找出相反的节点
                    {
                        double pMalicious = 1 - pNormal[node];
                        p1 = p1 * pMalicious;
                    }
                    pGroupNormal += p1;
                }
            }

            double aAccept = 0, aReject = 0;
            bool isAccept = false;

            double pFwrd = 1 - pDrop;
            double pGroupMalicious = 1 - pGroupNormal;

            double pSupportMByDropAndGroupNormal= 0, pSupportMByFwrdAndGroupNormal = 0, pSupportMByDropAndGroupMalicious = 0,
                pSupportMByFwrdAndGroupMalicious = 0, pNonsupportMByDropAndGroupNormal = 0, pNonsupportMByFwrdAndGroupNormal = 0,
                pNonsupportMByDropAndGroupMalicious = 0, pNonsupportMByFwrdAndGroupMalicious = 0;

            CalculateBelief(ref pSupportMByDropAndGroupNormal, ref pSupportMByFwrdAndGroupNormal,
                ref pSupportMByDropAndGroupMalicious, ref pSupportMByFwrdAndGroupMalicious,
                ref pNonsupportMByDropAndGroupNormal, ref pNonsupportMByFwrdAndGroupNormal,
                ref pNonsupportMByDropAndGroupMalicious, ref pNonsupportMByFwrdAndGroupMalicious, iteractions);

            double uA2DropAndNormalAndSupportMDAndAccept = global.uA2DropAndNormalAndSupportMDAndAccept;
            double uA2FwrdAndNormalAndSupportMDAndAccept = global.uA2FwrdAndNormalAndSupportMDAndAccept;
            double uA2DropAndMaliciousAndSupportMDAndAccept = global.uA2DropAndMaliciousAndSupportMDAndAccept;
            double uA2FwrdAndMaliciousAndSupportMDAndAccept = global.uA2FwrdAndMaliciousAndSupportMDAndAccept;
            double uA2DropAndNormalAndSupportMDAndReject = global.uA2DropAndNormalAndSupportMDAndReject;
            double uA2FwrdAndNormalAndSupportMDAndReject = global.uA2FwrdAndNormalAndSupportMDAndReject;
            double uA2DropAndMaliciousAndSupportMDAndReject = global.uA2DropAndMaliciousAndSupportMDAndReject;
            double uA2FwrdAndMaliciousAndSupportMDAndReject = global.uA2FwrdAndMaliciousAndSupportMDAndReject;
            double uA2DropAndNormalAndNonsupportMDAndAccept = global.uA2DropAndNormalAndNonsupportMDAndAccept;
            double uA2FwrdAndNormalAndNonsupportMDAndAccept = global.uA2FwrdAndNormalAndNonsupportMDAndAccept;
            double uA2DropAndMaliciousAndNonsupportMDAndAccept = global.uA2DropAndMaliciousAndNonsupportMDAndAccept;
            double uA2FwrdAndMaliciousAndNonsupportMDAndAccept = global.uA2FwrdAndMaliciousAndNonsupportMDAndAccept;
            double uA2DropAndNormalAndNonsupportMDAndReject = global.uA2DropAndNormalAndNonsupportMDAndReject;
            double uA2FwrdAndNormalAndNonsupportMDAndReject = global.uA2FwrdAndNormalAndNonsupportMDAndReject;
            double uA2DropAndMaliciousAndNonsupportMDAndReject = global.uA2DropAndMaliciousAndNonsupportMDAndReject;
            double uA2FwrdAndMaliciousAndNonsupportMDAndReject = global.uA2FwrdAndMaliciousAndNonsupportMDAndReject;

            Reader deduceNode = global.readers[deduceNodeId];
            UpdateAwardFunction(cachedresults[deduceNode].supportDroppingMalicious > 0, isReportSupportM,
                ref uA2DropAndNormalAndSupportMDAndAccept, ref uA2FwrdAndNormalAndSupportMDAndAccept,
                ref uA2DropAndMaliciousAndSupportMDAndAccept, ref uA2FwrdAndMaliciousAndSupportMDAndAccept,
                ref uA2DropAndNormalAndSupportMDAndReject, ref uA2FwrdAndNormalAndSupportMDAndReject,
                ref uA2DropAndMaliciousAndSupportMDAndReject, ref uA2FwrdAndMaliciousAndSupportMDAndReject,
                ref uA2DropAndNormalAndNonsupportMDAndAccept, ref uA2FwrdAndNormalAndNonsupportMDAndAccept,
                ref uA2DropAndMaliciousAndNonsupportMDAndAccept, ref uA2FwrdAndMaliciousAndNonsupportMDAndAccept,
                ref uA2DropAndNormalAndNonsupportMDAndReject, ref uA2FwrdAndNormalAndNonsupportMDAndReject,
                ref uA2DropAndMaliciousAndNonsupportMDAndReject, ref uA2FwrdAndMaliciousAndNonsupportMDAndReject
                );

            if (isReportSupportM)
            {
                double pSupportM = pSupportMByDropAndGroupNormal * pDrop * pGroupNormal
                    + pSupportMByFwrdAndGroupNormal * pFwrd * pGroupNormal
                    + pSupportMByDropAndGroupMalicious * pDrop * pGroupMalicious
                    + pSupportMByFwrdAndGroupMalicious * pFwrd * pGroupMalicious;

                double pDropAndGroupNormalBySupportM = pSupportMByDropAndGroupNormal * pDrop * pGroupNormal/pSupportM;
                double pFwrdAndGroupNormalBySupportM = pSupportMByFwrdAndGroupNormal * pFwrd * pGroupNormal / pSupportM;
                double pDropAndGroupMaliciousBySupportM = pSupportMByDropAndGroupMalicious * pDrop * pGroupMalicious / pSupportM;
                double pFwrdAndGroupMaliciousBySupportM = pSupportMByFwrdAndGroupMalicious * pFwrd * pGroupMalicious / pSupportM;

                //计算效用函数与后验概率的乘积
                double aNormalAndSupportMDAndAccept = pDropAndGroupNormalBySupportM * uA2DropAndNormalAndSupportMDAndAccept
                    + pFwrdAndGroupNormalBySupportM * uA2FwrdAndNormalAndSupportMDAndAccept;
                double aMaliciousAndSupportMDAndAccept = pDropAndGroupMaliciousBySupportM * uA2DropAndMaliciousAndSupportMDAndAccept
                    + pFwrdAndGroupMaliciousBySupportM * uA2FwrdAndMaliciousAndSupportMDAndAccept;

                double aNormalAndSupportMDAndReject = pDropAndGroupNormalBySupportM * uA2DropAndNormalAndSupportMDAndReject
                    + pFwrdAndGroupNormalBySupportM * uA2FwrdAndNormalAndSupportMDAndReject;
                double aMaliciousAndSupportMDAndReject = pDropAndGroupMaliciousBySupportM * uA2DropAndMaliciousAndSupportMDAndReject
                    + pFwrdAndGroupMaliciousBySupportM * uA2FwrdAndMaliciousAndSupportMDAndReject;

                aAccept = aNormalAndSupportMDAndAccept + aMaliciousAndSupportMDAndAccept;
                aReject = aNormalAndSupportMDAndReject + aMaliciousAndSupportMDAndReject;

                isAccept = (aAccept > aReject);
                //Console.WriteLine("pGroupNormalBySupportM:{0}, pDropBySupportM:{1}", pGroupNormalBySupportM, pDrop);
                //Console.WriteLine("pGroupMaliciousBySupportM:{0}, (1-pDropBySupportM):{1}", pGroupMaliciousBySupportM, (1 - pDrop));

            }
            else //NonsupportM 报告说事件是正常的
            {

                double pNonsupportM = pNonsupportMByDropAndGroupNormal * pDrop * pGroupNormal
                    + pNonsupportMByFwrdAndGroupNormal * pFwrd * pGroupNormal
                    + pNonsupportMByDropAndGroupMalicious * pDrop * pGroupMalicious
                    + pNonsupportMByFwrdAndGroupMalicious * pFwrd * pGroupMalicious;

                double pGroupDropAndNormalByNonsupportM = pNonsupportMByDropAndGroupNormal * pDrop * pGroupNormal / pNonsupportM;
                double pGroupFwrdAndNormalByNonsupportM = pNonsupportMByFwrdAndGroupNormal * pFwrd * pGroupNormal / pNonsupportM;
                double pGroupDropAndMaliciousByNonsupportM = pNonsupportMByDropAndGroupMalicious * pDrop * pGroupMalicious / pNonsupportM;
                double pGroupFwrdAndMaliciousByNonsupportM = pNonsupportMByFwrdAndGroupMalicious * pFwrd * pGroupMalicious / pNonsupportM;

                double aNormalAndNonsupportMDAndAccept = pGroupDropAndNormalByNonsupportM * uA2DropAndNormalAndNonsupportMDAndAccept
                    + pGroupFwrdAndNormalByNonsupportM * uA2FwrdAndNormalAndNonsupportMDAndAccept;
                double aMaliciousAndNonsupportMDAndAccept = pGroupDropAndMaliciousByNonsupportM * uA2DropAndMaliciousAndNonsupportMDAndAccept
                    + pGroupFwrdAndMaliciousByNonsupportM * uA2FwrdAndMaliciousAndNonsupportMDAndAccept;

                double aNormalAndNonsupportMDAndReject = pGroupDropAndNormalByNonsupportM * uA2DropAndNormalAndNonsupportMDAndReject
                    + pGroupFwrdAndNormalByNonsupportM * uA2FwrdAndNormalAndNonsupportMDAndReject;
                double aMaliciousAndNonsupportMDAndReject = pGroupDropAndMaliciousByNonsupportM * uA2DropAndMaliciousAndNonsupportMDAndReject
                    + pGroupFwrdAndMaliciousByNonsupportM * uA2FwrdAndMaliciousAndNonsupportMDAndReject;

                aAccept = aNormalAndNonsupportMDAndAccept + aMaliciousAndNonsupportMDAndAccept;
                aReject = aNormalAndNonsupportMDAndReject + aMaliciousAndNonsupportMDAndReject;

                isAccept = (aAccept > aReject);
                //Console.WriteLine("pGroupNormalByNonsupportM:{0}, pDropByNonsupportM:{1}", pGroupNormal, pDropByNonsupportM);
                //Console.WriteLine("pGroupMaliciousByNonsupportM:{0}, (1-pDropByNonsupportM):{1}", pGroupMalicious, (1 - pDropByNonsupportM));
            }
            bool isDuduceNormalEvent= (isReportSupportM ^ isAccept);
            string deductionStep = "";
            if(isDeduceStep2 == false)
                deductionStep = "DEDUCTION1-1";
            else if(deduceNode.Neighbors.ContainsKey(suspectedNodeId))//是第二步
                deductionStep = "DEDUCTION1-2";
            else
                deductionStep = "DEDUCTION1-3";
            string sAccept = (isAccept == true) ? "accept" : "reject";
            string sSupport = (isReportSupportM == true) ? "supporting" : "nonsupporting";

            //这里由于真实事件都是正常的,所以直接用true代替,ps:DropData代表正常场景下丢包
            string sResult = (isDuduceNormalEvent != global.DropData) ? "Succ" : "Fail";
            sResult = (isDeduceStep2 == true) ? sResult : "None";
            //尽管在第一步中,deduction是deduceNode做的,但是我们这里写是this做的
            Console.WriteLine("{0:F4} [{1}] {2}{3} deduces {4} {5}{6} is {7} by {8}. [{9}:{10}]\t[{11}:{12}]\t[{13}:{14}]-${15}$:{16}",
                scheduler.currentTime, deductionStep, this.type, this.Id, sSupport, NodeType.READER, suspectedNodeId, sAccept, deduceNodeId,
                aAccept, aReject, supportMWeight, nonsupportMWeight, supportMNodes, nonsupportMNodes, pkgIdent, sResult);

            if (isDeduceStep2 == true)
            {
                Console.Write("{0:F4} [ORG_TRUST] {1}{2}:\t", scheduler.currentTime, this.type, this.Id);
                for (int i = 0; i < global.orgNum; i++)
                {
                    Console.Write("{0}-", localNodeTrustWeights[global.orgs[i]]);
                }
                Console.WriteLine();
            }

            //比较各个节点的报告与整体报告的差别,如果我接受整体报告,则惩罚与整体报告不同的节点(即与supportReport相反的节点),反之亦然

            if (isDeduceStep2 == true)
            {
                PunishMaliciousNodes(isDuduceNormalEvent, cachedresults);
                if (this.Neighbors.ContainsKey(suspectedNodeId))
                    UpdateNodeBelieves(isDuduceNormalEvent, pGroupNormal > 0.5, isReportSupportM, iteractions, isDeduceStep2);
            }

            Deduce2Result d2r = new Deduce2Result();
            d2r.IsAccept = isAccept;
            d2r.IsTotalReportSupport = isReportSupportM;
            return d2r;
        }