Skip to content

Commit

Permalink
update engine
Browse files Browse the repository at this point in the history
  • Loading branch information
dnomd343 committed Jul 3, 2020
1 parent 4ffd141 commit 261b469
Show file tree
Hide file tree
Showing 8 changed files with 458 additions and 169 deletions.
111 changes: 108 additions & 3 deletions src/engine/HRD_analy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,108 @@
#include <fstream>
#include "HRD_analy.h"

void HRD_analy::Output_Graph(unsigned long long Code, unsigned int square_width, unsigned int square_gap, char str[3]) {
Case_cal dat;
unsigned int i, j;
unsigned int x, y;
unsigned int width, height;
bool exclude[4][5]; // 用于标记排除
vector < vector <bool> > temp;
temp.resize(square_width * 4 + square_gap * 3);
for (x = 0; x < temp.size(); x++) {
temp[x].resize(square_width * 5 + square_gap * 4);
for (y = 0; y < temp[0].size(); y++) {
temp[x][y] = false;
}
}
for (x = 0; x < 4; x++) { // 初始化exclude
for (y = 0; y < 5; y++) {
exclude[x][y] = false;
}
}
Parse_Code(dat, Code);
for (y = 0; y < 5; y++) { // 遍历20个格
for (x = 0; x < 4; x++) {
if (exclude[x][y] == true || dat.status[x][y] == 0xFE) {continue;} // 该格为空或已被占用
switch (dat.type[dat.status[x][y]]) { // type -> 0 / 1 / 2 / 3
case 0: // 2 * 2
width = height = 2;
exclude[x][y + 1] = exclude[x + 1][y] = exclude[x + 1][y + 1] = true;
break;
case 1: // 2 * 1
width = 2;
height = 1;
exclude[x + 1][y] = true;
break;
case 2: // 1 * 2
width = 1;
height = 2;
exclude[x][y + 1] = true;
break;
case 3: // 1 * 1
width = height = 1;
break;
}
if (width == 1) {
width = square_width;
} else {
width = square_width * 2 + square_gap;
}
if (height == 1) {
height = square_width;
} else {
height = square_width * 2 + square_gap;
}
for (i = 0; i < width; i++) {
for (j = 0; j < height; j++) {
temp[x * (square_width + square_gap) + i][y * (square_width + square_gap) + j] = true;
}
}
}
}
width = temp.size();
height = temp[0].size();
for (x = 0; x < width + square_gap * 2 + 2; x++) {
cout << str;
}
cout << endl;
for (y = 0; y < square_gap; y++) {
cout << str;
for (x = 0; x < width + square_gap * 2; x++) {
cout << " ";
}
cout << str << endl;
}
for (y = 0; y < height; y++) {
cout << str;
for (x = 0; x < square_gap; x++) {
cout << " ";
}
for (x = 0; x < width; x++) {
if (temp[x][y] == true) {
cout << str;
} else {
cout << " ";
}
}
for (x = 0; x < square_gap; x++) {
cout << " ";
}
cout << str << endl;
}
for (y = 0; y < square_gap; y++) {
cout << str;
for (x = 0; x < width + square_gap * 2; x++) {
cout << " ";
}
cout << str << endl;
}
for (x = 0; x < width + square_gap * 2 + 2; x++) {
cout << str;
}
cout << endl;
}

