src/tpp.cpp
/**
 * Returns cost of a TPP solution given in route and based on the data in
 * instance.
 *
 * The complexity  O(MK log K) where K is the number of markets and L the number of
 * products.
*/
Returns cost of a TPP solution given in route and based on the data in
instance.

The complexity  O(MK log K) where K is the number of markets and L the number of
products.
Returns cost of a TPP solution given in route and based on the data in instance . The complexity O ( MK log K ) where K is the number of markets and L the number of products .
int calc_solution_cost_capacitated(const Instance& instance,
                                 const vector<uint32_t> &route) {
    if (route.size() > instance.dimension_) {
        LOG_F(INFO, "route too long: %zu", route.size());
        return -1;
    }
    if (route.at(0) != 0) {
        LOG_F(INFO, "First node should be 0 (depot), not : %zu", route[0]);
        return -1;
    }
    vector<vector<ProductOffer>> product_offers;
    product_offers.resize(instance.product_count_);
    int total_distance = 0;
    auto prev = route.back();
    for (const auto market : route) {
        total_distance += instance.get_travel_cost(prev, market);
        for (const auto &offer : instance.market_offers_.at(market)) {
            auto id = offer.product_id_;
            if (instance.demands_.at(id) > 0) {
                product_offers.at(id).push_back(offer);
            }
        }
        prev = market;
    }
    for (auto &offers : product_offers) {
        sort(begin(offers), end(offers),
             [](const auto &o1, const auto &o2) {
                return o1.price_ < o2.price_;
             });
    }
    int purchase_cost = 0;
    for (auto i = 0ul, n = instance.demands_.size(); i < n; ++i) {
        int needed = instance.demands_[i];
        for (const auto &offer : product_offers.at(i)) {
            const auto bought = min(needed, offer.quantity_);
            needed -= bought;
            purchase_cost += offer.price_ * bought;
            if (needed == 0) {
                break ;
            }
        }
    }
    return total_distance + purchase_cost;
}
int calc_solution_cost_capacitated ( const Instance & instance , const vector < uint32_t > & route ) { if ( route . size ( ) > instance . dimension_ ) { LOG_F ( INFO , " " , route . size ( ) ) ; return -1 ; } if ( route . at ( 0 ) != 0 ) { LOG_F ( INFO , " " , route [ 0 ] ) ; return -1 ; } vector < vector < ProductOffer > > product_offers ; product_offers . resize ( instance . product_count_ ) ; int total_distance = 0 ; auto prev = route . back ( ) ; for ( const auto market : route ) { total_distance += instance . get_travel_cost ( prev , market ) ; for ( const auto & offer : instance . market_offers_ . at ( market ) ) { auto id = offer . product_id_ ; if ( instance . demands_ . at ( id ) > 0 ) { product_offers . at ( id ) . push_back ( offer ) ; } } prev = market ; } for ( auto & offers : product_offers ) { sort ( begin ( offers ) , end ( offers ) , [ ] ( const auto & o1 , const auto & o2 ) { return o1 . price_ < o2 . price_ ; } ) ; } int purchase_cost = 0 ; for ( auto i = 0ul , n = instance . demands_ . size ( ) ; i < n ; ++ i ) { int needed = instance . demands_ [ i ] ; for ( const auto & offer : product_offers . at ( i ) ) { const auto bought = min ( needed , offer . quantity_ ) ; needed -= bought ; purchase_cost += offer . price_ * bought ; if ( needed == 0 ) { break ; } } } return total_distance + purchase_cost ; }
Returns cost of a TPP solution given in route and based on the data in
 instance.
Returns cost of a TPP solution given in route and based on the data in instance .
// A sum of product quantities available at visitied markets