BCP_lp_main.cpp
Go to the documentation of this file.
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 #include <cfloat>
4 #include <cerrno>
5 #ifdef _MSC_VER
6 #include <process.h>
7 #endif
8 
9 #include "CoinTime.hpp"
10 
11 #include "BcpConfig.h"
12 #include "BCP_os.hpp"
13 
14 #include "BCP_USER.hpp"
15 #include "BCP_buffer.hpp"
16 #include "BCP_problem_core.hpp"
17 #include "BCP_lp_node.hpp"
18 #include "BCP_lp.hpp"
19 #include "BCP_main_fun.hpp"
20 #include "BCP_lp_functions.hpp"
21 #include "BCP_lp_user.hpp"
22 
23 #include "BCP_message.hpp"
24 
25 //#############################################################################
26 
27 void
29 {
30  p.core->unpack(buf);
31 
32  // create the core_as_change from core
33  *p.core_as_change = *p.core;
34 
35  // create the core part of p.node
36  const BCP_vec<BCP_var_core*>& bvars = p.core->vars;
37  const int bvarnum = bvars.size();
38  p.node->vars.reserve(std::max<int>(1000, 3 * bvarnum));
39  if (bvarnum > 0) {
40  BCP_vec<BCP_var*>& vars = p.node->vars;
41  for (int i = 0; i < bvarnum; ++i) {
42  vars.unchecked_push_back(new BCP_var_core(*bvars[i]));
43  }
44  }
45 
46  const BCP_vec<BCP_cut_core*>& bcuts = p.core->cuts;
47  const int bcutnum = bcuts.size();
48  p.node->cuts.reserve(std::max<int>(1000, 3 * bcutnum));
49  if (bcutnum > 0) {
50  BCP_vec<BCP_cut*>& cuts = p.node->cuts;
51  for (int i = 0; i < bcutnum; ++i) {
52  cuts.unchecked_push_back(new BCP_cut_core(*bcuts[i]));
53  }
54  }
55 }
56 
57 //#############################################################################
58 
60  USER_initialize* user_init,
61  int my_id, int parent, double ub)
62 {
63  BCP_lp_prob p(my_id, parent);
64  p.upper_bound = ub;
65  p.msg_env = msg_env;
66 
67  // wait for the message with the parameters and unpack it
68  p.msg_buf.clear();
69  msg_env->receive(parent /*tree_manager*/,
71  p.par.unpack(p.msg_buf);
72  double wallclockInit;
73  p.msg_buf.unpack(wallclockInit);
75  CoinWallclockTime(wallclockInit);
76 
77  // Let us be nice
78  setpriority(PRIO_PROCESS, 0, p.par.entry(BCP_lp_par::NiceLevel));
79 
80  FILE* logfile = 0;
81 
83  if (! (p.par.entry(BCP_lp_par::LogFileName) == "")) {
84  int len = log.length();
85  char *logname = new char[len + 300];
86  memcpy(logname, log.c_str(), len);
87  memcpy(logname + len, "-lp-", 4);
88  len += 4;
89  gethostname(logname + len, 255);
90  len = strlen(logname);
91  logname[len++] = '-';
92  sprintf(logname + len, "%i", static_cast<int>(GETPID));
93  logfile = freopen(logname, "a", stdout);
94  if (logfile == 0) {
95  fprintf(stderr, "Error while redirecting stdout: %i\n", errno);
96  throw BCP_fatal_error("");
97  }
98  setvbuf(logfile, NULL, _IOLBF, 0); // make it line buffered
99  delete[] logname;
100  } else {
101  setvbuf(stdout, NULL, _IOLBF, 0); // make it line buffered
102  }
103 
104  // now create the user universe
105  p.user = user_init->lp_init(p);
106  p.user->setLpProblemPointer(&p);
107  p.packer = user_init->packer_init(p.user);
108  p.packer->user_class = p.user;
109 
110  // wait for the core description and process it
111  p.msg_buf.clear();
112  p.msg_env->receive(parent /*tree_manager*/,
115 
116  // wait for the user info
117  p.msg_buf.clear();
118  msg_env->receive(parent /*tree_manager*/,
121 
124 
125  // ok, we're all geared up to process search tree nodes
126  // wait for messages and process them...
127  BCP_message_tag msgtag;
129  while (true) {
130  p.msg_buf.clear();
131  msg_env->receive(BCP_AnyProcess,
132  BCP_Msg_AnyMessage, p.msg_buf, -1);
133  msgtag = p.msg_buf.msgtag();
134  if (msgtag == BCP_Msg_ProcessType) {
135  p.msg_buf.unpack(ptype);
136  break;
137  }
138  p.no_more_cuts_cnt = -1; // not waiting for cuts
139  p.process_message();
140  if (msgtag == BCP_Msg_FinishedBCP)
141  break;
142  }
143 
144  if (logfile)
145  fclose(logfile);
146 
147  return ptype;
148 }
BCP_message_tag
This enumerative constant describes the message tags different processes of BCP understand.
BCP_parameter_set< BCP_lp_par > par
Definition: BCP_lp.hpp:145
BCP has finished.
Used when receiving, message with any message tag will be received.
BCP_lp_node * node
Description he current search tree node.
Definition: BCP_lp.hpp:168
BCP_buffer & unpack(T &value)
Unpack a single object of type T.
Definition: BCP_buffer.hpp:186
virtual OsiSolverInterface * initialize_solver_interface()
Create LP solver environment.
virtual void receive(const int source, const BCP_message_tag tag, BCP_buffer &buf, const double timeout)=0
Blocking receive with timeout.
const int BCP_AnyProcess
Definition: BCP_message.hpp:21
BCP_var_set vars
This is an abstract base class that describes the message passing environment.
Definition: BCP_message.hpp:30
BCP_process_t
This enumerative constant describes the various process types.
char entry(const chr_params key) const
const char * c_str() const
Definition: BCP_string.hpp:19
virtual BCP_user_pack * packer_init(BCP_user_class *p)
Definition: BCP_USER.hpp:225
OsiSolverInterface * master_lp
A class that holds the methods about how to pack things.
Definition: BCP_lp.hpp:135
void BCP_lp_process_core(BCP_lp_prob &p, BCP_buffer &buf)
Definition: BCP_lp_main.cpp:28
The TM sends the description of the core formulation to the slave process.
virtual void process_message()
Core cuts are the cuts that always stay in the LP formulation.
Definition: BCP_cut.hpp:195
The TM sends the process type to the process (LP, Cut Generator, etc.)
virtual void initialize_int_and_sos_list(std::vector< OsiObject * > &intAndSosObjects)
Create the list of objects that can be used for branching (simple integer vars and SOS sets)...
NO OLD DOC.
Definition: BCP_lp.hpp:102
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.
#define setpriority(x, y, z)
Definition: BCP_os.hpp:13
This class is a very simple impelementation of a constant length string.
Definition: BCP_string.hpp:13
Core variables are the variables that always stay in the LP formulation.
Definition: BCP_var.hpp:230
double upper_bound
Definition: BCP_lp.hpp:222
BCP_vec< BCP_cut_core * > cuts
A vector of pointers to the cuts in the core of the problem.
BCP_cut_set cuts
int no_more_cuts_cnt
Definition: BCP_lp.hpp:226
BCP_process_t BCP_lp_main(BCP_message_environment *msg_env, USER_initialize *user_init, int my_id, int parent, double ub)
Definition: BCP_lp_main.cpp:59
BCP_lp_user * user
A class that holds the methods about how to pack things.
Definition: BCP_lp.hpp:131
This class is an abstract base class for the initializer class the user has to provide.
Definition: BCP_USER.hpp:160
#define GETPID
Definition: BCP_os.hpp:31
int length() const
Definition: BCP_string.hpp:16
BCP_user_class * user_class
A pointer ot the usr class of the process from which the methods of this class are invoked from...
Definition: BCP_USER.hpp:55
#define gethostname(x, y)
Definition: BCP_os.hpp:25
The TM sends the appropriate parameters to the slave process.
virtual void unpack_module_data(BCP_buffer &buf)
Unpack the initial information sent to the LP process by the Tree Manager.
void setLpProblemPointer(BCP_lp_prob *ptr)
Set the pointer.
Definition: BCP_lp_user.hpp:93
The TM sends the initial user packed information to the slave process.
void clear()
Completely clear the buffer.
Definition: BCP_buffer.hpp:168
BCP_message_tag msgtag() const
Return the message tag of the message in the buffer.
Definition: BCP_buffer.hpp:90
The filename all the output should go to.
Currently there isn&#39;t any error handling in BCP.
Definition: BCP_error.hpp:20
What should be the &quot;niceness&quot; of the LP process.
size_t size() const
Return the current number of entries.
Definition: BCP_vector.hpp:116
std::vector< OsiObject * > intAndSosObjects
Things that can be branched on.
Definition: BCP_lp.hpp:161
This class describes the message buffer used for all processes of BCP.
Definition: BCP_buffer.hpp:39
virtual BCP_lp_user * lp_init(BCP_lp_prob &p)
Definition: BCP_USER.hpp:200
void unchecked_push_back(const_reference x)
Append x to the end of the vector.
BCP_problem_core * core
Definition: BCP_lp.hpp:153
double start_time
Definition: BCP_lp.hpp:211
void unpack(BCP_buffer &buf)
Unpack the parameter set from the buffer.
BCP_message_environment * msg_env
A class that holds the methods about how to pack things.
Definition: BCP_lp.hpp:139
BCP_problem_core_change * core_as_change
Definition: BCP_lp.hpp:155
BCP_buffer msg_buf
Definition: BCP_lp.hpp:239
BCP_vec< BCP_var_core * > vars
A vector of pointers to the variables in the core of the problem.
CouExpr & log(CouExpr &e)
BCP_user_pack * packer
A class that holds the methods about how to pack things.
Definition: BCP_lp.hpp:133