CYCLUS
bid_portfolio.h
Go to the documentation of this file.
1 #ifndef CYCLUS_SRC_BID_PORTFOLIO_H_
2 #define CYCLUS_SRC_BID_PORTFOLIO_H_
3 
4 #include <set>
5 #include <sstream>
6 #include <string>
7 
8 #include <boost/shared_ptr.hpp>
9 
10 #include "bid.h"
11 #include "capacity_constraint.h"
12 #include "error.h"
13 
14 namespace cyclus {
15 
16 class Trader;
17 
18 std::string GetTraderPrototype(Trader* bidder);
19 std::string GetTraderSpec(Trader* bidder);
20 
21 /// @class BidPortfolio
22 ///
23 /// @brief A BidPortfolio is a collection of bids as responses to requests for
24 /// resources and associated constraints on those bids.
25 ///
26 /// A BidPortfolio contains all the information corresponding to a bidder's
27 /// response to resource requests. It is a light wrapper around the set of bids
28 /// and constraints for a given bidder, guaranteeing a single bidder per
29 /// portfolio. Responses are grouped by the bidder. Constraints are assumed to
30 /// act over the entire set of possible bids.
31 template <class T>
32 class BidPortfolio : public boost::enable_shared_from_this<BidPortfolio<T>> {
33  public:
34  typedef boost::shared_ptr<BidPortfolio<T>> Ptr;
35 
36  /// @brief default constructor
37  BidPortfolio() : bidder_(NULL) {}
38 
39  /// deletes all bids associated with it
41  typename std::set<Bid<T>*>::iterator it;
42  for (it = bids_.begin(); it != bids_.end(); ++it) {
43  delete *it;
44  }
45  }
46 
47  /// @brief add a bid to the portfolio
48  /// @param request the request being responded to by this bid
49  /// @param offer the resource being offered in response to the request
50  /// @param bidder the bidder
51  /// @param exclusive indicates whether the bid is exclusive
52  /// @param preference sets the preference of the bid on a request
53  /// bid arc.
54  /// @throws KeyError if a bid is added from a different bidder than the
55  /// original
56  Bid<T>* AddBid(Request<T>* request, boost::shared_ptr<T> offer,
57  Trader* bidder, bool exclusive, double preference) {
58  Bid<T>* b = Bid<T>::Create(request, offer, bidder, this->shared_from_this(),
59  exclusive, preference);
60  VerifyResponder_(b);
61  if (offer->quantity() > 0)
62  bids_.insert(b);
63  else {
64  std::stringstream ss;
65  ss << GetTraderPrototype(bidder) << " from " << GetTraderSpec(bidder)
66  << " is offering a bid quantity <= 0, Q = " << offer->quantity();
67  throw ValueError(ss.str());
68  }
69  return b;
70  }
71 
72  /// @brief add a bid to the portfolio
73  /// @param request the request being responded to by this bid
74  /// @param offer the resource being offered in response to the request
75  /// @param bidder the bidder
76  /// @param exclusive indicates whether the bid is exclusive
77  /// @throws KeyError if a bid is added from a different bidder than the
78  /// original
79  Bid<T>* AddBid(Request<T>* request, boost::shared_ptr<T> offer,
80  Trader* bidder, bool exclusive = false) {
81  return AddBid(request, offer, bidder, exclusive,
82  std::numeric_limits<double>::quiet_NaN());
83  }
84 
85  /// @brief add a capacity constraint associated with the portfolio
86  /// @param c the constraint to add
87  inline void AddConstraint(const CapacityConstraint<T>& c) {
88  constraints_.insert(c);
89  }
90 
91  /// @return the agent associated with the portfolio. If no bids have
92  /// been added, the bidder is NULL.
93  inline Trader* bidder() const { return bidder_; }
94 
95  /// @return *deprecated* the commodity associated with the portfolio.
96  inline std::string commodity() const { return ""; }
97 
98  /// @return const access to the bids
99  inline const std::set<Bid<T>*>& bids() const { return bids_; }
100 
101  /// @return the set of constraints over the bids
102  inline const std::set<CapacityConstraint<T>>& constraints() const {
103  return constraints_;
104  }
105 
106  private:
107  /// @brief copy constructor is private to prevent copying and preserve
108  /// explicit single-ownership of bids
109  BidPortfolio(const BidPortfolio& rhs) {
110  bidder_ = rhs.bidder_;
111  bids_ = rhs.bids_;
112  constraints_ = rhs.constraints_;
113  typename std::set<Bid<T>*>::iterator it;
114  for (it = bids_.begin(); it != bids_.end(); ++it) {
115  it->get()->set_portfolio(this->shared_from_this());
116  }
117  }
118 
119  /// @brief if the bidder has not been determined yet, it is set. Otherwise
120  /// VerifyResponder() verifies the bid is associated with the
121  /// portfolio's bidder
122  /// @throws KeyError if a bid is added from a different bidder than the
123  /// original
124  void VerifyResponder_(Bid<T>* b) {
125  if (bidder_ == NULL) {
126  bidder_ = b->bidder();
127  } else if (bidder_ != b->bidder()) {
128  std::string msg = "Insertion error: bidders do not match.";
129  throw KeyError(msg);
130  }
131  }
132 
133  /// @brief *deprecated*
134  void VerifyCommodity_(const Bid<T>* r) {}
135 
136  // bid_ is a set because there is a one-to-one correspondence between a
137  // bid and a request, i.e., bids are unique
138  std::set<Bid<T>*> bids_;
139 
140  // constraints_ is a set because constraints are assumed to be unique
141  std::set<CapacityConstraint<T>> constraints_;
142 
143  Trader* bidder_;
144 };
145 
146 } // namespace cyclus
147 
148 #endif // CYCLUS_SRC_BID_PORTFOLIO_H_
boost::shared_ptr< BidPortfolio< T > > Ptr
Definition: bid_portfolio.h:34
double b(int nuc)
Computes the scattering length [cm] from the coherent and incoherent components.
Definition: pyne.cc:11180
Trader * bidder() const
Definition: bid.h:80
For values that are too big, too small, etc.
Definition: error.h:41
std::string GetTraderPrototype(Trader *bidder)
A CapacityConstraint provides an ability to determine an agent&#39;s constraints on resource allocation g...
A simple API for agents that wish to exchange resources in the simulation.
Definition: trader.h:24
std::string commodity() const
Definition: bid_portfolio.h:96
Trader * bidder() const
Definition: bid_portfolio.h:93
A Bid encapsulates all the information required to communicate a bid response to a request for a reso...
Definition: bid.h:20
~BidPortfolio()
deletes all bids associated with it
Definition: bid_portfolio.h:40
A BidPortfolio is a collection of bids as responses to requests for resources and associated constrai...
Definition: bid.h:13
Bid< T > * AddBid(Request< T > *request, boost::shared_ptr< T > offer, Trader *bidder, bool exclusive, double preference)
add a bid to the portfolio
Definition: bid_portfolio.h:56
const std::set< Bid< T > * > & bids() const
Definition: bid_portfolio.h:99
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
static Bid< T > * Create(Request< T > *request, boost::shared_ptr< T > offer, Trader *bidder, typename BidPortfolio< T >::Ptr portfolio, bool exclusive, double preference)
a factory method for a bid
Definition: bid.h:33
For failed retrieval/insertion of key-based data into/from data structures.
Definition: error.h:47
const std::set< CapacityConstraint< T > > & constraints() const
void AddConstraint(const CapacityConstraint< T > &c)
add a capacity constraint associated with the portfolio
Definition: bid_portfolio.h:87
std::string GetTraderSpec(Trader *bidder)
Bid< T > * AddBid(Request< T > *request, boost::shared_ptr< T > offer, Trader *bidder, bool exclusive=false)
add a bid to the portfolio
Definition: bid_portfolio.h:79
BidPortfolio()
default constructor
Definition: bid_portfolio.h:37
A Request encapsulates all the information required to communicate the needs of an agent in the Dynam...
Definition: request.h:29