// // Created by Brady Bodily on 3/28/20. // #include "MakePermutationMatrix.h" #include std::vector MakePermutationMatrix::MakePermutation(std::vector source, int rank, int size, std::vector> costMatrix){ std::vector returnVector; std::vector localCostVector; std::sort(source.begin(), source.end()); std::vector myPerm = source; std::vector nextPerm = source; auto offset = myPerm.size()/size; if(!rank){ returnVector.resize(source.size(), 0); } if(rank) std::rotate(nextPerm.begin(), nextPerm.begin()+offset*(rank+1), nextPerm.end()); else nextPerm = source; if(rank) std::rotate(myPerm.begin(), myPerm.begin()+offset*rank,myPerm.end()); do { double cost; for (int i = 1; i < myPerm.size(); i++) { cost += costMatrix[myPerm[i].GetName()][myPerm[i-1].GetName()]; } localCostVector.push_back(cost); } while (std::next_permutation(myPerm.begin(), myPerm.end()), myPerm != nextPerm); auto lowest = std::vector(1,localCostVector[0]); for(int i = 0; i < localCostVector.size(); i++) { if(localCostVector[i] < lowest[0]) lowest[0] = localCostVector[i]; } MPI_Gather(&localCostVector,1, MPI_DOUBLE, &returnVector,1, MPI_DOUBLE,0, MPI_COMM_WORLD); return returnVector; } std::vector MakePermutationMatrix::GetLowestCost(std::vector source, int rank, int size, std::vector> costMatrix){ return MakePermutation(source, rank, size, costMatrix); }