CYCLUS
Loading...
Searching...
No Matches
total_inv_tracker.h
Go to the documentation of this file.
1#ifndef CYCLUS_SRC_TOOLKIT_TOTAL_INV_TRACKER_H_
2#define CYCLUS_SRC_TOOLKIT_TOTAL_INV_TRACKER_H_
3
4#include "error.h"
5#include "res_buf.h"
6
7namespace cyclus {
8namespace toolkit {
9
10/// TotalInvTracker is a helper class that tracks the total inventory of
11/// multiple ResBufs. This can be useful when a single agent uses multiple
12/// buffers to manage their process, but a limit should be placed on their
13/// combined inventory, such as a agent- or facility-wide maximum inventory.
14/// Like ResBufs, TotalInvTracker has an infinite capacity unless explicitly
15/// changed.
16///
17/// TotalInvTracker does not hold any inventory itself, it only tracks the
18/// quantities of other ResBufs. TotalInvTracker currently tracks quantities
19/// alone, not compositions.
20///
21/// @code
22/// class MyAgent : public cyclus:: Facility {
23/// public:
24/// EnterNotify() {
25/// cyclus::Facility::EnterNotify();
26/// tracker_.Init({&inventory_, &outventory_}, facility_max_inv_size_);
27///
28/// cyclus::MatlBuyPolicy p;
29/// p.Init(this, &inventory_, "inventory", &tracker_);
30/// }
31/// protected:
32/// cyclus::toolkit::ResBuf<cyclus::Material> inventory_;
33/// cyclus::toolkit::ResBuf<cyclus::Material> outventory_;
34///
35/// double facility_max_inv_size_ = 1000;
36/// cyclus::toolkit::TotalInvTracker tracker_;
37/// }
38
39class TotalInvTracker {
40 public:
41 /// Creates an uninitialized tracker. The Init function MUST be called before
42 /// the tracker is used.
43 TotalInvTracker() : max_inv_size_(std::numeric_limits<double>::max()), qty_(0) {};
44
45 TotalInvTracker(std::vector<ResBuf<Material>*> bufs,
46 double max_inv_size = std::numeric_limits<double>::max()) {
47 Init(bufs, max_inv_size);
48 }
49
50 ~TotalInvTracker() {};
51
52 /// Initializes the tracker with the given ResBufs. The tracker will have
53 /// infinite capacity unless explicitly changed.
54 void Init(std::vector<ResBuf<Material>*> bufs,
55 double max_inv_size = std::numeric_limits<double>::max()) {
56 if (bufs.size() == 0) {
57 throw ValueError("TotalInvTracker must be initialized with at least one ResBuf");
58 }
59 bufs_ = bufs;
60 if (max_inv_size <= 0) {
61 throw ValueError("TotalInvTracker must be initialized with a positive capacity");
62 }
63 max_inv_size_ = max_inv_size;
64 }
65
66 /// Returns the total quantity of all tracked ResBufs.
67 /// @throws ValueError if the tracker has not been initialized (zero)
68 inline double quantity() {
69 int num = num_bufs();
70 qty_ = 0;
71 for (int i = 0; i < num; i++) {
72 qty_ += bufs_[i]->quantity();
73 }
74 return qty_;
75 }
76
77 /// Returns the total capacity that could go in the ResBufs. Either the
78 /// capacity of the TotalInvTracker, or the sum of each ResBuf's capacity,
79 /// whichever is lower.
80 inline double capacity() {
81 return std::min(total_capacity_bufs(), max_inv_size_);
82 }
83
84 // Returns the sum of the capacities of all buffers. Does not include the
85 // capacity of the tracker
86 inline double total_capacity_bufs() {
87 int num = num_bufs();
88 double cap = 0;
89 for (int i = 0; i < num; i++) {
90 cap += bufs_[i]->capacity();
91 }
92 return cap;
93 }
94
95 /// Returns the total capacity of the traker. Does not include ResBufs
96 inline double tracker_capacity() { return max_inv_size_; }
97
98 /// Returns the remaining facility-wide space across all tracked ResBufs.
99 inline double space() { return std::max(0.0, capacity() - quantity()); }
100
101 /// Returns the remaining space in the given ResBuf, considering the
102 /// facility-wide limitations.
103 inline double constrained_buf_space(ResBuf<Material>* buf) {
104 return std::min(buf->space(), space());
105 }
106
107 /// Returns true if there are no resources in any buffer
108 inline bool empty() { return quantity() == 0; }
109
110 /// Returns number of buffers being tracked
111 /// @throws ValueError if the tracker has not been initialized (zero)
112 inline int num_bufs() {
113 int num = bufs_.size();
114 if (num == 0) {
115 throw ValueError("TotalInvTracker has not been initialized, no buffers to track");
116 }
117 return num;
118 }
119
120 /// Change the total capacity across all ResBufs. The new capacity must be
121 /// greater than the current quantity.
122 /// @throws ValueError if the new capacity is less than the current quantity
123 void set_capacity(double cap) {
124 if (quantity() - cap > eps_rsrc()) {
125 std::stringstream ss;
126 ss << std::setprecision(17) << "new capacity " << cap <<
127 " lower than existing quantity " << quantity();
128 throw ValueError(ss.str());
129 }
130 max_inv_size_ = cap;
131 }
132
133 bool buf_in_tracker(ResBuf<Material>* buf) {
134 for (int i = 0; i < num_bufs(); i++) {
135 if (bufs_[i] == buf) { return true; }
136 }
137 return false;
138 }
139
140 private:
141 double max_inv_size_;
142 double qty_;
143 std::vector<ResBuf<Material>*> bufs_;
144};
145
146} // namespace toolkit
147} // namespace cyclus
148
149#endif // CYCLUS_SRC_TOOLKIT_TOTAL_INV_TRACKER_H_
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
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters