CYCLUS
Loading...
Searching...
No Matches
exchange_graph.h
Go to the documentation of this file.
1#ifndef CYCLUS_SRC_EXCHANGE_GRAPH_H_
2#define CYCLUS_SRC_EXCHANGE_GRAPH_H_
3
4#include <limits>
5#include <map>
6#include <string>
7#include <utility>
8#include <vector>
9
10#include <boost/weak_ptr.hpp>
11#include <boost/shared_ptr.hpp>
12
13namespace cyclus {
14
16class Arc;
17
18/// @class ExchangeNode
19///
20/// @brief ExchangeNodes are used in ExchangeGraphs to house information about a
21/// given translated Bid or Request. Specifically, ExchangeNodes have a notion
22/// of unit capacities that the given Bid or Request contribute to the overall
23/// capacity of ExchangeNodeGroup. ExchangeNodes also have a notion of quantity,
24/// i.e., the maximum amount of a resource that can be attributed to
25/// it. Finally, nodes can be exclusive, that is to say that they represent a
26/// request or bid that must be exclusively satisfied (it can not be split).
28 public:
29 typedef boost::shared_ptr<ExchangeNode> Ptr;
30
32 explicit ExchangeNode(double qty);
33 ExchangeNode(double qty, bool exclusive);
34 ExchangeNode(double qty, bool exclusive, std::string commod);
35 ExchangeNode(double qty, bool exclusive, std::string commod, int agent_id);
36
37 /// @brief the parent ExchangeNodeGroup to which this ExchangeNode belongs
39
40 /// @brief unit values associated with this ExchangeNode corresponding to
41 /// capacties of its parent ExchangeNodeGroup. This information corresponds to
42 /// the resource object from which this ExchangeNode was translated.
43 std::map<Arc, std::vector<double>> unit_capacities;
44
45 /// @brief preference values for arcs
46 std::map<Arc, double> prefs;
47
48 /// @brief whether this node represents an exclusive request or offer
50
51 /// @brief the commodity associated with this exchange node
52 std::string commod;
53
54 /// @brief the id of the agent associated with this node
56
57 /// @brief the maximum amount of a resource that can be associated with this
58 /// node
59 double qty;
60};
61
62/// @brief An arc represents a possible connection between two nodes in the
63/// bipartite resource exchange graph. It is common to refer to the two sets of
64/// nodes in a bipartite graph by the set variables U and V (see
65/// http://en.wikipedia.org/wiki/Bipartite_graph). By convention, arc.unode() ==
66/// request node, arc.vnode() == bid node.
67class Arc {
68 public:
69 /// default required for usage in maps
70 /// @warning, in general do not use this constructor; it exists for arcs to be
71 /// map values
72 Arc() {}
73
74 Arc(boost::shared_ptr<ExchangeNode> unode,
75 boost::shared_ptr<ExchangeNode>
76 vnode);
77 Arc(const Arc& other);
78
79 inline Arc& operator=(const Arc& other) {
80 unode_ = other.unode();
81 vnode_ = other.vnode();
82 exclusive_ = other.exclusive();
83 excl_val_ = other.excl_val();
84 return *this;
85 }
86
87 inline bool operator<(const Arc& rhs) const {
88 return unode_ < rhs.unode_ ||
89 (!(rhs.unode_ < unode_) && vnode_ < rhs.vnode_);
90 }
91
92 inline bool operator==(const Arc& rhs) const {
93 return unode() == rhs.unode() && vnode() == rhs.vnode();
94 }
95
96 inline boost::shared_ptr<ExchangeNode> unode() const { return unode_.lock(); }
97 inline boost::shared_ptr<ExchangeNode> vnode() const { return vnode_.lock(); }
98 inline bool exclusive() const { return exclusive_; }
99 inline double excl_val() const { return excl_val_; }
100 inline double pref() const { return pref_; }
101 inline void pref(double pref) { pref_ = pref; }
102
103 private:
104 boost::weak_ptr<ExchangeNode> unode_;
105 boost::weak_ptr<ExchangeNode> vnode_;
106 bool exclusive_;
107 double excl_val_, pref_;
108};
109
110/// @brief ExchangeNode-ExchangeNode equality operator
111inline bool operator==(const ExchangeNode& lhs, const ExchangeNode& rhs);
112
113/// @class ExchangeNodeGroup
114///
115/// @brief A ExchangeNodeGroup is a collection of ExchangeNodes, and is the
116/// ExchangeGraph representation of a BidPortfolio or RequestPortfolio. It
117/// houses information about the concrete capacities associated with either
118/// portfolio.
120 public:
121 typedef boost::shared_ptr<ExchangeNodeGroup> Ptr;
122
123 const std::vector<ExchangeNode::Ptr>& nodes() const { return nodes_; }
124 std::vector<ExchangeNode::Ptr>& nodes() { return nodes_; }
125
126 /// @brief exclusive node groups represent nodes over whose combined arcs flow
127 /// can only exist on one arc
128 const std::vector<std::vector<ExchangeNode::Ptr>>& excl_node_groups() const {
129 return excl_node_groups_;
130 }
131 std::vector<std::vector<ExchangeNode::Ptr>>& excl_node_groups() {
132 return excl_node_groups_;
133 }
134
135 /// @brief the flow capacities assocaited with this group
136 const std::vector<double>& capacities() const { return capacities_; }
137 std::vector<double>& capacities() { return capacities_; }
138
139 /// @brief Add the node to the ExchangeNodeGroup and informs the node it is a
140 /// member of this ExchangeNodeGroup
141 virtual void AddExchangeNode(ExchangeNode::Ptr node);
142
143 /// @brief Adds a node grouping to the set of exclusive node groups, in
144 /// general this function is used for bid exclusivity. An exclusive group
145 /// implies that for all nodes in that group, flow is only allowed to flow
146 /// over one. This is the case for multiple bids that refer to the same
147 /// exclusive object.
148 inline void AddExclGroup(std::vector<ExchangeNode::Ptr>& nodes) {
149 excl_node_groups_.push_back(nodes);
150 }
151
152 /// @return true of any nodes have arcs associated with them
153 bool HasArcs() {
154 for (std::vector<ExchangeNode::Ptr>::iterator it = nodes_.begin();
155 it != nodes_.end();
156 ++it) {
157 if (it->get()->prefs.size() > 0) return true;
158 }
159 return false;
160 }
161
162 /// @brief adds a single node to the set of exclusive node groupings, in
163 /// general this function is used for demand exclusivity
165
166 /// @brief Add a flow capacity to the group
167 inline void AddCapacity(double c) { capacities_.push_back(c); }
168
169 private:
170 std::vector<ExchangeNode::Ptr> nodes_;
171 std::vector<std::vector<ExchangeNode::Ptr>> excl_node_groups_;
172 std::vector<double> capacities_;
173};
174
175/// @class RequestGroup
176///
177/// @brief A RequestGroup is a specific ExchangeNodeGroup with a notion of an
178/// total requested quantity.
180 public:
181 typedef boost::shared_ptr<RequestGroup> Ptr;
182
183 explicit RequestGroup(double qty = 0.0);
184
185 double qty() { return qty_; }
186
187 /// @brief Add the node to the ExchangeNodeGroup and informs the node it is a
188 /// member of this ExchangeNodeGroup, if the node is exclusive, also add it to
189 /// the group of exclusive nodes
190 virtual void AddExchangeNode(ExchangeNode::Ptr node);
191
192 private:
193 double qty_;
194};
195
196typedef std::pair<Arc, double> Match;
197
198/// @class ExchangeGraph
199///
200/// @brief An ExchangeGraph is a resource-neutral representation of a
201/// ResourceExchange. In general, it is produced via translation by an
202/// ExchangeTranslator. It is comprised of ExchangeNodes that are collected into
203/// ExchangeNodeGroups. Arcs are defined, connecting ExchangeNodes to each
204/// other. An ExchangeSolver can solve a given instance of an ExchangeGraph, and
205/// the solution is stored on the Graph in the form of Matches.
207 public:
208 typedef boost::shared_ptr<ExchangeGraph> Ptr;
209
211
212 /// @brief adds a request group to the graph
214
215 /// @brief adds a supply group to the graph
217
218 /// @brief adds an arc to the graph
219 void AddArc(const Arc& a);
220
221 /// @brief adds a match for a quanity of flow along an arc
222 ///
223 /// @param pa the arc corresponding to a match
224 /// @param qty the amount of flow corresponding to a match
225 void AddMatch(const Arc& a, double qty);
226
227 /// clears all matches
228 inline void ClearMatches() { matches_.clear(); }
229
230 inline const std::vector<RequestGroup::Ptr>& request_groups() const {
231 return request_groups_;
232 }
233 inline std::vector<RequestGroup::Ptr>& request_groups() {
234 return request_groups_;
235 }
236
237 inline const std::vector<ExchangeNodeGroup::Ptr>& supply_groups() const {
238 return supply_groups_;
239 }
240 inline std::vector<ExchangeNodeGroup::Ptr>& supply_groups() {
241 return supply_groups_;
242 }
243
244 inline const std::map<ExchangeNode::Ptr, std::vector<Arc>>& node_arc_map()
245 const {
246 return node_arc_map_;
247 }
248 inline std::map<ExchangeNode::Ptr, std::vector<Arc>>& node_arc_map() {
249 return node_arc_map_;
250 }
251
252 inline const std::vector<Match>& matches() { return matches_; }
253
254 inline const std::vector<Arc>& arcs() const { return arcs_; }
255 inline std::vector<Arc>& arcs() { return arcs_; }
256
257 inline const std::map<Arc, int>& arc_ids() const { return arc_ids_; }
258 inline std::map<Arc, int>& arc_ids() { return arc_ids_; }
259
260 inline const std::map<int, Arc>& arc_by_id() const { return arc_by_id_; }
261 inline std::map<int, Arc>& arc_by_id() { return arc_by_id_; }
262
263 private:
264 std::vector<RequestGroup::Ptr> request_groups_;
265 std::vector<ExchangeNodeGroup::Ptr> supply_groups_;
266 std::map<ExchangeNode::Ptr, std::vector<Arc>> node_arc_map_;
267 std::vector<Match> matches_;
268 std::vector<Arc> arcs_;
269 std::map<Arc, int> arc_ids_;
270 std::map<int, Arc> arc_by_id_;
271 int next_arc_id_;
272};
273
274} // namespace cyclus
275
276#endif // CYCLUS_SRC_EXCHANGE_GRAPH_H_
An arc represents a possible connection between two nodes in the bipartite resource exchange graph.
Arc & operator=(const Arc &other)
void pref(double pref)
boost::shared_ptr< ExchangeNode > vnode() const
bool operator<(const Arc &rhs) const
double pref() const
bool operator==(const Arc &rhs) const
boost::shared_ptr< ExchangeNode > unode() const
double excl_val() const
bool exclusive() const
Arc()
default required for usage in maps
const std::vector< Arc > & arcs() const
std::vector< ExchangeNodeGroup::Ptr > & supply_groups()
const std::vector< ExchangeNodeGroup::Ptr > & supply_groups() const
const std::map< ExchangeNode::Ptr, std::vector< Arc > > & node_arc_map() const
void AddRequestGroup(RequestGroup::Ptr prs)
adds a request group to the graph
const std::vector< Match > & matches()
std::map< Arc, int > & arc_ids()
const std::map< Arc, int > & arc_ids() const
std::vector< Arc > & arcs()
void AddSupplyGroup(ExchangeNodeGroup::Ptr prs)
adds a supply group to the graph
void ClearMatches()
clears all matches
const std::vector< RequestGroup::Ptr > & request_groups() const
std::map< int, Arc > & arc_by_id()
void AddMatch(const Arc &a, double qty)
adds a match for a quanity of flow along an arc
std::vector< RequestGroup::Ptr > & request_groups()
const std::map< int, Arc > & arc_by_id() const
void AddArc(const Arc &a)
adds an arc to the graph
std::map< ExchangeNode::Ptr, std::vector< Arc > > & node_arc_map()
boost::shared_ptr< ExchangeGraph > Ptr
A ExchangeNodeGroup is a collection of ExchangeNodes, and is the ExchangeGraph representation of a Bi...
const std::vector< double > & capacities() const
the flow capacities assocaited with this group
void AddCapacity(double c)
Add a flow capacity to the group.
boost::shared_ptr< ExchangeNodeGroup > Ptr
std::vector< ExchangeNode::Ptr > & nodes()
std::vector< std::vector< ExchangeNode::Ptr > > & excl_node_groups()
void AddExclGroup(std::vector< ExchangeNode::Ptr > &nodes)
Adds a node grouping to the set of exclusive node groups, in general this function is used for bid ex...
virtual void AddExchangeNode(ExchangeNode::Ptr node)
Add the node to the ExchangeNodeGroup and informs the node it is a member of this ExchangeNodeGroup.
const std::vector< ExchangeNode::Ptr > & nodes() const
const std::vector< std::vector< ExchangeNode::Ptr > > & excl_node_groups() const
exclusive node groups represent nodes over whose combined arcs flow can only exist on one arc
void AddExclNode(ExchangeNode::Ptr n)
adds a single node to the set of exclusive node groupings, in general this function is used for deman...
std::vector< double > & capacities()
virtual void AddExchangeNode(ExchangeNode::Ptr node)
Add the node to the ExchangeNodeGroup and informs the node it is a member of this ExchangeNodeGroup,...
RequestGroup(double qty=0.0)
boost::shared_ptr< RequestGroup > Ptr
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
std::pair< Arc, double > Match
bool operator==(const CapacityConstraint< T > &lhs, const CapacityConstraint< T > &rhs)
CapacityConstraint-CapacityConstraint equality operator.
int agent_id
the id of the agent associated with this node
bool exclusive
whether this node represents an exclusive request or offer
ExchangeNodeGroup * group
the parent ExchangeNodeGroup to which this ExchangeNode belongs
std::map< Arc, std::vector< double > > unit_capacities
unit values associated with this ExchangeNode corresponding to capacties of its parent ExchangeNodeGr...
double qty
the maximum amount of a resource that can be associated with this node
std::map< Arc, double > prefs
preference values for arcs
boost::shared_ptr< ExchangeNode > Ptr
std::string commod
the commodity associated with this exchange node