CYCLUS
Loading...
Searching...
No Matches
solver_factory.cc
Go to the documentation of this file.
1#include "solver_factory.h"
2
3#include <iostream>
4
5#include "OsiClpSolverInterface.hpp"
7#include "CbcSolver.hpp"
8#include "CoinTime.hpp"
9
10#include "error.h"
11
12namespace cyclus {
13
14int CbcCallBack(CbcModel * model, int from) {
15 int ret = 0;
16 switch (from) {
17 case 1:
18 case 2:
19 if (!model->status() && model->secondaryStatus())
20 ret=1;
21 break;
22 case 3:
23 case 4:
24 case 5:
25 break;
26 }
27 return ret;
28}
29
30ObjValueHandler::ObjValueHandler(double obj, double time, bool found)
31 : obj_(obj),
32 time_(time),
33 found_(found) { };
34
36 : obj_(obj),
37 time_(0),
38 found_(false) { };
39
41
43 obj_ = other.obj();
44 time_ = other.time();
45 found_ = other.found();
46}
47
49 if (this != &other) {
50 obj_ = other.obj();
51 time_ = other.time();
52 found_ = other.found();
53 CbcEventHandler::operator=(other);
54 }
55 return *this;
56}
57
58CbcEventHandler* ObjValueHandler::clone() {
59 return new ObjValueHandler(*this);
60}
61
62CbcEventHandler::CbcAction ObjValueHandler::event(CbcEvent e) {
63 if (!found_ && (e == solution || e == heuristicSolution)) {
64 const CbcModel* m = getModel();
65 double cbcobj = m->getObjValue();
66 if (cbcobj < obj_) {
67 time_ = CoinCpuTime() -
68 m->getDblParam(CbcModel::CbcStartSeconds);
69 found_ = true;
70 }
71 }
72 return noAction;
73}
74
75// 10800 s = 3 hrs * 60 min/hr * 60 s/min
76#define CYCLUS_SOLVER_TIMEOUT 10800
77
79SolverFactory::SolverFactory(std::string t) : t_(t), tmax_(CYCLUS_SOLVER_TIMEOUT) { }
80SolverFactory::SolverFactory(std::string t, double tmax)
81 : t_(t),
82 tmax_(tmax) { }
83
84OsiSolverInterface* SolverFactory::get() {
85 if (t_ == "clp" || t_ == "cbc") {
87 s->getModelPtr()->setMaximumSeconds(tmax_);
88 return s;
89 } else {
90 throw ValueError("invalid SolverFactory type '" + t_ + "'");
91 }
92}
93
94void ReportProg(OsiSolverInterface* si) {
95 const double* objs = si->getObjCoefficients();
96 const double* clbs = si->getColLower();
97 const double* cubs = si->getColUpper();
98 int ncol = si->getNumCols();
99 std::cout << "Column info\n";
100 for (int i = 0; i != ncol; i ++) {
101 std::cout << i
102 << " obj" << ": " << objs[i]
103 << " lb" << ": " << clbs[i]
104 << " ub" << ": " << cubs[i]
105 << " int" << ": " << std::boolalpha << si->isInteger(i) << '\n';
106 }
107
108 const CoinPackedMatrix* m = si->getMatrixByRow();
109 const double* rlbs = si->getRowLower();
110 const double* rubs = si->getRowUpper();
111 int nrow = si->getNumRows();
112 std::cout << "Row info\n";
113 for (int i = 0; i != nrow; i ++) {
114 std::cout << i
115 << " lb" << ": " << rlbs[i]
116 << " ub" << ": " << rubs[i] << '\n';
117 }
118 std::cout << "matrix:\n";
119 m->dumpMatrix();
120}
121
122void SolveProg(OsiSolverInterface* si, double greedy_obj, bool verbose) {
123 if (verbose)
124 ReportProg(si);
125
126 if (HasInt(si)) {
127 CbcModel model(*si);
129 model.passInEventHandler(&handler);
130 model.setLogLevel(0);
131 model.initialSolve();
132 model.branchAndBound();
133 si->setColSolution(model.bestSolution());
134 if (verbose) {
135 std::cout << "Greedy equivalent time: " << handler.time()
136 << " and obj " << handler.obj()
137 << " and found " << std::boolalpha << handler.found() << "\n";
138 }
139 } else {
140 // no ints, just solve 'initial lp relaxation'
141 si->initialSolve();
142 }
143
144 if (verbose) {
145 const double* soln = si->getColSolution();
146 for (int i = 0; i != si->getNumCols(); i ++) {
147 std::cout << "soln " << i << ": " << soln[i]
148 << " integer: " << std::boolalpha << si->isInteger(i) << "\n";
149 }
150 }
151}
152
153void SolveProg(OsiSolverInterface* si) {
154 SolveProg(si, si->getInfinity(), false);
155}
156
157void SolveProg(OsiSolverInterface* si, bool verbose) {
158 SolveProg(si, si->getInfinity(), verbose);
159}
160
161void SolveProg(OsiSolverInterface* si, double greedy_obj) {
162 SolveProg(si, greedy_obj, false);
163}
164
165bool HasInt(OsiSolverInterface* si) {
166 int i = 0;
167 for (i = 0; i != si->getNumCols(); i++) {
168 if (si->isInteger(i)) {
169 return true;
170 }
171 }
172 return false;
173}
174
175} // namespace cyclus
An event handler that records the time that a better solution is found.
virtual CbcEventHandler::CbcAction event(CbcEvent e)
ObjValueHandler & operator=(const ObjValueHandler &other)
ObjValueHandler(double obj, double time, bool found)
virtual CbcEventHandler * clone()
OsiSolverInterface * get()
get the configured solver
SolverFactory()
currently supported solver types are 'clp' and 'cbc'
For values that are too big, too small, etc.
Definition error.h:41
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
Definition agent.cc:14
void ReportProg(OsiSolverInterface *si)
void SolveProg(OsiSolverInterface *si, double greedy_obj, bool verbose)
bool HasInt(OsiSolverInterface *si)
int CbcCallBack(CbcModel *model, int from)
this is taken exactly from driver4.cpp in the Cbc examples
T OptionalQuery(InfileTree *tree, std::string query, T default_val)
a query method for optional parameters
#define CYCLUS_SOLVER_TIMEOUT