/home/coin/SVN-release/OS-2.1.1/OS/examples/branchCutPriceTSP/TM/OS_tm.cpp

Go to the documentation of this file.
00001 // Last edit: 1/6/07
00002 //
00003 // Name:     OS_tm.cpp
00004 // Author:   Francois Margot
00005 //           Tepper School of Business
00006 //           Carnegie Mellon University, Pittsburgh, PA 15213
00007 //           email: fmargot@andrew.cmu.edu
00008 // Date:     12/28/03
00009 //-----------------------------------------------------------------------------
00010 // Copyright (C) 2003, Francois Margot, International Business Machines
00011 // Corporation and others.  All Rights Reserved.
00012 
00013 #include<iomanip>
00014 
00015 //OS Stuff
00016 #include "OSConfig.h"
00017 #include "OSResult.h" 
00018 #include "OSiLReader.h"        
00019 #include "OSiLWriter.h"   
00020 #include "OSrLReader.h"          
00021 #include "OSrLWriter.h"      
00022 #include "OSFileUtil.h"      
00023 #include "OSErrorClass.h"
00024 #include "OSErrorClass.h"
00025 //end OS Stuff
00026 
00027 #include <CoinHelperFunctions.hpp>
00028 #include <CoinFileIO.hpp>
00029 #include <OsiClpSolverInterface.hpp>
00030 #include <cassert>
00031 
00032 #include "OS_init.hpp"
00033 #include "BCP_tm.hpp"
00034 #include "OS_tm.hpp"
00035 #include "OS_user_data.hpp"
00036 #include "OS.hpp"
00037 
00038 #include "BCP_math.hpp"
00039 
00040 using namespace std;
00041 
00042 
00043 
00044 /*************************************************************************/
00045 
00046 int main(int argc, char* argv[]) {
00047         WindowsErrorPopupBlocker();
00048         OS_init os_init;
00049         int retCode = -1;
00050         std::cout << "Call bcp_main  "  << std::endl;
00051         retCode =  bcp_main(argc, argv, &os_init);
00052         std::cout << "Return from call to bcp_main  "<< retCode << std::endl;
00053         //delete os_init.tm;
00054         return retCode;
00055 }
00056 
00057 /*************************************************************************/
00058 
00059 
00060 
00061 //kipp where is the constructon
00062 
00063 
00064 void OS_tm::readInput(const char* filename){
00065 
00066 
00067 
00068    // Create OSInstance and feed into class OS_prob
00069         
00070         // get the file
00071         FileUtil *fileUtil = NULL; 
00072         fileUtil = new FileUtil();
00073         const char dirsep =  CoinFindDirSeparator();
00074         // Set directory containing mps data files.
00075         std::string dataDir;
00076         dataDir = dirsep == '/' ? "../../data/" : "..\\..\\data\\";
00077         cout << "Start Building the Model" << endl;     
00078         
00079         try{
00080                 // get the parincLinear problem
00081                 os_prob.locs = 48;
00082                 os_prob.routes = 1;
00083             os_prob.hubloc = 3; //do not change this variable 
00084                                                 
00085                 std::string osilFileName;
00086                 osilFileName =  dataDir  + "osilFiles" + dirsep +  "test48locR1.osil";
00087                 //osilFileName =  dataDir  + "osilFiles" + dirsep +  "p0033.osil";
00088                 std::cout << "Try to read a sample file" << std::endl;
00089                 std::cout << "The file is: " << osilFileName << std::endl <<std::endl ;
00090                 std::string osil = fileUtil->getFileAsString( osilFileName.c_str() );
00091                 //if(filename == NULL) throw ErrorClass("put file name on command line");
00092                 //osil = fileUtil->getFileAsString( osilFileName);
00093                 os_prob.osilreader = new OSiLReader();
00094                 os_prob.osinstance = os_prob.osilreader->readOSiL( osil);
00095                 os_prob.initialcons = os_prob.osinstance->getConstraintNumber();
00096                 delete fileUtil;
00097         }// end the try
00098 
00099         
00100         catch(const ErrorClass& eclass){
00101                 delete fileUtil;
00102                 std::cout << eclass.errormsg <<  std::endl;
00103                 return ;
00104         } 
00105         
00106         
00107         
00108 }
00109 
00110 /*************************************************************************/
00111 void OS_tm::pack_module_data(BCP_buffer& buf, BCP_process_t ptype) {
00112   // possible process types looked up in BCP_enum_process_t.hpp
00113   
00114   switch (ptype) {
00115   case BCP_ProcessType_LP:
00116     {
00117       // Pack a pointer; does not work for parallel machines
00118       // this make make the os_prob object available to the os_lp object
00119       buf.pack(&os_prob); 
00120     }
00121     break;
00122   default:
00123     abort();
00124   }
00125 }
00126 
00127 
00128 
00129 void OS_tm::initialize_core(BCP_vec<BCP_var_core*>& vars,
00130                        BCP_vec<BCP_cut_core*>& cuts,
00131                        BCP_lp_relax*& matrix){
00132   
00133 // Transmit the core constraints and variables to BCP
00134         std::cout << "INITIALIZE THE CORE " << std::endl;
00135         int i;
00136         int num_vars = os_prob.osinstance->getVariableNumber();         
00137         double objsign = 1;
00138         
00139         objsign = ( os_prob.osinstance->getObjectiveMaxOrMins()[0] == "min") ? 1 : -1;
00140 
00141         for (i = 0; i < num_vars; ++i) {
00142                 os_prob.osinstance->getDenseObjectiveCoefficients()[0][i] = objsign*(os_prob.osinstance->getDenseObjectiveCoefficients()[0][i]);
00143                 if ( os_prob.osinstance->getVariableTypes()[i] == 'B'    ||   os_prob.osinstance->getVariableTypes()[i] == 'I') {
00144                         if ( os_prob.osinstance->getVariableTypes()[i] == 'B' ) {
00145                                 vars.push_back(new BCP_var_core(BCP_BinaryVar, os_prob.osinstance->getDenseObjectiveCoefficients()[0][i], 0, 1));
00146                         }
00147                         else {
00148                                 vars.push_back(new BCP_var_core(BCP_IntegerVar, os_prob.osinstance->getDenseObjectiveCoefficients()[0][i], 
00149                                                 os_prob.osinstance->getVariableLowerBounds()[ i], 
00150                                                 os_prob.osinstance->getVariableUpperBounds()[ i]) );
00151                         }
00152                 } 
00153                 else {
00154                         vars.push_back(new BCP_var_core(BCP_ContinuousVar, os_prob.osinstance->getDenseObjectiveCoefficients()[0][i], 
00155                                         os_prob.osinstance->getVariableLowerBounds()[ i], 
00156                                         os_prob.osinstance->getVariableUpperBounds()[ i] ));
00157                 }
00158                 
00159         }
00160         
00161         const int num_cons = os_prob.osinstance->getConstraintNumber();
00162         
00163     // core constraints
00164    
00165         for (i=0; i < num_cons; ++i) {
00166                 cuts.push_back(new BCP_cut_core( os_prob.osinstance->getConstraintLowerBounds()[ i], 
00167                                 os_prob.osinstance->getConstraintUpperBounds()[ i]));
00168         }
00169   
00170         matrix = new BCP_lp_relax;
00171         bool columnMajor = os_prob.osinstance->getLinearConstraintCoefficientMajor();
00172         int maxGap = 0;
00173         CoinPackedMatrix*  core_matrix = new CoinPackedMatrix(
00174                 columnMajor, //Column or Row Major
00175                 columnMajor? os_prob.osinstance->getConstraintNumber() : os_prob.osinstance->getVariableNumber(), //Minor Dimension
00176                 columnMajor? os_prob.osinstance->getVariableNumber() : os_prob.osinstance->getConstraintNumber(), //Major Dimension
00177                 os_prob.osinstance->getLinearConstraintCoefficientNumber(), //Number of nonzeroes
00178                 columnMajor? os_prob.osinstance->getLinearConstraintCoefficientsInColumnMajor()->values : 
00179                         os_prob.osinstance->getLinearConstraintCoefficientsInRowMajor()->values, //Pointer to matrix nonzeroes
00180                 columnMajor? os_prob.osinstance->getLinearConstraintCoefficientsInColumnMajor()->indexes : 
00181                         os_prob.osinstance->getLinearConstraintCoefficientsInRowMajor()->indexes, //Pointer to start of minor dimension indexes -- change to allow for row storage
00182                 columnMajor? os_prob.osinstance->getLinearConstraintCoefficientsInColumnMajor()->starts : 
00183                         os_prob.osinstance->getLinearConstraintCoefficientsInRowMajor()->starts, //Pointers to start of columns.
00184                         0,   0, maxGap );
00185         
00186         //os_prob.core is a coin packed matrix
00187         matrix->copyOf(*core_matrix, os_prob.osinstance->getDenseObjectiveCoefficients()[0], os_prob.osinstance->getVariableLowerBounds(), 
00188                         os_prob.osinstance->getVariableUpperBounds(), os_prob.osinstance->getConstraintLowerBounds(), 
00189                         os_prob.osinstance->getConstraintUpperBounds());
00190         
00191         //do the garbage collection
00192         delete core_matrix;
00193 
00194 
00195 }//end initialize_core
00196 
00197 /**************************************************************************/
00198 void  OS_tm::create_root(BCP_vec<BCP_var*>& added_vars,
00199                 BCP_vec<BCP_cut*>& added_cuts, BCP_user_data*& user_data) {
00200   
00201 #ifdef USER_DATA
00202   user_data = new MY_user_data(os_prob.colnum);
00203 #endif
00204 
00205 }
00206 
00207 /*************************************************************************/
00208 void  OS_tm::display_feasible_solution(const BCP_solution *soln) {
00209         
00210         //kipp:  -- later write the OSiL file from here
00211         std::cout << "Default BCP display of the feasible solution: ***************" << std::endl << std::endl;
00212         BCP_tm_user::display_feasible_solution(soln);
00213         int ii,jj,varidx,zz,yy;
00214         double objval;
00215         int rt;
00216         const BCP_solution_generic* gensoln = dynamic_cast<const BCP_solution_generic*>(soln);
00217 
00218         FileUtil *fileUtil = NULL; 
00219         fileUtil = new FileUtil();
00220         std::string finaltxt;
00221         //ostringstream os_prob.os_prob.addtxtstr;
00222         int locs;
00223         int routes;
00224         locs = os_prob.locs;
00225         routes = os_prob.routes;
00226 
00227         double objcf,objvalue;
00228 
00229 
00230 
00231 for ( rt =0; rt<routes;rt++)
00232           {os_prob.addtxtstr << "  FINAL assignments of " << rt+1 << " route" << "\n";
00233             for ( zz = 0; zz < gensoln->_vars.size(); zz ++)
00234                     { varidx = gensoln->_vars[zz]->bcpind();
00235                   for ( ii = 0; ii<locs;ii++)
00236                                 {
00237                                   for (jj=0; jj< locs; jj++)
00238                                         {
00239                                                 if ( ii!=jj)
00240                                                         {
00241                                                           if( ii<jj)
00242                                                                 {     if( (routes*locs + (locs-1)*routes*ii + rt + routes* (jj-1)) == varidx )
00243                                                                                         os_prob.addtxtstr << ii+1 << " " << jj+1 << "   " << gensoln->_values[ii] << "  " << gensoln->_values[jj] << "\n"; ;
00244                                                                 }
00245                                                           else
00246                                                             {   if( (routes*locs + (locs-1)*routes*ii + rt + routes*jj) == varidx )
00247                                                                                         os_prob.addtxtstr << ii+1 << " " << jj+1 << "   " << gensoln->_values[ii] << "  " << gensoln->_values[jj] << "\n"; ;
00248                                                                 }
00249                                                      }
00250                                         }
00251                         }
00252                         }
00253            }
00254 
00255         os_prob.addtxtstr << "\n";
00256         objval = gensoln->objective_value();
00257         os_prob.addtxtstr << "actual gensoln->_vars[zz]->bcpind() " << objval << "\n";
00258         for ( zz = 0; zz < gensoln->_vars.size(); zz ++)
00259                 {   varidx = gensoln->_vars[zz]->bcpind();
00260                         os_prob.addtxtstr << " " << varidx ;}
00261         os_prob.addtxtstr << "\n";
00262         os_prob.addtxtstr << "actual gensoln->values[zz] " << objval << "\n";
00263         for ( zz = 0; zz < gensoln->_vars.size(); zz ++)
00264                 {   os_prob.addtxtstr << " " << gensoln->_values[zz] ;}
00265 
00266 
00267         os_prob.addtxtstr << "obj.function value = " << objval << "\n";
00268         
00269         os_prob.addtxtstr << "OBJ COEFFCIENTS" << "\n";
00270         for ( zz = 0; zz < gensoln->_vars.size(); zz ++)        
00271                 os_prob.addtxtstr << "  " << os_prob.osinstance->getDenseObjectiveCoefficients()[0][gensoln->_vars[zz]->bcpind()];
00272                 
00273         objvalue =0;
00274         for ( zz = 0; zz < gensoln->_vars.size(); zz ++)        
00275                 {       objcf = gensoln->_values[zz]*os_prob.osinstance->getDenseObjectiveCoefficients()[0][gensoln->_vars[zz]->bcpind()];
00276                         objvalue = objvalue + objcf;}
00277 
00278         os_prob.addtxtstr <<" \n";
00279         os_prob.addtxtstr << "CALCULATED OBJECTIVE FUNCTION VALUE = " << objvalue << "\n";
00280         os_prob.addtxtstr << " TOTAL CUTS " << os_prob.ttlcuts << "\n";
00281 
00282         /*for ( ii=0; ii < os_prob.conscount; ii++)
00283                         os_prob.addtxtstr<< "ConsA"<<ii<<",";
00284         os_prob.addtxtstr <<" \n";
00285         for ( ii=0; ii < os_prob.conscount; ii++)
00286                         os_prob.addtxtstr<< "ConsA"<<ii<<"\n";*/
00287 
00288         long int corecons = os_prob.osinstance->getConstraintNumber(); 
00289 
00290         for ( ii=0; ii < os_prob.initialcons-corecons ; ii++)
00291                         os_prob.addtxtstr<< "ConsA"<<corecons+ii+1<<",";
00292         os_prob.addtxtstr <<" \n";
00293 
00294         for ( ii=0; ii < os_prob.initialcons-corecons; ii++)
00295                         os_prob.addtxtstr<< "ConsA"<<corecons+ii+1<<"\n";
00296         
00297         
00298         finaltxt = os_prob.addtxtstr.str();
00299         fileUtil->writeFileFromString("test.txt", finaltxt);
00300         
00301         
00302         
00303         delete fileUtil;
00304         fileUtil = NULL;
00305 
00306 
00307         }
00308 
00309 
00310 
00311 /*************************************************************************/
00312 
00313 OS_tm::OS_tm(){
00314 
00315 }
00316 
00317 
00318 
00319 /*************************************************************************/
00320 
00321 OS_tm::~OS_tm(){
00322         std::cout << "Inside OS_tm destructor" << std::endl;
00323         delete os_prob.osilreader;
00324         
00325 }

Generated on Mon May 3 03:05:22 2010 by  doxygen 1.4.7