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()) ret = 1;
20 break;
21 case 3:
22 case 4:
23 case 5:
24 break;
25 }
26 return ret;
27}
28
30 : obj_(obj), time_(time), found_(found){};
31
33 : obj_(obj), time_(0), found_(false){};
34
36
38 : CbcEventHandler(other) {
39 obj_ = other.obj();
40 time_ = other.time();
41 found_ = other.found();
42}
43
45 if (this != &other) {
46 obj_ = other.obj();
47 time_ = other.time();
48 found_ = other.found();
49 CbcEventHandler::operator=(other);
50 }
51 return *this;
52}
53
54CbcEventHandler* ObjValueHandler::clone() {
55 return new ObjValueHandler(*this);
56}
57
58CbcEventHandler::CbcAction ObjValueHandler::event(CbcEvent e) {
59 if (!found_ && (e == solution || e == heuristicSolution)) {
60 const CbcModel* m = getModel();
61 double cbcobj = m->getObjValue();
62 if (cbcobj < obj_) {
63 time_ = CoinCpuTime() - m->getDblParam(CbcModel::CbcStartSeconds);
64 found_ = true;
65 }
66 }
67 return noAction;
68}
69
70// 10800 s = 3 hrs * 60 min/hr * 60 s/min
71#define CYCLUS_SOLVER_TIMEOUT 10800
72
75 : t_(t), tmax_(CYCLUS_SOLVER_TIMEOUT) {}
76SolverFactory::SolverFactory(std::string t, double tmax) : t_(t), tmax_(tmax) {}
77
78OsiSolverInterface* SolverFactory::get() {
79 if (t_ == "clp" || t_ == "cbc") {
80 OsiClpSolverInterface* s = new OsiClpSolverInterface();
81 s->getModelPtr()->setMaximumSeconds(tmax_);
82 return s;
83 } else {
84 throw ValueError("invalid SolverFactory type '" + t_ + "'");
85 }
86}
87
88void ReportProg(OsiSolverInterface* si) {
89 const double* objs = si->getObjCoefficients();
90 const double* clbs = si->getColLower();
91 const double* cubs = si->getColUpper();
92 int ncol = si->getNumCols();
93 std::cout << "Column info\n";
94 for (int i = 0; i != ncol; i++) {
95 std::cout << i << " obj" << ": " << objs[i] << " lb" << ": " << clbs[i]
96 << " ub" << ": " << cubs[i] << " int" << ": " << std::boolalpha
97 << si->isInteger(i) << '\n';
98 }
99
100 const CoinPackedMatrix* m = si->getMatrixByRow();
101 const double* rlbs = si->getRowLower();
102 const double* rubs = si->getRowUpper();
103 int nrow = si->getNumRows();
104 std::cout << "Row info\n";
105 for (int i = 0; i != nrow; i++) {
106 std::cout << i << " lb" << ": " << rlbs[i] << " ub" << ": " << rubs[i]
107 << '\n';
108 }
109 std::cout << "matrix:\n";
110 m->dumpMatrix();
111}
112
113void SolveProg(OsiSolverInterface* si, double greedy_obj, bool verbose) {
114 if (verbose) ReportProg(si);
115
116 if (HasInt(si)) {
117 CbcModel model(*si);
118 ObjValueHandler handler(greedy_obj);
119 model.passInEventHandler(&handler);
120 model.setLogLevel(0);
121 model.initialSolve();
122 model.branchAndBound();
123 si->setColSolution(model.bestSolution());
124 if (verbose) {
125 std::cout << "Greedy equivalent time: " << handler.time() << " and obj "
126 << handler.obj() << " and found " << std::boolalpha
127 << handler.found() << "\n";
128 }
129 } else {
130 // no ints, just solve 'initial lp relaxation'
131 si->initialSolve();
132 }
133
134 if (verbose) {
135 const double* soln = si->getColSolution();
136 for (int i = 0; i != si->getNumCols(); i++) {
137 std::cout << "soln " << i << ": " << soln[i]
138 << " integer: " << std::boolalpha << si->isInteger(i) << "\n";
139 }
140 }
141}
142
143void SolveProg(OsiSolverInterface* si) {
144 SolveProg(si, si->getInfinity(), false);
145}
146
147void SolveProg(OsiSolverInterface* si, bool verbose) {
148 SolveProg(si, si->getInfinity(), verbose);
149}
150
151void SolveProg(OsiSolverInterface* si, double greedy_obj) {
152 SolveProg(si, greedy_obj, false);
153}
154
155bool HasInt(OsiSolverInterface* si) {
156 int i = 0;
157 for (i = 0; i != si->getNumCols(); i++) {
158 if (si->isInteger(i)) {
159 return true;
160 }
161 }
162 return false;
163}
164
165} // 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:37
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
#define CYCLUS_SOLVER_TIMEOUT