/** * Returns a human-readable representation of a machine. * * @param machineId Machine's id. * @return Machine's representation for debugging purposes. */ std::string toString ( uint machineId ) const { assert ( exists ( machineId ) ); std::string output; output += " #" ; output += boost::lexical_cast<std::string> ( machineId ) ; output += ": n"; output += boost::lexical_cast<std::string> ( getNeighborhood ( machineId ) ) ; output += ", l"; output += boost::lexical_cast<std::string> ( getLocation ( machineId ) ) ; output += "\n"; output += " capacities : " ; for ( uint r = 0 ; r < _resources.size(); ++r ) { output += "r" ; output += boost::lexical_cast<std::string> ( r ); output += ":" ; output += boost::lexical_cast<std::string> ( getCapacity ( machineId, r ) ) ; output += " "; } output += "\n"; output += " saf. capacities : " ; for ( uint r = 0 ; r < _resources.size(); ++r ) { output += "r" ; output += boost::lexical_cast<std::string> ( r ); output += ":"; output += boost::lexical_cast<std::string> ( getSafetyCapacity ( machineId, r ) ) ; output += " "; } output += "\n"; output += " mov. cost : " ; for ( uint m = 0 ; m < size(); ++m ) { output += "m" ; output += boost::lexical_cast<std::string> ( m ); output += ":"; output += boost::lexical_cast<std::string> ( getMovingCost ( machineId, m ) ) ; output += " "; } output += "\n"; return output ; }
// Zhu et al. "A Rank-Order Distance based Clustering Algorithm for Face Tagging", CVPR 2011 br::Clusters br::ClusterGallery(const QStringList &simmats, float aggressiveness, const QString &csv) { qDebug("Clustering %d simmat(s)", simmats.size()); // Read in gallery parts, keeping top neighbors of each template Neighborhood neighborhood = getNeighborhood(simmats); const int cutoff = neighborhood.first().size(); const float threshold = 3*cutoff/4 * aggressiveness/5; // Initialize clusters Clusters clusters(neighborhood.size()); for (int i=0; i<neighborhood.size(); i++) clusters[i].append(i); bool done = false; while (!done) { // nextClusterIds[i] = j means that cluster i is set to merge into cluster j QVector<int> nextClusterIDs(neighborhood.size()); for (int i=0; i<neighborhood.size(); i++) nextClusterIDs[i] = i; // For each cluster for (int clusterID=0; clusterID<neighborhood.size(); clusterID++) { const Neighbors &neighbors = neighborhood[clusterID]; int nextClusterID = nextClusterIDs[clusterID]; // Check its neighbors foreach (const Neighbor &neighbor, neighbors) { int neighborID = neighbor.first; int nextNeighborID = nextClusterIDs[neighborID]; // Don't bother if they have already merged if (nextNeighborID == nextClusterID) continue; // Flag for merge if similar enough if (normalizedROD(neighborhood, clusterID, neighborID) < threshold) { if (nextClusterID < nextNeighborID) nextClusterIDs[neighborID] = nextClusterID; else nextClusterIDs[clusterID] = nextNeighborID; } } } // Transitive merge for (int i=0; i<neighborhood.size(); i++) { int nextClusterID = i; while (nextClusterID != nextClusterIDs[nextClusterID]) { assert(nextClusterIDs[nextClusterID] < nextClusterID); nextClusterID = nextClusterIDs[nextClusterID]; } nextClusterIDs[i] = nextClusterID; } // Construct new clusters QHash<int, int> clusterIDLUT; QList<int> allClusterIDs = QSet<int>::fromList(nextClusterIDs.toList()).values(); for (int i=0; i<neighborhood.size(); i++) clusterIDLUT[i] = allClusterIDs.indexOf(nextClusterIDs[i]); Clusters newClusters(allClusterIDs.size()); Neighborhood newNeighborhood(allClusterIDs.size()); for (int i=0; i<neighborhood.size(); i++) { int newID = clusterIDLUT[i]; newClusters[newID].append(clusters[i]); newNeighborhood[newID].append(neighborhood[i]); } // Update indices and trim for (int i=0; i<newNeighborhood.size(); i++) { Neighbors &neighbors = newNeighborhood[i]; int size = qMin(neighbors.size(),cutoff); std::partial_sort(neighbors.begin(), neighbors.begin()+size, neighbors.end(), compareNeighbors); for (int j=0; j<size; j++) neighbors[j].first = clusterIDLUT[j]; neighbors = neighbors.mid(0, cutoff); } // Update results done = true; //(newClusters.size() >= clusters.size()); clusters = newClusters; neighborhood = newNeighborhood; }
std::vector<SDL_Rect> getPath(const SDL_Rect& start, const SDL_Rect& goal, const TileMap& map, const std::vector<SDL_Rect> colliders) { TileNode initialNode; initialNode.rect = start; initialNode.parent = nullptr; initialNode.g = 0; initialNode.h = manhattanHeuristic(start, goal, map) * 10; // std::vector<TileNode> openList { initialNode }; std::vector<TileNode> openList; openList.reserve(map.h * map.w); openList.push_back(initialNode); std::vector<TileNode> closedList; closedList.reserve(map.h * map.w); std::vector<SDL_Rect> path; bool done = false; while (!done) { auto currentIt = std::min_element(openList.begin(), openList.end()); if (currentIt == openList.end()) { std::cout << "DEU PAU" << std::endl; for (auto& e : openList) { std::cout << e.rect.x << "," << e.rect.y << std::endl; } return path; } else { } closedList.push_back(*currentIt); TileNode& current = closedList.back(); SDL_Rect currentRect = current.rect; int currentG = current.g; if ( SDL_RectEquals(¤tRect, &goal) ) { path.push_back(goal); TileNode *parent = current.parent; while (parent) { path.push_back(parent->rect); parent = parent->parent; } done = true; break; } auto new_end = std::remove(openList.begin(), openList.end(), current); openList.erase(new_end, openList.end()); // std::cout << std::endl << "ATUAL: " << currentRect.x << "," << currentRect.y << std::endl; auto neighbourhood = getNeighborhood(current, goal, map, colliders); for (auto& neighbour : neighbourhood) { // std::cout << "VIZINHO: " << neighbour.rect.x << "," << neighbour.rect.y << std::endl; if (std::find(closedList.begin(), closedList.end(), neighbour) != closedList.end()) continue; if (std::find(openList.begin(), openList.end(), neighbour) != openList.end()) { int difX = ceil((neighbour.rect.x - currentRect.x) / map.tileW); int difY = ceil((neighbour.rect.y - currentRect.y) / map.tileH); bool diagonal = difX != 0 && difY != 0; int score = currentG + (diagonal ? 14 : 10); if (score < neighbour.g) { neighbour.parent = ¤t; } } else { // if (clearance(neighbour.rect, map, colliders) > 1 || SDL_RectEquals(&neighbour.rect, &goal)) { openList.push_back(neighbour); } } } } return path; }
void Query::mapNodesHelp(vector<Neighborhood>& nbs, HashGraph* Gdb, OrthologInfoList* pOrthinfolist_db, vector<int>& Vq, vector<int>& Vdb, GraphMatch* gm, Queue& Q, hash_set<int>& nodesInQ, vector<bool>& mark, vector<bool>& dbmark) { vector<Neighborhood> dbnbh; bool* qvisit=new bool[Vq.size()]; bool* dbvisit=new bool[Vdb.size()]; for(unsigned int j=0; j<Vdb.size(); j++) { dbvisit[j]=false; Neighborhood nbh; nbh.degree=Gdb->degree(Vdb[j]); getNeighborhood(Gdb, (*pOrthinfolist_db), Vdb[j], nbh); dbnbh.push_back(nbh); } vector<MappingIndex> mcand; for(unsigned int i=0; i<Vq.size(); i++) { qvisit[i]=false; Neighborhood& nbh=nbs[Vq[i]]; for(unsigned int j=0; j<Vdb.size(); j++) { MappingIndex m; m.indexv=i; m.indexw=j; if( getScore(nbh, dbnbh[j], m.score) ) mcand.push_back(m); } } std::sort(mcand.begin(), mcand.end(), orderMappingIndexByScore); for(unsigned int i=0; i<mcand.size(); i++) { int vi=mcand[i].indexv; int wi=mcand[i].indexw; float score=mcand[i].score; if(!qvisit[vi] && !dbvisit[wi]) { NodeMapping nm; nm.source=Vq[vi]; nm.target=Vdb[wi]; if(nodesInQ.find(Vq[vi])==nodesInQ.end())//Vq[vi] has no mapping in Q) { qvisit[vi]=true; dbvisit[wi]=true; Q.insert(nm); nodesInQ.insert(nm.source); dbmark[nm.target]=true; }else { //find the mapping in Q Queue::iterator p; for(p=Q.begin(); p!=Q.end(); p++) if(p->source==Vq[vi]) break; if( score < p->score ) { qvisit[vi]=true; dbvisit[wi]=true; dbmark[p->target]=false; Q.erase(p);//edge tm= remove the mapping of m.source from Q Q.insert(nm); //nodesInQ.insert(nm.source); dbmark[nm.target]=true; }else { qvisit[vi]=true; } } } } delete [] dbvisit; delete [] qvisit; }