void bin_oper_opti(int oper, node* node_copy) { //if both operands are simple constant numerics if(node_copy->args[0]->node_type == NUM && node_copy->args[1]->node_type == NUM) { //make this a numerical node node_copy->node_type = NUM; //pre calculate the nodes value and store it as leaf_value node_copy->leaf_value = pre_calculate(oper, node_copy->args[0]->leaf_value, node_copy->args[1]->leaf_value); //null all the pointers in this node node_copy->args[0] = node_copy->args[1] = node_copy->args[2] = node_copy->args[3] = 0; } //if the left operand is a variable and the right a constant else if(node_copy->args[0]->node_type == ID && node_copy->args[1]->node_type == NUM) { //if the numeric is a 0 and the operand is +,- or the numeric is a 1 and the operand is *,/ if((node_copy->args[1]->leaf_value == 0 && (oper == '+' || oper == '-')) || (node_copy->args[1]->leaf_value == 1 && (oper == '*' || oper == '/'))) { //the operand does nothing and just results in the variable node_copy->node_type = ID; node_copy->leaf_value = node_copy->args[0]->leaf_value; node_copy->args[0] = node_copy->args[1] = node_copy->args[2] = node_copy->args[3] = 0; } //if multiplying with 0 else if(node_copy->args[1]->leaf_value == 0 && oper == '*') { node_copy->node_type = NUM; node_copy->leaf_value = 0; node_copy->args[0] = node_copy->args[1] = node_copy->args[2] = node_copy->args[3] = 0; } } //if the left operand is a constant and the right a variable else if(node_copy->args[0]->node_type == NUM && node_copy->args[1]->node_type == ID) { //if the numeric is a 0 and the operand is +,- or the numeric is a 1 and the operand is *,/ if((node_copy->args[0]->leaf_value == 0 && (oper == '+' || oper == '-')) || (node_copy->args[0]->leaf_value == 1 && (oper == '*' || oper == '/'))) { //the operand does nothing and just results in the variable node_copy->node_type = ID; node_copy->leaf_value = node_copy->args[1]->leaf_value; node_copy->args[0] = node_copy->args[1] = node_copy->args[2] = node_copy->args[3] = 0; } //if multiplying with 0 else if(node_copy->args[0]->leaf_value == 0 && oper == '*') { node_copy->node_type = NUM; node_copy->leaf_value = 1; node_copy->args[0] = node_copy->args[1] = node_copy->args[2] = node_copy->args[3] = 0; } } else ;//do nothing }
int LatentRelevance::train() { if (initialize() != 0) { printf("[ERROR] Initialize failed!\n"); return -1; } if (pre_calculate() != 0) { printf("[ERROR] Pre-calculate failed!\n"); return -1; } if (run_gd() != 0) { printf("[ERROR] Running gradient descent failed!\n"); return -1; } return 0; }
int LatentRelevance::test(const char* test_file_name, const char* model_name) { if (load_model(model_name) != 0) { printf("[ERROR] Load model failed!\n"); return -1; } if (read_data(test_file_name) != 0) { printf("[ERROR] Read data failed!\n"); return -1; } if (pre_calculate() != 0) { printf("[ERROR] Pre-calculate failed!\n"); return -1; } const int MAX_FILE_NAME_LEN = 1024; char res_file_name[MAX_FILE_NAME_LEN]; snprintf(res_file_name, MAX_FILE_NAME_LEN, "%s.res", test_file_name); FILE* fp = fopen(res_file_name, "w"); if (fp == NULL) { printf("[ERROR] Cannot open %s! Testing file failed!\n", res_file_name); return -1; } for (int i = 0; i < data_num; ++i) { float score = predict(m_data + i); int label = m_data[i].y; if (label < 0) { // Change -1 to 0 for calculating AUC label = 0; } fprintf(fp, "%f\t1\t%d\n", score, label); } printf("[NOTICE] Predict results are saved in %s\n", res_file_name); fclose(fp); return 0; }