15 #ifndef PAAL_XOR_BIDS_HPP
16 #define PAAL_XOR_BIDS_HPP
24 #include <boost/algorithm/cxx11/all_of.hpp>
25 #include <boost/concept_check.hpp>
26 #include <boost/concept/requires.hpp>
27 #include <boost/optional/optional.hpp>
28 #include <boost/range/adaptor/filtered.hpp>
29 #include <boost/range/algorithm/copy.hpp>
30 #include <boost/range/iterator.hpp>
33 #include <type_traits>
38 template<
class GetB
ids,
class GetValue,
class GetItems,
class Gamma>
61 GetCopiesNum get_copies_num;
68 auto&& bids = get_bids(*std::begin(bidders));
69 BOOST_CONCEPT_ASSERT((boost::ForwardRangeConcept<
71 auto bid = std::begin(bids);
72 using value_t =
puretype(get_value(*bid));
73 static_assert(std::is_arithmetic<value_t>::value,
74 "get_value return type is not arithmetic!");
75 auto&& bid_items = get_items(*bid);
76 using bundle_t =
puretype(bid_items);
77 static_assert(std::is_move_constructible<bundle_t>::value,
78 "bundle_t is not move constructible!");
79 static_assert(std::is_default_constructible<bundle_t>::value,
80 "bundle_t is not default constructible!");
81 BOOST_CONCEPT_ASSERT((boost::ForwardRangeConcept<
82 decltype(bid_items)>));
89 template <
class B
idder,
class GetB
ids,
class GetValue,
class GetItems>
92 typename boost::range_iterator<
typename std::result_of<
93 GetBids(
Bidder)>::type>::type;
94 using bid =
typename std::iterator_traits<bid_iterator>::reference;
96 using items =
typename std::result_of<GetItems(bid)>::type;
97 using items_val =
typename std::decay<items>::type;
99 template <
class GetPrice>
103 template <
class GetB
ids,
class GetValue,
class GetItems>
107 GetValue m_get_value;
108 GetItems m_get_items;
110 template <
class B
idder>
115 : m_get_bids(get_bids), m_get_value(get_value), m_get_items(get_items) {}
121 class Value =
typename Traits::value
123 Value operator()(
Bidder&& bidder,
const ItemSet& item_set)
const
125 using Bid =
typename Traits::bid;
126 using Item =
typename Traits::item;
128 auto is_contained = [&](Bid b)
130 return boost::algorithm::all_of(
131 m_get_items(std::forward<Bid>(b)),
132 [&](Item i) {
return item_set.count(std::forward<Item>(i)) > 0; }
136 m_get_bids(std::forward<Bidder>(bidder)) |
137 boost::adaptors::filtered(is_contained),
176 GetCopiesNum get_copies_num = GetCopiesNum{}
179 std::forward<Bidders>(bidders),
180 std::forward<Items>(items),
181 detail::xor_bids_value_query<GetBids, GetValue, GetItems>(get_bids, get_value, get_items),
185 BOOST_CONCEPT_ASSERT((concepts::xor_bids<Bidders, Items, GetBids, GetValue, GetItems, GetCopiesNum>));
187 std::forward<Bidders>(bidders),
188 std::forward<Items>(items),
189 detail::xor_bids_value_query<GetBids, GetValue, GetItems>(get_bids, get_value, get_items),
196 template <
class GetB
ids,
class GetValue,
class GetItems>
200 GetValue m_get_value;
201 GetItems m_get_items;
203 template <
class B
idder>
206 template <
class B
idder,
class GetPrice,
class Base = traits<B
idder>>
207 struct price_traits : Base {
208 using price =
typename Base::template price<GetPrice>;
214 GetItems get_items) : m_get_bids(get_bids),
215 m_get_value(get_value), m_get_items(get_items) {}
217 template <
class B
idder,
class GetPrice>
218 auto operator()(
Bidder&& bidder, GetPrice get_price)
const
220 using Traits = price_traits<Bidder, GetPrice>;
221 using Items =
typename Traits::items_val;
222 using Res = std::pair<Items, typename Traits::utility>;
224 Res
best = {Items{}, 0};
225 auto&& bids = m_get_bids(std::forward<Bidder>(bidder));
226 for (
auto bid = std::begin(bids);
bid != std::end(bids); ++
bid) {
227 auto const value = m_get_value(*
bid);
230 auto const util = value - price;
231 if (util > best.second)
232 best = {m_get_items(*
bid), util};
270 GetCopiesNum get_copies_num = GetCopiesNum{})
272 BOOST_CONCEPT_ASSERT((concepts::xor_bids<Bidders, Items, GetBids,
273 GetValue, GetItems, GetCopiesNum>));
275 std::forward<Bidders>(bidders),
276 std::forward<Items>(items),
277 detail::xor_bids_demand_query<GetBids, GetValue, GetItems>(get_bids,
278 get_value, get_items),
285 template<
class GetB
ids,
class GetValue,
class GetItems>
289 GetValue m_get_value;
290 GetItems m_get_items;
298 friend class ::detail::test_xor_bids_gamma_oracle;
300 template <
class B
idder>
303 template <
class B
idder,
class GetPrice,
class Base = traits<B
idder>>
304 struct price_traits :
public Base {
305 using price =
typename Base::template price<GetPrice>;
309 boost::optional<std::pair<typename Base::bid_iterator, frac>>;
317 class BestBid =
typename price_traits<Bidder, GetPrice>::best_bid
328 auto&& bids = m_get_bids(std::forward<Bidder>(bidder));
329 for (
auto bid = std::begin(bids);
bid != std::end(bids); ++
bid) {
330 auto const value = m_get_value(*
bid);
331 if (value <= threshold)
continue;
336 if (is_better(frac, result))
337 result = std::make_pair(
bid, frac);
346 class Traits = price_traits<Bidder, GetPrice>,
347 class BestBid =
typename Traits::best_bid
350 minimum_frac(
Bidder&& bidder, GetPrice get_price, Threshold threshold)
353 return calculate_best(
354 std::forward<Bidder>(bidder),
357 [&](
typename Traits::frac frac,
const BestBid& result)
359 return !result || frac < result->second;
364 template <
class Result,
class OutputIterator>
365 auto output(
const Result& result, OutputIterator out)
const
367 auto bid = result.first;
368 auto frac = result.second;
369 boost::copy(m_get_items(*
bid), out);
375 : m_get_bids(get_bids), m_get_value(get_value), m_get_items(get_items) {}
381 class Traits = price_traits<Bidder, GetPrice>
383 boost::optional<std::pair<typename Traits::items_val, typename Traits::frac>>
384 operator()(
Bidder&& bidder, GetPrice get_price, Threshold threshold)
const
386 auto const best = minimum_frac(std::forward<Bidder>(bidder),
387 get_price, threshold);
388 if (!
best)
return boost::none;
389 return std::make_pair(m_get_items(*
best->first),
best->second);
424 GetCopiesNum get_copies_num = GetCopiesNum{}
427 std::forward<Bidders>(bidders),
428 std::forward<Items>(items),
429 detail::xor_bids_gamma_oracle<GetBids, GetValue, GetItems>(get_bids, get_value, get_items),
434 BOOST_CONCEPT_ASSERT((concepts::xor_bids<Bidders, Items, GetBids, GetValue, GetItems, GetCopiesNum>));
436 std::forward<Bidders>(bidders),
437 std::forward<Items>(items),
438 detail::xor_bids_gamma_oracle<GetBids, GetValue, GetItems>(get_bids, get_value, get_items),
457 template<
class B
idders,
class GetB
ids,
class GetItems,
class OutputIterator>
462 OutputIterator output
464 for (
auto&& bidder: bidders) {
465 for (
auto&& bid: get_bids(std::forward<decltype(bidder)>(bidder))) {
466 boost::copy(get_items(std::forward<decltype(bid)>(bid)), output);
473 #endif // PAAL_XOR_BIDS_HPP
auto make_xor_bids_to_demand_query_auction(Bidders &&bidders, Items &&items, GetBids get_bids, GetValue get_value, GetItems get_items, GetCopiesNum get_copies_num=GetCopiesNum{})
detail
auto make_xor_bids_to_value_query_auction(Bidders &&bidders, Items &&items, GetBids get_bids, GetValue get_value, GetItems get_items, GetCopiesNum get_copies_num=GetCopiesNum{}) -> decltype(make_value_query_auction_components(std::forward< Bidders >(bidders), std::forward< Items >(items), detail::xor_bids_value_query< GetBids, GetValue, GetItems >(get_bids, get_value, get_items), get_copies_num))
detail
std::string Bidder
[Demand Query Auction Components Example]
bool best(Solution &solution, ContinueOnSuccess on_success, components...comps)
This local search chooses the best possible move and applies it to the solution. Note that this strat...
auto make_value_query_auction_components(Args &&...args) -> decltype(value_query_components::make_components(std::forward< Args >(args)...))
make function for value query components
auto make_xor_bids_to_gamma_oracle_auction(Bidders &&bidders, Items &&items, GetBids get_bids, GetValue get_value, GetItems get_items, GetCopiesNum get_copies_num=GetCopiesNum{}) -> decltype(make_gamma_oracle_auction_components(std::forward< Bidders >(bidders), std::forward< Items >(items), detail::xor_bids_gamma_oracle< GetBids, GetValue, GetItems >(get_bids, get_value, get_items), 1, get_copies_num))
detail
#define puretype(t)
for given expression returns its type with removed const and reference
This file contains set of simple useful functors or functor adapters.
fraction< A, B > make_fraction(A a, B b)
make function for fraction
T accumulate_functor(const Range &rng, T init, Functor f, BinaryOperation bin_op=BinaryOperation{})
combination of boost::accumulate and boost::adaptors::transformed
auto make_demand_query_auction_components(Args &&...args)
make function for demand query components
void extract_items_from_xor_bids(Bidders &&bidders, GetBids get_bids, GetItems get_items, OutputIterator output)
extract all items appearing in all bids. This function doesn't eliminate duplicates, this is left out to the caller.
auto sum_functor(const Range &rng, Functor f)
sum of functor values over the range elements
Implementation of fractions, which are used only for comparison purposes, and thus they can be used w...
typename std::decay< typename std::result_of< F >::type >::type pure_result_of_t
return pure type of function (decays const and reference)
puretype(std::declval< T1 >()+std::declval< T2 >()) promote_with_t
return type obtained after adding values of given types
auto make_gamma_oracle_auction_components(Args &&...args) -> decltype(gamma_oracle_components::make_components(std::forward< Args >(args)...))
make function for gamma oracle components
typename boost::range_reference< Range >::type range_to_ref_t
for given range returns type of its reference
simple class to represent fraction