5#include "CoinPackedVector.hpp"
6#include "OsiSolverInterface.hpp"
20 pseudo_cost_(
std::numeric_limits<double>::max()) {
29 pseudo_cost_(
std::numeric_limits<double>::max()) {
35 : g_(g), iface_(iface), excl_(false), pseudo_cost_(pseudo_cost) {
40 bool exclusive,
double pseudo_cost)
41 : g_(g), iface_(iface), excl_(exclusive), pseudo_cost_(pseudo_cost) {
45void ProgTranslator::Init() {
46 arc_offset_ = g_->
arcs().size();
51 ctx_.
m = CoinPackedMatrix(
false, 0, 0);
54void ProgTranslator::CheckPref(
double pref) {
57 ss <<
"Preference value found to be nonpositive (" << pref
58 <<
"). Preferences must be positive when using an optimization solver."
59 <<
" If using Cyclus in simulation mode (e.g., from the command line),"
60 <<
" this error is likely a bug in Cyclus. Please report it to the "
62 <<
"list (https://groups.google.com/forum/#!forum/cyclus-dev).";
63 throw ValueError(ss.str());
71 std::vector<RequestGroup::Ptr>& rgs = g_->request_groups();
72 for (
int i = 0; i != g_->request_groups().size(); ++i)
73 nfalse += rgs[i].get()->HasArcs() ? 1 : 0;
74 int n_cols = g_->arcs().size() + nfalse;
75 ctx_.m.setDimensions(0, n_cols);
78 std::vector<ExchangeNodeGroup::Ptr>& sgs = g_->supply_groups();
79 for (
int i = 0; i != sgs.size(); i++) {
81 XlateGrp_(sgs[i].get(), request);
84 for (
int i = 0; i != rgs.size(); i++) {
86 XlateGrp_(rgs[i].get(), request);
92 double inf = iface_->getInfinity();
93 for (
int i = g_->arcs().size(); i != arc_offset_; i++) {
94 ctx_.obj_coeffs[i] = pseudo_cost_;
96 ctx_.col_ubs[i] = inf;
101 iface_->setObjSense(1.0);
104 iface_->loadProblem(ctx_.m, &ctx_.col_lbs[0], &ctx_.col_ubs[0],
105 &ctx_.obj_coeffs[0], &ctx_.row_lbs[0], &ctx_.row_ubs[0]);
108 std::vector<Arc>& arcs = g_->arcs();
109 for (
int i = 0; i != arcs.size(); i++) {
112 iface_->setInteger(g_->arc_ids()[a]);
124 double inf = iface_->getInfinity();
125 std::vector<double>& caps = grp->
capacities();
127 if (request && !grp->
HasArcs())
130 std::vector<CoinPackedVector> cap_rows;
131 std::vector<CoinPackedVector> excl_rows;
132 for (
int i = 0; i != caps.size(); i++) {
133 cap_rows.push_back(CoinPackedVector());
136 std::vector<ExchangeNode::Ptr>& nodes = grp->
nodes();
137 for (
int i = 0; i != nodes.size(); i++) {
138 std::map<Arc, std::vector<double>>& ucap_map = nodes[i]->unit_capacities;
139 std::map<Arc, std::vector<double>>::iterator cap_it;
142 for (cap_it = ucap_map.begin(); cap_it != ucap_map.end(); ++cap_it) {
143 const Arc& a = cap_it->first;
144 std::vector<double>& ucaps = cap_it->second;
148 for (
int j = 0; j != ucaps.size(); j++) {
149 double coeff = ucaps[j];
150 if (excl_ && a.exclusive()) {
151 coeff *= a.excl_val();
154 cap_rows[j].insert(arc_id, coeff);
160 ctx_.col_lbs[arc_id] = 0;
161 ctx_.col_ubs[arc_id] =
162 (excl_ && a.exclusive()) ? 1 : std::min(nodes[i]->qty, inf);
169 faux_id = arc_offset_++;
173 for (
int i = 0; i != cap_rows.size(); i++) {
175 cap_rows[i].insert(faux_id, 1.0);
180 double rlb = std::min(caps[i], 1e15);
181 ctx_.row_lbs.push_back(request ? rlb : 0);
182 ctx_.row_ubs.push_back(request ? inf : caps[i]);
183 ctx_.m.appendRow(cap_rows[i]);
188 std::vector<std::vector<ExchangeNode::Ptr>>& exngs =
190 for (
int i = 0; i != exngs.size(); i++) {
191 CoinPackedVector excl_row;
192 std::vector<ExchangeNode::Ptr>& nodes = exngs[i];
193 for (
int j = 0; j != nodes.size(); j++) {
194 std::vector<Arc>& arcs = g_->node_arc_map()[nodes[j]];
195 for (
int k = 0; k != arcs.size(); k++) {
196 excl_row.insert(g_->arc_ids()[arcs[k]], 1.0);
199 if (excl_row.getNumElements() > 0) {
200 excl_rows.push_back(excl_row);
205 for (
int i = 0; i != excl_rows.size(); i++) {
206 ctx_.row_lbs.push_back(0.0);
207 ctx_.row_ubs.push_back(1.0);
208 ctx_.m.appendRow(excl_rows[i]);
214 const double* sol = iface_->getColSolution();
215 std::vector<Arc>& arcs = g_->arcs();
217 for (
int i = 0; i < arcs.size(); i++) {
218 Arc& a = g_->arc_by_id().at(i);
222 g_->AddMatch(a, flow);
An arc represents a possible connection between two nodes in the bipartite resource exchange graph.
An ExchangeGraph is a resource-neutral representation of a ResourceExchange.
const std::vector< Arc > & arcs() const
const std::map< Arc, int > & arc_ids() const
const std::vector< RequestGroup::Ptr > & request_groups() const
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
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
static double Cost(const Arc &a, bool exclusive_orders=kDefaultExclusive)
return the cost of an arc
ProgTranslator(ExchangeGraph *g, OsiSolverInterface *iface)
constructor
void FromProg()
translates solution from iface back into graph matches
void Populate()
populates the solver interface with values from the translators Context
void ToProg()
translates graph into mathematic program via iface.
void Translate()
translates the graph, filling the translators Context
Code providing rudimentary logging capability for the Cyclus core.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
@ LEV_DEBUG1
debugging information - least verbose
double eps()
a generic epsilon value
std::vector< double > col_lbs
std::vector< double > col_ubs
std::vector< double > obj_coeffs