constraint_solver: update internal doc
This commit is contained in:
@@ -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()
|
||||
```
|
||||
|
||||
|
||||
@@ -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()
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user