CYCLUS
Loading...
Searching...
No Matches
package.cc
Go to the documentation of this file.
1#include "package.h"
2#include "error.h"
3#include "cyc_limits.h"
4
5namespace cyclus {
6
7Package::Ptr Package::unpackaged_ = NULL;
8
9Package::Ptr Package::Create(std::string name, double fill_min,
10 double fill_max,std::string strategy) {
11 if (fill_min < 0 || fill_max < 0) {
12 throw ValueError("fill_min and fill_max must be non-negative");
13 }
14 else if (fill_min > fill_max) {
15 throw ValueError("fill_min must be less than or equal to fill_max");
16 }
18 return p;
19}
20
21// singleton pattern:
22// if the static member is not yet set, create a new object
23// otherwise return the object that already exists
25
26 if (!unpackaged_) {
27 unpackaged_ = Ptr(new Package(unpackaged_name_));
28 }
29
30 return unpackaged_;
31}
32
34 if (strategy_ == "uniform") {
35 dist_ = UniformDoubleDist::Ptr (new UniformDoubleDist(fill_min_, fill_max_));
36 } else if (strategy_ == "normal") {
38 (fill_min_ + fill_max_) / 2,
39 (fill_max_ - fill_min_) / 6,
40 fill_min_,
41 fill_max_));
42 }
43}
44
45std::vector<double> Package::GetFillMass(double qty) {
46 std::vector<double> packages;
47 if ((qty - fill_min_) < -eps_rsrc()) {
48 // less than one pkg of material available
49 return packages;
50 }
51
52 // simple check for whether vector limits *might* be exceeded
53 ExceedsSplitLimits(qty / fill_max_);
54
55 double fill_mass;
57
58 if (strategy_ == "first") {
59 fill_mass = fill_max_;
60 } else if (strategy_ == "equal") {
61 int num_min_fill = std::floor(qty / fill_min_);
62 int num_max_fill = std::ceil(qty / fill_max_);
64 // all material can fit in package(s)
65 fill_mass = qty / num_max_fill;
66 } else {
67 // some material will remain unpackaged, fill up as many max packages as possible
68 fill_mass = fill_max_;
69 }
70 }
71
72 if (strategy_ == "first" || strategy_ == "equal") {
73 fill_mass = std::min(qty, fill_mass);
74 num_at_fill_mass = static_cast<int>(std::floor(qty / fill_mass));
77
79 }
80
81 if (strategy_ == "uniform" || strategy_ == "normal") {
82 // only use random if a full package amount is available. if less than one
83 // full amount is available, below will fill a partial package (no random).
84 while ( qty >= std::max(eps_rsrc(), fill_max_) ) {
85 fill_mass = dist_->sample();
86 packages.push_back(fill_mass);
87 qty -= fill_mass;
88 }
89 }
90
91 if (qty >= std::max(eps_rsrc(),fill_min_) ) {
92 // leftover material is enough to fill one more partial package.
93 packages.push_back(qty);
94 }
95
97
98 return packages;
99}
100
101Package::Package(std::string name, double fill_min, double fill_max,
102 std::string strategy) :
103 name_(name), fill_min_(fill_min), fill_max_(fill_max), strategy_(strategy) {
104 if (name == unpackaged_name_) {
105 if (unpackaged_) {
106 throw ValueError("can't create a new package with name 'unpackaged'");
107 }
108 }
109 if (!IsValidStrategy(strategy_)) {
110 throw ValueError("Invalid strategy for package: " + strategy_);
111 }
113}
114
115TransportUnit::Ptr TransportUnit::unrestricted_ = NULL;
116
117TransportUnit::Ptr TransportUnit::Create(std::string name, int fill_min, int fill_max, std::string strategy) {
118 if (fill_min < 0 || fill_max < 0) {
119 throw ValueError("fill_min and fill_max must be non-negative");
120 }
121 else if (fill_min > fill_max) {
122 throw ValueError("fill_min must be less than or equal to fill_max");
123 }
125 return t;
126}
127
128// singleton pattern:
129// if the static member is not yet set, create a new object
130// otherwise return the object that already exists
132 if (!unrestricted_) {
133 unrestricted_ = Ptr(new TransportUnit(unrestricted_name_));
134 }
135 return unrestricted_;
136}
137
139 if (qty < fill_min_) {
140 // less than one TransportUnit of material available
141 return 0;
142 }
143
144 if (strategy_ == "first") {
145 return fill_max_;
146 } else if (strategy_ == "equal" || strategy_ == "hybrid") {
147 // int division automatically rounds down. don't need floor in min, and
148 // get ceil by hand instead
149 int num_at_min_fill = qty / fill_min_;
150 int num_at_max_fill = (qty + fill_max_ - 1) / fill_max_;
151
153 // all material *might* fit transport units. However, this is more
154 // challenging than packages because transport units are discrete.
155 double dbl_fill_mass = (double)qty / (double)num_at_max_fill;
156 return std::floor(dbl_fill_mass);
157 }
158 // some material will remain unrestricted, fill up as many transport
159 // units as possible. Or, perfect fill is possible but not with integer
160 // fill (see above).
161 }
162 return fill_max_;
163}
164
166 int TU_fill;
167 int shippable = 0;
168
169 if (pkgs == 0 || pkgs < fill_min_) {
170 return 0;
171
172 } else if (name_ == unrestricted_name_) {
173 return pkgs;
174
175 } else if (strategy_ == "first" || strategy_ == "equal") {
177 if (TU_fill == 0) {
178 return 0;
179 }
180 shippable = std::min(pkgs, (pkgs / TU_fill) * TU_fill);
181 return shippable;
182
183 } else if (strategy_ == "hybrid") {
184 while ((pkgs > 0) && (pkgs >= fill_min_)) {
187 pkgs -= TU_fill;
188 }
189 return shippable;
190 }
191 return 0;
192}
193
194TransportUnit::TransportUnit(std::string name, int fill_min, int fill_max, std::string strategy) :
195 name_(name), fill_min_(fill_min), fill_max_(fill_max), strategy_(strategy) {
196 if (name == unrestricted_name_) {
197 if (unrestricted_) {
198 throw ValueError("can't create a new transport unit with name 'unrestricted'");
199 }
200 }
201 if (strategy != "first" && strategy != "equal" && strategy != "hybrid") {
202 throw ValueError("Invalid strategy for transport unit: " + strategy_);
203 }
204}
205
206} // namespace cyclus
boost::shared_ptr< NormalDoubleDist > Ptr
Package is a class that packages materials into discrete items in ways.
Definition package.h:19
static void ExceedsSplitLimits(int pkgs, std::string ss_extra="")
Definition package.h:73
static Ptr Create(std::string name, double fill_min=0, double fill_max=std::numeric_limits< double >::max(), std::string strategy="first")
Definition package.cc:9
static Ptr & unpackaged()
Definition package.cc:24
boost::shared_ptr< Package > Ptr
Definition package.h:21
std::string name() const
Definition package.h:59
std::vector< double > GetFillMass(double qty)
Returns optimal fill mass for a resource to be packaged, and number of packages that can be crated at...
Definition package.cc:45
void SetDistribution()
Definition package.cc:33
double fill_min() const
Definition package.h:61
double fill_max() const
Definition package.h:63
std::string strategy() const
Definition package.h:65
bool IsValidStrategy(std::string strategy)
Definition package.h:98
TransportUnit is a class that can be used in conjunction with packages to restrict the amount of mate...
Definition package.h:134
std::string strategy() const
Definition package.h:160
int fill_max() const
Definition package.h:158
int fill_min() const
Definition package.h:156
int GetTransportUnitFill(int qty)
Returns number of packages for each transport unit.
Definition package.cc:138
boost::shared_ptr< TransportUnit > Ptr
Definition package.h:136
int MaxShippablePackages(int pkgs)
Returns the max number of transport units that can be shipped from the available quantity.
Definition package.cc:165
static Ptr Create(std::string name, int fill_min=0, int fill_max=std::numeric_limits< int >::max(), std::string strategy="first")
create a new transport unit type.
Definition package.cc:117
static Ptr & unrestricted()
Definition package.cc:131
std::string name() const
Definition package.h:154
boost::shared_ptr< UniformDoubleDist > Ptr
For values that are too big, too small, etc.
Definition error.h:41
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