Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 112 additions & 71 deletions java-ortools-routing/input.json
Original file line number Diff line number Diff line change
@@ -1,74 +1,115 @@
{
"distance_matrix": [
[
0, 548, 776, 696, 582, 274, 502, 194, 308, 194, 536, 502, 388, 354, 468,
776, 662
],
[
548, 0, 684, 308, 194, 502, 730, 354, 696, 742, 1084, 594, 480, 674, 1016,
868, 1210
],
[
776, 684, 0, 992, 878, 502, 274, 810, 468, 742, 400, 1278, 1164, 1130,
788, 1552, 754
],
[
696, 308, 992, 0, 114, 650, 878, 502, 844, 890, 1232, 514, 628, 822, 1164,
560, 1358
],
[
582, 194, 878, 114, 0, 536, 764, 388, 730, 776, 1118, 400, 514, 708, 1050,
674, 1244
],
[
274, 502, 502, 650, 536, 0, 228, 308, 194, 240, 582, 776, 662, 628, 514,
1050, 708
],
[
502, 730, 274, 878, 764, 228, 0, 536, 194, 468, 354, 1004, 890, 856, 514,
1278, 480
],
[
194, 354, 810, 502, 388, 308, 536, 0, 342, 388, 730, 468, 354, 320, 662,
742, 856
],
[
308, 696, 468, 844, 730, 194, 194, 342, 0, 274, 388, 810, 696, 662, 320,
1084, 514
],
[
194, 742, 742, 890, 776, 240, 468, 388, 274, 0, 342, 536, 422, 388, 274,
810, 468
],
[
536, 1084, 400, 1232, 1118, 582, 354, 730, 388, 342, 0, 878, 764, 730,
388, 1152, 354
],
[
502, 594, 1278, 514, 400, 776, 1004, 468, 810, 536, 878, 0, 114, 308, 650,
274, 844
],
[
388, 480, 1164, 628, 514, 662, 890, 354, 696, 422, 764, 114, 0, 194, 536,
388, 730
],
[
354, 674, 1130, 822, 708, 628, 856, 320, 662, 388, 730, 308, 194, 0, 342,
422, 536
],
[
468, 1016, 788, 1164, 1050, 514, 514, 662, 320, 274, 388, 650, 536, 342,
0, 764, 194
],
[
776, 868, 1552, 560, 674, 1050, 1278, 742, 1084, 810, 1152, 274, 388, 422,
764, 0, 798
],
[
662, 1210, 754, 1358, 1244, 708, 480, 856, 514, 468, 354, 844, 730, 536,
194, 798, 0
]
"defaults": {
"vehicles": {
"start_location": {
"lon": 7.625,
"lat": 51.9622
},
"end_location": {
"lon": 7.625,
"lat": 51.9622
},
"start_time": "2024-03-19T11:00:00+00:00",
"capacity": 5
},
"stops": {
"quantity": 1
}
},
"vehicles": [
{
"id": "vehicle-1"
},
{
"id": "vehicle-2"
}
],
"num_vehicles": 4,
"depot": 0
"stops": [
{
"id": "location-1",
"location": {
"lon": 7.6228,
"lat": 51.9624
}
},
{
"id": "location-2",
"location": {
"lon": 7.6353,
"lat": 51.9584
}
},
{
"id": "location-3",
"location": {
"lon": 7.6233,
"lat": 51.9638
}
},
{
"id": "location-4",
"location": {
"lon": 7.6163,
"lat": 51.9635
}
},
{
"id": "location-5",
"location": {
"lon": 7.6045,
"lat": 51.9509
}
},
{
"id": "location-6",
"location": {
"lon": 7.6292,
"lat": 51.9577
}
},
{
"id": "location-7",
"location": {
"lon": 7.6385,
"lat": 51.9694
}
},
{
"id": "location-8",
"location": {
"lon": 7.6628,
"lat": 51.9616
}
},
{
"id": "location-9",
"location": {
"lon": 7.6217,
"lat": 51.9667
}
},
{
"id": "location-10",
"location": {
"lon": 7.6299,
"lat": 51.9582
}
}
],
"duration_matrix": [
[0, 831, 968, 648, 773, 689, 872, 997, 685, 698, 613, 613, 613, 613],
[822, 0, 415, 257, 273, 83, 152, 266, 212, 74, 262, 262, 262, 262],
[925, 466, 0, 342, 499, 454, 447, 624, 294, 463, 372, 372, 372, 372],
[647, 293, 456, 0, 207, 168, 275, 452, 88, 178, 101, 101, 101, 101],
[775, 385, 584, 202, 0, 280, 387, 544, 238, 289, 229, 229, 229, 229],
[739, 143, 455, 211, 228, 0, 191, 308, 249, 9, 179, 179, 179, 179],
[843, 152, 415, 260, 348, 158, 0, 274, 201, 149, 297, 297, 297, 297],
[1001, 270, 573, 418, 466, 276, 293, 0, 370, 267, 455, 455, 455, 455],
[687, 217, 444, 89, 247, 208, 199, 375, 0, 214, 141, 141, 141, 141],
[748, 134, 446, 203, 219, 9, 182, 299, 240, 0, 188, 188, 188, 188],
[613, 272, 372, 102, 221, 129, 320, 437, 139, 138, 0, 0, 0, 0],
[613, 272, 372, 102, 221, 129, 320, 437, 139, 138, 0, 0, 0, 0],
[613, 272, 372, 102, 221, 129, 320, 437, 139, 138, 0, 0, 0, 0],
[613, 272, 372, 102, 221, 129, 320, 437, 139, 138, 0, 0, 0, 0]
]
}
3 changes: 2 additions & 1 deletion java-ortools-routing/java-ortools.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
},
"extensions": {
"recommendations": ["ms-vscode-remote.remote-containers"]
}
},
"settings": {}
}
71 changes: 46 additions & 25 deletions java-ortools-routing/src/main/java/com/nextmv/example/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,71 @@

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.stream.Collectors;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

