did some work on perms

This commit is contained in:
2020-03-29 23:55:52 -06:00
parent 182e3d1e18
commit 8732edf8f4
3 changed files with 42 additions and 39 deletions

View File

@@ -4,31 +4,43 @@
#include "MakePermutationMatrix.h"
std::vector<std::vector<City>> MakePermutationMatrix::MakePermutation(std::vector<City> source, int rank, int size){
std::vector<std::vector<City>> permutations;
#include <mpi.h>
std::vector<double> MakePermutationMatrix::MakePermutation(std::vector<City> source, int rank, int size, std::vector<std::vector<double>> costMatrix){
std::vector<double> returnVector;
std::vector<double> localCostVector;
std::sort(source.begin(), source.end());
std::vector<City> myPerm = source;
std::vector<City> nextPerm = source;
auto offset = myPerm.size()/size;
if(!rank)
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)
if(rank)
std::rotate(myPerm.begin(), myPerm.begin()+offset*rank,myPerm.end());
do {
std::vector<City> currentPerm;
for (int i = 0; i < myPerm.size(); i++) {
currentPerm.push_back(myPerm[i]);
double cost;
for (int i = 1; i < myPerm.size(); i++) {
cost += costMatrix[myPerm[i].GetName()][myPerm[i-1].GetName()];
}
permutations.push_back(currentPerm);
localCostVector.push_back(cost);
} while (std::next_permutation(myPerm.begin(), myPerm.end()), myPerm != nextPerm);
return permutations;
auto lowest = std::vector<double>(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<std::vector<City>> MakePermutationMatrix::GetLowestCost(std::vector<City> source, int rank, int size, std::vector<std::vector<double>> costMatrix){
std::vector<double> MakePermutationMatrix::GetLowestCost(std::vector<City> source, int rank, int size, std::vector<std::vector<double>> costMatrix){
return MakePermutation(source, rank, size, costMatrix);
}