K-Nearest Neighbor Graph Testing Library
C++ Python library that is able to import exisiting NN-structures; Implements Property Testing Algorithm that rejects with high probability if queries to given structure are epsilon-far from giving a K-Nearest Neighbor Graph
oracle.hpp
1 /*
2 Copyright 2018 Dennis Rohde
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 
6 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 
8 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 */
10 
11 #pragma once
12 
13 #include <iostream>
14 
15 #include <boost/chrono/include.hpp>
16 #include <boost/python/numpy.hpp>
17 
18 #include "relation.hpp"
19 
20 namespace p = boost::python;
21 namespace np = boost::python::numpy;
22 
23 template <typename T = double>
24 class Query_Oracle {
25  typedef unsigned long long index_type;
26 
27  p::object function;
28  unsigned long long query_time;
29 
30 public:
31  explicit Query_Oracle(PyObject *callable) : query_time(0) {
32  try {
33  index_type v = 0;
34  auto result = p::call<np::ndarray>(callable, v);
35  if (result.get_nd() != 2) {
36  throw std::invalid_argument("Function must return 2-dim. ndarray of correct type!");
37  }
38  if (result.get_dtype() != np::dtype::get_builtin<T>()) {
39  throw std::invalid_argument("Function must return 2-dim. ndarray of correct type!");
40  }
41  function = p::object{p::handle<>(p::borrowed(callable))};
42  } catch (std::exception &e) {
43  std::cerr << "Need a function f: {0, ..., n-1} -> P(R^d)!" << std::endl;
44  std::cerr << e.what() << std::endl;
45  }
46  }
47 
48  auto query(const index_type i) {
49  p::list l;
50  np::ndarray result{np::array(l, np::dtype::get_builtin<T>())};
51  auto start = boost::chrono::thread_clock::now();
52  try {
53  result = p::call<np::ndarray>(function.ptr(), i);
54  } catch (std::exception &e) {
55  std::cerr << e.what() << std::endl;
56  }
57  auto stop = boost::chrono::thread_clock::now();
58  auto duration = (stop-start).count();
59  query_time += static_cast<unsigned long long>(duration);
60  return Relation<T>(result);
61  }
62 
63  void reset_timer() {
64  query_time = 0;
65  }
66 
67  unsigned long long time() const {
68  return query_time;
69  }
70 };
Definition: oracle.hpp:24
Definition: relation.hpp:24