diff --git a/src/bid.h b/src/bid.h index 0d958efe3a..1403e1849d 100644 --- a/src/bid.h +++ b/src/bid.h @@ -10,15 +10,14 @@ namespace cyclus { class Trader; -template class BidPortfolio; +template class BidPortfolio; /// @class Bid /// /// @brief A Bid encapsulates all the information required to communicate a bid /// response to a request for a resource, including the resource bid and the /// bidder. -template -class Bid { +template class Bid { public: /// @brief a factory method for a bid /// @param request the request being responded to by this bid @@ -26,72 +25,89 @@ class Bid { /// @param bidder the bidder /// @param portfolio the porftolio of which this bid is a part /// @param exclusive flag for whether the bid is exclusive - /// @param preference specifies the preference of a bid in a request - /// to bid arc. If NaN the request preference is used. + /// @param preference specifies the preference of a bid in a request + /// to bid arc. If NaN the request preference is used. /// WARNING: This should only be set by the bidder using the - /// requests callback cost function. Bidders should not - /// arbitrarily set this preference. + /// requests callback cost function. Bidders should not + /// arbitrarily set this preference. inline static Bid* Create(Request* request, - boost::shared_ptr offer, + boost::shared_ptr + offer, Trader* bidder, typename BidPortfolio::Ptr portfolio, - bool exclusive = false, - double preference = std::numeric_limits::quiet_NaN()) { + bool exclusive, + double preference) { return new Bid(request, offer, bidder, portfolio, exclusive, preference); } + /// @brief a factory method for a bid + /// @param request the request being responded to by this bid + /// @param offer the resource being offered in response to the request + /// @param bidder the bidder + /// @param portfolio the porftolio of which this bid is a part + /// @param exclusive flag for whether the bid is exclusive + inline static Bid* Create(Request* request, + boost::shared_ptr + offer, + Trader* bidder, + typename BidPortfolio::Ptr portfolio, + bool exclusive = false) { + return Create(request, offer, bidder, portfolio, exclusive, + std::numeric_limits::quiet_NaN()); + } /// @brief a factory method for a bid for a bid without a portfolio /// @warning this factory should generally only be used for testing inline static Bid* Create(Request* request, boost::shared_ptr offer, - Trader* bidder, bool exclusive = false, - double preference = std::numeric_limits::quiet_NaN()) { + Trader* bidder, bool exclusive, + double preference) { return new Bid(request, offer, bidder, exclusive, preference); } + /// @brief a factory method for a bid for a bid without a portfolio + /// @warning this factory should generally only be used for testing + inline static Bid* Create(Request* request, boost::shared_ptr offer, + Trader* bidder, bool exclusive = false) { + return Create(request, offer, bidder, exclusive, + std::numeric_limits::quiet_NaN()); + } /// @return the request being responded to - inline Request* request() const { - return request_; - } + inline Request* request() const { return request_; } /// @return the bid object for the request - inline boost::shared_ptr offer() const { - return offer_; - } + inline boost::shared_ptr offer() const { return offer_; } /// @return the agent responding the request - inline Trader* bidder() const { - return bidder_; - } + inline Trader* bidder() const { return bidder_; } /// @return the portfolio of which this bid is a part - inline typename BidPortfolio::Ptr portfolio() { - return portfolio_.lock(); - } + inline typename BidPortfolio::Ptr portfolio() { return portfolio_.lock(); } /// @return whether or not this an exclusive bid - inline bool exclusive() const { - return exclusive_; - } + inline bool exclusive() const { return exclusive_; } /// @return the preference of this bid - inline double preference() const { - return preference_; - } + inline double preference() const { return preference_; } private: /// @brief constructors are private to require use of factory methods Bid(Request* request, boost::shared_ptr offer, Trader* bidder, - bool exclusive = false, - double preference = std::numeric_limits::quiet_NaN()) + bool exclusive, double preference) : request_(request), offer_(offer), bidder_(bidder), exclusive_(exclusive), preference_(preference) {} + /// @brief constructors are private to require use of factory methods + Bid(Request* request, boost::shared_ptr offer, Trader* bidder, + bool exclusive = false) + : request_(request), + offer_(offer), + bidder_(bidder), + exclusive_(exclusive), + preference_(std::numeric_limits::quiet_NaN()) {} Bid(Request* request, boost::shared_ptr offer, Trader* bidder, - typename BidPortfolio::Ptr portfolio, bool exclusive = false, - double preference = std::numeric_limits::quiet_NaN()) + typename BidPortfolio::Ptr portfolio, bool exclusive, double preference) : request_(request), offer_(offer), bidder_(bidder), @@ -99,14 +115,22 @@ class Bid { exclusive_(exclusive), preference_(preference) {} + Bid(Request* request, boost::shared_ptr offer, Trader* bidder, + typename BidPortfolio::Ptr portfolio, bool exclusive = false) + : request_(request), + offer_(offer), + bidder_(bidder), + portfolio_(portfolio), + exclusive_(exclusive), + preference_(std::numeric_limits::quiet_NaN()) {} + Request* request_; boost::shared_ptr offer_; Trader* bidder_; - boost::weak_ptr > portfolio_; + boost::weak_ptr> portfolio_; bool exclusive_; double preference_; }; } // namespace cyclus - #endif // CYCLUS_SRC_BID_H_ diff --git a/src/bid_portfolio.h b/src/bid_portfolio.h index fb63424e92..855c74b7e5 100644 --- a/src/bid_portfolio.h +++ b/src/bid_portfolio.h @@ -2,8 +2,8 @@ #define CYCLUS_SRC_BID_PORTFOLIO_H_ #include -#include #include +#include #include @@ -15,11 +15,9 @@ namespace cyclus { class Trader; - std::string GetTraderPrototype(Trader* bidder); std::string GetTraderSpec(Trader* bidder); - /// @class BidPortfolio /// /// @brief A BidPortfolio is a collection of bids as responses to requests for @@ -31,9 +29,9 @@ std::string GetTraderSpec(Trader* bidder); /// portfolio. Responses are grouped by the bidder. Constraints are assumed to /// act over the entire set of possible bids. template -class BidPortfolio : public boost::enable_shared_from_this< BidPortfolio > { +class BidPortfolio : public boost::enable_shared_from_this> { public: - typedef boost::shared_ptr< BidPortfolio > Ptr; + typedef boost::shared_ptr> Ptr; /// @brief default constructor BidPortfolio() : bidder_(NULL) {} @@ -52,26 +50,38 @@ class BidPortfolio : public boost::enable_shared_from_this< BidPortfolio > { /// @param bidder the bidder /// @param exclusive indicates whether the bid is exclusive /// @param preference sets the preference of the bid on a request - /// bid arc. + /// bid arc. /// @throws KeyError if a bid is added from a different bidder than the /// original Bid* AddBid(Request* request, boost::shared_ptr offer, - Trader* bidder, bool exclusive = false, - double preference = std::numeric_limits::quiet_NaN()) { - Bid* b = - Bid::Create(request, offer, bidder, this->shared_from_this(), - exclusive, preference); + Trader* bidder, bool exclusive, double preference) { + Bid* b = Bid::Create(request, offer, bidder, this->shared_from_this(), + exclusive, preference); VerifyResponder_(b); - if(offer->quantity() > 0 ) + if (offer->quantity() > 0) bids_.insert(b); - else{ + else { std::stringstream ss; - ss << GetTraderPrototype(bidder) << " from " << GetTraderSpec(bidder) << " is offering a bid quantity <= 0, Q = " << offer->quantity() ; + ss << GetTraderPrototype(bidder) << " from " << GetTraderSpec(bidder) + << " is offering a bid quantity <= 0, Q = " << offer->quantity(); throw ValueError(ss.str()); } return b; } + /// @brief add a bid to the portfolio + /// @param request the request being responded to by this bid + /// @param offer the resource being offered in response to the request + /// @param bidder the bidder + /// @param exclusive indicates whether the bid is exclusive + /// @throws KeyError if a bid is added from a different bidder than the + /// original + Bid* AddBid(Request* request, boost::shared_ptr offer, + Trader* bidder, bool exclusive = false) { + return AddBid(request, offer, bidder, exclusive, + std::numeric_limits::quiet_NaN()); + } + /// @brief add a capacity constraint associated with the portfolio /// @param c the constraint to add inline void AddConstraint(const CapacityConstraint& c) { @@ -80,22 +90,16 @@ class BidPortfolio : public boost::enable_shared_from_this< BidPortfolio > { /// @return the agent associated with the portfolio. If no bids have /// been added, the bidder is NULL. - inline Trader* bidder() const { - return bidder_; - } + inline Trader* bidder() const { return bidder_; } /// @return *deprecated* the commodity associated with the portfolio. - inline std::string commodity() const { - return ""; - } + inline std::string commodity() const { return ""; } /// @return const access to the bids - inline const std::set*>& bids() const { - return bids_; - } + inline const std::set*>& bids() const { return bids_; } /// @return the set of constraints over the bids - inline const std::set< CapacityConstraint >& constraints() const { + inline const std::set>& constraints() const { return constraints_; } @@ -127,14 +131,14 @@ class BidPortfolio : public boost::enable_shared_from_this< BidPortfolio > { } /// @brief *deprecated* - void VerifyCommodity_(const Bid* r) { } - + void VerifyCommodity_(const Bid* r) {} + // bid_ is a set because there is a one-to-one correspondence between a // bid and a request, i.e., bids are unique std::set*> bids_; // constraints_ is a set because constraints are assumed to be unique - std::set< CapacityConstraint > constraints_; + std::set> constraints_; Trader* bidder_; }; diff --git a/src/request.h b/src/request.h index b976942a2e..44a61510a3 100644 --- a/src/request.h +++ b/src/request.h @@ -26,8 +26,7 @@ template class RequestPortfolio; /// the needs of an agent in the Dynamic Resource Exchange, including the /// commodity it needs as well as a resource specification for that commodity. /// A Request is templated its resource. -template -class Request { +template class Request { public: typedef std::function)> cost_function_t; @@ -38,34 +37,60 @@ class Request { /// @param commodity the commodity associated with this request /// @param preference the preference associated with this request (relative to /// others in the portfolio) - /// @param exclusive a flag denoting that this request must be met exclusively, + /// @param exclusive a flag denoting that this request must be met + /// exclusively, /// i.e., in its entirety by a single offer /// @param cost_function a standard function object that returns the cost of a - /// potential resource when called. - inline static Request* Create( - boost::shared_ptr target, - Trader* requester, - typename RequestPortfolio::Ptr portfolio, - std::string commodity = "", - double preference = kDefaultPref, - bool exclusive = false, - cost_function_t cost_function = NULL) { - return new Request(target, requester, portfolio, - commodity, preference, exclusive, - cost_function); + /// potential resource when called. + inline static Request* Create(boost::shared_ptr target, + Trader* requester, + typename RequestPortfolio::Ptr portfolio, + std::string commodity, + double preference, + bool exclusive, + cost_function_t cost_function) { + return new Request(target, requester, portfolio, commodity, preference, + exclusive, cost_function); + } + /// @brief a factory method for a request + /// @param target the target resource associated with this request + /// @param requester the requester + /// @param portfolio the porftolio of which this request is a part + /// @param commodity the commodity associated with this request + /// @param preference the preference associated with this request (relative to + /// others in the portfolio) + /// @param exclusive a flag denoting that this request must be met + /// exclusively, + /// i.e., in its entirety by a single offer + inline static Request* Create(boost::shared_ptr target, + Trader* requester, + typename RequestPortfolio::Ptr portfolio, + std::string commodity = "", + double preference = kDefaultPref, + bool exclusive = false) { + return Create(target, requester, portfolio, commodity, preference, + exclusive, NULL); } /// @brief a factory method for a bid for a bid without a portfolio /// @warning this factory should generally only be used for testing - inline static Request* Create( - boost::shared_ptr target, - Trader* requester, - std::string commodity = "", - double preference = kDefaultPref, - bool exclusive = false, - cost_function_t cost_function = NULL) { - return new Request(target, requester, commodity, preference, - exclusive, cost_function); + inline static Request* Create(boost::shared_ptr target, + Trader* requester, + std::string commodity, + double preference, + bool exclusive, + cost_function_t cost_function) { + return new Request(target, requester, commodity, preference, exclusive, + cost_function); + } + /// @brief a factory method for a bid for a bid without a portfolio + /// @warning this factory should generally only be used for testing + inline static Request* Create(boost::shared_ptr target, + Trader* requester, + std::string commodity = "", + double preference = kDefaultPref, + bool exclusive = false) { + return Create(target, requester, commodity, preference, exclusive, NULL); } /// @return this request's target @@ -89,41 +114,58 @@ class Request { inline bool exclusive() const { return exclusive_; } /// @return the cost function for the request - inline cost_function_t cost_function() const { - return cost_function_; - } + inline cost_function_t cost_function() const { return cost_function_; } private: + /// @brief constructors are private to require use of factory methods + Request(boost::shared_ptr target, Trader* requester, std::string commodity, + double preference, bool exclusive, cost_function_t cost_function) + : target_(target), + requester_(requester), + commodity_(commodity), + preference_(preference), + exclusive_(exclusive), + cost_function_(cost_function) {} + /// @brief constructors are private to require use of factory methods Request(boost::shared_ptr target, Trader* requester, std::string commodity = "", double preference = kDefaultPref, - bool exclusive = false, - cost_function_t cost_function = NULL) + bool exclusive = false) : target_(target), requester_(requester), commodity_(commodity), preference_(preference), exclusive_(exclusive), + cost_function_(NULL) {} + + Request(boost::shared_ptr target, Trader* requester, + typename RequestPortfolio::Ptr portfolio, std::string commodity, + double preference, bool exclusive, cost_function_t cost_function) + : target_(target), + requester_(requester), + commodity_(commodity), + preference_(preference), + portfolio_(portfolio), + exclusive_(exclusive), cost_function_(cost_function) {} Request(boost::shared_ptr target, Trader* requester, typename RequestPortfolio::Ptr portfolio, std::string commodity = "", double preference = kDefaultPref, - bool exclusive = false, - cost_function_t cost_function = NULL) + bool exclusive = false) : target_(target), requester_(requester), commodity_(commodity), preference_(preference), portfolio_(portfolio), exclusive_(exclusive), - cost_function_(cost_function) {} + cost_function_(NULL) {} boost::shared_ptr target_; Trader* requester_; double preference_; std::string commodity_; - boost::weak_ptr > portfolio_; + boost::weak_ptr> portfolio_; bool exclusive_; cost_function_t cost_function_; }; diff --git a/src/request_portfolio.h b/src/request_portfolio.h index 030d93e3b2..003f1f0e7a 100644 --- a/src/request_portfolio.h +++ b/src/request_portfolio.h @@ -20,8 +20,7 @@ namespace cyclus { class Trader; /// @brief accumulator sum for request quantities -template -inline double SumQty(double total, Request* r) { +template inline double SumQty(double total, Request* r) { return total += r->target()->quantity(); } @@ -30,21 +29,19 @@ inline double SumQty(double total, Request* r) { /// Coefficients are determiend by the request portfolio and are provided to the /// converter. The arc and exchange context are used in order to reference the /// original request so that the request's coefficient can be applied. -template -struct QtyCoeffConverter : public Converter { +template struct QtyCoeffConverter : public Converter { QtyCoeffConverter(const std::map*, double>& coeffs) : coeffs(coeffs) {} inline virtual double convert( boost::shared_ptr offer, - Arc const * a, - ExchangeTranslationContext const * ctx) const { + Arc const* a, + ExchangeTranslationContext const* ctx) const { return offer->quantity() * coeffs.at(ctx->node_to_request.at(a->unode())); } virtual bool operator==(Converter& other) const { - QtyCoeffConverter* cast = - dynamic_cast*>(&other); + QtyCoeffConverter* cast = dynamic_cast*>(&other); return cast != NULL && coeffs == cast->coeffs; } @@ -58,7 +55,8 @@ struct QtyCoeffConverter : public Converter { /// /// The portfolio contains a grouping of resource requests that may be mutually /// met by suppliers. These requests may share a common set of -/// constraints. Take, for instance, a facility that needs fuel, of which there are +/// constraints. Take, for instance, a facility that needs fuel, of which there +/// are /// two commodity types, fuelA and fuelB. If some combination of the two suffice /// the facility's needs, then requests for both would be added to the portfolio /// along with a capacity constraint. @@ -67,7 +65,8 @@ struct QtyCoeffConverter : public Converter { /// accounts for mutual requests, if the portfolio has them. , e.g., /// @code /// -/// RequestPortfolio::Ptr rp(new RequestPortfolio()); +/// RequestPortfolio::Ptr rp(new +/// RequestPortfolio()); /// // add some requests /// rp->AddRequest(/* args */); /// // declare some of them as multicommodity requsts (i.e., any one will @@ -83,11 +82,11 @@ struct QtyCoeffConverter : public Converter { /// determine the demand as "met". In this case, the total demand is 9.5, the /// MOX order is given a coefficient of 9.5 / 10, and the UOX order is given a /// coefficient of 9.5 / 9. -template -class RequestPortfolio : -public boost::enable_shared_from_this< RequestPortfolio > { +template +class RequestPortfolio + : public boost::enable_shared_from_this> { public: - typedef boost::shared_ptr< RequestPortfolio > Ptr; + typedef boost::shared_ptr> Ptr; typedef std::function)> cost_function_t; RequestPortfolio() : requester_(NULL), qty_(0) {} @@ -106,17 +105,16 @@ public boost::enable_shared_from_this< RequestPortfolio > { /// @param commodity the commodity associated with this request /// @param preference the preference associated with this request (relative to /// others in the portfolio) - /// @param exclusive a flag denoting that this request must be met exclusively, + /// @param exclusive a flag denoting that this request must be met + /// exclusively, /// i.e., in its entirety by a single offer /// @param cost_function The cost function that the requester sets so that the /// bidder may evaluate many potential resources. /// @throws KeyError if a request is added from a different requester than the /// original or if the request quantity is different than the original Request* AddRequest(boost::shared_ptr target, Trader* requester, - std::string commodity = "", - double preference = kDefaultPref, - bool exclusive = false, - cost_function_t cost_function = NULL) { + std::string commodity, double preference, + bool exclusive, cost_function_t cost_function) { Request* r = Request::Create(target, requester, this->shared_from_this(), commodity, preference, exclusive, cost_function); @@ -126,6 +124,24 @@ public boost::enable_shared_from_this< RequestPortfolio > { qty_ += target->quantity(); return r; } + /// @brief add a request to the portfolio + /// @param target the target resource associated with this request + /// @param requester the requester + /// @param commodity the commodity associated with this request + /// @param preference the preference associated with this request (relative to + /// others in the portfolio) + /// @param exclusive a flag denoting that this request must be met + /// exclusively, + /// i.e., in its entirety by a single offer + /// @throws KeyError if a request is added from a different requester than the + /// original or if the request quantity is different than the original + Request* AddRequest(boost::shared_ptr target, Trader* requester, + std::string commodity = "", + double preference = kDefaultPref, + bool exclusive = false) { + return AddRequest(target, requester, commodity, preference, exclusive, + NULL); + } /// @brief adds a collection of requests (already having been registered with /// this portfolio) as multicommodity requests @@ -162,12 +178,10 @@ public boost::enable_shared_from_this< RequestPortfolio > { inline double qty() const { return qty_; } /// @return const access to the unconstrained requests - inline const std::vector*>& requests() const { - return requests_; - } + inline const std::vector*>& requests() const { return requests_; } /// @return const access to the request constraints - inline const std::set< CapacityConstraint >& constraints() const { + inline const std::set>& constraints() const { return constraints_; } @@ -191,7 +205,8 @@ public boost::enable_shared_from_this< RequestPortfolio > { } /// @brief if the requester has not been determined yet, it is set. otherwise - /// VerifyRequester() verifies the the request is associated with the portfolio's + /// VerifyRequester() verifies the the request is associated with the + /// portfolio's /// requester /// @throws KeyError if a request is added from a different requester than the /// original @@ -212,7 +227,7 @@ public boost::enable_shared_from_this< RequestPortfolio > { std::map*, double> mass_coeffs_; /// constraints_ is a set because constraints are assumed to be unique - std::set< CapacityConstraint > constraints_; + std::set> constraints_; /// the total quantity of resources assocaited with the portfolio double qty_;