private static void MergeClusters(FuzzyModel fm) { List <FuzzyNodeCluster> clustersToRemove = new List <FuzzyNodeCluster>(); foreach (FuzzyNodeCluster fnc in fm.GetClusters()) { // are all predecessors clusters? bool allClustersIn = true; if (fnc.GetInEdges().Count == 0) { allClustersIn = false; } else { foreach (FuzzyEdge fe in fnc.GetInEdges()) { if (!fm.IsCluster(fe.GetFromNode())) { allClustersIn = false; break; } } } // if they are if (allClustersIn) { // find the most correlated float max = 0; FuzzyNodeCluster chosenOne = null; foreach (FuzzyEdge fe in fnc.GetInEdges()) { if ((fe.GetEndpointCorrelation() + fe.GetProximityCorrelation()) > max) { max = fe.GetEndpointCorrelation() + fe.GetProximityCorrelation(); chosenOne = fm.GetCluster(fe.GetFromNode()); } } // merge the clusters foreach (FuzzyNode fn in fnc.GetNodesInCluster()) { chosenOne.AddNodeToCluster(fn); } List <FuzzyEdge> toRemove = new List <FuzzyEdge>(); foreach (FuzzyEdge fe in fnc.GetInEdges()) { if (fe.Equals(fm.GetEdge(chosenOne, fnc))) { //RemoveEdge(fe); toRemove.Add(fe); } else { FuzzyEdge newFe = new FuzzyEdge(fe.GetFromNode(), chosenOne); //RemoveEdge(fe); toRemove.Add(fe); fm.AddEdge(newFe); } } foreach (FuzzyEdge fe in fnc.GetOutEdges()) { FuzzyEdge newFe = new FuzzyEdge(chosenOne, fe.GetToNode()); //RemoveEdge(fe); toRemove.Add(fe); fm.AddEdge(newFe); } foreach (FuzzyEdge fe in toRemove) { fm.RemoveEdge(fe); } //RemoveCluster(fnc); clustersToRemove.Add(fnc); } else { // are all successors clusters? bool allClustersOut = true; if (fnc.GetOutEdges().Count == 0) { allClustersOut = false; } else { foreach (FuzzyEdge fe in fnc.GetOutEdges()) { if (!fm.IsCluster(fe.GetToNode())) { allClustersOut = false; break; } } } // if they are if (allClustersOut) { // find the most correlated float max = 0; FuzzyNodeCluster chosenOne = null; foreach (FuzzyEdge fe in fnc.GetOutEdges()) { if ((fe.GetEndpointCorrelation() + fe.GetProximityCorrelation()) > max) { max = fe.GetEndpointCorrelation() + fe.GetProximityCorrelation(); chosenOne = fm.GetCluster(fe.GetToNode()); } } // merge the clusters foreach (FuzzyNode fn in fnc.GetNodesInCluster()) { chosenOne.AddNodeToCluster(fn); } List <FuzzyEdge> toRemove = new List <FuzzyEdge>(); foreach (FuzzyEdge fe in fnc.GetOutEdges()) { if (fe.Equals(fm.GetEdge(chosenOne, fnc))) { toRemove.Add(fe); } else { FuzzyEdge newFe = new FuzzyEdge(chosenOne, fe.GetToNode()); toRemove.Add(fe); fm.AddEdge(newFe); } } foreach (FuzzyEdge fe in fnc.GetInEdges()) { FuzzyEdge newFe = new FuzzyEdge(fe.GetFromNode(), chosenOne); toRemove.Add(fe); fm.AddEdge(newFe); } foreach (FuzzyEdge fe in toRemove) { fm.RemoveEdge(fe); } clustersToRemove.Add(fnc); } } } foreach (FuzzyNodeCluster fnc in clustersToRemove) { fm.RemoveCluster(fnc); } }
private static void BuildClusters(FuzzyModel fm, float nodeCutoff) { // TODO normalize significance // find victims HashSet <FuzzyNode> victims = new HashSet <FuzzyNode>(); foreach (FuzzyNode fn in fm.GetNodes()) { if (fn.GetFrequencySignificance() < nodeCutoff) { victims.Add(fn); } } // build clusters foreach (FuzzyNode fn in victims) { // find the most correlated neighbor float max = 0; FuzzyNode chosenOne = null; foreach (FuzzyEdge fe in fn.GetInEdges()) { if ((fe.GetEndpointCorrelation() + fe.GetProximityCorrelation()) > max) { max = fe.GetEndpointCorrelation() + fe.GetProximityCorrelation(); chosenOne = fe.GetFromNode(); } } foreach (FuzzyEdge fe in fn.GetOutEdges()) { if ((fe.GetEndpointCorrelation() + fe.GetProximityCorrelation()) > max) { max = fe.GetEndpointCorrelation() + fe.GetProximityCorrelation(); chosenOne = fe.GetToNode(); } } // if it is a cluster, then add the victim to it if (fm.IsCluster(fm.GetCluster(chosenOne))) { FuzzyNodeCluster fnc = fm.GetCluster(chosenOne); fnc.AddNodeToCluster(fn); List <FuzzyEdge> toRemove = new List <FuzzyEdge>(); List <FuzzyEdge> toAdd = new List <FuzzyEdge>(); foreach (FuzzyEdge fe in fn.GetInEdges()) { FuzzyEdge newFe = new FuzzyEdge(fe.GetFromNode(), fnc); toRemove.Add(fe); toAdd.Add(newFe); } foreach (FuzzyEdge fe in fn.GetOutEdges()) { FuzzyEdge newFe = new FuzzyEdge(fnc, fe.GetToNode()); toRemove.Add(fe); toAdd.Add(newFe); } foreach (FuzzyEdge fe in toRemove) { fm.RemoveEdge(fe); } foreach (FuzzyEdge fe in toAdd) { if (fe.GetFromNode() != fe.GetToNode()) { fm.AddEdge(fe); } } fm.RemoveNode(fn); } else { FuzzyNodeCluster fnc = new FuzzyNodeCluster(fn, "Cluster " + (fm.GetClusters().Count() + 1)); fm.AddCluster(fnc); //Console.WriteLine(fnc.GetLabel()); List <FuzzyEdge> toRemove = new List <FuzzyEdge>(); List <FuzzyEdge> toAdd = new List <FuzzyEdge>(); foreach (FuzzyEdge fe in fn.GetInEdges()) { FuzzyEdge newFe = new FuzzyEdge(fe.GetFromNode(), fnc); //Console.WriteLine(newFe.ToString()); toRemove.Add(fe); toAdd.Add(newFe); } foreach (FuzzyEdge fe in fn.GetOutEdges()) { FuzzyEdge newFe = new FuzzyEdge(fnc, fe.GetToNode()); //Console.WriteLine(newFe.ToString()); toRemove.Add(fe); toAdd.Add(newFe); } foreach (FuzzyEdge fe in toRemove) { fm.RemoveEdge(fe); } foreach (FuzzyEdge fe in toAdd) { fm.AddEdge(fe); } fm.RemoveNode(fn); } } }