Debian Codegraph @

Bonmin/experimental/Bcp/BM.hpp

raw
1 // (C) Copyright International Business Machines Corporation and Carnegie Mellon University 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 // Date : 03/15/2006
10 #ifndef _BM_H
11 #define _BM_H
12
13 #include "BCP_USER.hpp"
14 #include "BCP_parameters.hpp"
15 #include "BCP_tm_user.hpp"
16 #include "BCP_lp_user.hpp"
17
18 #include "BB_cut.hpp"
19
20 #include "BonIpoptWarmStart.hpp"
21
22 #define BM_DISREGARD_SOS
23
24 //#############################################################################
25
26 class BM_node : public BCP_user_data {
27 public:
28 /** A counter for how many times in a row did the NLP code fail. When the
29 NLP fails we branch; hopefully it'll be OK in the children. If it
30 fails too many times in a row then we fathom the node: it's hopelessly
31 difficult. */
32 int numNlpFailed_;
33 public:
34 BM_node() : numNlpFailed_(0) {}
35 BM_node(BCP_buffer& buf) : numNlpFailed_(0) {
36 buf.unpack(numNlpFailed_);
37 }
38 ~BM_node() {}
39
40 inline void pack(BCP_buffer& buf) const {
41 buf.pack(numNlpFailed_);
42 }
43 };
44
45 //#############################################################################
46
47 enum BM_message {
48 BM_StrongBranchRequest,
49 BM_StrongBranchResult,
50 BM_PseudoCostUpdate
51 };
52
53 enum BM_BoundChange {
54 BM_Var_DownBranch,
55 BM_Var_UpBranch
56 };
57
58 //#############################################################################
59
60 class BM_par {
61 public:
62 enum chr_params {
63 //
64 DisregardPriorities,
65 PrintBranchingInfo,
66 end_of_chr_params
67 };
68 enum int_params {
69 //
70 UsePseudoCosts,
71 DecreasingSortInSetupList,
72 PreferHighCombinationInBranching,
73 NumNlpFailureMax,
74
75 // twice the number of candidates if all candidates have 2 children.
76 // We want to do SB on at least this many (if there are this many)
77 SBNumBranchesInRoot,
78
79 SBNumBranchesInTree,
80 // The level where (and below) there are no min number of branches to
81 // be considered and SB need not be done (we can use pseudo costs
82 // instead)
83 SBMaxLevel,
84
85 end_of_int_params
86 };
87 enum dbl_params {
88 dummy_dbl_param,
89 //
90 end_of_dbl_params
91 };
92 enum str_params {
93 NL_filename,
94 IpoptParamfile,
95 //
96 end_of_str_params
97 };
98 enum str_array_params {
99 dummy_str_array_param,
100 //
101 end_of_str_array_params
102 };
103 };
104
105 //#############################################################################
106
107 class BM_stats {
108 public:
109 BM_stats() :
110 numberNodeSolves_(0),
111 numberSbSolves_(0),
112 numberFixed_(0),
113 numberStrongBranching_(0),
114 sumStrongBranchingListIndices_(0),
115 sumStrongBranchingListPositions_(0.)
116 {}
117
118 ~BM_stats();
119
120 inline void incNumberNodeSolves() {
121 numberNodeSolves_++;
122 }
123 inline void incNumberSbSolves(int cnt) {
124 numberSbSolves_ += cnt;
125 }
126 inline void incNumberFixed() {
127 numberFixed_++;
128 }
129 inline void updateStrongBrachingInfo(int chosenIndex, int listLength) {
130 numberStrongBranching_++;
131 sumStrongBranchingListIndices_ += chosenIndex;
132 sumStrongBranchingListPositions_ +=
133 (double)(listLength-chosenIndex)/(double)listLength;
134 }
135 private:
136 /** Total number of NLP solves as node solves */
137 int numberNodeSolves_;
138 /** Total number of NLP solves for strong-branching */
139 int numberSbSolves_;
140 /** Total number of times variables were fixed due to strong branching */
141 int numberFixed_;
142 /** Total number of times this node did strong branching */
143 int numberStrongBranching_;
144 /** Sum of all list indices */
145 int sumStrongBranchingListIndices_;
146 /** Sum of all relative list positions */
147 double sumStrongBranchingListPositions_;
148 };
149
150 //#############################################################################
151
152 // Data needed to be sent off to do strong branching
153
154 struct BM_BranchData {
155 // These are the input for doing the branching
156 int changeType;
157 int objInd;
158 int colInd;
159 double solval;
160 double bd;
161 // These are the results of doing the branching
162 int status;
163 double objval;
164 int iter;
165 double time;
166 };
167
168 //#############################################################################
169
170 class BM_tm : public BCP_tm_user {
171
172 public:
173
174 /**@name Private data member */
175 BCP_string ipopt_file_content;
176 BCP_string nl_file_content;
177 BCP_parameter_set<BM_par> par;
178 OsiPseudoCosts pseudoCosts_;
179
180 public:
181
182 /**@name Constructors and destructors */
183 //@{
184 /// Default constructor
185 BM_tm() {}
186
187 /// Default destructor
188 virtual ~BM_tm() {}
189 //@}
190
191 /**@name Packing and unpacking methods */
192 //@{
193 virtual void pack_module_data(BCP_buffer& buf, BCP_process_t ptype);
194
195 //@}
196
197 /// Pass the core constraints and core variables to bcp
198 virtual void initialize_core(BCP_vec<BCP_var_core*>& vars,
199 BCP_vec<BCP_cut_core*>& cuts,
200 BCP_lp_relax*& matrix);
201
202 /** Create the set of extra variables and cuts that should be added to the
203 formulation in the root node. Also decide how variable pricing shuld be
204 done, that is, if column generation is requested in the
205 init_new_phase() method of this class then column
206 generation should be performed according to \c pricing_status.
207 */
208 virtual void
209 create_root(BCP_vec<BCP_var*>& added_vars,
210 BCP_vec<BCP_cut*>& added_cuts,
211 BCP_user_data*& user_data);
212
213 /// Print a feasible solution
214 virtual void display_feasible_solution(const BCP_solution* sol);
215
216 /** Process a message that has been sent by another process' user part to
217 this process' user part. */
218 virtual void
219 process_message(BCP_buffer& buf);
220
221 void receive_pseudo_cost_update(BCP_buffer& buf);
222 void pack_pseudo_costs(BCP_buffer& buf);
223
224 /// Output the final solution
225 virtual void display_final_information(const BCP_lp_statistics& lp_stat);
226
227 virtual void init_new_phase(int phase,
228 BCP_column_generation& colgen,
229 CoinSearchTreeBase*& candidates);
230
231 void readIpopt();
232
233 private:
234
235 /// auxilliary method for handling output for AMPL
236 void write_AMPL_solution(const BCP_solution* sol,
237 bool write_file, bool write_screen);
238
239 };
240
241 //#############################################################################
242
243 struct BM_SB_result
244 {
245 /** 0: Not done 1: Only down 2: only up 3: both ways */
246 int branchEval;
247 int objInd;
248 int colInd;
249 int status[2];
250 int iter[2];
251 double objval[2];
252 double varChange[2];
253 double time[2];
254 };
255
256 //#############################################################################
257
258 #include <OsiAuxInfo.hpp>
259 #include <OsiCuts.hpp>
260 #include "CglGomory.hpp"
261 #include "CglProbing.hpp"
262 #include "CglKnapsackCover.hpp"
263 #include "CglMixedIntegerRounding.hpp"
264 #include "BonOaFeasChecker.hpp"
265 #include "BonOaNlpOptim.hpp"
266 #include "BonEcpCuts.hpp"
267 #include "BonOACutGenerator2.hpp"
268
269 #include "BCP_lp_user.hpp"
270 #include "BonAmplSetup.hpp"
271 #include "BonChooseVariable.hpp"
272
273 class BM_lp : public BCP_lp_user
274 {
275 /* There's no totalTime_ and nodeTime_. Look at the top of BM.cpp */
276 // double totalTime_;
277 // double nodeTime_;
278 int in_strong;
279
280 BCP_string ipopt_file_content;
281 BCP_string nl_file_content;
282 BCP_parameter_set<BM_par> par;
283 BCP_buffer bm_buf;
284
285 /** This contains the setup for running Bonmin in particular nlp
286 solver, continuous solver, cut generators,...*/
287 Bonmin::BonminAmplSetup bonmin_;
288
289 double integerTolerance_;
290
291 /** A counter for how many times in a row did the NLP code fail. When the
292 NLP fails we branch; hopefully it'll be OK in the children. If it
293 fails too many times in a row then we fathom the node: it's hopelessly
294 difficult. */
295 int numNlpFailed_;
296
297 OsiCuts cuts_;
298
299 /** These are the indices of the integral (i.e., things that can be branched
300 on) objects in the solver, sorted based on the priority of the
301 corresponding objects */
302 int* objInd_;
303 int objNum_;
304
305 /** Every time when branching decisions are to be made, we create 6 arrays,
306 3 for those objects that are infeasible and 3 for those that are
307 feasible. infInd_ contains the indices of the objects (into the objects_
308 array of objects) that are not feasible, infUseful_ cointains their
309 usefulness and infNum_ their number. They are ordered by their priority
310 and within that by their usefulness (that depends on their pseudocosts,
311 etc.). feasXXX_ contains the same for objects that are feasible, except
312 that SOS objects are not listed there, nor variables that are fixed. */
313 int* infInd_;
314 double* infUseful_;
315 int infNum_;
316 int* feasInd_;
317 double* feasUseful_;
318 int feasNum_;
319
320 /** This is where we keep the results in case of distributed strong
321 branching. The length of the array is objNum_ */
322 BM_SB_result* sbResult_;
323 /** A pointer to the entry that got selected */
324 BM_SB_result* bestSbResult_;
325
326 /** The time when we started to process the node */
327 double node_start_time;
328
329 /** Class for collecting statistics */
330 BM_stats bm_stats;
331
332 public:
333 BM_lp();
334 virtual ~BM_lp();
335
336 inline int& numNlpFailed() {
337 return (dynamic_cast<BM_node*>(get_user_data()))->numNlpFailed_;
338 }
339
340 virtual void
341 unpack_module_data(BCP_buffer& buf);
342
343 /** Process a message that has been sent by another process' user part to
344 this process' user part. */
345 virtual void
346 process_message(BCP_buffer& buf);
347
348 virtual OsiSolverInterface *
349 initialize_solver_interface();
350
351 virtual void
352 load_problem(OsiSolverInterface& osi, BCP_problem_core* core,
353 BCP_var_set& vars, BCP_cut_set& cuts);
354
355 virtual void
356 modify_lp_parameters(OsiSolverInterface* lp, bool in_strong_branching);
357
358 virtual BCP_solution*
359 test_feasibility(const BCP_lp_result& lp_result,
360 const BCP_vec<BCP_var*>& vars,
361 const BCP_vec<BCP_cut*>& cuts);
362 BCP_solution* test_feasibility_BB(const BCP_lp_result& lp_result,
363 const BCP_vec<BCP_var*>& vars);
364 BCP_solution* test_feasibility_hybrid(const BCP_lp_result& lp_result,
365 const BCP_vec<BCP_var*>& vars,
366 const BCP_vec<BCP_cut*>& cuts);
367
368 virtual void
369 generate_cuts_in_lp(const BCP_lp_result& lpres,
370 const BCP_vec<BCP_var*>& vars,
371 const BCP_vec<BCP_cut*>& cuts,
372 BCP_vec<BCP_cut*>& new_cuts,
373 BCP_vec<BCP_row*>& new_rows);
374 virtual void
375 cuts_to_rows(const BCP_vec<BCP_var*>& vars, // on what to expand
376 BCP_vec<BCP_cut*>& cuts, // what to expand
377 BCP_vec<BCP_row*>& rows, // the expanded rows
378 // things that the user can use for lifting cuts if allowed
379 const BCP_lp_result& lpres,
380 BCP_object_origin origin, bool allow_multiple);
381 virtual double
382 compute_lower_bound(const double old_lower_bound,
383 const BCP_lp_result& lpres,
384 const BCP_vec<BCP_var*>& vars,
385 const BCP_vec<BCP_cut*>& cuts);
386
387 virtual void
388 initialize_new_search_tree_node(const BCP_vec<BCP_var*>& vars,
389 const BCP_vec<BCP_cut*>& cuts,
390 const BCP_vec<BCP_obj_status>& vs,
391 const BCP_vec<BCP_obj_status>& cs,
392 BCP_vec<int>& var_changed_pos,
393 BCP_vec<double>& var_new_bd,
394 BCP_vec<int>& cut_changed_pos,
395 BCP_vec<double>& cut_new_bd);
396
397 virtual BCP_branching_decision
398 select_branching_candidates(const BCP_lp_result& lpres,
399 const BCP_vec<BCP_var*>& vars,
400 const BCP_vec<BCP_cut*>& cuts,
401 const BCP_lp_var_pool& local_var_pool,
402 const BCP_lp_cut_pool& local_cut_pool,
403 BCP_vec<BCP_lp_branching_object*>& cans,
404 bool force_branch = false);
405
406 BCP_branching_decision bbBranch(OsiBranchingInformation& brInfo,
407 BCP_vec<BCP_lp_branching_object*>& cands);
408 BCP_branching_decision hybridBranch();
409
410 /** Methods invoked from bbBranch() */
411 void send_pseudo_cost_update(OsiBranchingInformation& branchInfo);
412 void unpack_pseudo_costs(BCP_buffer& buf);
413 int sort_objects(OsiBranchingInformation& branchInfo,
414 Bonmin::BonChooseVariable* choose, int& branchNum);
415 void clear_SB_results();
416 void collect_branch_data(OsiBranchingInformation& branchInfo,
417 OsiSolverInterface* solver,
418 const int branchNum,
419 BM_BranchData* branchData);
420 void do_distributed_SB(OsiBranchingInformation& branchInfo,
421 OsiSolverInterface* solver,
422 const CoinWarmStart* cws,
423 const int branchNum,
424 const int* pids, const int pidNum);
425 bool isBranchFathomable(int status, double obj);
426 int process_SB_results(OsiBranchingInformation& branchInfo,
427 OsiSolverInterface* solver,
428 Bonmin::BonChooseVariable* choose,
429 OsiBranchingObject*& branchObject);
430 int try_to_branch(OsiBranchingInformation& branchInfo,
431 OsiSolverInterface* solver,
432 Bonmin::BonChooseVariable* choose,
433 OsiBranchingObject*& branchObject,
434 bool allowVarFix);
435
436 virtual void
437 set_user_data_for_children(BCP_presolved_lp_brobj* best,
438 const int selected);
439
440 };
441
442 //#############################################################################
443
444 #include "BCP_USER.hpp"
445
446 class BM_pack : public BCP_user_pack {
447 public:
448 virtual ~BM_pack() {}
449
450 virtual void pack_user_data(const BCP_user_data* ud, BCP_buffer& buf);
451 virtual BCP_user_data* unpack_user_data(BCP_buffer& buf);
452
453 virtual void pack_cut_algo(const BCP_cut_algo* cut, BCP_buffer& buf);
454 virtual BCP_cut_algo* unpack_cut_algo(BCP_buffer& buf);
455
456 };
457
458 //#############################################################################
459
460 class BM_init : public USER_initialize {
461
462 public:
463
464 virtual BCP_tm_user * tm_init(BCP_tm_prob& p,
465 const int argnum,
466 const char * const * arglist);
467
468 virtual BCP_lp_user * lp_init(BCP_lp_prob& p);
469
470 virtual BCP_user_pack * packer_init(BCP_user_class* p);
471 };
472
473 #endif