Exemple #1
0
        public IEnumerable <CaseRetrievalResult> Retrieve(ReasonerContext context)
        {
            var cases = new Dictionary <string, (double count, double measure)>();

            foreach (var retriever in retrievers)
            {
                var retrievedCases = retriever.Retrieve(context).ToArray();
                for (var i = 0; i < retrievedCases.Length; i++)
                {
                    var retrievedCase = retrievedCases[i];
                    if (!cases.TryGetValue(retrievedCase.CaseId, out var targetCase))
                    {
                        cases[retrievedCase.CaseId] = (i, retrievedCase.Measure);
                    }
                    else
                    {
                        cases[retrievedCase.CaseId] = (targetCase.count + i, targetCase.measure + retrievedCase.Measure);
                    }
                }
            }

            return(cases.Select(c => new CaseRetrievalResult(c.Key, c.Value.count))
                   .OrderBy(c => c.Measure)
                   .Take(threshold));
        }
        public IEnumerable <CaseRetrievalResult> Retrieve(ReasonerContext context)
        {
            var similaritySet = new SortedSet <CaseRetrievalResult>(new CaseRetrievalComparer());

            using (var cursor = GetTargetFrame(context).GetCaseCursor())
            {
                while (cursor.MoveNext())
                {
                    var similarity = measure.GetSimilarity(cursor, context.TargetCase, context);
                    var result     = new CaseRetrievalResult(cursor.GetId(), similarity);
                    if (similaritySet.Count == threshold)
                    {
                        if (result.Measure >= similaritySet.Max.Measure)
                        {
                            continue;
                        }
                        similaritySet.Remove(similaritySet.Max);
                        similaritySet.Add(result);
                    }
                    else
                    {
                        similaritySet.Add(result);
                    }
                }
            }

            return(similaritySet);
        }
        public double GetSimilarity(ICase caseA, ICase caseB, ReasonerContext context)
        {
            if (covarianceMatrix == null)
            {
                covarianceMatrix = GetCovarianceMatrix(context.Cases);
            }
            if (caseA == null)
            {
                throw new ArgumentNullException(nameof(caseA));
            }
            if (caseB == null)
            {
                throw new ArgumentNullException(nameof(caseB));
            }

            var differenceVector = new DenseMatrix(1, caseA.Schema.Count);

            for (var i = 0; i < caseA.Schema.Count; i++)
            {
                differenceVector[0, i] = caseA.GetAttribute <double>(caseA.Schema[i]) - caseB.GetAttribute <double>(caseA.Schema[i]);
            }

            var distance = differenceVector * covarianceMatrix.Transpose() * differenceVector.Transpose();

            return(Math.Sqrt(distance[0, 0]));
        }
Exemple #4
0
        public TSolution Reuse <TSolution>(ReasonerContext context)
        {
            var nearestNeighbors = context.RetrievedCases
                                   .Take(k)
                                   .ToDictionary(c => c.CaseId, c => c.Measure);

            if (!nearestNeighbors.Any())
            {
                return(default);
Exemple #5
0
 public IEnumerable <CaseRetrievalResult> Retrieve(ReasonerContext context)
 {
     if (lshSpace == null)
     {
         CreateLshSpace(context);
     }
     return(lshSpace
            .Get(context.TargetCase)
            .Select(id => new CaseRetrievalResult(id, 1)));
 }
Exemple #6
0
 private void CreateLshSpace(ReasonerContext context)
 {
     lshSpace = new LshSpace(size, context.Schema.Count, depth);
     using (var cursor = context.Cases.GetCaseCursor())
     {
         while (cursor.MoveNext())
         {
             lshSpace.Add(cursor);
         }
     }
 }
        private static ICaseFrame GetTargetFrame(ReasonerContext context)
        {
            if (context.RetrievedCases == null || !context.RetrievedCases.Any())
            {
                return(context.Cases);
            }

            var retrievedIds = context.RetrievedCases.Select(c => c.CaseId).ToArray();

            return(context.Cases.FilterCases(retrievedIds));
        }
Exemple #8
0
        public double GetSimilarity(ICase caseA, ICase caseB, ReasonerContext context)
        {
            if (caseA == null)
            {
                throw new ArgumentNullException(nameof(caseA));
            }
            if (caseB == null)
            {
                throw new ArgumentNullException(nameof(caseB));
            }

            var attributesA = caseA.Schema.Select(caseA.GetAttribute <double>).ToArray();
            var attributesB = caseA.Schema.Select(caseB.GetAttribute <double>).ToArray();

            return(Dot(attributesA, attributesB) / (Math.Pow(Dot(attributesA, attributesA), 2) * Math.Pow(Dot(attributesB, attributesB), 2)));
        }
Exemple #9
0
        public TSolution GetSolution <TSolution>(ICase targetCase)
        {
            if (data == null)
            {
                throw new InvalidOperationException("Case data for the reasoner is not set.");
            }
            var context = new ReasonerContext(data, targetCase);

            foreach (var retriever in cycle.Retrievers)
            {
                var retrievedCases = retriever.Retrieve(context);
                context.RetrievedCases = retrievedCases;
            }

            var result = cycle.Reuser != null?cycle.Reuser.Reuse <TSolution>(context) : default;

            return(result);
        }
        public double GetSimilarity(ICase caseA, ICase caseB, ReasonerContext context)
        {
            if (caseA == null)
            {
                throw new ArgumentNullException(nameof(caseA));
            }
            if (caseB == null)
            {
                throw new ArgumentNullException(nameof(caseB));
            }

            double result = 0;

            foreach (var attribute in caseA.Schema)
            {
                if (targetAttributes.Length > 0 && !targetAttributes.Contains(attribute.Name))
                {
                    continue;
                }
                result += Math.Pow(Math.Abs(caseA.GetAttribute <double>(attribute) - caseB.GetAttribute <double>(attribute)), order);
            }

            return(Math.Pow(result, 1 / order));
        }