CYCLUS
Loading...
Searching...
No Matches
greedy_solver.h
Go to the documentation of this file.
1#ifndef CYCLUS_SRC_GREEDY_SOLVER_H_
2#define CYCLUS_SRC_GREEDY_SOLVER_H_
3
4#include <boost/shared_ptr.hpp>
5#include "exchange_graph.h"
6#include "exchange_solver.h"
8
9namespace cyclus {
10
11/// @warning Deprecated! Use the Capacity member functions defined in the
12/// GreedySolver class.
13// @{
14void Capacity(cyclus::Arc const&, double, double);
15void Capacity(boost::shared_ptr<cyclus::ExchangeNode>, cyclus::Arc const&,
16 double);
17// @}
18
19/// double Capacity(const Arc& a, double u_curr_qty, double v_curr_qty) {
20/// return 0;
21/// }
22
23/// @brief A comparison function for sorting a container of Arcs by the
24/// requester's (unode's) preference, in decensing order (i.e., most preferred
25/// Arc first). In the case of a tie, a lexicalgraphic ordering of node ids is
26/// used.
27inline bool ReqPrefComp(const Arc& l, const Arc& r) {
28 int lu = l.unode()->agent_id;
29 int lv = l.vnode()->agent_id;
30 int ru = r.unode()->agent_id;
31 int rv = r.vnode()->agent_id;
32 double lpref = l.unode()->prefs[l];
33 double rpref = r.unode()->prefs[r];
34 return (lpref != rpref) ? (lpref > rpref) : (lu > ru || (lu == ru && lv > rv));
35}
36
37/// @brief A comparison function for sorting a container of Nodes by the nodes
38/// preference in decensing order (i.e., most preferred Node first). In the case
39/// of a tie, a lexicalgraphic ordering of node ids is used.
41 int lid = l->agent_id;
42 int rid = r->agent_id;
43 double lpref = AvgPref(l);
44 double rpref = AvgPref(r);
45 return (lpref != rpref) ? (lpref > rpref) : (lid > rid);
46}
47
48class ExchangeGraph;
49class GreedyPreconditioner;
50
51/// @brief The GreedySolver provides the implementation for a "greedy" solution
52/// to a resource exchange graph.
53///
54/// Given an ExchangeGraph, the greedy solver will march through each
55/// RequestGroup in the graph, matching request nodes "greedily" with supply
56/// nodes. Each request node will attempt to be supplied by supplier arcs as
57/// long as those supplier arcs have some excess capacity. The possible
58/// suppliers will be ordered by descending preference. The algorithm terminates
59/// when one of the following conditions is met:
60/// 1) All RequestGroups are satisfied
61/// 2) All SupplySets are at capacity
62///
63/// @warning the GreedySolver is responsible for deleting is conditioner!
65 public:
66 /// GreedySolver constructor
67 /// @param exclusive_orders a flag for enforcing integral, quantized orders
68 /// @param c a conditioner to use before solving a graph instance
69 /// @warning if a NULL pointer is passed as a conditioner argument,
70 /// conditioning will *NOT* occur
71 /// @{
73 explicit GreedySolver(bool exclusive_orders);
76 /// @}
77
78 virtual ~GreedySolver();
79
80 /// Uses the provided (or a default) GreedyPreconditioner to condition the
81 /// solver's ExchangeGraph so that RequestGroups are ordered by average
82 /// preference and commodity weight.
83 ///
84 /// @warning this function is called during the Solve step and should most
85 /// likely not be called independently thereof (except for testing)
86 void Condition();
87
88 /// Initialize member values based on the given graph.
89 void Init();
90
91 /// @brief the capacity of the arc
92 ///
93 /// @throws StateError if either ExchangeNode does not have a ExchangeNodeGroup
94 /// @param a the arc
95 /// @param u_curr_qty the current quantity assigned to the unode (if solving
96 /// piecemeal)
97 /// @param v_curr_qty the current quantity assigned to the vnode (if solving
98 /// piecemeal)
99 /// @return The minimum of the unode and vnode's capacities
100 // @{
101 double Capacity(const Arc& a, double u_curr_qty, double v_curr_qty);
102 inline double Capacity(const Arc& a) { return Capacity(a, 0, 0); }
103 // @}
104
105 /// @brief the capacity of a node
106 ///
107 /// @throws StateError if ExchangeNode does not have a ExchangeNodeGroup
108 /// @param n the node
109 /// @param min_cap whether to use the minimum or maximum capacity value. In general,
110 /// nodes that represent bids use the minimum (i.e., the capacities represents a
111 /// less-than constraint) and nodes that represent requests use the maximum
112 /// value (i.e., the capacities represents a greater-than constraint).
113 /// @param curr_qty the currently allocated node quantity (if solving piecemeal)
114 /// @return The minimum of the node's nodegroup capacities / the node's unit
115 /// capacities, or the ExchangeNode's remaining qty -- whichever is smaller.
116 /// @{
117 double Capacity(ExchangeNode::Ptr n, const Arc& a, bool min_cap,
118 double curr_qty);
119 inline double Capacity(ExchangeNode::Ptr n, const Arc& a, bool min_cap) {
120 return Capacity(n, a, min_cap, 0.0);
121 }
122 inline double Capacity(ExchangeNode::Ptr n, const Arc& a, double curr_qty) {
123 return Capacity(n, a, true, curr_qty);
124 }
125 inline double Capacity(ExchangeNode::Ptr n, const Arc& a) {
126 return Capacity(n, a, true, 0.0);
127 }
128 /// @}
129
130 protected:
131 /// @brief the GreedySolver solves an ExchangeGraph by iterating over each
132 /// RequestGroup and matching requests with the minimum bids possible, starting
133 /// from the beginning of the the respective request and bid containers.
134 virtual double SolveGraph();
135
136 private:
137 /// @brief updates the capacity of a given ExchangeNode (i.e., its max_qty and the
138 /// capacities of its ExchangeNodeGroup)
139 ///
140 /// @throws StateError if ExchangeNode does not have a ExchangeNodeGroup
141 /// @throws ValueError if the update results in a negative ExchangeNodeGroup
142 /// capacity or a negative ExchangeNode max_qty
143 /// @param n the ExchangeNode
144 /// @param qty the quantity for the node to update
145 void GetCaps(ExchangeNodeGroup::Ptr prs);
146 void GreedilySatisfySet(RequestGroup::Ptr prs);
147 void UpdateCapacity(ExchangeNode::Ptr n, const Arc& a, double qty);
148 void UpdateObj(double qty, double pref);
149
150 GreedyPreconditioner* conditioner_;
151 std::map<ExchangeNode::Ptr, double> n_qty_;
152 std::map<ExchangeNodeGroup*, std::vector<double> > grp_caps_;
153 double obj_;
154 double unmatched_;
155};
156
157} // namespace cyclus
158
159#endif // CYCLUS_SRC_GREEDY_SOLVER_H_
An arc represents a possible connection between two nodes in the bipartite resource exchange graph.
boost::shared_ptr< ExchangeNodeGroup > Ptr
a very simple interface for solving translated resource exchanges
A GreedyPreconditioner conditions an ExchangeGraph for a GreedySolver by ordering the RequestGroups a...
The GreedySolver provides the implementation for a "greedy" solution to a resource exchange graph.
virtual double SolveGraph()
the GreedySolver solves an ExchangeGraph by iterating over each RequestGroup and matching requests wi...
double Capacity(ExchangeNode::Ptr n, const Arc &a, double curr_qty)
GreedySolver()
GreedySolver constructor.
double Capacity(ExchangeNode::Ptr n, const Arc &a, bool min_cap)
double Capacity(const Arc &a, double u_curr_qty, double v_curr_qty)
the capacity of the arc
double Capacity(const Arc &a)
void Condition()
Uses the provided (or a default) GreedyPreconditioner to condition the solver's ExchangeGraph so that...
double Capacity(ExchangeNode::Ptr n, const Arc &a)
void Init()
Initialize member values based on the given graph.
boost::shared_ptr< RequestGroup > Ptr
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
double AvgPref(ExchangeNode::Ptr n)
void Capacity(cyclus::Arc const &, double, double)
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; }
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters
boost::shared_ptr< ExchangeNode > Ptr