コード例 #1
0
        public static List <Facility> Cluster(string outputFile, double param1, double param2)
        {
            IClustering fac = new MShift();

            // add some properties to particular clustering algorithms
            Dictionary <string, object> properties = new Dictionary <string, object>();

            properties.Add("boundingBox", BoundingCluster);
            properties.Add("PARAM_1", param1);
            properties.Add("PARAM_2", param2);

            fac.SetProperties(properties);
            fac.ComputeClustering(VerticesToCluster);

            // save clustering result
            List <Facility> facilities = fac.GetFacilities();

            // ClusterSolution.SaveClusteringSolution(fac, inputFile, Dialog_SaveCluster.FileName, facilities);
            ClusterSolution.SaveClusteringToMeshLab(fac, outputFile, facilities, VerticesToCluster);

            return(facilities);
        }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: OKaas/cluster-analyzer
        private void Menu_Grouping_Click(object sender, EventArgs e)
        {
            showClusterGroups = true;

            double param1 = (TextBox_Param1.Text.Length > 0) ?
                            double.Parse(TextBox_Param1.Text) : 0;
            double param2 = (TextBox_Param2.Text.Length > 0) ?
                            double.Parse(TextBox_Param2.Text) : 0;

            // Array.Copy(verticesOriginal, vertices, verticesOriginal.Length);
            // facilities = new List<Facility>(facilitiesOriginal);

            //bool[] valid = new bool[facilities.Count];
            //for (int i = 0; i < valid.Length; ++i) { valid[i] = true; }

            //SortedList<double, Tuple<int, int>> facAgregation = new SortedList<double, Tuple<int, int>>();

            //// 1/2 agregation
            //while (valid.Length - 500 < facilities.Count )
            //{
            //    Console.WriteLine("Facilities: {0}", facilities.Count);

            //    int bestI = -1;
            //    int bestJ = -1;
            //    double bestMatch = double.PositiveInfinity;

            //    Parallel.ForEach(facilities, (facA, state, i) =>
            //    {
            //        double best = double.PositiveInfinity;
            //        int index = -1;

            //        for (int j = 0; j < facilities.Count; ++j)
            //        {
            //            // dont judge me, it's PoC!
            //            if (i != j && valid[j])
            //            {
            //                Facility facB = facilities[j];

            //                // calculate distance
            //                double dist = vertices[facA.VertexIndex].WeightedDistance(vertices[facB.VertexIndex]);

            //                if (best > dist)
            //                {
            //                    best = dist;
            //                    index = j;
            //                }
            //            }
            //        }

            //        if ( bestMatch > best )
            //        {
            //            bestMatch = best;
            //            bestI = (int)i;
            //            bestJ = index;
            //        }
            //    });

            //    //for (int i = 0; i < facilities.Count; ++i)
            //    //{
            //    //    Facility facA = facilities[i];

            //    //    double best = double.PositiveInfinity;
            //    //    int index = -1;

            //    //    for (int j = 0; j < facilities.Count; ++j)
            //    //    {
            //    //        // dont judge me, it's PoC!
            //    //        if (i != j && valid[j])
            //    //        {
            //    //            Facility facB = facilities[j];

            //    //            // calculate distance
            //    //            double dist = vertices[facA.VertexIndex].WeightedDistance(vertices[facB.VertexIndex]);

            //    //            if (best > dist)
            //    //            {
            //    //                best = dist;
            //    //                index = j;
            //    //            }
            //    //        }
            //    //    }

            //    //    if ( bestMatch > best )
            //    //    {
            //    //        bestMatch = best;
            //    //        bestI = i;
            //    //        bestJ = index;
            //    //    }
            //    //}

            //    if (0 <= bestI && 0 <= bestJ && bestI <= valid.Length - 1 && bestJ <= valid.Length - 1)
            //    {
            //        foreach (int clB in facilities[bestJ].VertexIndices)
            //        {
            //            // add all B clients to A facility -> A is greedy!
            //            // dummy distance
            //            vertices[clB].AssignToFacility(facilities[bestI], 0);
            //            facilities[bestI].AddVertex(clB, 0);
            //        }

            //        facilities[bestJ].RemoveAllClient();

            //        facilities.RemoveAt(bestJ);

            //    }
            //    else
            //    {
            //        break;
            //    }

            //}



            // List<Facility> newFacilities = new List<Facility>();

            //for (int i = 0; i < facilities.Count; ++i)
            //{
            //    // can be taken
            //    // dont judge me, it's PoC!
            //    if ( valid[i] )
            //    {
            //        Facility facA = facilities[i];

            //        double best = double.PositiveInfinity;
            //        int index = -1;

            //        for (int j = 0; j < facilities.Count; ++j)
            //        {
            //            // dont judge me, it's PoC!
            //            if ( i != j && valid[j] )
            //            {
            //                Facility facB = facilities[j];

            //                // calculate distance
            //                double dist = vertices[facA.VertexIndex].WeightedDistance( vertices[facB.VertexIndex] );

            //                if ( best > dist )
            //                {
            //                    best = dist;
            //                    index = j;
            //                }
            //            }
            //        }

            //        if ( 0 <= index && index <= valid.Length - 1)
            //        {
            //            valid[index] = false;

            //            foreach (int clB in facilities[index].VertexIndices)
            //            {
            //                // add all B clients to A facility -> A is greedy!
            //                // dummy distance
            //                vertices[clB].AssignToFacility(facA, 0);
            //                facA.AddVertex(clB, 0);
            //            }

            //            facilities[index].RemoveAllClient();
            //        } else
            //        {
            //            break;
            //        }
            //    }
            //}

            // foreach facilities which has valid flag
            //for (int j = facilities.Count - 1; j >= 0; --j)
            //{
            //    if ( !valid[j] )
            //    {
            //        facilities.RemoveAt(j);
            //    } else
            //    {
            //        // calculate middle of cluster
            //    }
            //}

            //showClusterGroups = true;
            //convexHull.Clear();

            //Console.WriteLine("Facilities: {0}", facilities.Count);

            //// it's time to calculate convex hull of clusters
            //foreach (Facility fac in facilities)
            //{
            //    List<Vertex> points = new List<Vertex>();
            //    foreach (int i in fac.VertexIndices)
            //    {
            //        points.Add(verticesOriginal[i]);
            //    }

            //    if ( points.Count > 3)
            //    {
            //        convexHull.Add(ConvexHull.MakeConvexHull(points));
            //    }
            //}

            //Canvas.Invalidate();

            IClustering fac = new MShift();
            // IClustering fac = new KMeans();
            // IClustering fac = new CMeans();
            // IClustering fac = new FacilityLocation();

            // add some properties to particular clustering algorithms
            Dictionary <string, object> properties = new Dictionary <string, object>();

            properties.Add("boundingBox", bBox);

            // properties.Add("numberOfClusters", int.Parse(TextBox_Param1.Text));

            properties.Add("BANDWIDTH", double.Parse(TextBox_Param1.Text));
            properties.Add("ITERATION", double.Parse(TextBox_Param2.Text));
            //properties.Add("treshold", 0.0);
            //properties.Add("weights", new double[] { 1, 1, 1, 1, 1, 1, 1, 1 });

            fac.SetProperties(properties);

            // transform facilities to vertices
            Vertex[] ret = new Vertex[facilities.Count];

            for (int i = 0; i < facilities.Count; ++i)
            {
                ret[i] = new Vertex(vertices[facilities[i].VertexIndex].Coords);
            }

            vertices = ret;

            fac.ComputeClustering(vertices);

            showPoints = false;

            // save clustering result
            facilities = fac.GetFacilities();

            ShowCalculateClusterConvexHull();

            CalculateFacilitiesPosition();

            //Graph.Draw();

            //if (Dialog_SaveCluster.ShowDialog() == DialogResult.OK)
            //{
            //    ClusterSolution.SaveClusteringSolution(fac, inputFile, Dialog_SaveCluster.FileName, facilities);
            //}
        }
