Debian Codegraph @

Bonmin/experimental/Bcp/BM_tm.cpp

raw
1 // (C) Copyright International Business Machines Corporation 2006, 2007
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // Laszlo Ladanyi, International Business Machines Corporation
7 // Pierre Bonami, Carnegie Mellon University
8
9 #include "OsiClpSolverInterface.hpp"
10
11 #include "BCP_problem_core.hpp"
12 #include "BCP_tm.hpp"
13 #include "BCP_lp.hpp"
14
15 // #include "BonIpoptSolver.hpp"
16 // #ifdef COIN_HAS_FILTERSQP
17 // # include "BonFilterSolver.hpp"
18 // #endif
19 // #include "BonOACutGenerator2.hpp"
20 // #include "BonEcpCuts.hpp"
21 // #include "BonOaNlpOptim.hpp"
22 #include "BonAmplSetup.hpp"
23
24 #include "BM.hpp"
25
26 //#############################################################################
27
28 void
29 BM_tm::readIpopt()
30 {
31 if (par.entry(BM_par::IpoptParamfile).length() != 0) {
32 FILE* ipopt = fopen(par.entry(BM_par::IpoptParamfile).c_str(), "r");
33 if (!ipopt) {
34 throw BCP_fatal_error("Non-existent IpoptParamfile\n");
35 }
36 fseek(ipopt, 0, SEEK_END);
37 int len = ftell(ipopt) + 1;
38 fseek(ipopt, 0, SEEK_SET);
39 char* ipopt_content = new char[len+1];
40 len = fread(ipopt_content, 1, len, ipopt);
41 ipopt_file_content.assign(ipopt_content, len);
42 delete[] ipopt_content;
43 }
44
45 FILE* nl = fopen(par.entry(BM_par::NL_filename).c_str(), "r");
46 if (!nl) {
47 throw BCP_fatal_error("Non-existent NL_filename\n");
48 }
49 fseek(nl, 0, SEEK_END);
50 int len = ftell(nl) + 1;
51 fseek(nl, 0, SEEK_SET);
52 char* nl_content = new char[len+1];
53 len = fread(nl_content, 1, len, nl);
54 nl_file_content.assign(nl_content, len);
55 delete[] nl_content;
56 }
57
58 /****************************************************************************/
59
60 void
61 BM_tm::initialize_core(BCP_vec<BCP_var_core*>& vars,
62 BCP_vec<BCP_cut_core*>& cuts,
63 BCP_lp_relax*& matrix)
64 {
65 char* argv_[3];
66 char** argv = argv_;
67 argv[0] = NULL;
68 argv[1] = strdup(par.entry(BM_par::NL_filename).c_str());
69 argv[2] = NULL;
70
71 /* Get the basic options. */
72 Bonmin::BonminAmplSetup bonmin;
73 bonmin.readOptionsFile(par.entry(BM_par::IpoptParamfile).c_str());
74 bonmin.initialize(argv);
75
76 free(argv[1]);
77
78 Bonmin::OsiTMINLPInterface& nlp = *bonmin.nonlinearSolver();
79 #if defined(BM_DISREGARD_SOS)
80 const Bonmin::TMINLP::SosInfo * sos = nlp.model()->sosConstraints();
81 if (sos->num > 0) {
82 printf("There are SOS constraints... disregarding them...\n");
83 }
84 #endif
85
86 OsiSolverInterface& clp = *bonmin.continuousSolver();
87
88 const int numCols = clp.getNumCols();
89 const int numRows = clp.getNumRows();
90
91 const double* clb = clp.getColLower();
92 const double* cub = clp.getColUpper();
93
94 double* obj = new double[numCols];
95 if (bonmin.getAlgorithm() == Bonmin::B_BB /* pure B&B */) {
96 std::cout<<"Doing branch and bound"<<std::endl;
97 CoinFillN(obj, numCols, 0.0);
98 matrix = NULL;
99 } else {
100 std::cout<<"Doing hybrid"<<std::endl;
101 CoinDisjointCopyN(clp.getObjCoefficients(), numCols, obj);
102 cuts.reserve(numRows);
103 const double* rlb = clp.getRowLower();
104 const double* rub = clp.getRowUpper();
105 for (int i = 0; i < numRows; ++i) {
106 cuts.push_back(new BCP_cut_core(rlb[i], rub[i]));
107 }
108 matrix = new BCP_lp_relax(true /*column major ordered*/);
109 matrix->copyOf(*clp.getMatrixByCol(), obj, clb, cub, rlb, rub);
110 }
111
112 vars.reserve(numCols);
113 for (int i = 0; i < numCols; ++i) {
114 BCP_var_t type = BCP_ContinuousVar;
115 if (clp.isBinary(i)) type = BCP_BinaryVar;
116 if (clp.isInteger(i)) type = BCP_IntegerVar;
117 vars.push_back(new BCP_var_core(type, obj[i], clb[i], cub[i]));
118 }
119 delete[] obj;
120
121 /* Initialize pseudocosts */
122 int nObj = 0;
123 for (int i = 0; i < numCols; ++i) {
124 if (nlp.isInteger(i)) {
125 ++nObj;
126 }
127 }
128 #if ! defined(BM_DISREGARD_SOS)
129 const Bonmin::TMINLP::SosInfo * sos = nlp.model()->sosConstraints();
130 nObj += sos->num;
131 #endif
132 pseudoCosts_.initialize(nObj);
133 }
134
135 /****************************************************************************/
136 void
137 BM_tm::create_root(BCP_vec<BCP_var*>& added_vars,
138 BCP_vec<BCP_cut*>& added_cuts,
139 BCP_user_data*& user_data)
140 {
141 BM_node* data = new BM_node;
142 data->numNlpFailed_ = 0;
143 user_data = data;
144 }
145
146 void
147 BM_tm::write_AMPL_solution(const BCP_solution* sol,
148 bool write_file, bool write_screen)
149 {
150 const BCP_solution_generic* bg =
151 dynamic_cast<const BCP_solution_generic*>(sol);
152
153 /* Parse again the input file so that we have a nice and clean ampl
154 setup */
155 char* argv_[3];
156 char** argv = argv_;
157 argv[0] = NULL;
158 argv[1] = strdup(par.entry(BM_par::NL_filename).c_str());
159 argv[2] = NULL;
160 Bonmin::BonminAmplSetup bonmin;
161 bonmin.initialize(argv);
162
163 Bonmin::OsiTMINLPInterface& nlpSolver = *bonmin.nonlinearSolver();
164 free(argv[1]);
165 OsiSolverInterface& clp = *bonmin.continuousSolver();
166
167 const int numCols = clp.getNumCols();
168
169 int i;
170
171 /* Create a dense vector with the value of each variable. Round the
172 integer vars (they were tested to be near enough to integrality) so
173 in the printouts we won't have 9.99999991234, etc.) */
174 double* dsol = new double[numCols];
175 for (i = 0; i < numCols; ++i) {
176 dsol[i] = 0.0;
177 }
178 const BCP_vec<BCP_var*>& vars = bg->_vars;
179 const BCP_vec<double>& values = bg->_values;;
180 const int size = vars.size();
181 for (i = 0; i < size; ++i) {
182 const int ind = vars[i]->bcpind();
183 const double v = values[i];
184 const BCP_var_t type = vars[i]->var_type();
185 dsol[ind] = (type == BCP_ContinuousVar) ? v : floor(v+0.5);
186 }
187
188 if (write_screen) {
189 /* Display the solution on the screen */
190 printf("bonmin: feasible solution found. Objective value: %f\n",
191 bg->objective_value());
192 for (i = 0; i < size; ++i) {
193 printf(" index: %5i value: %f\n",
194 vars[i]->bcpind(), dsol[vars[i]->bcpind()]);
195 }
196 printf("\n");
197 }
198
199 if (write_file) {
200 /* create the AMPL solfile */
201 nlpSolver.model()->finalize_solution(Bonmin::TMINLP::SUCCESS, nlpSolver.getNumCols(),
202 dsol, bg->objective_value());
203 }
204 delete[] dsol;
205 }
206
207 //#############################################################################
208
209 void
210 BM_tm::process_message(BCP_buffer& buf)
211 {
212 int msgtag;
213 buf.unpack(msgtag);
214 if (msgtag == BM_PseudoCostUpdate) {
215 receive_pseudo_cost_update(buf);
216 }
217 }
218
219 /****************************************************************************/
220
221 void
222 BM_tm::display_final_information(const BCP_lp_statistics& lp_stat)
223 {
224 bool write_screen = false;
225 BCP_tm_prob *p = getTmProblemPointer();
226 if (p->param(BCP_tm_par::TmVerb_FinalStatistics)) {
227 printf("TM: Running time: %.3f\n", CoinWallclockTime() - p->start_time);
228 printf("TM: search tree size: %i ( processed %i ) max depth: %i\n",
229 int(p->search_tree.size()), int(p->search_tree.processed()),
230 p->search_tree.maxdepth());
231 lp_stat.display();
232
233 if (! p->feas_sol) {
234 printf("TM: No feasible solution is found\n");
235 } else {
236 printf("TM: The best solution found has value %f\n",
237 p->feas_sol->objective_value());
238 if (p->param(BCP_tm_par::TmVerb_BestFeasibleSolution)) {
239 write_screen = true;
240 }
241 }
242 }
243 if (p->feas_sol) {
244 write_AMPL_solution(p->feas_sol, true, write_screen);
245 }
246 }
247
248 /****************************************************************************/
249
250 void
251 BM_tm::display_feasible_solution(const BCP_solution* sol)
252 {
253 // For now, we want to write also the AMPL solution file...(?)
254 // This needs to be changed though
255 write_AMPL_solution(sol, true, true);
256 }
257
258 struct BMSearchTreeCompareBest {
259 static const std::string compName;
260 static inline const char* name() { return "BMSearchTreeCompareBest"; }
261 inline bool operator()(const CoinTreeSiblings* x,
262 const CoinTreeSiblings* y) const {
263 double allowable_gap = 1e-8;
264 const double xq = x->currentNode()->getQuality();
265 const double yq = y->currentNode()->getQuality();
266 const int xd = x->currentNode()->getDepth();
267 const int yd = y->currentNode()->getDepth();
268 return ((xq < yq-allowable_gap) ||
269 (xq <= yq+allowable_gap && xd > yd));
270 }
271 };
272
273 const std::string
274 BMSearchTreeCompareBest::compName("BMSearchTreeCompareBest");
275
276 void
277 BM_tm::init_new_phase(int phase,
278 BCP_column_generation& colgen,
279 CoinSearchTreeBase*& candidates)
280 {
281 BCP_tm_prob* p = getTmProblemPointer();
282 colgen = BCP_DoNotGenerateColumns_Fathom;
283 switch (p->param(BCP_tm_par::TreeSearchStrategy)) {
284 case BCP_BestFirstSearch:
285 candidates = new CoinSearchTree<BMSearchTreeCompareBest>;
286 printf("Creating candidate list with BMSearchTreeCompareBest\n");
287 break;
288 case BCP_BreadthFirstSearch:
289 candidates = new CoinSearchTree<CoinSearchTreeCompareBreadth>;
290 printf("Creating candidate list with CoinSearchTreeCompareBreadth\n");
291 break;
292 case BCP_DepthFirstSearch:
293 candidates = new CoinSearchTree<CoinSearchTreeCompareDepth>;
294 printf("Creating candidate list with CoinSearchTreeCompareDepth\n");
295 break;
296 case BCP_PreferredFirstSearch:
297 candidates = new CoinSearchTree<CoinSearchTreeComparePreferred>;
298 printf("Creating candidate list with CoinSearchTreeComparePreferred\n");
299 break;
300 }
301 }
302
303 //#############################################################################
304
305 void
306 BM_tm::receive_pseudo_cost_update(BCP_buffer& buf)
307 {
308 double* upTotalChange = pseudoCosts_.upTotalChange();
309 double* downTotalChange = pseudoCosts_.downTotalChange();
310 int* upNumber = pseudoCosts_.upNumber();
311 int* downNumber = pseudoCosts_.downNumber();
312 int objInd;
313 int branch;
314 double pcChange;
315 while (true) {
316 buf.unpack(objInd);
317 if (objInd == -1) {
318 break;
319 }
320 buf.unpack(branch);
321 buf.unpack(pcChange);
322 if (branch == 0) {
323 downTotalChange[objInd] += pcChange;
324 ++downNumber[objInd];
325 } else {
326 upTotalChange[objInd] += pcChange;
327 ++upNumber[objInd];
328 }
329 }
330 }
331
332 //-----------------------------------------------------------------------------
333
334 void
335 BM_tm::pack_pseudo_costs(BCP_buffer& buf)
336 {
337 buf.pack(pseudoCosts_.upTotalChange(), pseudoCosts_.numberObjects());
338 buf.pack(pseudoCosts_.upNumber(), pseudoCosts_.numberObjects());
339 buf.pack(pseudoCosts_.downTotalChange(), pseudoCosts_.numberObjects());
340 buf.pack(pseudoCosts_.downNumber(), pseudoCosts_.numberObjects());
341 }