📄 pdt_05.cc
字号:
// check the node // if (node_a == (TreeNode*)NULL) { return Error::handle(name(), L"isSplitOccupancyBelowThreshold - NULL VERTEX", Error::ARG, __FILE__, __LINE__); } // get the name and values of this attribute // String& attr_name = attribute_a.first(); SingleLinkedList<String>& attr_values = attribute_a.second(); // get the data points on the node // PhoneticDecisionTreeNode* pdt_node = node_a->getItem(); Data& data = pdt_node->getDataPoints(); // get the first datapoint from the singlelinked list // DataPoint* datapoint = data.getFirst(); // check if this attribute is on the first item of the current node, // else return false // HashTable<String, String>& datapoint_attr = datapoint->third(); // return error if the attribute is missing on a datapoint // if (!datapoint_attr.containsKey(attr_name)) { return Error::handle(name(), L"isSplitOccupancyBelowThreshold", ERR, __FILE__, __LINE__); } // loop over all the values for this attribute, compute // sum-state_occupancies for each and then check it these meet the // threshold // for (boolean l = attr_values.gotoFirst(); l; l = attr_values.gotoNext()) { // local variables // TreeNode node; PhoneticDecisionTreeNode child_pdt_node; Data child_data; float sum_num_occ = (float)0; // get the value of the current attribute // String* value = attr_values.getCurr(); // loop over data and count the number of each value // for (boolean j = data.gotoFirst(); j; j = data.gotoNext()) { // get the data point in triple // datapoint = data.getCurr(); // get the attribute value in hashtable // HashTable<String, String>& datapoint_attr = datapoint->third(); // check if this datapoint has this value for the current // attribute and add this to the singlelinked list // if (value->eq(*datapoint_attr.get(attr_name))) { child_data.insert(datapoint); } } // set the singlelinked list of data at this child node // child_pdt_node.setDataPoints(child_data); node.setItem(&child_pdt_node); // compute the likelihood for the child node // computeSumOccupancy(&node, sum_num_occ); // check if this sum for the threshold conditions // if ( sum_num_occ <= num_occ_threshold_d) { res = false; } } // exit gracefully // return res;}// method: mergeSubTree//// arguments:// TreeNode* node: (input) input node//// return: a boolean value indicating status//// this method merges two leaf nodes at a time, below a given node.//boolean PhoneticDecisionTree::mergeSubTree(TreeNode* node_a) { // define local variable // TreeNode* node = (TreeNode*)NULL; TreeNode* start_node = (TreeNode*)NULL;; boolean res = true; boolean merge = true; // check the node // if (node_a == (TreeNode*)NULL) { return Error::handle(name(), L"mergeSubTree - NULL VERTEX", Error::ARG, __FILE__, __LINE__); } // merge the leaf nodes, two at a time, till the threshold // conditions are met // while (merge) { // define local variable // TreeNode* best_node = (TreeNode*)NULL; float first_likelihood = (float)0; SingleLinkedList<TreeNode> leaf_nodes(DstrBase::USER); merge = false; // get all the leaf nodes below the node // leaf_nodes.clear(Integral::RESET); node = node_a; res = getLeafNodes(*node, leaf_nodes); // get the start node // for (boolean more = leaf_nodes.gotoFirst(); more; more = leaf_nodes.gotoNext() ) { start_node = leaf_nodes.getCurr(); // check the node // if (start_node == (TreeNode*)NULL) { return Error::handle(name(), L"mergeSubTree - NULL VERTEX", Error::ARG, __FILE__, __LINE__); } // mark the current posistion // leaf_nodes.setMark(); // intialize the min_dec_likelihood to the merge_threshold // float min_dec_likelihood = merge_threshold_d; // compute the likelihood of the first node // computeLikelihoodNode(start_node, first_likelihood); // find the best leaf-node if any that will be merged with this // start node // for (boolean more = leaf_nodes.gotoNext(); more; more = leaf_nodes.gotoNext() ) { node = leaf_nodes.getCurr(); // check the node // if (node == (TreeNode*)NULL) { return Error::handle(name(), L"mergeSubTree - NULL VERTEX", Error::ARG, __FILE__, __LINE__); } // define local variable // float merge_likelihood = (float)0; float dec_likelihood = (float)0; float second_likelihood = (float)0; boolean att = false; // find the likelihood decrease by merging the start and this // node. if any of the nodes is non-existing this function // returns false // att = computeLikelihoodMergeNodes(start_node, node, merge_likelihood); // compute decrease in likelihood due to merging of the two // nodes // computeLikelihoodNode(node, second_likelihood); dec_likelihood = first_likelihood + second_likelihood - merge_likelihood; // update the best node that will be merged with the start // node, if the decrease in likelihood is less than the // min_dec_likelihood. note that min_dec_likelihood is // initallized to merge_likelihood_d // if ( att && (dec_likelihood < min_dec_likelihood)) { min_dec_likelihood = dec_likelihood; best_node = node; merge = true; } } // merge the best_node with start_node only if the decrease in // likelihood is less than the merge_threshold, and then mark // the best_node as non-existing and update its typical-index to // the typical-index of the start-node to which it is merged // if (merge) { res = mergeLeafNodes(start_node, best_node); // mark the best-node as non-existing since it is merged with // the start-node // boolean flag = false; res = markNode(best_node, flag); res = updateTypicalIndex(start_node, best_node); break; } // go back to the marked node // leaf_nodes.gotoMark(); } } // exit gracefully // return res;}// method: computeLikelihoodMergeNodes//// arguments:// TreeNode* start_node: (input) input start_node//// TreeNode* node: (input) input node//// return: a boolean value indicating status//// this method computes the likelihood if the two input nodes are// merged.//boolean PhoneticDecisionTree::computeLikelihoodMergeNodes(TreeNode* start_node_a, TreeNode* node_a, float& merge_likelihood_a) { // local variables // TreeNode node; Data parent_data; boolean res = true; boolean start_flag = true; boolean flag = true; // check the input nodes // if (start_node_a == (TreeNode*)NULL) { return Error::handle(name(), L"computeLikelihoodMergeNodes - NULL INPUT-VERTEX", Error::ARG, __FILE__, __LINE__); } if (node_a == (TreeNode*)NULL) { return Error::handle(name(), L"computeLikelihoodMergeNodes - NULL INPUT-VERTEX", Error::ARG, __FILE__, __LINE__); } // check if this node exists // PhoneticDecisionTreeNode* pdt_start_node = start_node_a->getItem(); start_flag = pdt_start_node->getFlagExists(); // get the data points on the start_node // Data& data_start_node = pdt_start_node->getDataPoints(); // loop over data and insert the datapoint into the parent // for (boolean j = data_start_node.gotoFirst(); j; j = data_start_node.gotoNext()) { // get the data point in triple // DataPoint* datapoint_start_node = data_start_node.getCurr(); // add this datapoint to the singlelinked list // parent_data.insert(datapoint_start_node); } // check if this node exists // PhoneticDecisionTreeNode* pdt_node = node_a->getItem(); flag = pdt_node->getFlagExists(); // exit gracefully // if (!start_flag || !flag) { return false; } // get the data points on the node // Data& data_node = pdt_node->getDataPoints(); // get the first datapoint from the singlelinked list // DataPoint* datapoint_node = data_node.getFirst(); // loop over data and insert the datapoint into the parent // for (boolean j = data_node.gotoFirst(); j; j = data_node.gotoNext()) { // get the data point in triple // datapoint_node = data_node.getCurr(); // add this datapoint to the singlelinked list // parent_data.insert(datapoint_node); } // set the singlelinked list of data at this parent node // PhoneticDecisionTreeNode parent_pdt_node; parent_pdt_node.setDataPoints(parent_data); node.setItem(&parent_pdt_node); // compute the likelihood for the child node only if there is // data on the node // if (!parent_data.isEmpty()) { merge_likelihood_a = (float)0; res = computeLikelihoodNode(&node, merge_likelihood_a); } // exit gracefully // return res;}// method: mergeLeafNodes//// arguments:// TreeNode* start_node: (input) input start_node//// TreeNode* best_node: (input) input best candidate node that will// be merged with the start_node//// return: a boolean value indicating status//// this method appends the data on the best_node to the start_node and// deletes the best_node//boolean PhoneticDecisionTree::mergeLeafNodes(TreeNode* start_node_a, TreeNode* best_node_a) { // local variables // PhoneticDecisionTreeNode* start_pdt_node = (PhoneticDecisionTreeNode*)NULL; PhoneticDecisionTreeNode* best_pdt_node = (PhoneticDecisionTreeNode*)NULL; boolean start_flag = true; boolean best_flag = true; boolean res = true; // check the nodes // if (start_node_a == (TreeNode*)NULL) { return Error::handle(name(), L"mergeLeafNodes - NULL VERTEX", Error::ARG, __FILE__, __LINE__); } if (best_node_a == (TreeNode*)NULL) { return Error::handle(name(), L"mergeLeafNodes - NULL VERTEX", Error::ARG, __FILE__, __LINE__); } // get the data points on the start_node // start_pdt_node = start_node_a->getItem(); // check the data // if (start_pdt_node == (PhoneticDecisionTreeNode*)NULL) { return Error::handle(name(), L"mergeLeafNodes - NULL DATA", Error::ARG, __FILE__, __LINE__); } // see if the start_node exists, it might have been merged // Data& start_data = start_pdt_node->getDataPoints(); start_flag = start_pdt_node->getFlagExists(); // see if the best_node exists, it might have been merged // best_pdt_node = best_node_a->getItem(); // check the data // if (best_pdt_node == (PhoneticDecisionTreeNode*)NULL) { return Error::handle(name(), L"mergeLeafNodes - NULL DATA", Error::ARG, __FILE__, __LINE__); } best_flag = best_pdt_node->getFlagExists(); // merge nodes only if both exists // if (start_flag && best_flag) { // get the data points on the best_node // Data& data = best_pdt_node->getDataPoints(); // loop over data and insert the datapoint into the parent // for (boolean j = data.gotoFirst(); j; j = data.gotoNext()) { // get the data point in triple // DataPoint* datapoint = data.getCurr(); // add this datapoint to the singlelinked list // start_data.insert(datapoint); } // set the singlelinked list of data at the start_node // // start_pdt_node->setDataPoints(start_data); } else res = false; // exit gracefully // return res; }// method: classifyDataPoint//// arguments:// DataPoint& datapoint: (input) input data-point that will be classified//// return: a Long value (index) indicating the class//// this method classifies the data//Long PhoneticDecisionTree::classifyDataPoint(DataPoint& datapoint_a) { // local variables // Long index = -1; // runmode: TEST && stopmode: THRESH // if ((runmode_d == TEST) && (stopmode_d == THRESH)){ // classify the datapoint in test mode // // classify the input datapoint on the basis of its attributes, // starting from the root-node, till it falls into one of the // leaf-nodes (classes) // // local variables // TreeNode* root_node = (TreeNode*)NULL; // get the root node // root_node = getFirst(); // check the node // if (root_node == (TreeNode*)NULL) { return Error::handle(name(), L"classifyDataPoint - NULL VERTEX", Error::ARG, __FILE__, __LINE__); } // get the index // index = findClass(root_node, datapoint_a); } // error: unknown mode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -