OS_tm.cpp
Go to the documentation of this file.
1 /* $Id: OS_tm.cpp 2904 2009-09-06 06:47:42Z kmartin $ */
2 // Last edit: 1/6/07
3 //
4 // Name: OS_tm.cpp
5 // Author: Francois Margot
6 // Tepper School of Business
7 // Carnegie Mellon University, Pittsburgh, PA 15213
8 // email: fmargot@andrew.cmu.edu
9 // Date: 12/28/03
10 //-----------------------------------------------------------------------------
11 // Copyright (C) 2003, Francois Margot, International Business Machines
12 // Corporation and others. All Rights Reserved.
13 
14 #include<iomanip>
15 
16 //OS Stuff
17 #include "OSConfig.h"
18 #include "OSResult.h"
19 #include "OSiLReader.h"
20 #include "OSiLWriter.h"
21 #include "OSrLReader.h"
22 #include "OSrLWriter.h"
23 #include "OSFileUtil.h"
24 #include "OSErrorClass.h"
25 #include "OSErrorClass.h"
26 //end OS Stuff
27 
28 #include <CoinHelperFunctions.hpp>
29 #include <CoinFileIO.hpp>
30 #include <OsiClpSolverInterface.hpp>
31 #include <cassert>
32 
33 #include "OS_init.hpp"
34 #include "BCP_tm.hpp"
35 #include "OS_tm.hpp"
36 #include "OS_user_data.hpp"
37 #include "OS.hpp"
38 
39 #include "BCP_math.hpp"
40 
41 using namespace std;
42 
43 /*************************************************************************/
44 
45 int main(int argc, char* argv[]) {
46  WindowsErrorPopupBlocker();
47  OS_init os_init;
48  int retCode = -1;
49  std::cout << "Call bcp_main " << std::endl;
50  retCode = bcp_main(argc, argv, &os_init);
51  std::cout << "Return from call to bcp_main " << retCode << std::endl;
52  //delete os_init.tm;
53  std::cout << "OSBranchCutPrice terminates normally" << std::endl;
54  return retCode;
55 }
56 
57 /*************************************************************************/
58 
59 
60 
61 //kipp where is the constructon
62 
63 
64 void OS_tm::readInput(const char* filename){
65 
66 
67 
68  // Create OSInstance and feed into class OS_prob
69 
70  // get the file
71  FileUtil *fileUtil = NULL;
72  fileUtil = new FileUtil();
73  const char dirsep = CoinFindDirSeparator();
74  // Set directory containing mps data files.
75  std::string dataDir;
76  dataDir = dirsep == '/' ? "../data/" : "..\\data\\";
77  cout << "Start Building the Model" << endl;
78 
79  try{
80  // get the parincLinear problem
81  std::string osilFileName;
82  //osilFileName = dataDir + "osilFiles" + dirsep + "parincLinear.osil";
83  osilFileName = dataDir + "osilFiles" + dirsep + "p0033.osil";
84  std::cout << "Try to read a sample file" << std::endl;
85  std::cout << "The file is: " << osilFileName << std::endl <<std::endl ;
86  std::string osil = fileUtil->getFileAsString( osilFileName.c_str() );
87  //if(filename == NULL) throw ErrorClass("put file name on command line");
88  //osil = fileUtil->getFileAsString( osilFileName);
89  os_prob.osilreader = new OSiLReader();
90  os_prob.osinstance = os_prob.osilreader->readOSiL( osil);
91  delete fileUtil;
92  }// end the try
93 
94 
95  catch(const ErrorClass& eclass){
96  delete fileUtil;
97  std::cout << eclass.errormsg << std::endl;
98  return ;
99  }
100 
101 
102 
103 }
104 
105 /*************************************************************************/
107  // possible process types looked up in BCP_enum_process_t.hpp
108 
109  switch (ptype) {
110  case BCP_ProcessType_LP:
111  {
112  // Pack a pointer; does not work for parallel machines
113  // this make make the os_prob object available to the os_lp object
114  buf.pack(&os_prob);
115  }
116  break;
117  default:
118  abort();
119  }
120 }
121 
122 
123 
126  BCP_lp_relax*& matrix){
127 
128 // Transmit the core constraints and variables to BCP
129  std::cout << "INITIALIZE THE CORE " << std::endl;
130  int i;
131  int num_vars = os_prob.osinstance->getVariableNumber();
132  double objsign = 1;
133 
134  objsign = ( os_prob.osinstance->getObjectiveMaxOrMins()[0] == "min") ? 1 : -1;
135 
136  for (i = 0; i < num_vars; ++i) {
137  os_prob.osinstance->getDenseObjectiveCoefficients()[0][i] = objsign*(os_prob.osinstance->getDenseObjectiveCoefficients()[0][i]);
138  if ( os_prob.osinstance->getVariableTypes()[i] == 'B' || os_prob.osinstance->getVariableTypes()[i] == 'I') {
139  if ( os_prob.osinstance->getVariableTypes()[i] == 'B' ) {
140  vars.push_back(new BCP_var_core(BCP_BinaryVar, os_prob.osinstance->getDenseObjectiveCoefficients()[0][i], 0, 1));
141  }
142  else {
143  vars.push_back(new BCP_var_core(BCP_IntegerVar, os_prob.osinstance->getDenseObjectiveCoefficients()[0][i],
144  os_prob.osinstance->getVariableLowerBounds()[ i],
145  os_prob.osinstance->getVariableUpperBounds()[ i]) );
146  }
147  }
148  else {
149  vars.push_back(new BCP_var_core(BCP_ContinuousVar, os_prob.osinstance->getDenseObjectiveCoefficients()[0][i],
150  os_prob.osinstance->getVariableLowerBounds()[ i],
151  os_prob.osinstance->getVariableUpperBounds()[ i] ));
152  }
153 
154  }
155 
156  const int num_cons = os_prob.osinstance->getConstraintNumber();
157 
158  // core constraints
159 
160  for (i=0; i < num_cons; ++i) {
161  cuts.push_back(new BCP_cut_core( os_prob.osinstance->getConstraintLowerBounds()[ i],
162  os_prob.osinstance->getConstraintUpperBounds()[ i]));
163  }
164 
165  matrix = new BCP_lp_relax;
166  bool columnMajor = os_prob.osinstance->getLinearConstraintCoefficientMajor();
167  int maxGap = 0;
168  CoinPackedMatrix* core_matrix = new CoinPackedMatrix(
169  columnMajor, //Column or Row Major
170  columnMajor? os_prob.osinstance->getConstraintNumber() : os_prob.osinstance->getVariableNumber(), //Minor Dimension
171  columnMajor? os_prob.osinstance->getVariableNumber() : os_prob.osinstance->getConstraintNumber(), //Major Dimension
172  os_prob.osinstance->getLinearConstraintCoefficientNumber(), //Number of nonzeroes
173  columnMajor? os_prob.osinstance->getLinearConstraintCoefficientsInColumnMajor()->values :
174  os_prob.osinstance->getLinearConstraintCoefficientsInRowMajor()->values, //Pointer to matrix nonzeroes
175  columnMajor? os_prob.osinstance->getLinearConstraintCoefficientsInColumnMajor()->indexes :
176  os_prob.osinstance->getLinearConstraintCoefficientsInRowMajor()->indexes, //Pointer to start of minor dimension indexes -- change to allow for row storage
177  columnMajor? os_prob.osinstance->getLinearConstraintCoefficientsInColumnMajor()->starts :
178  os_prob.osinstance->getLinearConstraintCoefficientsInRowMajor()->starts, //Pointers to start of columns.
179  0, 0, maxGap );
180 
181  //os_prob.core is a coin packed matrix
182  matrix->copyOf(*core_matrix, os_prob.osinstance->getDenseObjectiveCoefficients()[0], os_prob.osinstance->getVariableLowerBounds(),
183  os_prob.osinstance->getVariableUpperBounds(), os_prob.osinstance->getConstraintLowerBounds(),
184  os_prob.osinstance->getConstraintUpperBounds());
185 
186  //do the garbage collection
187  delete core_matrix;
188 
189 }//end initialize_core
190 
191 /**************************************************************************/
193  BCP_vec<BCP_cut*>& added_cuts, BCP_user_data*& user_data) {
194 
195 #ifdef USER_DATA
196  user_data = new MY_user_data(os_prob.colnum);
197 #endif
198 
199 }
200 
201 /*************************************************************************/
203 
204  //kipp: -- later write the OSiL file from here
205  std::cout << "Default BCP display of the feasible solution: ***************" << std::endl << std::endl;
207 
208 }
209 
210 
211 /*************************************************************************/
212 
214 
215 }
216 
217 
218 
219 /*************************************************************************/
220 
222  std::cout << "Inside OS_tm destructor" << std::endl;
223  delete os_prob.osilreader;
224 }
Binary (0-1) variable.
Definition: BCP_enum.hpp:163
virtual ~OS_tm()
Default destructor.
Definition: OS_tm.cpp:221
virtual void display_feasible_solution(const BCP_solution *sol)
Display a feasible solution.
BCP_buffer & pack(const T &value)
Pack a single object of type T.
Definition: BCP_buffer.hpp:177
std::string errormsg
errormsg is the error that is causing the exception to be thrown
Definition: OSErrorClass.h:42
int main(int argc, char *argv[])
Definition: BB_tm.cpp:32
BCP_process_t
This enumerative constant describes the various process types.
Core cuts are the cuts that always stay in the LP formulation.
Definition: BCP_cut.hpp:195
void readInput(const char *filename)
Read input and set up data in class OS_prob.
Definition: OS_tm.cpp:64
Core variables are the variables that always stay in the LP formulation.
Definition: BCP_var.hpp:230
void push_back(const_reference x)
Append x to the end of the vector.
return
Definition: OSdtoa.cpp:1794
void copyOf(const CoinPackedMatrix &m, const double *OBJ, const double *CLB, const double *CUB, const double *RLB, const double *RUB)
Set up the LP relaxation by making a copy of the arguments.
Definition: BCP_matrix.cpp:69
int bcp_main(int argc, char *argv[], USER_initialize *user_init)
This is the function the user must invoke when (s)he is ready to turn contrl over to BCP...
Definition: BCP_tm_main.cpp:37
Used to read an OSiL string.
Definition: OSiLReader.h:37
virtual void pack_module_data(BCP_buffer &buf, BCP_process_t ptype)
Pack data into a buffer; will not work in parallel environment as it uses pointer.
Definition: OS_tm.cpp:106
virtual void create_root(BCP_vec< BCP_var * > &added_vars, BCP_vec< BCP_cut * > &added_cuts, BCP_user_data *&user_data)
Create the root node of the enumeration.
Definition: OS_tm.cpp:192
Class taking care of interaction between user data and Bcp.
OS_tm()
Default constructor.
Definition: OS_tm.cpp:213
Continuous variable.
Definition: BCP_enum.hpp:167
virtual void display_feasible_solution(const BCP_solution *sol)
Print a feasible solution.
Definition: OS_tm.cpp:202
std::string getFileAsString(const char *fname)
read a file and return contents as a string.
Definition: OSFileUtil.cpp:35
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)
Packing of algorithmic cuts.
Definition: OS_tm.cpp:124
class used to make it easy to read and write files.
Definition: OSFileUtil.h:37
used for throwing exceptions.
Definition: OSErrorClass.h:31
General integer variable.
Definition: BCP_enum.hpp:165
An object of type BCP_lp_relax holds the description of an lp relaxation.
Definition: BCP_matrix.hpp:267
This is the abstract base class for a solution to a Mixed Integer Programming problem.