All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
demand_query_example.cpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2013 Robert Rosolek
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
17 
18 #include <boost/optional/optional.hpp>
19 #include <boost/range/algorithm/copy.hpp>
20 
21 #include <algorithm>
22 #include <cassert>
23 #include <vector>
24 
26 
27 namespace pa = paal::auctions;
28 namespace pds = paal::data_structures;
29 
30 using Bidder = std::string;
31 using Item = std::string;
32 using Items = std::vector<Item>;
33 using Value = int;
34 
35 const std::vector<Bidder> bidders {"Pooh Bear", "Rabbit"};
36 
37 const Items items {"honey", "baby carrot", "carrot", "jam"};
38 
40  template <class GetPrice>
41  std::pair<Items, Value>
42  operator()(Bidder bidder, GetPrice get_price) const
43  {
44  if (bidder == "Pooh Bear") {
45  const Value util = 10 - get_price("honey");
46  if (util <= 0) return std::make_pair(Items{}, 0);
47  return std::make_pair(Items{"honey"}, util);
48  }
49 
50  assert(bidder == "Rabbit");
51 
52  const Value baby_val = 2, val = 3;
53  auto const baby_price = get_price("baby carrot"), price = get_price("carrot");
54 
55  const Value baby_util = baby_val - baby_price,
56  util = val - price, both_util = baby_val + val - baby_price - price;
57 
58  if (baby_util <= 0 && util <= 0 && both_util <= 0) return std::make_pair(Items{}, 0);
59 
60  if (baby_util >= util && baby_util >= both_util)
61  return std::make_pair(Items{"baby carrot"}, baby_util);
62 
63  if (util >= both_util)
64  return std::make_pair(Items{"carrot"}, util);
65 
66  return std::make_pair(Items{"baby carrot", "carrot"}, both_util);
67  }
68 };
70 
71 int main() {
73  auto const auction = pa::make_demand_query_auction_components(
74  bidders, items, demand_query_func()
75  );
77 
79  auto get_price = [](Item item) { return item == "honey" ? 5 : 2; };
80 
81  std::cout << "pooh bear buys: ";
82  auto got_pooh_bear = auction.call<pa::demand_query>("Pooh Bear", get_price);
83  boost::copy(got_pooh_bear.first, std::ostream_iterator<Item>(std::cout, ", "));
84  std::cout << std::endl;
85 
86  std::cout << "rabbit oracle buys: ";
87  auto got_rabbit = auction.call<pa::demand_query>("Rabbit", get_price);
88  boost::copy(got_rabbit.first, std::ostream_iterator<Item>(std::cout, ", "));
89  std::cout << std::endl;
91  return 0;
92 }
int main()
[Demand Query Auction Components Example]
std::string Bidder
[Demand Query Auction Components Example]
auto make_demand_query_auction_components(Args &&...args)
make function for demand query components