예제 #1
0
        public FeatureItem SimilarInOtherGroupsTo(FeatureItem item)
        {
            StringBuilder outputText         = new StringBuilder();
            double        tempItemSimilarSum = 0;
            double        itemSimilarSum     = 0;
            FeatureItem   itemSimilar        = null;
            Cluster       cluster            = null;

            ItemToClusterMap.TryGetValue(item.Id, out cluster);
            if (cluster == null)
            {
                //czy aby dodawac? w cluster powinny byc elementy, ktore mamy na FeatureItemList, inaczej moze powstac niespojnosc!!!
                //AssignCluster(item); //przedyskutowac z Darkiem
            }
            else
            {
                HyperCluster hyperCluster = ClusterToHyperClusterMap[cluster];
                for (int j = 0; j < hyperCluster.ClusterList.Count; j++)
                {
                    if (!ReferenceEquals(cluster, hyperCluster.ClusterList[j]))    //find in clusters different than item
                    {
                        List <FeatureItem> clusterItemList = hyperCluster.ClusterList[j].ClusterItemList;
                        for (int i = 0; i < clusterItemList.Count; i++)
                        {
                            tempItemSimilarSum = CaulculateVectorIntersectionMagnitude(item.FeatureVector, clusterItemList[i].FeatureVector) / CalculateVectorMagnitude(clusterItemList[i].FeatureVector);    //||item(Reference) and itemFromCluster||/ ||itemFromcluster|| => max ||
                            if (itemSimilarSum == 0 || itemSimilarSum < tempItemSimilarSum)
                            {
                                itemSimilarSum = tempItemSimilarSum;
                                itemSimilar    = clusterItemList[i];
                            }
                        }
                    }
                }

                if (itemSimilar != null)
                {
                    outputText.Append(" Most similiar taste in hyper cluster (other clusters) have item " + itemSimilar.Name + "\r\n\r\n");
                }
                else
                {
                    outputText.Append(" There is no simiilar item in hyper cluster (other clusters) " + item.Name + "\r\n\r\n");
                }
            }
            Debug.WriteLine(outputText.ToString());

            return(itemSimilar);
        }
예제 #2
0
        public void AssignHyperCluster()
        {
            int  iterationCounter     = IterationLimit; //assign IterationLimit
            bool isAssignementChanged = true;

            while (isAssignementChanged && iterationCounter > 0)
            {
                isAssignementChanged = false;
                for (int j = 0; j < ClusterList.Count; j++)
                {
                    List <KeyValuePair <HyperCluster, double> > hyperClusterToProximityList = new List <KeyValuePair <HyperCluster, double> >();
                    Cluster cluster = ClusterList[j];
                    double  clusterVectorMagnitude = CalculateVectorMagnitude(cluster.ClusterVector);
                    double  proximityThreshold     = clusterVectorMagnitude / (bValue + rangeLimit * FeatureItemSize); // ||C_j||/(b+1)

                    //Calculate proximity values for cluster and hyperClusters
                    for (int i = 0; i < HyperClusterList.Count; i++)
                    {
                        double hyperClusterVectorMagnitude = CalculateVectorMagnitude(HyperClusterList[i].HyperClusterVector);
                        double proximity = CaulculateVectorIntersectionMagnitude(cluster.ClusterVector, HyperClusterList[i].HyperClusterVector) / (bValue + hyperClusterVectorMagnitude); //prox = ||HC_i and C_j ||/ (b + ||HC_j||) > proxThres
                        if (proximity > proximityThreshold)
                        {
                            hyperClusterToProximityList.Add(new KeyValuePair <HyperCluster, double>(HyperClusterList[i], proximity));
                        }
                    }

                    if (hyperClusterToProximityList.Count > 0)
                    {
                        hyperClusterToProximityList.Sort((x, y) => - 1 * x.Value.CompareTo(y.Value));  //sorting in place in descending order

                        //search from the maximum proximity to smallest
                        for (int i = 0; i < hyperClusterToProximityList.Count; i++)
                        {
                            HyperCluster newHyperCluster = hyperClusterToProximityList[i].Key;
                            double       vigilance       = CaulculateVectorIntersectionMagnitude(newHyperCluster.HyperClusterVector, cluster.ClusterVector) / clusterVectorMagnitude; //(vig = || HC_i and C_j|| / ||C_j||) >= p
                            if (vigilance >= p2Value)                                                                                                                                 //passed all tests and has max proximity
                            {
                                if (ClusterToHyperClusterMap.ContainsKey(cluster))                                                                                                    //find cluster with this item
                                {
                                    HyperCluster previousHyperCluster = ClusterToHyperClusterMap[cluster];
                                    if (ReferenceEquals(newHyperCluster, previousHyperCluster))
                                    {
                                        break;                                                                //if the best is the same, then it will break (not considered others)
                                    }
                                    if (previousHyperCluster.RemoveClusterFromHyperCluster(cluster) == false) //the cluster is empty
                                    {
                                        HyperClusterList.Remove(previousHyperCluster);
                                    }
                                }
                                //Add item to the current hyperCluster
                                newHyperCluster.AddClusterToHyperCluster(cluster);
                                ClusterToHyperClusterMap[cluster] = newHyperCluster;
                                isAssignementChanged = true;

                                break;
                            }
                        }
                    }

                    if (ClusterToHyperClusterMap.ContainsKey(cluster) == false)
                    {
                        HyperCluster newHyperCluster = new HyperCluster(cluster);
                        HyperClusterList.Add(newHyperCluster);
                        ClusterToHyperClusterMap.Add(cluster, newHyperCluster);
                        isAssignementChanged = true;
                    }
                }

                iterationCounter--;
            }
        }