Print entire solution in VRP callback

773 views
Skip to first unread message

Thomas Bridi

unread,
Jan 20, 2020, 9:33:20 AM1/20/20
to or-tools-discuss
Hello,
I'm trying to print each solution that the solver finds in a VRP.
In this group I've found the solution callback:

class SolutionCallback(object):
        def __init__(self, model):
            self.model = model

        def __call__(self):
            print("Solution", self.model.CostVar().Max())
            

solution_callback = SolutionCallback(routing)
routing.AddAtSolutionCallback(solution_callback)



And I have the classical print_solution of the examples:
def print_solution(manager, routing, assignment):
    """Prints assignment on console."""
    print('Objective: {} miles'.format(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))
        previous_index = index
        index = assignment.Value(routing.NextVar(index))
        route_distance += routing.GetArcCostForVehicle(previous_index, index, 0)
    plan_output += ' {}\n'.format(manager.IndexToNode(index))
    print(plan_output)
    plan_output += 'Route distance: {}miles\n'.format(route_distance)

What I would like is to call the print_solution function from the __call__ of the SolutionCallback class.
What I can not find is the assignment. 

How can I get the assignment in the solution in the callback?

Thanks,
Thomas

Vincent Furnon

unread,
Jan 20, 2020, 10:08:39 AM1/20/20
to or-tools...@googlegroups.com
You actually don't need the assignment, you can directly look at the variable values when the callback is called. For instance, instead of index = assignment.Value(routing.NextVar(index)), just do index = routing.NextVar(index).Value().

--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/935ffb69-16dd-428d-afad-65007d016147%40googlegroups.com.

Thomas Bridi

unread,
Jan 20, 2020, 10:20:20 AM1/20/20
to or-tools-discuss
It worked. Thanks


Il giorno lunedì 20 gennaio 2020 16:08:39 UTC+1, Vincent Furnon ha scritto:
You actually don't need the assignment, you can directly look at the variable values when the callback is called. For instance, instead of index = assignment.Value(routing.NextVar(index)), just do index = routing.NextVar(index).Value().

To unsubscribe from this group and stop receiving emails from it, send an email to or-tools...@googlegroups.com.

Haakon HR

unread,
Jun 23, 2020, 8:53:54 AM6/23/20
to or-tools-discuss
Is there a way to access the CumulVars inside the callback? I'm struggling with it at the moment. I'm using something like the below, but it does not work currently

class SolutionCallback(object):
        def __init__(self, model):
            self.model = model
            self.time = model.GetDimensionOrDie("Time")

        def __call__(self):
           index = self.model.NextVar(0)
           time_var = self.time.CumulVar(index)
           print(time_var.Value())

On Monday, January 20, 2020 at 4:08:39 PM UTC+1, Vincent Furnon wrote:
You actually don't need the assignment, you can directly look at the variable values when the callback is called. For instance, instead of index = assignment.Value(routing.NextVar(index)), just do index = routing.NextVar(index).Value().

To unsubscribe from this group and stop receiving emails from it, send an email to or-tools...@googlegroups.com.

Haakon HR

unread,
Jun 23, 2020, 8:56:22 AM6/23/20
to or-tools-discuss
The reason was obvious the moment I clicked "reply". I guess it is just due to the fact that the variable doesn't actually have a value, but an interval so only Min() and Max() makes sense; not Value().
Reply all
Reply to author
Forward
0 new messages