Git Product home page Git Product logo

closy's Introduction

Closy

codefactor maven-central javadoc Java license

Closy is a simple library for efficient nearest neighbor computations. It is designed generic and can be used with any class that defines a metric.

It is able to compute the nearest neighbor to a given point, as well as retrieving the k-nearest neighbors and also all neighbors within a given range. The corresponding methods are:

  • Optional<E> getNearestNeighbor(E point)
  • Collection<E> getKNearestNeighbors(E point, int k)
  • Collection<E> getNeighborhood(E point, double range)

Requirements

  • Requires at least Java 9

Download

Maven:

<dependency>
   <groupId>io.github.zabuzard.closy</groupId>
   <artifactId>closy</artifactId>
   <version>1.2.1</version>
</dependency>

Jar downloads are available from the release section.

Documentation

Getting started

  1. Integrate Closy into your project. The API is contained in the module io.github.zabuzard.closy.
  2. Create an implementation of Metric<E> for your custom E objects
  3. Create an algorithm using NearestNeighborComputations#of(Metric<E>)
  4. Add your objects to the algorithm using NearestNeighborComputation#add(E)
  5. Execute nearest neighbor computations using the methods offered by NearestNeighborComputation

Example

Consider the following simple class for points in a 2-dimensional space

class Point {
    private final int x;
    private final int y;

    // constructor, getter, equals, hashCode and toString omitted
}

As first step, we need to define a Metric that operates on Point. We decide for the Euclidean distance:

class EuclideanDistance implements Metric<Point> {
	@Override
	public double distance(final Point a, final Point b) {
		return Math.sqrt(Math.pow(b.getX() - a.getX(), 2) + Math.pow(b.getY() - a.getY(), 2));
	}
}

Next, we create an algorithm using this metric and then add some points to it:

var metric = new EuclideanDistance();
var algo = NearestNeighborComputations.of(metric);

algo.add(Point.of(1, 2));
algo.add(Point.of(5, 7));
algo.add(Point.of(-10, 4));
algo.add(Point.of(9, 8));
algo.add(Point.of(3, 3));

Finally, we use the methods provided by NearestNeighborComputation to compute some nearest neighbors:

var point = Point.of(4, 2);
Optional<Point> neighbor = algo.getNearestNeighbor(point);          // (3, 3)
Collection<Point> neighbors = algo.getKNearestNeighbors(point, 3);  // [(3, 3), (1, 2), (5, 7)]
Collection<Point> neighborhood = algo.getNeighborhood(point, 3.5);  // [(1, 2), (3, 3)]

Background

The current implementation uses Cover Trees, based on the implementation described in Cover Trees for Nearest Neighbor (Beygelzimer et al. in ICML '06).

A detailed description and benchmark can be found in Multi-Modal Route Planning in Road and Transit Networks (Daniel Tischner '18).

The following is a benchmark of Closy version 1.0. The experiment consists of continuous insertion of nodes, for each of three road networks respectively, and then measuring random nearest neighbor queries, i.e. the execution time of the algorithm. Measurements are done for tree sizes of 1, 10000 and then in steps of

  1. Each measurement is averaged over 1000 queries using randomly selected nodes.

Close version 1.0 benchmark

closy's People

Contributors

zabuzard avatar

Stargazers

 avatar  avatar  avatar  avatar

Forkers

wirefreethought

closy's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.