Skip to content

Commit

Permalink
murtys method
Browse files Browse the repository at this point in the history
  • Loading branch information
EirikKolas committed Apr 21, 2024
1 parent ce2b022 commit 10823cc
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions vortex-filtering/src/utils/algorithms/murtys_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,22 @@

namespace vortex::utils {

std::vector<std::pair<double, Eigen::VectorXi>> murtys_method(const Eigen::MatrixXd &cost_matrix, int num_solutions, auto assignment_solver)
using CostValuePair = std::pair<double, Eigen::VectorXi>;

std::vector<CostValuePair> murtys_method(const Eigen::MatrixXd &cost_matrix, int num_solutions, auto assignment_solver)
{
std::vector<std::pair<double, Eigen::VectorXi>> R; // Solution set R
std::set<Eigen::VectorXi> unique_solutions; // To avoid duplicate solutions
auto comp = [](const std::pair<double, Eigen::MatrixXd> &a, const std::pair<double, Eigen::MatrixXd> &b) { return a.first < b.first; };
std::priority_queue<std::pair<double, Eigen::MatrixXd>, std::vector<std::pair<double, Eigen::MatrixXd>>, decltype(comp)> Q(comp);
std::vector<CostValuePair> R; // Solution set R
auto comp = [](const CostValuePair &a, const CostValuePair &b) { return a.first < b.first; };
std::priority_queue<CostValuePair, std::vector<CostValuePair>, decltype(comp)> Q(comp);

auto first_solution = assignment_solver(cost_matrix); // Solve initial problem
unique_solutions.insert(first_solution.second);
R.push_back(first_solution); // Add the first solution to R
Q.push(assignment_solver(cost_matrix)); // Add the first problem to Q with its cost

while (R.size() < num_solutions && !Q.empty()) {
auto [_, P] = Q.top(); // Fetch the next problem to solve
auto [_, Q_max] = Q.top(); // Fetch the next problem to solve
Q.pop();

R.push_back(Q_max);
// Generate subproblems based on the last solution added to R
auto subproblems = partition(P, R.back().second);

for (auto &[__, new_P] : subproblems) {
auto new_solution = assignment_solver(new_P); // Solve the subproblem

// If the solution is new (unique) and valid, add it to the queue and solution set
if (unique_solutions.find(new_solution.second) == unique_solutions.end() && is_valid_solution(new_solution.second)) {
unique_solutions.insert(new_solution.second);
R.push_back(new_solution);
Q.push({new_solution.first, new_P}); // Add the new subproblem to Q with its cost
}
}
auto Q_ = partition(Q_max.first, Q_max.second);
}

return R; // Return the set of solutions found
Expand Down

0 comments on commit 10823cc

Please sign in to comment.