CYCLUS
greedy_preconditioner.h
Go to the documentation of this file.
1 #ifndef CYCLUS_SRC_GREEDY_PRECONDITIONER_H_
2 #define CYCLUS_SRC_GREEDY_PRECONDITIONER_H_
3 
4 #include <map>
5 #include <string>
6 
7 #include "exchange_graph.h"
8 
9 namespace cyclus {
10 
11 /// @returns the node's weight given the node and commodity weight
13  std::map<std::string, double>* weights,
14  double avg_pref);
15 
16 /// @returns average RequestGroup weight
18  std::map<std::string, double>* weights,
19  std::map<ExchangeNode::Ptr, double>* avg_prefs);
20 
21 /// @returns the average preference across arcs for a node
22 double AvgPref(ExchangeNode::Ptr n);
23 
24 /// @class GreedyPreconditioner
25 ///
26 /// @brief A GreedyPreconditioner conditions an ExchangeGraph for a GreedySolver
27 /// by ordering the RequestGroups and ExchangeNodes within each RequestGroup
28 /// weighted by their commodity's importance. The Graph is conditioned in-place.
29 ///
30 /// @section weighting Weighting
31 ///
32 /// Commodity weights are provided to the conditioner via its constructor. A
33 /// larger weight implies a higher level of importance for solving.
34 ///
35 /// The conditioning weight for a node is calculated as $w_cond_i = w_commod_i *
36 /// (1 + \frac{\overline{p_i}}{1 + \overline{p_i}})$, where $w_cond_i$ is the
37 /// calculated conditioning weight, $w_commod_i$ is node $i$'s commodity's
38 /// weight, and $\overline{p_i}$ is the average preference of all bid arcs
39 /// associated with node $i$.
40 ///
41 /// First, the ExchangeNodes of each RequestGroup are sorted according to
42 /// their conditioning weights. Then, the average weight of each RequestGroup is
43 /// determined. Finally, each RequestGroup is sorted according to their average
44 /// weight.
45 ///
46 /// @section example Example
47 /// Consider the following commodity-to-weight mapping: {"spam": 5, "eggs": 2}.
48 /// Now consider two RequestGroups with the following commodities:
49 /// #. g1 = {"eggs", "spam", "eggs"}
50 /// #. g2 = {"eggs", "spam"}
51 /// And the following preference-commodity mapping:
52 /// {g1: {"spam": 3/4, "eggs": 1/4}, g2: {"spam": 1, "eggs": 1}.
53 ///
54 /// First, the groups will be ordered by conditioning weights:
55 /// #. g1 = {"spam", "eggs", "eggs"}
56 /// #. g2 = {"spam", "eggs"}
57 ///
58 /// Finally, the groups themselves will be ordered by average weight:
59 /// #. {g2, g1}
61  public:
62  /// @brief the order of commodity weights
63  enum WgtOrder {
64  REVERSE, /// a flag for commodity weights given in the reverse order,
65  /// i.e, lightest first
66  END /// default flag, indicating heaviest-first ordering
67  };
68 
69  /// @brief constructor if weights are given in heaviest-first order
70  /// @warning weights are assumed to be positive
71  /// @{
73  GreedyPreconditioner(const std::map<std::string, double>& commod_weights);
74  /// @brief constructor if weights may not be given in heaviest-first order
75  /// @warning weights are assumed to be positive
76  GreedyPreconditioner(const std::map<std::string, double>& commod_weights,
77  WgtOrder order);
78  /// @}
79 
80  /// @brief conditions the graph as described above
81  /// @throws KeyError if a commodity is in the graph but not in the weight
82  /// mapping
83  void Condition(ExchangeGraph* graph);
84 
85  /// @brief a comparitor for ordering containers of ExchangeNode::Ptrs in
86  /// descending order based on their commodity's weight
87  inline bool NodeComp(const ExchangeNode::Ptr l,
88  const ExchangeNode::Ptr r) {
89  return
90  NodeWeight(l, &commod_weights_, avg_prefs_[l]) >
91  NodeWeight(r, &commod_weights_, avg_prefs_[r]);
92  }
93 
94  /// @brief a comparitor for ordering containers of Request::Ptrs in
95  /// descending order based on their average commodity weight
96  inline bool GroupComp(const RequestGroup::Ptr l,
97  const RequestGroup::Ptr r) {
98  return group_weights_[l] > group_weights_[r];
99  }
100 
101  private:
102  /// @brief normalizes all weights to 1 and puts them in the heaviest-first
103  /// direction
104  void ProcessWeights_(WgtOrder order);
105 
106  bool apply_commod_weights_;
107  std::map<ExchangeNode::Ptr, double> avg_prefs_;
108  std::map<std::string, double> commod_weights_;
109  std::map<RequestGroup::Ptr, double> group_weights_;
110 };
111 
112 } // namespace cyclus
113 
114 #endif // CYCLUS_SRC_GREEDY_PRECONDITIONER_H_
GreedyPreconditioner()
constructor if weights are given in heaviest-first 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...
boost::shared_ptr< RequestGroup > Ptr
boost::shared_ptr< ExchangeNode > Ptr
double GroupWeight(RequestGroup::Ptr g, std::map< std::string, double > *weights, std::map< ExchangeNode::Ptr, double > *avg_prefs)
const double g
Definition: material.h:18
A GreedyPreconditioner conditions an ExchangeGraph for a GreedySolver by ordering the RequestGroups a...
a flag for commodity weights given in the reverse order,
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
double AvgPref(ExchangeNode::Ptr n)
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...
double NodeWeight(ExchangeNode::Ptr n, std::map< std::string, double > *weights, double avg_pref)
An ExchangeGraph is a resource-neutral representation of a ResourceExchange.
void Condition(ExchangeGraph *graph)
conditions the graph as described above
WgtOrder
the order of commodity weights