7#include <boost/math/special_functions/next.hpp>
37 if (conditioner_ != NULL)
42 if (conditioner_ != NULL)
50 &GreedySolver::GetCaps,
52 std::placeholders::_1));
57 &GreedySolver::GetCaps,
59 std::placeholders::_1));
74 &GreedySolver::GreedilySatisfySet,
76 std::placeholders::_1));
78 obj_ += unmatched_ * pseudo_cost;
91 << std::min(ucap, vcap);
93 return std::min(ucap, vcap);
98 if (n->group == NULL) {
102 if (n->unit_capacities[a].size() == 0) {
103 return n->qty - curr_qty;
106 std::vector<double>& unit_caps = n->unit_capacities[a];
107 const std::vector<double>& group_caps = grp_caps_[n->group];
108 std::vector<double> caps;
109 double grp_cap, u_cap, cap;
111 for (
int i = 0; i < unit_caps.size(); i++) {
112 grp_cap = group_caps[i];
113 u_cap = unit_caps[i];
114 cap = grp_cap / u_cap;
121 if (grp_cap == std::numeric_limits<double>::max()) {
122 caps.push_back(std::numeric_limits<double>::max());
129 cap = *std::min_element(caps.begin(), caps.end());
131 cap = *std::max_element(caps.begin(), caps.end());
133 return std::min(cap, n->qty - curr_qty);
137 for (
int i = 0; i != g->nodes().size(); i++) {
138 n_qty_[g->nodes()[i]] = 0;
140 grp_caps_[g.get()] = g->capacities();
144 std::vector<ExchangeNode::Ptr>& nodes = prs->nodes();
145 std::stable_sort(nodes.begin(), nodes.end(),
AvgPrefComp);
147 std::vector<ExchangeNode::Ptr>::iterator req_it = nodes.begin();
148 double target = prs->qty();
152 std::vector<Arc>::const_iterator arc_it;
153 std::vector<Arc> sorted;
154 double remain, tomatch, excl_val;
157 <<
" amount of a resource.";
159 while ((match <= target) && (req_it != nodes.end())) {
165 sorted = std::vector<Arc>(arcs);
166 std::stable_sort(sorted.begin(), sorted.end(),
ReqPrefComp);
167 arc_it = sorted.begin();
169 while ((match <= target) && (arc_it != sorted.end())) {
170 remain = target - match;
171 const Arc& a = *arc_it;
175 tomatch = std::min(remain,
Capacity(a, n_qty_[u], n_qty_[v]));
178 if (arc_it->exclusive()) {
179 excl_val = a.excl_val();
183 double dist = boost::math::float_distance(tomatch, excl_val);
191 if (tomatch >
eps()) {
193 <<
" amount of a resource.";
194 UpdateCapacity(u, a, tomatch);
195 UpdateCapacity(v, a, tomatch);
196 n_qty_[u] += tomatch;
197 n_qty_[v] += tomatch;
201 UpdateObj(tomatch, u->prefs[a]);
209 unmatched_ += target - match;
212void GreedySolver::UpdateObj(
double qty,
double pref) {
223 std::vector<double>& unit_caps = n->unit_capacities[a];
224 std::vector<double>& caps = grp_caps_[n->group];
225 assert(unit_caps.size() == caps.size());
226 for (
int i = 0; i < caps.size(); i++) {
227 double prev = caps[i];
231 caps[i] = (prev == std::numeric_limits<double>::max()) ?
232 std::numeric_limits<double>::max() :
233 prev - qty * unit_caps[i];
239 std::stringstream ss;
240 ss <<
"A bid for " << n->commod <<
" was set at " << n->qty
241 <<
" but has been matched to a higher value " << qty
242 <<
". This could be due to a problem with your "
243 <<
"bid portfolio constraints.";
244 throw ValueError(ss.str());
An arc represents a possible connection between two nodes in the bipartite resource exchange graph.
boost::shared_ptr< ExchangeNode > vnode() const
boost::shared_ptr< ExchangeNode > unode() const
const std::vector< ExchangeNodeGroup::Ptr > & supply_groups() const
const std::map< ExchangeNode::Ptr, std::vector< Arc > > & node_arc_map() const
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
boost::shared_ptr< ExchangeNodeGroup > Ptr
a very simple interface for solving translated resource exchanges
double PseudoCost()
Calculates the ratio of the maximum objective coefficient to minimum unit capacity plus an added cost...
A GreedyPreconditioner conditions an ExchangeGraph for a GreedySolver by ordering the RequestGroups a...
void Condition(ExchangeGraph *graph)
conditions the graph as described above
virtual double SolveGraph()
the GreedySolver solves an ExchangeGraph by iterating over each RequestGroup and matching requests wi...
GreedySolver()
GreedySolver constructor.
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...
void Init()
Initialize member values based on the given graph.
boost::shared_ptr< RequestGroup > Ptr
For failed object state expectations.
For values that are too big, too small, etc.
Code providing rudimentary logging capability for the Cyclus core.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
void Capacity(cyclus::Arc const &, double, double)
bool IsNegative(double d)
returns true if a double is less than 0 - eps()
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....
bool ReqPrefComp(const Arc &l, const Arc &r)
double Capacity(const Arc& a, double u_curr_qty, double v_curr_qty) { return 0; }
@ LEV_DEBUG1
debugging information - least verbose
double eps()
a generic epsilon value
static const double float_ulp_eq
distance in ULP within which floating point numbers should be considered equal.
boost::shared_ptr< ExchangeNode > Ptr