source: trunk/Bonmin/experimental/Bcp/BM.hpp @ 530

Last change on this file since 530 was 530, checked in by ladanyi, 13 years ago

prepare Bcp for changing some LPs into TM storage places. Serial version works, currently debugging parallel version.

  • Property svn:eol-style set to native
  • Property svn:keywords set to "Author Date Id Revision"
File size: 10.3 KB
Line 
1// (C) Copyright International Business Machines Corporation and Carnegie Mellon University 2006, 2007
2// All Rights Reserved.
3// This code is published under the Common 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 "BonCbcParam.hpp"
14
15#include "BCP_USER.hpp"
16#include "BCP_parameters.hpp"
17#include "BCP_tm_user.hpp"
18#include "BCP_lp_user.hpp"
19
20#include "BB_cut.hpp"
21
22//#############################################################################
23
24class BM_node : public BCP_user_data {
25public:
26    /** A counter for how many times in a row did the NLP code fail. When the
27        NLP fails we branch; hopefully it'll be OK in the children. If it
28        fails too many times in a row then we fathom the node: it's hopelessly
29        difficult. */
30    int numNlpFailed_;
31public:
32    BM_node() : numNlpFailed_(0) {}
33    BM_node(BCP_buffer& buf) : numNlpFailed_(0) {
34        buf.unpack(numNlpFailed_);
35    }
36    ~BM_node() {}
37
38    virtual BM_node* clone() const {
39        return new BM_node(*this);
40    }
41
42    inline void pack(BCP_buffer& buf) const {
43        buf.pack(numNlpFailed_);
44    }
45};
46
47//#############################################################################
48   
49enum BM_WarmStartStrategy {
50    WarmStartNone,
51    WarmStartFromRoot,
52    WarmStartFromParent
53};
54
55// This needs to be the same as enum VarSelectStra_Enum in
56// BonOsiTMINLPInterface.hpp
57enum BM_BranchingStrategy {
58    BM_MostFractional=0,
59    BM_StrongBranching,
60    BM_ReliabilityBranching,
61    BM_CurvatureEstimator,
62    BM_QpStrongBranching,
63    BM_LpStrongBranching,
64    BM_NlpStrongBranching,
65    BM_OsiChooseVariable,
66    BM_OsiChooseStrong
67};
68
69enum BM_message {
70    StrongBranchRequest,
71    StrongBranchResult,
72    FreeToBusyRatio
73};
74
75//#############################################################################
76   
77class BM_par {
78public:
79    enum chr_params {
80        //
81        CombinedDistanceAndPriority,
82        PrintBranchingInfo,
83        SosWithLowPriorityMoreImportant,
84        VarWithLowPriorityMoreImportant,
85        end_of_chr_params
86    };
87    enum int_params {
88        //
89        BranchingStrategy,
90        FullStrongBranch,
91        NumNlpFailureMax,
92        WarmStartStrategy,
93        end_of_int_params
94    };
95    enum dbl_params {
96        //
97        end_of_dbl_params
98    };
99    enum str_params {
100        NL_filename,
101        IpoptParamfile,
102        //
103        end_of_str_params
104    };
105    enum str_array_params {
106        //
107        end_of_str_array_params
108    };
109};
110
111//#############################################################################
112
113class BM_tm : public BCP_tm_user {
114
115public:
116
117    /**@name Private data member */
118    BCP_string ipopt_file_content;
119    BCP_string nl_file_content;
120    BCP_parameter_set<BM_par> par;
121    Bonmin::BonminCbcParam minlpParams_;
122
123public:
124
125    /**@name Constructors and destructors */
126    //@{
127    /// Default constructor
128    BM_tm() {}
129
130    /// Default destructor
131    virtual ~BM_tm() {}
132    //@}
133
134    /**@name Packing and unpacking methods */
135    //@{
136    virtual void pack_module_data(BCP_buffer& buf, BCP_process_t ptype);
137
138    virtual BCP_solution* unpack_feasible_solution(BCP_buffer& buf);
139    //@}
140
141    /// Pass the core constraints and core variables to bcp
142    virtual void initialize_core(BCP_vec<BCP_var_core*>& vars,
143                                 BCP_vec<BCP_cut_core*>& cuts,
144                                 BCP_lp_relax*& matrix);
145
146    /** Create the set of extra variables and cuts that should be added to the
147        formulation in the root node. Also decide how variable pricing shuld be
148        done, that is, if column generation is requested in the
149        init_new_phase() method of this class then column
150        generation should be performed according to \c pricing_status.
151    */
152    virtual void
153    create_root(BCP_vec<BCP_var*>& added_vars,
154                BCP_vec<BCP_cut*>& added_cuts,
155                BCP_user_data*& user_data);
156
157    /// Print a feasible solution
158    virtual void display_feasible_solution(const BCP_solution* sol);
159
160    /** Process a message that has been sent by another process' user part to
161        this process' user part. */
162    virtual void
163    process_message(BCP_buffer& buf);
164
165    /// Output the final solution
166    virtual void display_final_information(const BCP_lp_statistics& lp_stat);
167
168    virtual void init_new_phase(int phase,
169                                BCP_column_generation& colgen,
170                                CoinSearchTreeBase*& candidates);
171
172    void readIpopt();
173
174  private:
175
176  /// auxilliary method for handling output for AMPL
177  void write_AMPL_solution(const BCP_solution* sol,
178                           bool write_file, bool write_screen);
179
180};
181
182//#############################################################################
183
184#include <OsiAuxInfo.hpp>
185#include <OsiCuts.hpp>
186#include "CglGomory.hpp"
187#include "CglProbing.hpp"
188#include "CglKnapsackCover.hpp"
189#include "CglMixedIntegerRounding.hpp"
190#include "BonOaFeasChecker.hpp"
191#include "BonOaNlpOptim.hpp"
192#include "BonEcpCuts.hpp"
193#include "BonOACutGenerator2.hpp"
194
195#include "BCP_lp_user.hpp"
196#include "BonAmplSetup.hpp"
197
198class BM_lp : public BCP_lp_user
199{
200    BCP_string ipopt_file_content;
201    BCP_string nl_file_content;
202    BCP_parameter_set<BM_par> par;
203
204    OsiBabSolver babSolver_;
205    /** PIERRE: This contains the setup for running Bonmin in particular nlp solver, continuous solver,
206      cut generators,...*/
207    Bonmin::BonminAmplSetup bonmin_;
208
209    CoinWarmStart* ws_;
210    OsiChooseVariable* chooseVar_;
211    int numEcpRounds_;
212    int numberStrong_;
213    int minReliability_;
214    int varselect_;
215    double integerTolerance_;
216    double cutOffDecrement_;
217
218    /* FIXME: gross cheating. works only for serial mode. Store the warmstart
219       informations in the lp process, do not send them over in user data or
220       anywhere. MUST be fixed. The map is indexed by the node index. */
221    std::map<int, CoinWarmStart*> warmStart;
222
223    double lower_bound_;
224    double* primal_solution_;
225
226    /** A counter for how many times in a row did the NLP code fail. When the
227        NLP fails we branch; hopefully it'll be OK in the children. If it
228        fails too many times in a row then we fathom the node: it's hopelessly
229        difficult. */
230    int numNlpFailed_;
231
232    OsiCuts cuts_;
233
234    /** The last free-to-busy ratio (among the LP processes) that the TM has
235        sent over. It is used to decide whether we want distributed strong
236        branching */
237    double freeToBusyRatio_;
238
239public:
240    BM_lp();
241    virtual ~BM_lp();
242
243    inline int& numNlpFailed() {
244        return (dynamic_cast<BM_node*>(get_user_data()))->numNlpFailed_;
245    }
246
247    virtual void
248    unpack_module_data(BCP_buffer& buf);
249
250    virtual void
251    pack_feasible_solution(BCP_buffer& buf, const BCP_solution* sol);
252
253    /** Process a message that has been sent by another process' user part to
254        this process' user part. */
255    virtual void
256    process_message(BCP_buffer& buf);
257
258    virtual OsiSolverInterface *
259    initialize_solver_interface();
260
261    virtual BCP_solution*
262    test_feasibility(const BCP_lp_result& lp_result,
263                     const BCP_vec<BCP_var*>& vars,
264                     const BCP_vec<BCP_cut*>& cuts);
265    BCP_solution* test_feasibility_BB(const BCP_vec<BCP_var*>& vars);
266    BCP_solution* test_feasibility_hybrid(const BCP_lp_result& lp_result,
267                                          const BCP_vec<BCP_var*>& vars,
268                                          const BCP_vec<BCP_cut*>& cuts);
269
270    virtual void
271    generate_cuts_in_lp(const BCP_lp_result& lpres,
272                        const BCP_vec<BCP_var*>& vars,
273                        const BCP_vec<BCP_cut*>& cuts,
274                        BCP_vec<BCP_cut*>& new_cuts,
275                        BCP_vec<BCP_row*>& new_rows);
276    virtual void
277    cuts_to_rows(const BCP_vec<BCP_var*>& vars, // on what to expand
278                 BCP_vec<BCP_cut*>& cuts,       // what to expand
279                 BCP_vec<BCP_row*>& rows,       // the expanded rows
280                 // things that the user can use for lifting cuts if allowed
281                 const BCP_lp_result& lpres,
282                 BCP_object_origin origin, bool allow_multiple);
283    virtual double
284    compute_lower_bound(const double old_lower_bound,
285                        const BCP_lp_result& lpres,
286                        const BCP_vec<BCP_var*>& vars,
287                        const BCP_vec<BCP_cut*>& cuts);
288
289    virtual void 
290    initialize_new_search_tree_node(const BCP_vec<BCP_var*>& vars,
291                                    const BCP_vec<BCP_cut*>& cuts,
292                                    const BCP_vec<BCP_obj_status>& vs,
293                                    const BCP_vec<BCP_obj_status>& cs,
294                                    BCP_vec<int>& var_changed_pos,
295                                    BCP_vec<double>& var_new_bd,
296                                    BCP_vec<int>& cut_changed_pos,
297                                    BCP_vec<double>& cut_new_bd);
298
299    virtual BCP_branching_decision
300    select_branching_candidates(const BCP_lp_result& lpres,
301                                const BCP_vec<BCP_var*>& vars,
302                                const BCP_vec<BCP_cut*>& cuts,
303                                const BCP_lp_var_pool& local_var_pool,
304                                const BCP_lp_cut_pool& local_cut_pool,
305                                BCP_vec<BCP_lp_branching_object*>& cans);
306
307    BCP_branching_decision bbBranch(OsiBranchingInformation& brInfo,
308                                    BCP_vec<BCP_lp_branching_object*>& cands);
309    BCP_branching_decision hybridBranch();
310
311    virtual void
312    set_user_data_for_children(BCP_presolved_lp_brobj* best, 
313                               const int selected);
314
315    virtual void
316    modify_lp_parameters(OsiSolverInterface* lp, bool in_strong_branching);
317
318private:
319    /* There's no totalTime_ and nodeTime_. Look at the top of BM.cpp */
320    //   double totalTime_;
321    //   double nodeTime_;
322    int in_strong;
323
324};
325
326//#############################################################################
327
328#include "BCP_USER.hpp"
329
330class BM_pack : public BCP_user_pack {
331public:
332    virtual ~BM_pack() {}
333
334    virtual void pack_user_data(const BCP_user_data* ud, BCP_buffer& buf);
335    virtual BCP_user_data* unpack_user_data(BCP_buffer& buf);
336
337    virtual void pack_cut_algo(const BCP_cut_algo* cut, BCP_buffer& buf);
338    virtual BCP_cut_algo* unpack_cut_algo(BCP_buffer& buf);
339
340};
341
342
343//#############################################################################
344
345class BM_init : public USER_initialize {
346
347public:
348
349    virtual BCP_tm_user * tm_init(BCP_tm_prob& p,
350                                  const int argnum,
351                                  const char * const * arglist);
352
353    virtual BCP_lp_user * lp_init(BCP_lp_prob& p);
354
355    virtual BCP_user_pack * packer_init(BCP_user_class* p);
356};
357
358//#############################################################################
359
360class BM_solution : public BCP_solution { 
361public:
362    double _objective;
363    BCP_vec<int> _ind;
364    BCP_vec<double> _values;
365
366public:
367    BM_solution() : _objective(0), _ind(), _values() {}
368    virtual ~BM_solution() {}
369
370    inline virtual double objective_value() const { return _objective; }
371    inline void setObjective(double obj) { _objective = obj; }
372
373    void add_entry(int i, double value) {
374        _ind.push_back(i);
375        _values.push_back(value);
376    }
377};
378
379#endif
Note: See TracBrowser for help on using the repository browser.