public void ClusteringTest() { List <ClusteredData> datanew = new List <ClusteredData>(); EuclidianDistance distance = new EuclidianDistance(); K_Means k_means = new K_Means(2, 0.1, distance); ClusterAnalysis clusterAnalysis = new ClusterAnalysis(k_means); datanew.Add(new ClusteredData(0, 0)); datanew.Add(new ClusteredData(0, 20)); datanew.Add(new ClusteredData(20, 0)); datanew.Add(new ClusteredData(20, 20)); var list = clusterAnalysis.Clustering(datanew); Assert.AreEqual(2, list.Count); var flag = from value in k_means.FinishesCentroids.Values where (value.X == 0 && value.Y == 0) || (value.X == 0 && value.Y == 20) || (value.X == 20 && value.Y == 20) || (value.X == 20 && value.Y == 0) || (value.X == 10 && value.Y == 20) || (value.X == 0 && value.Y == 10) || (value.X == 20 && value.Y == 10) || (value.X == 0 && value.Y == 10) select value; Assert.AreEqual(0.1, k_means.CoefficientTaboo); Assert.AreEqual(2, k_means.CountOfClusters); Assert.IsNotNull(flag); }
/// <summary> /// Converts the <see cref="sourceValue" /> parameter to the <see cref="destinationType" /> parameter using <see cref="formatProvider" /// /> and <see cref="ignoreCase" /> /// </summary> /// <param name="sourceValue">the value to convert into an instance of <see cref="ClusterAnalysis" />.</param> /// <returns> /// an instance of <see cref="ClusterAnalysis" />, or <c>null</c> if there is no suitable conversion. /// </returns> public static object ConvertFrom(dynamic sourceValue) { if (null == sourceValue) { return(null); } try { ClusterAnalysis.FromJsonString(typeof(string) == sourceValue.GetType() ? sourceValue : sourceValue.ToJsonString()); } catch { // Unable to use JSON pattern } try { return(new ClusterAnalysis { VmEfficiencyMap = sourceValue.VmEfficiencyMap, }); } catch { } return(null); }
public void ClusterAnalysisMain(int countCluster, double[] weight, bool normalization) { ClusterAnalysis clusterAnalysis = new ClusterAnalysis(); double[,] setForm = new double[30, 3] // Входное множество образов для кластеризации { { 15, 6, 15 }, { 1, 19, 0 }, { -8, -1, 4 }, { 13, 19, 15 }, { 15, 17, -14 }, { -3, 9, -35 }, { 12, 4, 16 }, { 8, 14, 9 }, { -6, 0, 5 }, { 11, 17, 10 }, { 12, 17, -10 }, { -1, 10, -25 }, { 18, 17, -11 }, { -4, 9, -31 }, { 19, 4, 13 }, { 8, 14, 10 }, { -6, -5, 1 }, { 20, 20, 20 }, { 7, 16, -17 }, { -1, 7, -26 }, { 15, 1, 10 }, { 0, 11, 8 }, { -8, -1, 5 }, { 10, 10, 10 }, { 12, 15, -10 }, { -4, 5, -27 }, { -7, -1, 4 }, { 3, 17, 11 }, { 0, 1, 3 }, { 7, 2, 0 } }; // Применение нормализации if (normalization) { setForm = GetNormineSetForm(setForm); } ClusterCalculationByTwoMethods(setForm, countCluster, weight); }
static void Main(string[] args) { List <ClusteredData> datanew = new List <ClusteredData>(); EuclidianDistance distance = new EuclidianDistance(); K_Means k_means = new K_Means(2, 0.1, distance); ClusterAnalysis clusterAnalysis = new ClusterAnalysis(k_means); datanew.Add(new ClusteredData(0, 0)); datanew.Add(new ClusteredData(0, 20)); datanew.Add(new ClusteredData(20, 0)); datanew.Add(new ClusteredData(20, 20)); var list = clusterAnalysis.Clustering(datanew); /* * List<ClusteredData> datanew = new List<ClusteredData>(); * List<K_Means> k_meansList = new List<K_Means>(); * List<List<Cluster>> clustersList = new List<List<Cluster>>(); * System.Diagnostics.Stopwatch myStopwatch = new System.Diagnostics.Stopwatch(); * EuclidianDistance distance = new EuclidianDistance(); * * datanew.Add(new ClusteredData(0,0)); * datanew.Add(new ClusteredData(0, 20)); * datanew.Add(new ClusteredData(20, 0)); * datanew.Add(new ClusteredData(20, 20)); * * var ewrt = datanew.Select(Er => Er.X); * var ety = ewrt.Average(); * * * * var e = datanew.Average(er => er.X); * * * myStopwatch.Start(); //запуск * for (var i = 0; i < 1000; i++) * { * K_Means k_means = new K_Means(2, 0.1, distance); * clustersList.Add(k_means.Clustering(datanew)); * k_meansList.Add(k_means); * Thread.Sleep(10); * * var flag = k_means.FinishesCentroids.ContainsValue(new Centroid(0, 0)); * var flag1 = k_means.FinishesCentroids.ContainsValue(new Centroid(0, 20)); * var flag2 = k_means.FinishesCentroids.ContainsValue(new Centroid(20, 20)); * var flag3 = k_means.FinishesCentroids.ContainsValue(new Centroid(20, 0)); * } * myStopwatch.Stop(); * * var fulltime = myStopwatch.ElapsedMilliseconds; * double Onetime = myStopwatch.ElapsedMilliseconds/1000.0; * * int a = 0; * int b = 0; * int c = 0; * int d = 0; * * foreach (var list in clustersList) * { * a += list.Count(q => q.Data.Count == 3); * b += list.Count(q => q.Data.Count == 2); * c += list.Count(q => q.Data.Count == 1); * d += list.Count(q => q.Data.Count == 0); * } * b = b / 2; * var ads = new Cluster(1, new Centroid(1, 10)); * //ads.IntraClusterDistance(); * * //// Поиск объектов класса Cluster в списке optimumClusters с максимальным внутрикластерным расcтоянием * //var nonOptimumClusters = from list in clustersList // определяем каждый объект из clusters как cluster * // where * // select cluster.IntraClusterDistance();// выбираем объект * */ }
private void TestForClusterAnalysis() { var common = new SQLCommon { SkylineType = new SkylineBNL() { Provider = Helper.ProviderName, ConnectionString = Helper.ConnectionString }, ShowInternalAttributes = true }; DbProviderFactory factory = DbProviderFactories.GetFactory(Helper.ProviderName); // use the factory object to create Data access objects. DbConnection connection = factory.CreateConnection(); // will return the connection object (i.e. SqlConnection ...) connection.ConnectionString = Helper.ConnectionString; var dt = new DataTable(); connection.Open(); DbDataAdapter dap = factory.CreateDataAdapter(); DbCommand selectCommand = connection.CreateCommand(); selectCommand.CommandTimeout = 0; //infinite timeout string strQuery; string operators; int numberOfRecords; string[] parameter; string ansiSql = common.GetAnsiSqlFromPrefSqlModel(common.GetPrefSqlModelFromPreferenceSql(_entireSkylineSql)); prefSQL.SQLParser.Helper.DetermineParameters(ansiSql, out parameter, out strQuery, out operators, out numberOfRecords); selectCommand.CommandText = strQuery; dap.SelectCommand = selectCommand; dt = new DataTable(); dap.Fill(dt); DataTable entireSkylineDataTable = common.ParseAndExecutePrefSQL(Helper.ConnectionString, Helper.ProviderName, _entireSkylineSql); int[] skylineAttributeColumns = SkylineSamplingHelper.GetSkylineAttributeColumns(entireSkylineDataTable); IReadOnlyDictionary <long, object[]> entireSkylineNormalized = prefSQL.SQLSkyline.Helper.GetDatabaseAccessibleByUniqueId(entireSkylineDataTable, 0); SkylineSamplingHelper.NormalizeColumns(entireSkylineNormalized, skylineAttributeColumns); DataTable sampleSkylineDataTable = common.ParseAndExecutePrefSQL(Helper.ConnectionString, Helper.ProviderName, _skylineSampleSql); IReadOnlyDictionary <long, object[]> sampleSkylineNormalized = prefSQL.SQLSkyline.Helper.GetDatabaseAccessibleByUniqueId(sampleSkylineDataTable, 0); SkylineSamplingHelper.NormalizeColumns(sampleSkylineNormalized, skylineAttributeColumns); for (var i = 0; i < skylineAttributeColumns.Length; i++) { dt.Columns.RemoveAt(0); } IReadOnlyDictionary <long, object[]> full = prefSQL.SQLSkyline.Helper.GetDatabaseAccessibleByUniqueId(dt, 0); SkylineSamplingHelper.NormalizeColumns(full, skylineAttributeColumns); ClusterAnalysis.CalcMedians(full, skylineAttributeColumns); IReadOnlyDictionary <BigInteger, List <IReadOnlyDictionary <long, object[]> > > entireBuckets = ClusterAnalysis.GetBuckets(entireSkylineNormalized, skylineAttributeColumns); IReadOnlyDictionary <BigInteger, List <IReadOnlyDictionary <long, object[]> > > sampleBuckets = ClusterAnalysis.GetBuckets(sampleSkylineNormalized, skylineAttributeColumns); //IReadOnlyDictionary<int, List<IReadOnlyDictionary<long, object[]>>> aggregatedEntireBuckets = // ClusterAnalysis.GetAggregatedBuckets(entireBuckets); //IReadOnlyDictionary<int, List<IReadOnlyDictionary<long, object[]>>> aggregatedSampleBuckets = // ClusterAnalysis.GetAggregatedBuckets(sampleBuckets); IReadOnlyDictionary <BigInteger, List <IReadOnlyDictionary <long, object[]> > > fullB = ClusterAnalysis.GetBuckets(full, skylineAttributeColumns); // IReadOnlyDictionary<int, List<IReadOnlyDictionary<long, object[]>>> aFullB = // ClusterAnalysis.GetAggregatedBuckets(fullB); IOrderedEnumerable <KeyValuePair <BigInteger, List <IReadOnlyDictionary <long, object[]> > > > sorted = fullB.OrderBy(l => l.Value.Count) .ThenBy(l => l.Key); int len = Convert.ToInt32(Math.Pow(2, skylineAttributeColumns.Length)); //for (var i = 0; i < len; i++) foreach (KeyValuePair <BigInteger, List <IReadOnlyDictionary <long, object[]> > > s in sorted) { BigInteger i = s.Key; int entire = entireBuckets.ContainsKey(i) ? entireBuckets[i].Count : 0; int sample = sampleBuckets.ContainsKey(i) ? sampleBuckets[i].Count : 0; double entirePercent = (double)entire / entireSkylineNormalized.Count; double samplePercent = (double)sample / sampleSkylineNormalized.Count; int fullX = fullB.ContainsKey(i) ? fullB[i].Count : 0; double fullP = (double)fullX / full.Count; Console.WriteLine("-- {0,5} -- {5,6} ({6,7:P2} %) -- {1,6} ({3,7:P2} %) -- {2,6} ({4,7:P2} %)", i, entire, sample, entirePercent, samplePercent, fullX, fullP); } Console.WriteLine(); Console.WriteLine("{0} - {1} - {2}", entireSkylineNormalized.Count, sampleSkylineNormalized.Count, full.Count); }