All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.graphhopper.routeopt.examples.TSP_BiggestCitiesInGermanyExample Maven / Gradle / Ivy

There is a newer version: 0.8.2.1
Show newest version
package com.graphhopper.routeopt.examples;

import com.graphhopper.routeopt.client.api.SolutionApi;
import com.graphhopper.routeopt.client.api.VrpApi;
import com.graphhopper.routeopt.client.model.Address;
import com.graphhopper.routeopt.client.model.Algorithm;
import com.graphhopper.routeopt.client.model.JobId;
import com.graphhopper.routeopt.client.model.Response;
import com.graphhopper.routeopt.client.model.Service;
import com.graphhopper.routeopt.client.model.Vehicle;
import com.graphhopper.routeopt.client.model.Request;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by schroeder on 21/05/15.
 */
public class TSP_BiggestCitiesInGermanyExample {

    public static void main(String[] args) throws Exception {
        new TSP_BiggestCitiesInGermanyExample().start();
    }

    private void start() throws Exception {

        Request request = createRequest();
        VrpApi vrpApi = new VrpApi();

        // enable debugging for sending and receiving requests
        // ApiClient client = new ApiClient().setDebugging(true);
        // vrpApi.setApiClient(client);        
        
        String key = System.getProperty("graphhopper.key", "");
        JobId jobId = vrpApi.postVrp(key, request);

        System.out.println(getClass() + ", jobId: " + jobId.getJobId());

        SolutionApi solApi = new SolutionApi();
        Response rsp;

        while (true) {
            rsp = solApi.getSolution(key, jobId.getJobId());
            if (rsp.getStatus().equals(Response.StatusEnum.FINISHED)) {
                break;
            }
            Thread.sleep(200);
        }
        System.out.println(rsp);

    }

    private Request createRequest() {
        Request request = new Request();
        request.setAlgorithm(new Algorithm());

        /*
        specify vehicles
         */
        List vehicles = new ArrayList();

        Vehicle v = new Vehicle();
        v.setVehicleId("v1");
        v.setStartAddress(createAddress("berlin", 52.537, 13.406));
        vehicles.add(v);
        request.setVehicles(vehicles);

        /*
        specify your services
         */
        List services = new ArrayList();

        services.add(createService("hamburg", 53.552, 9.999));
        services.add(createService("munich", 48.145, 11.570));
        services.add(createService("cologne", 50.936, 6.957));
        services.add(createService("frankfurt", 50.109, 8.670));

        request.setServices(services);

        return request;
    }

    public Address createAddress(String locationId, double lat, double lon) {
        Address a = new Address();
        a.setLat(lat);
        a.setLon(lon);
        a.setLocationId(locationId);
        return a;
    }

    public Service createService(String id, double lat, double lon) {
        Service service = new Service();
        service.setId(id);
        service.setType(Service.TypeEnum.SERVICE);
        service.setAddress(createAddress(id, lat, lon));
        return service;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy