CYCLUS
exchange_graph.cc
Go to the documentation of this file.
1 #include "exchange_graph.h"
2 
3 #include <algorithm>
4 #include <boost/math/special_functions/next.hpp>
5 
6 #include "cyc_limits.h"
7 #include "error.h"
8 #include "logger.h"
9 
10 namespace cyclus {
11 
12 ExchangeNode::ExchangeNode(double qty, bool exclusive, std::string commod,
13  int agent_id)
14  : qty(qty),
15  exclusive(exclusive),
16  commod(commod),
17  agent_id(agent_id),
18  group(NULL) {}
19 
21  : qty(qty),
22  exclusive(exclusive),
23  commod(""),
24  agent_id(-1),
25  group(NULL) {}
26 
27 ExchangeNode::ExchangeNode(double qty, bool exclusive, std::string commod)
28  : qty(qty),
29  exclusive(exclusive),
30  commod(commod),
31  agent_id(-1),
32  group(NULL) {}
33 
35  : qty(qty),
36  exclusive(false),
37  commod(""),
38  agent_id(-1),
39  group(NULL) {}
40 
42  : qty(std::numeric_limits<double>::max()),
43  exclusive(false),
44  commod(""),
45  agent_id(-1),
46  group(NULL) {}
47 
48 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49 bool operator==(const ExchangeNode& lhs, const ExchangeNode& rhs) {
50  return (lhs.unit_capacities == rhs.unit_capacities &&
51  lhs.qty == rhs.qty &&
52  lhs.exclusive == rhs.exclusive &&
53  lhs.group == rhs.group &&
54  lhs.commod == rhs.commod);
55 }
56 
57 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
58 Arc::Arc(boost::shared_ptr<ExchangeNode> unode,
59  boost::shared_ptr<ExchangeNode> vnode)
60  : unode_(unode),
61  vnode_(vnode) {
62  exclusive_ = unode->exclusive || vnode->exclusive;
63  if (exclusive_) {
64  double fqty = unode->qty;
65  double sqty = vnode->qty;
66  // this careful float comparison is vital for preventing false positive
67  // constraint violations w.r.t. exclusivity-related capacity.
68  double dist = boost::math::float_distance(fqty, sqty);
69  if (unode->exclusive && vnode->exclusive) {
70  excl_val_ = (std::abs(dist) <= float_ulp_eq) ? fqty : 0;
71  } else if (unode->exclusive) {
72  excl_val_ = dist >= -float_ulp_eq ? fqty : 0;
73  } else {
74  excl_val_ = dist <= float_ulp_eq ? sqty : 0;
75  }
76  } else {
77  excl_val_ = 0;
78  }
79 }
80 
81 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
82 Arc::Arc(const Arc& other)
83  : unode_(other.unode()),
84  vnode_(other.vnode()),
85  pref_(other.pref()),
86  exclusive_(other.exclusive()),
87  excl_val_(other.excl_val()) {}
88 
89 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
91  node->group = this;
92  nodes_.push_back(node);
93 }
94 
95 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
97  std::vector<ExchangeNode::Ptr> nodes;
98  nodes.push_back(node);
99  excl_node_groups_.push_back(nodes);
100 }
101 
102 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
103 RequestGroup::RequestGroup(double qty) : qty_(qty) {}
104 
105 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
108  if (node->exclusive) {
110  }
111 }
112 
113 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114 ExchangeGraph::ExchangeGraph() : next_arc_id_(0) { }
115 
116 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
118  request_groups_.push_back(prs);
119 }
120 
121 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
123  supply_groups_.push_back(pss);
124 }
125 
126 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
127 void ExchangeGraph::AddArc(const Arc& a) {
128  arcs_.push_back(a);
129  int id = next_arc_id_++;
130  arc_ids_.insert(std::pair<Arc, int>(a, id));
131  arc_by_id_.insert(std::pair<int, Arc>(id, a));
132  node_arc_map_[a.unode()].push_back(a);
133  node_arc_map_[a.vnode()].push_back(a);
134 }
135 
136 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
137 void ExchangeGraph::AddMatch(const Arc& a, double qty) {
138  matches_.push_back(std::make_pair(a, qty));
139 }
140 
141 } // namespace cyclus
void AddRequestGroup(RequestGroup::Ptr prs)
adds a request group to the graph
An arc represents a possible connection between two nodes in the bipartite resource exchange graph...
bool exclusive
whether this node represents an exclusive request or offer
bool exclusive() const
boost::shared_ptr< RequestGroup > Ptr
ExchangeNodeGroup * group
the parent ExchangeNodeGroup to which this ExchangeNode belongs
boost::shared_ptr< ExchangeNode > Ptr
boost::shared_ptr< ExchangeNodeGroup > Ptr
virtual void AddExchangeNode(ExchangeNode::Ptr node)
Add the node to the ExchangeNodeGroup and informs the node it is a member of this ExchangeNodeGroup...
std::string commod
the commodity associated with this exchange node
boost::shared_ptr< ExchangeNode > vnode() const
void AddArc(const Arc &a)
adds an arc to the graph
static const double float_ulp_eq
distance in ULP within which floating point numbers should be considered equal.
Definition: cyc_limits.h:35
RequestGroup(double qty=0.0)
void AddExclNode(ExchangeNode::Ptr n)
adds a single node to the set of exclusive node groupings, in general this function is used for deman...
int agent_id
the id of the agent associated with this node
ExchangeNodes are used in ExchangeGraphs to house information about a given translated Bid or Request...
Arc()
default required for usage in maps
void AddMatch(const Arc &a, double qty)
adds a match for a quanity of flow along an arc
virtual void AddExchangeNode(ExchangeNode::Ptr node)
Add the node to the ExchangeNodeGroup and informs the node it is a member of this ExchangeNodeGroup...
void AddSupplyGroup(ExchangeNodeGroup::Ptr prs)
adds a supply group to the graph
bool operator==(const CapacityConstraint< T > &lhs, const CapacityConstraint< T > &rhs)
CapacityConstraint-CapacityConstraint equality operator.
Code providing rudimentary logging capability for the Cyclus core.
std::map< Arc, std::vector< double > > unit_capacities
unit values associated with this ExchangeNode corresponding to capacties of its parent ExchangeNodeGr...
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
double pref() const
double qty
the maximum amount of a resource that can be associated with this node
double excl_val() const
boost::shared_ptr< ExchangeNode > unode() const