コード例 #3
0
ファイル: MainForm.cs プロジェクト: OKaas/cluster-analyzer
        private void Main_Cluster_Click(object sender, EventArgs e)
        {
            //int numberOfClusters = 9;
            //Vertex[] extendedVertices = new Vertex[vertices.Length + numberOfClusters];
            //for (int i = 0; i < vertices.Length; i++)
            //{
            //    extendedVertices[i] = vertices[i];
            //}
            //vertices = extendedVertices;
            // IClustering fac = new MShift();
            // IClustering fac = new KMeans();
            // IClustering fac = new CMeans();
            IClustering fac = new MShift();

            // add some properties to particular clustering algorithms
            Dictionary <string, object> properties = new Dictionary <string, object>();

            properties.Add("boundingBox", bBoxToCluster);
            properties.Add("PARAM_1", double.Parse(TextBox_Param1.Text));
            properties.Add("PARAM_2", double.Parse(TextBox_Param2.Text));
            // properties.Add("weights", new double[] { 1, 1, 1, 1, 1, 1, 1, 1 });

            fac.SetProperties(properties);

            // cluster again!
            //if ( facilities != null && facilities.Count > 0 )
            //{
            //    Vertex[] ret = new Vertex[facilities.Count];

            //    int index = -1;
            //    foreach (Facility f in facilities)
            //    {
            //        ret[++index] = new Vertex(vertices[f.VertexIndex].Coords);
            //    }

            //    Array.Clear(vertices, 0, vertices.Length);
            //    vertices = ret;
            //}

            fac.ComputeClustering(verticesToCluster);

            showPoints = false;

            // save clustering result
            facilities = fac.GetFacilities();

            ShowCalculateClusterConvexHull();

            CalculateFacilitiesPosition();

            Graph.Draw();


            if (Dialog_SaveCluster.ShowDialog() == DialogResult.OK)
            {
                // ClusterSolution.SaveClusteringSolution(fac, inputFile, Dialog_SaveCluster.FileName, facilities);
                ClusterSolution.SaveClusteringToMeshLab(fac, Dialog_SaveCluster.FileName, facilities, verticesToCluster);
            }

            // show debug window
            // new Statistics(bBox, fac.GetFacilities(), vertices);
        }