QQ - Adding services correlating to costs

Hiyas, hopefully some quick questions.

I’ve built my cost matrix from lat,longs - Now I need to setup services.

Service s1 = Service.Builder.newInstance("1").addSizeDimension(0, 1).setLocation(Location.newInstance("1")).build();

Adding a service, newInstance(“1”) is in relation to an ID?

This then correlates to costMatrixBuilder.addTransportDistance(“ **idFrom** ”, “ **idTo** ”, **distance** ); where the idFrom is “1” ?

So I only need to loop through all idFrom’s I used in the cost matrix and add a service?

Then when building the vrp, can I add all services?
In the examples it has;
.addVehicle(vehicle).addJob(s1).addJob(s2).addJob(s3).build();
Instead I setup the jobs as a collection and pass it via .addVehicle(vehicle).addAllJobs().build();

Is my thinking correct?

Thanks,

I think I’ve answered my own questions.

//Creating all services
for (int i = 0; i < points.size(); i++) {
            int offset = i + 1;
            Service service = Service.Builder.newInstance(String.valueOf(i)).addSizeDimension(WEIGHT_INDEX, 1).setLocation(Location.newInstance(points.get(i), points.get(offset))).build();
            vrpBuilder.addJob(service);
            i++;
	}

//Create cost matrix of added jobs
Thread one = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 1; i <= section; i++){
                        for (int j = 1; j <= vrpBuilder.getAddedJobs().size(); j++) {
                            double distance = pointToPointDistance(points.get(i*2-2), points.get(i*2-1), points.get(j*2-2), points.get(j*2-1));
                            costMatrixBuilder.addTransportDistance(String.valueOf(i), String.valueOf(j), distance);
                        }
                    }
                }
            });

//Build matrix
VehicleRoutingTransportCosts costMatrix = costMatrixBuilder.build();

//Build the VRP
 VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().setFleetSize(FleetSize.INFINITE).setRoutingCost(costMatrix).addAllJobs(vrpBuilder.getAddedJobs()).build();

//The rest
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
        vra.setMaxIterations(250);

        Collection<VehicleRoutingProblemSolution> solutions = vra.searchSolutions();

        SolutionPrinter.print(Solutions.bestOf(solutions));
        SolutionPrinter.print(vrp, Solutions.bestOf(solutions), SolutionPrinter.Print.VERBOSE);

Results in
Execution time in seconds : 19.43379547
[costs=30.31927841296698]
[#vehicles=0]
±-------------------------+
| problem |
±--------------±---------+
| indicator | value |
±--------------±---------+
| noJobs | 238 |
| noServices | 238 |
| noShipments | 0 |
| noBreaks | 0 |
| fleetsize | INFINITE |
±-------------------------+
±---------------------------------------------------------+
| solution |
±--------------±-----------------------------------------+
| indicator | value |
±--------------±-----------------------------------------+
| costs | 30.31927841296698 |
| noVehicles | 0 |
| unassgndJobs | 238 |
±---------------------------------------------------------+
±-------------------------------------------------------------------------------------------------------------------------------+
| detailed solution |
±--------±---------------------±----------------------±----------------±----------------±----------------±----------------+
| route | vehicle | activity | job | arrTime | endTime | costs |
±-------------------------------------------------------------------------------------------------------------------------------+
±---------------+
| unassignedJobs |

Just haven’t added any vehicles.

I’m having issues with assigning the service to a distance;

The service is looping through an array, starting at 0.
setLocation is setting the coordinates.

Service service = Service.Builder.newInstance(String.valueOf(i)).addSizeDimension(WEIGHT_INDEX, 1).setLocation(Location.newInstance(points.get(i), points.get(offset))).build();

costMatrixBuilder.addTransportDistance(String.valueOf(i-1), String.valueOf(j-1), distance);

Service first iteration is newInstance(0)
Adding distance first iteration is addTransportDistance(0, 0, distance)
second iteration is addTransportDistance(0, 1, distance)

Results in; distance value for relation from [x=-41.2212078987679][y=174.817758293497] to [x=-41.2210347264532][y=174.817871376896] does not exist

So I’m not getting a distance to the service ID when running. Unsure how to merge the 2.

How is the best way to output the matrix to see what is missing?

Got it working.
Issue was in my last thread I was using an incomplete loop, this caused some of the points being excluded.

I would still like to know the best way to output the matrix though, if anyone has the info please share.