starts and ends with initial routes set up

431 views
Skip to first unread message

Sachin S

unread,
Aug 22, 2021, 9:08:03 PM8/22/21
to or-tools-discuss
I set up starts and ends with initial routes, do not get any error or any response,

The same problem is solved separately for start and ends, and initial routes when I combine them together it does not respond .. any help will be appreciated

Sachin S

unread,
Aug 22, 2021, 9:23:28 PM8/22/21
to or-tools-discuss
here is the code 


    data['pickups_deliveries'] = [
        [6, 3],
        [5, 4],
   #     [10, 2]
  #      [9, 10]
     ]
    data['initial_routes'] = [
        [8, 6, 3],
        [7, 5, 4],
        [1, 2],
        [9, 10]
    ]
    data['starts'] = [8, 7, 1, 9]
    data['ends'] = [0, 0, 0, 0]
    data['depot'] = 0


# Setting first solution heuristic.
    search_parameters = pywrapcp.DefaultRoutingSearchParameters()
    search_parameters.first_solution_strategy = (
        routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)

    # Solve the problem.
    solution = routing.SolveWithParameters(search_parameters)

    
    initial_solution = routing.ReadAssignmentFromRoutes(data['initial_routes'],
                                                        True)
    print('Initial solution:')
    print_solution(data, manager, routing, initial_solution)
    
    # Solve the problem.
    solution = routing.SolveFromAssignmentWithParameters(
        initial_solution, search_parameters)

blind.line

unread,
Aug 23, 2021, 1:17:10 AM8/23/21
to or-tools...@googlegroups.com
Wild guess because I have no real idea what you are asking, but I notice that you have a 10 in your pickups that maybe should be a 1? 

If you send in a version of code that runs without any edits it will be easier to find your issue. 

James

On Aug 22, 2021, at 18:23, Sachin S <sumant...@gmail.com> wrote:

here is the code 
--
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/7cb6c1d2-4b92-4aba-a02e-177edc880f3dn%40googlegroups.com.

Sachin S

unread,
Aug 23, 2021, 6:19:04 AM8/23/21
to or-tools...@googlegroups.com
James,
Thanks

I ma combining the two problems from this example page

"""Vehicles Routing Problem (VRP)."""

from ortools.constraint_solver import pywrapcp
from ortools.constraint_solver import routing_enums_pb2

def create_data_model():
    """Stores the data for the problem."""
    data = {}
    data['distance_matrix'] = [
        [
            0548776696582274502194308194536502388354,
            468776662
        ],
        [
            54806843081945027303546967421084594480674,
            10168681210
        ],
        [
            776684099287850227481046874240012781164,
            11307881552754
        ],
        [
            69630899201146508785028448901232514628822,
            11645601358
        ],
        [
            58219487811405367643887307761118400514708,
            10506741244
        ],
        [
            2745025026505360228308194240582776662628,
            5141050708
        ],
        [
            50273027487876422805361944683541004890856,
            5141278480
        ],
        [
            1943548105023883085360342388730468354320,
            662742856
        ],
        [
            3086964688447301941943420274388810696662,
            3201084514
        ],
        [
            1947427428907762404683882740342536422388,
            274810468
        ],
        [
            5361084400123211185823547303883420878764,
            7303881152354
        ],
        [
            502594127851440077610044688105368780114,
            308650274844
        ],
        [
            38848011646285146628903546964227641140194,
            536388730
        ],
        [
            35467411308227086288563206623887303081940,
            342422536
        ],
        [
            468101678811641050514514662320274388650536,
            3420764194
        ],
        [
            77686815525606741050127874210848101152274,
            3884227640798
        ],
        [
            662121075413581244708480856514468354844730,
            5361947980
        ],
    ]
    data['initial_routes'] = [
        [814131211],
        [34910],
        [151],
        [7526],
    ]
    data['starts'] = [83157]
    data['ends'] = [0000]
    data['num_vehicles'] = 4
    #initial routes 
    #data['depot'] = 0
    return data


def print_solution(datamanagerroutingsolution):
    """Prints solution on console."""
    print(f'Objective: {solution.ObjectiveValue()}')
    max_route_distance = 0
    for vehicle_id in range(data['num_vehicles']):
        index = routing.Start(vehicle_id)
        plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
        route_distance = 0
        while not routing.IsEnd(index):
            plan_output += {} -> '.format(manager.IndexToNode(index))
            previous_index = index
            index = solution.Value(routing.NextVar(index))
            route_distance += routing.GetArcCostForVehicle(
                previous_indexindexvehicle_id)
        plan_output += '{}\n'.format(manager.IndexToNode(index))
        plan_output += 'Distance of the route: {}m\n'.format(route_distance)
        print(plan_output)
        max_route_distance = max(route_distancemax_route_distance)
    print('Maximum of the route distances: {}m'.format(max_route_distance))



def main():
    """Solve the CVRP problem."""
    # Instantiate the data problem.
    data = create_data_model()

    # Create the routing index manager. (Initial routes)
    #manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),
    #                                       data['num_vehicles'], data['depot'])
    # Create the routing index manager. (Starts and ends)
    manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),
                                           data['num_vehicles'], data['starts'],
                                           data['ends'])
    # Create Routing Model.
    routing = pywrapcp.RoutingModel(manager)


    # Create and register a transit callback.
    def distance_callback(from_indexto_index):
        """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)
        return data['distance_matrix'][from_node][to_node]

    transit_callback_index = routing.RegisterTransitCallback(distance_callback)

    # Define cost of each arc.
    routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)

    # Add Distance constraint.
    dimension_name = 'Distance'
    routing.AddDimension(
        transit_callback_index,
        0,  # no slack
        3000,  # vehicle maximum travel distance
        True,  # start cumul to zero
        dimension_name)
    distance_dimension = routing.GetDimensionOrDie(dimension_name)
    distance_dimension.SetGlobalSpanCostCoefficient(100)

    initial_solution = routing.ReadAssignmentFromRoutes(data['initial_routes'],
                                                        True)
    print('Initial solution:')
    print_solution(datamanagerroutinginitial_solution)

    # Set default search parameters.
    search_parameters = pywrapcp.DefaultRoutingSearchParameters()

    # copy from start ends sample
    search_parameters.first_solution_strategy = (
        routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)

    # Solve the problem.
    solution = routing.SolveFromAssignmentWithParameters(
        initial_solutionsearch_parameters)

    # Print solution on console.
    if solution:
        print('Solution after search:')
        print_solution(datamanagerroutingsolution)


if __name__ == '__main__':
    main()

Sachin S

unread,
Aug 23, 2021, 6:50:28 AM8/23/21
to or-tools...@googlegroups.com
The goal is to optimize initial routes keeping same start nodes .. if I do not use starts, the solution might change start node for one or all vehicles. The vehicles are located at the start node, they have to start the route from those locations.

Sachin S

unread,
Aug 23, 2021, 7:01:31 AM8/23/21
to or-tools...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages