From 8be7663396232c06a9fd620c6c5ebda838e302a7 Mon Sep 17 00:00:00 2001 From: Corentin Le Molgat Date: Wed, 5 Jul 2023 18:33:58 +0200 Subject: [PATCH] constraint_solver: update internal doc --- ortools/constraint_solver/docs/CP.md | 29 ++++++++++++----------- ortools/constraint_solver/docs/ROUTING.md | 16 ++++++------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/ortools/constraint_solver/docs/CP.md b/ortools/constraint_solver/docs/CP.md index 0086be4e7e..df4d86d0ba 100644 --- a/ortools/constraint_solver/docs/CP.md +++ b/ortools/constraint_solver/docs/CP.md @@ -77,40 +77,41 @@ from ortools.constraint_solver import pywrapcp def main(): """Entry point of the program.""" # Instantiate the solver. - solver = pywrapcp.Solver('CPSimple') + solver = pywrapcp.Solver("CPSimple") # Create the variables. num_vals = 3 - x = solver.IntVar(0, num_vals - 1, 'x') - y = solver.IntVar(0, num_vals - 1, 'y') - z = solver.IntVar(0, num_vals - 1, 'z') + x = solver.IntVar(0, num_vals - 1, "x") + y = solver.IntVar(0, num_vals - 1, "y") + z = solver.IntVar(0, num_vals - 1, "z") # Constraint 0: x != y. solver.Add(x != y) - print('Number of constraints: ', solver.Constraints()) + print("Number of constraints: ", solver.Constraints()) # Solve the problem. - decision_builder = solver.Phase([x, y, z], solver.CHOOSE_FIRST_UNBOUND, - solver.ASSIGN_MIN_VALUE) + decision_builder = solver.Phase( + [x, y, z], solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MIN_VALUE + ) # Print solution on console. count = 0 solver.NewSearch(decision_builder) while solver.NextSolution(): count += 1 - solution = f'Solution {count}:\n' + solution = f"Solution {count}:\n" for var in [x, y, z]: - solution += f' {var.Name()} = {var.Value()}' + solution += f" {var.Name()} = {var.Value()}" print(solution) solver.EndSearch() - print(f'Number of solutions found: {count}') + print(f"Number of solutions found: {count}") - print('Advanced usage:') - print(f'Problem solved in {solver.WallTime()}ms') - print(f'Memory usage: {pywrapcp.Solver.MemoryUsage()}bytes') + print("Advanced usage:") + print(f"Problem solved in {solver.WallTime()}ms") + print(f"Memory usage: {pywrapcp.Solver.MemoryUsage()}bytes") -if __name__ == '__main__': +if __name__ == "__main__": main() ``` diff --git a/ortools/constraint_solver/docs/ROUTING.md b/ortools/constraint_solver/docs/ROUTING.md index f692090934..3517b169f1 100644 --- a/ortools/constraint_solver/docs/ROUTING.md +++ b/ortools/constraint_solver/docs/ROUTING.md @@ -110,7 +110,6 @@ def main(): # Create Routing Model. routing = pywrapcp.RoutingModel(manager) - # Create and register a transit callback. def distance_callback(from_index, to_index): """Returns the absolute difference between the two nodes.""" @@ -127,27 +126,28 @@ def main(): # Setting first solution heuristic. search_parameters = pywrapcp.DefaultRoutingSearchParameters() search_parameters.first_solution_strategy = ( - routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC) # pylint: disable=no-member + routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC + ) # pylint: disable=no-member # Solve the problem. assignment = routing.SolveWithParameters(search_parameters) # Print solution on console. - print(f'Objective: {assignment.ObjectiveValue()}') + print(f"Objective: {assignment.ObjectiveValue()}") index = routing.Start(0) - plan_output = 'Route for vehicle 0:\n' + plan_output = "Route for vehicle 0:\n" route_distance = 0 while not routing.IsEnd(index): - plan_output += f'{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 += f'{manager.IndexToNode(index)}\n' - plan_output += f'Distance of the route: {route_distance}m\n' + plan_output += f"{manager.IndexToNode(index)}\n" + plan_output += f"Distance of the route: {route_distance}m\n" print(plan_output) -if __name__ == '__main__': +if __name__ == "__main__": main() ```