did some work on perms
This commit is contained in:
@@ -4,31 +4,43 @@
|
|||||||
|
|
||||||
#include "MakePermutationMatrix.h"
|
#include "MakePermutationMatrix.h"
|
||||||
|
|
||||||
std::vector<std::vector<City>> MakePermutationMatrix::MakePermutation(std::vector<City> source, int rank, int size){
|
#include <mpi.h>
|
||||||
std::vector<std::vector<City>> permutations;
|
|
||||||
|
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::sort(source.begin(), source.end());
|
||||||
std::vector<City> myPerm = source;
|
std::vector<City> myPerm = source;
|
||||||
std::vector<City> nextPerm = source;
|
std::vector<City> nextPerm = source;
|
||||||
auto offset = myPerm.size()/size;
|
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());
|
std::rotate(nextPerm.begin(), nextPerm.begin()+offset*(rank+1), nextPerm.end());
|
||||||
else
|
else
|
||||||
nextPerm = source;
|
nextPerm = source;
|
||||||
|
|
||||||
if(!rank)
|
if(rank)
|
||||||
std::rotate(myPerm.begin(), myPerm.begin()+offset*rank,myPerm.end());
|
std::rotate(myPerm.begin(), myPerm.begin()+offset*rank,myPerm.end());
|
||||||
|
|
||||||
do {
|
do {
|
||||||
std::vector<City> currentPerm;
|
double cost;
|
||||||
for (int i = 0; i < myPerm.size(); i++) {
|
for (int i = 1; i < myPerm.size(); i++) {
|
||||||
currentPerm.push_back(myPerm[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);
|
} 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
|
class MakePermutationMatrix
|
||||||
{
|
{
|
||||||
public:
|
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:
|
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
|
#endif // HW10_MAKEPERMUTATIONMATRIX_H
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "CostMatrixGenerator.h"
|
#include "CostMatrixGenerator.h"
|
||||||
//#import "MakePermutationMatrix.h"
|
#include "MakePermutationMatrix.h"
|
||||||
#include "ReadFromFile.h"
|
|
||||||
#include "ParseMatrixForMPI.h"
|
#include "ParseMatrixForMPI.h"
|
||||||
|
#include "ReadFromFile.h"
|
||||||
#include "mpi.h"
|
#include "mpi.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -13,51 +13,42 @@ int main(int argc, char* argv[])
|
|||||||
MPI_Init(&argc, &argv);
|
MPI_Init(&argc, &argv);
|
||||||
MPI_Comm_rank(MCW, &rank);
|
MPI_Comm_rank(MCW, &rank);
|
||||||
MPI_Comm_size(MCW, &size);
|
MPI_Comm_size(MCW, &size);
|
||||||
std::vector<std::vector<double>> matrix;
|
std::vector<std::vector<double>> costMatrix;
|
||||||
std::vector<City> cities;
|
std::vector<City> cities;
|
||||||
std::vector<double> flatMatrix;
|
std::vector<double> flatMatrix;
|
||||||
if (rank == 0) {
|
if (rank == 0) {
|
||||||
std::cout << "Reading in file" << std::endl;
|
std::cout << "Reading in file" << std::endl;
|
||||||
cities = ReadFromFile::ReadFile("../input");
|
cities = ReadFromFile::ReadFile("../input");
|
||||||
citiesSize = cities.size();
|
citiesSize = cities.size();
|
||||||
//Make matrix
|
//Make costMatrix
|
||||||
matrix = CostMatrixGenerator::GenerateCostMatrix(cities);
|
costMatrix = CostMatrixGenerator::GenerateCostMatrix(cities);
|
||||||
for(int i =0; i < cities.size(); i++){
|
for(int i =0; i < cities.size(); i++){
|
||||||
for(int j = 0; j< cities.size(); j++){
|
for(int j = 0; j< cities.size(); j++){
|
||||||
std::cout << matrix[i][j] << " ";
|
std::cout << costMatrix[i][j] << " ";
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
flatMatrix = matrixTools::FlattenMatrix(matrix);
|
flatMatrix = matrixTools::FlattenMatrix(costMatrix);
|
||||||
}
|
}
|
||||||
MPI_Bcast(&citiesSize, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
MPI_Bcast(&citiesSize, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
||||||
if(rank){
|
if(rank){
|
||||||
// reserve memory for vectors
|
// reserve memory for vectors
|
||||||
flatMatrix.resize(citiesSize*citiesSize);
|
flatMatrix.resize(citiesSize*citiesSize);
|
||||||
matrix.resize(citiesSize);
|
|
||||||
|
costMatrix.resize(citiesSize);
|
||||||
for(int i = 0; i < citiesSize; i++) {
|
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);
|
MPI_Bcast(&flatMatrix[0], flatMatrix.size(), MPI_DOUBLE, 0, MPI_COMM_WORLD);
|
||||||
if(rank){
|
if(rank){
|
||||||
std::cout <<"Flat Matrix Size :" << flatMatrix.size() << std::endl;
|
costMatrix = matrixTools::UnflattenMatrix(flatMatrix, citiesSize, citiesSize);
|
||||||
for(int i = 0; i < citiesSize*citiesSize; i++){
|
//Make permutations
|
||||||
std::cout << flatMatrix[i] << " ";
|
auto perms = MakePermutationMatrix::GetLowestCost(cities, rank, size, costMatrix);
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MPI_Finalize();
|
MPI_Finalize();
|
||||||
// //Make permutations
|
|
||||||
// auto perms = MakePermutationMatrix::GetLowestCost(cities, rank, size, matrix);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user