1 year ago

#379610

test-img

Raccoon

Java nearest neighbour best route between cities

I am trying to get the optimum route between multiple cities based on their lat and lon. I am using the nearest neighbor algorithm but I can't seem to figure it out. I just started learning Java.

Can someone help me or push me to the right directions?

I made two classes: City and Route

City class

public class City {
    private String name;
    private double latitude;
    private double longitude;

    public City(String name, double latitude, double longitude) {
        this.name = name;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public String toString() {
        return name + ": " + latitude + ", " + longitude + "";
    }

    public double distance(City city) {

        final int R = 6371; // Radius of the earth

        double latDistance = Math.toRadians(city.getLatitude() - this.latitude);
        double lonDistance = Math.toRadians(city.getLongitude() - this.longitude);
        double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
                + Math.cos(Math.toRadians(this.latitude)) * Math.cos(Math.toRadians(city.getLatitude()))
                * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distance = R * c * 1000; // convert to meters

        double height = 0.0;

        distance = Math.pow(distance, 2) + Math.pow(height, 2);

        return Math.sqrt(distance);
    }
}

Route class

import java.util.ArrayList;

public class Route {
    private ArrayList<City> cities;

    public Route() {
        this.cities = new ArrayList<>();
    }

    public ArrayList<City> getCities() {
        return cities;
    }

    public void print() {
        System.out.println("------- Cities inside this route -------");
        for (City city: cities) {
            System.out.println(city);
        }

        System.out.println("------- Distance -------");
        measureDistances();
    }

    public void addCity(City city) {
        cities.add(city);
    }

    public void measureDistances() {
        City previousCity = cities.get(1);
        System.out.println(cities.get(1));
        for (City city: cities) {
            System.out.println(city.distance(previousCity));
            previousCity = city;
        }
    }
}

And of course my main class where I create a route and add multiple cities.

public class Main {
    public static void main(String[] args) {
        Route route = new Route();
        route.addCity(new City("Zwolle", 52.5125, 6.09444));
        route.addCity(new City("Assen", 52.993668, 6.558259));
        route.addCity(new City("Hardewijk", 52.3422025, 5.6367423));

        route.print();

        
    }
}

java

algorithm

nearest-neighbor

0 Answers

Your Answer

Accepted video resources