Skip to content

Commit

Permalink
Ver 0.1 | -s 50 | good result.
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlyqing00 committed Dec 11, 2018
1 parent 32fb233 commit 6d75fbd
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 81 deletions.
7 changes: 4 additions & 3 deletions include/path_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ class PathManager {
public:
int nodes_n {0};
int sols_n {0};
map< vector<int>, int> sol_paths;
map< vector<int>, pair<int,int> > sol_paths; // sol; word_cnt, sol_cnt;

PathManager(int _vertices_n, int _sols_n);

void add_sol_path(const vector<int> & sol_path, int sol_cnt=1);
void add_sol_words( const map< vector<int>, int > & sol_words,
int sol_cnt=1);
void print_sol_paths();

void build_path_graph();
Expand All @@ -28,7 +29,7 @@ class PathManager {
vector<StripePair> build_stripe_pairs();

private:
vector< vector< pair<int, int> > > path_graph;
vector< vector< pair<int, double> > > path_graph;
vector<StripePair> stripe_pairs;

};
Expand Down
7 changes: 4 additions & 3 deletions include/stripes_solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class StripesSolver {
// Path
PathManager path_manager;

StripesSolver(const string & _puzzle_foler, const int _stripes_n);
StripesSolver(const string & _puzzle_foler, int _stripes_n, int _samples_n);
~StripesSolver();

void m_metric();
Expand Down Expand Up @@ -83,18 +83,19 @@ class StripesSolver {

// Metric word-path
int sols_n = 10;
map< vector<int>, int> fragment;
vector< vector<double> > pixel_graph;

double m_metric_pixel(const cv::Mat & piece0, const cv::Mat & piece1);
double m_metric_word(const cv::Mat & piece0, const cv::Mat & piece1);
double m_metric_comp_eva(const cv::Mat & piece0, const cv::Mat & piece1);

vector< vector<int> > reassemble_greedy(bool probability_flag=false);
vector<int> reassemble_greedy_probability();
void reassemble_greedy_probability();

cv::Mat word_detection( const cv::Mat & img,
const vector<int> & sol,
int sol_cnt=1);
void finetune_sols(const vector< vector<int> > & composition_orders);

};

Expand Down
3 changes: 2 additions & 1 deletion include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ enum class PuzzleType {

const double eps = 1e-8;
const int U_a = 5;

const cv::Scalar boundary_color(50, 50, 255);

double avg_vec3b(const cv::Vec3b &v);

double diff_vec3b(const cv::Vec3b & v0, const cv::Vec3b & v1);

double m_metric_pixel(const cv::Mat & img0, const cv::Mat & img1);
Expand Down
60 changes: 32 additions & 28 deletions src/solver/path_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,63 @@ PathManager::PathManager(int _nodes_n, int _sols_n) :
sols_n(_sols_n) {
}

void PathManager::add_sol_path( const vector<int> & sol_path,
void PathManager::add_sol_words(const map< vector<int>, int > & sol_words,
int sol_cnt) {
sol_paths[sol_path] += sol_cnt;

for (const auto & iter: sol_words) {

const vector<int> sol_path = iter.first;
int word_cnt = iter.second;
if (sol_paths.find(sol_path) != sol_paths.end()) {
auto val = sol_paths[sol_path];
val.first += word_cnt;
val.second += sol_cnt;
} else {
sol_paths[sol_path] = make_pair(word_cnt, sol_cnt);
}

}

}

void PathManager::print_sol_paths() {

for (const auto & iter: sol_paths) {

const vector<int> sol_path = iter.first;
int cnt = iter.second;
const auto val = iter.second;

cout << "Path: ";
for (int vertex_idx: sol_path) {
cout << vertex_idx << " ";
}

cout << endl << "Path cnt: " << cnt << endl;
cout << endl << "Word cnt: " << val.first << " Sol cnt: " << val.second << endl;

}

}

void PathManager::build_path_graph() {

path_graph = vector< vector< pair<int, int> > >(nodes_n);
path_graph = vector< vector< pair<int, double> > >(nodes_n);

for (const auto & iter: sol_paths) {

const vector<int> sol_path = iter.first;
int cnt = iter.second;
const auto val = iter.second;

int score = cnt * sol_path.size();
double score = (double)val.first * sol_path.size() / val.second;

for (int i = 1; i < sol_path.size(); i++) {

bool found_flag = false;
int cur_node = sol_path[i - 1];
int next_node = sol_path[i];

for (auto edge: path_graph[cur_node]) {
for (auto & edge: path_graph[cur_node]) {
if (edge.first == next_node) {
edge.second+=score;
edge.second += score;
found_flag = true;
break;
}
Expand All @@ -66,7 +80,7 @@ void PathManager::print_path_graph() {

for (int i = 0; i < nodes_n; i++) {
cout << "Node " << i << ": " << endl;
for (const auto edge: path_graph[i]) {
for (const auto & edge: path_graph[i]) {
cout << edge.first << " " << edge.second << endl;
}
}
Expand All @@ -75,29 +89,19 @@ void PathManager::print_path_graph() {

vector<StripePair> PathManager::build_stripe_pairs() {

for (const auto & iter: sol_paths) {

const vector<int> sol_path = iter.first;
int cnt = iter.second;

int score = cnt * sol_path.size();

for (int i = 1; i < sol_path.size(); i++) {

bool found_flag = false;
int cur_node = sol_path[i - 1];
int next_node = sol_path[i];
for (int i = 0; i < nodes_n; i++) {
for (const auto & edge: path_graph[i]) {

if (edge.second < 3) continue;
stripe_pairs.push_back(StripePair(
cur_node,
next_node,
score,
i,
edge.first,
edge.second,
1,
false
));

}

}

sort(stripe_pairs.begin(), stripe_pairs.end());
Expand Down
19 changes: 14 additions & 5 deletions src/solver/solve_puzzle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

void solve_stripes( const string & stripes_folder,
const string & case_name,
const int vertical_n,
int vertical_n,
int samples_n,
StripesSolver::Metric metric_mode,
StripesSolver::Composition composition_mode) {

StripesSolver stripes_solver(stripes_folder, vertical_n);
StripesSolver stripes_solver(stripes_folder, vertical_n, samples_n);
stripes_solver.reassemble(metric_mode, composition_mode);
stripes_solver.save_result(case_name);

Expand Down Expand Up @@ -63,11 +64,12 @@ int main(int argc, char ** argv) {
string case_name = "doc0";
PuzzleType puzzle_type = PuzzleType::STRIPES;
int vertical_n = 4;
int samples_n = 10;
StripesSolver::Composition composition_mode = StripesSolver::GREEDY;
StripesSolver::Metric metric_mode = StripesSolver::PIXEL;

// Parse command line parameters
const string opt_str = "t:n:s:c:m:";
const string opt_str = "t:n:Sc:m:s:";
int opt = getopt(argc, argv, opt_str.c_str());

while (opt != -1) {
Expand All @@ -81,12 +83,18 @@ int main(int argc, char ** argv) {
case 'c':
composition_mode = static_cast<StripesSolver::Composition>(atoi(optarg));
break;
case 's':
case 'S':
puzzle_type = PuzzleType::SQUARES;
break;
case 'm':
metric_mode = static_cast<StripesSolver::Metric>(atoi(optarg));
break;
case 's':
samples_n = atoi(optarg);
break;
default:
cerr << "[ ERR] Unknon options " << opt << endl;
exit(-1);
}

opt = getopt(argc, argv, opt_str.c_str());
Expand All @@ -101,13 +109,14 @@ int main(int argc, char ** argv) {
cout << "Vertical cut num: \t" << vertical_n << endl;
cout << "Puzzle type: \t" << (puzzle_type == PuzzleType::SQUARES ? "Squares": "Stripes") << endl;
cout << "Metric mode: \t" << metric_mode_str << endl;
cout << "Samples times: \t" << samples_n << endl;
cout << "Composition mode: \t" << (composition_mode == StripesSolver::Composition::GREEDY ? "Greedy" : "Greedy Probability") << endl;

// Import stripes
if (puzzle_type == PuzzleType::STRIPES) {

const string puzzle_folder = "data/stripes/" + case_name + "_" + to_string(vertical_n) + "/";
solve_stripes(puzzle_folder, case_name, vertical_n, metric_mode, composition_mode);
solve_stripes(puzzle_folder, case_name, vertical_n, samples_n, metric_mode, composition_mode);

} else {

Expand Down
Loading

0 comments on commit 6d75fbd

Please sign in to comment.