|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Peloton |
| 4 | +// |
| 5 | +// query_clusterer.cpp |
| 6 | +// |
| 7 | +// Identification: src/brain/query_clusterer.cpp |
| 8 | +// |
| 9 | +// Copyright (c) 2015-2018, Carnegie Mellon University Database Group |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "brain/query_clusterer.h" |
| 14 | +#include "common/logger.h" |
| 15 | + |
| 16 | +namespace peloton { |
| 17 | +namespace brain { |
| 18 | + |
| 19 | +void QueryClusterer::UpdateFeatures() { |
| 20 | + // Read the latest queries from server over RPC or the brainside catalog |
| 21 | + // Update the feature vectors for template queries and l2 - normalize them |
| 22 | + // For new templates - insert into templates_ and |
| 23 | + // call UpdateTemplate(fingerprint, true) |
| 24 | +} |
| 25 | + |
| 26 | +void QueryClusterer::UpdateTemplate(std::string fingerprint, bool is_new) { |
| 27 | + // Find the nearest cluster of the template's feature vector by querying the |
| 28 | + // KDTree of the centroids of the clusters. If the similarity of the feature |
| 29 | + // with the cluster is greater than the threshold, add it to the cluster. |
| 30 | + // Otherwise create a new cluster with this template |
| 31 | + auto feature = features_[fingerprint]; |
| 32 | + double similarity = 0.0; |
| 33 | + Cluster *cluster = nullptr; |
| 34 | + |
| 35 | + kd_tree_.GetNN(feature, cluster, similarity); |
| 36 | + |
| 37 | + if (cluster == nullptr) { |
| 38 | + // If the kd_tree_ is empty |
| 39 | + cluster = new Cluster(num_features_); |
| 40 | + cluster->AddTemplateAndUpdateCentroid(fingerprint, feature); |
| 41 | + kd_tree_.Insert(cluster); |
| 42 | + clusters_.insert(cluster); |
| 43 | + template_cluster_[fingerprint] = cluster; |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if (similarity > threshold_) { |
| 48 | + // If the nearest neighbor has a similarity higher than the threshold_ |
| 49 | + if (is_new) { |
| 50 | + cluster->AddTemplateAndUpdateCentroid(fingerprint, feature); |
| 51 | + kd_tree_.Update(cluster); |
| 52 | + } else { |
| 53 | + // updating an existing template, so need not update the centroid |
| 54 | + cluster->AddTemplate(fingerprint); |
| 55 | + } |
| 56 | + } else { |
| 57 | + // create a new cluster as the nearest neighbor is not similar enough |
| 58 | + cluster = new Cluster(num_features_); |
| 59 | + cluster->AddTemplateAndUpdateCentroid(fingerprint, feature); |
| 60 | + kd_tree_.Insert(cluster); |
| 61 | + clusters_.insert(cluster); |
| 62 | + } |
| 63 | + |
| 64 | + template_cluster_[fingerprint] = cluster; |
| 65 | +} |
| 66 | + |
| 67 | +void QueryClusterer::UpdateExistingTemplates() { |
| 68 | + // for each template check the similarity with the cluster |
| 69 | + // if the similarity is less than the threshold, then remove it |
| 70 | + // and insert into the next nearest cluster |
| 71 | + // Update the centroids at the end of the round only |
| 72 | + for (auto &feature : features_) { |
| 73 | + auto fingerprint = feature.first; |
| 74 | + auto *cluster = template_cluster_[fingerprint]; |
| 75 | + auto similarity = cluster->CosineSimilarity(feature.second); |
| 76 | + if (similarity < threshold_) { |
| 77 | + cluster->RemoveTemplate(fingerprint); |
| 78 | + UpdateTemplate(fingerprint, false); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + std::vector<Cluster *> to_delete; |
| 83 | + for (auto &cluster : clusters_) { |
| 84 | + if (cluster->GetSize() == 0) { |
| 85 | + to_delete.push_back(cluster); |
| 86 | + } else { |
| 87 | + cluster->UpdateCentroid(features_); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Delete the clusters that are empty |
| 92 | + for (auto cluster : to_delete) { |
| 93 | + clusters_.erase(cluster); |
| 94 | + delete cluster; |
| 95 | + } |
| 96 | + |
| 97 | + // Rebuild the tree to account for the deleted clusters |
| 98 | + kd_tree_.Build(clusters_); |
| 99 | +} |
| 100 | + |
| 101 | +void QueryClusterer::MergeClusters() { |
| 102 | + // Merge two clusters that are within the threshold in similarity |
| 103 | + // Iterate from left to right and merge the left one into right one and mark |
| 104 | + // the left one for deletion |
| 105 | + std::vector<Cluster *> to_delete; |
| 106 | + for (auto i = clusters_.begin(); i != clusters_.end(); i++) { |
| 107 | + for (auto j = i; ++j != clusters_.end();) { |
| 108 | + auto left = *i; |
| 109 | + auto right = *j; |
| 110 | + auto r_centroid = right->GetCentroid(); |
| 111 | + auto similarity = left->CosineSimilarity(r_centroid); |
| 112 | + |
| 113 | + if (similarity > threshold_) { |
| 114 | + auto templates = left->GetTemplates(); |
| 115 | + for (auto &fingerprint : templates) { |
| 116 | + right->AddTemplate(fingerprint); |
| 117 | + template_cluster_[fingerprint] = right; |
| 118 | + } |
| 119 | + right->UpdateCentroid(features_); |
| 120 | + to_delete.push_back(left); |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Delete the clusters that are empty |
| 127 | + for (auto cluster : to_delete) { |
| 128 | + clusters_.erase(cluster); |
| 129 | + delete cluster; |
| 130 | + } |
| 131 | + |
| 132 | + // Rebuild the KDTree to account for changed clusters |
| 133 | + kd_tree_.Build(clusters_); |
| 134 | +} |
| 135 | + |
| 136 | +void QueryClusterer::UpdateCluster() { |
| 137 | + // This function needs to be scheduled periodically for updating the clusters |
| 138 | + // Update the feature vectors of all templates, update new and existing |
| 139 | + // templates and merge the clusters |
| 140 | + UpdateFeatures(); |
| 141 | + UpdateExistingTemplates(); |
| 142 | + MergeClusters(); |
| 143 | +} |
| 144 | + |
| 145 | +void QueryClusterer::AddFeature(std::string &fingerprint, |
| 146 | + std::vector<double> feature) { |
| 147 | + // Normalize and add a feature into the cluster. |
| 148 | + // This is currently used only for testing. |
| 149 | + double l2_norm = 0.0; |
| 150 | + for (uint i = 0; i < feature.size(); i++) l2_norm += feature[i] * feature[i]; |
| 151 | + |
| 152 | + if (l2_norm > 0.0) |
| 153 | + for (uint i = 0; i < feature.size(); i++) feature[i] /= l2_norm; |
| 154 | + |
| 155 | + if (features_.find(fingerprint) == features_.end()) { |
| 156 | + // Update the cluster if it's a new template |
| 157 | + features_[fingerprint] = feature; |
| 158 | + UpdateTemplate(fingerprint, true); |
| 159 | + } else { |
| 160 | + features_[fingerprint] = feature; |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +QueryClusterer::~QueryClusterer() { |
| 165 | + for (auto &cluster : clusters_) delete cluster; |
| 166 | +} |
| 167 | + |
| 168 | +} // namespace brain |
| 169 | +} // namespace peloton |
0 commit comments