private IEnumerable <SuspiciousNode> SelectSuspiciousElementsWithMaskWithSmallGrowing(
     IReadOnlyList <List <SuspiciousNode> > candidateNodesLists, BigInteger xor, BigInteger mask)
 {
     for (int i = 0; i < candidateNodesLists.Count; i++)
     {
         var vector     = BigInteger.Zero;
         var candidates = candidateNodesLists[i];
         while (true)
         {
             var            minDiffCount = int.MaxValue;
             SuspiciousNode newNode      = null;
             foreach (var candidate in candidates)
             {
                 var newVector = (vector | (candidate.Vector ^ xor)) & mask;
                 var diff      = newVector ^ vector;
                 var diffCount = LearningExperimentUtil.CountBits(diff);
                 if (diffCount > 0 && minDiffCount > diffCount)
                 {
                     minDiffCount = diffCount;
                     vector       = newVector;
                     newNode      = candidate;
                 }
             }
             if (newNode != null)
             {
                 yield return(newNode);
             }
             else
             {
                 break;
             }
         }
     }
 }
        private SuspiciousNode SelectMostDifferentElement(
            IEnumerable <BigInteger> existings, IEnumerable <SuspiciousNode> candidates,
            BigInteger mask)
        {
            if (!existings.Any())
            {
                return(candidates.FirstOrDefault());
            }
            var            maxDiff = 0;
            SuspiciousNode ret     = null;

            foreach (var candidate in candidates)
            {
                var vector = candidate.Vector & mask;
                var diff   = existings.Min(f => LearningExperimentUtil.CountBits((f & mask) ^ vector));
                if (maxDiff < diff)
                {
                    maxDiff = diff;
                    ret     = candidate;
                }
            }
            return(ret);
        }
예제 #3
0
        public ClassificationResult Classify(
            int count, Classifier classifier, GroupCache groupCache,
            EncodingResult encodingResult,
            RevealedVectorSet trainingSet = null)
        {
            trainingSet = trainingSet ?? new RevealedVectorSet();
            var correctlyAccepted            = 0;
            var correctlyRejected            = 0;
            var wronglyAccepted              = 0;
            var wronglyRejected              = 0;
            var correctlyRejectedInRejecting = 0;
            var wronglyRejectedInRejecting   = 0;
            var rejectAccept = new List <List <SuspiciousNode> >();
            var rejectReject = new List <List <SuspiciousNode> >();
            var acceptAccept = new List <List <SuspiciousNode> >();
            var acceptReject = new List <List <SuspiciousNode> >();

            for (int i = 0; i < classifier.GroupCount; i++)
            {
                rejectAccept.Add(new List <SuspiciousNode>());
                rejectReject.Add(new List <SuspiciousNode>());
                acceptAccept.Add(new List <SuspiciousNode>());
                acceptReject.Add(new List <SuspiciousNode>());
            }

            var wrongNodeCount          = 0;
            var wronglyRejectedFeatures = new List <BigInteger>();
            var wronglyAcceptedFeatures = new List <BigInteger>();

            foreach (var vectorAndGroupPath in encodingResult.IdealVectorSet.Accepted)
            {
                var vector     = vectorAndGroupPath.Key;
                var groupPath  = vectorAndGroupPath.Value;
                var groupIndex = groupCache.GetGroupIndex(vector);
                var rejected   = classifier.IsRejected(vector, groupIndex);
                var accepted   = classifier.IsAccepted(vector, groupIndex);
                if (!trainingSet.Accepted.ContainsKey(vector) &&
                    !trainingSet.Rejected.ContainsKey(vector))
                {
                    var target = new SuspiciousNode {
                        Vector   = vector,
                        GroupKey = groupPath,
                        Used     = false,
                    };
                    if (accepted)
                    {
                        if (!rejected)
                        {
                            acceptAccept[groupIndex].Add(target);
                        }
                        else
                        {
                            acceptReject[groupIndex].Add(target);
                        }
                    }
                    else
                    {
                        if (!rejected)
                        {
                            rejectAccept[groupIndex].Add(target);
                        }
                        else
                        {
                            rejectReject[groupIndex].Add(target);
                        }
                    }
                }
                if (!accepted)
                {
                    wronglyRejected++;
                    wrongNodeCount += encodingResult.Vector2Count[vector];
                    wronglyRejectedFeatures.Add(vector);
                }
                else if (!rejected)
                {
                    correctlyAccepted++;
                }
                else
                {
                    wronglyRejectedInRejecting++;
                    wrongNodeCount += encodingResult.Vector2Count[vector];
                    wronglyRejectedFeatures.Add(vector);
                }
            }

            foreach (var vectorAndGroupPath in encodingResult.IdealVectorSet.Rejected)
            {
                var vector     = vectorAndGroupPath.Key;
                var groupPath  = vectorAndGroupPath.Value;
                var groupIndex = groupCache.GetGroupIndex(vector);
                var rejected   = classifier.IsRejected(vector, groupIndex);
                var accepted   = classifier.IsAccepted(vector, groupIndex);
                if (!trainingSet.Accepted.ContainsKey(vector) &&
                    !trainingSet.Rejected.ContainsKey(vector))
                {
                    var target = new SuspiciousNode {
                        Vector   = vector,
                        GroupKey = groupPath,
                        Used     = false,
                    };
                    if (accepted)
                    {
                        if (!rejected)
                        {
                            acceptAccept[groupIndex].Add(target);
                        }
                        else
                        {
                            acceptReject[groupIndex].Add(target);
                        }
                    }
                    else
                    {
                        if (!rejected)
                        {
                            rejectAccept[groupIndex].Add(target);
                        }
                        else
                        {
                            rejectReject[groupIndex].Add(target);
                        }
                    }
                }
                if (!accepted)
                {
                    correctlyRejected++;
                }
                else if (!rejected)
                {
                    wronglyAccepted++;
                    wrongNodeCount += encodingResult.Vector2Count[vector];
                    wronglyAcceptedFeatures.Add(vector);
                }
                else
                {
                    correctlyRejectedInRejecting++;
                }
            }
            Console.WriteLine("done");
            Console.WriteLine(
                "WA: " + wronglyAccepted + ", WR: " + wronglyRejected + "/"
                + wronglyRejectedInRejecting + ", CA: "
                + correctlyAccepted + ", CR: " + correctlyRejected + "/"
                + correctlyRejectedInRejecting);
            Console.WriteLine(
                "TR: "
                + trainingSet.Count + ", AF: "
                + String.Join(", ", classifier.CountAcceptingFeatures())
                + ", RF: "
                + String.Join(", ", classifier.CountRejectingFeatures()));
            var wrongFeatureCount = wronglyAccepted + wronglyRejected
                                    + wronglyRejectedInRejecting;

            var selector = new SuspiciousNodeSelector(classifier.AcceptingFeatureBitMask,
                                                      classifier.RejectingFeatureBitMask);
            var suspiciousNodes = selector.SelectSuspiciousNodes(count, classifier, acceptAccept,
                                                                 acceptReject, rejectAccept, rejectReject);

            return(new ClassificationResult(suspiciousNodes, wronglyAcceptedFeatures,
                                            wronglyRejectedFeatures,
                                            wrongFeatureCount, wrongNodeCount, encodingResult));
        }