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) /
32 const std::map<std::string, double>& commod_weights)
33 : commod_weights_(commod_weights) {
34 if (commod_weights_.size() != 0) ProcessWeights_(
END);
38 const std::map<std::string, double>& commod_weights,
WgtOrder order)
39 : commod_weights_(commod_weights) {
40 if (commod_weights_.size() != 0) ProcessWeights_(order);
46 std::vector<RequestGroup::Ptr>& groups =
47 const_cast<std::vector<RequestGroup::Ptr>&
>(graph->
request_groups());
49 std::vector<RequestGroup::Ptr>::iterator it;
50 for (it = groups.begin(); it != groups.end(); ++it) {
51 std::vector<ExchangeNode::Ptr>& nodes =
52 const_cast<std::vector<ExchangeNode::Ptr>&
>((*it)->nodes());
55 for (
int i = 0; i != nodes.size(); i++) {
56 avg_prefs_[nodes[i]] =
AvgPref(nodes[i]);
61 nodes.begin(), nodes.end(),
65 group_weights_[*it] =
GroupWeight(*it, &commod_weights_, &avg_prefs_);
66 CLOG(
LEV_DEBUG1) <<
"Group weight value during graph preconditioning is "
67 << group_weights_[*it] <<
".";
72 groups.begin(), groups.end(),
76 group_weights_.clear();
80void GreedyPreconditioner::ProcessWeights_(WgtOrder order) {
81 double min = std::min_element(commod_weights_.begin(),
82 commod_weights_.end(),
83 SecondLT<std::pair<std::string, double>>())
86 double max = std::max_element(commod_weights_.begin(),
87 commod_weights_.end(),
88 SecondLT<std::pair<std::string, double>>())
91 assert(commod_weights_.size() == 0 || min >= 0);
93 std::map<std::string, double>::iterator it;
96 for (it = commod_weights_.begin(); it != commod_weights_.end(); ++it) {
97 it->second = max + min - it->second;
104 for (it = commod_weights_.begin(); it != commod_weights_.end(); ++it) {
105 CLOG(
LEV_INFO1) <<
"GreedyPreconditioner commodity weight value for "
106 << it->first <<
" is " << it->second;
112 std::map<std::string, double>* weights,
113 std::map<ExchangeNode::Ptr, double>* avg_prefs) {
114 std::vector<ExchangeNode::Ptr>& nodes = g->nodes();
118 for (
int i = 0; i != nodes.size(); i++) {
120 sum +=
NodeWeight(n, weights, (*avg_prefs)[n]);
122 return nodes.size() > 0 ? sum / nodes.size() : 0;
127 std::map<std::string, double>* weights,
129 double commod_weight = (weights->size() != 0) ? (*weights)[n->commod] : 1;
130 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, i.e, lightest first
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.
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