src/tpp.cpp
/**
 * Returns a list of nearest neighbors for each market (according to edge
 * weights). Each list has size equal to instance.dimension - 1.
 */
Returns a list of nearest neighbors for each market (according to edge
weights). Each list has size equal to instance.dimension - 1.
Returns a list of nearest neighbors for each market ( according to edge weights ) . Each list has size equal to instance . dimension - 1 .
vector<vector<uint32_t>> calc_nearest_neighbors(const TPP::Instance &instance) {
    CHECK_F(instance.dimension_ > 0);
    vector<vector<uint32_t>> nn_lists(instance.dimension_);
    uint32_t from = 0;
    for (auto i = 0u; i < instance.dimension_; ++i) {
        auto &nn_list = nn_lists.at(i);
        nn_list.reserve(instance.dimension_ - 1);
        for (auto j = 0u; j < instance.dimension_; ++j) {
            if (j != i) {  
                nn_list.push_back(j);
            }
        }
        auto cmp = [from=i, &instance]
                   (uint32_t m1, uint32_t m2) -> bool {
                       return instance.get_travel_cost(from, m1)
                            < instance.get_travel_cost(from, m2);
                   };
        sort(begin(nn_list), end(nn_list), cmp);
    }
    return nn_lists;
}
vector < vector < uint32_t > > calc_nearest_neighbors ( const TPP :: Instance & instance ) { CHECK_F ( instance . dimension_ > 0 ) ; vector < vector < uint32_t > > nn_lists ( instance . dimension_ ) ; uint32_t from = 0 ; for ( auto i = 0u ; i < instance . dimension_ ; ++ i ) { auto & nn_list = nn_lists . at ( i ) ; nn_list . reserve ( instance . dimension_ - 1 ) ; for ( auto j = 0u ; j < instance . dimension_ ; ++ j ) { if ( j != i ) { nn_list . push_back ( j ) ; } } auto cmp = [ from = i , & instance ] ( uint32_t m1 , uint32_t m2 ) -> bool { return instance . get_travel_cost ( from , m1 ) < instance . get_travel_cost ( from , m2 ) ; } ; sort ( begin ( nn_list ) , end ( nn_list ) , cmp ) ; } return nn_lists ; }
Returns a list of nearest neighbors for each market (according to edge
 weights).
Returns a list of nearest neighbors for each market ( according to edge weights ) .
// Do not add self to the nn list // Now we have the closest markets first in nn_list