Using Capacity Constraint with Pickup and Delivery - python-3.x

Being new to OR-Tools libraries I am unable to modify the existing code for my requirements. I have a requirement to add capacity constraint with pickup and delivery i.e a person will deliver items items as mentioned in pickup and delivery algo, but there will be a constraint how much items he can accommodate with him. I tried using code for capacity constraint with pickup and delivery code, but didn't get success. Here is a sample code:
from __future__ import print_function
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
def create_data_model():
"""Stores the data for the problem."""
data = {}
data['distance_matrix'] = [
[
0, 548, 776, 696, 582, 274, 502, 194, 308, 194, 536, 502, 388, 354,
468, 776, 662
],
[
548, 0, 684, 308, 194, 502, 730, 354, 696, 742, 1084, 594, 480, 674,
1016, 868, 1210
],
[
776, 684, 0, 992, 878, 502, 274, 810, 468, 742, 400, 1278, 1164,
1130, 788, 1552, 754
],
[
696, 308, 992, 0, 114, 650, 878, 502, 844, 890, 1232, 514, 628, 822,
1164, 560, 1358
],
[
582, 194, 878, 114, 0, 536, 764, 388, 730, 776, 1118, 400, 514, 708,
1050, 674, 1244
],
[
274, 502, 502, 650, 536, 0, 228, 308, 194, 240, 582, 776, 662, 628,
514, 1050, 708
],
[
502, 730, 274, 878, 764, 228, 0, 536, 194, 468, 354, 1004, 890, 856,
514, 1278, 480
],
[
194, 354, 810, 502, 388, 308, 536, 0, 342, 388, 730, 468, 354, 320,
662, 742, 856
],
[
308, 696, 468, 844, 730, 194, 194, 342, 0, 274, 388, 810, 696, 662,
320, 1084, 514
],
[
194, 742, 742, 890, 776, 240, 468, 388, 274, 0, 342, 536, 422, 388,
274, 810, 468
],
[
536, 1084, 400, 1232, 1118, 582, 354, 730, 388, 342, 0, 878, 764,
730, 388, 1152, 354
],
[
502, 594, 1278, 514, 400, 776, 1004, 468, 810, 536, 878, 0, 114,
308, 650, 274, 844
],
[
388, 480, 1164, 628, 514, 662, 890, 354, 696, 422, 764, 114, 0, 194,
536, 388, 730
],
[
354, 674, 1130, 822, 708, 628, 856, 320, 662, 388, 730, 308, 194, 0,
342, 422, 536
],
[
468, 1016, 788, 1164, 1050, 514, 514, 662, 320, 274, 388, 650, 536,
342, 0, 764, 194
],
[
776, 868, 1552, 560, 674, 1050, 1278, 742, 1084, 810, 1152, 274,
388, 422, 764, 0, 798
],
[
662, 1210, 754, 1358, 1244, 708, 480, 856, 514, 468, 354, 844, 730,
536, 194, 798, 0
],
]
data['pickups_deliveries'] = [
[1, 6],
[2, 10],
[4, 3],
[5, 9],
[7, 8],
[15, 11],
[13, 12],
[16, 14],
]
data['num_vehicles'] = 4
data['depot'] = 0
data['vehicle_capacities'] = [15,15,15,15]
data['demands'] = [0, 1, 1, 3, 6, 3, 6, 8, 8, 1, 2, 1, 2, 6, 6, 8, 8]
return data
def print_solution(data, manager, routing, assignment):
"""Prints assignment on console."""
total_distance = 0
total_load = 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
route_load = 0
while not routing.IsEnd(index):
node_index = manager.IndexToNode(index)
route_load += data['demands'][node_index]
plan_output += ' {0} Load({1}) -> '.format(node_index, route_load)
previous_index = index
index = assignment.Value(routing.NextVar(index))
route_distance += routing.GetArcCostForVehicle(
previous_index, index, vehicle_id)
plan_output += ' {0} Load({1})\n'.format(manager.IndexToNode(index),
route_load)
plan_output += 'Distance of the route: {}m\n'.format(route_distance)
plan_output += 'Load of the route: {}\n'.format(route_load)
print(plan_output)
total_distance += route_distance
total_load += route_load
print('Total distance of all routes: {}m'.format(total_distance))
print('Total load of all routes: {}'.format(total_load))
def main():
"""Entry point of the program."""
# Instantiate the data problem.
data = create_data_model()
# Create the routing index manager.
manager = pywrapcp.RoutingIndexManager(len(data['distance_matrix']),
data['num_vehicles'], data['depot'])
# Create Routing Model.
routing = pywrapcp.RoutingModel(manager)
# Define cost of each arc.
def distance_callback(from_index, to_index):
"""Returns the manhattan 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)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# Add Capacity constraint.
def demand_callback(from_index):
"""Returns the demand of the node."""
# Convert from routing variable Index to demands NodeIndex.
from_node = manager.IndexToNode(from_index)
return data['demands'][from_node]
demand_callback_index = routing.RegisterUnaryTransitCallback(
demand_callback)
routing.AddDimensionWithVehicleCapacity(
demand_callback_index,
0, # null capacity slack
data['vehicle_capacities'], # vehicle maximum capacities
True, # start cumul to zero
'Capacity')
# 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)
# Define Transportation Requests.
for request in data['pickups_deliveries']:
pickup_index = manager.NodeToIndex(request[0])
delivery_index = manager.NodeToIndex(request[1])
routing.AddPickupAndDelivery (pickup_index, delivery_index)
routing.solver().Add(routing.VehicleVar(pickup_index) == routing.VehicleVar(delivery_index))
routing.solver().Add(distance_dimension.CumulVar(pickup_index) <= distance_dimension.CumulVar(delivery_index))
# Setting first solution heuristic.
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PARALLEL_CHEAPEST_INSERTION)
# Solve the problem.
assignment = routing.SolveWithParameters(search_parameters)
print(assignment)
# Print solution on console.
if assignment:
print("1")
print_solution(data, manager, routing, assignment)
if __name__ == '__main__':
main()

There is no solution because the total demand is greater than the total vehicle capacity. The demand is 70 the capacity is 60.

For anyone having trouble getting a solution to this problem, I'll just leave it here instead of creating a new question and writing the solution myself. There might be something in this answer you might've missed.
Lets first understand how capacity constraint works in plain English
What is the demands array?
It is a unit of quantity which the vehicle will have to carry upon going to that location.
What is the vehicle_capacities array?
It is the maximum quantity in units which a particular vehicle can carry.
Now moving on to pickup & delivery
In this case however, our 'vehicles' don't carry an additional weight/quantity (in units) upon reaching the delivery location. Instead, it will release the quantity (in units) which it carried from the pickup location.
So, our demands array will change accordingly.
Considering this is our pickup_deliveries array
data['pickups_deliveries'] = [
[1, 6],
[2, 10],
[4, 3],
[5, 9],
[7, 8],
[15, 11],
[13, 12],
[16, 14],
]
Our demands array should look something like:
data['demands'] = [0, 1, 2, -6, 6, 10, -1, 8, -8, -10, -2, -9, -4, 4, -13, 9, 13]
Where each delivery location will release that same amount of quantity which we picked up from the pickup location.
Side Note (Not related to question but might help you)
When using pickup and delivery with capacity constraint or combining it with any other constraint like time window or even multiple start end, it makes the job a whole lot easier if we first specify depots and then pickup1 then drop1, then pickup2 then drop2, etc.
Eg:
data['num_vehicles'] = 4
# put all vehicles at the start of your 'addresses' array (i.e. they will be the first rows in the distance / time matrices)
data['starts'] = [0, 1, 2, 3]
data['ends'] = [0, 1, 2, 3]
# then simply, from the next 2 indices start defining the pickups and drops
# i.e. 4 is pickup1 5 is drop1, 6 is pickup2 7 is drop2, etc. (this also makes it easier for dynamic input)
# i.e. if num_vehicles is 3 -> next 2 -> 3,4 (since index starts from 0)
data['pickups_deliveries'] = [
[4, 5],
[6, 7],
[8, 9],
[10, 11],
[12, 13],
[14, 15]
]
# then 0 to num_vehicle values will be 0 (since depots won't have weight in normal condition unless you have something...)
# and the rest of the numbers will be pairs of pickups and drop weights i.e. what is picked up is dropped so (num,-num) for 1 pickup-drop pair etc.
data['demands'] = [0, 0, 0, 0, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1]
data['vehicle_capacities'] = [1, 1, 1, 1] # this depends on your problem.
# here I have considered that a vehicle can only carry one thing at a time

Related

Loss is nan, stopping training

I have a program as in the attached file.
The error is
Loss is nan, stopping training
{'loss_classifier': tensor(nan, device='cuda:0', grad_fn=<NllLossBackward>), 'loss_box_reg': tensor(nan, device='cuda:0', grad_fn=<DivBackward0>), 'loss_mask': tensor(-2.1386e+22, device='cuda:0',
grad_fn=<BinaryCrossEntropyWithLogitsBackward>), 'loss_objectness': tensor(0.7630, device='cuda:0', grad_fn=<BinaryCrossEntropyWithLogitsBackward>), 'loss_rpn_box_reg': tensor(0.0572, device='cuda:0', grad_fn=<DivBackward0>)}
An exception has occurred, use %tb to see the full traceback.
I have suspect to data and printed all
labels, masks, boxes = self.readxml(annotation_path, img.size)
print(labels)
print(masks.shape)
print(boxes)
I don't see any error in data.
What could be wrong?
[3, 3, 3, 3, 3, 3, 3, 1]
(8, 576, 720)
[[101, 351, 124, 394], [96, 430, 126, 482], [110, 509, 141, 571], [472, 332, 515, 375], [541, 272, 585, 309], [487, 269, 529, 316], [379, 149, 418, 176], [441, 141, 508, 189]]
[3, 2, 3, 3, 3, 3, 1, 1]
(8, 576, 720)
[[164, 402, 221, 465], [260, 390, 322, 487], [328, 325, 372, 372], [312, 287, 374, 322], [318, 218, 339, 236], [180, 257, 225, 289], [400, 246, 534, 326], [487, 189, 529, 229]]
[1, 3, 3, 1, 3, 3, 3]
(7, 576, 720)
[[127, 353, 233, 570], [308, 239, 333, 272], [270, 223, 294, 253], [232, 200, 264, 247], [323, 214, 344, 234], [366, 278, 439, 311], [583, 275, 656, 326]]
[3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 3, 4]
(15, 576, 720)
[[240, 173, 289, 220], [83, 190, 127, 233], [158, 243, 213, 305], [79, 300, 109, 359], [58, 403, 101, 478], [297, 348, 354, 403], [301, 222, 347, 270], [402, 297, 448, 361], [455, 324, 506, 390], [482, 445, 546, 500], [486, 480, 517, 508], [529, 419, 600, 483], [561, 46, 604, 82], [559, 138, 603, 189], [187, 308, 210, 345]]
[3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 1, 3, 3, 3, 3, 1, 3]
(18, 576, 720)
[[273, 158, 321, 202], [232, 101, 278, 143], [278, 84, 323, 132], [179, 67, 221, 101], [131, 23, 174, 71], [495, 456, 566, 524], [486, 507, 547, 570], [302, 308, 358, 369], [268, 400, 290, 431], [325, 360, 383, 417], [206, 475, 268, 540], [465, 196, 556, 318], [451, 168, 493, 211], [520, 103, 558, 139], [586, 34, 622, 69], [568, 134, 609, 184], [650, 135, 688, 224], [298, 142, 346, 184]]
/home/centos/anaconda3/lib/python3.8/site-packages/torch/nn/functional.py:3103: UserWarning: The default behavior for interpolate/upsample with float scale_factor changed in 1.6.0 to align with other frameworks/libraries, and now uses scale_factor directly, instead of relying on the computed output size. If you wish to restore the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details.
warnings.warn("The default behavior for interpolate/upsample with float scale_factor changed "
[3, 3, 3, 4, 3, 3, 3, 2, 3, 2]
(10, 576, 720)
[[295, 462, 346, 514], [347, 402, 401, 461], [386, 414, 433, 459], [483, 443, 507, 480], [441, 370, 485, 414], [408, 359, 453, 402], [452, 301, 500, 353], [493, 306, 537, 361], [491, 465, 541, 532], [486, 145, 573, 208]]
[3, 4, 3, 3, 3, 4, 4]
(7, 576, 720)
[[193, 317, 242, 379], [262, 325, 290, 380], [319, 322, 370, 382], [518, 273, 597, 310], [313, 274, 384, 308], [477, 256, 489, 291], [458, 256, 474, 291]]
[3, 3, 3, 3, 2, 3, 3, 3, 4, 3, 3, 3, 2, 3, 3]
(15, 576, 720)
[[136, 109, 182, 151], [184, 129, 228, 178], [85, 65, 129, 115], [541, 122, 575, 172], [230, 157, 305, 233], [300, 214, 349, 270], [344, 265, 385, 328], [89, 200, 137, 240], [109, 244, 131, 274], [84, 309, 114, 371], [187, 322, 248, 380], [58, 401, 103, 474], [557, 421, 636, 507], [554, 149, 598, 206], [633, 60, 676, 107]]
[3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 2, 3, 3, 4, 4, 4, 3, 3, 3, 3]
(27, 576, 720)
[[598, 102, 642, 150], [570, 142, 608, 192], [131, 481, 199, 558], [84, 38, 121, 73], [125, 69, 167, 105], [191, 94, 259, 161], [152, 117, 199, 163], [211, 158, 262, 205], [299, 162, 347, 216], [295, 209, 346, 260], [333, 156, 379, 202], [378, 146, 430, 196], [399, 236, 450, 288], [442, 229, 495, 280], [352, 335, 413, 396], [287, 379, 316, 410], [363, 476, 429, 544], [483, 483, 565, 573], [514, 456, 591, 535], [519, 407, 591, 476], [490, 287, 522, 324], [585, 278, 618, 314], [642, 335, 675, 380], [434, 89, 478, 119], [526, 75, 574, 107], [340, 61, 391, 108], [129, 40, 172, 76]]
[3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 4]
(12, 576, 720)
[[95, 328, 149, 381], [15, 313, 92, 358], [211, 204, 266, 293], [138, 269, 203, 303], [363, 297, 420, 340], [303, 224, 328, 250], [273, 223, 298, 246], [530, 353, 597, 435], [514, 286, 555, 328], [461, 281, 496, 325], [491, 233, 517, 258], [428, 260, 443, 287]]
[3, 3, 3, 1, 2, 3, 3, 3, 3, 3, 1, 3, 3, 3, 3, 3]
(16, 576, 720)
[[124, 100, 168, 142], [293, 208, 345, 262], [176, 124, 224, 173], [143, 247, 210, 333], [79, 311, 115, 385], [301, 305, 366, 370], [335, 256, 381, 315], [317, 430, 374, 483], [467, 347, 518, 408], [503, 455, 578, 519], [437, 152, 495, 269], [582, 450, 646, 524], [23, 482, 86, 571], [556, 150, 600, 203], [592, 109, 633, 157], [632, 60, 676, 106]]
Epoch: [0] [ 0/303] eta: 0:09:51 lr: 0.000000 loss: -262.4095 (-262.4095) loss_classifier: 1.5747 (1.5747) loss_box_reg: 0.6368 (0.6368) loss_mask: -264.8073 (-264.8073) loss_objectness: 0.1623 (0.1623) loss_rpn_box_reg: 0.0240 (0.0240) time: 1.9534 data: 0.3372 max mem: 4226
[3, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
(13, 576, 720)
[[191, 126, 229, 149], [230, 130, 272, 155], [282, 127, 342, 168], [344, 161, 386, 186], [397, 164, 442, 198], [453, 179, 493, 207], [92, 205, 125, 257], [103, 348, 129, 392], [259, 488, 321, 547], [426, 380, 472, 425], [570, 382, 621, 433], [530, 250, 567, 285], [568, 265, 614, 302]]
[3, 3, 3, 3, 3, 3, 3, 3, 2, 4, 3, 4, 3, 4, 2, 3, 1, 3, 3, 3, 4]
(21, 576, 720)
[[325, 69, 371, 102], [133, 42, 171, 73], [100, 51, 136, 84], [138, 74, 181, 114], [195, 110, 237, 146], [265, 113, 310, 158], [193, 144, 238, 186], [246, 144, 290, 184], [329, 142, 382, 196], [300, 214, 320, 243], [331, 200, 385, 245], [385, 212, 405, 235], [162, 456, 230, 529], [372, 391, 401, 421], [425, 382, 495, 453], [503, 397, 565, 456], [555, 80, 646, 203], [520, 69, 563, 108], [637, 49, 682, 95], [549, 486, 626, 555], [374, 445, 398, 472]]
[3, 3, 1, 3, 3, 4, 3, 3, 3, 3, 3]
(11, 576, 720)
[[101, 387, 158, 451], [459, 303, 499, 351], [503, 242, 569, 333], [450, 245, 476, 275], [479, 220, 503, 246], [429, 257, 442, 291], [326, 234, 349, 260], [295, 227, 321, 250], [234, 277, 271, 315], [338, 211, 358, 231], [181, 256, 236, 291]]
[3, 3, 2, 3, 3, 2, 2, 4, 1, 3, 3, 3, 3, 3, 4]
(15, 576, 720)
[[109, 105, 145, 133], [174, 122, 211, 148], [237, 125, 275, 157], [286, 134, 328, 166], [351, 150, 393, 177], [412, 139, 455, 184], [447, 173, 495, 205], [495, 156, 514, 178], [570, 190, 641, 253], [534, 277, 580, 317], [483, 310, 531, 363], [474, 444, 532, 504], [329, 461, 385, 515], [109, 416, 138, 472], [83, 196, 99, 222]]
[3, 3, 3, 3, 1, 3, 3, 3]
(8, 576, 720)
[[100, 313, 127, 367], [98, 452, 124, 501], [264, 479, 321, 538], [397, 405, 446, 451], [484, 356, 634, 487], [499, 297, 549, 348], [569, 255, 618, 292], [490, 159, 538, 185]]
[3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3]
(11, 576, 720)
[[165, 367, 221, 423], [269, 340, 313, 395], [236, 289, 306, 332], [148, 290, 220, 327], [307, 275, 358, 314], [362, 281, 408, 315], [12, 306, 93, 349], [447, 250, 475, 295], [495, 251, 522, 292], [205, 261, 255, 290], [330, 208, 353, 226]]
[3, 3, 3, 3, 3, 3, 3, 1, 3, 4]
(10, 576, 720)
[[248, 336, 297, 388], [321, 319, 363, 361], [211, 312, 253, 357], [302, 250, 332, 288], [352, 468, 426, 550], [429, 329, 474, 381], [457, 277, 493, 326], [443, 220, 472, 275], [498, 235, 522, 259], [335, 237, 347, 259]]
[3, 3, 3, 3, 3, 3]
(6, 576, 720)
[[165, 427, 240, 505], [373, 449, 457, 528], [336, 371, 395, 438], [250, 363, 312, 431], [219, 271, 296, 319], [250, 237, 279, 267]]
[3, 3, 2, 1, 3, 3, 4, 4, 3, 3, 3, 4, 1, 3]
(14, 576, 720)
[[151, 118, 184, 142], [215, 126, 256, 154], [181, 108, 219, 135], [294, 122, 373, 173], [94, 270, 121, 316], [105, 370, 131, 416], [350, 457, 381, 489], [395, 410, 426, 444], [445, 356, 495, 403], [558, 263, 603, 299], [450, 179, 495, 206], [501, 180, 523, 203], [375, 127, 460, 186], [642, 235, 684, 265]]
[3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 2, 3, 1]
(14, 576, 720)
[[159, 388, 226, 460], [184, 342, 231, 394], [126, 290, 207, 329], [212, 285, 284, 333], [298, 277, 349, 313], [375, 274, 420, 309], [362, 250, 390, 280], [236, 237, 265, 270], [280, 229, 305, 257], [314, 204, 341, 242], [243, 196, 285, 236], [453, 236, 483, 290], [489, 250, 518, 286], [504, 191, 529, 233]]
[3, 3, 3, 3, 4, 4, 2, 3, 3, 3]
(10, 576, 720)
[[103, 363, 157, 422], [338, 341, 384, 391], [210, 259, 243, 296], [260, 248, 288, 282], [395, 288, 423, 336], [458, 269, 473, 305], [495, 342, 615, 499], [292, 230, 318, 262], [331, 214, 351, 232], [448, 239, 475, 270]]
[3, 3, 1, 3, 3, 3, 3, 4, 3, 1, 3]
(11, 576, 720)
[[184, 336, 232, 387], [237, 241, 268, 275], [242, 182, 276, 239], [333, 217, 359, 237], [360, 261, 386, 292], [405, 252, 435, 291], [445, 252, 474, 287], [527, 290, 552, 333], [468, 211, 490, 238], [503, 178, 532, 231], [483, 251, 521, 286]]
[3, 2, 4, 3, 3, 3, 3, 3, 4, 3, 3, 1, 3, 1]
(14, 576, 720)
[[269, 330, 314, 384], [201, 292, 246, 375], [354, 323, 375, 372], [272, 276, 311, 325], [325, 231, 351, 261], [288, 216, 308, 240], [259, 235, 286, 275], [227, 231, 255, 252], [315, 455, 354, 552], [588, 358, 647, 418], [510, 358, 570, 420], [415, 263, 480, 375], [510, 282, 548, 321], [500, 191, 533, 233]]
[3, 3, 4, 4, 1, 3, 3, 2, 3, 3, 3]
(11, 576, 720)
[[335, 311, 375, 372], [221, 254, 276, 288], [294, 236, 308, 261], [321, 241, 337, 273], [288, 191, 333, 230], [346, 208, 369, 226], [382, 287, 432, 333], [453, 326, 502, 394], [646, 269, 719, 312], [436, 258, 469, 291], [315, 277, 366, 316]]
[1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
(14, 576, 720)
[[280, 169, 401, 300], [607, 91, 647, 135], [565, 139, 606, 193], [173, 129, 219, 174], [522, 120, 555, 161], [452, 312, 500, 376], [330, 382, 392, 440], [178, 220, 230, 266], [94, 230, 148, 287], [66, 266, 99, 324], [70, 375, 108, 442], [469, 431, 542, 502], [601, 472, 687, 544], [639, 53, 679, 96]]
[3, 3, 1, 3, 3, 3, 3, 3, 4, 3]
(10, 576, 720)
[[432, 168, 476, 197], [479, 183, 523, 221], [262, 106, 352, 166], [90, 250, 114, 287], [87, 408, 116, 465], [329, 457, 380, 508], [492, 445, 541, 499], [440, 356, 481, 399], [569, 283, 590, 309], [526, 254, 568, 291]]
[2, 3, 3, 3, 3, 4, 3, 4, 3, 4, 3]
(11, 576, 720)
[[144, 421, 221, 556], [259, 480, 339, 572], [337, 352, 389, 415], [268, 267, 316, 302], [130, 268, 182, 301], [194, 253, 214, 288], [312, 222, 338, 242], [436, 266, 456, 300], [493, 257, 523, 289], [372, 259, 395, 298], [406, 251, 434, 299]]
[3, 3, 3, 3, 4, 3, 4, 4, 3, 3, 3, 3, 3, 3]
(14, 576, 720)
[[259, 340, 302, 400], [106, 355, 158, 412], [88, 289, 172, 330], [175, 284, 246, 332], [36, 284, 76, 335], [279, 238, 311, 266], [352, 288, 393, 326], [396, 275, 431, 315], [360, 261, 391, 294], [494, 274, 572, 313], [620, 262, 694, 306], [397, 218, 420, 243], [297, 206, 319, 226], [580, 398, 661, 482]]
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 3, 4, 3, 3]
(17, 576, 720)
[[273, 158, 321, 202], [337, 134, 382, 171], [278, 84, 323, 132], [179, 67, 221, 101], [131, 23, 174, 71], [495, 456, 566, 524], [486, 507, 547, 570], [239, 446, 295, 509], [316, 462, 380, 546], [325, 361, 383, 417], [381, 154, 489, 290], [446, 231, 491, 279], [598, 17, 632, 53], [653, 139, 676, 193], [487, 203, 503, 239], [298, 142, 346, 184], [677, 240, 720, 300]]
[3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 1, 1]
(13, 576, 720)
[[181, 356, 227, 413], [260, 337, 303, 386], [202, 295, 281, 334], [381, 297, 437, 334], [478, 281, 539, 314], [475, 232, 499, 258], [447, 254, 474, 289], [395, 258, 422, 292], [502, 181, 531, 233], [461, 198, 475, 220], [387, 480, 469, 574], [226, 207, 258, 257], [300, 192, 351, 228]]
[3, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, 3, 2, 3, 4]
(24, 576, 720)
[[293, 436, 347, 486], [328, 453, 375, 500], [342, 397, 391, 445], [370, 418, 422, 464], [399, 350, 450, 393], [421, 372, 470, 417], [450, 312, 489, 354], [470, 335, 512, 377], [100, 71, 141, 102], [127, 107, 162, 133], [205, 127, 248, 154], [225, 107, 262, 132], [73, 159, 96, 190], [89, 200, 112, 234], [87, 239, 109, 271], [85, 273, 110, 313], [85, 321, 113, 364], [90, 366, 104, 395], [638, 294, 666, 340], [585, 262, 613, 305], [586, 219, 629, 256], [497, 198, 546, 238], [391, 169, 427, 198], [524, 309, 550, 339]]
[1, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 1, 3]
(13, 576, 720)
[[274, 166, 395, 297], [83, 318, 112, 375], [539, 417, 612, 485], [469, 348, 530, 419], [609, 91, 647, 135], [565, 139, 606, 193], [636, 487, 718, 560], [139, 106, 185, 151], [55, 420, 107, 495], [458, 472, 539, 559], [281, 391, 346, 462], [235, 285, 352, 398], [639, 53, 679, 96]]
[3, 3, 2, 3, 1, 1, 3, 3, 3, 3, 1]
(11, 576, 720)
[[265, 415, 327, 487], [329, 327, 375, 392], [267, 268, 350, 338], [178, 257, 231, 288], [222, 180, 258, 254], [349, 183, 383, 221], [404, 312, 459, 363], [533, 303, 577, 350], [435, 266, 508, 311], [623, 272, 691, 320], [396, 224, 436, 288]]
[3, 3, 3, 3, 1, 1, 3]
(7, 576, 720)
[[246, 357, 301, 431], [106, 338, 153, 388], [212, 282, 278, 325], [228, 250, 268, 280], [271, 209, 319, 262], [440, 203, 482, 257], [445, 259, 471, 295]]
[1, 3, 2, 3, 3, 3, 3, 2, 3, 3, 3, 2, 3]
(13, 576, 720)
[[152, 65, 236, 114], [270, 120, 311, 145], [336, 127, 380, 174], [435, 184, 475, 210], [504, 266, 544, 302], [554, 270, 592, 309], [484, 320, 529, 361], [428, 357, 477, 414], [378, 366, 427, 410], [366, 421, 416, 467], [264, 466, 314, 513], [86, 330, 116, 391], [601, 225, 643, 254]]
[4, 3, 3, 3, 2, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 2, 3, 3, 2, 3, 3, 3, 3, 3, 3]
(31, 576, 720)
[[45, 62, 59, 82], [150, 87, 188, 118], [212, 105, 252, 129], [157, 141, 202, 166], [210, 179, 256, 214], [268, 164, 315, 192], [284, 143, 327, 172], [317, 125, 361, 155], [361, 164, 397, 190], [375, 127, 416, 153], [421, 149, 464, 182], [414, 177, 455, 204], [299, 208, 346, 240], [382, 211, 427, 240], [431, 245, 489, 274], [505, 232, 559, 271], [518, 202, 559, 236], [549, 195, 594, 224], [504, 158, 550, 197], [587, 174, 631, 200], [604, 267, 657, 299], [470, 319, 518, 376], [432, 328, 473, 373], [432, 372, 477, 414], [385, 349, 434, 408], [384, 409, 432, 456], [326, 410, 382, 459], [309, 470, 363, 522], [207, 487, 274, 554], [84, 204, 108, 237], [467, 188, 509, 217]]
[3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 4, 3, 3]
(13, 576, 720)
[[167, 394, 235, 496], [372, 481, 453, 570], [332, 342, 382, 403], [257, 351, 297, 406], [238, 264, 278, 299], [328, 279, 407, 312], [386, 303, 460, 354], [473, 275, 550, 312], [291, 215, 317, 234], [326, 209, 346, 233], [440, 262, 451, 295], [399, 218, 421, 241], [467, 214, 486, 237]]
[3, 4, 1, 3]
(4, 576, 720)
[[404, 247, 427, 276], [458, 258, 472, 290], [482, 197, 518, 241], [487, 255, 513, 287]]
[3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
(13, 576, 720)
[[129, 353, 180, 409], [41, 402, 115, 507], [271, 305, 305, 348], [232, 282, 263, 323], [162, 254, 219, 293], [297, 260, 325, 289], [332, 228, 354, 251], [364, 467, 444, 551], [496, 365, 560, 443], [600, 354, 671, 422], [454, 268, 485, 303], [486, 251, 512, 284], [477, 204, 493, 225]]
[3, 4, 3, 4, 3, 1, 3, 1, 3, 3, 3, 2, 3, 3]
(14, 576, 720)
[[250, 476, 325, 568], [308, 311, 324, 357], [164, 290, 235, 326], [56, 285, 90, 329], [156, 261, 209, 292], [222, 215, 271, 266], [291, 231, 315, 263], [322, 199, 354, 230], [447, 280, 481, 336], [496, 322, 540, 371], [546, 380, 617, 447], [473, 228, 508, 260], [405, 214, 426, 243], [331, 311, 370, 356]]
[2, 3, 3, 3, 3, 3, 3, 3, 3]
(9, 576, 720)
[[336, 382, 396, 477], [268, 294, 303, 331], [244, 272, 275, 305], [286, 242, 315, 270], [342, 219, 362, 243], [550, 371, 621, 450], [501, 333, 551, 391], [529, 304, 573, 353], [495, 254, 525, 301]]
[3, 3, 1, 1, 3, 4, 3, 3, 2]
(9, 576, 720)
[[130, 104, 164, 127], [175, 121, 212, 146], [294, 123, 374, 171], [377, 126, 458, 185], [450, 176, 492, 206], [621, 216, 644, 241], [570, 255, 615, 289], [456, 323, 493, 360], [107, 447, 137, 517]]
[3, 3, 3, 3, 3, 1, 3, 2, 4, 3, 1, 4, 3, 1]
(14, 576, 720)
[[117, 337, 168, 390], [172, 357, 223, 415], [154, 424, 218, 539], [259, 405, 320, 475], [339, 342, 382, 401], [398, 213, 448, 324], [355, 261, 387, 297], [268, 239, 295, 276], [248, 253, 263, 283], [312, 223, 338, 245], [488, 202, 534, 298], [467, 279, 483, 312], [284, 220, 306, 239], [238, 180, 277, 237]]
[3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
(12, 576, 720)
[[299, 219, 349, 272], [540, 124, 584, 174], [189, 136, 238, 185], [578, 87, 615, 128], [623, 27, 658, 71], [79, 71, 119, 107], [132, 104, 174, 145], [95, 299, 127, 360], [59, 426, 102, 494], [484, 149, 527, 196], [573, 133, 617, 185], [654, 78, 686, 140]]
[3, 3, 3, 3, 3, 1, 1, 4, 4, 3, 3, 3, 3, 3]
(14, 576, 720)
[[162, 418, 225, 495], [180, 357, 227, 412], [258, 337, 302, 385], [210, 251, 255, 284], [260, 237, 292, 268], [243, 197, 278, 238], [501, 183, 530, 233], [480, 216, 491, 239], [403, 217, 414, 241], [490, 256, 521, 289], [449, 253, 474, 289], [399, 261, 427, 292], [524, 292, 577, 336], [654, 277, 715, 314]]
[3, 3, 4, 3, 3, 2, 3, 3, 3, 3, 3, 3, 4]
(13, 576, 720)
[[255, 446, 322, 525], [377, 429, 455, 530], [321, 383, 347, 455], [250, 268, 282, 299], [111, 266, 168, 300], [274, 238, 302, 274], [314, 229, 336, 251], [351, 206, 371, 223], [469, 270, 512, 337], [500, 321, 559, 395], [556, 326, 607, 390], [637, 277, 713, 319], [454, 219, 463, 238]]
[4, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 3, 1, 3, 3, 3, 3, 2, 3, 3, 3, 3, 1]
(28, 576, 720)
[[237, 69, 256, 96], [259, 76, 305, 121], [314, 95, 377, 165], [268, 120, 311, 160], [253, 182, 304, 230], [281, 166, 336, 214], [317, 151, 361, 196], [508, 454, 588, 532], [204, 150, 249, 197], [106, 19, 145, 56], [119, 66, 158, 101], [280, 54, 322, 87], [110, 210, 155, 246], [273, 414, 329, 478], [319, 253, 382, 318], [462, 205, 512, 260], [411, 88, 462, 121], [477, 57, 542, 112], [614, 6, 647, 41], [585, 44, 619, 78], [541, 75, 579, 125], [579, 82, 621, 131], [528, 130, 574, 182], [557, 150, 599, 203], [662, 193, 700, 256], [617, 40, 652, 78], [632, 62, 672, 106], [384, 253, 543, 389]]
[3, 4, 3, 3, 3, 3, 3, 3]
(8, 576, 720)
[[88, 201, 116, 229], [106, 258, 120, 288], [95, 331, 125, 377], [106, 429, 135, 483], [339, 465, 390, 517], [475, 334, 520, 379], [543, 282, 582, 315], [445, 176, 487, 205]]
[3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 3]
(15, 576, 720)
[[175, 89, 217, 120], [229, 107, 258, 130], [264, 117, 299, 139], [307, 120, 346, 151], [364, 166, 405, 192], [431, 180, 473, 208], [507, 157, 547, 181], [60, 68, 95, 91], [90, 241, 113, 279], [87, 302, 114, 344], [85, 437, 114, 495], [351, 409, 395, 458], [446, 334, 494, 389], [520, 280, 569, 331], [617, 247, 665, 280]]
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3]
(15, 576, 720)
[[393, 165, 430, 198], [342, 162, 380, 189], [270, 124, 308, 157], [233, 108, 270, 134], [294, 151, 334, 176], [196, 113, 233, 139], [127, 98, 163, 123], [99, 77, 133, 104], [411, 377, 463, 425], [507, 397, 576, 470], [538, 276, 583, 313], [80, 178, 105, 209], [97, 225, 110, 251], [85, 268, 108, 308], [323, 454, 378, 504]]
[3, 2, 3, 3, 3, 3, 1, 4, 3, 3, 3, 3]
(12, 576, 720)
[[299, 219, 349, 272], [540, 124, 584, 174], [579, 85, 618, 126], [622, 27, 658, 72], [146, 107, 194, 154], [189, 230, 240, 273], [95, 230, 225, 370], [73, 284, 88, 318], [70, 340, 105, 409], [331, 386, 399, 451], [451, 328, 507, 393], [558, 436, 633, 507]]
Loss is nan, stopping training
{'loss_classifier': tensor(nan, device='cuda:0', grad_fn=<NllLossBackward>), 'loss_box_reg': tensor(nan, device='cuda:0', grad_fn=<DivBackward0>), 'loss_mask': tensor(-2.1386e+22, device='cuda:0',
grad_fn=<BinaryCrossEntropyWithLogitsBackward>), 'loss_objectness': tensor(0.7630, device='cuda:0', grad_fn=<BinaryCrossEntropyWithLogitsBackward>), 'loss_rpn_box_reg': tensor(0.0572, device='cuda:0', grad_fn=<DivBackward0>)}
An exception has occurred, use %tb to see the full traceback.
SystemExit: 1
/home/centos/anaconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py:3435: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

ortools vrp does not give me any solution

I want solve a vehicle routing problem with ORTools, both distance and duration matrix will be used.
but the problem is when I change the matrix , it wouldn't give me any solutions anymore!
there are 2 groups of matrixes. with the commented matrixes there is solution, but with the other group, there is not. do you have any idea why this is happening:
from __future__ import print_function
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
def create_data_model():
"""Stores the data for the problem."""
data = {}
#data['distance_matrix']=[[0, 329, 146, 157, 318, 528, 457, 242, 491, 335, 471, 456, 391, 128, 461, 555, 460], [329, 0, 399, 384, 544, 493, 339, 378, 108, 243, 125, 394, 136, 561, 505, 315, 447], [146, 399, 0, 262, 471, 316, 297, 227, 548, 377, 267, 430, 383, 154, 234, 188, 400], [157, 384, 262, 0, 440, 271, 383, 525, 223, 367, 511, 354, 112, 539, 159, 152, 373], [318, 544, 471, 440, 0, 423, 112, 381, 346, 512, 161, 239, 581, 291, 284, 145, 143], [528, 493, 316, 271, 423, 0, 380, 196, 409, 212, 199, 277, 387, 515, 391, 261, 318], [457, 339, 297, 383, 112, 380, 0, 379, 298, 267, 482, 247, 462, 256, 296, 533, 200], [242, 378, 227, 525, 381, 196, 379, 0, 156, 230, 551, 555, 338, 372, 403, 358, 506], [491, 108, 548, 223, 346, 409, 298, 156, 0, 140, 532, 405, 531, 129, 220, 482, 222], [335, 243, 377, 367, 512, 212, 267, 230, 140, 0, 418, 440, 526, 255, 455, 296, 430], [471, 125, 267, 511, 161, 199, 482, 551, 532, 418, 0, 439, 285, 181, 254, 208, 304], [456, 394, 430, 354, 239, 277, 247, 555, 405, 440, 439, 0, 397, 229, 121, 385, 147], [391, 136, 383, 112, 581, 387, 462, 338, 531, 526, 285, 397, 0, 544, 205, 197, 226], [128, 561, 154, 539, 291, 515, 256, 372, 129, 255, 181, 229, 544, 0, 150, 204, 516], [461, 505, 234, 159, 284, 391, 296, 403, 220, 455, 254, 121, 205, 150, 0, 192, 544], [555, 315, 188, 152, 145, 261, 533, 358, 482, 296, 208, 385, 197, 204, 192, 0, 138], [460, 447, 400, 373, 143, 318, 200, 506, 222, 430, 304, 147, 226, 516, 544, 138, 0]]
data['distance_matrix']=[[0, 228, 299, 301, 235, 208, 405, 447, 144, 579], [228, 0, 343, 288, 357, 426, 530, 510, 122, 490], [299, 343, 0, 236, 228, 523, 274, 377, 397, 530], [301, 288, 236, 0, 594, 523, 289, 397, 154, 380], [235, 357, 228, 594, 0, 558, 370, 444, 173, 558], [208, 426, 523, 523, 558, 0, 219, 278, 504, 507], [405, 530, 274, 289, 370, 219, 0, 195, 283, 257], [447, 510, 377, 397, 444, 278, 195, 0, 407, 417], [144, 122, 397, 154, 173, 504, 283, 407, 0, 273], [579, 490, 530, 380, 558, 507, 257, 417, 273, 0]]
data['time_matrix']=[[0, 205, 519, 308, 428, 574, 399, 138, 573, 541], [205, 0, 447, 578, 296, 536, 135, 345, 198, 315], [519, 447, 0, 209, 438, 174, 231, 382, 104, 522], [308, 578, 209, 0, 235, 264, 492, 305, 134, 538], [428, 296, 438, 235, 0, 600, 177, 435, 204, 556], [574, 536, 174, 264, 600, 0, 476, 119, 183, 476], [399, 135, 231, 492, 177, 476, 0, 497, 208, 167], [138, 345, 382, 305, 435, 119, 497, 0, 344, 454], [573, 198, 104, 134, 204, 183, 208, 344, 0, 422], [541, 315, 522, 538, 556, 476, 167, 454, 422, 0]]
data['cost_matrix']=[[0, 160, 135, 433, 581, 453, 336, 329, 343, 237], [160, 0, 313, 596, 576, 458, 264, 380, 348, 354], [135, 313, 0, 591, 391, 211, 561, 236, 304, 414], [433, 596, 591, 0, 539, 253, 427, 300, 214, 118], [581, 576, 391, 539, 0, 243, 521, 499, 560, 255], [453, 458, 211, 253, 243, 0, 571, 216, 121, 314], [336, 264, 561, 427, 521, 571, 0, 425, 271, 165], [329, 380, 236, 300, 499, 216, 425, 0, 425, 549], [343, 348, 304, 214, 560, 121, 271, 425, 0, 176], [237, 354, 414, 118, 255, 314, 165, 549, 176, 0]]
data['num_vehicles'] = 4
data['depot'] = 0
return data
def print_solution(data, manager, routing, assignment):
"""Prints assignment on console."""
total_cost ,total_distance,total_time= 0,0,0
print('Objective: {}'.format(assignment.ObjectiveValue()))
distance_dimension=routing.GetDimensionOrDie('Distance')
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)
route_cost = 0
route_distance = 0
route_time = 0
while not routing.IsEnd(index):
plan_output += ' {} -> '.format(manager.IndexToNode(index))
distance_var=distance_dimension.CumulVar(index)
time_var=time_dimension.CumulVar(index)
previous_index = index
index = assignment.Value(routing.NextVar(index))
route_cost += routing.GetArcCostForVehicle(previous_index, index, vehicle_id)
route_distance+=assignment.Value(distance_var)
route_time+=assignment.Value(time_var)
plan_output += '{}\n'.format(manager.IndexToNode(index))
plan_output += 'Cost of the route: {0}\nDistance of the route: {1}m\nTime of route: {2}\n'.format(
route_cost,
route_distance,
route_time)
print(plan_output)
total_cost += route_cost
total_time+=route_time
total_distance+=route_distance
print('Total Cost of all routes: {}\nTotal Distance of all routes: {}\nTotal Time of all routes: {}\n'.format(total_cost,total_distance,total_time))
def get_routes(manager, routing, solution, num_routes):
"""Get vehicle routes from a solution and store them in an array."""
# Get vehicle routes and store them in a two dimensional array whose
# i,j entry is the jth location visited by vehicle i along its route.
routes = []
for route_nbr in range(num_routes):
index = routing.Start(route_nbr)
route = [manager.IndexToNode(index)]
while not routing.IsEnd(index):
index = solution.Value(routing.NextVar(index))
route.append(manager.IndexToNode(index))
routes.append(route)
return routes
def main():
# Instantiate the data problem.
data = create_data_model()
# Create the routing index manager.
manager = pywrapcp.RoutingIndexManager(len(data['cost_matrix']), data['num_vehicles'], data['depot'])
# Create Routing Model.
routing = pywrapcp.RoutingModel(manager)
# Create and register a transit callback.
def cost_callback(from_index, to_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['cost_matrix'][from_node][to_node]
transit_callback_index = routing.RegisterTransitCallback(cost_callback)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# Add Cost constraint.
routing.AddDimension(
transit_callback_index,
0, # no slack
3000, # vehicle maximum travel distance
False, # start cumul to zero
'Cost')
cost_dimension = routing.GetDimensionOrDie('Cost')
cost_dimension.SetGlobalSpanCostCoefficient(1000)
#Add Distance constraint.
def distance_callback(from_index,to_index):
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['distance_matrix'][from_node][to_node]
distance_callback_index=routing.RegisterTransitCallback(distance_callback)
routing.AddDimension(
distance_callback_index,
0,
3000,
False,
'Distance')
distance_dimension=routing.GetDimensionOrDie('Distance')
#Add Time constraint.
def time_callback(from_index,to_index):
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['time_matrix'][from_node][to_node]
time_callback_index=routing.RegisterTransitCallback(time_callback)
routing.AddDimension(
time_callback_index,
0,
300,
False,
'Time')
time_dimension=routing.GetDimensionOrDie('Time')
# Setting first solution heuristic.
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
search_parameters.solution_limit = 100
search_parameters.time_limit.seconds = 3
# Solve the problem.
assignment = routing.SolveWithParameters(search_parameters)
# Print solution on console.
if assignment:
print_solution(data, manager, routing, assignment)
routes = get_routes(manager, routing, assignment, data['num_vehicles'])
# Display the routes.
for i, route in enumerate(routes):
print('Route', i, route)
if __name__ == '__main__':
main()
None could mean that it did not find a solution. Most likely your limits are too low
by increasing the limits, it works fine.
but for better understanding, it's better to check solver status. time limit errors mostly refer to the low limitations.
in this example we have many value more than 300 in time matrix but the maximum time for every vehicle is 300.so there is not a feasible solution for this problem.

conversion of string to tuple in python

I have a tuple (h) as follows:
(array([[145, 34, 26, 18, 90, 89],
[ 86, 141, 216, 167, 67, 214],
[ 18, 0, 212, 49, 232, 34],
...,
[147, 99, 73, 110, 108, 9],
[222, 133, 231, 48, 227, 154],
[184, 133, 169, 201, 162, 168]], dtype=uint8), array([[178, 58, 24, 90],
[ 3, 31, 129, 243],
[ 48, 92, 19, 108],
...,
[148, 21, 25, 209],
[189, 114, 46, 218],
[ 15, 43, 92, 61]], dtype=uint8), array([[ 17, 254, 216, ..., 126, 74, 129],
[231, 168, 214, ..., 131, 50, 107],
[ 77, 185, 229, ..., 86, 167, 61],
...,
[105, 240, 95, ..., 230, 158, 27],
[211, 46, 193, ..., 48, 57, 79],
[136, 126, 235, ..., 109, 33, 185]], dtype=uint8))
I converted it into a string s = str(h):
'(array([[ 1, 60, 249, 162, 51, 3],\n [ 57, 76, 193, 244, 17, 238],\n [ 22, 72, 101, 229, 185, 124],\n ...,\n [132, 243, 123, 192, 152, 107],\n [163, 187, 131, 47, 253, 155],\n [ 21, 3, 77, 208, 229, 15]], dtype=uint8), array([[119, 149, 215, 129],\n [146, 71, 121, 79],\n [114, 148, 121, 140],\n ...,\n [175, 121, 81, 71],\n [178, 92, 1, 99],\n [ 80, 122, 189, 209]], dtype=uint8), array([[ 26, 122, 248, ..., 104, 167, 29],\n [ 41, 213, 250, ..., 82, 71, 211],\n [ 20, 122, 4, ..., 152, 99, 121],\n ...,\n [133, 77, 84, ..., 238, 243, 240],\n [208, 183, 187, ..., 182, 51, 116],\n [ 19, 135, 48, ..., 210, 163, 58]], dtype=uint8))'
Now, I want to convert s back to a tuple. I tried using ast.literal_eval(s), but I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/ast.py", line 84, in literal_eval
return _convert(node_or_string)
File "/usr/lib/python3.5/ast.py", line 55, in _convert
return tuple(map(_convert, node.elts))
File "/usr/lib/python3.5/ast.py", line 83, in _convert
raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: <_ast.Call object at 0x76a6f770>
I could not find this exact solution anywhere. It would be great if someone could help me out.
You can't use str() on numpy arrays (wrapped in tuples or otherwise) and hope to recover the data.
First of all, the ast.literal_eval() function only supports literals and literal displays, not numpy array(...) syntax.
Next, str() on a tuple produces debugging-friendly output; tuples don't implement a __str__ string conversion hook, so their repr() representation is returned instead. Numpy arrays do support str() conversion, but their output is still but a friendly-looking string that omits a lot of detail from the actual values. In your example, those ... ellipsis dots indicate that there is more data in that part of the array, but the strings do not include those values. So you are losing data if you were to try to re-create your arrays from this.
If you need to store these tuples in a file or database column, or need to transmit them over a network connection, you need to serialise the data. Proper serialisation will preserve every detail of the arrays.
For tuples with numpy arrays, you can use pickle.dumps() to produce a bytes object that can be passed back to pickles.loads() to recreate the same value.
You can also convert invidual numpy arrays to a numpy-specific binary format, and load that format again, with the numpy.save() and numpy.load() functions (which operate directly on files, but you can pass in io.BytesIO() objects).

stateful autoencoder in Keras

I'm trying to create a stateful autoencoder model. The goal is to make the autoencoder stateful for each timeseries. The data consists of 10 timeseries and each timeseries has 567 length.
timeseries#1: 451, 318, 404, 199, 225, 158, 357, 298, 339, 155, 135, 239, 306, ....
timeseries#2: 304, 274, 150, 143, 391, 357, 278, 557, 98, 106, 305, 288, 325, ....
...
timeseries#10: 208, 138, 201, 342, 280, 282, 280, 140, 124, 261, 193, .....
My lookback windeow is 28. So I generated the following sequences with 28 timesteps:
[451, 318, 404, 199, 225, 158, 357, 298, 339, 155, 135, 239, 306, .... ]
[318, 404, 199, 225, 158, 357, 298, 339, 155, 135, 239, 306, 56, ....]
[404, 199, 225, 158, 357, 298, 339, 155, 135, 239, 306, 56, 890, ....]
...
[304, 274, 150, 143, 391, 357, 278, 557, 98, 106, 305, 288, 325, ....]
[274, 150, 143, 391, 357, 278, 557, 98, 106, 305, 288, 325, 127, ....]
[150, 143, 391, 357, 278, 557, 98, 106, 305, 288, 325, 127, 798, ....]
...
[208, 138, 201, 342, 280, 282, 280, 140, 124, 261, 193, .....]
[138, 201, 342, 280, 282, 280, 140, 124, 261, 193, 854, .....]
That gives me 539 sequences for each timeseries. What I need to do is to make the LSTMs to be stateful for each of the timeseries and reset the state after seeing all the sequences from a timeseries. Here is the code I have:
batch_size = 35 #(total Number of samples is 5390, and it is dividable by 35)
timesteps = 28
n_features = 1
hunits = 14
RepeatVector(timesteps/hunits = 2)
epochs = 1000
inputEncoder = Input(batch_shape=(35, 28, 1), name='inputEncoder')
outEncoder, c, h = LSTM(14, stateful=True, return_state=True, name='outputEncoder')(inputEncoder)
encoder_model = Model(inputEncoder, outEncoder)
context = RepeatVector(2, name='inputDecoder')(outEncoder)
context_reshaped = Reshape(28, 1), name='ReshapeLayer')(context)
outDecoder = LSTM(1, return_sequences=True, stateful=True, name='decoderLSTM')(context_reshaped)
autoencoder = Model(inputEncoder, outDecoder)
autoencoder.compile(loss='mse', optimizer='rmsprop')
for i in range(epochs):
history = autoencoder.fit(data, data,
validation_split=config['validation_split_ratio'],
shuffle=False,
batch_size=35,
epochs=1,
)
autoencoder.reset_states()
2 questions:
1- I'm getting this error after the first epoch is finished, I wonder how it is happening:
ValueError: Cannot feed value of shape (6, 28, 1) for Tensor u'inputEncoder:0', which has shape '(35, 28, 1)'
2- I don't think that model works as I want. Here it will reset the states after all batches (one epoch) which means after all timeseries are processed. How should I change it to be stateful between timeseries?
The issue is from the validation_split rate!! It is set to a 0.33% and when the splits happens it tries to train on 3611 data samples which is not divisible by my batch_size=35 . Based on this post I could find the proper number, copying from that post:
def quantize_validation_split(validation_split, sample_count, batch_size):
batch_count = sample_count / batch_size
return float(int(batch_count * validation_split)) / batch_count
then you can call model.fit(...,
validation_split=fix_validation_split(0.05, len(X), batch_size)). but
it would be cool if keras did this for you inside fit().
Also, regarding make the autoencoder stateful the way I need: there shouldn't be a reset_state at the end of each epoch!

Palindrome in Python not working

I am trying to make a simple program that displays the palindrome numbers between 2 numbers in Python3 but it doesn't seem to work. I get only 4 output which are 1,2,4 and 8. What about the other numbers like 11, 22, 33, ..., 111, 121,131,..., 191, 222, etc?
Here is my code. I can't figure out why it's not working.
a = 0
b = 500
a += 1
for i in range(a,b):
if(str(a) == str(a)[::-1]):
print(a)
a += a
Maybe
for i in range (0, 500):
if str(i) == str(i)[::-1]:
print(i)
>>> palindromes = [a for a in range(500) if str(a) == str(a)[::-1]]
>>> palindromes
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131,
141, 151, 161, 171, 181, 191, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292, 303,
313, 323, 333, 343, 353, 363, 373, 383, 393, 404, 414, 424, 434, 444, 454, 464, 474,
484, 494]
That shows you the principle. If you want to do it in a function, you can use yield instead of constructing the whole list in memory (much more efficient):
>>> def palindromes(a, b):
... """Return palindromes in closed interval from a to b"""
... for i in range(a, b):
... if str(i) == str(i)[::-1]:
... yield i
...
>>> list(palindromes(0, 500))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292, 303, 313, 323, 333, 343, 353, 363, 373, 383, 393, 404, 414, 424, 434, 444, 454, 464, 474, 484, 494]

Resources