did some work on perms
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -11,10 +11,10 @@
|
||||
class MakePermutationMatrix
|
||||
{
|
||||
public:
|
||||
static std::vector<std::vector<City>> GetLowestCost(std::vector<City> source, int rank, int size, std::vector<std::vector<double>> costMatrix);
|
||||
static std::vector<double> GetLowestCost(std::vector<City> source, int rank, int size, std::vector<std::vector<double>> costMatrix);
|
||||
|
||||
private:
|
||||
static std::vector<std::vector<City>> MakePermutation(std::vector<City> source, int rank, int size);
|
||||
static std::vector<double> MakePermutation(std::vector<City> source, int rank, int size, std::vector<std::vector<double>> costMatrix);
|
||||
};
|
||||
|
||||
#endif // HW10_MAKEPERMUTATIONMATRIX_H
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "CostMatrixGenerator.h"
|
||||
//#import "MakePermutationMatrix.h"
|
||||
#include "ReadFromFile.h"
|
||||
#include "MakePermutationMatrix.h"
|
||||
#include "ParseMatrixForMPI.h"
|
||||
#include "ReadFromFile.h"
|
||||
#include "mpi.h"
|
||||
|
||||
#include <iostream>
|
||||
@@ -13,51 +13,42 @@ int main(int argc, char* argv[])
|
||||
MPI_Init(&argc, &argv);
|
||||
MPI_Comm_rank(MCW, &rank);
|
||||
MPI_Comm_size(MCW, &size);
|
||||
std::vector<std::vector<double>> matrix;
|
||||
std::vector<std::vector<double>> costMatrix;
|
||||
std::vector<City> cities;
|
||||
std::vector<double> flatMatrix;
|
||||
if (rank == 0) {
|
||||
std::cout << "Reading in file" << std::endl;
|
||||
cities = ReadFromFile::ReadFile("../input");
|
||||
citiesSize = cities.size();
|
||||
//Make matrix
|
||||
matrix = CostMatrixGenerator::GenerateCostMatrix(cities);
|
||||
//Make costMatrix
|
||||
costMatrix = CostMatrixGenerator::GenerateCostMatrix(cities);
|
||||
for(int i =0; i < cities.size(); i++){
|
||||
for(int j = 0; j< cities.size(); j++){
|
||||
std::cout << matrix[i][j] << " ";
|
||||
std::cout << costMatrix[i][j] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
flatMatrix = matrixTools::FlattenMatrix(matrix);
|
||||
flatMatrix = matrixTools::FlattenMatrix(costMatrix);
|
||||
}
|
||||
MPI_Bcast(&citiesSize, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
||||
if(rank){
|
||||
// reserve memory for vectors
|
||||
flatMatrix.resize(citiesSize*citiesSize);
|
||||
matrix.resize(citiesSize);
|
||||
|
||||
costMatrix.resize(citiesSize);
|
||||
for(int i = 0; i < citiesSize; i++) {
|
||||
matrix[i].resize(citiesSize);
|
||||
costMatrix[i].resize(citiesSize);
|
||||
}
|
||||
}
|
||||
MPI_Bcast(&flatMatrix[0], flatMatrix.size(), MPI_DOUBLE, 0, MPI_COMM_WORLD);
|
||||
if(rank){
|
||||
std::cout <<"Flat Matrix Size :" << flatMatrix.size() << std::endl;
|
||||
for(int i = 0; i < citiesSize*citiesSize; i++){
|
||||
std::cout << flatMatrix[i] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
matrix = matrixTools::UnflattenMatrix(flatMatrix, citiesSize, citiesSize);
|
||||
std::cout << "after unflatten" << std::endl;
|
||||
std::cout << "Process "<< rank << " has value " << citiesSize << " as size of city" << std::endl;
|
||||
for(int i = 0; i < citiesSize; i++) {
|
||||
for (int j = 0; j < citiesSize; j++) {
|
||||
std::cout << "Process " << rank << " has value " << matrix[i][j] << " at postion"
|
||||
<< "(" << i << "," << j << ")" << std::endl;
|
||||
}
|
||||
}
|
||||
costMatrix = matrixTools::UnflattenMatrix(flatMatrix, citiesSize, citiesSize);
|
||||
//Make permutations
|
||||
auto perms = MakePermutationMatrix::GetLowestCost(cities, rank, size, costMatrix);
|
||||
|
||||
}
|
||||
|
||||
MPI_Finalize();
|
||||
// //Make permutations
|
||||
// auto perms = MakePermutationMatrix::GetLowestCost(cities, rank, size, matrix);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user