Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlyqing00 committed Dec 15, 2018
1 parent b962622 commit 96db662
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 54 deletions.
17 changes: 14 additions & 3 deletions include/KM.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
#ifndef KM_H
#define KM_H

#include <cmath>
#include <limits>
#include <vector>

#include <utils.h>

using namespace std;

class KM {

public:
KM(const vector< vector<int> > & _edges);
KM(const vector< vector<double> > & _edges);
vector<int> solve();
vector<int> cut_loops();

void print_edges();
void print_matches();

private:

int nodes_n;
const vector< vector<int> > edges;
vector<int> lx, ly, match_x, slack;
const vector< vector<double> > edges;
vector<int> match_x;
vector<double> lx, ly, slack;
vector<bool> vx, vy;

bool Hungarian(int cur_node);

};
#endif
4 changes: 2 additions & 2 deletions include/stripes_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ class StripesSolver {
cv::Mat word_detection( const cv::Mat & img,
const vector<int> & sol,
int sol_cnt=1);
void merge_single_sol(vector< vector<int> > & composition_orders);
void finetune_sols(const vector< vector<int> > & composition_orders);
void merge_single_sol(vector< vector<int> > & fragments);
void finetune_sols(const vector< vector<int> > & fragments);

};

Expand Down
111 changes: 106 additions & 5 deletions src/solver/KM.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#include <KM.h>

KM::KM(const vector< vector<int> > & _edges) :
KM::KM(const vector< vector<double> > & _edges) :
edges(_edges) {

nodes_n = edges.size();
lx = vector<int>(nodes_n, 0);
ly = vector<int>(nodes_n, 0);
lx = vector<double>(nodes_n, 0);
ly = vector<double>(nodes_n, 0);
match_x = vector<int>(nodes_n, -1);
slack = vector<int>(nodes_n, numeric_limits<int>::max());
slack = vector<double>(nodes_n, numeric_limits<double>::max());


for (int i = 0; i < nodes_n; i++) {
lx[i] = numeric_limits<int>::min();
lx[i] = numeric_limits<double>::min();
for (int j = 0; j < nodes_n; j++) {
lx[i] = max(lx[i], edges[i][j]);
}
Expand All @@ -28,7 +29,107 @@ vector<int> KM::solve() {

while (true) {

vx = vector<bool>(nodes_n, false);
vy = vector<bool>(nodes_n, false);
if (Hungarian(i)) break;

double d = numeric_limits<double>::max();
for (int j = 0; j < nodes_n; j++) {
if (!vy[j]) d = min(d, slack[j]);
}

for (int j = 0; j < nodes_n; j++) {
if (vx[j]) lx[j] -= d;
}
for (int j = 0; j < nodes_n; j++) {
if (vy[j]) ly[j] += d;
}

}

}

return match_x;

}

vector<int> KM::cut_loops() {

vector<int> arr;
vector<bool> visited(nodes_n, false);

for (int i = 0; i < nodes_n; i++) {

if (visited[i]) continue;

double match_score = numeric_limits<double>::max();
int p = i;
int st_idx = i;

do {
if (match_score > edges[match_x[p]][p]) {
match_score = edges[match_x[p]][p];
st_idx = p;
}
p = match_x[p];
} while (p != st_idx);

p = match_x[st_idx];
do {
arr.push_back(p);
visited[p] = true;
p = match_x[p];
} while (p != match_x[st_idx]);

}

reverse(arr.begin(), arr.end());
return arr;

}

void KM::print_edges() {

cout << "Edges:" << endl;
for (int i = 0; i < nodes_n; i++) {
for (int j = 0; j < nodes_n; j++) {
cout << edges[i][j] << " ";
}
cout << endl;
}
}

void KM::print_matches() {

cout << "Matches:" << endl;
for (int i = 0; i < nodes_n; i++) {
cout << match_x[i] << "\t-->\t" << i << endl;
}

}

bool KM::Hungarian(int cur_node) {

vx[cur_node] = 1;

for (int i = 0; i < nodes_n; i++) {

if (cur_node == i) continue;
if (vy[i]) continue;

double t = lx[cur_node] + ly[i] - edges[cur_node][i];
if (abs(t) < eps) {
vy[i] = true;
if (match_x[i] == -1 || Hungarian(match_x[i])) {
match_x[i] = cur_node;
return true;
}
} else {
slack[i] = min(slack[i], t);
}

}

return false;

}
Loading

0 comments on commit 96db662

Please sign in to comment.