BCP_problem_core.cpp
Go to the documentation of this file.
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 // #include <functional>
4 #include "BCP_error.hpp"
5 #include "BCP_buffer.hpp"
6 #include "BCP_problem_core.hpp"
7 #include "BCP_var.hpp"
8 #include "BCP_cut.hpp"
9 #include "BCP_matrix.hpp"
10 #include "BCP_branch.hpp"
11 
12 //#############################################################################
13 
14 inline void BCP_problem_core::clear() {
15  delete matrix; matrix = 0;
18 }
19 
20 //-----------------------------------------------------------------------------
21 
23  vars(), cuts(), matrix(new BCP_lp_relax(true /*colordered*/)) {}
24 
26 {
27  clear();
28 }
29 
30 //-----------------------------------------------------------------------------
31 
33 {
34  buf.pack(vars.size());
35  if (vars.size() > 0) {
38  while (++vi != lastvi) {
39  const BCP_var_core& var = **vi;
40  const int bcpind = var.bcpind();
41  const BCP_object_t obj_t = var.obj_type();
42  const BCP_obj_status stat = var.status();
43  const BCP_var_t var_t = var.var_type();
44  const double obj = var.obj();
45  const double lb = var.lb();
46  const double ub = var.ub();
47  buf.pack(bcpind)
48  .pack(obj_t).pack(stat).pack(var_t).pack(obj).pack(lb).pack(ub);
49  }
50  }
51 
52  buf.pack(cuts.size());
53  if (cuts.size() > 0) {
56  while (++ci != lastci) {
57  BCP_cut_core& cut = **ci;
58  const int bcpind = cut.bcpind();
59  const BCP_object_t obj_t = cut.obj_type();
60  const BCP_obj_status stat = cut.status();
61  const double lb = cut.lb();
62  const double ub = cut.ub();
63  buf.pack(bcpind).pack(obj_t).pack(stat).pack(lb).pack(ub);
64  }
65  }
66 
67  matrix->pack(buf);
68 }
69 
70 //-----------------------------------------------------------------------------
71 
73 {
74  clear();
75  size_t size;
76  int bcpind;
77  BCP_object_t obj_t;
78  BCP_obj_status stat;
79  BCP_var_t var_t;
80  double obj;
81  double lb;
82  double ub;
83 
84  buf.unpack(size);
85  if (size > 0) {
86  BCP_var_core* bvar;
87  for (vars.reserve(size); size; --size) {
88  buf.unpack(bcpind)
89  .unpack(obj_t).unpack(stat)
90  .unpack(var_t).unpack(obj).unpack(lb).unpack(ub);
91  bvar = new BCP_var_core(var_t, obj, lb, ub);
92  bvar->set_bcpind(bcpind);
93  bvar->set_status(stat);
95  }
96  }
97 
98  buf.unpack(size);
99  if (size > 0) {
100  BCP_cut_core* bcut;
101  for (cuts.reserve(size); size; --size) {
102  buf.unpack(bcpind).unpack(obj_t).unpack(stat).unpack(lb).unpack(ub);
103  bcut = new BCP_cut_core(lb, ub);
104  bcut->set_bcpind(bcpind);
105  bcut->set_status(stat);
107  }
108  }
109 
110  matrix = new BCP_lp_relax(true /*colordered*/);
111  matrix->unpack(buf);
112 }
113 
114 //#############################################################################
115 
117  var_pos.clear();
118  var_ch.clear();
119  cut_pos.clear();
120  cut_ch.clear();
121 }
122 
123 #if 0
126  _storage = x._storage;
127  var_pos = x.var_pos;
128  var_ch = x.var_ch;
129  cut_pos = x.cut_pos;
130  cut_ch = x.cut_ch;
131  return *this;
132 }
133 #endif
134 
135 //-----------------------------------------------------------------------------
136 
139  clear();
141 
142  var_ch.reserve(core.varnum());
144  const BCP_vec<BCP_var_core*>::const_iterator lastvi = core.vars.end();
145  for ( ; vi != lastvi; ++vi)
146  var_ch.unchecked_push_back(BCP_obj_change((*vi)->lb(), (*vi)->ub(),
148  cut_ch.reserve(core.cutnum());
150  const BCP_vec<BCP_cut_core*>::const_iterator lastci = core.cuts.end();
151  for ( ; ci != lastci; ++ci)
152  cut_ch.unchecked_push_back(BCP_obj_change((*ci)->lb(), (*ci)->ub(),
154  return *this;
155 }
156 
157 //-----------------------------------------------------------------------------
158 
160  BCP_var_set& vars,
161  int bcutnum,
162  BCP_cut_set& cuts) :
163  _storage(BCP_Storage_Explicit), var_pos(), var_ch(), cut_pos(), cut_ch()
164 {
166  var_ch.reserve(bvarnum);
167 #if 0
169  const BCP_var_set::const_iterator lastvari = vars.entry(bvarnum);
170  for (vari = vars.begin(); vari != lastvari; ++vari)
171  var_ch.unchecked_push_back(BCP_obj_change((*vari)->lb(), (*vari)->ub(),
172  (*vari)->status()));
173 #else
174  for (int i = 0; i < bvarnum; ++i) {
175  const BCP_var* v = vars[i];
177  }
178 #endif
179  cut_ch.reserve(bcutnum);
181  const BCP_cut_set::const_iterator lastcuti = cuts.entry(bcutnum);
182  for (cuti = cuts.begin(); cuti != lastcuti; ++cuti)
183  cut_ch.unchecked_push_back(BCP_obj_change((*cuti)->lb(), (*cuti)->ub(),
184  (*cuti)->status()));
185 }
186 
187 //-----------------------------------------------------------------------------
188 
192  BCP_problem_core_change& ncore) :
193  _storage(storage), var_pos(), var_ch(), cut_pos(), cut_ch()
194 {
195  if (storage != BCP_Storage_WrtParent && storage != BCP_Storage_WrtCore) {
196  throw BCP_fatal_error("\
197 BCP_problem_core_change() : bad proposed storage\n");
198  }
199 
200  if (ocore.storage() != BCP_Storage_Explicit)
201  throw BCP_fatal_error("BCP_problem_core_change() : bad ocore storage\n");
202  if (ncore.storage() != BCP_Storage_Explicit)
203  throw BCP_fatal_error("BCP_problem_core_change() : bad ncore storage\n");
204 
205  int i;
206 
207  const int bvarnum = ncore.varnum();
208  var_pos.reserve(bvarnum);
209  var_ch.reserve(bvarnum);
210  for (i = 0; i < bvarnum; ++i) {
211  if (ocore.var_ch[i] != ncore.var_ch[i]) {
214  }
215  }
216 
217  const int bcutnum = ncore.cutnum();
218  cut_pos.reserve(bcutnum);
219  cut_ch.reserve(bcutnum);
220  for (i = 0; i < bcutnum; ++i) {
221  if (ocore.cut_ch[i] != ncore.cut_ch[i]) {
224  }
225  }
226 }
227 
228 //-----------------------------------------------------------------------------
229 
230 void
232 {
234  return;
236  throw BCP_fatal_error("\
237 BCP_problem_core_change::ensure_explicit() : bad current storage\n");
238  if (ecore.storage() != BCP_Storage_Explicit)
239  throw BCP_fatal_error("\
240 BCP_problem_core_change::ensure_explicit() : bad ecore storage\n");
241 
242  BCP_problem_core_change new_core;
243  new_core.update(ecore, *this);
244  swap(new_core);
245 }
246 
247 //-----------------------------------------------------------------------------
248 
249 void
252 {
254  throw BCP_fatal_error("\
255 BCP_problem_core_change::make_wrtcore_if_shorter() : bad storage\n");
256  if (ecore.storage() != BCP_Storage_Explicit)
257  throw BCP_fatal_error("\
258 BCP_problem_core_change::make_wrtcore_if_shorter() : bad ecore storage\n");
259  if (varnum() != ecore.varnum())
260  throw BCP_fatal_error("\
261 BCP_problem_core_change::make_wrtcore_if_shorter() : nonequal sizes\n");
262 
263  int i;
264 
265  // save the positions of the changed vars / cuts
266  BCP_vec<int> chvar;
267  chvar.reserve(ecore.varnum());
268 
269  const int bvarnum = var_ch.size();
270  for (i = 0; i < bvarnum; ++i)
271  if (var_ch[i] != ecore.var_ch[i])
272  chvar.unchecked_push_back(i);
273 
274  BCP_vec<int> chcut;
275  chcut.reserve(ecore.cutnum());
276 
277  const int bcutnum = cut_ch.size();
278  for (i = 0; i < bcutnum; ++i)
279  if (cut_ch[i] != ecore.cut_ch[i])
280  chcut.unchecked_push_back(i);
281 
282  if ((chvar.size() + chcut.size()) * sizeof(int) <
283  (ecore.varnum() - chvar.size() + ecore.cutnum() - chcut.size()) *
285  // wrt core is shorter
287  var_pos.swap(chvar);
289  cut_pos.swap(chcut);
291  }
292 }
293 
294 //-----------------------------------------------------------------------------
295 
297 {
298  std::swap(_storage, other._storage);
299 
300  var_pos.swap(other.var_pos);
301  var_ch.swap(other.var_ch);
302  cut_pos.swap(other.cut_pos);
303  cut_ch.swap(other.cut_ch);
304 }
305 
306 //-----------------------------------------------------------------------------
307 
309  const BCP_problem_core_change& ch_core)
310 {
311  switch (ch_core._storage){
313  operator=(ch_core); // _storage becomes Explicit
314  break;
315 
316  case BCP_Storage_NoData:
318  break;
319 
320  case BCP_Storage_WrtCore:
321  if (expl_core.storage() != BCP_Storage_Explicit)
322  throw BCP_fatal_error("BCP_problem_core_change::update():\n\
323  ch_core's WrtCore, but expl_core's not Explicit\n");
324  operator=(expl_core); // _storage becomes Explicit
325  // deliberate fall-through
326 
329  throw BCP_fatal_error("BCP_problem_core_change::update():\n\
330  _storage isn't Explicit && ch_core's WrtParent\n");
331  var_ch.update(ch_core.var_pos, ch_core.var_ch);
332  cut_ch.update(ch_core.cut_pos, ch_core.cut_ch);
333  break;
334 
335  default:
336  throw BCP_fatal_error("\
337 BCP_problem_core_change::update(): bad ch_core storage!\n");
338  break;
339  }
340 }
341 
342 //-----------------------------------------------------------------------------
343 
345  int siz = sizeof(BCP_storage_t);
346  siz += sizeof(int) + sizeof(int) * var_pos.size();
347  siz += sizeof(int) + BCP_obj_change::pack_size() * var_ch.size();
348  siz += sizeof(int) + sizeof(int) * cut_pos.size();
349  siz += sizeof(int) + BCP_obj_change::pack_size() * cut_ch.size();
350  return siz;
351 }
352 
353 //-----------------------------------------------------------------------------
354 
356  buf.pack(_storage);
359 }
360 
361 //-----------------------------------------------------------------------------
362 
364  buf.unpack(_storage);
367 }
This class describes changes in the core of the problem.
This class describes the core of the MIP problem, the variables/cuts in it as well as the matrix corr...
BCP_buffer & pack(const T &value)
Pack a single object of type T.
Definition: BCP_buffer.hpp:177
BCP_buffer & unpack(T &value)
Unpack a single object of type T.
Definition: BCP_buffer.hpp:186
The data stored is with respect to the original description of the base problem (as was given by the ...
Definition: BCP_enum.hpp:94
void clear()
Delete every entry.
Abstract base class that defines members common to all types of cuts.
Definition: BCP_cut.hpp:29
void pack(BCP_buffer &buf) const
Pack the core change into the buffer.
void set_bcpind(const int bcpind)
Set the internal index of the cut.
Definition: BCP_cut.hpp:149
BCP_storage_t storage() const
Return the storage type.
BCP_lp_relax * matrix
A pointer to the constraint matrix corresponding to the core variables and cuts.
size_t varnum() const
Return the number of changed variables (the length of the array var_ch).
void set_bcpind(const int bcpind)
Set the internal index of the variable.
Definition: BCP_var.hpp:176
BCP_storage_t _storage
Describes how the change is stored.
Core cuts are the cuts that always stay in the LP formulation.
Definition: BCP_cut.hpp:195
No data is stored.
Definition: BCP_enum.hpp:86
iterator begin()
Return an iterator to the beginning of the object.
Definition: BCP_vector.hpp:99
double ub() const
Return the upper bound on the cut.
Definition: BCP_cut.hpp:84
This class is just a collection of pointers to cuts with a number of methods to manipulate these cuts...
Definition: BCP_cut.hpp:279
void unpack(BCP_buffer &buf)
Unpack the contents of the core description from the buffer.
void reserve(const size_t n)
Reallocate the object to make space for n entries.
The object is not removable from the LP formulation, even if the object becomes inactive.
Definition: BCP_enum.hpp:115
BCP_vec< int > cut_pos
The positions of the core cuts (in the cuts member of [BCP_problem_core]{BCP_problem_core.html}) whose bounds and/or stati have changed.
static int pack_size()
size_t cutnum() const
Return the number of changed cuts (the length of the array cut_ch).
Core variables are the variables that always stay in the LP formulation.
Definition: BCP_var.hpp:230
BCP_vec< BCP_cut_core * > cuts
A vector of pointers to the cuts in the core of the problem.
void keep_by_index(const BCP_vec< int > &positions)
Keep the entries indexed by indices.
BCP_obj_status
This enumerative constant gives the status of an object (variable or cut).
Definition: BCP_enum.hpp:105
The data stored is an explicit listing of values.
Definition: BCP_enum.hpp:88
size_t varnum() const
Return the number of variables in the core.
BCP_vec< int > var_pos
The positions of the core variables (in the vars member of [BCP_problem_core]{BCP_problem_core.html}) whose bounds and/or stati have changed.
BCP_vec< BCP_obj_change > var_ch
The new lb/ub/status triplet for each variable for which any of those three have changed.
double lb() const
Return the lower bound on the cut.
Definition: BCP_cut.hpp:82
void unpack(BCP_buffer &buf)
Unpack the LP relaxation from the buffer.
double ub() const
Return the upper bound.
Definition: BCP_var.hpp:91
void make_wrtcore_if_shorter(const BCP_problem_core_change &orig_core)
Replace the current explicitly stored core change with one stored with respect to the explicitly stor...
void clear()
Clear all vector data members.
int pack_size() const
Return the buffer size needed to pack the data in the core change.
void pack(BCP_buffer &buf) const
Pack the contents of the core description into the buffer.
size_t cutnum() const
Return the number of cuts in the core.
BCP_obj_status status() const
Return the status of the cut.
Definition: BCP_cut.hpp:91
void update(const BCP_vec< int > &positions, const BCP_vec< T > &values)
Update those entries listed in positions to the given values.
The data stored is with respect to the same kind of data in the parent of the search tree node...
Definition: BCP_enum.hpp:91
double obj() const
Return the objective coefficient.
Definition: BCP_var.hpp:87
BCP_object_t obj_type() const
Return BCP_CoreObj indicating that the object is a core variable.
Definition: BCP_var.hpp:261
void set_status(const BCP_obj_status stat)
Set the status of the cut.
Definition: BCP_cut.hpp:156
static int
Definition: OSdtoa.cpp:2173
BCP_var_t var_type() const
Return the integrality type of the variable.
Definition: BCP_var.hpp:85
void unpack(BCP_buffer &buf)
Unpack the core change data from the buffer.
BCP_problem_core()
The default constructor creates an empty core description: no variables/cuts and an empty matrix...
void pack(BCP_buffer &buf) const
Pack the LP relaxation into the buffer.
BCP_object_t obj_type() const
Return BCP_CoreObj indicating that the object is a core cut.
Definition: BCP_cut.hpp:226
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
int bcpind() const
Return the internal index of the variable.
Definition: BCP_var.hpp:93
size_t size() const
Return the current number of entries.
Definition: BCP_vector.hpp:116
BCP_problem_core_change & operator=(const BCP_problem_core &core)
Set the core change description to be an explicit description (in the form of a change) of the given ...
iterator end()
Return an iterator to the end of the object.
Definition: BCP_vector.hpp:104
~BCP_problem_core()
The desctructor deletes all data members.
BCP_obj_status status() const
Return the status of the variable.
Definition: BCP_var.hpp:98
This class is just a collection of pointers to variables with a number of methods to manipulate these...
Definition: BCP_var.hpp:316
The class BCP_vec serves the same purpose as the vector class in the standard template library...
Definition: BCP_vector.hpp:24
This class describes the message buffer used for all processes of BCP.
Definition: BCP_buffer.hpp:39
void unchecked_push_back(const_reference x)
Append x to the end of the vector.
void swap(BCP_problem_core_change &other)
Swap the contents of the current core change with that of other.
double lb() const
Return the lower bound.
Definition: BCP_var.hpp:89
void purge_ptr_vector(BCP_vec< T * > &pvec, typename BCP_vec< T * >::iterator first, typename BCP_vec< T * >::iterator last)
This function purges the entries [first,last) from the vector of pointers pvec.
Definition: BCP_vector.hpp:266
void update(const BCP_problem_core_change &expl_core, const BCP_problem_core_change &core_change)
Update the current change according to core_change.
void ensure_explicit(const BCP_problem_core_change &expl_core)
If the current storage is not already explicit then replace it with an explicit description of the co...
BCP_storage_t
This enumerative constant describes how to store certain data for a search tree node.
Definition: BCP_enum.hpp:84
BCP_problem_core_change(const BCP_problem_core_change &)
The copy constructor is disabled by declaring it private and not defining it.
iterator entry(const int i)
Return an iterator to the i-th entry.
Definition: BCP_vector.hpp:109
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
void swap(BCP_vec< T > &x)
Exchange the contents of the object with that of x.
BCP_vec< BCP_var_core * > vars
A vector of pointers to the variables in the core of the problem.
An object of type BCP_lp_relax holds the description of an lp relaxation.
Definition: BCP_matrix.hpp:267
void clear()
Delete all data members.
BCP_var_t
This enumerative constant describes the integrality type of a variable.
Definition: BCP_enum.hpp:161
void fint fint fint real fint real * x
BCP_vec< BCP_obj_change > cut_ch
The new lb/ub/status triplet for each cut for which any of those three have changed.