public class Input {
@SerializedName(value = "distance_matrix")
public final long[][] distanceMatrix;
@SerializedName(value = "num_vehicles")
public final int vehicleNumber;
public final int depot;
public final class Location {
public double lat;
public double lon;

public Input(int vehicleNumber, int depot, long[][] distanceMatrix) {
this.distanceMatrix = new long[vehicleNumber+1][vehicleNumber+1];
this.vehicleNumber = vehicleNumber;
this.depot = depot;
for (int i = 0; i < vehicleNumber+1; i++) {
for (int j = 0; j < vehicleNumber+1; j++) {
this.distanceMatrix[i][j] = distanceMatrix[i][j];
}
public Location(double lat, double lon) {
this.lat = lat;
this.lon = lon;
}
}

public long[][] getDistanceMatrix() {
return this.distanceMatrix;
public final class Vehicle {
public String id;
public double speed;

public Vehicle(String id) {
this.id = id;
this.speed = 10.0; // Default speed if not specified.
}
}

public int getVehicleNumber() {
return this.vehicleNumber;
public final class Stop {
public String id;
public Location location;
}

public int getDepot() {
return this.depot;
@SerializedName(value = "duration_matrix")
public final long[][] durationMatrix;
public final List<Vehicle> vehicles;
public final List<Stop> stops;

public Input(
long[][] durationMatrix,
List<Vehicle> vehicles,
List<Stop> stops) {
if (durationMatrix != null) {
for (long[] row : durationMatrix) {
if (row.length != durationMatrix.length) {
throw new IllegalArgumentException("Duration matrix must be square.");
}
}
if (durationMatrix.length != stops.size() * 2 * vehicles.size()) {
throw new IllegalArgumentException(
"Duration matrix size does not match number of stops and vehicles (n_stops + 2 * n_vehicles).");
}
}

this.durationMatrix = durationMatrix;
this.vehicles = vehicles;
this.stops = stops;
}

public static Input fromString(String path) {
Gson gson = new Gson();
// Read stdin if no path is provided.
if (path.isEmpty()) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
return gson.fromJson(
reader.lines().collect(Collectors.joining("\n")), Input.class
);
reader.lines().collect(Collectors.joining("\n")), Input.class);
} catch (java.io.IOException e) {
System.err.println("Error reading stdin: " + e.getMessage());
System.exit(1);
Expand All @@ -54,9 +76,8 @@ public static Input fromString(String path) {
// Read the path otherwise.
try {
return gson.fromJson(
java.nio.file.Files.readString(java.nio.file.Paths.get(path)),
Input.class
);
java.nio.file.Files.readString(java.nio.file.Paths.get(path)),
Input.class);
} catch (java.io.IOException e) {
System.err.println("Error reading '" + path + "': " + e.getMessage());
System.exit(1);
Expand Down
Loading
Loading