/** * @brief AgentCluster::assignmentPhase Runs the assignment phase of the AgentSwarm algorithm. */ void AgentCluster::assignmentPhase() { for (unsigned int i = 0; i < m_agents.size(); i++) { Agent* agent = m_agents[i]; if (agent->visited) continue; agent->visited = true; Cluster* cluster = new Cluster(); cluster->id = m_clusters.size(); m_clusters.push_back(cluster); std::vector<Agent*> neighbors = agentsWithinRange(agent, agent->foragingRange * 2.0); addToCluster(cluster, agent, neighbors); } for (unsigned int i = 0; i < m_clusters.size(); i++) { Cluster* cluster = m_clusters[i]; for (unsigned int j = 0; j < cluster->agents.size(); j++) { Agent* agent = cluster->agents[j]; std::vector<ClusterItem*> items = dataWithinForagingRange(agent); for (unsigned int k = 0; k < items.size(); k++) { ClusterItem* item = items[k]; if (item->group != -1) continue; item->group = cluster->id; cluster->points.push_back(item); } } } for (unsigned int i = 0; i < m_data.size(); i++) { //assign unassigned points to the closest groups ClusterItem* item = m_data[i]; if (item->group != -1) continue; Agent* closestAgent = 0; double closestDistance = -1; for (unsigned int j = 0; j < m_agents.size(); j++) { Agent* current = m_agents[j]; double dist = pointDistance(current->x, item->x, current->y, item->y); if (dist < closestDistance || closestDistance == -1) { closestAgent = current; closestDistance = dist; } } item->group = closestAgent->cluster; for (unsigned int j = 0; j < m_clusters.size(); j++) { Cluster* cluster = m_clusters[j]; if (cluster->id == item->group) cluster->points.push_back(item); } } emit setClusters(&m_clusters); }
/******************************************************************** * Main SCAN algorithm ********************************************************************/ void Scan::run(const double epsilon, const int mi) { // All vertices begin unclassified // So let's begin. We will iterate the labels list. In no moment // can a label became "unclassified" again, so this loop will // do the trick. hmap::iterator node; uint cnt = 0; for (node = g.graph_map.begin(); node != g.graph_map.end(); ++node) { // Making sure this node was not yet evaluated if (getClusterLabel(node->first) == -1) { // Is it a core? if (isCore(node->first, epsilon, mi)) { //std::cout << node->first << " is a core!\n"; // Will begin a new cluster int cid = getNewClusterID(); // Put all the e-neighborhood of the node in a queue std::set<Edge> x; x = neighborhood(node->first, epsilon); std::queue<uint> q; std::set<Edge>::iterator it; //std::cout << node->first << " nhood is:"; for (it = x.begin(); it != x.end(); ++it) { //std::cout << " " << it->getNode() << std::endl; q.push(it->getNode()); } while (!q.empty()) { uint y = q.front(); q.pop(); std::set<Edge> r; r = dirReach(y, epsilon, mi); std::set<Edge>::iterator setIt; for (setIt = r.begin(); setIt != r.end(); ++setIt) { // If the node is unclassified if (getClusterLabel(setIt->getNode()) == -1) { addToCluster(setIt->getNode(), cid); q.push(setIt->getNode()); } // If the node is a non-member of the cluster if (getClusterLabel(setIt->getNode()) == 0) { addToCluster(setIt->getNode(), cid); } } } } else { // Not a Core, so it will be labeled as non-member (0) setClusterLabel(node->first, 0); } } else { // Node already evaluated. Move along. continue; } } // Further classifies non-members hmap_ii::iterator nmnode; for (nmnode = cluster_label.begin(); nmnode != cluster_label.end(); ++nmnode) { if (nmnode->second == 0) { std::set<uint> tmp; tmp = getNeighborClusters(nmnode->first); if (tmp.size() > 1) { addHub(nmnode->first, tmp); } else { std::pair<uint, uint> o(nmnode->first,*tmp.begin()); outliers.push_back(o); } } } std::cout << "SCAN finished."; std::cout << std::endl; }