/home/coin/SVN-release/OS-2.3.0/OS/src/OSCommonInterfaces/OSGeneral.cpp

Go to the documentation of this file.
00001 /* $Id: OSGeneral.cpp 3186 2010-02-06 23:38:35Z Gassmann $ */
00018 #include "OSGeneral.h"
00019 #include "OSParameters.h"
00020 #include "OSErrorClass.h"
00021 #include "OSMathUtil.h"
00022 #include "OSBase64.h"
00023 
00024 #include <iostream>
00025 #include <sstream>
00026 
00027 //#define DEBUG_OSGENERAL
00028 //#define DEBUG_ISEQUAL_ROUTINES
00029 
00030 using namespace std;
00031 using std::cout;
00032 using std::endl;
00033 
00034 
00035 SparseVector::SparseVector( int number_):
00036 number( number_)
00037 {
00038         indexes = new int[ number];
00039         values = new double[ number];
00040         bDeleteArrays = true;
00041 }// end SparseVector constructor
00042 
00043 
00044 SparseVector::SparseVector( ):
00045         bDeleteArrays(true),
00046         indexes( NULL),
00047         values( NULL)
00048 {
00049 }// end SparseVector constructor
00050 
00051 SparseVector::~SparseVector(){
00052         #ifdef DEBUG
00053         cout << "inside sparseVector destructor" << endl;
00054         #endif
00055         if(     bDeleteArrays == true){
00056 #ifdef DEBUG
00057 cout << "delete[] indexes and arrays" << endl;
00058 #endif
00059                 delete[] indexes;
00060                 delete[] values;
00061         }
00062         values = NULL;
00063         indexes = NULL;
00064 }// end SparseVector constructor
00065 
00066 SparseMatrix::SparseMatrix():
00067         bDeleteArrays( true),
00068         isColumnMajor(true),
00069         startSize(0),
00070         valueSize(0),
00071         starts(NULL),
00072         indexes(NULL),
00073         values(NULL)
00074 {
00075 }// end SparseMatrix Constructor
00076 
00077 
00078 
00079 SparseMatrix::SparseMatrix(bool isColumnMajor_, int startSize_, int valueSize_):
00080         isColumnMajor(isColumnMajor_), 
00081         startSize(startSize_),
00082         valueSize(valueSize_)
00083 {
00084         bDeleteArrays = true;
00085         starts = new int[startSize];
00086         indexes = new int[valueSize];
00087         values = new double[valueSize];
00088                 
00089 }//end SparseMatrix constructor
00090 
00091 
00092 SparseMatrix::~SparseMatrix(){
00093         #ifdef DEBUG
00094         cout << "inside SparseMatrix destructor" << endl;
00095         #endif
00096         if( bDeleteArrays == true){
00097                 delete[] starts;
00098                 delete[] indexes;
00099                 delete[] values;
00100 
00101         }
00102         starts = NULL;
00103         indexes = NULL;
00104         values = NULL;
00105 }// end SparseMatrix Destructor
00106 
00107 
00108 bool SparseMatrix::display(int secondaryDim){
00109                 int i, j, k;
00110                 for ( i = 0; i < startSize - 1; i++){                   
00111                         
00112                         if (starts[i] == starts[i + 1]){
00113                                 
00114                                 for ( k = 0; k < secondaryDim; k++){
00115                                         //System.out.print("0,");                                       
00116                                 }                               
00117                         }
00118                         else {
00119                                 for ( j = 0; j < indexes[starts[i]]; j ++){                             
00120                                         //System.out.print("0,");                                       
00121                                 }
00122                                 
00123                                 for ( j = starts[ i ]; j < starts[i + 1]; j++){
00124                                         
00125                                         //System.out.print (values[j] + ",");
00126                                         
00127                                         if ( j < starts[i + 1] - 1){
00128                                                 for ( k = indexes [j] + 1; k < indexes[j + 1]; k++){
00129                                                         //System.out.print("0,");                                       
00130                                                 }
00131                                         }
00132                                         else {
00133                                                 
00134                                                 for ( k = indexes [j] + 1; k < secondaryDim; k++){
00135                                                         //System.out.print("0,");                                       
00136                                                 }
00137                                         }
00138                                 }
00139                         }
00140                         //System.out.println();
00141                 }
00142 
00143                 return true;
00144                 
00145         }//display
00146         
00147 SparseJacobianMatrix::SparseJacobianMatrix():
00148         bDeleteArrays( true),
00149         startSize(0),
00150         valueSize(0),
00151         starts(NULL),
00152         conVals(NULL),
00153         indexes(NULL),
00154         values(NULL)
00155 {
00156 }// end SparseJaccobianMatrix Constructor
00157 
00158 
00159 SparseJacobianMatrix::SparseJacobianMatrix(int startSize_, int valueSize_):
00160         bDeleteArrays( true),
00161         startSize(startSize_),
00162         valueSize(valueSize_)
00163 {
00164         starts = new int[startSize];
00165         conVals = new int[startSize];
00166         indexes = new int[valueSize];
00167         values = new double[valueSize];         
00168 }//end SparseJacobianMatrix constructor
00169 
00170 
00171 SparseJacobianMatrix::~SparseJacobianMatrix(){
00172         #ifdef DEBUG
00173         cout << "inside SparseJacobianMatrix destructor" << endl;
00174         #endif
00175         if(bDeleteArrays == true){
00176                 #ifdef DEBUG
00177                 cout << "delete SparseJacobianArrays" << endl;
00178                 #endif
00179                 delete[] starts;
00180                 delete[] conVals;
00181                 delete[] indexes;
00182                 delete[] values;
00183         }
00184                 starts = NULL;
00185                 conVals = NULL;
00186                 indexes = NULL;
00187                 values = NULL;
00188 }// end SparseJacobianMatrix Destructor
00189 
00190 
00191 SparseHessianMatrix::SparseHessianMatrix():
00192         bDeleteArrays( true),
00193         hessDimension(0),
00194         hessRowIdx( NULL),
00195         hessColIdx( NULL),
00196         hessValues( NULL)
00197 {
00198 }// end SparseHessianMatrix Constructor
00199 
00200 
00201 
00202 SparseHessianMatrix::~SparseHessianMatrix(){
00203         #ifdef DEBUG
00204         cout << "inside SparseHessianMatrix destructor" << endl;
00205         #endif
00206         if(bDeleteArrays == true){
00207                 delete[] hessRowIdx;
00208                 delete[] hessColIdx;
00209                 delete[] hessValues;
00210         }
00211         hessRowIdx = NULL;
00212         hessColIdx = NULL;
00213         hessValues = NULL;
00214 }// end SparseHessianMatrix Destructor
00215 
00216 
00217         
00218 QuadraticTerms::QuadraticTerms():
00219         rowIndexes(NULL),
00220         varOneIndexes(NULL),
00221         varTwoIndexes(NULL),
00222         coefficients(NULL)
00223 {
00224 }
00225 
00226 QuadraticTerms::~QuadraticTerms(){
00227         #ifdef DEBUG
00228         cout << "inside Quadratic Terms destructor" << endl;
00229         #endif
00230         delete[] rowIndexes;
00231         rowIndexes = NULL;
00232         delete[] varOneIndexes;
00233         varOneIndexes = NULL;
00234         delete[] varTwoIndexes;
00235         varTwoIndexes = NULL;
00236         delete[] coefficients;
00237         coefficients = NULL;
00238 }
00239 
00240 
00241 IntVector::IntVector():
00242         bDeleteArrays(true),
00243         numberOfEl(0),
00244         el(NULL)
00245 {  
00246         #ifdef DEBUG
00247         cout << "Inside the IntVector Constructor" << endl;
00248         #endif
00249 } 
00250 
00251 
00252 IntVector::IntVector(int n):
00253         bDeleteArrays(true)
00254 {  
00255         #ifdef DEBUG
00256         cout << "Inside the IntVector Constructor" << endl;
00257         #endif
00258 
00259         numberOfEl = n;
00260         el = new int[n];
00261 } 
00262 
00263 IntVector::~IntVector(){  
00264         #ifdef DEBUG
00265         cout << "Inside the IntVector Destructor" << endl;
00266         #endif
00267         if(     bDeleteArrays == true){
00268                 delete[] el;
00269                 el = NULL;
00270         }
00271 } 
00272 
00273 
00274 bool IntVector::IsEqual(IntVector *that)
00275 {
00276         #ifdef DEBUG_ISEQUAL_ROUTINES
00277                 cout << "Start comparing in IntVector" << endl;
00278         #endif
00279         if (this == NULL)
00280         {       if (that == NULL)
00281                         return true;
00282                 else
00283                 {
00284                         #ifdef DEBUG_ISEQUAL_ROUTINES
00285                                 cout << "First object is NULL, second is not" << endl;
00286                         #endif
00287                         return false;
00288                 }
00289         }
00290         else 
00291         {       if (that == NULL)
00292                 {
00293                         #ifdef DEBUG_ISEQUAL_ROUTINES
00294                                 cout << "Second object is NULL, first is not" << endl;
00295                         #endif
00296                         return false;
00297                 }
00298                 else    
00299                 {
00300                         if (this->numberOfEl != that->numberOfEl)
00301                         {
00302 #ifdef DEBUG_ISEQUAL_ROUTINES
00303                                 cout << "numberOfEl: " << this->numberOfEl << " vs. " << that->numberOfEl << endl;
00304 #endif  
00305                                 return false;
00306                         }
00307                         for (int i=0; i<this->numberOfEl; i++)
00308                         {
00309                                 if (this->el[i] != that->el[i])
00310                                 {
00311 
00312 #ifdef DEBUG_ISEQUAL_ROUTINES
00313                                         cout << "El[" << i << "]: " << this->el[i] << " vs. " << that->el[i] << endl;
00314 #endif  
00315                                         return false;
00316                                 }
00317                         }
00318                         return true;
00319                 }
00320         }
00321 }//IntVector::IsEqual
00322 
00323 
00324 OtherOptionEnumeration::OtherOptionEnumeration():
00325         IntVector(),
00326         value(""),
00327         description("")
00328 {  
00329         #ifdef DEBUG
00330         cout << "Inside the OtherOptionEnumeration Constructor" << endl;
00331         #endif
00332         //IntVector::IntVector();
00333 } 
00334 
00335 OtherOptionEnumeration::OtherOptionEnumeration(int n):
00336         IntVector(n),
00337         value(""),
00338         description("")
00339 {  
00340         #ifdef DEBUG
00341         cout << "Inside the OtherOptionEnumeration Constructor" << endl;
00342         #endif
00343 
00344         //IntVector::IntVector(n);
00345 } 
00346 
00347 OtherOptionEnumeration::~OtherOptionEnumeration(){  
00348         #ifdef DEBUG
00349         cout << "Inside the OtherOptionEnumeration Destructor" << endl;
00350         #endif
00351 } 
00352 
00353 bool OtherOptionEnumeration::setOtherOptionEnumeration(std::string value, std::string description, int *i, int ni)
00354 {
00355         if (this->el == NULL) this->el = new int[ni];
00356         this->value = value;
00357         this->description = description;
00358         this->numberOfEl = ni;
00359         for (int j=0; j<ni; j++)
00360                 this->el[j] = i[j];
00361         return true;
00362 }
00363 
00364 bool OtherOptionEnumeration::IsEqual(OtherOptionEnumeration *that)
00365 {
00366         #ifdef DEBUG_ISEQUAL_ROUTINES
00367                 cout << "Start comparing in IntVector" << endl;
00368         #endif
00369         if (this == NULL)
00370         {       if (that == NULL)
00371                         return true;
00372                 else
00373                 {
00374                         #ifdef DEBUG_ISEQUAL_ROUTINES
00375                                 cout << "First object is NULL, second is not" << endl;
00376                         #endif
00377                         return false;
00378                 }
00379         }
00380         else 
00381         {       if (that == NULL)
00382                 {
00383                         #ifdef DEBUG_ISEQUAL_ROUTINES
00384                                 cout << "Second object is NULL, first is not" << endl;
00385                         #endif
00386                         return false;
00387                 }
00388                 else    
00389                 {
00390                         if (this->value != that->value || this->description != that->description)
00391                         {
00392 #ifdef DEBUG_ISEQUAL_ROUTINES
00393                                 cout << "value:       " << this->value       << " vs. " << that->value       << endl;
00394                                 cout << "description: " << this->description << " vs. " << that->description << endl;
00395 #endif  
00396                                 return false;
00397                         }
00398 
00399                         return this->IntVector::IsEqual(that);
00400                 }
00401         }
00402 }//IntVector::IsEqual
00403 
00404 
00405 
00406 DoubleVector::DoubleVector():
00407         bDeleteArrays(true),
00408         el(NULL)
00409 {
00410         #ifdef DEBUG
00411         cout << "Inside the DoubleVector Constructor" << endl;
00412         #endif
00413 
00414 }
00415 
00416 
00417 DoubleVector::~DoubleVector(){  
00418         #ifdef DEBUG
00419         cout << "Inside the DoubleVector Destructor" << endl;
00420         #endif
00421         if(     bDeleteArrays == true){
00422                 delete[] el;
00423                 el = NULL;
00424         }
00425 }
00426 
00427 
00428 bool DoubleVector::IsEqual(DoubleVector *that)
00429 {
00430         #ifdef DEBUG_ISEQUAL_ROUTINES
00431                 cout << "Start comparing in DoubleVector" << endl;
00432         #endif
00433         if (this == NULL)
00434         {       if (that == NULL)
00435                         return true;
00436                 else
00437                 {
00438                         #ifdef DEBUG_ISEQUAL_ROUTINES
00439                                 cout << "First object is NULL, second is not" << endl;
00440                         #endif
00441                         return false;
00442                 }
00443         }
00444         else 
00445         {       if (that == NULL)
00446                 {
00447                         #ifdef DEBUG_ISEQUAL_ROUTINES
00448                                 cout << "Second object is NULL, first is not" << endl;
00449                         #endif
00450                         return false;
00451                 }
00452                 else    
00453                 {
00454                         if (this->numberOfEl != that->numberOfEl)
00455                         {
00456 #ifdef DEBUG_ISEQUAL_ROUTINES
00457                                 cout << "numberOfEl: " << this->numberOfEl << " vs. " << that->numberOfEl << endl;
00458 #endif  
00459                                 return false;
00460                         }
00461                         for (int i=0; i<this->numberOfEl; i++)
00462                         {
00463                                 if (this->el[i] != that->el[i])
00464                                 {
00465 
00466 #ifdef DEBUG_ISEQUAL_ROUTINES
00467                                         cout << "El[" << i << "]: " << this->el[i] << " vs. " << that->el[i] << endl;
00468 #endif  
00469                                         return false;
00470                                 }
00471                         }
00472                         return true;
00473                 }
00474         }
00475 }//DoubleVector::IsEqual
00476 
00477 
00478 BasisStatus::BasisStatus():
00479         basic(NULL),
00480         atLower(NULL),
00481         atUpper(NULL),
00482         isFree(NULL),
00483         superbasic(NULL),
00484         unknown(NULL)
00485 { 
00486         #ifdef DEBUG_OSRESULT
00487         cout << "Inside the BasisStatus Constructor" << endl;
00488         #endif
00489 }//end BasisStatus constructor
00490 
00491 
00492 BasisStatus::~BasisStatus()
00493 {
00494         #ifdef DEBUG_OSRESULT  
00495         cout << "Inside the BasisStatus Destructor" << endl;
00496         #endif
00497         if (basic != NULL)
00498         {
00499                 delete basic;
00500                 basic = NULL;
00501         }
00502         if (atLower != NULL)
00503         {
00504                 delete atLower;
00505                 atLower = NULL;
00506         }
00507         if (atUpper != NULL)
00508         {
00509                 delete atUpper;
00510                 atUpper = NULL;
00511         }
00512         if (isFree != NULL)
00513         {
00514                 delete isFree;
00515                 isFree = NULL;
00516         }
00517         if (superbasic != NULL)
00518         {
00519                 delete superbasic;
00520                 superbasic = NULL;
00521         }
00522         if (unknown != NULL)
00523         {
00524                 delete unknown;
00525                 unknown = NULL;
00526         }
00527 }// end BasisStatus destructor 
00528 
00529 
00530 bool BasisStatus::setBasisStatusIntVector(int status, int *i, int ni)
00531 {
00532         switch (status)
00533         {
00534                 case ENUM_BASIS_STATUS_basic:
00535                 {
00536                         if (this->basic == NULL) this->basic = new IntVector(ni);
00537                         else delete[] this->basic;
00538                         this->basic->numberOfEl = ni;
00539                         for (int j=0; j<ni; j++)
00540                                 this->basic->el[j] = i[j];
00541                         return true;
00542                 }
00543                 case ENUM_BASIS_STATUS_atLower:
00544                 {
00545                         if (this->atLower == NULL) this->atLower = new IntVector(ni);
00546                         else delete[] this->atLower;
00547                         this->atLower->numberOfEl = ni;
00548                         for (int j=0; j<ni; j++)
00549                                 this->atLower->el[j] = i[j];
00550                         return true;
00551                 }
00552                 case ENUM_BASIS_STATUS_atUpper:
00553                 {
00554                         if (this->atUpper == NULL) this->atUpper = new IntVector(ni);
00555                         else delete[] this->atUpper;
00556                         this->atUpper->numberOfEl = ni;
00557                         for (int j=0; j<ni; j++)
00558                                 this->atUpper->el[j] = i[j];
00559                         return true;
00560                 }
00561                 case ENUM_BASIS_STATUS_isFree:
00562                 {
00563                         if (this->isFree == NULL) this->isFree = new IntVector(ni);
00564                         else delete[] this->isFree;
00565                         this->isFree->numberOfEl = ni;
00566                         for (int j=0; j<ni; j++)
00567                                 this->isFree->el[j] = i[j];
00568                         return true;
00569                 }
00570                 case ENUM_BASIS_STATUS_superbasic:
00571                 {
00572                         if (this->superbasic == NULL) this->superbasic = new IntVector(ni);
00573                         else delete[] this->superbasic;
00574                         this->superbasic->numberOfEl = ni;
00575                         for (int j=0; j<ni; j++)
00576                                 this->superbasic->el[j] = i[j];
00577                         return true;
00578                 }
00579                 case ENUM_BASIS_STATUS_unknown:
00580                 {
00581                         if (this->unknown == NULL) this->unknown = new IntVector(ni);
00582                         else delete[] this->unknown;
00583                         this->unknown->numberOfEl = ni;
00584                         for (int j=0; j<ni; j++)
00585                                 this->unknown->el[j] = i[j];
00586                         return true;
00587                 }
00588         default:
00589                 throw ErrorClass("Unknown basis status encountered in setBasisStatusIntVector");  
00590          }
00591 }//setBasisStatusIntVector
00592 
00593 bool BasisStatus::IsEqual(BasisStatus *that)
00594 {
00595         #ifdef DEBUG_ISEQUAL_ROUTINES
00596                 cout << "Start comparing in BasisStatus" << endl;
00597         #endif
00598         if (this == NULL)
00599         {       if (that == NULL)
00600                         return true;
00601                 else
00602                 {
00603                         #ifdef DEBUG_ISEQUAL_ROUTINES
00604                                 cout << "First object is NULL, second is not" << endl;
00605                         #endif
00606                         return false;
00607                 }
00608         }
00609         else 
00610         {       if (that == NULL)
00611                 {
00612                         #ifdef DEBUG_ISEQUAL_ROUTINES
00613                                 cout << "Second object is NULL, first is not" << endl;
00614                         #endif
00615                         return false;
00616                 }
00617                 else    
00618                 {
00619                         if (      !this->basic->IsEqual(that->basic)      ) return false;
00620                         if (    !this->atLower->IsEqual(that->atLower)    ) return false;
00621                         if (    !this->atUpper->IsEqual(that->atUpper)    ) return false;
00622                         if (     !this->isFree->IsEqual(that->isFree)     ) return false;
00623                         if ( !this->superbasic->IsEqual(that->superbasic) ) return false;
00624                         if (    !this->unknown->IsEqual(that->unknown)    ) return false;
00625 
00626                         return true;
00627                 }
00628         }
00629 }//BasisStatus::IsEqual
00630 
00631 

Generated on Fri Nov 19 13:31:04 2010 by  doxygen 1.4.7