All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
gamma_oracle_example.cpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c)
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 //=======================================================================
18 
19 #include <boost/optional/optional.hpp>
20 #include <boost/range/algorithm/copy.hpp>
21 
22 #include <cassert>
23 #include <iostream>
24 #include <utility>
25 #include <vector>
26 
28 
29 namespace pa = paal::auctions;
30 namespace pds = paal::data_structures;
31 
32 using Bidder = std::string;
33 using Item = std::string;
34 using Items = std::vector<Item>;
35 using Value = int;
37 
38 const std::vector<Bidder> bidders {"Pooh Bear", "Rabbit"};
39 
40 const Items items {"honey", "baby carrot", "carrot", "jam"};
41 
42 const int gamma_val = 2;
43 
45  template <class GetPrice, class Threshold>
46  boost::optional<std::pair<Items, Frac>>
47  operator()(Bidder bidder, GetPrice get_price, Threshold z) const {
48 
49  if (bidder == "Pooh Bear") {
50  const Value val = 10;
51  if (val <= z) return boost::none;
52  return std::make_pair(Items{"honey"}, Frac(get_price("honey"), val - z));
53  }
54 
55  assert(bidder == "Rabbit");
56 
57  const Value baby_val = 2, val = 3;
58  auto const baby_price = get_price("baby carrot");
59  auto const price = get_price("carrot");
60  auto const baby_frac = Frac(baby_price, baby_val - z),
61  frac = Frac(price, val - z),
62  both_frac = Frac(baby_price + price, baby_val + val - z);
63 
64  auto check = [=](Frac candidate, Frac other1, Frac other2) {
65  if (candidate.den <= 0) return false;
66  auto check_single = [=](Frac candidate, Frac other) {
67  return other.den <= 0 || candidate <= gamma_val * other;
68  };
69  return check_single(candidate, other1) && check_single(candidate, other2);
70  };
71 
72  if (check(baby_frac, frac, both_frac))
73  return std::make_pair(Items{"baby carrot"}, baby_frac);
74  if (check(frac, baby_frac, both_frac))
75  return std::make_pair(Items{"carrot"}, frac);
76  if (check(both_frac, baby_frac, frac))
77  return std::make_pair(Items{"baby carrot", "carrot"}, both_frac);
78  return boost::none;
79  }
80 };
81 
83 
84 int main()
85 {
87  auto const auction = pa::make_gamma_oracle_auction_components(
88  bidders, items, gamma_oracle_func(), gamma_val
89  );
91 
93  auto get_price_func = [](Item item) { return item == "honey" ? 5 : 2; };
94 
95  std::cout << "pooh bear buys: ";
96  auto got_pooh_bear =
97  auction.call<pa::gamma_oracle>("Pooh Bear", get_price_func, 10);
98  if (!got_pooh_bear)
99  std::cout << "nothing";
100  else
101  boost::copy(got_pooh_bear->first, std::ostream_iterator<Item>(std::cout, ", "));
102  std::cout << std::endl;
103 
104  std::cout << "rabbit oracle buys: ";
105  auto got_rabbit = auction.call<pa::gamma_oracle>("Rabbit", get_price_func, 1);
106  if (!got_rabbit)
107  std::cout << "nothing";
108  else
109  boost::copy(got_rabbit->first, std::ostream_iterator<Item>(std::cout, ", "));
110  std::cout << std::endl;
111 
113  return 0;
114 }
int main()
[Gamma Oracle Auction Components Example]
std::string Bidder
[Demand Query Auction Components Example]
Implementation of fractions, which are used only for comparison purposes, and thus they can be used w...
auto make_gamma_oracle_auction_components(Args &&...args) -> decltype(gamma_oracle_components::make_components(std::forward< Args >(args)...))
make function for gamma oracle components
simple class to represent fraction
Definition: fraction.hpp:31