BCP_tm.cpp
Go to the documentation of this file.
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 
4 #include <map>
5 
6 #include "BCP_math.hpp"
7 #include "BCP_process.hpp"
8 #include "BCP_lp.hpp"
9 #include "BCP_tm.hpp"
10 #include "BCP_problem_core.hpp"
11 #include "BCP_tm_user.hpp"
12 #include "BCP_var.hpp"
13 #include "BCP_cut.hpp"
14 #include "BCP_solution.hpp"
15 
16 #include "BCP_warmstart.hpp"
17 
18 double BCP_tm_prob::lb_multiplier = 1e12;
19 
21  BCP_process(0, -1),
22  user(0),
23  msg_env(0),
24  lp_stat(0),
25  feas_sol(0),
26  upper_bound(BCP_DBL_MAX),
27  core(0),
28  core_as_change(0),
29  next_cut_index_set_start(1),
30  next_var_index_set_start(1),
31  candidate_list()
32 {}
33 
35 {
36  delete user;
37  delete packer;
38 
39  delete lp_stat;
40 
41  delete feas_sol;
42 
43  delete core;
44  delete core_as_change;
45 }
46 
47 //#############################################################################
48 
49 void
50 BCP_tm_stat::print(bool final, double t)
51 {
52  const int wait_stat_freq = 1200;
53  bool do_print = false;
54  if (final) {
55  printf("TM: final statistics:\n");
56  do_print = true;
57  } else {
58  if (floor(t/wait_stat_freq) > cnt) {
59  cnt = static_cast<int>(floor(t/wait_stat_freq));
60  printf("TM: statistics at %12.6f:\n", t);
61  do_print = true;
62  }
63  }
64  if (do_print) {
65  for (int i = 0; i < num_lp; ++i) {
66  if ((wait_time[i] > 5e-3) ||
67  (numQueueLength[i]>0 && sumQueueLength[i]/numQueueLength[i] > 5)) {
68  if (numQueueLength[i] > 0) {
69  printf("TM: With %5i LP working: wait: %12.6f queue: %.2f\n",
71  } else {
72  printf("TM: With %5i LP working: wait: %12.6f queue: %.2f\n",
73  i, wait_time[i], 0.0);
74  }
75  }
76  }
77  }
78 }
79 
80 //#############################################################################
81 
82 void
84 {
85  const int bcpind = var.bcpind();
86  const BCP_object_t obj_t = var.obj_type();
87  const BCP_obj_status stat = var.status();
88  const BCP_var_t var_t = var.var_type();
89  const double obj = var.obj();
90  const double lb = var.lb();
91  const double ub = var.ub();
92  msg_buf.pack(bcpind)
93  .pack(obj_t).pack(stat).pack(var_t).pack(obj).pack(lb).pack(ub);
94  switch (obj_t) {
95  case BCP_CoreObj:
96  break;
97  case BCP_AlgoObj:
98  packer->pack_var_algo(&dynamic_cast<const BCP_var_algo&>(var), msg_buf);
99  break;
100  default:
101  throw BCP_fatal_error("BCP_tm_prob::_pack_var(): unexpected obj_t.\n");
102  }
103 }
104 
105 //-----------------------------------------------------------------------------
106 
107 BCP_var*
109 {
110  BCP_var* var = 0;
111  BCP_object_t obj_t;
112  BCP_var_t var_t;
113  double obj, lb, ub;
115  buf.unpack(obj_t).unpack(stat)
116  .unpack(var_t).unpack(obj).unpack(lb).unpack(ub);
117  switch (obj_t) {
118  case BCP_CoreObj:
119  var = new BCP_var_core(var_t, obj, lb, ub);
120  break;
121  case BCP_AlgoObj:
122  var = packer->unpack_var_algo(buf);
123  var->set_var_type(var_t);
124  var->change_bounds(lb, ub);
125  var->set_obj(obj);
126  break;
127  default:
128  throw BCP_fatal_error("BCP_tm_prob::_unpack_var(): unexpected obj_t.\n");
129  }
130  var->set_status(stat);
131  return var;
132 }
133 
134 //-----------------------------------------------------------------------------
135 
136 int
138 {
139  int bcpind;
140  BCP_var* var = 0;
141  msg_buf.unpack(bcpind);
142  if (bcpind == 0) {
143  throw BCP_fatal_error("BCP_tm_prob::unpack_var(): 0 bcpind.\n");
144  }
145  if (bcpind > 0) {
146  if (vars_local.find(bcpind) != vars_local.end() ||
147  vars_remote.find(bcpind) != vars_remote.end() ) {
148  throw BCP_fatal_error("\
149 BCP_tm_prob::unpack_var(): received a var with positive bcpind, \n\
150  but the var already exists.\n");
151  }
152  } else {
154  if (vars_local.find(-bcpind) != vars_local.end() ||
155  vars_remote.find(-bcpind) != vars_remote.end() ) {
156  // It's OK, we got it from some other place. Nothing to do.
157  delete var;
158  } else {
159  var->set_bcpind(-bcpind);
160  vars_local[-bcpind] = var;
161  }
162  }
163  return bcpind;
164 }
165 
166 //#############################################################################
167 
168 void
170 {
171  const int bcpind = cut.bcpind();
172  const BCP_object_t obj_t = cut.obj_type();
173  const BCP_obj_status stat = cut.status();
174  const double lb = cut.lb();
175  const double ub = cut.ub();
176  msg_buf.pack(bcpind)
177  .pack(obj_t).pack(stat).pack(lb).pack(ub);
178  switch (obj_t) {
179  case BCP_CoreObj:
180  break;
181  case BCP_AlgoObj:
182  packer->pack_cut_algo(&dynamic_cast<const BCP_cut_algo&>(cut), msg_buf);
183  break;
184  default:
185  throw BCP_fatal_error("BCP_tm_prob::_pack_cut(): unexpected obj_t.\n");
186  }
187 }
188 
189 //-----------------------------------------------------------------------------
190 
191 BCP_cut*
193 {
194  BCP_cut* cut = 0;
195  BCP_object_t obj_t;
196  double lb, ub;
198  buf.unpack(obj_t).unpack(stat).unpack(lb).unpack(ub);
199  switch (obj_t) {
200  case BCP_CoreObj:
201  cut = new BCP_cut_core(lb, ub);
202  break;
203  case BCP_AlgoObj:
204  cut = packer->unpack_cut_algo(buf);
205  cut->change_bounds(lb, ub);
206  break;
207  default:
208  throw BCP_fatal_error("BCP_tm_prob::_unpack_cut(): unexpected obj_t.\n");
209  }
210  cut->set_status(stat);
211  return cut;
212 }
213 
214 //-----------------------------------------------------------------------------
215 
216 int
218 {
219  int bcpind;
220  BCP_cut* cut = 0;
221  msg_buf.unpack(bcpind);
222  if (bcpind == 0) {
223  throw BCP_fatal_error("BCP_tm_prob::unpack_cut(): 0 bcpind.\n");
224  }
225  if (bcpind > 0) {
226  if (cuts_local.find(bcpind) != cuts_local.end() ||
227  cuts_remote.find(bcpind) != cuts_remote.end() ) {
228  throw BCP_fatal_error("\
229 BCP_tm_prob::unpack_cut(): received a cut with positive bcpind, \n\
230  but the cut already exists.\n");
231  }
232  } else {
234  if (cuts_local.find(-bcpind) != cuts_local.end() ||
235  cuts_remote.find(-bcpind) != cuts_remote.end() ) {
236  // It's OK, we got it from some other place. Nothing to do.
237  delete cut;
238  } else {
239  cut->set_bcpind(-bcpind);
240  cuts_local[-bcpind] = cut;
241  }
242  }
243  return bcpind;
244 }
static double lb_multiplier
The lower bounds of the unexplored search tree nodes.
Definition: BCP_tm.hpp:198
BCP_buffer msg_buf
members to measure how long it took to process the root node.
Definition: BCP_tm.hpp:183
BCP_var * unpack_var_without_bcpind(BCP_buffer &buf)
Definition: BCP_tm.cpp:108
BCP_buffer & pack(const T &value)
Pack a single object of type T.
Definition: BCP_buffer.hpp:177
void print(bool final, double t)
Definition: BCP_tm.cpp:50
BCP_buffer & unpack(T &value)
Unpack a single object of type T.
Definition: BCP_buffer.hpp:186
void set_var_type(const BCP_var_t type)
Set the integrality type of the variable.
Definition: BCP_var.hpp:141
std::map< int, int > cuts_remote
Definition: BCP_tm.hpp:226
std::map< int, Coin::SmartPtr< BCP_cut > > cuts_local
Definition: BCP_tm.hpp:224
Abstract base class that defines members common to all types of cuts.
Definition: BCP_cut.hpp:29
virtual BCP_cut_algo * unpack_cut_algo(BCP_buffer &buf)
Unpack an algorithmic cut.
Definition: BCP_USER.hpp:109
void set_bcpind(const int bcpind)
Set the internal index of the cut.
Definition: BCP_cut.hpp:149
double ub() const
Definition: BCP_tm.hpp:321
virtual ~BCP_tm_prob()
Definition: BCP_tm.cpp:34
void set_bcpind(const int bcpind)
Set the internal index of the variable.
Definition: BCP_var.hpp:176
Core cuts are the cuts that always stay in the LP formulation.
Definition: BCP_cut.hpp:195
double ub() const
Return the upper bound on the cut.
Definition: BCP_cut.hpp:84
BCP_solution * feas_sol
Definition: BCP_tm.hpp:163
BCP_problem_core * core
Definition: BCP_tm.hpp:208
int num_lp
Definition: BCP_tm.hpp:87
Core variables are the variables that always stay in the LP formulation.
Definition: BCP_var.hpp:230
int * numQueueLength
Definition: BCP_tm.hpp:96
BCP_obj_status
This enumerative constant gives the status of an object (variable or cut).
Definition: BCP_enum.hpp:105
void change_bounds(const double lb, const double ub)
Change the lower and upper bounds to the given values.
Definition: BCP_var.hpp:170
#define BCP_DBL_MAX
Definition: BCP_math.hpp:6
double lb() const
Return the lower bound on the cut.
Definition: BCP_cut.hpp:82
void fint fint fint real fint real real real real real real real real real * e
std::map< int, Coin::SmartPtr< BCP_var > > vars_local
Definition: BCP_tm.hpp:220
double ub() const
Return the upper bound.
Definition: BCP_var.hpp:91
virtual void pack_cut_algo(const BCP_cut_algo *cut, BCP_buffer &buf)
Pack an algorithmic cut.
Definition: BCP_USER.hpp:102
BCP_lp_statistics * lp_stat
Definition: BCP_tm.hpp:160
BCP_obj_status status() const
Return the status of the cut.
Definition: BCP_cut.hpp:91
double obj() const
Return the objective coefficient.
Definition: BCP_var.hpp:87
void set_status(const BCP_obj_status stat)
Set the status of the cut.
Definition: BCP_cut.hpp:156
virtual BCP_object_t obj_type() const =0
Return the type of the variable.
BCP_tm_stat stat
Definition: BCP_tm.hpp:265
void pack_cut(const BCP_cut &cut)
Definition: BCP_tm.cpp:169
BCP_var_t var_type() const
Return the integrality type of the variable.
Definition: BCP_var.hpp:85
std::map< int, int > vars_remote
Definition: BCP_tm.hpp:222
int unpack_cut()
Definition: BCP_tm.cpp:217
int unpack_var()
Definition: BCP_tm.cpp:137
double * sumQueueLength
Definition: BCP_tm.hpp:93
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 pack_var(const BCP_var &var)
Definition: BCP_tm.cpp:83
BCP_user_pack * packer
A class that holds the methods about how to pack things.
Definition: BCP_tm.hpp:153
int bcpind() const
Return the internal index of the variable.
Definition: BCP_var.hpp:93
Algorithmic object.
Definition: BCP_enum.hpp:53
BCP_obj_status status() const
Return the status of the variable.
Definition: BCP_var.hpp:98
virtual BCP_var_algo * unpack_var_algo(BCP_buffer &buf)
Unpack an algorithmic variable.
Definition: BCP_USER.hpp:93
This class describes the message buffer used for all processes of BCP.
Definition: BCP_buffer.hpp:39
void change_bounds(const double lb, const double ub)
Change just the lower/upper bounds.
Definition: BCP_cut.hpp:142
void set_obj(const double obj)
Set the objective coefficient.
Definition: BCP_var.hpp:143
virtual void pack_var_algo(const BCP_var_algo *var, BCP_buffer &buf)
Pack an algorithmic variable.
Definition: BCP_USER.hpp:86
void fint fint fint fint fint fint fint fint fint fint real real real real real real real real real fint real fint real char real * user
double * wait_time
Definition: BCP_tm.hpp:90
double lb() const
Return the lower bound.
Definition: BCP_var.hpp:89
BCP_tm_user * user
A class that holds the methods about how to pack things.
Definition: BCP_tm.hpp:151
BCP_problem_core_change * core_as_change
Definition: BCP_tm.hpp:210
BCP_tm_prob()
Definition: BCP_tm.cpp:20
void set_status(const BCP_obj_status status)
Set the status of the variable.
Definition: BCP_var.hpp:183
int bcpind() const
Return the internal index of the cut.
Definition: BCP_cut.hpp:86
BCP_object_t
This enumerative constant describes the possible types of objects (variables and cuts).
Definition: BCP_enum.hpp:49
static char * t
Definition: OSdtoa.cpp:3645
virtual BCP_object_t obj_type() const =0
Return the type of the variable.
Base object.
Definition: BCP_enum.hpp:51
BCP_cut * unpack_cut_without_bcpind(BCP_buffer &buf)
Definition: BCP_tm.cpp:192
BCP_var_t
This enumerative constant describes the integrality type of a variable.
Definition: BCP_enum.hpp:161