CYCLUS
Loading...
Searching...
No Matches
greedy_preconditioner.cc
Go to the documentation of this file.
2
3#include <algorithm>
4#include <assert.h>
5#include <numeric>
6#include <string>
7
8#include <boost/lambda/bind.hpp>
9
10#include "cyc_std.h"
11#include "logger.h"
12
13namespace l = boost::lambda;
14
15namespace cyclus {
16
17inline double SumPref(double total, std::pair<Arc, double> pref) {
18 return total += pref.second;
19}
20
22 std::map<Arc, double>& prefs = n->prefs;
23 return prefs.size() > 0
24 ? std::accumulate(prefs.begin(), prefs.end(), 0.0, SumPref) /
25 prefs.size()
26 : 0;
27}
28
30
32 const std::map<std::string, double>& commod_weights)
33 : commod_weights_(commod_weights) {
34 if (commod_weights_.size() != 0) ProcessWeights_(END);
35};
36
38 const std::map<std::string, double>& commod_weights, WgtOrder order)
39 : commod_weights_(commod_weights) {
40 if (commod_weights_.size() != 0) ProcessWeights_(order);
41};
42
44 avg_prefs_.clear();
45
46 std::vector<RequestGroup::Ptr>& groups =
47 const_cast<std::vector<RequestGroup::Ptr>&>(graph->request_groups());
48
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());
53
54 // get avg prefs
55 for (int i = 0; i != nodes.size(); i++) {
56 avg_prefs_[nodes[i]] = AvgPref(nodes[i]);
57 }
58
59 // sort nodes by weight
60 std::stable_sort(
61 nodes.begin(), nodes.end(),
62 l::bind(&GreedyPreconditioner::NodeComp, this, l::_1, l::_2));
63
64 // get avg group weights
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] << ".";
68 }
69
70 // sort groups by avg weight
71 std::stable_sort(
72 groups.begin(), groups.end(),
73 l::bind(&GreedyPreconditioner::GroupComp, this, l::_1, l::_2));
74
75 // clear graph-specific state
76 group_weights_.clear();
77}
78
79// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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>>())
84 ->second;
85
86 double max = std::max_element(commod_weights_.begin(),
87 commod_weights_.end(),
88 SecondLT<std::pair<std::string, double>>())
89 ->second;
90
91 assert(commod_weights_.size() == 0 || min >= 0);
92
93 std::map<std::string, double>::iterator it;
94 switch (order) {
95 case REVERSE:
96 for (it = commod_weights_.begin(); it != commod_weights_.end(); ++it) {
97 it->second = max + min - it->second; // reverses order
98 }
99 break;
100 default: // do nothing
101 break;
102 }
103
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;
107 }
108}
109
110// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
112 std::map<std::string, double>* weights,
113 std::map<ExchangeNode::Ptr, double>* avg_prefs) {
114 std::vector<ExchangeNode::Ptr>& nodes = g->nodes();
115 double sum = 0;
116
118 for (int i = 0; i != nodes.size(); i++) {
119 n = nodes[i];
120 sum += NodeWeight(n, weights, (*avg_prefs)[n]);
121 }
122 return nodes.size() > 0 ? sum / nodes.size() : 0;
123}
124
125// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
127 std::map<std::string, double>* weights,
128 double avg_pref) {
129 double commod_weight = (weights->size() != 0) ? (*weights)[n->commod] : 1;
130 double node_weight = commod_weight * (1 + avg_pref / (1 + avg_pref));
131
132 CLOG(LEV_DEBUG5) << "Determining node weight: ";
133 CLOG(LEV_DEBUG5) << " commodity weight: " << commod_weight;
134 CLOG(LEV_DEBUG5) << " avg pref: " << avg_pref;
135 CLOG(LEV_DEBUG5) << " node weight: " << node_weight;
136
137 return node_weight;
138}
139
140} // namespace cyclus
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.
#define CLOG(level)
Definition logger.h:40
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
double AvgPref(ExchangeNode::Ptr n)
@ LEV_DEBUG5
debugging information - most verbose
Definition logger.h:74
@ LEV_DEBUG1
debugging information - least verbose
Definition logger.h:70
@ LEV_INFO1
Information helpful for simulation users and developers alike.
Definition logger.h:60
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
Definition cyc_std.h:11