8#include <boost/lambda/bind.hpp>
13namespace l = boost::lambda;
17inline double SumPref(
double total, std::pair<Arc, double> pref) {
18 return total += pref.second;
22 std::map<Arc, double>& prefs = n->prefs;
23 return prefs.size() > 0 ?
24 std::accumulate(prefs.begin(), prefs.end(), 0.0,
SumPref) / prefs.size() :
31 const std::map<std::string, double>& commod_weights)
32 : commod_weights_(commod_weights) {
33 if (commod_weights_.size() != 0)
38 const std::map<std::string, double>& commod_weights,
40 : commod_weights_(commod_weights) {
41 if (commod_weights_.size() != 0)
42 ProcessWeights_(order);
48 std::vector<RequestGroup::Ptr>& groups =
49 const_cast<std::vector<RequestGroup::Ptr>&
>(graph->
request_groups());
51 std::vector<RequestGroup::Ptr>::iterator it;
52 for (it = groups.begin(); it != groups.end(); ++it) {
53 std::vector<ExchangeNode::Ptr>& nodes =
54 const_cast<std::vector<ExchangeNode::Ptr>&
>((*it)->nodes());
57 for (
int i = 0; i != nodes.size(); i++) {
58 avg_prefs_[nodes[i]] =
AvgPref(nodes[i]);
63 nodes.begin(), nodes.end(),
67 group_weights_[*it] =
GroupWeight(*it, &commod_weights_, &avg_prefs_);
68 CLOG(
LEV_DEBUG1) <<
"Group weight value during graph preconditioning is "
69 << group_weights_[*it] <<
".";
74 groups.begin(), groups.end(),
78 group_weights_.clear();
82void GreedyPreconditioner::ProcessWeights_(WgtOrder order) {
83 double min = std::min_element(
84 commod_weights_.begin(),
85 commod_weights_.end(),
86 SecondLT< std::pair<std::string, double> >())->second;
88 double max = std::max_element(
89 commod_weights_.begin(),
90 commod_weights_.end(),
91 SecondLT< std::pair<std::string, double> >())->second;
93 assert(commod_weights_.size() == 0 || min >= 0);
95 std::map<std::string, double>::iterator it;
98 for (it = commod_weights_.begin();
99 it != commod_weights_.end();
101 it->second = max + min - it->second;
108 for (it = commod_weights_.begin();
109 it != commod_weights_.end();
111 CLOG(
LEV_INFO1) <<
"GreedyPreconditioner commodity weight value for "
113 <<
" is " << it->second;
119 std::map<std::string, double>* weights,
120 std::map<ExchangeNode::Ptr, double>* avg_prefs) {
121 std::vector<ExchangeNode::Ptr>& nodes = g->nodes();
125 for (
int i = 0; i != nodes.size(); i++) {
127 sum +=
NodeWeight(n, weights, (*avg_prefs)[n]);
129 return nodes.size() > 0 ? sum / nodes.size() : 0;
134 std::map<std::string, double>* weights,
136 double commod_weight = (weights->size() != 0) ? (*weights)[n->commod] : 1;
137 double node_weight = commod_weight * (1 + avg_pref / (1 + avg_pref));
An ExchangeGraph is a resource-neutral representation of a ResourceExchange.
const std::vector< RequestGroup::Ptr > & request_groups() const
void Condition(ExchangeGraph *graph)
conditions the graph as described above
GreedyPreconditioner()
constructor if weights are given in heaviest-first order
WgtOrder
the order of commodity weights
@ END
a flag for commodity weights given in the reverse order,
bool NodeComp(const ExchangeNode::Ptr l, const ExchangeNode::Ptr r)
a comparitor for ordering containers of ExchangeNode::Ptrs in descending order based on their commodi...
bool GroupComp(const RequestGroup::Ptr l, const RequestGroup::Ptr r)
a comparitor for ordering containers of Request::Ptrs in descending order based on their average comm...
boost::shared_ptr< RequestGroup > Ptr
Code providing rudimentary logging capability for the Cyclus core.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
double AvgPref(ExchangeNode::Ptr n)
@ LEV_DEBUG5
debugging information - most verbose
@ LEV_DEBUG1
debugging information - least verbose
@ LEV_INFO1
Information helpful for simulation users and developers alike - least verbose.
double SumPref(double total, std::pair< Arc, double > pref)
double NodeWeight(ExchangeNode::Ptr n, std::map< std::string, double > *weights, double avg_pref)
double GroupWeight(RequestGroup::Ptr g, std::map< std::string, double > *weights, std::map< ExchangeNode::Ptr, double > *avg_prefs)
boost::shared_ptr< ExchangeNode > Ptr
a less-than comparison for pairs