src/gsh.cpp
/**
 * This creates a matrix M of market and product prices, i.e. M[i][j] denotes a
 * price of product j in market i. If market does not provide a product than
 * the corresponding entry has value equal to "super price", i.e. a special
 * high value returned through out_super_price param.
 */
This creates a matrix M of market and product prices, i.e. M[i][j] denotes a
price of product j in market i. If market does not provide a product than
the corresponding entry has value equal to "super price", i.e. a special
high value returned through out_super_price param.
This creates a matrix M of market and product prices i . e . M [ i ] [ j ] denotes a price of product j in market i . If market does not provide a product than the corresponding entry has value equal to " super price " i . e . a special high value returned through out_super_price param .
vector<vector<int>> calc_market_product_prices(const TPP::Instance& instance,
        int *out_super_price = nullptr,
        bool filter_not_required_products = true) {
    vector<vector<int>> market_product_prices;
    market_product_prices.resize(instance.dimension_);
    auto market_id = 0u;
    auto max_price = 0;
    for (const auto &offers: instance.market_offers_) {
        auto &prices = market_product_prices.at(market_id);
        prices.resize(instance.product_count_, numeric_limits<int>::max());
        for (auto &offer: offers) {
            prices.at(static_cast<size_t>(offer.product_id_)) = offer.price_;
            max_price = max(max_price, offer.price_);
        }
        ++market_id;
    }
    int max_weight = 0;
    for (const auto &weights: instance.edge_weights_) {
        max_weight = max(max_weight,
                         *max_element(begin(weights), end(weights)));
    }
    const auto max_demand = *max_element(begin(instance.demands_),
                                         end(instance.demands_));
    const auto super_price = static_cast<int>(instance.dimension_ + instance.product_count_)
                           * max(max_weight, max_price)
                           * max_demand;
    assert(static_cast<int64_t>(numeric_limits<int32_t>::max()) > super_price
                         * static_cast<int64_t>(max_demand)
                         * static_cast<int>(instance.product_count_)
                         + static_cast<int>(instance.dimension_)
                         * max_weight );
    if (out_super_price) {
        *out_super_price = super_price;
    }
    if (filter_not_required_products) {
        for (auto &prices : market_product_prices) {
            vector<int> just_required;
            just_required.reserve(instance.needed_products_.size());
            for (auto p : instance.needed_products_) {
                just_required.push_back(prices.at(p));
            }
            prices = just_required;  
        }
    }
    LOG_F(INFO, "Max price: %d, max weight: %d, super price: %d",
            max_price, max_weight, super_price);
    for (auto &prices: market_product_prices) {
        replace(begin(prices), end(prices),
                numeric_limits<int>::max(), super_price); 
    }
    return market_product_prices;
}
vector < vector < int > > calc_market_product_prices ( const TPP :: Instance & instance , int * out_super_price = nullptr , bool filter_not_required_products = true ) { vector < vector < int > > market_product_prices ; market_product_prices . resize ( instance . dimension_ ) ; auto market_id = 0u ; auto max_price = 0 ; for ( const auto & offers : instance . market_offers_ ) { auto & prices = market_product_prices . at ( market_id ) ; prices . resize ( instance . product_count_ , numeric_limits < int > :: max ( ) ) ; for ( auto & offer : offers ) { prices . at ( static_cast < size_t > ( offer . product_id_ ) ) = offer . price_ ; max_price = max ( max_price , offer . price_ ) ; } ++ market_id ; } int max_weight = 0 ; for ( const auto & weights : instance . edge_weights_ ) { max_weight = max ( max_weight , * max_element ( begin ( weights ) , end ( weights ) ) ) ; } const auto max_demand = * max_element ( begin ( instance . demands_ ) , end ( instance . demands_ ) ) ; const auto super_price = static_cast < int > ( instance . dimension_ + instance . product_count_ ) * max ( max_weight , max_price ) * max_demand ; assert ( static_cast < int64_t > ( numeric_limits < int32_t > :: max ( ) ) > super_price * static_cast < int64_t > ( max_demand ) * static_cast < int > ( instance . product_count_ ) + static_cast < int > ( instance . dimension_ ) * max_weight ) ; if ( out_super_price ) { * out_super_price = super_price ; } if ( filter_not_required_products ) { for ( auto & prices : market_product_prices ) { vector < int > just_required ; just_required . reserve ( instance . needed_products_ . size ( ) ) ; for ( auto p : instance . needed_products_ ) { just_required . push_back ( prices . at ( p ) ) ; } prices = just_required ; } } LOG_F ( INFO , " " , max_price , max_weight , super_price ) ; for ( auto & prices : market_product_prices ) { replace ( begin ( prices ) , end ( prices ) , numeric_limits < int > :: max ( ) , super_price ) ; } return market_product_prices ; }
This creates a matrix M of market and product prices, i.e.
This creates a matrix M of market and product prices i . e .
// max val. will be replaced with a smaller value later // A price value that is >> max(price, edge weight) // super_price is used to denote that a market does not provide a product // It can be used in calculations more conveniently than // numeric_limits<int>::max() which would cause an overflow if added to a // non-zero value // Make sure that super_price will not cause overflow in later calculations // Remove columns for the products which are not needed, i.e. demand is 0 // replace only with required product prices // old -> super_price