7 #include <boost/math/special_functions/next.hpp> 37 if (conditioner_ != NULL)
42 if (conditioner_ != NULL)
50 std::mem_fun(&GreedySolver::GetCaps),
56 std::mem_fun(&GreedySolver::GetCaps),
72 std::mem_fun(&GreedySolver::GreedilySatisfySet),
75 obj_ += unmatched_ * pseudo_cost;
88 << std::min(ucap, vcap);
90 return std::min(ucap, vcap);
95 if (n->group == NULL) {
99 if (n->unit_capacities[a].size() == 0) {
100 return n->qty - curr_qty;
103 std::vector<double>& unit_caps = n->unit_capacities[a];
104 const std::vector<double>& group_caps = grp_caps_[n->group];
105 std::vector<double> caps;
106 double grp_cap, u_cap, cap;
108 for (
int i = 0; i < unit_caps.size(); i++) {
109 grp_cap = group_caps[i];
110 u_cap = unit_caps[i];
111 cap = grp_cap / u_cap;
118 if (grp_cap == std::numeric_limits<double>::max()) {
119 caps.push_back(std::numeric_limits<double>::max());
126 cap = *std::min_element(caps.begin(), caps.end());
128 cap = *std::max_element(caps.begin(), caps.end());
130 return std::min(cap, n->qty - curr_qty);
134 for (
int i = 0; i != g->nodes().size(); i++) {
135 n_qty_[g->nodes()[i]] = 0;
137 grp_caps_[g.get()] = g->capacities();
141 std::vector<ExchangeNode::Ptr>& nodes = prs->nodes();
142 std::stable_sort(nodes.begin(), nodes.end(),
AvgPrefComp);
144 std::vector<ExchangeNode::Ptr>::iterator req_it = nodes.begin();
145 double target = prs->qty();
149 std::vector<Arc>::const_iterator arc_it;
150 std::vector<Arc> sorted;
151 double remain, tomatch, excl_val;
154 <<
" amount of a resource.";
156 while ((match <= target) && (req_it != nodes.end())) {
162 sorted = std::vector<Arc>(arcs);
163 std::stable_sort(sorted.begin(), sorted.end(),
ReqPrefComp);
164 arc_it = sorted.begin();
166 while ((match <= target) && (arc_it != sorted.end())) {
167 remain = target - match;
168 const Arc& a = *arc_it;
172 tomatch = std::min(remain,
Capacity(a, n_qty_[u], n_qty_[v]));
175 if (arc_it->exclusive()) {
180 double dist = boost::math::float_distance(tomatch, excl_val);
188 if (tomatch >
eps()) {
190 <<
" amount of a resource.";
191 UpdateCapacity(u, a, tomatch);
192 UpdateCapacity(v, a, tomatch);
193 n_qty_[u] += tomatch;
194 n_qty_[v] += tomatch;
198 UpdateObj(tomatch, u->prefs[a]);
206 unmatched_ += target - match;
209 void GreedySolver::UpdateObj(
double qty,
double pref) {
220 std::vector<double>& unit_caps = n->unit_capacities[a];
221 std::vector<double>& caps = grp_caps_[n->group];
222 assert(unit_caps.size() == caps.size());
223 for (
int i = 0; i < caps.size(); i++) {
224 double prev = caps[i];
228 caps[i] = (prev == std::numeric_limits<double>::max()) ?
229 std::numeric_limits<double>::max() :
230 prev - qty * unit_caps[i];
236 std::stringstream ss;
237 ss <<
"A bid for " << n->commod <<
" was set at " << n->qty
238 <<
" but has been matched to a higher value " << qty
239 <<
". This could be due to a problem with your " 240 <<
"bid portfolio constraints.";
bool AvgPrefComp(ExchangeNode::Ptr l, ExchangeNode::Ptr r)
A comparison function for sorting a container of Nodes by the nodes preference in decensing order (i...
double Capacity(const Arc &a, double u_curr_qty, double v_curr_qty)
the capacity of the arc
void Condition()
Uses the provided (or a default) GreedyPreconditioner to condition the solver's ExchangeGraph so that...
An arc represents a possible connection between two nodes in the bipartite resource exchange graph...
For failed object state expectations.
For values that are too big, too small, etc.
boost::shared_ptr< RequestGroup > Ptr
boost::shared_ptr< ExchangeNode > unode() const
const std::vector< ExchangeNodeGroup::Ptr > & supply_groups() const
bool ReqPrefComp(const Arc &l, const Arc &r)
double Capacity(const Arc& a, double u_curr_qty, double v_curr_qty) { return 0; } ...
boost::shared_ptr< ExchangeNode > Ptr
void Init()
Initialize member values based on the given graph.
boost::shared_ptr< ExchangeNodeGroup > Ptr
boost::shared_ptr< ExchangeNode > vnode() const
static const double float_ulp_eq
distance in ULP within which floating point numbers should be considered equal.
virtual double SolveGraph()
the GreedySolver solves an ExchangeGraph by iterating over each RequestGroup and matching requests wi...
bool IsNegative(double d)
returns true if a double is less than 0 - eps()
A GreedyPreconditioner conditions an ExchangeGraph for a GreedySolver by ordering the RequestGroups a...
const std::vector< RequestGroup::Ptr > & request_groups() const
void AddMatch(const Arc &a, double qty)
adds a match for a quanity of flow along an arc
void Capacity(cyclus::Arc const &, double, double)
GreedySolver()
GreedySolver constructor.
Code providing rudimentary logging capability for the Cyclus core.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
debugging information - least verbose
a very simple interface for solving translated resource exchanges
void Condition(ExchangeGraph *graph)
conditions the graph as described above
const std::map< ExchangeNode::Ptr, std::vector< Arc > > & node_arc_map() const
double PseudoCost()
Calculates the ratio of the maximum objective coefficient to minimum unit capacity plus an added cost...
double eps()
a generic epsilon value