src/tpp.cpp
/**
 * Returns cost of a TPP solution given in route and based on the data in
 * instance.
 *
 * This assumes that the instance is uncapacitated, i.e. a demand for each
 * product is at most 1.
 * The calculations are more efficient than for the capacitated version.
 *
 * The complexity  O(MK) 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.

This assumes that the instance is uncapacitated, i.e. a demand for each
product is at most 1.
The calculations are more efficient than for the capacitated version.

The complexity  O(MK) 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 . This assumes that the instance is uncapacitated i . e . a demand for each product is at most 1 . The calculations are more efficient than for the capacitated version . The complexity O ( MK ) where K is the number of markets and L the number of products .
int calc_solution_cost_uncapacitated(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;
    }
    CHECK_F(instance.is_capacitated_ == false,
            "Uncapacitated TPP instance required");
    vector<int> product_offers;
    product_offers.resize(instance.product_count_,
                          numeric_limits<int>::max());
    int total_distance = 0;
    auto prev = route.back();
    for (const auto node : route) {
        total_distance += instance.get_travel_cost(prev, node);
        for (const auto &offer : instance.market_offers_.at(node)) {
            const auto p = offer.product_id_;
            if (instance.demands_.at(p) > 0) {
                product_offers[p] = min(product_offers.at(p), offer.price_);
            }
        }
        prev = node;
    }
    int purchase_cost = 0;
    for (auto el : product_offers) {
        purchase_cost += el;
    }
    return total_distance + purchase_cost;
}
int calc_solution_cost_uncapacitated ( 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 ; } CHECK_F ( instance . is_capacitated_ == false , " " ) ; vector < int > product_offers ; product_offers . resize ( instance . product_count_ , numeric_limits < int > :: max ( ) ) ; int total_distance = 0 ; auto prev = route . back ( ) ; for ( const auto node : route ) { total_distance += instance . get_travel_cost ( prev , node ) ; for ( const auto & offer : instance . market_offers_ . at ( node ) ) { const auto p = offer . product_id_ ; if ( instance . demands_ . at ( p ) > 0 ) { product_offers [ p ] = min ( product_offers . at ( p ) , offer . price_ ) ; } } prev = node ; } int purchase_cost = 0 ; for ( auto el : product_offers ) { purchase_cost += el ; } 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 .
// Lowest product prices available at visitied markets