CYCLUS
Loading...
Searching...
No Matches
matl_sell_policy.cc
Go to the documentation of this file.
1#include "matl_sell_policy.h"
2
3#include "error.h"
4#include "comp_math.h"
5
6#define LG(X) LOG(LEV_##X, "selpol")
7#define LGH(X) \
8 LOG(LEV_##X, "selpol") << "policy " << name_ << " (agent " \
9 << Trader::manager()->prototype() << "-" \
10 << Trader::manager()->id() << "): "
11
12namespace cyclus {
13namespace toolkit {
14
16 : Trader(NULL),
17 name_(""),
18 quantize_(0),
19 throughput_(std::numeric_limits<double>::max()),
20 ignore_comp_(false),
21 package_(Package::unpackaged()),
22 transport_unit_(TransportUnit::unrestricted()) {
24 "MatlSellPolicy is experimental and its API may be subject to change");
25}
26
30
31void MatlSellPolicy::set_quantize(double x) {
32 assert(x >= 0);
33 quantize_ = x;
34}
35
36void MatlSellPolicy::set_throughput(double x) {
37 assert(x >= 0);
38 throughput_ = x;
39}
40
41void MatlSellPolicy::set_ignore_comp(bool x) {
42 ignore_comp_ = x;
43}
44
45void MatlSellPolicy::set_package(std::string x) {
46 // if no real context, only unpackaged can be used (keep default)
47 if (manager() != NULL) {
49 std::stringstream ss;
50
51 if (quantize_ > 0 && pkg->name() != Package::unpackaged_name()) {
52 if (pkg->strategy() == "first" || pkg->strategy() == "equal") {
53 std::vector<double> fill = pkg->GetFillMass(quantize_);
54 if (fill.size() > 1 || std::fmod(fill.front(), quantize_) > 0) {
55 ss << "Quantize " << quantize_
56 << " is not fully packagable based on fill min/max values ("
57 << pkg->fill_min() << ", " << pkg->fill_max() << ")";
58 throw ValueError(ss.str());
59 }
60 } else {
61 ss << "Package strategy " << pkg->strategy()
62 << " is not allowed for sell policies with quantize.";
63 throw ValueError(ss.str());
64 }
65 }
66 package_ = pkg;
67 }
68}
69
70void MatlSellPolicy::set_transport_unit(std::string x) {
71 if (manager() != NULL) {
73
74 if ((quantize_ > 0) && (tu->name() != TransportUnit::unrestricted_name())) {
75 std::vector<double> fill = package_->GetFillMass(quantize_);
76 int num_pkgs = fill.size();
77 int max_shippable = tu->MaxShippablePackages(num_pkgs);
78
79 if (max_shippable != num_pkgs) {
80 std::stringstream ss;
81 ss << "Quantize " << quantize_
82 << " packages cannot be shipped according to transport unit fill "
83 << "min/max values (" << tu->fill_min() << ", " << tu->fill_max()
84 << ")";
85 throw ValueError(ss.str());
86 }
87 }
88 transport_unit_ = tu;
89 }
90}
91
93 std::string name) {
95 buf_ = buf;
96 name_ = name;
97 return *this;
98}
99
101 std::string name, double throughput) {
103 buf_ = buf;
104 name_ = name;
105 set_throughput(throughput);
106 return *this;
107}
108
110 std::string name, bool ignore_comp) {
112 buf_ = buf;
113 name_ = name;
114 set_ignore_comp(ignore_comp);
115 return *this;
116}
117
119 std::string name, double throughput,
120 bool ignore_comp) {
122 buf_ = buf;
123 name_ = name;
124 set_throughput(throughput);
125 set_ignore_comp(ignore_comp);
126 return *this;
127}
128
130 std::string name, double throughput,
131 bool ignore_comp, double quantize,
132 std::string package_name,
133 std::string transport_unit_name) {
135 buf_ = buf;
136 name_ = name;
137 set_quantize(quantize);
138 set_throughput(throughput);
139 set_ignore_comp(ignore_comp);
140 set_package(package_name);
141 set_transport_unit(transport_unit_name);
142 return *this;
143}
144
146 commods_.insert(commod);
147 return *this;
148}
149
151 if (manager() == NULL) {
152 std::stringstream ss;
153 ss << "No manager set on Sell Policy " << name_;
154 throw ValueError(ss.str());
155 }
156 manager()->context()->RegisterTrader(this);
157}
158
160 if (manager() == NULL) {
161 std::stringstream ss;
162 ss << "No manager set on Sell Policy " << name_;
163 throw ValueError(ss.str());
164 }
165 manager()->context()->UnregisterTrader(this);
166}
167
168double MatlSellPolicy::Limit() const {
169 double bcap = buf_->quantity();
170 double limit =
171 Excl() ? quantize_ * static_cast<int>(std::floor(bcap / quantize_))
172 : bcap;
173 return std::min(throughput_, limit);
174}
175
176std::set<BidPortfolio<Material>::Ptr> MatlSellPolicy::GetMatlBids(
177 CommodMap<Material>::type& commod_requests) {
178 std::set<BidPortfolio<Material>::Ptr> ports;
179
180 double limit = Limit();
181 if (buf_->empty() || buf_->quantity() < eps() || limit < eps()) return ports;
182
185 port->AddConstraint(cc);
186 ports.insert(port);
187 LGH(INFO3) << "bidding out " << limit << " kg";
188
189 bool excl = Excl();
190 std::string commod;
192 Material::Ptr m, offer;
193 double qty;
194 int n_full_bids = 0;
195 double bid_qty;
196 double remaining_qty;
197 std::vector<double> bids;
198 std::set<std::string>::iterator sit;
199 std::vector<Request<Material>*>::const_iterator rit;
200 for (sit = commods_.begin(); sit != commods_.end(); ++sit) {
201 commod = *sit;
202 if (commod_requests.count(commod) < 1) continue;
203
204 const std::vector<Request<Material>*>& requests =
205 commod_requests.at(commod);
206 for (rit = requests.begin(); rit != requests.end(); ++rit) {
207 req = *rit;
208 qty = std::min(req->target()->quantity(), limit);
209
210 std::vector<double> bids;
211 if (excl) {
212 int n_full_bids = static_cast<int>(std::floor(qty / quantize_));
213 bids.assign(n_full_bids, quantize_);
214 } else {
215 bids = package_->GetFillMass(qty);
216 }
217
218 // check transportability
219 int shippable_pkgs = transport_unit_->MaxShippablePackages(bids.size());
220 if (shippable_pkgs < bids.size()) {
221 // can't ship all bids. Pop the extras.
222 bids.erase(bids.begin() + shippable_pkgs, bids.end());
223 }
224
225 // Peek at resbuf to get current composition
226 m = buf_->Peek();
227
228 std::vector<double>::iterator bit;
229 for (bit = bids.begin(); bit != bids.end(); ++bit) {
230 offer = ignore_comp_
231 ? Material::CreateUntracked(*bit, req->target()->comp())
232 : Material::CreateUntracked(*bit, m->comp());
233 port->AddBid(req, offer, this, excl);
234 LG(INFO3) << " - bid " << *bit << " kg on a request for " << commod;
235 }
236 }
237 }
238 return ports;
239}
240
242 const std::vector<Trade<Material>>& trades,
243 std::vector<std::pair<Trade<Material>, Material::Ptr>>& responses) {
245 std::vector<Trade<Material>>::const_iterator it;
246
247 // confirm that trades are within transport unit limits
248 int shippable_pkgs = transport_unit_->MaxShippablePackages(trades.size());
249
250 double qty;
251
252 for (it = trades.begin(); it != trades.end(); ++it) {
253 if (shippable_pkgs > 0) {
254 qty = it->amt;
255 LGH(INFO3) << " sending " << qty << " kg of " << it->request->commodity();
256 Material::Ptr mat = buf_->Pop(qty, eps_rsrc());
257 Material::Ptr trade_mat;
258
259 // don't go through packaging if you don't need to. packaging always bumps
260 // resource ids and records on resources table, which is not necessary
261 // when nothing is happening
262 if (package_->name() != mat->package_name()) { // packaging needed
263 std::vector<Material::Ptr> mat_pkgd = mat->Package<Material>(package_);
264
265 if (mat->quantity() > eps_rsrc()) {
266 // push any extra material that couldn't be packaged back onto buffer
267 // don't push unless there's leftover material
268 buf_->Push(mat);
269 }
270 if (mat_pkgd.size() > 0) {
271 // packaging successful
272 trade_mat = mat_pkgd[0];
273 shippable_pkgs -= 1;
274 } else {
275 // packaging failed. Will need to ship empty trade
276 trade_mat = Material::CreateUntracked(0, mat->comp());
277 }
278
279 } else { // no packaging needed
280 trade_mat = mat;
281 }
282
283 if (ignore_comp_ &&
284 compmath::AlmostEq(it->request->target()->comp()->mass(),
285 trade_mat->comp()->mass(), eps_rsrc())) {
286 trade_mat->Transmute(it->request->target()->comp());
287 }
288 responses.push_back(std::make_pair(*it, trade_mat));
289 }
290 }
291}
292
293} // namespace toolkit
294} // namespace cyclus
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:50
Context * context() const
Returns this agent's simulation context.
Definition agent.h:364
A BidPortfolio is a collection of bids as responses to requests for resources and associated constrai...
void AddConstraint(const CapacityConstraint< T > &c)
add a capacity constraint associated with the portfolio
Bid< T > * AddBid(Request< T > *request, boost::shared_ptr< T > offer, Trader *bidder, bool exclusive, double preference)
add a bid to the portfolio
boost::shared_ptr< BidPortfolio< T > > Ptr
A CapacityConstraint provides an ability to determine an agent's constraints on resource allocation g...
boost::shared_ptr< Composition > Ptr
Definition composition.h:43
void UnregisterTrader(Trader *e)
Unregisters an agent as a participant in resource exchanges.
Definition context.h:181
void RegisterTrader(Trader *e)
Registers an agent as a participant in resource exchanges.
Definition context.h:178
Package::Ptr GetPackage(std::string name)
Retrieve a registered package.
Definition context.cc:219
TransportUnit::Ptr GetTransportUnit(std::string name)
Retrieve a registered transport unit.
Definition context.cc:254
The material class is primarily responsible for enabling basic material manipulation while helping en...
Definition material.h:71
static Ptr CreateUntracked(double quantity, Composition::Ptr c, double unit_value=kUnsetUnitValue)
Creates a new material resource that does not actually exist as part of the simulation and is untrack...
Definition material.cc:26
boost::shared_ptr< Material > Ptr
Definition material.h:75
Package is a class that packages materials into discrete items in ways.
Definition package.h:19
boost::shared_ptr< Package > Ptr
Definition package.h:21
static std::string unpackaged_name()
Definition package.h:68
A Request encapsulates all the information required to communicate the needs of an agent in the Dynam...
Definition request.h:29
boost::shared_ptr< T > target() const
Definition request.h:97
Trader(Agent *manager)
Definition trader.h:26
Agent * manager_
Definition trader.h:82
virtual Agent * manager()
Definition trader.h:28
TransportUnit is a class that can be used in conjunction with packages to restrict the amount of mate...
Definition package.h:129
static std::string unrestricted_name()
Definition package.h:161
boost::shared_ptr< TransportUnit > Ptr
Definition package.h:131
For values that are too big, too small, etc.
Definition error.h:37
MatlSellPolicy()
Creates an uninitialized policy.
virtual void GetMatlTrades(const std::vector< Trade< Material > > &trades, std::vector< std::pair< Trade< Material >, Material::Ptr > > &responses)
default implementation for responding to material trades
virtual std::set< BidPortfolio< Material >::Ptr > GetMatlBids(CommodMap< Material >::type &commod_requests)
Trader Methods.
MatlSellPolicy & Init(Agent *manager, ResBuf< Material > *buf, std::string name)
Configures the policy to keep buf empty every time step by offering away as much material as possible...
void Stop()
Unregisters this policy as a trader in the current simulation.
MatlSellPolicy & Set(std::string commod)
Instructs the policy to empty its buffer with offers on the given commodity.
void Start()
Registers this policy as a trader in the current simulation.
bool Excl() const
whether trades will be denoted as exclusive or not
double Limit() const
the current (total) limit on transactions, i.e., the quantity of resources that can be transacted in ...
ResBuf is a helper class that provides semi-automated management of a collection of resources (e....
Definition res_buf.h:61
#define LG(X)
#define LGH(X)
bool AlmostEq(const CompMap &v1, const CompMap &v2, double threshold)
Returns true if all nuclides of v1 and v2 are the same within threshold.
Definition comp_math.cc:93
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
double eps_rsrc()
an epsilon value to be used by resources
Definition cyc_limits.h:19
double eps()
a generic epsilon value
Definition cyc_limits.h:12
void Warn(const std::string &msg)
Issue a warning with the approriate message, accoring to the current warning settings.
Definition error.h:108
std::map< std::string, std::vector< Request< T > * > > type
A Trade is a simple container that associates a request for a resource with a bid for that resource.
Definition trade.h:15