void HRD_analy::Output_Detail(string File_name) { // 输出分析结果到文件
unsigned int i, j, k;
ofstream output;
Expand All @@ -13,25 +115,29 @@ void HRD_analy::Output_Detail(string File_name) { // 输出分析结果到文件
cout << "Output into: " << File_name << "...";
}
output.open(File_name);
// min_solution
output << "[Min_solution_step]" << endl;
output << min_solution_step << endl;
output << "[Min_solution_case]" << endl;
output << "num: " << min_solution_num << endl;
for (i = 0; i < min_solution_case.size(); i++) {
output << Change_str(min_solution_case[i]) << endl;
}
// farthest
output << "[Farthest_step]" << endl;
output << farthest_step << endl;
output << "[Farthest_case]" << endl;
output << "num: " << farthest_num << endl;
for (i = 0; i < farthest_case.size(); i++) {
output << Change_str(farthest_case[i]) << endl;
}
// solution
output << "[Solution]" << endl;
output << "num: " << solution_num << endl;
for (i = 0; i < solution_case.size(); i++) {
output << Change_str(solution_case[i]) << "(" << solution_step[i] << ")" << endl;
}
// layer
output << "[Layer_Size]" << endl;
for (i = 0; i < Layer.size(); i++) {
output << i << " -> " << Layer[i].size() << endl;
Expand Down Expand Up @@ -486,7 +592,7 @@ void HRD_analy::Get_Code(Case_cal &dat) { // 获取编码并存储在dat.code
dat.code &= 0xFFFFFFFFF; // 清除高28位内容
}

bool HRD_analy::Parse_Code(unsigned long long Code) {
bool HRD_analy::Parse_Code(unsigned long long Code) { // 外部解析函数 结果储存在Parse_dat 返回编码正确性
Parse_dat.layer_num = Parse_dat.layer_index = 0;
return Parse_Code(Parse_dat, Code);
}
Expand All @@ -506,7 +612,7 @@ bool HRD_analy::Parse_Code(Case_cal &dat, unsigned long long Code) { // 解析
}
num = 0;
for (i = 15; i >= 0; i--) { // 载入排列到range
range[i] = Code & 0x3 ;
range[i] = Code & 0x3;
if (range[i] == 0) {num++;}
Code >>= 2;
}
Expand All @@ -517,7 +623,6 @@ bool HRD_analy::Parse_Code(Case_cal &dat, unsigned long long Code) { // 解析
x = Code % 4;
y = Code / 4;
dat.status[x][y] = dat.status[x + 1][y] = dat.status[x][y + 1] = dat.status[x + 1][y + 1] = 0;

num = x = y = 0;
for (i = 0; i < 16; i++) {
while (dat.status[x][y] != 0xFF) { // 找到下一个未填入的位置
Expand Down
3 changes: 2 additions & 1 deletion src/engine/HRD_analy.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class HRD_analy {
bool Parse_Code(unsigned long long Code);
void Analyse_Case(unsigned long long code);
void Output_Detail(string File_name);
void Free_Data();
void Output_Graph(unsigned long long Code, unsigned int square_width, unsigned int square_gap, char str[2]);

private:
vector <Case_cal *> Layer_hash[0x10000]; // 哈希表
Expand All @@ -53,7 +55,6 @@ class HRD_analy {
void Find_Next_Case(Case_cal &dat_raw);
void Add_Case(Case_cal *dat);
void Calculate(unsigned long long code);
void Free_Data();
};

#endif
11 changes: 5 additions & 6 deletions src/engine/HRD_cal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ vector <unsigned long long> HRD_cal::Calculate_All(unsigned long long Code) { //
for (i = 0; i < List.size(); i++) {
data.push_back((*List[i]).code); // 储存计算结果
}
init_data();
init_data(); // 防止内存泄漏
return data;
}

Expand All @@ -35,7 +35,7 @@ vector <unsigned long long> HRD_cal::Calculate(unsigned long long Code, unsigned
if (flag == true) { // 若找到目标
return Get_Path(result);
} else { // 未找到目标
init_data();
init_data(); // 防止内存泄漏
return temp; // 返回空序列
}
}
Expand All @@ -55,7 +55,7 @@ vector <unsigned long long> HRD_cal::Calculate(unsigned long long Code) { // 寻
if (flag == true) { // 若找到解
return Get_Path(result);
} else { // 无解
init_data();
init_data(); // 防止内存泄漏
return temp; // 返回空序列
}
}
Expand Down Expand Up @@ -86,7 +86,7 @@ void HRD_cal::init_data() { // 初始化数据结构
List_source.clear();
}

bool HRD_cal::Check_Code(unsigned long long Code) {
bool HRD_cal::Check_Code(unsigned long long Code) { // 检查编码: 错误 -> false / 正确 -> true
Case_cal dat;
return Parse_Code(dat, Code);
}
Expand Down Expand Up @@ -387,7 +387,7 @@ bool HRD_cal::Parse_Code(Case_cal &dat, unsigned long long Code) { // 解析编
}
num = 0;
for (i = 15; i >= 0; i--) { // 载入排列到range
range[i] = Code & 0x3 ;
range[i] = Code & 0x3;
if (range[i] == 0) {num++;}
Code >>= 2;
}
Expand All @@ -398,7 +398,6 @@ bool HRD_cal::Parse_Code(Case_cal &dat, unsigned long long Code) { // 解析编
x = Code % 4;
y = Code / 4;
dat.status[x][y] = dat.status[x + 1][y] = dat.status[x][y + 1] = dat.status[x + 1][y + 1] = 0;

num = x = y = 0;
for (i = 0; i < 16; i++) {
while (dat.status[x][y] != 0xFF) { // 找到下一个未填入的位置
Expand Down
57 changes: 43 additions & 14 deletions src/engine/HRD_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,53 @@

ofstream output_farthest;
ofstream output_solution;
ifstream input_seed;

void HRD_group::Batch_Analyse(unsigned long long seed, string name_farthest, string name_solution, bool is_output_solution) {
bool HRD_group::Multi_Analyse(string seed_File_name, string name_farthest, string name_solution, bool is_output_solution) {
HRD_cal cal;
char str[10];
vector <unsigned long long> dat;
if (cal.Check_Code(seed) == false) {return;}
dat = cal.Calculate_All(seed);
sort(dat.begin(), dat.end());
File_name_farthest = name_farthest;
File_name_solution = name_solution;
vector <unsigned long long> seed; // 储存所有将要计算的布局编码
input_seed.open(seed_File_name);
while (input_seed.eof() != true) { // 从外部文件读入列表
input_seed >> str;
seed.push_back(Change_int(str));
}
for (unsigned int i = 0; i < seed.size(); i++) { // 判断编码正确性
if (cal.Check_Code(seed[i]) == false) {
cout << "input code error" << endl; // 发现错误编码
return false; // 退出
}
}
Output_solution_case = is_output_solution;
Analyse_Group(dat);
output_farthest.open(name_farthest);
output_solution.open(name_solution);
for (unsigned int i = 0; i < seed.size(); i++) {
cout << "Start: " << Change_str(seed[i]) << endl;
dat = cal.Calculate_All(seed[i]); // 得到整个群
sort(dat.begin(), dat.end()); // 排列
Analyse_Group(dat); // 分析整个群
}
output_farthest.close();
output_solution.close();
return true;
}

void HRD_group::Analyse_Group(vector <unsigned long long> dat) { // 传入整个群 并将结果以csv格式输出到文件
void HRD_group::Batch_Analyse(unsigned long long seed, string name_farthest, string name_solution, bool is_output_solution) { // 根据群中一个布局分析整个群全部布局的参数 并将结果以csv格式输出到文件
HRD_cal cal;
vector <unsigned long long> dat;
if (cal.Check_Code(seed) == false) {return;} // 编码错误 退出
dat = cal.Calculate_All(seed); // 得到整个群
sort(dat.begin(), dat.end()); // 排列
Output_solution_case = is_output_solution;
output_farthest.open(name_farthest);
output_solution.open(name_solution);
Analyse_Group(dat); // 分析整个群
output_farthest.close();
output_solution.close();
}

void HRD_group::Analyse_Group(vector <unsigned long long> dat) { // 传入整个群并将结果输出到文件
unsigned int i, j, k;
int hash_index;
vector <Case *> List; // 全组数据
Expand Down Expand Up @@ -52,22 +85,18 @@ void HRD_group::Analyse_Group(vector <unsigned long long> dat) { // 传入整个
}
}
Case_detail *result;
output_farthest.open(File_name_farthest);
output_solution.open(File_name_solution);
for(i = 0; i < List.size(); i++) { // 遍历整个队列
for (k = 0; k < List.size(); k++) { // 初始化
(*List[k]).Layer_num = -1;
(*List[k]).Flag = false;
}
result = Analyse_Case(List[i]); // 计算对应布局数据并储存到result中
Output_detail(result);
delete result;
delete result; // 释放内存
if (i % 13 == 0) {
cout << i << "/" << List.size() << endl;
}
}
output_farthest.close();
output_solution.close();
for (i = 0; i < List.size(); i++) { // 释放List数据
delete List[i];
}
Expand Down Expand Up @@ -429,7 +458,7 @@ bool HRD_group::Parse_Code(Case_cal &dat, unsigned long long Code) { // 解析
}
num = 0;
for (i = 15; i >= 0; i--) { // 载入排列到range
range[i] = Code & 0x3 ;
range[i] = Code & 0x3;
if (range[i] == 0) {num++;}
Code >>= 2;
}
Expand Down
5 changes: 2 additions & 3 deletions src/engine/HRD_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class HRD_group {
unsigned long long Change_int(char str[10]);
string Change_str(unsigned long long dat);
void Batch_Analyse(unsigned long long seed, string name_farthest, string name_solution, bool is_output_solution);
bool Multi_Analyse(string seed_File_name, string name_farthest, string name_solution, bool is_output_solution);

private:
struct Case_cal {
Expand Down Expand Up @@ -39,9 +40,7 @@ class HRD_group {
vector <unsigned long long> solution_step;
};
vector <unsigned long long> Next_Case_dat; // 储存Find_Next_Case找到的结果
string File_name_farthest;
string File_name_solution;
bool Output_solution_case;
bool Output_solution_case; // 是否输出全部solution_case

bool Parse_Code(Case_cal &dat, unsigned long long Code);
void Get_Code(Case_cal &dat);
Expand Down
Loading

0 comments on commit 261b469

Please sign in to comment.