Beispiel #1
0
 public void Load(BinarySerializer reader)
 {
     Utils.ThrowException(reader == null ? new ArgumentNullException("reader") : null);
     // the following statements throw serialization-related exceptions
     m_centroids  = reader.ReadObject <ArrayList <Pair <LblT, SparseVector <double> .ReadOnly> > >();
     m_similarity = reader.ReadObject <ISimilarity <SparseVector <double> .ReadOnly> >();
     m_normalize  = reader.ReadBool();
 }
Beispiel #2
0
 public void Load(BinarySerializer reader)
 {
     Utils.ThrowException(reader == null ? new ArgumentNullException("reader") : null);
     // the following statements throw serialization-related exceptions
     m_examples    = new ArrayList <LabeledExample <LblT, ExT> >(reader);
     m_similarity  = reader.ReadObject <ISimilarity <ExT> >();
     m_k           = reader.ReadInt();
     m_soft_voting = reader.ReadBool();
 }
Beispiel #3
0
 public void Load(BinarySerializer reader)
 {
     Utils.ThrowException(reader == null ? new ArgumentNullException("reader") : null);
     // the following statements throw serialization-related exceptions
     mExamples   = reader.ReadObject <ArrayList <LabeledExample <LblT, ExT> > >();
     mSimilarity = reader.ReadObject <ISimilarity <ExT> >();
     mK          = reader.ReadInt();
     mSoftVoting = reader.ReadBool();
     mLblCmp     = reader.ReadObject <IEqualityComparer <LblT> >();
 }
Beispiel #4
0
        public static ISimilarity GetSimilarity(int mode)
        {
            if (mode == 0)
            {
                _similarity = new Similarity();
            }
            else
            {
                _similarity = new SimilarityByShingle();
            }

            return(_similarity);
        }
Beispiel #5
0
        public static void SimilarityInterfaceTest(ISimilarity distance, string first, string second, double expected)
        {
            var resultString = distance.GetSimilarity(first, second);

            Assert.AreEqual(expected, resultString, ErrorTollerance);

            var resultNorm = distance.GetSimilarity(new NormalizedString(first), new NormalizedString(second));

            Assert.AreEqual(expected, resultNorm, ErrorTollerance);

            var resultToken = distance.GetSimilarity(new Token(first), new Token(second));

            Assert.AreEqual(expected, resultToken, ErrorTollerance);
        }
        /// <summary>
        /// compute the similarity of case base's cases and problem case
        ///
        /// </summary>
        /// <param name="cases"></param>
        /// <param name="problem"></param>
        /// <returns></returns>
        public ArrayList ComputeSimilarity(ArrayList cases, Case problem)
        {
            if (_env == null)
            {
                throw new ContextException("environment variable is not set");
            }

            ICBRContext ctx = CBRContextManager.GetCBRContext(_env);

            if (ctx == null)
            {
                throw new ContextException("not set context");
            }

            ISimilarity sim = (ISimilarity)ctx.GetSimilarity();

            if (sim == null)
            {
                throw new ContextException("similarity method is not set");
            }

            ArrayList stats = new ArrayList();
            double    similarityThrehold = ctx.GetSimilarityThrehold();

            for (int i = 0; i < cases.Count; i++)
            {
                Case   solution   = (Case)cases[i];
                double similarity = sim.Compare(problem, solution);
                //continue if the similarity by comparing is lower than the
                //similarity threhold in context setting
                if (similarity < similarityThrehold)
                {
                    continue;
                }
                IStat s = StatFactory.newInstance();
                s.SetCBRCase(solution);
                s.SetCaseSimilarity(similarity);
                //bi-sort similarity
                if (stats.Count <= 0)
                {
                    stats.Add(s);
                    continue;
                }
                SortSimilarity(stats, s);
            }

            return(stats);
        }
Beispiel #7
0
 public void SetSimilarity(ISimilarity similarity)
 {
     _similarity = similarity;
 }
 public RecommendationCreator(ISimilarity similarityComputer, IRatingPrediction predicter, int amountOfNeighbours)
 {
     this.similarityComputer = similarityComputer;
     this.predicter          = predicter;
     this.amountOfNeighbours = amountOfNeighbours;
 }
Beispiel #9
0
        public NearestNeighbours(Dictionary <int, Dictionary <int, double> > ratings, int userId, ISimilarity similarityFunction, int maxNeighbours, double threshold)
        {
            // loop over the users in the dictionary
            foreach (var user in ratings)
            {
                // only consider the ratings of the users that are not our target user
                if (user.Key != userId)
                {
                    Tuple <Vector, Vector> commonRatings;

                    if (similarityFunction.GetType() != typeof(Cosine))
                    {
                        commonRatings = CommonRatings.getCommonRatings(ratings[userId], user.Value);
                    }
                    else
                    {
                        commonRatings = CommonRatings.getCosineRatings(ratings[userId], user.Value);
                    }

                    var newSimilarity = similarityFunction.getSimilarity(commonRatings.Item1, commonRatings.Item2);
                    var anotherRating = anotherRatingYesOrNo(ratings[userId], user.Value);

                    if (newSimilarity > threshold && anotherRating)
                    {
                        if (nearestNeighbours.Count() < maxNeighbours)
                        {
                            nearestNeighbours.Add(user.Key, newSimilarity);
                        }
                        else
                        {
                            var oldSimilarity = nearestNeighbours.OrderBy(y => y.Value).First();
                            if (newSimilarity > oldSimilarity.Value)
                            {
                                nearestNeighbours.Remove(oldSimilarity.Key);
                                nearestNeighbours.Add(user.Key, newSimilarity);
                            }
                        }
                    }
                    if (nearestNeighbours.Count() == maxNeighbours)
                    {
                        threshold = nearestNeighbours.Values.Min();
                    }
                }
            }
        }
