CYCLUS
exchange_solver.cc
Go to the documentation of this file.
1 #include "exchange_solver.h"
2 
3 #include <vector>
4 #include <map>
5 
6 #include "context.h"
7 #include "exchange_graph.h"
8 
9 namespace cyclus {
10 
11 double ExchangeSolver::Cost(const Arc& a, bool exclusive_orders) {
12  return (exclusive_orders && a.exclusive()) ?
13  a.excl_val() / a.pref() : 1.0 / a.pref();
14 }
15 
17  return PseudoCost(1e-1);
18 }
19 
20 double ExchangeSolver::PseudoCost(double cost_factor) {
21  return PseudoCostByPref(cost_factor);
22 }
23 
24 double ExchangeSolver::PseudoCostByCap(double cost_factor) {
25  std::vector<ExchangeNode::Ptr>::iterator n_it;
26  std::map<Arc, std::vector<double> >::iterator c_it;
27  std::map<Arc, double>::iterator p_it;
28  std::vector<RequestGroup::Ptr>::iterator rg_it;
29  std::vector<ExchangeNodeGroup::Ptr>::iterator sg_it;
30  double min_cap, pref, coeff;
31 
32  double max_coeff = std::numeric_limits<double>::min();
33  double min_unit_cap = std::numeric_limits<double>::max();
34 
35  for (sg_it = graph_->supply_groups().begin();
36  sg_it != graph_->supply_groups().end();
37  ++sg_it) {
38  std::vector<ExchangeNode::Ptr>& nodes = (*sg_it)->nodes();
39  for (n_it = nodes.begin(); n_it != nodes.end(); ++n_it) {
40  // update min_unit_cap
41  std::map<Arc, std::vector<double> >::iterator c_it;
42  std::map<Arc, std::vector<double> >& caps = (*n_it)->unit_capacities;
43  for (c_it = caps.begin(); c_it != caps.end(); ++c_it) {
44  std::vector<double>& ucaps = c_it->second;
45  if (!ucaps.empty()) {
46  min_cap = *std::min_element(ucaps.begin(), ucaps.end());
47  if (min_cap < min_unit_cap)
48  min_unit_cap = min_cap;
49  }
50  }
51  }
52  }
53 
54  for (rg_it = graph_->request_groups().begin();
55  rg_it != graph_->request_groups().end();
56  ++rg_it) {
57  std::vector<ExchangeNode::Ptr>& nodes = (*rg_it)->nodes();
58  for (n_it = nodes.begin(); n_it != nodes.end(); ++n_it) {
59  // update min_unit_cap
60  std::map<Arc, std::vector<double> >::iterator c_it;
61  std::map<Arc, std::vector<double> >& caps = (*n_it)->unit_capacities;
62  for (c_it = caps.begin(); c_it != caps.end(); ++c_it) {
63  std::vector<double>& ucaps = c_it->second;
64  if (!ucaps.empty()) {
65  min_cap = *std::min_element(ucaps.begin(), ucaps.end());
66  if (min_cap < min_unit_cap)
67  min_unit_cap = min_cap;
68  }
69  }
70 
71  // update max_pref_
72  std::map<Arc, double>& prefs = (*n_it)->prefs;
73  for (p_it = prefs.begin(); p_it != prefs.end(); ++p_it) {
74  pref = p_it->second;
75  const Arc& a = p_it->first;
76  coeff = ArcCost(a);
77  if (coeff > max_coeff)
78  max_coeff = coeff;
79  }
80  }
81  }
82 
83  return max_coeff / min_unit_cap * (1 + cost_factor);
84 }
85 
86 double ExchangeSolver::PseudoCostByPref(double cost_factor) {
87  double max_cost = 0;
88  std::vector<Arc>& arcs = graph_->arcs();
89  for (int i = 0; i != arcs.size(); i++) {
90  const Arc& a = arcs[i];
91  // remove exclusive value factor from costs for preferences that are less
92  // than unity. otherwise they can artificially raise the maximum cost.
93  double factor = (a.exclusive() && a.excl_val() < 1) ? 1 / a.excl_val() : 1.0;
94  max_cost = std::max(max_cost, ArcCost(a) * factor);
95  }
96  return max_cost * (1 + cost_factor);
97 }
98 
99 } // namespace cyclus
double PseudoCostByCap(double cost_factor)
An arc represents a possible connection between two nodes in the bipartite resource exchange graph...
bool exclusive() const
const std::vector< Arc > & arcs() const
double ArcCost(const Arc &a)
return the cost of an arc
ExchangeGraph * graph_
const std::vector< ExchangeNodeGroup::Ptr > & supply_groups() const
double PseudoCostByPref(double cost_factor)
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 excl_val() const
const std::vector< RequestGroup::Ptr > & request_groups() const
double PseudoCost()
Calculates the ratio of the maximum objective coefficient to minimum unit capacity plus an added cost...
static double Cost(const Arc &a, bool exclusive_orders=kDefaultExclusive)
return the cost of an arc