CYCLUS
building_manager.cc
Go to the documentation of this file.
1 #include "building_manager.h"
2 
3 #include "prog_translator.h"
4 #include "CoinPackedVector.hpp"
5 
6 // Headers in this file below this pragma have all warnings shushed.
7 #pragma GCC system_header
9 
10 namespace cyclus {
11 namespace toolkit {
12 
14  : number(n),
15  builder(b),
16  producer(cp) {}
17 
18 std::vector<BuildOrder> BuildingManager::MakeBuildDecision(Commodity& commodity,
19  double demand) {
20  std::vector<BuildOrder> orders;
21  if (demand > 0) {
24  std::map<CommodityProducer*, Builder*> p_to_b;
25  std::map<int, CommodityProducer*> idx_to_p;
26  SetUp_(iface, ctx, p_to_b, idx_to_p, commodity, demand);
27  Solve_(iface, ctx, p_to_b, idx_to_p, orders);
28  }
29  return orders;
30 }
31 
32 void BuildingManager::SetUp_(OsiCbcSolverInterface& iface,
34  std::map<CommodityProducer*, Builder*>& p_to_b,
35  std::map<int, CommodityProducer*>& idx_to_p,
36  Commodity& commodity,
37  double demand) {
38  CoinPackedVector caps;
39  std::set<Builder*>::iterator bit;
40  std::set<CommodityProducer*>::iterator pit;
42  Builder* b;
43  int idx = 0;
44  double inf = iface.getInfinity();
45  for (bit = builders_.begin(); bit != builders_.end(); ++bit) {
46  b = *bit;
47  for (pit = b->producers().begin(); pit != b->producers().end(); ++pit) {
48  p = *pit;
49  if (p->Produces(commodity)) {
50  ctx.obj_coeffs.push_back(p->Cost(commodity));
51  caps.insert(idx, p->Capacity(commodity));
52  ctx.col_lbs.push_back(0);
53  ctx.col_ubs.push_back(inf);
54  p_to_b[p] = b;
55  idx_to_p[idx] = p;
56  idx++;
57  }
58  }
59  }
60  ctx.row_ubs.push_back(inf);
61  ctx.row_lbs.push_back(demand);
62  ctx.m.setDimensions(0, ctx.col_ubs.size());
63  ctx.m.appendRow(caps);
64 }
65 
66 void BuildingManager::Solve_(OsiCbcSolverInterface& iface,
68  std::map<CommodityProducer*, Builder*>& p_to_b,
69  std::map<int, CommodityProducer*>& idx_to_p,
70  std::vector<BuildOrder>& orders) {
71  int nvar = ctx.col_ubs.size();
72  iface.setObjSense(1.0); // minimize
73  iface.loadProblem(ctx.m, &ctx.col_lbs[0], &ctx.col_ubs[0],
74  &ctx.obj_coeffs[0], &ctx.row_lbs[0], &ctx.row_ubs[0]);
75  for (int i = 0; i != nvar; i++) {
76  iface.setInteger(i);
77  }
78  iface.initialSolve();
79  iface.branchAndBound();
80 
81  const double* sol = iface.getColSolution();
82  int n;
84  Builder* b;
85  for (int i = 0; i != nvar; i++) {
86  n = static_cast<int>(sol[i]);
87  if (n > 0) {
88  p = idx_to_p[i];
89  b = p_to_b[p];
90  orders.push_back(BuildOrder(n, b, p));
91  }
92  }
93 }
94 
95 } // namespace toolkit
96 } // namespace cyclus
std::vector< BuildOrder > MakeBuildDecision(Commodity &commodity, double demand)
Given a certain commodity and demand, a decision is made as to how many producers of each available t...
const std::set< CommodityProducer * > & producers() const
Definition: builder.h:31
A simple class defining a commodity; it is currently super simple.
Definition: commodity.h:12
double b(int nuc)
Computes the scattering length [cm] from the coherent and incoherent components.
Definition: pyne.cc:11180
virtual const double * getColSolution() const
Get pointer to array[getNumCols()] of primal solution vector.
virtual void initialSolve()
Solve initial LP relaxation.
virtual void setObjSense(double s)
Set objective function sense (1 for min (default), -1 for max,)
virtual double getInfinity() const
Get solver&#39;s value for infinity.
struct to hold all problem instance state
double Capacity(const Commodity &commodity)
virtual void branchAndBound()
Invoke solver&#39;s built-in enumeration algorithm.
BuildOrder(int n, Builder *b, CommodityProducer *cp)
A mixin to provide information about commodity producers that can be built.
Definition: builder.h:14
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or.org/Osi/browser/trunk.
Definition: agent.cc:14
std::vector< double > row_lbs
virtual void loadProblem(const CoinPackedMatrix &matrix, const double *collb, const double *colub, const double *obj, const double *rowlb, const double *rowub)
Load in an problem by copying the arguments (the constraints on the rows are given by lower and upper...
A mixin to provide information about produced commodities.
std::vector< double > col_ubs
double Cost(const Commodity &commodity)
std::vector< double > obj_coeffs
virtual void setInteger(int index)
Set the index-th variable to be an integer variable.
std::vector< double > row_ubs
std::vector< double > col_lbs
bool Produces(const Commodity &commodity) const