diff --git a/ortools/constraint_solver/csharp/routing.i b/ortools/constraint_solver/csharp/routing.i index 99ab122ac4..0f2c659b2e 100644 --- a/ortools/constraint_solver/csharp/routing.i +++ b/ortools/constraint_solver/csharp/routing.i @@ -108,12 +108,13 @@ namespace operations_research { %typemap(imtype, out="IntPtr") RoutingTransitCallback1 "UnaryTransitCallback" %typemap(imtype, out="IntPtr") RoutingTransitCallback2 "TransitCallback" -// Type use in module_csharp_wrap.h function declaration. -// since SWIG generate code as: `ctype argX` we can't use a C function pointer type. -%typemap(ctype) RoutingTransitCallback1 "void*" // "int64 (*)(int64)" -%typemap(ctype) RoutingTransitCallback2 "void*" // "int64 (*)(int64, int64)" +// Type use in module_csharp_wrap.h function declaration, since SWIG generate +// code as: `ctype argX`, we can't use the real C function pointer type. +%typemap(ctype) RoutingTransitCallback1 "void*" // "int64 (*argX)(int64)" +%typemap(ctype) RoutingTransitCallback2 "void*" // "int64 (*argX)(int64, int64)" -// Convert in module_csharp_wrap.cc input argument (delegate marshaled in C function pointer) to original std::function<...> +// Convert in module_csharp_wrap.cc input argument +// (delegate marshaled in C function pointer) to original std::function<...> %typemap(in) RoutingTransitCallback1 %{ $1 = [$input](int64 fromIndex) -> int64 { return (*(int64 (*)(int64))$input)(fromIndex); @@ -125,8 +126,10 @@ namespace operations_research { %} } // namespace operations_research -// Add PickupAndDeliveryPolicy enum value to RoutingModel (like RoutingModel::Status) -// For C++11 strongly typed enum SWIG support see https://github.com/swig/swig/issues/316 +// Add PickupAndDeliveryPolicy enum value to RoutingModel +// (like RoutingModel::Status) +// For C++11 strongly typed enum SWIG support +// see https://github.com/swig/swig/issues/316 %extend operations_research::RoutingModel { static const operations_research::RoutingModel::PickupAndDeliveryPolicy ANY = operations_research::RoutingModel::PickupAndDeliveryPolicy::ANY; @@ -151,6 +154,7 @@ PROTO2_RETURN(operations_research::RoutingSearchParameters, PROTO2_RETURN(operations_research::RoutingModelParameters, Google.OrTools.ConstraintSolver.RoutingModelParameters) - +// TODO(user): Replace with %ignoreall/%unignoreall +//swiglint: disable include-h-allglobals %include "ortools/constraint_solver/routing_parameters.h" %include "ortools/constraint_solver/routing.h" diff --git a/ortools/constraint_solver/java/constraint_solver.i b/ortools/constraint_solver/java/constraint_solver.i index 8447a4d26e..9345bc4f4d 100644 --- a/ortools/constraint_solver/java/constraint_solver.i +++ b/ortools/constraint_solver/java/constraint_solver.i @@ -186,7 +186,7 @@ PROTECT_FROM_FAILURE(Solver::Fail(), arg1); %typemap(javain) TYPE "$javainput" // passing the Callback to JNI java class. %enddef -// Method taking no parameters and returning a string +// Method taking no parameters and returning a std::string %define DEFINE_VOID_TO_STRING_CALLBACK( TYPE, JAVA_TYPE, JAVA_METHOD, JAVA_SIGN) @@ -236,7 +236,8 @@ PROTECT_FROM_FAILURE(Solver::Fail(), arg1); auto $input_guard = std::make_shared(jenv, $input_object); $1 = [jenv, $input_object, $input_method_id, $input_guard](operations_research::Solver* solver) -> void { - jclass solver_class = jenv->FindClass("com/google/ortools/constraintsolver/Solver"); + jclass solver_class = jenv->FindClass( + "com/google/ortools/constraintsolver/Solver"); assert(nullptr != solver_class); jmethodID solver_constructor = jenv->GetMethodID(solver_class, "", "(JZ)V"); assert(nullptr != solver_constructor); @@ -266,7 +267,7 @@ DEFINE_SOLVER_TO_VOID_CALLBACK( DEFINE_VOID_TO_STRING_CALLBACK( std::function, - Supplier, "get", "()Ljava/lang/String;") + Supplier, "get", "()Ljava/lang/Object;") DEFINE_VOID_TO_R_CALLBACK( std::function, @@ -313,6 +314,8 @@ DEFINE_ARGS_TO_R_CALLBACK( LongConsumer, "accept", "(J)V", void, CallVoidMethod, VAR_ARGS(long t), VAR_ARGS(t)) +#undef VAR_ARGS + // Renaming namespace operations_research { @@ -660,7 +663,9 @@ import java.lang.Runnable; // note: SWIG does not support multiple %typemap(javacode) Type, so we have to // define all Solver tweak here (ed and not in the macro DEFINE_CALLBACK_*) %typemap(javacode) Solver %{ - /** This exceptions signal that a failure has been raised in the C++ world. */ + /** + * This exceptions signal that a failure has been raised in the C++ world. + */ public static class FailException extends Exception { public FailException() { super(); diff --git a/ortools/constraint_solver/java/routing.i b/ortools/constraint_solver/java/routing.i index a8f5fdd859..73cf3b50f6 100644 --- a/ortools/constraint_solver/java/routing.i +++ b/ortools/constraint_solver/java/routing.i @@ -292,4 +292,5 @@ namespace operations_research { %unignoreall // TODO(user): Use ignoreall/unignoreall for this one. A lot of work. +//swiglint: disable include-h-allglobals %include "ortools/constraint_solver/routing.h" diff --git a/ortools/constraint_solver/python/routing.i b/ortools/constraint_solver/python/routing.i index dc3958051f..47f5575709 100644 --- a/ortools/constraint_solver/python/routing.i +++ b/ortools/constraint_solver/python/routing.i @@ -49,6 +49,7 @@ DEFINE_INDEX_TYPE_TYPEDEF( operations_research::RoutingVehicleClassIndex, operations_research::RoutingModel::VehicleClassIndex); + %ignore operations_research::RoutingModel::AddMatrixDimension( std::vector > values, int64 capacity, @@ -113,4 +114,5 @@ enum OptionalBoolean { } // namespace operations_research // TODO(user): Use ignoreall/unignoreall for this one. A lot of work. +//swiglint: disable include-h-allglobals %include "ortools/constraint_solver/routing.h" diff --git a/ortools/constraint_solver/samples/SimpleRoutingProgram.java b/ortools/constraint_solver/samples/SimpleRoutingProgram.java index e8f31330f5..22314b8775 100644 --- a/ortools/constraint_solver/samples/SimpleRoutingProgram.java +++ b/ortools/constraint_solver/samples/SimpleRoutingProgram.java @@ -16,11 +16,11 @@ import static java.lang.Math.abs; import com.google.ortools.constraintsolver.Assignment; -import com.google.ortools.constraintsolver.FirstSolutionStrategy; import com.google.ortools.constraintsolver.RoutingIndexManager; import com.google.ortools.constraintsolver.RoutingModel; -import com.google.ortools.constraintsolver.RoutingSearchParameters; import com.google.ortools.constraintsolver.main; +import com.google.ortools.constraintsolver.FirstSolutionStrategy; +import com.google.ortools.constraintsolver.RoutingSearchParameters; import java.util.logging.Logger; // [END import] @@ -52,8 +52,8 @@ public class SimpleRoutingProgram { // Create and register a transit callback. // [START transit_callback] - final int transitCallbackIndex = routing.registerTransitCallback( - (long fromIndex, long toIndex) -> { + final int transitCallbackIndex = + routing.registerTransitCallback((long fromIndex, long toIndex) -> { // Convert from routing variable Index to user NodeIndex. int fromNode = manager.indexToNode(fromIndex); int toNode = manager.indexToNode(toIndex); diff --git a/ortools/constraint_solver/samples/Tsp.java b/ortools/constraint_solver/samples/Tsp.java index 278200d3b5..634524c105 100644 --- a/ortools/constraint_solver/samples/Tsp.java +++ b/ortools/constraint_solver/samples/Tsp.java @@ -16,11 +16,11 @@ import static java.lang.Math.abs; import com.google.ortools.constraintsolver.Assignment; -import com.google.ortools.constraintsolver.FirstSolutionStrategy; import com.google.ortools.constraintsolver.RoutingIndexManager; import com.google.ortools.constraintsolver.RoutingModel; -import com.google.ortools.constraintsolver.RoutingSearchParameters; import com.google.ortools.constraintsolver.main; +import com.google.ortools.constraintsolver.FirstSolutionStrategy; +import com.google.ortools.constraintsolver.RoutingSearchParameters; import java.util.function.LongBinaryOperator; import java.util.logging.Logger; // [END import] @@ -35,37 +35,34 @@ public class Tsp { // [START data_model] static class DataModel { + public final int[][] locations = { + {4, 4}, + {2, 0}, + {8, 0}, + {0, 1}, + {1, 1}, + {5, 2}, + {7, 2}, + {3, 3}, + {6, 3}, + {5, 5}, + {8, 5}, + {1, 6}, + {2, 6}, + {3, 7}, + {6, 7}, + {0, 8}, + {7, 8}, + }; + public final int vehicleNumber = 1; + public final int depot = 0; public DataModel() { - locations = new int[][] { - {4, 4}, - {2, 0}, - {8, 0}, - {0, 1}, - {1, 1}, - {5, 2}, - {7, 2}, - {3, 3}, - {6, 3}, - {5, 5}, - {8, 5}, - {1, 6}, - {2, 6}, - {3, 7}, - {6, 7}, - {0, 8}, - {7, 8}, - }; // Convert locations in meters using a city block dimension of 114m x 80m. for (int[] element : locations) { element[0] *= 114; element[1] *= 80; } - vehicleNumber = 1; - depot = 0; } - public final int[][] locations; - public final int vehicleNumber; - public final int depot; } // [END data_model] @@ -91,7 +88,7 @@ public class Tsp { } } } - + @Override public long applyAsLong(long fromIndex, long toIndex) { // Convert from routing variable Index to distance matrix NodeIndex. int fromNode = indexManager.indexToNode(fromIndex); @@ -145,8 +142,8 @@ public class Tsp { // Create and register a transit callback. // [START transit_callback] - final int transitCallbackIndex = routing.registerTransitCallback( - new ManhattanDistance(data, manager)); + final int transitCallbackIndex = + routing.registerTransitCallback(new ManhattanDistance(data, manager)); // [END transit_callback] // Define cost of each arc. diff --git a/ortools/constraint_solver/samples/TspDistanceMatrix.java b/ortools/constraint_solver/samples/TspDistanceMatrix.java index f009c9266a..a82eaa1c33 100644 --- a/ortools/constraint_solver/samples/TspDistanceMatrix.java +++ b/ortools/constraint_solver/samples/TspDistanceMatrix.java @@ -13,12 +13,12 @@ // [START program] // [START import] -import com.google.ortools.constraintsolver.FirstSolutionStrategy; -import com.google.ortools.constraintsolver.RoutingSearchParameters; import com.google.ortools.constraintsolver.Assignment; import com.google.ortools.constraintsolver.RoutingIndexManager; import com.google.ortools.constraintsolver.RoutingModel; import com.google.ortools.constraintsolver.main; +import com.google.ortools.constraintsolver.FirstSolutionStrategy; +import com.google.ortools.constraintsolver.RoutingSearchParameters; import java.util.logging.Logger; // [END import] @@ -32,32 +32,27 @@ public class TspDistanceMatrix { // [START data_model] static class DataModel { - public DataModel() { - distanceMatrix = new long[][] { - {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}, - }; - vehicleNumber = 1; - depot = 0; - } - public final long[][] distanceMatrix; - public final int vehicleNumber; - public final int depot; + public final long[][] distanceMatrix = { + {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}, + }; + public final int vehicleNumber = 1; + public final int depot = 0; } // [END data_model] @@ -103,8 +98,8 @@ public class TspDistanceMatrix { // Create and register a transit callback. // [START transit_callback] - final int transitCallbackIndex = routing.registerTransitCallback( - (long fromIndex, long toIndex) -> { + final int transitCallbackIndex = + routing.registerTransitCallback((long fromIndex, long toIndex) -> { // Convert from routing variable Index to user NodeIndex. int fromNode = manager.indexToNode(fromIndex); int toNode = manager.indexToNode(toIndex); diff --git a/ortools/constraint_solver/samples/Vrp.java b/ortools/constraint_solver/samples/Vrp.java index 10d81c73e6..9e03cd1df6 100644 --- a/ortools/constraint_solver/samples/Vrp.java +++ b/ortools/constraint_solver/samples/Vrp.java @@ -14,11 +14,11 @@ // [START program] // [START import] import com.google.ortools.constraintsolver.Assignment; -import com.google.ortools.constraintsolver.FirstSolutionStrategy; import com.google.ortools.constraintsolver.RoutingIndexManager; import com.google.ortools.constraintsolver.RoutingModel; -import com.google.ortools.constraintsolver.RoutingSearchParameters; import com.google.ortools.constraintsolver.main; +import com.google.ortools.constraintsolver.FirstSolutionStrategy; +import com.google.ortools.constraintsolver.RoutingSearchParameters; import java.util.logging.Logger; // [END import] @@ -32,32 +32,27 @@ public class Vrp { // [START data_model] static class DataModel { - public DataModel() { - distanceMatrix = new long[][] { - {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}, - }; - vehicleNumber = 4; - depot = 0; - } - public final long[][] distanceMatrix; - public final int vehicleNumber; - public final int depot; + public final long[][] distanceMatrix = { + {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}, + }; + public final int vehicleNumber = 4; + public final int depot = 0; } // [END data_model] @@ -108,8 +103,8 @@ public class Vrp { // Create and register a transit callback. // [START transit_callback] - final int transitCallbackIndex = routing.registerTransitCallback( - (long fromIndex, long toIndex) -> { + final int transitCallbackIndex = + routing.registerTransitCallback((long fromIndex, long toIndex) -> { // Convert from routing variable Index to user NodeIndex. int fromNode = manager.indexToNode(fromIndex); int toNode = manager.indexToNode(toIndex); diff --git a/ortools/constraint_solver/samples/VrpCapacity.cs b/ortools/constraint_solver/samples/VrpCapacity.cs index 64fdd8e682..9e00d4e0a0 100644 --- a/ortools/constraint_solver/samples/VrpCapacity.cs +++ b/ortools/constraint_solver/samples/VrpCapacity.cs @@ -43,8 +43,10 @@ public class VrpCapacity { {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} }; + // [START demands_capacities] public long[] Demands = {0, 1, 1, 2, 4, 2, 4, 8, 8, 1, 2, 1, 2, 4, 4, 8, 8}; public long[] VehicleCapacities = {15, 15, 15, 15}; + // [END demands_capacities] public int VehicleNumber = 4; public int Depot = 0; }; @@ -81,8 +83,8 @@ public class VrpCapacity { totalDistance += routeDistance; totalLoad += routeLoad; } - Console.WriteLine("Total Distance of all routes: {0}m", totalDistance); - Console.WriteLine("Total Load of all routes: {0}m", totalLoad); + Console.WriteLine("Total distance of all routes: {0}m", totalDistance); + Console.WriteLine("Total load of all routes: {0}m", totalLoad); } // [END solution_printer] diff --git a/ortools/constraint_solver/samples/VrpCapacity.java b/ortools/constraint_solver/samples/VrpCapacity.java index 7b76df7001..068b3c1f0a 100644 --- a/ortools/constraint_solver/samples/VrpCapacity.java +++ b/ortools/constraint_solver/samples/VrpCapacity.java @@ -14,11 +14,11 @@ // [START program] // [START import] import com.google.ortools.constraintsolver.Assignment; -import com.google.ortools.constraintsolver.FirstSolutionStrategy; import com.google.ortools.constraintsolver.RoutingIndexManager; import com.google.ortools.constraintsolver.RoutingModel; -import com.google.ortools.constraintsolver.RoutingSearchParameters; import com.google.ortools.constraintsolver.main; +import com.google.ortools.constraintsolver.FirstSolutionStrategy; +import com.google.ortools.constraintsolver.RoutingSearchParameters; import java.util.logging.Logger; // [END import] @@ -32,36 +32,31 @@ public class VrpCapacity { // [START data_model] static class DataModel { - public DataModel() { - distanceMatrix = new long[][] { - {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}, - }; - demands = new long[] {0, 1, 1, 2, 4, 2, 4, 8, 8, 1, 2, 1, 2, 4, 4, 8, 8}; - vehicleNumber = 4; - vehicleCapacities = new long[] {15, 15, 15, 15}; - depot = 0; - } - public final long[][] distanceMatrix; - public final long[] demands; - public final int vehicleNumber; - public final long[] vehicleCapacities; - public final int depot; + public final long[][] distanceMatrix = { + {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}, + }; + // [START demands_capacities] + public final long[] demands = {0, 1, 1, 2, 4, 2, 4, 8, 8, 1, 2, 1, 2, 4, 4, 8, 8}; + public final long[] vehicleCapacities = {15, 15, 15, 15}; + // [END demands_capacities] + public final int vehicleNumber = 4; + public final int depot = 0; } // [END data_model] @@ -94,8 +89,8 @@ public class VrpCapacity { totalDistance += routeDistance; totalLoad += routeLoad; } - logger.info("Total Distance of all routes: " + totalDistance + "m"); - logger.info("Total Load of all routes: " + totalLoad); + logger.info("Total distance of all routes: " + totalDistance + "m"); + logger.info("Total load of all routes: " + totalLoad); } // [END solution_printer] @@ -118,8 +113,8 @@ public class VrpCapacity { // Create and register a transit callback. // [START transit_callback] - final int transitCallbackIndex = routing.registerTransitCallback( - (long fromIndex, long toIndex) -> { + final int transitCallbackIndex = + routing.registerTransitCallback((long fromIndex, long toIndex) -> { // Convert from routing variable Index to user NodeIndex. int fromNode = manager.indexToNode(fromIndex); int toNode = manager.indexToNode(toIndex); @@ -134,12 +129,11 @@ public class VrpCapacity { // Add Capacity constraint. // [START capacity_constraint] - final int demandCallbackIndex = routing.registerUnaryTransitCallback( - (long fromIndex) -> { - // Convert from routing variable Index to user NodeIndex. - int fromNode = manager.indexToNode(fromIndex); - return data.demands[fromNode]; - }); + final int demandCallbackIndex = routing.registerUnaryTransitCallback((long fromIndex) -> { + // Convert from routing variable Index to user NodeIndex. + int fromNode = manager.indexToNode(fromIndex); + return data.demands[fromNode]; + }); routing.addDimensionWithVehicleCapacity(demandCallbackIndex, 0, // null capacity slack data.vehicleCapacities, // vehicle maximum capacities true, // start cumul to zero diff --git a/ortools/constraint_solver/samples/VrpDropNodes.cs b/ortools/constraint_solver/samples/VrpDropNodes.cs index 661ca8a9a7..606861fab8 100644 --- a/ortools/constraint_solver/samples/VrpDropNodes.cs +++ b/ortools/constraint_solver/samples/VrpDropNodes.cs @@ -43,8 +43,10 @@ public class VrpDropNodes { {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} }; + // [START demands_capacities] public long[] Demands = {0, 1, 1, 3, 6, 3, 6, 8, 8, 1, 2, 1, 2, 6, 6, 8, 8}; public long[] VehicleCapacities = {15, 15, 15, 15}; + // [END demands_capacities] public int VehicleNumber = 4; public int Depot = 0; }; diff --git a/ortools/constraint_solver/samples/VrpDropNodes.java b/ortools/constraint_solver/samples/VrpDropNodes.java index 3917abec9d..ce955a0e12 100644 --- a/ortools/constraint_solver/samples/VrpDropNodes.java +++ b/ortools/constraint_solver/samples/VrpDropNodes.java @@ -14,11 +14,11 @@ // [START program] // [START import] import com.google.ortools.constraintsolver.Assignment; -import com.google.ortools.constraintsolver.FirstSolutionStrategy; import com.google.ortools.constraintsolver.RoutingIndexManager; import com.google.ortools.constraintsolver.RoutingModel; -import com.google.ortools.constraintsolver.RoutingSearchParameters; import com.google.ortools.constraintsolver.main; +import com.google.ortools.constraintsolver.FirstSolutionStrategy; +import com.google.ortools.constraintsolver.RoutingSearchParameters; import java.util.logging.Logger; // [END import] @@ -32,36 +32,31 @@ public class VrpDropNodes { // [START data_model] static class DataModel { - public DataModel() { - distanceMatrix = new long[][] { - {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}, - }; - demands = new long[] {0, 1, 1, 3, 6, 3, 6, 8, 8, 1, 2, 1, 2, 6, 6, 8, 8}; - vehicleNumber = 4; - vehicleCapacities = new long[] {15, 15, 15, 15}; - depot = 0; - } - public final long[][] distanceMatrix; - public final long[] demands; - public final int vehicleNumber; - public final long[] vehicleCapacities; - public final int depot; + public final long[][] distanceMatrix = { + {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}, + }; + // [START demands_capacities] + public final long[] demands = {0, 1, 1, 3, 6, 3, 6, 8, 8, 1, 2, 1, 2, 6, 6, 8, 8}; + public final long[] vehicleCapacities = {15, 15, 15, 15}; + // [END demands_capacities] + public final int vehicleNumber = 4; + public final int depot = 0; } // [END data_model] @@ -129,8 +124,8 @@ public class VrpDropNodes { // Create and register a transit callback. // [START transit_callback] - final int transitCallbackIndex = routing.registerTransitCallback( - (long fromIndex, long toIndex) -> { + final int transitCallbackIndex = + routing.registerTransitCallback((long fromIndex, long toIndex) -> { // Convert from routing variable Index to user NodeIndex. int fromNode = manager.indexToNode(fromIndex); int toNode = manager.indexToNode(toIndex); @@ -145,12 +140,11 @@ public class VrpDropNodes { // Add Capacity constraint. // [START capacity_constraint] - final int demandCallbackIndex = routing.registerUnaryTransitCallback( - (long fromIndex) -> { - // Convert from routing variable Index to user NodeIndex. - int fromNode = manager.indexToNode(fromIndex); - return data.demands[fromNode]; - }); + final int demandCallbackIndex = routing.registerUnaryTransitCallback((long fromIndex) -> { + // Convert from routing variable Index to user NodeIndex. + int fromNode = manager.indexToNode(fromIndex); + return data.demands[fromNode]; + }); routing.addDimensionWithVehicleCapacity(demandCallbackIndex, 0, // null capacity slack data.vehicleCapacities, // vehicle maximum capacities true, // start cumul to zero diff --git a/ortools/constraint_solver/samples/VrpGlobalSpan.cs b/ortools/constraint_solver/samples/VrpGlobalSpan.cs index ccbdc93901..6688d1ed62 100644 --- a/ortools/constraint_solver/samples/VrpGlobalSpan.cs +++ b/ortools/constraint_solver/samples/VrpGlobalSpan.cs @@ -74,7 +74,7 @@ public class VrpGlobalSpan { Console.WriteLine("Distance of the route: {0}m", routeDistance); totalDistance += routeDistance; } - Console.WriteLine("Total Distance of all routes: {0}m", totalDistance); + Console.WriteLine("Total distance of all routes: {0}m", totalDistance); } // [END solution_printer] diff --git a/ortools/constraint_solver/samples/VrpGlobalSpan.java b/ortools/constraint_solver/samples/VrpGlobalSpan.java index b1a13a1560..4d91c69fbd 100644 --- a/ortools/constraint_solver/samples/VrpGlobalSpan.java +++ b/ortools/constraint_solver/samples/VrpGlobalSpan.java @@ -14,12 +14,12 @@ // [START program] // [START import] import com.google.ortools.constraintsolver.Assignment; -import com.google.ortools.constraintsolver.FirstSolutionStrategy; import com.google.ortools.constraintsolver.RoutingDimension; import com.google.ortools.constraintsolver.RoutingIndexManager; import com.google.ortools.constraintsolver.RoutingModel; -import com.google.ortools.constraintsolver.RoutingSearchParameters; import com.google.ortools.constraintsolver.main; +import com.google.ortools.constraintsolver.FirstSolutionStrategy; +import com.google.ortools.constraintsolver.RoutingSearchParameters; import java.util.logging.Logger; // [END import] @@ -33,32 +33,27 @@ public class VrpGlobalSpan { // [START data_model] static class DataModel { - public DataModel() { - distanceMatrix = new long[][] { - {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}, - }; - vehicleNumber = 4; - depot = 0; - } - public final long[][] distanceMatrix; - public final int vehicleNumber; - public final int depot; + public final long[][] distanceMatrix = { + {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}, + }; + public final int vehicleNumber = 4; + public final int depot = 0; } // [END data_model] @@ -85,7 +80,7 @@ public class VrpGlobalSpan { logger.info("Distance of the route: " + routeDistance + "m"); totalDistance += routeDistance; } - logger.info("Total Distance of all routes: " + totalDistance + "m"); + logger.info("Total distance of all routes: " + totalDistance + "m"); } // [END solution_printer] @@ -108,8 +103,8 @@ public class VrpGlobalSpan { // Create and register a transit callback. // [START transit_callback] - final int transitCallbackIndex = routing.registerTransitCallback( - (long fromIndex, long toIndex) -> { + final int transitCallbackIndex = + routing.registerTransitCallback((long fromIndex, long toIndex) -> { // Convert from routing variable Index to user NodeIndex. int fromNode = manager.indexToNode(fromIndex); int toNode = manager.indexToNode(toIndex); diff --git a/ortools/constraint_solver/samples/VrpStartsEnds.cs b/ortools/constraint_solver/samples/VrpStartsEnds.cs index 8874585f1e..7165f687f5 100644 --- a/ortools/constraint_solver/samples/VrpStartsEnds.cs +++ b/ortools/constraint_solver/samples/VrpStartsEnds.cs @@ -43,9 +43,11 @@ public class VrpStartsEnds { {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} }; + public int VehicleNumber = 4; + // [START starts_ends] public int[] Starts = {1, 2, 15, 16}; public int[] Ends = {0, 0, 0, 0}; - public int VehicleNumber = 4; + // [END starts_ends] }; // [END data_model] diff --git a/ortools/constraint_solver/samples/VrpStartsEnds.java b/ortools/constraint_solver/samples/VrpStartsEnds.java index 802e4576a8..08b5c95041 100644 --- a/ortools/constraint_solver/samples/VrpStartsEnds.java +++ b/ortools/constraint_solver/samples/VrpStartsEnds.java @@ -14,12 +14,12 @@ // [START program] // [START import] import com.google.ortools.constraintsolver.Assignment; -import com.google.ortools.constraintsolver.FirstSolutionStrategy; import com.google.ortools.constraintsolver.RoutingDimension; import com.google.ortools.constraintsolver.RoutingIndexManager; import com.google.ortools.constraintsolver.RoutingModel; -import com.google.ortools.constraintsolver.RoutingSearchParameters; import com.google.ortools.constraintsolver.main; +import com.google.ortools.constraintsolver.FirstSolutionStrategy; +import com.google.ortools.constraintsolver.RoutingSearchParameters; import java.util.logging.Logger; // [END import] @@ -33,34 +33,30 @@ public class VrpStartsEnds { // [START data_model] static class DataModel { - public DataModel() { - distanceMatrix = new long[][] { - {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}, - }; - vehicleNumber = 4; - starts = new int[] {1, 2, 15, 16}; - ends = new int[] {0, 0, 0, 0}; - } - public final long[][] distanceMatrix; - public final int vehicleNumber; - public final int[] starts; - public final int[] ends; + public final long[][] distanceMatrix = { + {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}, + }; + public final int vehicleNumber = 4; + // [START starts_ends] + public final int[] starts = {1, 2, 15, 16}; + public final int[] ends = {0, 0, 0, 0}; + // [END starts_ends] } // [END data_model] @@ -111,8 +107,8 @@ public class VrpStartsEnds { // Create and register a transit callback. // [START transit_callback] - final int transitCallbackIndex = routing.registerTransitCallback( - (long fromIndex, long toIndex) -> { + final int transitCallbackIndex = + routing.registerTransitCallback((long fromIndex, long toIndex) -> { // Convert from routing variable Index to user NodeIndex. int fromNode = manager.indexToNode(fromIndex); int toNode = manager.indexToNode(toIndex); diff --git a/ortools/constraint_solver/samples/VrpTimeWindows.cs b/ortools/constraint_solver/samples/VrpTimeWindows.cs index 6ae7587094..a3cfdc5a6b 100644 --- a/ortools/constraint_solver/samples/VrpTimeWindows.cs +++ b/ortools/constraint_solver/samples/VrpTimeWindows.cs @@ -105,7 +105,7 @@ public class VrpTimeWindows { Console.WriteLine("Time of the route: {0}m", routeTime); totalTime += routeTime; } - Console.WriteLine("Total Distance of all routes: {0}m", totalTime); + Console.WriteLine("Total distance of all routes: {0}m", totalTime); } // [END solution_printer] diff --git a/ortools/constraint_solver/samples/VrpTimeWindows.java b/ortools/constraint_solver/samples/VrpTimeWindows.java index 93d6eac2ff..19008c22e6 100644 --- a/ortools/constraint_solver/samples/VrpTimeWindows.java +++ b/ortools/constraint_solver/samples/VrpTimeWindows.java @@ -14,13 +14,13 @@ // [START program] // [START import] import com.google.ortools.constraintsolver.Assignment; -import com.google.ortools.constraintsolver.FirstSolutionStrategy; import com.google.ortools.constraintsolver.IntVar; import com.google.ortools.constraintsolver.RoutingDimension; import com.google.ortools.constraintsolver.RoutingIndexManager; import com.google.ortools.constraintsolver.RoutingModel; -import com.google.ortools.constraintsolver.RoutingSearchParameters; import com.google.ortools.constraintsolver.main; +import com.google.ortools.constraintsolver.FirstSolutionStrategy; +import com.google.ortools.constraintsolver.RoutingSearchParameters; import java.util.logging.Logger; // [END import] @@ -34,52 +34,46 @@ public class VrpTimeWindows { // [START data_model] static class DataModel { - public DataModel() { - timeMatrix = new long[][] { - {0, 6, 9, 8, 7, 3, 6, 2, 3, 2, 6, 6, 4, 4, 5, 9, 7}, - {6, 0, 8, 3, 2, 6, 8, 4, 8, 8, 13, 7, 5, 8, 12, 10, 14}, - {9, 8, 0, 11, 10, 6, 3, 9, 5, 8, 4, 15, 14, 13, 9, 18, 9}, - {8, 3, 11, 0, 1, 7, 10, 6, 10, 10, 14, 6, 7, 9, 14, 6, 16}, - {7, 2, 10, 1, 0, 6, 9, 4, 8, 9, 13, 4, 6, 8, 12, 8, 14}, - {3, 6, 6, 7, 6, 0, 2, 3, 2, 2, 7, 9, 7, 7, 6, 12, 8}, - {6, 8, 3, 10, 9, 2, 0, 6, 2, 5, 4, 12, 10, 10, 6, 15, 5}, - {2, 4, 9, 6, 4, 3, 6, 0, 4, 4, 8, 5, 4, 3, 7, 8, 10}, - {3, 8, 5, 10, 8, 2, 2, 4, 0, 3, 4, 9, 8, 7, 3, 13, 6}, - {2, 8, 8, 10, 9, 2, 5, 4, 3, 0, 4, 6, 5, 4, 3, 9, 5}, - {6, 13, 4, 14, 13, 7, 4, 8, 4, 4, 0, 10, 9, 8, 4, 13, 4}, - {6, 7, 15, 6, 4, 9, 12, 5, 9, 6, 10, 0, 1, 3, 7, 3, 10}, - {4, 5, 14, 7, 6, 7, 10, 4, 8, 5, 9, 1, 0, 2, 6, 4, 8}, - {4, 8, 13, 9, 8, 7, 10, 3, 7, 4, 8, 3, 2, 0, 4, 5, 6}, - {5, 12, 9, 14, 12, 6, 6, 7, 3, 3, 4, 7, 6, 4, 0, 9, 2}, - {9, 10, 18, 6, 8, 12, 15, 8, 13, 9, 13, 3, 4, 5, 9, 0, 9}, - {7, 14, 9, 16, 14, 8, 5, 10, 6, 5, 4, 10, 8, 6, 2, 9, 0}, - }; - timeWindows = new long[][] { - {0, 0}, - {10, 15}, - {10, 15}, - {5, 10}, - {5, 10}, - {0, 5}, - {5, 10}, - {0, 5}, - {5, 10}, - {0, 5}, - {10, 15}, - {10, 15}, - {0, 5}, - {5, 10}, - {5, 10}, - {10, 15}, - {5, 10}, - }; - vehicleNumber = 4; - depot = 0; - } - public final long[][] timeMatrix; - public final long[][] timeWindows; - public final int vehicleNumber; - public final int depot; + public final long[][] timeMatrix = { + {0, 6, 9, 8, 7, 3, 6, 2, 3, 2, 6, 6, 4, 4, 5, 9, 7}, + {6, 0, 8, 3, 2, 6, 8, 4, 8, 8, 13, 7, 5, 8, 12, 10, 14}, + {9, 8, 0, 11, 10, 6, 3, 9, 5, 8, 4, 15, 14, 13, 9, 18, 9}, + {8, 3, 11, 0, 1, 7, 10, 6, 10, 10, 14, 6, 7, 9, 14, 6, 16}, + {7, 2, 10, 1, 0, 6, 9, 4, 8, 9, 13, 4, 6, 8, 12, 8, 14}, + {3, 6, 6, 7, 6, 0, 2, 3, 2, 2, 7, 9, 7, 7, 6, 12, 8}, + {6, 8, 3, 10, 9, 2, 0, 6, 2, 5, 4, 12, 10, 10, 6, 15, 5}, + {2, 4, 9, 6, 4, 3, 6, 0, 4, 4, 8, 5, 4, 3, 7, 8, 10}, + {3, 8, 5, 10, 8, 2, 2, 4, 0, 3, 4, 9, 8, 7, 3, 13, 6}, + {2, 8, 8, 10, 9, 2, 5, 4, 3, 0, 4, 6, 5, 4, 3, 9, 5}, + {6, 13, 4, 14, 13, 7, 4, 8, 4, 4, 0, 10, 9, 8, 4, 13, 4}, + {6, 7, 15, 6, 4, 9, 12, 5, 9, 6, 10, 0, 1, 3, 7, 3, 10}, + {4, 5, 14, 7, 6, 7, 10, 4, 8, 5, 9, 1, 0, 2, 6, 4, 8}, + {4, 8, 13, 9, 8, 7, 10, 3, 7, 4, 8, 3, 2, 0, 4, 5, 6}, + {5, 12, 9, 14, 12, 6, 6, 7, 3, 3, 4, 7, 6, 4, 0, 9, 2}, + {9, 10, 18, 6, 8, 12, 15, 8, 13, 9, 13, 3, 4, 5, 9, 0, 9}, + {7, 14, 9, 16, 14, 8, 5, 10, 6, 5, 4, 10, 8, 6, 2, 9, 0}, + }; + public final long[][] timeWindows = { + {0, 0}, + {10, 15}, + {10, 15}, + {5, 10}, + {5, 10}, + {0, 5}, + {5, 10}, + {0, 5}, + {5, 10}, + {0, 5}, + {10, 15}, + {10, 15}, + {0, 5}, + {5, 10}, + {5, 10}, + {10, 15}, + {5, 10}, + }; + public final int vehicleNumber = 4; + public final int depot = 0; } // [END data_model] @@ -112,7 +106,7 @@ public class VrpTimeWindows { logger.info("Time of the route: " + routeTime + "m"); totalTime += routeTime; } - logger.info("Total Time of all routes: " + totalTime + "m"); + logger.info("Total time of all routes: " + totalTime + "m"); } // [END solution_printer] @@ -135,8 +129,8 @@ public class VrpTimeWindows { // Create and register a transit callback. // [START transit_callback] - final int transitCallbackIndex = routing.registerTransitCallback( - (long fromIndex, long toIndex) -> { + final int transitCallbackIndex = + routing.registerTransitCallback((long fromIndex, long toIndex) -> { // Convert from routing variable Index to user NodeIndex. int fromNode = manager.indexToNode(fromIndex); int toNode = manager.indexToNode(toIndex); diff --git a/ortools/constraint_solver/samples/tsp.cc b/ortools/constraint_solver/samples/tsp.cc index 1d8450c6b7..233373d6f2 100644 --- a/ortools/constraint_solver/samples/tsp.cc +++ b/ortools/constraint_solver/samples/tsp.cc @@ -24,39 +24,19 @@ namespace operations_research { // [START data_model] struct DataModel { - DataModel() - : locations({ - {4, 4}, - {2, 0}, - {8, 0}, - {0, 1}, - {1, 1}, - {5, 2}, - {7, 2}, - {3, 3}, - {6, 3}, - {5, 5}, - {8, 5}, - {1, 6}, - {2, 6}, - {3, 7}, - {6, 7}, - {0, 8}, - {7, 8}, - }), - num_locations(locations.size()), - num_vehicles(1), - depot(0) { + const std::vector> locations{ + {4, 4}, {2, 0}, {8, 0}, {0, 1}, {1, 1}, {5, 2}, {7, 2}, {3, 3}, {6, 3}, + {5, 5}, {8, 5}, {1, 6}, {2, 6}, {3, 7}, {6, 7}, {0, 8}, {7, 8}, + }; + const int num_vehicles = 1; + const RoutingIndexManager::NodeIndex depot{0}; + DataModel() { // Convert locations in meters using a city block dimension of 114m x 80m. for (auto& it : locations) { const_cast&>(it)[0] *= 114; const_cast&>(it)[1] *= 80; } } - const std::vector> locations; - const int num_locations; - const int num_vehicles; - const RoutingIndexManager::NodeIndex depot; }; // [END data_model] @@ -65,15 +45,15 @@ struct DataModel { * @details It uses the data.locations to computes the Manhattan distance * between the two positions of two different indices.*/ std::vector> GenerateManhattanDistanceMatrix( - const DataModel& data) { + const std::vector>& locations) { std::vector> distances = std::vector>( - data.num_locations, std::vector(data.num_locations, int64{0})); - for (int fromNode = 0; fromNode < data.num_locations; fromNode++) { - for (int toNode = 0; toNode < data.num_locations; toNode++) { + locations.size(), std::vector(locations.size(), int64{0})); + for (int fromNode = 0; fromNode < locations.size(); fromNode++) { + for (int toNode = 0; toNode < locations.size(); toNode++) { if (fromNode != toNode) distances[fromNode][toNode] = - std::abs(data.locations[toNode][0] - data.locations[fromNode][0]) + - std::abs(data.locations[toNode][1] - data.locations[fromNode][1]); + int64{std::abs(locations[toNode][0] - locations[fromNode][0]) + + std::abs(locations[toNode][1] - locations[fromNode][1])}; } } return distances; @@ -116,7 +96,7 @@ void Tsp() { // Create Routing Index Manager // [START index_manager] - RoutingIndexManager manager(data.num_locations, data.num_vehicles, + RoutingIndexManager manager(data.locations.size(), data.num_vehicles, data.depot); // [END index_manager] @@ -127,7 +107,7 @@ void Tsp() { // Create and register a transit callback. // [START transit_callback] - const auto distance_matrix = GenerateManhattanDistanceMatrix(data); + const auto distance_matrix = GenerateManhattanDistanceMatrix(data.locations); const int transit_callback_index = routing.RegisterTransitCallback( [&distance_matrix, &manager](int64 from_index, int64 to_index) -> int64 { // Convert from routing variable Index to distance matrix NodeIndex. diff --git a/ortools/constraint_solver/samples/tsp_distance_matrix.cc b/ortools/constraint_solver/samples/tsp_distance_matrix.cc index dee800db2b..27f4a40642 100644 --- a/ortools/constraint_solver/samples/tsp_distance_matrix.cc +++ b/ortools/constraint_solver/samples/tsp_distance_matrix.cc @@ -23,49 +23,45 @@ namespace operations_research { // [START data_model] struct DataModel { - DataModel() - : 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}, - }), - num_vehicles(1), - depot(0) {} - const std::vector> distance_matrix; - const int num_vehicles; - const RoutingIndexManager::NodeIndex depot; -}; // namespace operations_research + const std::vector> 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}, + }; + const int num_vehicles = 1; + const RoutingIndexManager::NodeIndex depot{0}; +}; // [END data_model] // [START solution_printer] diff --git a/ortools/constraint_solver/samples/tsp_distance_matrix.py b/ortools/constraint_solver/samples/tsp_distance_matrix.py index 1bb08cebcd..9601707487 100644 --- a/ortools/constraint_solver/samples/tsp_distance_matrix.py +++ b/ortools/constraint_solver/samples/tsp_distance_matrix.py @@ -17,7 +17,6 @@ from __future__ import print_function from ortools.constraint_solver import routing_enums_pb2 from ortools.constraint_solver import pywrapcp - # [END import] @@ -141,7 +140,7 @@ def main(): # Create and register a transit callback. # [START transit_callback] def distance_callback(from_index, to_index): - """Returns the manhattan distance between the two nodes.""" + """Returns the distance between the two nodes.""" # Convert from routing variable Index to distance matrix NodeIndex. from_node = manager.IndexToNode(from_index) to_node = manager.IndexToNode(to_index) diff --git a/ortools/constraint_solver/samples/vrp.cc b/ortools/constraint_solver/samples/vrp.cc index 80bafd3b18..2413153ae2 100644 --- a/ortools/constraint_solver/samples/vrp.cc +++ b/ortools/constraint_solver/samples/vrp.cc @@ -23,49 +23,45 @@ namespace operations_research { // [START data_model] struct DataModel { - DataModel() - : 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}, - }), - num_vehicles(4), - depot(0) {} - const std::vector> distance_matrix; - const int num_vehicles; - const RoutingIndexManager::NodeIndex depot; -}; // namespace operations_research + const std::vector> 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}, + }; + const int num_vehicles = 4; + const RoutingIndexManager::NodeIndex depot{0}; +}; // [END data_model] // [START solution_printer] diff --git a/ortools/constraint_solver/samples/vrp.py b/ortools/constraint_solver/samples/vrp.py index 2516d23a0f..ba92fba5f3 100755 --- a/ortools/constraint_solver/samples/vrp.py +++ b/ortools/constraint_solver/samples/vrp.py @@ -144,7 +144,7 @@ def main(): # Create and register a transit callback. # [START transit_callback] def distance_callback(from_index, to_index): - """Returns the manhattan distance between the two nodes.""" + """Returns the distance between the two nodes.""" # Convert from routing variable Index to distance matrix NodeIndex. from_node = manager.IndexToNode(from_index) to_node = manager.IndexToNode(to_index) diff --git a/ortools/constraint_solver/samples/vrp_capacity.cc b/ortools/constraint_solver/samples/vrp_capacity.cc index 092e271464..8e8bae122e 100644 --- a/ortools/constraint_solver/samples/vrp_capacity.cc +++ b/ortools/constraint_solver/samples/vrp_capacity.cc @@ -23,53 +23,51 @@ namespace operations_research { // [START data_model] struct DataModel { - DataModel() - : 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}, - }), - demands({0, 1, 1, 2, 4, 2, 4, 8, 8, 1, 2, 1, 2, 4, 4, 8, 8}), - num_vehicles(4), - vehicle_capacities({15, 15, 15, 15}), - depot(0) {} - const std::vector> distance_matrix; - const std::vector demands; - const int num_vehicles; - const std::vector vehicle_capacities; - const RoutingIndexManager::NodeIndex depot; -}; // namespace operations_research + const std::vector> 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}, + }; + // [START demands_capacities] + const std::vector demands{ + 0, 1, 1, 2, 4, 2, 4, 8, 8, 1, 2, 1, 2, 4, 4, 8, 8, + }; + const std::vector vehicle_capacities{15, 15, 15, 15}; + // [END demands_capacities] + const int num_vehicles = 4; + const RoutingIndexManager::NodeIndex depot{0}; +}; // [END data_model] // [START solution_printer] diff --git a/ortools/constraint_solver/samples/vrp_capacity.py b/ortools/constraint_solver/samples/vrp_capacity.py index 5abe05ff89..a43467e8d3 100644 --- a/ortools/constraint_solver/samples/vrp_capacity.py +++ b/ortools/constraint_solver/samples/vrp_capacity.py @@ -94,9 +94,11 @@ def create_data_model(): 536, 194, 798, 0 ], ] + # [START demands_capacities] data['demands'] = [0, 1, 1, 2, 4, 2, 4, 8, 8, 1, 2, 1, 2, 4, 4, 8, 8] - data['num_vehicles'] = 4 data['vehicle_capacities'] = [15, 15, 15, 15] + # [END demands_capacities] + data['num_vehicles'] = 4 data['depot'] = 0 return data # [END data_model] @@ -128,8 +130,8 @@ def print_solution(data, manager, routing, assignment): print(plan_output) total_distance += route_distance total_load += route_load - print('Total Distance of all routes: {}m'.format(total_distance)) - print('Total Load of all routes: {}'.format(total_load)) + print('Total distance of all routes: {}m'.format(total_distance)) + print('Total load of all routes: {}'.format(total_load)) # [END solution_printer] @@ -155,7 +157,7 @@ def main(): # Create and register a transit callback. # [START transit_callback] def distance_callback(from_index, to_index): - """Returns the manhattan distance between the two nodes.""" + """Returns the distance between the two nodes.""" # Convert from routing variable Index to distance matrix NodeIndex. from_node = manager.IndexToNode(from_index) to_node = manager.IndexToNode(to_index) diff --git a/ortools/constraint_solver/samples/vrp_drop_nodes.cc b/ortools/constraint_solver/samples/vrp_drop_nodes.cc index 10d2117f44..aa68480af2 100644 --- a/ortools/constraint_solver/samples/vrp_drop_nodes.cc +++ b/ortools/constraint_solver/samples/vrp_drop_nodes.cc @@ -23,53 +23,51 @@ namespace operations_research { // [START data_model] struct DataModel { - DataModel() - : 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}, - }), - demands({0, 1, 1, 3, 6, 3, 6, 8, 8, 1, 2, 1, 2, 6, 6, 8, 8}), - num_vehicles(4), - vehicle_capacities({15, 15, 15, 15}), - depot(0) {} - const std::vector> distance_matrix; - const std::vector demands; - const int num_vehicles; - const std::vector vehicle_capacities; - const RoutingIndexManager::NodeIndex depot; -}; // namespace operations_research + const std::vector> 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}, + }; + // [START demands_capacities] + const std::vector demands{ + 0, 1, 1, 3, 6, 3, 6, 8, 8, 1, 2, 1, 2, 6, 6, 8, 8, + }; + const std::vector vehicle_capacities{15, 15, 15, 15}; + // [END demands_capacities] + const int num_vehicles = 4; + const RoutingIndexManager::NodeIndex depot{0}; +}; // [END data_model] // [START solution_printer] diff --git a/ortools/constraint_solver/samples/vrp_drop_nodes.py b/ortools/constraint_solver/samples/vrp_drop_nodes.py index 0cb6d45324..ec8a83db2a 100644 --- a/ortools/constraint_solver/samples/vrp_drop_nodes.py +++ b/ortools/constraint_solver/samples/vrp_drop_nodes.py @@ -94,9 +94,11 @@ def create_data_model(): 536, 194, 798, 0 ], ] + # [START demands_capacities] data['demands'] = [0, 1, 1, 3, 6, 3, 6, 8, 8, 1, 2, 1, 2, 6, 6, 8, 8] - data['num_vehicles'] = 4 data['vehicle_capacities'] = [15, 15, 15, 15] + # [END demands_capacities] + data['num_vehicles'] = 4 data['depot'] = 0 return data # [END data_model] @@ -164,7 +166,7 @@ def main(): # Create and register a transit callback. # [START transit_callback] def distance_callback(from_index, to_index): - """Returns the manhattan distance between the two nodes.""" + """Returns the distance between the two nodes.""" # Convert from routing variable Index to distance matrix NodeIndex. from_node = manager.IndexToNode(from_index) to_node = manager.IndexToNode(to_index) diff --git a/ortools/constraint_solver/samples/vrp_global_span.cc b/ortools/constraint_solver/samples/vrp_global_span.cc index 39fb9f1c90..89383afe23 100644 --- a/ortools/constraint_solver/samples/vrp_global_span.cc +++ b/ortools/constraint_solver/samples/vrp_global_span.cc @@ -23,49 +23,45 @@ namespace operations_research { // [START data_model] struct DataModel { - DataModel() - : 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}, - }), - num_vehicles(4), - depot(0) {} - const std::vector> distance_matrix; - const int num_vehicles; - const RoutingIndexManager::NodeIndex depot; -}; // namespace operations_research + const std::vector> 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}, + }; + const int num_vehicles = 4; + const RoutingIndexManager::NodeIndex depot{0}; +}; // [END data_model] // [START solution_printer] diff --git a/ortools/constraint_solver/samples/vrp_global_span.py b/ortools/constraint_solver/samples/vrp_global_span.py index ff3cf20657..0d549fe6f9 100644 --- a/ortools/constraint_solver/samples/vrp_global_span.py +++ b/ortools/constraint_solver/samples/vrp_global_span.py @@ -119,7 +119,7 @@ def print_solution(data, manager, routing, assignment): plan_output += 'Distance of the route: {}m\n'.format(route_distance) print(plan_output) total_distance += route_distance - print('Total Distance of all routes: {}m'.format(total_distance)) + print('Total distance of all routes: {}m'.format(total_distance)) # [END solution_printer] @@ -145,7 +145,7 @@ def main(): # Create and register a transit callback. # [START transit_callback] def distance_callback(from_index, to_index): - """Returns the manhattan distance between the two nodes.""" + """Returns the distance between the two nodes.""" # Convert from routing variable Index to distance matrix NodeIndex. from_node = manager.IndexToNode(from_index) to_node = manager.IndexToNode(to_index) diff --git a/ortools/constraint_solver/samples/vrp_starts_ends.cc b/ortools/constraint_solver/samples/vrp_starts_ends.cc index 0adc4f48e2..5da6c0ff0f 100644 --- a/ortools/constraint_solver/samples/vrp_starts_ends.cc +++ b/ortools/constraint_solver/samples/vrp_starts_ends.cc @@ -23,61 +23,58 @@ namespace operations_research { // [START data_model] struct DataModel { - DataModel() - : 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}, - }), - num_vehicles(4), - starts({ - RoutingIndexManager::NodeIndex{1}, - RoutingIndexManager::NodeIndex{2}, - RoutingIndexManager::NodeIndex{15}, - RoutingIndexManager::NodeIndex{16}, - }), - ends({ - RoutingIndexManager::NodeIndex{0}, - RoutingIndexManager::NodeIndex{0}, - RoutingIndexManager::NodeIndex{0}, - RoutingIndexManager::NodeIndex{0}, - }) {} - const std::vector> distance_matrix; - const int num_vehicles; - const std::vector starts; - const std::vector ends; -}; // namespace operations_research + const std::vector> 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}, + }; + const int num_vehicles = 4; + // [START starts_ends] + const std::vector starts{ + RoutingIndexManager::NodeIndex{1}, + RoutingIndexManager::NodeIndex{2}, + RoutingIndexManager::NodeIndex{15}, + RoutingIndexManager::NodeIndex{16}, + }; + const std::vector ends{ + RoutingIndexManager::NodeIndex{0}, + RoutingIndexManager::NodeIndex{0}, + RoutingIndexManager::NodeIndex{0}, + RoutingIndexManager::NodeIndex{0}, + }; + // [END starts_ends] +}; // [END data_model] // [START solution_printer] diff --git a/ortools/constraint_solver/samples/vrp_starts_ends.py b/ortools/constraint_solver/samples/vrp_starts_ends.py index bb0cdeb8d3..cdab44a985 100644 --- a/ortools/constraint_solver/samples/vrp_starts_ends.py +++ b/ortools/constraint_solver/samples/vrp_starts_ends.py @@ -95,8 +95,10 @@ def create_data_model(): ], ] data['num_vehicles'] = 4 + # [START starts_ends] data['starts'] = [1, 2, 15, 16] data['ends'] = [0, 0, 0, 0] + # [END starts_ends] return data # [END data_model] @@ -120,7 +122,7 @@ def print_solution(data, manager, routing, assignment): plan_output += 'Distance of the route: {}m\n'.format(route_distance) print(plan_output) total_distance += route_distance - print('Total Distance of all routes: {}m'.format(total_distance)) + print('Total distance of all routes: {}m'.format(total_distance)) # [END solution_printer] @@ -147,7 +149,7 @@ def main(): # Create and register a transit callback. # [START transit_callback] def distance_callback(from_index, to_index): - """Returns the manhattan distance between the two nodes.""" + """Returns the distance between the two nodes.""" # Convert from routing variable Index to distance matrix NodeIndex. from_node = manager.IndexToNode(from_index) to_node = manager.IndexToNode(to_index) diff --git a/ortools/constraint_solver/samples/vrp_time_windows.cc b/ortools/constraint_solver/samples/vrp_time_windows.cc index b247031130..b6675e513e 100644 --- a/ortools/constraint_solver/samples/vrp_time_windows.cc +++ b/ortools/constraint_solver/samples/vrp_time_windows.cc @@ -24,52 +24,33 @@ namespace operations_research { // [START data_model] struct DataModel { - DataModel() - : time_matrix({ - {0, 6, 9, 8, 7, 3, 6, 2, 3, 2, 6, 6, 4, 4, 5, 9, 7}, - {6, 0, 8, 3, 2, 6, 8, 4, 8, 8, 13, 7, 5, 8, 12, 10, 14}, - {9, 8, 0, 11, 10, 6, 3, 9, 5, 8, 4, 15, 14, 13, 9, 18, 9}, - {8, 3, 11, 0, 1, 7, 10, 6, 10, 10, 14, 6, 7, 9, 14, 6, 16}, - {7, 2, 10, 1, 0, 6, 9, 4, 8, 9, 13, 4, 6, 8, 12, 8, 14}, - {3, 6, 6, 7, 6, 0, 2, 3, 2, 2, 7, 9, 7, 7, 6, 12, 8}, - {6, 8, 3, 10, 9, 2, 0, 6, 2, 5, 4, 12, 10, 10, 6, 15, 5}, - {2, 4, 9, 6, 4, 3, 6, 0, 4, 4, 8, 5, 4, 3, 7, 8, 10}, - {3, 8, 5, 10, 8, 2, 2, 4, 0, 3, 4, 9, 8, 7, 3, 13, 6}, - {2, 8, 8, 10, 9, 2, 5, 4, 3, 0, 4, 6, 5, 4, 3, 9, 5}, - {6, 13, 4, 14, 13, 7, 4, 8, 4, 4, 0, 10, 9, 8, 4, 13, 4}, - {6, 7, 15, 6, 4, 9, 12, 5, 9, 6, 10, 0, 1, 3, 7, 3, 10}, - {4, 5, 14, 7, 6, 7, 10, 4, 8, 5, 9, 1, 0, 2, 6, 4, 8}, - {4, 8, 13, 9, 8, 7, 10, 3, 7, 4, 8, 3, 2, 0, 4, 5, 6}, - {5, 12, 9, 14, 12, 6, 6, 7, 3, 3, 4, 7, 6, 4, 0, 9, 2}, - {9, 10, 18, 6, 8, 12, 15, 8, 13, 9, 13, 3, 4, 5, 9, 0, 9}, - {7, 14, 9, 16, 14, 8, 5, 10, 6, 5, 4, 10, 8, 6, 2, 9, 0}, - }), - time_windows({ - {0, 0}, - {10, 15}, - {10, 15}, - {5, 10}, - {5, 10}, - {0, 5}, - {5, 10}, - {0, 5}, - {5, 10}, - {0, 5}, - {10, 15}, - {10, 15}, - {0, 5}, - {5, 10}, - {5, 10}, - {10, 15}, - {5, 10}, - }), - num_vehicles(4), - depot(0) {} - const std::vector> time_matrix; - const std::vector> time_windows; - const int num_vehicles; - const RoutingIndexManager::NodeIndex depot; -}; // namespace operations_research + const std::vector> time_matrix{ + {0, 6, 9, 8, 7, 3, 6, 2, 3, 2, 6, 6, 4, 4, 5, 9, 7}, + {6, 0, 8, 3, 2, 6, 8, 4, 8, 8, 13, 7, 5, 8, 12, 10, 14}, + {9, 8, 0, 11, 10, 6, 3, 9, 5, 8, 4, 15, 14, 13, 9, 18, 9}, + {8, 3, 11, 0, 1, 7, 10, 6, 10, 10, 14, 6, 7, 9, 14, 6, 16}, + {7, 2, 10, 1, 0, 6, 9, 4, 8, 9, 13, 4, 6, 8, 12, 8, 14}, + {3, 6, 6, 7, 6, 0, 2, 3, 2, 2, 7, 9, 7, 7, 6, 12, 8}, + {6, 8, 3, 10, 9, 2, 0, 6, 2, 5, 4, 12, 10, 10, 6, 15, 5}, + {2, 4, 9, 6, 4, 3, 6, 0, 4, 4, 8, 5, 4, 3, 7, 8, 10}, + {3, 8, 5, 10, 8, 2, 2, 4, 0, 3, 4, 9, 8, 7, 3, 13, 6}, + {2, 8, 8, 10, 9, 2, 5, 4, 3, 0, 4, 6, 5, 4, 3, 9, 5}, + {6, 13, 4, 14, 13, 7, 4, 8, 4, 4, 0, 10, 9, 8, 4, 13, 4}, + {6, 7, 15, 6, 4, 9, 12, 5, 9, 6, 10, 0, 1, 3, 7, 3, 10}, + {4, 5, 14, 7, 6, 7, 10, 4, 8, 5, 9, 1, 0, 2, 6, 4, 8}, + {4, 8, 13, 9, 8, 7, 10, 3, 7, 4, 8, 3, 2, 0, 4, 5, 6}, + {5, 12, 9, 14, 12, 6, 6, 7, 3, 3, 4, 7, 6, 4, 0, 9, 2}, + {9, 10, 18, 6, 8, 12, 15, 8, 13, 9, 13, 3, 4, 5, 9, 0, 9}, + {7, 14, 9, 16, 14, 8, 5, 10, 6, 5, 4, 10, 8, 6, 2, 9, 0}, + }; + const std::vector> time_windows{ + {0, 0}, {10, 15}, {10, 15}, {5, 10}, {5, 10}, {0, 5}, + {5, 10}, {0, 5}, {5, 10}, {0, 5}, {10, 15}, {10, 15}, + {0, 5}, {5, 10}, {5, 10}, {10, 15}, {5, 10}, + }; + const int num_vehicles = 4; + const RoutingIndexManager::NodeIndex depot{0}; +}; // [END data_model] // [START solution_printer] diff --git a/ortools/constraint_solver/samples/vrp_time_windows.py b/ortools/constraint_solver/samples/vrp_time_windows.py index 80f4b92de6..497dce3557 100644 --- a/ortools/constraint_solver/samples/vrp_time_windows.py +++ b/ortools/constraint_solver/samples/vrp_time_windows.py @@ -82,22 +82,21 @@ def print_solution(data, manager, routing, assignment): time_var = time_dimension.CumulVar(index) slack_var = time_dimension.SlackVar(index) plan_output += ' {0} Time({1},{2}) Slack({3},{4})-> '.format( - manager.IndexToNode(index), - assignment.Min(time_var), - assignment.Max(time_var), - assignment.Min(slack_var), assignment.Max(slack_var)) + manager.IndexToNode(index), assignment.Min(time_var), + assignment.Max(time_var), assignment.Min(slack_var), + assignment.Max(slack_var)) previous_index = index index = assignment.Value(routing.NextVar(index)) route_time += routing.GetArcCostForVehicle(previous_index, index, vehicle_id) time_var = time_dimension.CumulVar(index) plan_output += ' {0} Time({1},{2})\n'.format( - manager.IndexToNode(index), - assignment.Min(time_var), assignment.Max(time_var)) + manager.IndexToNode(index), assignment.Min(time_var), + assignment.Max(time_var)) plan_output += 'Time of the route: {}min\n'.format(route_time) print(plan_output) total_time += route_time - print('Total Time of all routes: {}min'.format(total_time)) + print('Total time of all routes: {}min'.format(total_time)) # [END solution_printer] @@ -123,7 +122,7 @@ def main(): # Create and register a transit callback. # [START transit_callback] def time_callback(from_index, to_index): - """Returns the manhattan distance travel time between the two nodes.""" + """Returns the travel time between the two nodes.""" # Convert from routing variable Index to time matrix NodeIndex. from_node = manager.IndexToNode(from_index) to_node = manager.IndexToNode(to_index) diff --git a/ortools/util/python/functions.i b/ortools/util/python/functions.i index e5885b879d..1b799f8614 100644 --- a/ortools/util/python/functions.i +++ b/ortools/util/python/functions.i @@ -109,7 +109,7 @@ static ReturnT InvokePythonCallableReturning(PyObject* pyfunc) { %typemap(in) std::function { SharedPyPtr input($input); $1 = [input](int64 index) { - return InvokePythonCallableReturning(input.get(), "(l)", index); + return InvokePythonCallableReturning(input.get(), "(L)", index); }; } @@ -122,7 +122,7 @@ static ReturnT InvokePythonCallableReturning(PyObject* pyfunc) { %typemap(in) std::function { SharedPyPtr input($input); $1 = [input](int64 i, int64 j) { - return InvokePythonCallableReturning(input.get(), "ll", i, j); + return InvokePythonCallableReturning(input.get(), "LL", i, j); }; } @@ -135,7 +135,7 @@ static ReturnT InvokePythonCallableReturning(PyObject* pyfunc) { %typemap(in) std::function { SharedPyPtr input($input); $1 = [input](int64 i, int64 j, int64 k) { - return InvokePythonCallableReturning(input.get(), "lll", i, j, k); + return InvokePythonCallableReturning(input.get(), "LLL", i, j, k); }; } @@ -174,7 +174,7 @@ static ReturnT InvokePythonCallableReturning(PyObject* pyfunc) { %typemap(in) std::function { SharedPyPtr input($input); $1 = [input](int64 index) { - return InvokePythonCallableReturning(input.get(), "(l)", index); + return InvokePythonCallableReturning(input.get(), "(L)", index); }; }