Ejemplo n.º 1
0
		public static ClusterObject Create(string rawData)
		{
			ClusterObject newClusterObject = new ClusterObject();
			string[] coordList = rawData.Split(" ".ToCharArray());
			foreach (string coord in coordList)
			{
				newClusterObject.Coordinates.Add(Double.Parse(coord));
			}
			return newClusterObject;
		}
		public override double CalculateDistance(ClusterObject obj1, ClusterObject obj2)
		{
			double dDistance = 0;
			double dTemp = 0;
			for (int i = 0; i < obj1.Coordinates.Count; i++)
			{
				dTemp += Math.Pow((obj2.Coordinates[i] - obj1.Coordinates[i]), 2);
			}
			
			dDistance = Math.Sqrt(dTemp);

			return dDistance;
		}
Ejemplo n.º 3
0
		public void UpdateClusterMeans()
		{
			ClusterObject newCentroid = new ClusterObject();
			double[] newCoordinates = new double[m_nDimensionCount];
			
			foreach (KeyValuePair<int, ClusterObject> kvp in m_dictObjects)
			{
				for (int i = 0; i < m_nDimensionCount; i++)
				{
					newCoordinates[i] += kvp.Value.Coordinates[i];
				}
			}

			for (int i = 0; i < m_nDimensionCount; i++)
			{
				newCoordinates[i] /= m_dictObjects.Count;
			}

			newCentroid.Coordinates = new List<double>(newCoordinates);
			
			if (m_OldClusterCentroid == null)
			{
				m_OldClusterCentroid = newCentroid;
			}
			else
			{
				m_OldClusterCentroid = m_ClusterCentroid;
			}
			
			m_ClusterCentroid = newCentroid;
			
			EuclidianDistanceStrategy e = new EuclidianDistanceStrategy();
			double dDistance = e.CalculateDistance(m_OldClusterCentroid, m_ClusterCentroid);
			if (dDistance > 0) //TODO: Set an acceptable margin to determine if the 
							   // center moved or not. To aviod osciallation, small changes 
							   // may be neglected.
			{
				CenterMoved = true;
			}
			else
			{
				CenterMoved = false;
			}
		}
Ejemplo n.º 4
0
		public void AddObject(int rowId, ClusterObject newObject)
		{
			m_dictObjects.Add(rowId, newObject);
			m_nDimensionCount = newObject.Coordinates.Count;
		}
Ejemplo n.º 5
0
		public static double CalculateDistance(ClusterObject object1, ClusterObject object2)
		{
			EuclidianDistanceStrategy distance = new EuclidianDistanceStrategy();
			return distance.CalculateDistance(object1, object2);
		}
		abstract public double CalculateDistance(ClusterObject obj1, ClusterObject obj2);
		private Cluster FindClosestCluster(ClusterObject clusterObject)
		{
			Trace.WriteLineIf(SystemDefinitions.ErrSev.TraceVerbose, HelperMethods.GenerateLogLine("Find closest cluster to object : " + clusterObject.ToString()));
			
			double[] distances = new double[K];
			Cluster closestCluster = null;
			for (int i = 0; i < clusters.Length; i++)
			{
				distances[i] = ClusterObject.CalculateDistance(clusterObject, clusters[i].ClusterCentroid);
				Trace.WriteLineIf(SystemDefinitions.ErrSev.TraceVerbose, HelperMethods.GenerateLogLine(String.Format("Distance for cluster [{0}] = {1}", i, distances[i])));
			}
			
			int nSmallestDistanceIndex = 0;
			double dSmallestDistance = Double.MaxValue;
			for (int i = 0; i < distances.Length; i++)
			{
				if (distances[i] < dSmallestDistance)
				{
					dSmallestDistance = distances[i];
					nSmallestDistanceIndex = i;
				}
			}
			
			closestCluster = clusters[nSmallestDistanceIndex];
			Trace.WriteLineIf(SystemDefinitions.ErrSev.TraceVerbose, HelperMethods.GenerateLogLine(String.Format("Closest cluster : [{0}] ", nSmallestDistanceIndex)));
			return closestCluster;
		}
		public override double CalculateDistance(ClusterObject obj1, ClusterObject obj2)
		{
			throw new NotImplementedException();
		}