(static, tour_indices)
return tour_len.sum(1)
Euclidean distance between all cities / nodes given by tour_indices
Euclidean distance between all cities / nodes given by tour_indices
Euclidean distance between all cities / nodes given by tour_indices
def reward(static, tour_indices):
    """
    Euclidean distance between all cities / nodes given by tour_indices
    """

    # Convert the indices back into a tour
    idx = tour_indices.unsqueeze(1).expand(-1, static.size(1), -1)
    tour = torch.gather(static.data, 2, idx).permute(0, 2, 1)

    # Ensure we're always returning to the depot - note the extra concat
    # won't add any extra loss, as the euclidean distance between consecutive
    # points is 0
    start = static.data[:, :, 0].unsqueeze(1)
    y = torch.cat((start, tour, start), dim=1)

    # Euclidean distance between each consecutive point
    tour_len = torch.sqrt(torch.sum(torch.pow(y[:, :-1] - y[:, 1:], 2), dim=2))

    return tour_len.sum(1)
def reward ( static , tour_indices ) : # Convert the indices back into a tour idx = tour_indices . unsqueeze ( 1 ) . expand ( - 1 , static . size ( 1 ) , - 1 ) tour = torch . gather ( static . data , 2 , idx ) . permute ( 0 , 2 , 1 ) # Ensure we're always returning to the depot - note the extra concat # won't add any extra loss, as the euclidean distance between consecutive # points is 0 start = static . data [ : , : , 0 ] . unsqueeze ( 1 ) y = torch . cat ( ( start , tour , start ) , dim = 1 ) # Euclidean distance between each consecutive point tour_len = torch . sqrt ( torch . sum ( torch . pow ( y [ : , : - 1 ] - y [ : , 1 : ] , 2 ) , dim = 2 ) ) return tour_len . sum ( 1 )