BCP_tm_user.cpp
Go to the documentation of this file.
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 #include "CoinTime.hpp"
4 
5 #include "BCP_vector.hpp"
6 #include "BCP_tm_user.hpp"
7 #include "BCP_tm.hpp"
8 #include "BCP_lp.hpp"
9 #include "BCP_solution.hpp"
10 #include "BCP_var.hpp"
11 #include "BCP_functions.hpp"
12 
13 //#############################################################################
14 // Informational methods for the user
15 double BCP_tm_user::upper_bound() const { return p->ub(); }
16 
18 {
19  return *(p->lower_bounds.begin()) / p->lb_multiplier;
20 }
21 
22 //#############################################################################
23 // Informational methods for the user
24 /* Methods to get/set BCP parameters on the fly */
25 char
27 { return p->par.entry(key); }
28 int
30 { return p->par.entry(key); }
31 double
33 { return p->par.entry(key); }
34 const BCP_string&
36 { return p->par.entry(key); }
37 
38 void BCP_tm_user::set_param(const BCP_tm_par::chr_params key, const bool val)
39 { p->par.set_entry(key, val); }
40 void BCP_tm_user::set_param(const BCP_tm_par::chr_params key, const char val)
41 { p->par.set_entry(key, val); }
42 void BCP_tm_user::set_param(const BCP_tm_par::int_params key, const int val)
43 { p->par.set_entry(key, val); }
44 void BCP_tm_user::set_param(const BCP_tm_par::dbl_params key, const double val)
45 { p->par.set_entry(key, val); }
46 void BCP_tm_user::set_param(const BCP_tm_par::str_params key, const char * val)
47 { p->par.set_entry(key, val); }
48 
49 //#############################################################################
50 
51 void
53 
54 //-----------------------------------------------------------------------------
55 // unpack an MIP feasible solution
58 {
60  printf(" TM: Default unpack_feasible_solution() executed.\n");
61  }
62 
64 
65  int varnum;
66  buf.unpack(varnum);
67 
68  double val;
69  int bcpind;
70  while (--varnum >= 0) {
71  buf.unpack(val);
72  // these vars are stored only in the solution, so noone cares if we
73  // flip negative bcpind's
74  buf.unpack(bcpind);
75  BCP_var* var = p->unpack_var_without_bcpind(buf);
76  var->set_bcpind(bcpind < 0 ? -bcpind : bcpind);
77  soln->add_entry(var, val);
78  }
79  buf.unpack(val);
80  soln->set_objective_value(val);
81 
82  return soln;
83 }
84 
85 //-----------------------------------------------------------------------------
86 
87 bool
89  const BCP_solution* new_sol)
90 {
91  return false;
92 }
93 
94 //#############################################################################
95 
97 int
99 {
100  return p->get_process_id();
101 }
102 
104 void
105 BCP_tm_user::send_message(const int target, const BCP_buffer& buf)
106 {
107  p->msg_env->send(target, BCP_Msg_User, buf);
108 }
109 
111 void
113  const BCP_buffer& buf)
114 {
115  switch (proc_type) {
116  case BCP_ProcessType_LP:
117  p->msg_env->multicast(p->lp_procs.size(), &p->lp_procs[0],
118  BCP_Msg_User, buf);
119  break;
120  case BCP_ProcessType_CP:
121  throw BCP_fatal_error("\
122 BCP_tm_user::broadcast_message: CP not yet implemented\n");
123  break;
124  case BCP_ProcessType_VP:
125  throw BCP_fatal_error("\
126 BCP_tm_user::broadcast_message: VP not yet implemented\n");
127  break;
128 #if ! defined(BCP_ONLY_LP_PROCESS_HANDLING_WORKS)
129  case BCP_ProcessType_CG:
130  p->msg_env->multicast(*p->slaves.cg, BCP_Msg_User, buf);
131  break;
132  case BCP_ProcessType_VG:
133  p->msg_env->multicast(*p->slaves.vg, BCP_Msg_User, buf);
134  break;
135 #endif
136  case BCP_ProcessType_Any:
137  p->msg_env->multicast(p->lp_procs.size(), &p->lp_procs[0],
138  BCP_Msg_User, buf);
139 #if ! defined(BCP_ONLY_LP_PROCESS_HANDLING_WORKS)
140  p->msg_env->multicast(*p->slaves.cg, BCP_Msg_User, buf);
141  p->msg_env->multicast(*p->slaves.vg, BCP_Msg_User, buf);
142 #endif
143  break;
144  case BCP_ProcessType_TM:
145  case BCP_ProcessType_TS:
147  throw BCP_fatal_error("\
148 BCP_tm_user::broadcast_message: broadcast to TM/TS/EndProcess...\n");
149  default:
150  throw BCP_fatal_error("BCP_tm_user::Unknown process type (%i)\n",
151  proc_type);
152  }
153 }
154 
157 void
159 {
160  throw BCP_fatal_error("\
161 BCP_tm_user::process_message() invoked but not overridden!\n");
162 }
163 
164 //#############################################################################
165 // setting the core
166 
167 void
170  BCP_lp_relax*& matrix)
171 {
173  printf(" TM: Default BCP_tm_user::initialize_core() executed.\n");
174  }
175 }
176 
177 //--------------------------------------------------------------------------
178 // create the root node
179 void
181  BCP_vec<BCP_cut*>& added_cuts,
182  BCP_user_data*& user_data)
183 {
185  printf(" TM: Default BCP_tm_user::create_root() executed.\n");
186  }
187 }
188 
189 //--------------------------------------------------------------------------
190 // display a feasible solution
191 void
193 {
195  printf("\
196  TM: Default BCP_tm_user::display_feasible_solution() executed.\n");
197  }
198 
199  const BCP_solution_generic* gsol =
200  dynamic_cast<const BCP_solution_generic*>(sol);
201  if (! gsol) {
202  throw BCP_fatal_error("\
203 BCP_tm_user::display_feasible_solution() invoked with non-generic sol.\n");
204  }
205 
206  gsol->display();
207 }
208 
209 //-----------------------------------------------------------------------------
212 void
214  const BCP_tm_node& node)
215 {
216 }
217 
218 //-----------------------------------------------------------------------------
222 void
224  const BCP_tm_node& node,
225  bool after_processing_node)
226 {
227 }
228 
229 //-----------------------------------------------------------------------------
231 void
233 {
235  printf("TM: Running time: %.3f\n", CoinWallclockTime() - p->start_time);
236  printf("TM: search tree size: %i ( processed %i ) max depth: %i\n",
237  int(p->search_tree.size()), int(p->search_tree.processed()),
238  p->search_tree.maxdepth());
239  lp_stat.display();
240 
241  if (! p->feas_sol) {
242  printf("TM: No feasible solution is found\n");
243  } else {
244  printf("TM: The best solution found has value %f\n",
248  }
249  }
250  }
251 }
252 
253 //--------------------------------------------------------------------------
254 // Initialize new phase
255 void
257  BCP_column_generation& colgen,
258  CoinSearchTreeBase*& candidates)
259 {
261  printf(" TM: Default init_new_phase() executed.\n");
262  }
265  case BCP_BestFirstSearch:
266  candidates = new CoinSearchTree<CoinSearchTreeCompareBest>;
267  break;
269  candidates = new CoinSearchTree<CoinSearchTreeCompareBreadth>;
270  break;
272  candidates = new CoinSearchTree<CoinSearchTreeCompareDepth>;
273  break;
275  candidates = new CoinSearchTree<CoinSearchTreeComparePreferred>;
276  break;
277  }
278 }
279 
280 //--------------------------------------------------------------------------
281 // Compare tree nodes
282 void
283 BCP_tm_user::change_candidate_heap(CoinSearchTreeManager& candidates,
284  const bool new_solution)
285 {
286  if (new_solution) {
287  candidates.newSolution(p->ub());
288  } else {
289  candidates.reevaluateSearchStrategy();
290  }
291 }
BCP_tree search_tree
Definition: BCP_tm.hpp:239
static double lb_multiplier
The lower bounds of the unexplored search tree nodes.
Definition: BCP_tm.hpp:198
virtual void display_feasible_solution(const BCP_solution *sol)
Display a feasible solution.
BCP_var * unpack_var_without_bcpind(BCP_buffer &buf)
Definition: BCP_tm.cpp:108
virtual void send(const int target, const BCP_message_tag tag)=0
Send an empty message (message tag only) to the process given by the frist argument.
int get_process_id() const
Definition: BCP_process.hpp:24
BCP_buffer & unpack(T &value)
Unpack a single object of type T.
Definition: BCP_buffer.hpp:186
double upper_bound() const
Return what is the best known upper bound (might be BCP_DBL_MAX)
Definition: BCP_tm_user.cpp:15
virtual double objective_value() const =0
The method returning the objective value of the solution.
virtual void pack_module_data(BCP_buffer &buf, BCP_process_t ptype)
Pack the initial information (info that the user wants to send over) for the process specified by the...
Definition: BCP_tm_user.cpp:52
Print out a message when the default version of an overridable method is executed.
virtual void change_candidate_heap(CoinSearchTreeManager &candidates, const bool new_solution)
double start_time
Definition: BCP_tm.hpp:203
dbl_params
Double parameters.
BCP_process_t
This enumerative constant describes the various process types.
double ub() const
Definition: BCP_tm.hpp:321
virtual void display_final_information(const BCP_lp_statistics &lp_stat)
Display information after BCP finished processing the search tree.
char entry(const chr_params key) const
void set_bcpind(const int bcpind)
Set the internal index of the variable.
Definition: BCP_var.hpp:176
char get_param(const BCP_tm_par::chr_params key) const
Definition: BCP_tm_user.cpp:26
size_t size() const
int processed() const
BCP_solution * feas_sol
Definition: BCP_tm.hpp:163
NO OLD DOC.
Definition: BCP_lp.hpp:56
NO OLD DOC.
This class is a very simple impelementation of a constant length string.
Definition: BCP_string.hpp:13
void send_message(const int target, const BCP_buffer &buf)
Send a message to a particular process.
virtual BCP_solution * unpack_feasible_solution(BCP_buffer &buf)
Unpack a MIP feasible solution that was packed by the BCP_lp_user::pack_feasible_solution() method...
Definition: BCP_tm_user.cpp:57
void set_param(const BCP_tm_par::chr_params key, const bool val)
Definition: BCP_tm_user.cpp:38
chr_params
Character parameters.
void set_objective_value(double v)
Set the objective value of the solution.
BCP_tm_prob * p
Definition: BCP_tm_user.hpp:60
Which search tree enumeration strategy should be used.
void set_entry(const chr_params key, const char val)
void fint fint fint * phase
int maxdepth() const
BCP_parameter_set< BCP_tm_par > par
Definition: BCP_tm.hpp:168
virtual void process_message(BCP_buffer &buf)
Process a message that has been sent by another process&#39; user part to this process&#39; user part...
virtual void init_new_phase(int phase, BCP_column_generation &colgen, CoinSearchTreeBase *&candidates)
Do whatever initialization is necessary before the phase-th phase.
void add_entry(BCP_var *var, double value)
Append a variable and the corresponding value to the end of the appropriate vectors.
Abstract base class that defines members common to all types of variables.
Definition: BCP_var.hpp:28
Currently there isn&#39;t any error handling in BCP.
Definition: BCP_error.hpp:20
void broadcast_message(const BCP_process_t proc_type, const BCP_buffer &buf)
Broadcast the message to all processes of the given type.
BCP_message_environment * msg_env
A class that holds the methods about how to pack things.
Definition: BCP_tm.hpp:156
int_params
Integer parameters.
void display() const
Print out the statistics.
Definition: BCP_lp.cpp:39
Invoke &quot;display_feasible_solution&quot; user routine for the best feasible solution after the entire tree ...
std::vector< int > lp_procs
members to measure how long it took to process the root node.
Definition: BCP_tm.hpp:186
virtual void create_root(BCP_vec< BCP_var * > &added_vars, BCP_vec< BCP_cut * > &added_cuts, BCP_user_data *&user_data)
Create the set of extra variables and cuts that should be added to the formulation in the root node...
Do fathom the node.
Definition: BCP_enum.hpp:67
This class describes the message buffer used for all processes of BCP.
Definition: BCP_buffer.hpp:39
virtual void initialize_core(BCP_vec< BCP_var_core * > &vars, BCP_vec< BCP_cut_core * > &cuts, BCP_lp_relax *&matrix)
Create the core of the problem by filling out the last three arguments.
std::multiset< double > lower_bounds
Definition: BCP_tm.hpp:199
BCP_tm_user * user
A class that holds the methods about how to pack things.
Definition: BCP_tm.hpp:151
Print statistics: running time, tree size, best solution value.
Used by the user to send a message to the user portion of the other process.
BCP_column_generation
This enumerative constant describes what to do when a search tree node becomes fathomable for the cur...
Definition: BCP_enum.hpp:65
virtual bool replace_solution(const BCP_solution *old_sol, const BCP_solution *new_sol)
Decide whether to replace old_sol with new_sol.
Definition: BCP_tm_user.cpp:88
virtual void display_node_information(BCP_tree &search_tree, const BCP_tm_node &node)
Display user information just before a new node is sent to the LP or diving into a node is acknowledg...
This class holds a MIP feasible primal solution.
int process_id() const
What is the process id of the current process.
Definition: BCP_tm_user.cpp:98
void display() const
Display the solution.
Definition: BCP_solution.cpp:8
char param(BCP_tm_par::chr_params key) const
Definition: BCP_tm.hpp:298
An object of type BCP_lp_relax holds the description of an lp relaxation.
Definition: BCP_matrix.hpp:267
str_params
String parameters.
double lower_bound() const
Return a global lower bound.
Definition: BCP_tm_user.cpp:17
virtual void multicast(int num, const int *targets, const BCP_message_tag tag)=0
Send an empty message (message tag only) to all the processes in the process array.
This is the abstract base class for a solution to a Mixed Integer Programming problem.