Beispiel #10
0
 public KnnClassifier(ISimilarity <ExT> similarity) : this(similarity, /*lblCmp=*/ null) // throws ArgumentNullException
 {
 }
Beispiel #11
0
 public KnnClassifier(ISimilarity <ExT> similarity, IEqualityComparer <LblT> lblCmp)
 {
     Similarity = similarity; // throws ArgumentNullException
     mLblCmp    = lblCmp;
 }
Beispiel #12
0
 public KnnClassifier(ISimilarity <ExT> similarity)
 {
     Similarity = similarity; // throws ArgumentNullException
 }
 public KNNClassifier(double similarityCoefficient, int kCoefficient)
 {
     _similarity            = new EuclideanSimilarity();
     _similarityCoefficient = similarityCoefficient;
     _neighboursToConsider  = kCoefficient;
 }
Beispiel #14
0
        public Dictionary <User, double> getNearestNeighboursAndSimilarities(int maximumNeighboursAmount, List <User> allUsers, ISimilarity similarityMeasurer)
        {
            var similarityMinimum = 0.35;

            var neighboursAndSimilarities = new Dictionary <User, double>();

            foreach (var currentUser in allUsers)
            {
                if (currentUser.userId != this.userId)
                {
                    var ourRatingsVectors     = this.getRatingsVectors(currentUser, similarityMeasurer.canHandleSparseData());
                    var myRatingVector        = ourRatingsVectors[this.userId];
                    var otherUserRatingVector = ourRatingsVectors[currentUser.userId];

                    var currentSimilarity = similarityMeasurer.computeSimilarity(myRatingVector, otherUserRatingVector);

                    if (currentSimilarity > similarityMinimum && hasRatedDifferentProducts(currentUser))
                    {
                        if (neighboursAndSimilarities.Count < maximumNeighboursAmount)
                        {
                            neighboursAndSimilarities.Add(currentUser, currentSimilarity);
                        }
                        else
                        {
                            var lowestSimilarity      = getLowestSimilarity(neighboursAndSimilarities);
                            var leastSimilarNeighbour = neighboursAndSimilarities.Aggregate((l, r) => l.Value < r.Value ? l : r).Key;

                            //Ugly extra if
                            if (currentSimilarity > lowestSimilarity)
                            {
                                neighboursAndSimilarities.Remove(leastSimilarNeighbour);
                                neighboursAndSimilarities.Add(currentUser, currentSimilarity);

                                //calling getLowestSimilarity since the neighbours are now updated
                                similarityMinimum = getLowestSimilarity(neighboursAndSimilarities);
                            }
                        }
                    }

                    if (neighboursAndSimilarities.Count == maximumNeighboursAmount)
                    {
                        similarityMinimum = getLowestSimilarity(neighboursAndSimilarities);
                    }
                }
            }
            return(neighboursAndSimilarities);
        }
Beispiel #15
0
        public static void RunAnalysis( )
        {
            Analysis analytics = new Analysis();
            isSim = new Similarity();
            isSim.Threshold = 0.25;

            var topN = 500;

            bool toConsole = true; // If we want to see the output live.

            inputs = new TicketCompressible[0];
            analytics.ObtainTickets(ref inputs);

            // Now each element in inputs has itemID, summary, and complexity.

            using (var w = new StreamWriter("output.txt"))
            {
                var sw = new Stopwatch();
                sw.Start();
                foreach (TicketCompressible ticket in inputs.Take(topN))
                {
                    ticket.SimilarIDList = analytics.FindSimilars(ticket, inputs);

                    // Display all the matches to this ticket
                    var dupItemIDs = new List<string>();
                    foreach (TicketCompressible match in ticket.SimilarIDList)
                    {
                        if (match.ItemID != ticket.ItemID)
                        {
                            dupItemIDs.Add(match.ItemID);
                        }
                    }

                    if (dupItemIDs.Count > 0 && dupItemIDs.Count <= 3)
                    {
                        var matches = string.Format("{0}: {1}", ticket.ItemID, string.Join(", ", dupItemIDs));
                        if (toConsole)
                        {
                            Console.WriteLine(matches);
                        }
                        w.WriteLine(matches);
                    }
                }

                sw.Stop();
                var avgTimePer = String.Format("Average Time (ms): {0}", (double)sw.Elapsed.TotalMilliseconds / topN);
                if (toConsole)
                {
                    Console.WriteLine();
                    Console.WriteLine(avgTimePer);
                }
                w.WriteLine();
                w.WriteLine(avgTimePer);

            }

            if (toConsole)
            {
                Console.ReadLine();
            }
        }