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
31
32void MatlSellPolicy::set_quantize(double x) {
33 assert(x >= 0);
34 quantize_ = x;
35}
36
37void MatlSellPolicy::set_throughput(double x) {
38 assert(x >= 0);
39 throughput_ = x;
40}
41
42void MatlSellPolicy::set_ignore_comp(bool x) {
43 ignore_comp_ = x;
44}
45
46void MatlSellPolicy::set_package(std::string x) {
47 // if no real context, only unpackaged can be used (keep default)
48 if (manager() != NULL) {
50 std::stringstream ss;
51
52 if (quantize_ > 0 && pkg->name() != Package::unpackaged_name()) {
53 if (pkg->strategy() == "first" || pkg->strategy() == "equal") {
54 std::vector<double> fill = pkg->GetFillMass(quantize_);
55 if (fill.size() > 1 || std::fmod(fill.front(), quantize_) > 0) {
56 ss << "Quantize " << quantize_
57 << " is not fully packagable based on fill min/max values ("
58 << pkg->fill_min() << ", " << pkg->fill_max() << ")";
59 throw ValueError(ss.str());
60 }
61 } else {
62 ss << "Package strategy " << pkg->strategy()
63 << " is not allowed for sell policies with quantize.";
64 throw ValueError(ss.str());
65 }
66 }
67 package_ = pkg;
68 }
69}
70
71void MatlSellPolicy::set_transport_unit(std::string x) {
72 if (manager() != NULL) {
74
75 if ((quantize_ > 0) && (tu->name() != TransportUnit::unrestricted_name())) {
76 std::vector<double> fill = package_->GetFillMass(quantize_);
77 int num_pkgs = fill.size();
78 int max_shippable = tu->MaxShippablePackages(num_pkgs);
79
80 if (max_shippable != num_pkgs) {
81 std::stringstream ss;
82 ss << "Quantize " << quantize_
83 << " packages cannot be shipped according to transport unit fill "
84 << "min/max values ("
85 << tu->fill_min() << ", " << tu->fill_max() << ")";
86 throw ValueError(ss.str());
87 }
88 }
89 transport_unit_ = tu;
90 }
91}
92
94 std::string name) {
96 buf_ = buf;
97 name_ = name;
98 return *this;
99}
100
102 std::string name, double throughput) {
104 buf_ = buf;
105 name_ = name;
106 set_throughput(throughput);
107 return *this;
108}
109
111 std::string name, bool ignore_comp) {
113 buf_ = buf;
114 name_ = name;
115 set_ignore_comp(ignore_comp);
116 return *this;
117}
118
120 std::string name, double throughput,
121 bool ignore_comp) {
123 buf_ = buf;
124 name_ = name;
125 set_throughput(throughput);
126 set_ignore_comp(ignore_comp);
127 return *this;
128}
129
131 std::string name, double throughput,
132 bool ignore_comp, double quantize,
133 std::string package_name,
134 std::string transport_unit_name) {
136 buf_ = buf;
137 name_ = name;
138 set_quantize(quantize);
139 set_throughput(throughput);
140 set_ignore_comp(ignore_comp);
141 set_package(package_name);
142 set_transport_unit(transport_unit_name);
143 return *this;
144}
145
147 commods_.insert(commod);
148 return *this;
149}
150
152 if (manager() == NULL) {
153 std::stringstream ss;
154 ss << "No manager set on Sell Policy " << name_;
155 throw ValueError(ss.str());
156 }
157 manager()->context()->RegisterTrader(this);
158}
159
161 if (manager() == NULL) {
162 std::stringstream ss;
163 ss << "No manager set on Sell Policy " << name_;
164 throw ValueError(ss.str());
165 }
166 manager()->context()->UnregisterTrader(this);
167}
168
169
170double MatlSellPolicy::Limit() const {
171 double bcap = buf_->quantity();
172 double limit = Excl() ? \
173 quantize_ * static_cast<int>(std::floor(bcap / quantize_)) : bcap;
174 return std::min(throughput_, limit);
175}
176
177std::set<BidPortfolio<Material>::Ptr> MatlSellPolicy::GetMatlBids(
178 CommodMap<Material>::type& commod_requests) {
179 std::set<BidPortfolio<Material>::Ptr> ports;
180
181
182 double limit = Limit();
183 if (buf_->empty() || buf_->quantity() < eps()|| limit < eps())
184 return ports;
185
188 port->AddConstraint(cc);
189 ports.insert(port);
190 LGH(INFO3) << "bidding out " << limit << " kg";
191
192 bool excl = Excl();
193 std::string commod;
195 Material::Ptr m, offer;
196 double qty;
197 int n_full_bids = 0;
198 double bid_qty;
199 double remaining_qty;
200 std::vector<double> bids;
201 std::set<std::string>::iterator sit;
202 std::vector<Request<Material>*>::const_iterator rit;
203 for (sit = commods_.begin(); sit != commods_.end(); ++sit) {
204 commod = *sit;
205 if (commod_requests.count(commod) < 1)
206 continue;
207
208 const std::vector<Request<Material>*>& requests =
209 commod_requests.at(commod);
210 for (rit = requests.begin(); rit != requests.end(); ++rit) {
211 req = *rit;
212 qty = std::min(req->target()->quantity(), limit);
213
214 std::vector<double> bids;
215 if (excl) {
216 int n_full_bids = static_cast<int>(std::floor(qty / quantize_));
217 bids.assign(n_full_bids, quantize_);
218 } else {
219 bids = package_->GetFillMass(qty);
220 }
221
222 // check transportability
223 int shippable_pkgs = transport_unit_->MaxShippablePackages(bids.size());
224 if (shippable_pkgs < bids.size()) {
225 // can't ship all bids. Pop the extras.
226 bids.erase(bids.begin() + shippable_pkgs, bids.end());
227 }
228
229 // Peek at resbuf to get current composition
230 m = buf_->Peek();
231
232 std::vector<double>::iterator bit;
233 for (bit = bids.begin(); bit != bids.end(); ++bit) {
234 offer = ignore_comp_ ? \
235 Material::CreateUntracked(*bit, req->target()->comp()) : \
236 Material::CreateUntracked(*bit, m->comp());
237 port->AddBid(req, offer, this, excl);
238 LG(INFO3) << " - bid " << *bit << " kg on a request for " << commod;
239 }
240 }
241 }
242 return ports;
243}
244
246 const std::vector<Trade<Material> >& trades,
247 std::vector<std::pair<Trade<Material>, Material::Ptr> >& responses) {
249 std::vector<Trade<Material> >::const_iterator it;
250
251 // confirm that trades are within transport unit limits
252 int shippable_pkgs = transport_unit_->MaxShippablePackages(trades.size());
253
254 double qty;
255
256 for (it = trades.begin(); it != trades.end(); ++it) {
257 if (shippable_pkgs > 0){
258 qty = it->amt;
259 LGH(INFO3) << " sending " << qty << " kg of " << it->request->commodity();
260 Material::Ptr mat = buf_->Pop(qty, eps_rsrc());
262
263 // don't go through packaging if you don't need to. packaging always bumps
264 // resource ids and records on resources table, which is not necessary
265 // when nothing is happening
266 if (package_->name() != mat->package_name()) { // packaging needed
267 std::vector<Material::Ptr> mat_pkgd = mat->Package<Material>(package_);
268
269 if (mat->quantity() > eps_rsrc()) {
270 // push any extra material that couldn't be packaged back onto buffer
271 // don't push unless there's leftover material
272 buf_->Push(mat);
273 }
274 if (mat_pkgd.size() > 0) {
275 // packaging successful
276 trade_mat = mat_pkgd[0];
278 } else {
279 // packaging failed. Will need to ship empty trade
281 }
282
283 } else { // no packaging needed
284 trade_mat = mat;
285 }
286
287 if (ignore_comp_ && compmath::AlmostEq(it->request->target()->comp()->mass(), trade_mat->comp()->mass(), eps_rsrc())) {
288 trade_mat->Transmute(it->request->target()->comp());
289 }
290 responses.push_back(std::make_pair(*it, trade_mat));
291 }
292 }
293}
294
295} // namespace toolkit
296} // namespace cyclus
The abstract base class used by all types of agents that live and interact in a simulation.
Definition agent.h:49
Context * context() const
Returns this agent's simulation context.
Definition agent.h:367
A BidPortfolio is a collection of bids as responses to requests for resources and associated constrai...
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:176
Package::Ptr GetPackage(std::string name)
Retrieve a registered package.
Definition context.cc:214
TransportUnit::Ptr GetTransportUnit(std::string name)
Retrieve a registered transport unit.
Definition context.cc:249
The material class is primarily responsible for enabling basic material manipulation while helping en...
Definition material.h:71
boost::shared_ptr< Material > Ptr
Definition material.h:75
static Ptr CreateUntracked(double quantity, Composition::Ptr c)
Creates a new material resource that does not actually exist as part of the simulation and is untrack...
Definition material.cc:24
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
A simple API for agents that wish to exchange resources in the simulation.
Definition trader.h:24
Agent * manager_
Definition trader.h:88
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:134
static std::string unrestricted_name()
Definition package.h:166
boost::shared_ptr< TransportUnit > Ptr
Definition package.h:136
For values that are too big, too small, etc.
Definition error.h:41
MatlSellPolicy performs semi-automatic inventory management of a material buffer by making offers and...
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:62
#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
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters
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:16