diff --git a/ortools/constraint_solver/samples/cvrptw_break.py b/ortools/constraint_solver/samples/cvrptw_break.py index 5f76cd3e09..0429eba09c 100755 --- a/ortools/constraint_solver/samples/cvrptw_break.py +++ b/ortools/constraint_solver/samples/cvrptw_break.py @@ -202,18 +202,17 @@ def add_time_window_constraints(routing, manager, data, time_evaluator_index): # [START solution_printer] def print_solution(data, manager, routing, assignment): # pylint:disable=too-many-locals """Prints assignment on console.""" - print('Objective: {}'.format(assignment.ObjectiveValue())) + print(f'Objective: {assignment.ObjectiveValue()}') print('Breaks:') intervals = assignment.IntervalVarContainer() for i in range(intervals.Size()): brk = intervals.Element(i) if brk.PerformedValue() == 1: - print('{}: Start({}) Duration({})'.format(brk.Var().Name(), - brk.StartValue(), - brk.DurationValue())) + print(f'{brk.Var().Name()}:' + f' Start({brk.StartValue()}) Duration({brk.DurationValue()})') else: - print('{}: Unperformed'.format(brk.Var().Name())) + print(f'{brk.Var().Name()}: Unperformed') total_distance = 0 total_load = 0 @@ -222,37 +221,40 @@ def print_solution(data, manager, routing, assignment): # pylint:disable=too-ma time_dimension = routing.GetDimensionOrDie('Time') for vehicle_id in range(data['num_vehicles']): index = routing.Start(vehicle_id) - plan_output = 'Route for vehicle {}:\n'.format(vehicle_id) + plan_output = f'Route for vehicle {vehicle_id}:\n' distance = 0 while not routing.IsEnd(index): load_var = capacity_dimension.CumulVar(index) time_var = time_dimension.CumulVar(index) slack_var = time_dimension.SlackVar(index) - plan_output += ' {0} Load({1}) Time({2},{3}) Slack({4},{5}) ->'.format( - manager.IndexToNode(index), assignment.Value(load_var), - assignment.Min(time_var), assignment.Max(time_var), - assignment.Min(slack_var), assignment.Max(slack_var)) + node = manager.IndexToNode(index) + plan_output += ( + f' {node}' + f' Load({assignment.Value(load_var)})' + f' Time({assignment.Min(time_var)}, {assignment.Max(time_var)})' + f' Slack({assignment.Min(slack_var)}, {assignment.Max(slack_var)})' + ' ->') previous_index = index index = assignment.Value(routing.NextVar(index)) distance += routing.GetArcCostForVehicle(previous_index, index, vehicle_id) load_var = capacity_dimension.CumulVar(index) time_var = time_dimension.CumulVar(index) - plan_output += ' {0} Load({1}) Time({2},{3})\n'.format( - manager.IndexToNode(index), assignment.Value(load_var), - assignment.Min(time_var), assignment.Max(time_var)) - plan_output += 'Distance of the route: {0}m\n'.format(distance) - plan_output += 'Load of the route: {}\n'.format( - assignment.Value(load_var)) - plan_output += 'Time of the route: {}\n'.format( - assignment.Value(time_var)) + node = manager.IndexToNode(index) + plan_output += ( + f' {node}' + f' Load({assignment.Value(load_var)})' + f' Time({assignment.Min(time_var)}, {assignment.Max(time_var)})\n') + plan_output += f'Distance of the route: {distance}m\n' + plan_output += f'Load of the route: {assignment.Value(load_var)}\n' + plan_output += f'Time of the route: {assignment.Value(time_var)}\n' print(plan_output) total_distance += distance total_load += assignment.Value(load_var) total_time += assignment.Value(time_var) - print('Total Distance of all routes: {0}m'.format(total_distance)) - print('Total Load of all routes: {}'.format(total_load)) - print('Total Time of all routes: {0}min'.format(total_time)) + print(f'Total Distance of all routes: {total_distance}m') + print(f'Total Load of all routes: {total_load}') + print(f'Total Time of all routes: {total_time}min') # [END solution_printer] diff --git a/ortools/constraint_solver/samples/minimal_jobshop_cp.cc b/ortools/constraint_solver/samples/minimal_jobshop_cp.cc index f0170137ef..8ab2d86755 100644 --- a/ortools/constraint_solver/samples/minimal_jobshop_cp.cc +++ b/ortools/constraint_solver/samples/minimal_jobshop_cp.cc @@ -198,7 +198,7 @@ void SolveJobShopExample() { int main(int argc, char** argv) { InitGoogle(argv[0], &argc, &argv, true); - absl::SetFlag(&FLAGS_logtostderr, true); + absl::SetFlag(&FLAGS_stderrthreshold, 0); operations_research::SolveJobShopExample(); return EXIT_SUCCESS; } diff --git a/ortools/constraint_solver/samples/nurses_cp.cc b/ortools/constraint_solver/samples/nurses_cp.cc index 17d712638d..df808e1293 100644 --- a/ortools/constraint_solver/samples/nurses_cp.cc +++ b/ortools/constraint_solver/samples/nurses_cp.cc @@ -205,7 +205,7 @@ void SolveNursesExample() { int main(int argc, char** argv) { InitGoogle(argv[0], &argc, &argv, true); - absl::SetFlag(&FLAGS_logtostderr, true); + absl::SetFlag(&FLAGS_stderrthreshold, 0); operations_research::SolveNursesExample(); return EXIT_SUCCESS; } diff --git a/ortools/constraint_solver/samples/rabbits_and_pheasants_cp.cc b/ortools/constraint_solver/samples/rabbits_and_pheasants_cp.cc index 810dac5fc4..7707d34fe5 100644 --- a/ortools/constraint_solver/samples/rabbits_and_pheasants_cp.cc +++ b/ortools/constraint_solver/samples/rabbits_and_pheasants_cp.cc @@ -60,7 +60,7 @@ void RunConstraintProgrammingExample() { int main(int argc, char** argv) { InitGoogle(argv[0], &argc, &argv, true); - absl::SetFlag(&FLAGS_logtostderr, true); + absl::SetFlag(&FLAGS_stderrthreshold, 0); operations_research::RunConstraintProgrammingExample(); return EXIT_SUCCESS; } diff --git a/ortools/constraint_solver/samples/simple_cp_program.py b/ortools/constraint_solver/samples/simple_cp_program.py index c3d802a306..ac402bdfbd 100755 --- a/ortools/constraint_solver/samples/simple_cp_program.py +++ b/ortools/constraint_solver/samples/simple_cp_program.py @@ -53,18 +53,18 @@ def main(): solver.NewSearch(decision_builder) while solver.NextSolution(): count += 1 - solution = 'Solution {}:\n'.format(count) + solution = f'Solution {count}:\n' for var in [x, y, z]: - solution += ' {} = {}'.format(var.Name(), var.Value()) + solution += f' {var.Name()} = {var.Value()}' print(solution) solver.EndSearch() - print('Number of solutions found: ', count) + print(f'Number of solutions found: {count}') # [END print_solution] # [START advanced] print('Advanced usage:') - print('Problem solved in ', solver.WallTime(), 'ms') - print('Memory usage: ', pywrapcp.Solver.MemoryUsage(), 'bytes') + print(f'Problem solved in {solver.WallTime()}ms') + print(f'Memory usage: {pywrapcp.Solver.MemoryUsage()}bytes') # [END advanced] diff --git a/ortools/constraint_solver/samples/simple_ls_program.cc b/ortools/constraint_solver/samples/simple_ls_program.cc index cea8e7c7c9..893d0e55d6 100644 --- a/ortools/constraint_solver/samples/simple_ls_program.cc +++ b/ortools/constraint_solver/samples/simple_ls_program.cc @@ -29,13 +29,12 @@ class OneVarLns : public BaseLns { explicit OneVarLns(const std::vector& vars) : BaseLns(vars), index_(0) {} - ~OneVarLns() override {} + ~OneVarLns() override = default; void InitFragments() override { index_ = 0; } bool NextFragment() override { - const int size = Size(); - if (index_ < size) { + if (index_ < Size()) { AppendToFragment(index_); ++index_; return true; @@ -55,7 +54,7 @@ class MoveOneVar : public IntVarLocalSearchOperator { variable_index_(0), move_up_(false) {} - ~MoveOneVar() override {} + ~MoveOneVar() override = default; protected: // Make a neighbor assigning one variable to its target value. @@ -88,7 +87,7 @@ class SumFilter : public IntVarLocalSearchFilter { explicit SumFilter(const std::vector& vars) : IntVarLocalSearchFilter(vars), sum_(0) {} - ~SumFilter() override {} + ~SumFilter() override = default; void OnSynchronize(const Assignment* delta) override { sum_ = 0; @@ -200,7 +199,7 @@ void SolveProblem(SolveType solve_type) { int main(int argc, char** argv) { InitGoogle(argv[0], &argc, &argv, true); - absl::SetFlag(&FLAGS_logtostderr, true); + absl::SetFlag(&FLAGS_stderrthreshold, 0); operations_research::SolveProblem(operations_research::LNS); operations_research::SolveProblem(operations_research::LS); operations_research::SolveProblem(operations_research::LS_WITH_FILTER); diff --git a/ortools/constraint_solver/samples/simple_routing_program.cc b/ortools/constraint_solver/samples/simple_routing_program.cc index 0d582d1bc4..29a492003a 100644 --- a/ortools/constraint_solver/samples/simple_routing_program.cc +++ b/ortools/constraint_solver/samples/simple_routing_program.cc @@ -15,6 +15,7 @@ // [START import] #include #include +#include #include #include "ortools/constraint_solver/routing.h" @@ -73,11 +74,11 @@ void SimpleRoutingProgram() { // Inspect solution. int64_t index = routing.Start(0); LOG(INFO) << "Route for Vehicle 0:"; - int64_t route_distance{0}; + int64_t route_distance = 0; std::ostringstream route; - while (routing.IsEnd(index) == false) { + while (!routing.IsEnd(index)) { route << manager.IndexToNode(index).value() << " -> "; - int64_t previous_index = index; + const int64_t previous_index = index; index = solution->Value(routing.NextVar(index)); route_distance += routing.GetArcCostForVehicle(previous_index, index, int64_t{0}); diff --git a/ortools/constraint_solver/samples/simple_routing_program.py b/ortools/constraint_solver/samples/simple_routing_program.py index a8d98c973c..a3259dec11 100755 --- a/ortools/constraint_solver/samples/simple_routing_program.py +++ b/ortools/constraint_solver/samples/simple_routing_program.py @@ -72,17 +72,17 @@ def main(): # Print solution on console. # [START print_solution] - print('Objective: {}'.format(assignment.ObjectiveValue())) + print(f'Objective: {assignment.ObjectiveValue()}') index = routing.Start(0) plan_output = 'Route for vehicle 0:\n' route_distance = 0 while not routing.IsEnd(index): - plan_output += '{} -> '.format(manager.IndexToNode(index)) + plan_output += f'{manager.IndexToNode(index)} -> ' previous_index = index index = assignment.Value(routing.NextVar(index)) route_distance += routing.GetArcCostForVehicle(previous_index, index, 0) - plan_output += '{}\n'.format(manager.IndexToNode(index)) - plan_output += 'Distance of the route: {}m\n'.format(route_distance) + plan_output += f'{manager.IndexToNode(index)}\n' + plan_output += f'Distance of the route: {route_distance}m\n' print(plan_output) # [END print_solution] diff --git a/ortools/constraint_solver/samples/tsp.cc b/ortools/constraint_solver/samples/tsp.cc index 0b2a36419d..c1f1d7d285 100644 --- a/ortools/constraint_solver/samples/tsp.cc +++ b/ortools/constraint_solver/samples/tsp.cc @@ -15,6 +15,7 @@ // [START import] #include #include +#include #include #include @@ -52,12 +53,12 @@ std::vector> GenerateManhattanDistanceMatrix( std::vector> distances = std::vector>( locations.size(), std::vector(locations.size(), int64_t{0})); - for (int fromNode = 0; fromNode < locations.size(); fromNode++) { - for (int toNode = 0; toNode < locations.size(); toNode++) { - if (fromNode != toNode) - distances[fromNode][toNode] = - int64_t{std::abs(locations[toNode][0] - locations[fromNode][0]) + - std::abs(locations[toNode][1] - locations[fromNode][1])}; + for (int from_node = 0; from_node < locations.size(); from_node++) { + for (int to_node = 0; to_node < locations.size(); to_node++) { + if (from_node != to_node) + distances[from_node][to_node] = + int64_t{std::abs(locations[to_node][0] - locations[from_node][0]) + + std::abs(locations[to_node][1] - locations[from_node][1])}; } } return distances; @@ -77,9 +78,9 @@ void PrintSolution(const RoutingIndexManager& manager, LOG(INFO) << "Route for Vehicle 0:"; int64_t distance{0}; std::stringstream route; - while (routing.IsEnd(index) == false) { + while (!routing.IsEnd(index)) { route << manager.IndexToNode(index).value() << " -> "; - int64_t previous_index = index; + const int64_t previous_index = index; index = solution.Value(routing.NextVar(index)); distance += routing.GetArcCostForVehicle(previous_index, index, int64_t{0}); } @@ -112,8 +113,8 @@ void Tsp() { // [START transit_callback] const auto distance_matrix = GenerateManhattanDistanceMatrix(data.locations); const int transit_callback_index = routing.RegisterTransitCallback( - [&distance_matrix, &manager](int64_t from_index, - int64_t to_index) -> int64_t { + [&distance_matrix, &manager](const int64_t from_index, + const int64_t to_index) -> int64_t { // Convert from routing variable Index to distance matrix NodeIndex. auto from_node = manager.IndexToNode(from_index).value(); auto to_node = manager.IndexToNode(to_index).value(); diff --git a/ortools/constraint_solver/samples/tsp.py b/ortools/constraint_solver/samples/tsp.py index 20e4576a6f..ee2e14c414 100755 --- a/ortools/constraint_solver/samples/tsp.py +++ b/ortools/constraint_solver/samples/tsp.py @@ -78,17 +78,17 @@ def create_distance_callback(data, manager): # [START solution_printer] def print_solution(manager, routing, assignment): """Prints assignment on console.""" - print('Objective: {}'.format(assignment.ObjectiveValue())) + print(f'Objective: {assignment.ObjectiveValue()}') index = routing.Start(0) plan_output = 'Route for vehicle 0:\n' route_distance = 0 while not routing.IsEnd(index): - plan_output += ' {} ->'.format(manager.IndexToNode(index)) + plan_output += f' {manager.IndexToNode(index)} ->' previous_index = index index = assignment.Value(routing.NextVar(index)) route_distance += routing.GetArcCostForVehicle(previous_index, index, 0) - plan_output += ' {}\n'.format(manager.IndexToNode(index)) - plan_output += 'Distance of the route: {}m\n'.format(route_distance) + plan_output += f' {manager.IndexToNode(index)}\n' + plan_output += f'Distance of the route: {route_distance}m\n' print(plan_output) # [END solution_printer] diff --git a/ortools/constraint_solver/samples/tsp_circuit_board.cc b/ortools/constraint_solver/samples/tsp_circuit_board.cc index df4fcf6d51..67f1c623d6 100644 --- a/ortools/constraint_solver/samples/tsp_circuit_board.cc +++ b/ortools/constraint_solver/samples/tsp_circuit_board.cc @@ -88,12 +88,12 @@ std::vector> ComputeEuclideanDistanceMatrix( std::vector> distances = std::vector>( locations.size(), std::vector(locations.size(), int64_t{0})); - for (int fromNode = 0; fromNode < locations.size(); fromNode++) { - for (int toNode = 0; toNode < locations.size(); toNode++) { - if (fromNode != toNode) - distances[fromNode][toNode] = static_cast( - std::hypot((locations[toNode][0] - locations[fromNode][0]), - (locations[toNode][1] - locations[fromNode][1]))); + for (int from_node = 0; from_node < locations.size(); from_node++) { + for (int to_node = 0; to_node < locations.size(); to_node++) { + if (from_node != to_node) + distances[from_node][to_node] = static_cast( + std::hypot((locations[to_node][0] - locations[from_node][0]), + (locations[to_node][1] - locations[from_node][1]))); } } return distances; @@ -113,9 +113,9 @@ void PrintSolution(const RoutingIndexManager& manager, LOG(INFO) << "Route:"; int64_t distance{0}; std::stringstream route; - while (routing.IsEnd(index) == false) { + while (!routing.IsEnd(index)) { route << manager.IndexToNode(index).value() << " -> "; - int64_t previous_index = index; + const int64_t previous_index = index; index = solution.Value(routing.NextVar(index)); distance += routing.GetArcCostForVehicle(previous_index, index, int64_t{0}); } @@ -147,8 +147,8 @@ void Tsp() { // [START transit_callback] const auto distance_matrix = ComputeEuclideanDistanceMatrix(data.locations); const int transit_callback_index = routing.RegisterTransitCallback( - [&distance_matrix, &manager](int64_t from_index, - int64_t to_index) -> int64_t { + [&distance_matrix, &manager](const int64_t from_index, + const int64_t to_index) -> int64_t { // Convert from routing variable Index to distance matrix NodeIndex. auto from_node = manager.IndexToNode(from_index).value(); auto to_node = manager.IndexToNode(to_index).value(); diff --git a/ortools/constraint_solver/samples/tsp_circuit_board.py b/ortools/constraint_solver/samples/tsp_circuit_board.py index 9fc1f0ce88..d6079900da 100755 --- a/ortools/constraint_solver/samples/tsp_circuit_board.py +++ b/ortools/constraint_solver/samples/tsp_circuit_board.py @@ -103,18 +103,18 @@ def compute_euclidean_distance_matrix(locations): # [START solution_printer] def print_solution(manager, routing, solution): """Prints solution on console.""" - print('Objective: {}'.format(solution.ObjectiveValue())) + print(f'Objective: {solution.ObjectiveValue()}') index = routing.Start(0) plan_output = 'Route:\n' route_distance = 0 while not routing.IsEnd(index): - plan_output += ' {} ->'.format(manager.IndexToNode(index)) + plan_output += f' {manager.IndexToNode(index)} ->' previous_index = index index = solution.Value(routing.NextVar(index)) route_distance += routing.GetArcCostForVehicle(previous_index, index, 0) - plan_output += ' {}\n'.format(manager.IndexToNode(index)) + plan_output += f' {manager.IndexToNode(index)}\n' print(plan_output) - plan_output += 'Objective: {}m\n'.format(route_distance) + plan_output += f'Objective: {route_distance}m\n' # [END solution_printer] diff --git a/ortools/constraint_solver/samples/tsp_cities.cc b/ortools/constraint_solver/samples/tsp_cities.cc index d025288478..12311ec12b 100644 --- a/ortools/constraint_solver/samples/tsp_cities.cc +++ b/ortools/constraint_solver/samples/tsp_cities.cc @@ -60,9 +60,9 @@ void PrintSolution(const RoutingIndexManager& manager, LOG(INFO) << "Route:"; int64_t distance{0}; std::stringstream route; - while (routing.IsEnd(index) == false) { + while (!routing.IsEnd(index)) { route << manager.IndexToNode(index).value() << " -> "; - int64_t previous_index = index; + const int64_t previous_index = index; index = solution.Value(routing.NextVar(index)); distance += routing.GetArcCostForVehicle(previous_index, index, int64_t{0}); } @@ -93,7 +93,8 @@ void Tsp() { // [START transit_callback] const int transit_callback_index = routing.RegisterTransitCallback( - [&data, &manager](int64_t from_index, int64_t to_index) -> int64_t { + [&data, &manager](const int64_t from_index, + const int64_t to_index) -> int64_t { // Convert from routing variable Index to distance matrix NodeIndex. auto from_node = manager.IndexToNode(from_index).value(); auto to_node = manager.IndexToNode(to_index).value(); diff --git a/ortools/constraint_solver/samples/tsp_cities.py b/ortools/constraint_solver/samples/tsp_cities.py index 43e18b7924..4d5273a459 100755 --- a/ortools/constraint_solver/samples/tsp_cities.py +++ b/ortools/constraint_solver/samples/tsp_cities.py @@ -49,18 +49,18 @@ def create_data_model(): # [START solution_printer] def print_solution(manager, routing, solution): """Prints solution on console.""" - print('Objective: {} miles'.format(solution.ObjectiveValue())) + print(f'Objective: {solution.ObjectiveValue()} miles') index = routing.Start(0) plan_output = 'Route for vehicle 0:\n' route_distance = 0 while not routing.IsEnd(index): - plan_output += ' {} ->'.format(manager.IndexToNode(index)) + plan_output += f' {manager.IndexToNode(index)} ->' previous_index = index index = solution.Value(routing.NextVar(index)) route_distance += routing.GetArcCostForVehicle(previous_index, index, 0) - plan_output += ' {}\n'.format(manager.IndexToNode(index)) + plan_output += f' {manager.IndexToNode(index)}\n' print(plan_output) - plan_output += 'Route distance: {}miles\n'.format(route_distance) + plan_output += f'Route distance: {route_distance}miles\n' # [END solution_printer] diff --git a/ortools/constraint_solver/samples/tsp_cities_routes.cc b/ortools/constraint_solver/samples/tsp_cities_routes.cc index 05ad923309..ecbc6df7cd 100644 --- a/ortools/constraint_solver/samples/tsp_cities_routes.cc +++ b/ortools/constraint_solver/samples/tsp_cities_routes.cc @@ -92,7 +92,8 @@ void Tsp() { // Define cost of each arc. // [START arc_cost] const int transit_callback_index = routing.RegisterTransitCallback( - [&data, &manager](int64_t from_index, int64_t to_index) -> int64_t { + [&data, &manager](const int64_t from_index, + const int64_t to_index) -> int64_t { // Convert from routing variable Index to distance matrix NodeIndex. auto from_node = manager.IndexToNode(from_index).value(); auto to_node = manager.IndexToNode(to_index).value(); diff --git a/ortools/constraint_solver/samples/tsp_distance_matrix.cc b/ortools/constraint_solver/samples/tsp_distance_matrix.cc index 6465370c66..3044fcef9a 100644 --- a/ortools/constraint_solver/samples/tsp_distance_matrix.cc +++ b/ortools/constraint_solver/samples/tsp_distance_matrix.cc @@ -80,9 +80,9 @@ void PrintSolution(const RoutingIndexManager& manager, LOG(INFO) << "Route for Vehicle 0:"; int64_t distance{0}; std::stringstream route; - while (routing.IsEnd(index) == false) { + while (!routing.IsEnd(index)) { route << manager.IndexToNode(index).value() << " -> "; - int64_t previous_index = index; + const int64_t previous_index = index; index = solution.Value(routing.NextVar(index)); distance += routing.GetArcCostForVehicle(previous_index, index, int64_t{0}); } @@ -114,7 +114,8 @@ void Tsp() { // Create and register a transit callback. // [START transit_callback] const int transit_callback_index = routing.RegisterTransitCallback( - [&data, &manager](int64_t from_index, int64_t to_index) -> int64_t { + [&data, &manager](const int64_t from_index, + const int64_t to_index) -> int64_t { // Convert from routing variable Index to distance matrix NodeIndex. auto from_node = manager.IndexToNode(from_index).value(); auto to_node = manager.IndexToNode(to_index).value(); diff --git a/ortools/constraint_solver/samples/tsp_distance_matrix.py b/ortools/constraint_solver/samples/tsp_distance_matrix.py index 6452c39f29..e5876cc323 100755 --- a/ortools/constraint_solver/samples/tsp_distance_matrix.py +++ b/ortools/constraint_solver/samples/tsp_distance_matrix.py @@ -104,17 +104,17 @@ def create_data_model(): # [START solution_printer] def print_solution(manager, routing, solution): """Prints solution on console.""" - print('Objective: {}'.format(solution.ObjectiveValue())) + print(f'Objective: {solution.ObjectiveValue()}') index = routing.Start(0) plan_output = 'Route for vehicle 0:\n' route_distance = 0 while not routing.IsEnd(index): - plan_output += ' {} ->'.format(manager.IndexToNode(index)) + plan_output += f' {manager.IndexToNode(index)} ->' previous_index = index index = solution.Value(routing.NextVar(index)) route_distance += routing.GetArcCostForVehicle(previous_index, index, 0) - plan_output += ' {}\n'.format(manager.IndexToNode(index)) - plan_output += 'Distance of the route: {}m\n'.format(route_distance) + plan_output += f' {manager.IndexToNode(index)}\n' + plan_output += f'Distance of the route: {route_distance}m\n' print(plan_output) # [END solution_printer] diff --git a/ortools/constraint_solver/samples/vrp.cc b/ortools/constraint_solver/samples/vrp.cc index f2c03e6bc3..aa8ad528d8 100644 --- a/ortools/constraint_solver/samples/vrp.cc +++ b/ortools/constraint_solver/samples/vrp.cc @@ -82,9 +82,9 @@ void PrintSolution(const DataModel& data, const RoutingIndexManager& manager, LOG(INFO) << "Route for Vehicle " << vehicle_id << ":"; int64_t distance{0}; std::stringstream route; - while (routing.IsEnd(index) == false) { + while (!routing.IsEnd(index)) { route << manager.IndexToNode(index).value() << " -> "; - int64_t previous_index = index; + const int64_t previous_index = index; index = solution.Value(routing.NextVar(index)); distance += routing.GetArcCostForVehicle(previous_index, index, int64_t{vehicle_id}); @@ -120,7 +120,8 @@ void Vrp() { // Create and register a transit callback. // [START transit_callback] const int transit_callback_index = routing.RegisterTransitCallback( - [&data, &manager](int64_t from_index, int64_t to_index) -> int64_t { + [&data, &manager](const int64_t from_index, + const int64_t to_index) -> int64_t { // Convert from routing variable Index to distance matrix NodeIndex. auto from_node = manager.IndexToNode(from_index).value(); auto to_node = manager.IndexToNode(to_index).value(); diff --git a/ortools/constraint_solver/samples/vrp.py b/ortools/constraint_solver/samples/vrp.py index 4d412b423f..04d0570f14 100755 --- a/ortools/constraint_solver/samples/vrp.py +++ b/ortools/constraint_solver/samples/vrp.py @@ -116,19 +116,19 @@ def print_solution(data, manager, routing, solution): total_distance = 0 for vehicle_id in range(data['num_vehicles']): index = routing.Start(vehicle_id) - plan_output = 'Route for vehicle {}:\n'.format(vehicle_id) + plan_output = f'Route for vehicle {vehicle_id}:\n' route_distance = 0 while not routing.IsEnd(index): - plan_output += ' {} ->'.format(manager.IndexToNode(index)) + plan_output += f' {manager.IndexToNode(index)} ->' previous_index = index index = solution.Value(routing.NextVar(index)) route_distance += routing.GetArcCostForVehicle( previous_index, index, vehicle_id) - plan_output += ' {}\n'.format(manager.IndexToNode(index)) - plan_output += 'Distance of the route: {}m\n'.format(route_distance) + plan_output += f' {manager.IndexToNode(index)}\n' + plan_output += f'Distance of the route: {route_distance}m\n' print(plan_output) total_distance += route_distance - print('Total Distance of all routes: {}m'.format(total_distance)) + print(f'Total Distance of all routes: {total_distance}m') # [END solution_printer] diff --git a/ortools/constraint_solver/samples/vrp_breaks.cc b/ortools/constraint_solver/samples/vrp_breaks.cc index 8172119ffc..d8d286b9ec 100644 --- a/ortools/constraint_solver/samples/vrp_breaks.cc +++ b/ortools/constraint_solver/samples/vrp_breaks.cc @@ -89,7 +89,7 @@ void PrintSolution(const RoutingIndexManager& manager, LOG(INFO) << "Route for Vehicle " << vehicle_id << ":"; int64_t index = routing.Start(vehicle_id); std::stringstream route; - while (routing.IsEnd(index) == false) { + while (!routing.IsEnd(index)) { const IntVar* time_var = time_dimension.CumulVar(index); route << manager.IndexToNode(index).value() << " Time(" << solution.Value(time_var) << ") -> "; @@ -128,7 +128,8 @@ void VrpBreaks() { // Create and register a transit callback. // [START transit_callback] const int transit_callback_index = routing.RegisterTransitCallback( - [&data, &manager](int64_t from_index, int64_t to_index) -> int64_t { + [&data, &manager](const int64_t from_index, + const int64_t to_index) -> int64_t { // Convert from routing variable Index to distance matrix NodeIndex. int from_node = manager.IndexToNode(from_index).value(); int to_node = manager.IndexToNode(to_index).value();