diff --git a/ortools/constraint_solver/samples/SimpleRoutingProgram.java b/ortools/constraint_solver/samples/SimpleRoutingProgram.java index fdb740094f..aa909fe3c9 100644 --- a/ortools/constraint_solver/samples/SimpleRoutingProgram.java +++ b/ortools/constraint_solver/samples/SimpleRoutingProgram.java @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// Copyright 2010-2018 Google LLC // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -13,6 +13,8 @@ // [START program] // [START import] +import static java.lang.Math.abs; + import com.google.ortools.constraintsolver.Assignment; import com.google.ortools.constraintsolver.FirstSolutionStrategy; import com.google.ortools.constraintsolver.LongLongToLong; @@ -23,23 +25,25 @@ import com.google.ortools.constraintsolver.main; import java.util.logging.Logger; // [END import] -class SimpleRoutingProgram { - static { System.loadLibrary("jniortools");} +/** Minimal Routing example to showcase calling the solver.*/ +public class SimpleRoutingProgram { + static { + System.loadLibrary("jniortools"); + } - private static Logger logger = - Logger.getLogger(SimpleRoutingProgram.class.getName()); + private static final Logger logger = Logger.getLogger(SimpleRoutingProgram.class.getName()); - static void SimpleRoutingProgram() { + public static void main(String[] args) throws Exception { // Instantiate the data problem. // [START data] - final int num_location = 5; - final int num_vehicles = 1; + final int numLocation = 5; + final int numVehicles = 1; final int depot = 0; // [END data] // Create Routing Index Manager // [START index_manager] - RoutingIndexManager manager = new RoutingIndexManager(num_location, num_vehicles, depot); + RoutingIndexManager manager = new RoutingIndexManager(numLocation, numVehicles, depot); // [END index_manager] // Create Routing Model. @@ -49,28 +53,26 @@ class SimpleRoutingProgram { // Define cost of each arc. // [START arc_cost] - routing.setArcCostEvaluatorOfAllVehicles( - routing.registerTransitCallback( - new LongLongToLong() { - @Override - public long run(long from_node, long to_node) { - return 1; - } - })); + routing.setArcCostEvaluatorOfAllVehicles(routing.registerTransitCallback(new LongLongToLong() { + @Override + public long run(long fromIndex, long toIndex) { + return abs(fromIndex - toIndex); + } + })); // [END arc_cost] // Setting first solution heuristic. // [START parameters] - RoutingSearchParameters search_parameters = - RoutingSearchParameters.newBuilder() - .mergeFrom(main.defaultRoutingSearchParameters()) + RoutingSearchParameters searchParameters = + main.defaultRoutingSearchParameters() + .toBuilder() .setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC) .build(); // [END parameters] // Solve the problem. // [START solve] - Assignment solution = routing.solveWithParameters(search_parameters); + Assignment solution = routing.solveWithParameters(searchParameters); // [END solve] // Print solution on console. @@ -79,22 +81,18 @@ class SimpleRoutingProgram { // Inspect solution. long index = routing.start(0); logger.info("Route for Vehicle 0:"); - long route_distance = 0; + long routeDistance = 0; String route = ""; - while (routing.isEnd(index) == false) { + while (!routing.isEnd(index)) { route += manager.indexToNode(index) + " -> "; - long previous_index = index; + long previousIndex = index; index = solution.value(routing.nextVar(index)); - route_distance += routing.getArcCostForVehicle(previous_index, index, 0); + routeDistance += routing.getArcCostForVehicle(previousIndex, index, 0); } route += manager.indexToNode(index); logger.info(route); - logger.info("Distance of the route: " + route_distance + "m"); + logger.info("Distance of the route: " + routeDistance + "m"); // [END print_solution] } - - public static void main(String[] args) throws Exception { - SimpleRoutingProgram(); - } } // [END program] diff --git a/ortools/constraint_solver/samples/Tsp.java b/ortools/constraint_solver/samples/Tsp.java index be5df8cf1e..54e15def46 100644 --- a/ortools/constraint_solver/samples/Tsp.java +++ b/ortools/constraint_solver/samples/Tsp.java @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// Copyright 2010-2018 Google LLC // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -14,6 +14,7 @@ // [START program] // [START import] import static java.lang.Math.abs; + import com.google.ortools.constraintsolver.Assignment; import com.google.ortools.constraintsolver.FirstSolutionStrategy; import com.google.ortools.constraintsolver.LongLongToLong; @@ -26,7 +27,9 @@ import java.util.logging.Logger; /** Minimal TSP.*/ public class Tsp { - static { System.loadLibrary("jniortools"); } + static { + System.loadLibrary("jniortools"); + } private static final Logger logger = Logger.getLogger(Tsp.class.getName()); @@ -53,9 +56,9 @@ public class Tsp { {7, 8}, }; // Convert locations in meters using a city block dimension of 114m x 80m. - for (int i = 0; i < locations.length; i++) { - locations[i][0] *= 114; - locations[i][1] *= 80; + for (int[] element : locations) { + element[0] *= 114; + element[1] *= 80; } vehicleNumber = 1; depot = 0; @@ -95,8 +98,8 @@ public class Tsp { int toNode = indexManager_.indexToNode(toIndex); return distanceMatrix_[fromNode][toNode]; } - private long[][] distanceMatrix_; - private RoutingIndexManager indexManager_; + private final long[][] distanceMatrix_; + private final RoutingIndexManager indexManager_; } // [END manhattan_distance] diff --git a/ortools/constraint_solver/samples/TspDistanceMatrix.java b/ortools/constraint_solver/samples/TspDistanceMatrix.java index 5460bba307..e25316d283 100644 --- a/ortools/constraint_solver/samples/TspDistanceMatrix.java +++ b/ortools/constraint_solver/samples/TspDistanceMatrix.java @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// Copyright 2010-2018 Google LLC // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -25,7 +25,9 @@ import java.util.logging.Logger; /** Minimal TSP using distance matrix.*/ public class TspDistanceMatrix { - static { System.loadLibrary("jniortools"); } + static { + System.loadLibrary("jniortools"); + } private static final Logger logger = Logger.getLogger(TspDistanceMatrix.class.getName()); @@ -77,8 +79,8 @@ public class TspDistanceMatrix { int toNode = indexManager_.indexToNode(toIndex); return distanceMatrix_[fromNode][toNode]; } - private long[][] distanceMatrix_; - private RoutingIndexManager indexManager_; + private final long[][] distanceMatrix_; + private final RoutingIndexManager indexManager_; } // [END manhattan_distance] diff --git a/ortools/constraint_solver/samples/Vrp.java b/ortools/constraint_solver/samples/Vrp.java index ab75dadb20..437a5a837e 100644 --- a/ortools/constraint_solver/samples/Vrp.java +++ b/ortools/constraint_solver/samples/Vrp.java @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// Copyright 2010-2018 Google LLC // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -25,7 +25,9 @@ import java.util.logging.Logger; /** Minimal VRP.*/ public class Vrp { - static { System.loadLibrary("jniortools"); } + static { + System.loadLibrary("jniortools"); + } private static final Logger logger = Logger.getLogger(Vrp.class.getName()); @@ -77,8 +79,8 @@ public class Vrp { int toNode = indexManager_.indexToNode(toIndex); return distanceMatrix_[fromNode][toNode]; } - private long[][] distanceMatrix_; - private RoutingIndexManager indexManager_; + private final long[][] distanceMatrix_; + private final RoutingIndexManager indexManager_; } // [END manhattan_distance] diff --git a/ortools/constraint_solver/samples/VrpCapacity.java b/ortools/constraint_solver/samples/VrpCapacity.java index be8c2418f0..559689dfb0 100644 --- a/ortools/constraint_solver/samples/VrpCapacity.java +++ b/ortools/constraint_solver/samples/VrpCapacity.java @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// Copyright 2010-2018 Google LLC // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -17,10 +17,8 @@ import com.google.ortools.constraintsolver.Assignment; import com.google.ortools.constraintsolver.FirstSolutionStrategy; import com.google.ortools.constraintsolver.LongLongToLong; import com.google.ortools.constraintsolver.LongToLong; -import com.google.ortools.constraintsolver.RoutingDimension; import com.google.ortools.constraintsolver.RoutingIndexManager; import com.google.ortools.constraintsolver.RoutingModel; -import com.google.ortools.constraintsolver.RoutingDimension; import com.google.ortools.constraintsolver.RoutingSearchParameters; import com.google.ortools.constraintsolver.main; import java.util.logging.Logger; @@ -28,7 +26,9 @@ import java.util.logging.Logger; /** Minimal VRP.*/ public class VrpCapacity { - static { System.loadLibrary("jniortools"); } + static { + System.loadLibrary("jniortools"); + } private static final Logger logger = Logger.getLogger(VrpCapacity.class.getName()); @@ -84,12 +84,11 @@ public class VrpCapacity { int toNode = indexManager_.indexToNode(toIndex); return distanceMatrix_[fromNode][toNode]; } - private long[][] distanceMatrix_; - private RoutingIndexManager indexManager_; + private final long[][] distanceMatrix_; + private final RoutingIndexManager indexManager_; } // [END manhattan_distance] - // [START demands] static class DemandCallback extends LongToLong { public DemandCallback(DataModel data, RoutingIndexManager manager) { @@ -102,8 +101,9 @@ public class VrpCapacity { int fromNode = indexManager_.indexToNode(fromIndex); return demands_[fromNode]; } - private long[] demands_; - private RoutingIndexManager indexManager_; + + private final long[] demands_; + private final RoutingIndexManager indexManager_; } // [END demands] @@ -169,11 +169,10 @@ public class VrpCapacity { // [START capacity_constraint] LongToLong demandEvaluator = new DemandCallback(data, manager); int demandCostIndex = routing.registerUnaryTransitCallback(demandEvaluator); - routing.addDimensionWithVehicleCapacity( - demandCostIndex, 0, // null capacity slack - data.vehicleCapacities, // vehicle maximum capacities - true, // start cumul to zero - "Capacity"); + routing.addDimensionWithVehicleCapacity(demandCostIndex, 0, // null capacity slack + data.vehicleCapacities, // vehicle maximum capacities + true, // start cumul to zero + "Capacity"); // [END capacity_constraint] // Setting first solution heuristic. diff --git a/ortools/constraint_solver/samples/VrpDropNodes.java b/ortools/constraint_solver/samples/VrpDropNodes.java index e08e3a895b..0b49f50ae3 100644 --- a/ortools/constraint_solver/samples/VrpDropNodes.java +++ b/ortools/constraint_solver/samples/VrpDropNodes.java @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// Copyright 2010-2018 Google LLC // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -26,7 +26,9 @@ import java.util.logging.Logger; /** Minimal VRP.*/ public class VrpDropNodes { - static { System.loadLibrary("jniortools"); } + static { + System.loadLibrary("jniortools"); + } private static final Logger logger = Logger.getLogger(VrpDropNodes.class.getName()); @@ -82,8 +84,8 @@ public class VrpDropNodes { int toNode = indexManager_.indexToNode(toIndex); return distanceMatrix_[fromNode][toNode]; } - private long[][] distanceMatrix_; - private RoutingIndexManager indexManager_; + private final long[][] distanceMatrix_; + private final RoutingIndexManager indexManager_; } // [END manhattan_distance] @@ -99,8 +101,8 @@ public class VrpDropNodes { int fromNode = indexManager_.indexToNode(fromIndex); return demands_[fromNode]; } - private long[] demands_; - private RoutingIndexManager indexManager_; + private final long[] demands_; + private final RoutingIndexManager indexManager_; } // [END demands] @@ -184,8 +186,7 @@ public class VrpDropNodes { // Allow to drop nodes. long penalty = 1000; for (int i = 1; i < data.distanceMatrix.length; ++i) { - routing.addDisjunction( - new long[] {manager.nodeToIndex(i)}, penalty); + routing.addDisjunction(new long[] {manager.nodeToIndex(i)}, penalty); } // [END capacity_constraint] diff --git a/ortools/constraint_solver/samples/VrpGlobalSpan.java b/ortools/constraint_solver/samples/VrpGlobalSpan.java index ceb666082c..12d1bd4f2d 100644 --- a/ortools/constraint_solver/samples/VrpGlobalSpan.java +++ b/ortools/constraint_solver/samples/VrpGlobalSpan.java @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// Copyright 2010-2018 Google LLC // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -26,7 +26,9 @@ import java.util.logging.Logger; /** Minimal VRP.*/ public class VrpGlobalSpan { - static { System.loadLibrary("jniortools"); } + static { + System.loadLibrary("jniortools"); + } private static final Logger logger = Logger.getLogger(VrpGlobalSpan.class.getName()); @@ -82,8 +84,8 @@ public class VrpGlobalSpan { int toNode = indexManager_.indexToNode(toIndex); return distanceMatrix_[fromNode][toNode]; } - private long[][] distanceMatrix_; - private RoutingIndexManager indexManager_; + private final long[][] distanceMatrix_; + private final RoutingIndexManager indexManager_; } // [END manhattan_distance] @@ -141,8 +143,8 @@ public class VrpGlobalSpan { // Add Distance constraint. // [START distance_constraint] routing.addDimension(transitCostIndex, 0, 3000, - true, // start cumul to zero - "Distance"); + true, // start cumul to zero + "Distance"); RoutingDimension distanceDimension = routing.getMutableDimension("Distance"); distanceDimension.setGlobalSpanCostCoefficient(100); // [END distance_constraint] diff --git a/ortools/constraint_solver/samples/VrpStartsEnds.java b/ortools/constraint_solver/samples/VrpStartsEnds.java index bb0bfbec68..2278ed1c91 100644 --- a/ortools/constraint_solver/samples/VrpStartsEnds.java +++ b/ortools/constraint_solver/samples/VrpStartsEnds.java @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// Copyright 2010-2018 Google LLC // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -26,7 +26,9 @@ import java.util.logging.Logger; /** Minimal VRP.*/ public class VrpStartsEnds { - static { System.loadLibrary("jniortools"); } + static { + System.loadLibrary("jniortools"); + } private static final Logger logger = Logger.getLogger(VrpStartsEnds.class.getName()); @@ -80,8 +82,8 @@ public class VrpStartsEnds { int toNode = indexManager_.indexToNode(toIndex); return distanceMatrix_[fromNode][toNode]; } - private long[][] distanceMatrix_; - private RoutingIndexManager indexManager_; + private final long[][] distanceMatrix_; + private final RoutingIndexManager indexManager_; } // [END manhattan_distance] @@ -121,9 +123,8 @@ public class VrpStartsEnds { // Create Routing Index Manager // [START index_manager] - RoutingIndexManager manager = - new RoutingIndexManager( - data.distanceMatrix.length, data.vehicleNumber, data.starts, data.ends); + RoutingIndexManager manager = new RoutingIndexManager( + data.distanceMatrix.length, data.vehicleNumber, data.starts, data.ends); // [END index_manager] // Create Routing Model. @@ -141,8 +142,8 @@ public class VrpStartsEnds { // Add Distance constraint. // [START distance_constraint] routing.addDimension(transitCostIndex, 0, 2000, - true, // start cumul to zero - "Distance"); + true, // start cumul to zero + "Distance"); RoutingDimension distanceDimension = routing.getMutableDimension("Distance"); distanceDimension.setGlobalSpanCostCoefficient(100); // [END distance_constraint] diff --git a/ortools/constraint_solver/samples/VrpTimeWindows.java b/ortools/constraint_solver/samples/VrpTimeWindows.java index 39d121893c..d175871104 100644 --- a/ortools/constraint_solver/samples/VrpTimeWindows.java +++ b/ortools/constraint_solver/samples/VrpTimeWindows.java @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// Copyright 2010-2018 Google LLC // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -27,7 +27,9 @@ import java.util.logging.Logger; /** Minimal VRP.*/ public class VrpTimeWindows { - static { System.loadLibrary("jniortools"); } + static { + System.loadLibrary("jniortools"); + } private static final Logger logger = Logger.getLogger(VrpTimeWindows.class.getName()); @@ -95,8 +97,8 @@ public class VrpTimeWindows { int toNode = indexManager_.indexToNode(toIndex); return timeMatrix_[fromNode][toNode]; } - private long[][] timeMatrix_; - private RoutingIndexManager indexManager_; + private final long[][] timeMatrix_; + private final RoutingIndexManager indexManager_; } // [END times]