Version 2.0. Improved Knn and MBR

parent ee7b7843
build/
cmake-build-debug/
.idea/
......@@ -208,6 +208,21 @@ namespace gract_ct {
return static_cast<uint64_t >(x * x + y * y);
}
uint64_t max_distance_knn(const region &r, const point &p){
uint64_t x, y;
if (p.m_x < r.m_min_p.m_x + (r.m_max_p.m_x - r.m_min_p.m_x) / 2) {
x = r.m_max_p.m_x - p.m_x;
} else {
x = p.m_x - r.m_min_p.m_x;
}
if(p.m_y < r.m_min_p.m_y + (r.m_max_p.m_y - r.m_min_p.m_y)/2){
y = r.m_max_p.m_y - p.m_y;
}else{
y = p.m_y - r.m_min_p.m_y;
}
return static_cast<uint64_t >(x * x + y * y);
}
double_t distance(const point &p, const point &p1){
double_t x = 0, y = 0;
x = std::abs(p1.m_x - p.m_x);
......@@ -216,10 +231,10 @@ namespace gract_ct {
}
uint64_t distance_knn(const point &p, const point &p1){
uint64_t x = 0, y = 0;
x = std::abs(p1.m_x - p.m_x);
y = std::abs(p1.m_y - p.m_y);
return x * x + y * y;
int64_t x = 0, y = 0;
x = p1.m_x - p.m_x;
y = p1.m_y - p.m_y;
return static_cast<uint64_t >(x * x + y * y);
}
bool touches_coord(size_type p_min, size_type p_max, size_type r_min, size_type r_max){
......
This diff is collapsed.
This diff is collapsed.
/***
BSD 2-Clause License
Copyright (c) 2018, Adrián
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
//
// Created by Adrián on 09/03/2019.
//
#ifndef GRACT_KNN_SUPPORT_HELPER_HPP
#define GRACT_KNN_SUPPORT_HELPER_HPP
#include <geometry.hpp>
#include <queue>
namespace gract_ct {
namespace knn_support_helper {
typedef uint64_t size_type;
typedef uint64_t distance_type;
struct knn_element {
bool is_point = false;
bool is_leaf = false;
size_type id;
point min;
point max;
distance_type distance;
distance_type distance_max;
size_type t;
size_type log_idx = 0;
size_type log_pos = 0;
size_type mov_pos = 0;
size_type tim_pos = 0;
size_type level = 0;
knn_element(size_type _id, point _p, distance_type _dist, distance_type _dist_max,
size_type _t, size_type _log_idx, size_type _log_pos,
size_type _mov_pos, size_type _tim_pos, bool _leaf, bool _obj){
is_point = _obj;
is_leaf = _leaf;
id = _id;
min = _p;
distance = _dist;
distance_max = _dist_max;
t = _t;
log_idx = _log_idx;
log_pos = _log_pos;
mov_pos = _mov_pos;
tim_pos = _tim_pos;
}
knn_element(size_type _id, point _min, point _max, distance_type _dist,
distance_type _dist_max, size_type _t, size_type _level){
is_point = false;
is_leaf = false;
id = _id;
min = _min;
max = _max;
distance = _dist;
distance_max = _dist_max;
t = _t;
level = _level;
}
};
class knn_element_comparator {
public:
bool operator()(const knn_element &lce, const knn_element &rce){
if(lce.distance == rce.distance) return lce.distance_max > rce.distance_max;
return lce.distance > rce.distance;
}
};
typedef std::priority_queue<knn_element, std::vector<knn_element>, knn_element_comparator> pq_knn_element_type;
typedef std::priority_queue<uint64_t , std::vector<uint64_t >, std::less<uint64_t >> pq_knn_movement_type;
}
}
#endif //GRACT_KNN_SUPPORT_HELPER_HPP
This diff is collapsed.
......@@ -170,17 +170,14 @@ namespace gract_ct {
bool exist_snap = false;
bool exist_log = false;
point p;
while(t < t_end && idx_log < n_snapshot && !exist_snap && !exist_log){
idx_log++;
if(idx_log < n_snapshot){
auto snap = graCT->get_snapshot(idx_log);
exist_snap = snap->find_object_position(oid, p);
exist_log = log_obj->exist_log(idx_log, n_snapshot);
t = idx_log * period_snapshot;
}else{
return;
}
while((idx_log+1)*period_snapshot <= t_end && (idx_log+1) < n_snapshot && !exist_snap && !exist_log){
++idx_log;
auto snap = graCT->get_snapshot(idx_log);
exist_snap = snap->find_object_position(oid, p);
exist_log = log_obj->exist_log(idx_log, n_snapshot);
t = idx_log * period_snapshot;
};
if(!exist_log && !exist_snap) return;
log_reap_movement_pos = log_obj->get_start_reap_movement(idx_log);
log_reap_time_pos = log_obj->get_start_reap_time(idx_log);
if(exist_snap){
......@@ -557,9 +554,9 @@ namespace gract_ct {
template<class index>
std::vector<data_traj> search_trajectory_run(const uint64_t oid, const uint64_t t_start, const uint64_t t_end,
void search_trajectory_run(const uint64_t oid, const uint64_t t_start, const uint64_t t_end,
const uint64_t period_snapshot, const uint64_t n_snapshot,
const index* graCT){
const index* graCT, std::vector<data_traj> &traj){
uint64_t t_after_snap = t_start % period_snapshot;
uint64_t idx_snap = t_start / period_snapshot;
......@@ -567,7 +564,7 @@ namespace gract_ct {
//return _search_object_left(oid, t_q, idx_snap, t_snap, period_snapshot, n_snapshot, p, graCT);
//Para poder facer busqueda dende a dereita a snapshot da esquerda ten que ser menor que a ultima
std::vector<data_traj> traj;
//std::vector<data_traj> traj;
if (t_after_snap > period_snapshot/2 && idx_snap < n_snapshot - 1) {
idx_snap++;
t_snap = idx_snap * period_snapshot;
......@@ -575,7 +572,7 @@ namespace gract_ct {
} else {
_search_trajectory_left(oid, t_start, t_end, idx_snap, t_snap, period_snapshot, n_snapshot, traj, graCT);
}
return traj;
//return traj;
}
}
......
This diff is collapsed.
......@@ -17,12 +17,12 @@ namespace gract_ct {
std::map<int64_t, uint64_t> m_succ_map;
std::vector<int64_t>* m_repair_string;
std::vector<int64_t>* m_terminals;
std::map<int64_t, u_char>* m_map_terminals;
std::map<int64_t, char>* m_map_terminals;
typedef std::map<int64_t, uint64_t>::iterator it_type;
public:
split_repair_helper(std::vector<int64_t>* repair_string, std::vector<int64_t>* terminals,
std::map<int64_t, u_char>* map_terminals){
std::map<int64_t, char>* map_terminals){
m_repair_string = repair_string;
m_terminals = terminals;
m_map_terminals = map_terminals;
......@@ -67,7 +67,7 @@ namespace gract_ct {
m_repair_string->at(i) = -max;
if(m_map_terminals->find(-max) == m_map_terminals->end()){
m_terminals->push_back(-max);
m_map_terminals->insert(std::pair<int64_t, u_char>(-max, 'a'));
m_map_terminals->insert(std::pair<int64_t, char>(-max, 'a'));
}
}
......
......@@ -468,7 +468,7 @@ namespace gract_ct {
sdsl::bit_vector checked(n_objects, 0);
for(uint64_t i = 0; i < candidates.size(); i++){
uint64_t oid = candidates[i].m_id;
if(!checked[oid] && !solution[oid]){
if(/*!checked[oid] &&*/ !solution[oid]){
auto log_obj = graCT->get_log_object(oid);
uint64_t log_pos = log_obj->get_start_log(idx_log);
uint64_t log_reap_movement_pos = log_obj->get_start_reap_movement(idx_log);
......@@ -493,7 +493,7 @@ namespace gract_ct {
sdsl::int_vector<> reap_candidates = graCT->reap[idx_log];
for(uint64_t i = 0; t_snap < t_end && i < reap_candidates.size(); i++){
uint64_t oid = reap_candidates[i];
if(!checked[oid] && !solution[oid]){
if(/*!checked[oid] &&*/ !solution[oid]){
uint64_t t = t_snap;
auto log_obj = graCT->get_log_object(oid);
uint64_t log_pos = log_obj->get_start_log(idx_log);
......@@ -559,7 +559,7 @@ namespace gract_ct {
r_i.m_min_p.m_y, r_i.m_max_p.m_y);
for(uint64_t i = 0; i < candidates.size(); i++){
uint64_t oid = candidates[i].m_id;
if(!checked[oid] && !solution[oid]){
if(/*!checked[oid] && */!solution[oid]){
auto log_obj = graCT->get_log_object(oid);
uint64_t log_pos = log_obj->get_end_log(idx_log, n_snapshot);
uint64_t log_reap_movement_pos = log_obj->get_end_reap_movement(idx_log, n_snapshot);
......@@ -585,7 +585,7 @@ namespace gract_ct {
sdsl::int_vector<> disap_candidates = graCT->disap[idx_log];
for(uint64_t i = 0; t_snap > t_start && i < disap_candidates.size(); i++){
uint64_t oid = disap_candidates[i];
if(!checked[oid] && !solution[oid]){
if( /*!checked[oid] && */ !solution[oid]){
uint64_t t = t_snap;
auto log_obj = graCT->get_log_object(oid);
uint64_t log_pos = log_obj->get_end_log(idx_log, n_snapshot);
......
......@@ -343,7 +343,7 @@ namespace gract_ct{
r_i.m_min_p.m_y, r_i.m_max_p.m_y);
for(uint64_t i = 0; i < candidates.size(); i++){
uint64_t oid = candidates[i].m_id;
if(!checked[oid]){
//if(!checked[oid]){
auto log_obj = graCT->get_log_object(oid);
uint64_t log_pos = log_obj->get_start_log(idx_log);
uint64_t log_reap_movement_pos = log_obj->get_start_reap_movement(idx_log);
......@@ -357,7 +357,7 @@ namespace gract_ct{
results.push_back(id_point(oid, p));
};
checked[oid] = 1;
}
//}
}
return results;
}
......@@ -378,7 +378,7 @@ namespace gract_ct{
sdsl::int_vector<> disap_candidates = graCT->disap[idx_log];
for(uint64_t i = 0; i < graCT->disap[idx_log].size(); i++){
uint64_t oid = graCT->disap[idx_log][i];
if(!checked[oid]){
// if(!checked[oid]){
uint64_t t = t_snap;
auto log_obj = graCT->get_log_object(oid);
uint64_t log_pos = log_obj->get_end_log(idx_log, n_snapshot);
......@@ -406,14 +406,14 @@ namespace gract_ct{
std::cout << "Error: search_object_right. oid = " << oid << " t = " << t_q << std::endl;
exit(20);
}
}
// }
}
auto snap = graCT->get_snapshot(idx_snap);
std::vector<id_point> candidates = snap->find_objects_in_region(r_i.m_min_p.m_x, r_i.m_max_p.m_x,
r_i.m_min_p.m_y, r_i.m_max_p.m_y);
for(uint64_t i = 0; i < candidates.size(); i++){
uint64_t oid = candidates[i].m_id;
if(!checked[oid]){
// if(!checked[oid]){
auto log_obj = graCT->get_log_object(oid);
uint64_t log_pos = log_obj->get_end_log(idx_log, n_snapshot);
uint64_t log_reap_movement_pos = log_obj->get_end_reap_movement(idx_log, n_snapshot);
......@@ -427,7 +427,7 @@ namespace gract_ct{
results.push_back(id_point(oid, p));
};
checked[oid] = 1;
}
// }
}
return results;
}
......
......@@ -23,7 +23,7 @@ namespace gract_ct {
uint64_t x;
uint64_t y;
uint64_t weight;
double_t distance;
uint64_t distance;
uint64_t logIdx = 0;
uint64_t posLog = 0;
uint64_t posMove = 0;
......@@ -48,7 +48,7 @@ namespace gract_ct {
pq_knn_candidate_element() = default;
pq_knn_candidate_element(uint64_t p_id, uint64_t p_t, uint64_t p_x, uint64_t p_y, double_t p_distance, uint64_t p_weight){
pq_knn_candidate_element(uint64_t p_id, uint64_t p_t, uint64_t p_x, uint64_t p_y, uint64_t p_distance, uint64_t p_weight){
id = p_id;
t = p_t;
x = p_x;
......
/***
BSD 2-Clause License
Copyright (c) 2018, Adrián
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/
//
// Created by Adrián on 10/01/2019.
//
#ifndef UTIL_MEM_HPP
#define UTIL_MEM_HPP
#include <unistd.h>
namespace util {
namespace memory {
int64_t total_memory_bytes() {
#if USE_SYSCTL_HW_MEMSIZE
uint64_t mem;
size_t len = sizeof(mem);
sysctlbyname("hw.memsize", &mem, &len, NULL, 0);
unsigned phys_pages = mem/sysconf(_SC_PAGE_SIZE);
#else
return sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE);
#endif
}
int64_t total_memory_megabytes(){
return (total_memory_bytes() >> 20);
}
int64_t total_memory_gigabytes(){
return (total_memory_bytes() >> 30);
}
}
}
#endif //UTIL_MEM_HPP
......@@ -49,9 +49,10 @@ int main(int argc, char **argv) {
//-2
auto run_search_trajectory = [&](){
std::vector<data_traj> trajectory;
finQ >> id >> tstart >> tend >> type_query;
std::cout << "traj" << std::endl;
m_index_ct.search_trajectory(id, tstart, tend);
m_index_ct.search_trajectory(id, tstart, tend, trajectory);
};
//-3
......@@ -71,7 +72,16 @@ int main(int argc, char **argv) {
//-5
auto run_knn = [&](){
finQ >> tstart >> minX >> minY >> k >> type_query;
m_index_ct.find_knn(k, point(minX, minY), tstart);
std::cout << "knn" << std::endl;
m_index_ct.find_knn(k, point(minX, minY), tstart, 10);
};
//-6
auto run_mbr = [&]() {
finQ >> id >> tstart >> tend >> type_query;
std::cout << "mbr" << std::endl;
region mbr;
m_index_ct.search_mbr(id, tstart, tend, mbr);
};
......@@ -94,6 +104,9 @@ int main(int argc, char **argv) {
case -5:
run_knn();
break;
case -6:
run_mbr();
break;
default:
std::cout << "Query not implemented" << std::endl;
break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment