/home/coin/SVN-release/OS-2.3.5/OS/src/OSCommonInterfaces/OSInstance.cpp

Go to the documentation of this file.
00001 /* $Id: OSInstance.cpp 4120 2011-03-30 06:28:16Z kmartin $ */
00021 #include "OSGeneral.h"
00022 #include "OSInstance.h"
00023 #include "OSMathUtil.h"
00024 #include "OSErrorClass.h"
00025 #include "OSParameters.h"
00026 
00027 #include<stack>
00028 #include<iostream>  
00029 #include<sstream>
00030 
00031 //#define DEBUG
00032 #define DEBUG_ISEQUAL_ROUTINES
00033 
00034 using namespace std;
00035 using std::ostringstream; 
00036 
00037 
00038 OSInstance::OSInstance(): 
00039         bVariablesModified(false),
00040         bObjectivesModified(false),
00041         bConstraintsModified(false),
00042         bAMatrixModified(false),
00043         m_sInstanceName(""),
00044         m_sInstanceSource(""),  
00045         m_sInstanceDescription(""),
00046         m_bProcessVariables(false),
00047         m_iVariableNumber(-1),
00048         m_iNumberOfIntegerVariables( 0),
00049         m_iNumberOfBinaryVariables( 0),
00050         m_iNumberOfSemiContinuousVariables( 0),
00051         m_iNumberOfSemiIntegerVariables( 0),
00052         m_iNumberOfStringVariables( 0),
00053         m_iNumberOfQuadraticRowIndexes( 0),
00054         m_bQuadraticRowIndexesProcessed( false),
00055         m_miQuadRowIndexes( NULL),
00056         m_iNumberOfNonlinearExpressionTreeIndexes( 0),
00057         m_bNonlinearExpressionTreeIndexesProcessed( false),
00058         m_miNonlinearExpressionTreeIndexes( NULL),
00059         m_iNumberOfNonlinearExpressionTreeModIndexes( 0),
00060         m_bNonlinearExpressionTreeModIndexesProcessed( false),
00061         m_miNonlinearExpressionTreeModIndexes( NULL),           
00062         m_msVariableNames(NULL),
00063         //m_mdVariableInitialValues(NULL), -- deprecated
00064         //m_msVariableInitialStringValues(NULL), -- deprecated
00065         m_mcVariableTypes(NULL),
00066         m_mdVariableLowerBounds(NULL),
00067         m_mdVariableUpperBounds(NULL),
00068         m_bProcessObjectives(false),
00069         m_iObjectiveNumber(-1),
00070         m_iObjectiveNumberNonlinear( 0),
00071         m_msObjectiveNames(NULL),
00072         m_msMaxOrMins(NULL),
00073         m_miNumberOfObjCoef(NULL),
00074         m_mdObjectiveConstants(NULL),
00075         m_mdObjectiveWeights(NULL),
00076         m_mObjectiveCoefficients(NULL),
00077         m_bGetDenseObjectives(false),
00078         m_mmdDenseObjectiveCoefficients(NULL),
00079         m_bProcessConstraints(false),
00080         m_iConstraintNumber(-1),
00081         m_iConstraintNumberNonlinear( 0),       
00082         m_msConstraintNames(NULL),
00083         m_mdConstraintLowerBounds(NULL),
00084         m_mdConstraintUpperBounds(NULL),
00085         m_mdConstraintConstants( NULL),
00086         m_mcConstraintTypes(NULL),
00087         m_bProcessLinearConstraintCoefficients(false),  
00088         m_iLinearConstraintCoefficientNumber(-1),
00089         m_bColumnMajor(true),
00090         m_binitForAlgDiff( false),      
00091         m_linearConstraintCoefficientsInColumnMajor(NULL),
00092         m_linearConstraintCoefficientsInRowMajor(NULL), 
00093         m_bProcessQuadraticTerms(false),
00094         m_iQuadraticTermNumber(-1),
00095         m_mdConstraintFunctionValues( NULL),
00096         m_mdObjectiveFunctionValues( NULL),
00097         m_iJacValueSize( 0),
00098         m_miJacStart( NULL),
00099         m_miJacIndex( NULL),
00100         m_mdJacValue( NULL),
00101         m_miJacNumConTerms( NULL),
00102         m_sparseJacMatrix( NULL),       
00103         m_iHighestTaylorCoeffOrder(-1), 
00104         m_quadraticTerms( NULL),        
00105         m_bQTermsAdded( false), 
00106         m_iNumberOfNonlinearVariables( 0),
00107         m_bProcessNonlinearExpressions( false),
00108         m_iNonlinearExpressionNumber( -1),              
00109         m_miNonlinearExpressionIndexes( NULL),
00110         m_bProcessExpressionTrees( false),
00111         m_bProcessExpressionTreesMod( false),
00112         m_LagrangianExpTree(NULL),
00113         m_bLagrangianExpTreeCreated( false),
00114         m_LagrangianSparseHessian( NULL),
00115         m_bLagrangianSparseHessianCreated( false),
00116         m_miNonLinearVarsReverseMap( NULL),
00117         m_bAllNonlinearVariablesIndex( false),
00118         m_bOSADFunIsCreated( false),
00119         m_bCppADTapesBuilt( false),
00120         m_bCppADMustReTape( false),
00121         m_bDuplicateExpressionTreesMap( false),
00122         m_bNonLinearStructuresInitialized( false),
00123         m_bSparseJacobianCalculated( false),
00124         m_iHighestOrderEvaluated( -1),
00125         m_mmdObjGradient( NULL),
00126         m_bProcessTimeDomain( false),
00127         m_bProcessTimeStages( false),
00128         m_bProcessTimeInterval( false),
00129         m_bFiniteTimeStages( false),
00130         m_iNumberOfTimeStages(-1),
00131         m_msTimeDomainStageNames(NULL),
00132         m_miTimeDomainStageVariableNumber(NULL),
00133         m_mmiTimeDomainStageVarList(NULL),
00134         m_miTimeDomainStageConstraintNumber(NULL),
00135         m_mmiTimeDomainStageConList(NULL),
00136         m_miTimeDomainStageObjectiveNumber(NULL),
00137         m_mmiTimeDomainStageObjList(NULL),
00138         bUseExpTreeForFunEval( false)
00139 
00140 {    
00141         #ifdef DEBUG
00142         cout << "Inside OSInstance Constructor" << endl;
00143         #endif
00144         this->instanceHeader = new GeneralFileHeader();
00145         this->instanceData = new InstanceData();
00146 }  
00147 
00148 OSInstance::~OSInstance(){
00149         #ifdef DEBUG
00150         cout << "OSInstance Destructor Called" << endl;
00151         #endif
00152         std::map<int, OSExpressionTree*>::iterator posMapExpTree;
00153         // delete  the temporary arrays
00154         
00155         if(this->instanceData->variables->numberOfVariables > 0 && m_bProcessVariables == true){
00156                 delete[] m_msVariableNames;
00157                 m_msVariableNames = NULL;
00158                 delete[] m_mcVariableTypes;
00159                 m_mcVariableTypes = NULL;
00160                 delete[] m_mdVariableLowerBounds;
00161                 m_mdVariableLowerBounds = NULL;
00162                 delete[] m_mdVariableUpperBounds;
00163                 m_mdVariableUpperBounds = NULL;
00164         }
00165         
00166         
00167         if(m_bProcessConstraints == true){
00168                 delete[] m_msConstraintNames;
00169                 m_msConstraintNames = NULL;
00170                 delete[] m_mcConstraintTypes;
00171                 m_mcConstraintTypes = NULL;
00172                 delete[]  m_mdConstraintConstants;
00173                 m_mdConstraintConstants = NULL;
00174                 delete[] m_mdConstraintLowerBounds;
00175                 m_mdConstraintLowerBounds = NULL;
00176                 delete[] m_mdConstraintUpperBounds;
00177                 m_mdConstraintUpperBounds = NULL;
00178         }
00179         
00180         
00181         int i;
00182         //if(instanceData->objectives->numberOfObjectives > 0 && m_mObjectiveCoefficients != NULL){
00183         if(m_bProcessObjectives == true ){
00184                 for(i = 0; i < instanceData->objectives->numberOfObjectives; i++){
00185                         #ifdef DEBUG
00186                         std::cout <<  "Delete m_mObjectiveCoefficients[i]" << std::endl;
00187                         #endif
00188                         delete m_mObjectiveCoefficients[i];
00189                         m_mObjectiveCoefficients[i] = NULL;
00190                 }
00191                 #ifdef DEBUG
00192                 std::cout <<  "Delete m_msObjectiveNames" << std::endl;
00193                 std::cout <<  "Delete m_msMaxOrMins" << std::endl;
00194                 std::cout <<  "Delete m_miNumberOfObjCoef" << std::endl;
00195                 std::cout <<  "Delete m_mdObjectiveConstants" << std::endl;
00196                 std::cout <<  "Delete m_mdObjectiveWeights" << std::endl;
00197                 #endif          
00198                 delete[] m_msObjectiveNames;
00199                 m_msObjectiveNames = NULL;
00200                 delete[] m_msMaxOrMins;
00201                 m_msMaxOrMins = NULL;
00202                 delete[] m_miNumberOfObjCoef;
00203                 m_miNumberOfObjCoef = NULL;
00204                 delete[] m_mdObjectiveConstants; 
00205                 m_mdObjectiveConstants = NULL;
00206                 delete[] m_mdObjectiveWeights;
00207                 m_mdObjectiveWeights = NULL;
00208                 delete[] m_mObjectiveCoefficients;
00209                 m_mObjectiveCoefficients = NULL;
00210         }
00211         
00212         if(m_bGetDenseObjectives == true){
00213                 for(i = 0; i < instanceData->objectives->numberOfObjectives; i++){
00214                         //delete m_mmdDenseObjectiveCoefficients[i];
00215                         #ifdef DEBUG
00216                         std::cout <<  "delete m_mmdDenseObjectiveCoefficients[i]" << std::endl;
00217                         #endif
00218                     delete[] m_mmdDenseObjectiveCoefficients[i];
00219                         m_mmdDenseObjectiveCoefficients[i] = NULL;
00220                 }
00221                 delete[] m_mmdDenseObjectiveCoefficients;
00222                 m_mmdDenseObjectiveCoefficients = NULL;
00223         }
00224         
00225         
00226         
00227         //if(m_bProcessLinearConstraintCoefficients == true && m_bColumnMajor == true) delete m_linearConstraintCoefficientsInColumnMajor;
00228         //if(m_bProcessLinearConstraintCoefficients == true && m_bColumnMajor == false) delete m_linearConstraintCoefficientsInRowMajor;
00229         
00230         if( m_linearConstraintCoefficientsInColumnMajor != NULL) delete m_linearConstraintCoefficientsInColumnMajor;
00231         if (m_linearConstraintCoefficientsInRowMajor != NULL ) delete m_linearConstraintCoefficientsInRowMajor;
00232         
00233         
00234         if( (m_binitForAlgDiff == true)  ){     
00235                 delete[] m_miNonLinearVarsReverseMap;
00236                 m_miNonLinearVarsReverseMap = NULL;
00237                 if(instanceData->objectives->numberOfObjectives > 0 && m_mmdObjGradient != NULL){
00238                         
00239                         #ifdef DEBUG
00240                         std::cout <<  "The number of objectives =  " << instanceData->objectives->numberOfObjectives << std::endl;
00241                         #endif
00242                         for(i = 0; i < instanceData->objectives->numberOfObjectives; i++){
00243                                 #ifdef DEBUG
00244                                 std::cout << "deleting Objective function gradient " << i << std::endl;
00245                                 #endif
00246                                 delete[] m_mmdObjGradient[i];
00247 
00248                                 m_mmdObjGradient[i] = NULL;
00249                         }
00250                         delete[] m_mmdObjGradient;
00251                         m_mmdObjGradient = NULL;
00252                 }
00253         }
00254 
00255         //std::cout << "Do garbage collection for the nonlinear API" << std::endl;
00256         // garbage collection for the gradient
00257         if(m_bNonLinearStructuresInitialized == true ){
00258                 delete[] m_mdObjectiveFunctionValues; 
00259                 m_mdObjectiveFunctionValues = NULL;     
00260                 delete[] m_mdConstraintFunctionValues;
00261                 m_mdConstraintFunctionValues = NULL;
00262         }
00263         if(m_bSparseJacobianCalculated == true){
00264                 delete[] m_miJacStart;
00265                 m_miJacStart = NULL;
00266                 delete[] m_miJacIndex;
00267                 m_miJacIndex = NULL;
00268                 delete[] m_mdJacValue;
00269                 m_mdJacValue = NULL;
00270                 delete[] m_miJacNumConTerms;
00271                 m_miJacNumConTerms = NULL;
00272         }
00273         if( m_bLagrangianExpTreeCreated == true){
00274                 delete m_LagrangianExpTree;
00275                 m_LagrangianExpTree = NULL;
00276         }
00277         if( m_bLagrangianSparseHessianCreated == true){
00278                 delete m_LagrangianSparseHessian;
00279                 m_LagrangianSparseHessian = NULL;
00280         }
00281         if( m_bSparseJacobianCalculated == true){
00282                 delete m_sparseJacMatrix;
00283                 m_sparseJacMatrix = NULL;
00284         }
00285         if( (instanceData->quadraticCoefficients->qTerm != NULL) && (m_bProcessQuadraticTerms == true) ){
00286                 delete m_quadraticTerms;
00287                 m_quadraticTerms = NULL;
00288         }
00289         if( (instanceData->quadraticCoefficients->qTerm != NULL) && (m_bQuadraticRowIndexesProcessed == true) ){
00290                 delete[] m_miQuadRowIndexes;
00291                 m_miQuadRowIndexes = NULL;
00292         }
00293         //
00294         // delete the new expression trees that got created
00295         //if( m_bLagrangianExpTreeCreated == false  ||  m_bLagrangianExpTreeCreated == true){
00296         if( (m_bProcessExpressionTrees == true) && (m_bDuplicateExpressionTreesMap == false)  ) {
00297                 for(posMapExpTree = m_mapExpressionTrees.begin(); posMapExpTree != m_mapExpressionTrees.end(); ++posMapExpTree){
00298                         std::cout << "Deleting an expression tree from the map for row  " << posMapExpTree->first  << std::endl;
00299                         delete m_mapExpressionTrees[ posMapExpTree->first ];
00300                 }
00301         }
00302         if( m_bDuplicateExpressionTreesMap == true)   {
00303                 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree){          
00304                         #ifdef DEBUG
00305                                 std::cout << "Deleting an expression tree from m_mapExpressionTreesMod" << std::endl;
00306                         #endif
00307                         delete m_mapExpressionTreesMod[ posMapExpTree->first ];
00308                 }
00309         }
00310         //}
00312         if( (m_bNonlinearExpressionTreeIndexesProcessed == true) && (m_mapExpressionTrees.size() > 0) ){
00313                 std::cout << "Deleting  m_miNonlinearExpressionTreeIndexes" << std::endl;
00314                 delete[] m_miNonlinearExpressionTreeIndexes;
00315                 std::cout << "Done Deleting  m_miNonlinearExpressionTreeIndexes" << std::endl;
00316                 m_miNonlinearExpressionTreeIndexes = NULL;
00317         }
00318         if( (m_bNonlinearExpressionTreeModIndexesProcessed == true) && (m_mapExpressionTreesMod.size() > 0) ){
00319                 std::cout << "Deleting  m_miNonlinearExpressionTreeModIndexes" << std::endl;
00320                 delete[] m_miNonlinearExpressionTreeModIndexes;
00321                 std::cout << "Done Deleting  m_miNonlinearExpressionTreeModIndexes" << std::endl;
00322                 m_miNonlinearExpressionTreeModIndexes = NULL;
00323         }
00324         if(m_bOSADFunIsCreated == true){
00325                 try{
00326 #ifdef COIN_HAS_CPPAD
00327                         delete Fad;
00328                         Fad = NULL;
00329 #else
00330                 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
00331 #endif
00332                 }
00333                 catch(const ErrorClass& eclass){
00334                         throw ErrorClass( eclass.errormsg);
00335                 }
00336         }
00337 //      if( (instanceData->timeDomain->stages->stage != NULL) && (m_bProcessTimeStages == true) ){
00338 //              delete m_Stages;
00339 //              m_Stages = NULL;
00340 //      }
00341 
00342         if (m_msTimeDomainStageNames != NULL) {
00343                 delete[] m_msTimeDomainStageNames;
00344                 m_msTimeDomainStageNames = NULL;
00345         }
00346 
00347         if (m_miTimeDomainStageVariableNumber != NULL) {
00348                 delete[] m_miTimeDomainStageVariableNumber;
00349                 m_miTimeDomainStageVariableNumber = NULL;
00350         }
00351 
00352         if (m_mmiTimeDomainStageVarList != NULL) {
00353                 for (int i = 0; i < m_iNumberOfTimeStages; i ++) 
00354                         delete[] m_mmiTimeDomainStageVarList[i];
00355                 delete[] m_mmiTimeDomainStageVarList;
00356                 m_mmiTimeDomainStageVarList = NULL;
00357         }
00358 
00359         if (m_miTimeDomainStageConstraintNumber != NULL) {
00360                 delete[] m_miTimeDomainStageConstraintNumber;
00361                 m_miTimeDomainStageConstraintNumber = NULL;
00362         }
00363 
00364         if (m_mmiTimeDomainStageConList != NULL) {
00365                 for (int i = 0; i < m_iNumberOfTimeStages; i ++) 
00366                         delete[] m_mmiTimeDomainStageConList[i];
00367                 delete[] m_mmiTimeDomainStageConList;
00368                 m_mmiTimeDomainStageConList = NULL;
00369         }
00370 
00371         if (m_miTimeDomainStageObjectiveNumber != NULL) {
00372                 delete[] m_miTimeDomainStageObjectiveNumber;
00373                 m_miTimeDomainStageObjectiveNumber = NULL;
00374         }
00375 
00376         if (m_mmiTimeDomainStageObjList != NULL) {
00377                 for (int i = 0; i < m_iNumberOfTimeStages; i ++) 
00378                         delete[] m_mmiTimeDomainStageObjList[i];
00379                 delete[] m_mmiTimeDomainStageObjList;
00380                 m_mmiTimeDomainStageObjList = NULL;
00381         }
00382 
00383         // delete the two children of OSInstance
00384         //delete instanceHeader object
00385         delete instanceHeader;
00386         instanceHeader = NULL;
00387         //delete instanceData object
00388         delete instanceData;
00389         instanceData = NULL;
00390 }//OSInstance Destructor
00391 
00392 InstanceHeader::InstanceHeader():
00393         description(""),
00394         name(""),
00395         source("")
00396 
00397 { 
00398         #ifdef DEBUG
00399         cout << "Inside the InstanceHeader Constructor" << endl;
00400         #endif
00401 } 
00402 
00403 
00404 InstanceHeader::~InstanceHeader(){
00405         #ifdef DEBUG  
00406         cout << "Inside the InstanceHeader Destructor" << endl;
00407         #endif
00408 } 
00409 
00410 Variable::Variable():
00411         lb(0.0),
00412         ub(OSDBL_MAX),
00413         //init(OSNAN),  deprecated
00414         type('C'), 
00415         name("")
00416         //initString("") deprecated
00417 {  
00418         #ifdef DEBUG
00419         cout << "Inside the Variable Constructor" << endl;
00420         #endif 
00421 } 
00422 
00423 Variable::~Variable(){  
00424         #ifdef DEBUG
00425         cout << "Inside the Variable Destructor" << endl; 
00426         #endif
00427 } 
00428 
00429 Variables::Variables(){  
00430         #ifdef DEBUG
00431         cout << "Inside the Variables Constructor" << endl; 
00432         #endif 
00433         numberOfVariables = 0;
00434         var = NULL; 
00435 }
00436 
00437 Variables::~Variables(){ 
00438         #ifdef DEBUG 
00439         cout << "Inside the Variables Destructor" << endl;
00440         #endif
00441         int i;
00442         if(numberOfVariables > 0 && var != NULL){
00443                 for(i = 0; i < numberOfVariables; i++){
00444                         #ifdef DEBUG 
00445                         cout << "Deleting var[ i]" << endl;
00446                         #endif
00447                         delete var[i];
00448                         var[i] = NULL;
00449                 }
00450         }
00451         if (var != NULL)
00452                 delete[] var;
00453         var = NULL; 
00454 }  
00455 
00456 ObjCoef::ObjCoef():
00457         idx(-1),
00458         value(0.0)  
00459 {  
00460         #ifdef DEBUG
00461         cout << "Inside the Coef Constructor" << endl;
00462         #endif 
00463 }
00464 
00465 ObjCoef::~ObjCoef(){ 
00466         #ifdef DEBUG
00467         cout << "Inside the ObjCoef Destructor" << endl;  
00468         #endif
00469 }
00470 
00471 Objective::Objective():
00472         name("") ,
00473         maxOrMin("min"),
00474         constant(0.0),
00475         weight(OSNAN),
00476         numberOfObjCoef(0),
00477         coef(NULL)
00478 { 
00479  
00480         #ifdef DEBUG
00481         cout << "Inside the Objective Constructor" << endl;
00482         #endif
00483 }
00484 
00485 Objective::~Objective(){
00486         #ifdef DEBUG  
00487         cout << "Inside the Objective Destructor" << endl;
00488         #endif
00489         int i;
00490         if(numberOfObjCoef > 0 && coef != NULL){
00491                 for(i = 0; i < numberOfObjCoef; i++){
00492                         delete coef[i];
00493                         coef[i] = NULL;
00494                 }
00495         }
00496         if (coef != NULL)
00497                 delete[] coef;
00498         coef = NULL;
00499 }  
00500 
00501 Objectives::Objectives()
00502 {  
00503         #ifdef DEBUG
00504         cout << "Inside the Objectives Constructor" << endl; 
00505         #endif
00506         numberOfObjectives = 0;
00507         obj = NULL;
00508 } 
00509 
00510 Objectives::~Objectives(){ 
00511         #ifdef DEBUG 
00512         cout << "Inside the Objectives Destructor" << endl;
00513         #endif
00514         int i;
00515         if(numberOfObjectives > 0 && obj != NULL){
00516                 for(i = 0; i < numberOfObjectives; i++){
00517                         delete obj[i];
00518                         obj[i] = NULL;
00519                 }
00520         }
00521         if (obj != NULL)
00522                 delete[] obj;
00523         obj = NULL;
00524 }
00525 
00526 Constraint::Constraint():
00527         name(""),
00528         constant(0.0),
00529         lb(-OSDBL_MAX),
00530         ub(OSDBL_MAX)
00531 
00532 {
00533         #ifdef DEBUG  
00534         cout << "Inside the Constraint Constructor" << endl;
00535         #endif
00536 } 
00537 
00538 Constraint::~Constraint(){  
00539         #ifdef DEBUG
00540         cout << "Inside the Constraint Destructor" << endl;
00541         #endif
00542 } 
00543 
00544 Constraints::Constraints():
00545         numberOfConstraints(0),
00546         con(NULL)
00547 {
00548         #ifdef DEBUG
00549         cout << "Inside the Constraints Constructor" << endl;
00550         #endif
00551 } 
00552 
00553 Constraints::~Constraints(){  
00554         #ifdef DEBUG
00555         cout << "Inside the Constraints Destructor" << endl;
00556         #endif
00557         int i;
00558         if(numberOfConstraints > 0 && con != NULL){
00559                 for( i = 0; i < numberOfConstraints; i++){
00560                         delete con[i];
00561                         con[i] = NULL;
00562                 }
00563         }
00564         if (con != NULL)
00565                 delete[] con;
00566         con = NULL;
00567 } 
00568 
00569 
00570 
00571 LinearConstraintCoefficients::LinearConstraintCoefficients():
00572         numberOfValues(0) ,
00573         iNumberOfStartElements( 0)
00574 { 
00575         #ifdef DEBUG 
00576         cout << "Inside the LinearConstraintCoefficients Constructor" << endl; 
00577         #endif
00578         start  = new IntVector();
00579         rowIdx = new IntVector();
00580         colIdx = new IntVector();
00581         value  = new DoubleVector();
00582 
00583 } 
00584 
00585 
00586 LinearConstraintCoefficients::~LinearConstraintCoefficients(){  
00587         #ifdef DEBUG
00588         cout << "Inside the LinearConstraintCoefficients Destructor" << endl; 
00589         #endif
00590         delete start;
00591         start = NULL;
00592         delete rowIdx;
00593         rowIdx = NULL;
00594         delete colIdx;
00595         colIdx = NULL;
00596         delete value;
00597         value = NULL;
00598 }
00599 
00600 QuadraticTerm::QuadraticTerm():
00601 
00602         idx(0),   
00603         idxOne(-1),
00604         idxTwo(-1),
00605         coef(0.0)
00606 
00607 {
00608         #ifdef DEBUG  
00609         cout << "Inside the QuadraticTerm Constructor" << endl;
00610         #endif
00611 } 
00612 
00613 
00614 QuadraticTerm::~QuadraticTerm(){  
00615         #ifdef DEBUG
00616         cout << "Inside the QuadraticTerm Destructor" << endl;
00617         #endif
00618 }
00619 
00620 
00621 
00622 QuadraticCoefficients::QuadraticCoefficients():
00623         numberOfQuadraticTerms(0),
00624         qTerm(NULL)
00625 { 
00626         #ifdef DEBUG 
00627         cout << "Inside the QuadraticCoefficients Constructor" << endl;
00628         #endif
00629 }//end QuadraticCoefficients() 
00630 
00631 
00632 QuadraticCoefficients::~QuadraticCoefficients(){
00633         #ifdef DEBUG  
00634         cout << "Inside the QuadraticCoefficients Destructor" << endl;
00635         #endif
00636         int i;
00637         if(numberOfQuadraticTerms > 0 && qTerm != NULL){
00638                 for( i = 0; i < numberOfQuadraticTerms; i++){
00639                         delete qTerm[i];
00640                         qTerm[i] = NULL;
00641                 }
00642         }
00643         if (qTerm != NULL)
00644                 delete[] qTerm;
00645         qTerm = NULL;  
00646 }//end ~QuadraticCoefficients()  
00647 
00648 
00649 Nl::Nl(){
00650         idx = 0;
00651         osExpressionTree = NULL;
00652         m_bDeleteExpressionTree = true;
00653 }//end Nl
00654  
00655  
00656 Nl::~Nl(){
00657         #ifdef DEBUG  
00658         cout << "Inside the Nl Destructor" << endl;
00659         #endif
00660         // don't delete the expression tree if we created a map of the expression
00661         // trees, otherwise we would destroy twice
00662         if( m_bDeleteExpressionTree == true){
00663                 delete osExpressionTree;
00664                 osExpressionTree = NULL;
00665         }
00666 }//end ~Nl
00667 
00668 
00669 
00670 NonlinearExpressions::NonlinearExpressions():
00671         numberOfNonlinearExpressions(0) ,
00672         nl(NULL)
00673 { 
00674         #ifdef DEBUG 
00675         cout << "Inside the NonlinearExpressions Constructor" << endl;
00676         #endif
00677 }//end NonlinearExpressions() 
00678 
00679 NonlinearExpressions::~NonlinearExpressions(){
00680         #ifdef DEBUG  
00681         cout << "Inside the NonlinearExpressions Destructor" << endl;
00682         cout << "NUMBER OF NONLINEAR EXPRESSIONS = " << numberOfNonlinearExpressions << endl;
00683         #endif
00684         int i;
00685         if(numberOfNonlinearExpressions > 0 && nl != NULL){
00686                 for( i = 0; i < numberOfNonlinearExpressions; i++){
00687                         #ifdef DEBUG  
00688                                 cout << "DESTROYING EXPRESSION " << nl[ i]->idx << endl;
00689                         #endif
00690                         if(nl != NULL){
00691                                 if(nl[i] != NULL){
00692                                         delete nl[i];
00693                                         nl[i] = NULL;
00694                                 }
00695                         }
00696                 }
00697         }
00698         if(nl != NULL){
00699                 delete[] nl;
00700         }
00701         nl = NULL;  
00702 }//end ~NonlinearExpressions()  
00703 
00704 
00705 TimeDomainStageVar::TimeDomainStageVar():
00706         idx(0)
00707 {
00708         #ifdef DEBUG 
00709         cout << "Inside the Stage Objectives Var Constructor" << endl;
00710         #endif
00711 } // end TimeDomainStageVar
00712 
00713 
00714 TimeDomainStageVar::~TimeDomainStageVar()
00715 {
00716         #ifdef DEBUG 
00717         cout << "Inside the Stage Objectives Var Destructor" << endl;
00718         #endif
00719 } // end ~TimeDomainStageVar
00720 
00721 
00722 TimeDomainStageVariables::TimeDomainStageVariables():
00723         numberOfVariables(0),
00724         startIdx(-1)
00725 {
00726         #ifdef DEBUG 
00727         cout << "Inside the Stage Variables Constructor" << endl;
00728         #endif
00729         var = NULL;
00730 } // end TimeDomainStageVariables
00731 
00732 TimeDomainStageVariables::~TimeDomainStageVariables()
00733 {
00734         #ifdef DEBUG 
00735         cout << "Inside the Stage Variables Destructor" << endl;
00736         #endif
00737         if (numberOfVariables > 0 && var != NULL){
00738                 for (int i = 0; i < numberOfVariables; i++) {
00739                         delete var[i];
00740                         var[i] = NULL;
00741                 }
00742         }
00743         if (var != NULL)
00744                 delete [] var;
00745         var = NULL;
00746 } // end ~TimeDomainStageVariables
00747 
00748 TimeDomainStageCon::TimeDomainStageCon():
00749         idx(0)
00750 {
00751         #ifdef DEBUG 
00752         cout << "Inside the Stage Objectives Con Constructor" << endl;
00753         #endif
00754 } // end TimeDomainStageCon
00755 
00756 
00757 TimeDomainStageCon::~TimeDomainStageCon()
00758 {
00759         #ifdef DEBUG 
00760         cout << "Inside the Stage Objectives Con Destructor" << endl;
00761         #endif
00762 } // end ~TimeDomainStageCon
00763 
00764 
00765 TimeDomainStageConstraints::TimeDomainStageConstraints():
00766         numberOfConstraints(0),
00767         startIdx(-1)
00768 {
00769         #ifdef DEBUG 
00770         cout << "Inside the Stage Constraints Constructor" << endl;
00771         #endif
00772         con = NULL;
00773 } // end TimeDomainStageConstraints
00774 
00775 TimeDomainStageConstraints::~TimeDomainStageConstraints()
00776 {
00777         #ifdef DEBUG 
00778         cout << "Inside the Stage Constraints Destructor" << endl;
00779         #endif
00780         if (numberOfConstraints > 0 && con != NULL){
00781                 for (int i = 0; i < numberOfConstraints; i++) {
00782                         delete con[i];
00783                         con[i] = NULL;
00784                 }
00785         }
00786         if (con != NULL)
00787                 delete [] con;
00788         con = NULL;
00789 } // end ~TimeDomainStageConstraints
00790 
00791 TimeDomainStageObj::TimeDomainStageObj():
00792         idx(0)
00793 {
00794         #ifdef DEBUG 
00795         cout << "Inside the Stage Objectives Obj Constructor" << endl;
00796         #endif
00797 } // end TimeDomainStageObj
00798 
00799 
00800 TimeDomainStageObj::~TimeDomainStageObj()
00801 {
00802         #ifdef DEBUG 
00803         cout << "Inside the Stage Objectives Obj Destructor" << endl;
00804         #endif
00805 } // end ~TimeDomainStageObj
00806 
00807 
00808 TimeDomainStageObjectives::TimeDomainStageObjectives():
00809         numberOfObjectives(0),
00810         startIdx(-1)
00811 {
00812         #ifdef DEBUG 
00813         cout << "Inside the Stage Objectives Constructor" << endl;
00814         #endif
00815         obj = NULL;
00816 } // end TimeDomainStageObjectives
00817 
00818 TimeDomainStageObjectives::~TimeDomainStageObjectives()
00819 {
00820         #ifdef DEBUG 
00821         cout << "Inside the Stage Objectives Destructor" << endl;
00822         #endif
00823         if (numberOfObjectives > 0 && obj != NULL){
00824                 for (int i = 0; i < numberOfObjectives; i++) {
00825                         delete obj[i];
00826                         obj[i] = NULL;
00827                 }
00828         }
00829         if (obj != NULL)
00830                 delete [] obj;
00831         obj = NULL;
00832 } // end ~TimeDomainStageObjectives
00833 
00834 TimeDomainStage::TimeDomainStage():
00835         name("")
00836 { 
00837         #ifdef DEBUG 
00838         cout << "Inside the Stage Constructor" << endl;
00839         #endif
00840         variables   = NULL;
00841         constraints = NULL;
00842         objectives  = NULL;
00843 }//end TimeDomainStage() 
00844 
00845 
00846 TimeDomainStage::~TimeDomainStage(){
00847         #ifdef DEBUG  
00848         cout << "Inside the Stage Destructor" << endl;
00849         #endif
00850         if (variables != NULL)
00851         {       delete variables;
00852                 variables = NULL;
00853         }
00854         if (constraints != NULL)
00855         {       delete constraints;
00856                 constraints = NULL;
00857         }
00858         if (objectives != NULL)
00859         {       delete objectives;
00860                 objectives = NULL;
00861         }
00862 }//end ~TimeDomainStage()  
00863 
00864 
00865 TimeDomainStages::TimeDomainStages():
00866         numberOfStages(0),
00867         stage(NULL)
00868 {
00869         #ifdef DEBUG  
00870         cout << "Inside the Stages Constructor" << endl;
00871         #endif
00872 } 
00873 
00874 
00875 TimeDomainStages::~TimeDomainStages(){  
00876         #ifdef DEBUG
00877         cout << "Inside the Stages Destructor" << endl;
00878         #endif
00879         int i;
00880         if(numberOfStages > 0 && stage != NULL){
00881                 for( i = 0; i < numberOfStages; i++){
00882                         delete stage[i];
00883                         stage[i] = NULL;
00884                 }
00885         }
00886         if (stage != NULL)
00887                 delete[] stage;
00888         stage = NULL;  
00889 }
00890 
00891 TimeDomainInterval::TimeDomainInterval():
00892         start(0.0),
00893         horizon(0.0)
00894 {
00895         #ifdef DEBUG  
00896         cout << "Inside the Interval Constructor" << endl;
00897         #endif
00898 } 
00899 
00900 
00901 TimeDomainInterval::~TimeDomainInterval(){  
00902         #ifdef DEBUG
00903         cout << "Inside the Interval Destructor" << endl;
00904         #endif
00905 }
00906 
00907 TimeDomain::TimeDomain()
00908 {
00909         #ifdef DEBUG
00910         cout << "Inside the TimeDomain Constructor" << endl;
00911         #endif
00912         stages = NULL;
00913         interval = NULL;
00914 }
00915 
00916 TimeDomain::~TimeDomain()
00917 {  
00918         #ifdef DEBUG
00919         cout << "Inside the TimeDomain Destructor" << endl;
00920         #endif
00921         if (stages != NULL)
00922         {       delete stages;
00923                 stages = NULL;
00924         }
00925         if (interval != NULL)
00926         {       delete interval;
00927                 interval = NULL;
00928         }
00929 } 
00930 
00931 
00932 InstanceData::InstanceData(){ 
00933         #ifdef DEBUG 
00934         cout << "Inside the InstanceData Constructor" << endl;
00935         #endif 
00936         variables = new Variables();
00937         objectives = new Objectives();
00938         constraints = new Constraints();
00939         linearConstraintCoefficients = new LinearConstraintCoefficients();
00940         quadraticCoefficients = new QuadraticCoefficients();
00941         nonlinearExpressions = new NonlinearExpressions();
00942         timeDomain = NULL;
00943 } 
00944 
00945 InstanceData::~InstanceData(){  
00946         #ifdef DEBUG
00947         cout << "Inside the InstanceData Destructor" << endl; 
00948         #endif
00949         if (variables != NULL)
00950         {
00951                 delete variables;
00952                 variables = NULL;
00953         }
00954 
00955         if (objectives != NULL)
00956         {
00957                 delete objectives;
00958                 objectives = NULL;
00959         }
00960 
00961         if (constraints != NULL)
00962         {
00963                 delete constraints;
00964                 constraints = NULL;
00965         }
00966 
00967         if (linearConstraintCoefficients != NULL)
00968         {
00969                 delete linearConstraintCoefficients;
00970                 linearConstraintCoefficients = NULL;
00971         }
00972 
00973         if (quadraticCoefficients != NULL)
00974         {
00975                 delete quadraticCoefficients;
00976                 quadraticCoefficients = NULL;
00977         }
00978 
00979         if (nonlinearExpressions != NULL)
00980         {
00981                 delete nonlinearExpressions;
00982                 nonlinearExpressions = NULL;
00983         }
00984 
00985         if (timeDomain != NULL)
00986         {
00987                 delete timeDomain;
00988                 timeDomain = NULL;
00989         }
00990 } 
00991 
00992 string OSInstance::getInstanceName(){
00993         if (m_sInstanceName.length() <= 0){
00994                 if (instanceHeader == NULL) 
00995                         throw ErrorClass("instanceHeader object undefined in method getInstanceName()");
00996                 m_sInstanceName = instanceHeader->name;
00997         }
00998         return m_sInstanceName;
00999 }//getInstanceName
01000 
01001 string OSInstance::getInstanceSource(){
01002         if (m_sInstanceSource.length() <= 0){
01003                 if (instanceHeader == NULL) 
01004                         throw ErrorClass("instanceHeader object undefined in method getInstanceSource()");
01005                 m_sInstanceSource = instanceHeader->source;
01006         }
01007         return m_sInstanceSource;
01008 }//getInstanceSource
01009 
01010 string OSInstance::getInstanceDescription(){
01011         if (m_sInstanceDescription.length() <= 0){
01012                 if (instanceHeader == NULL) 
01013                         throw ErrorClass("instanceHeader object undefined in method getInstanceDescription()");
01014                 m_sInstanceDescription = instanceHeader->description;
01015         }
01016         return m_sInstanceDescription;
01017 }//getInstanceDescription
01018 
01019 int OSInstance::getVariableNumber(){
01020         if (m_iVariableNumber == -1){
01021                 if (instanceData == NULL || instanceData->variables == NULL)
01022                         throw ErrorClass("data object undefined in method getVariableNumber()");
01023                 m_iVariableNumber = instanceData->variables->numberOfVariables;
01024         }
01025         return m_iVariableNumber;
01026 }//getVariableNumber
01027 
01028 bool OSInstance::processVariables() {
01029         if(m_bProcessVariables == true && bVariablesModified == false) return true;
01030         //m_bProcessVariables = true;
01031         int i = 0;
01032         int varType;
01033         int n = getVariableNumber();
01034         try{
01035                 m_iNumberOfBinaryVariables = 0;
01036                 m_iNumberOfIntegerVariables = 0;
01037                 m_iNumberOfStringVariables = 0;
01038                 if(n > 0){
01039                         if(m_bProcessVariables != true ){
01040                                 m_mcVariableTypes = new char[n];
01041                                 m_mdVariableLowerBounds = new double[n];
01042                                 m_mdVariableUpperBounds = new double[n];
01043                                 m_msVariableNames = new string[n];
01044                                 m_bProcessVariables = true;
01045                         }
01046 
01047                         for(i = 0; i < n; i++){
01048                                 if(instanceData->variables->var[i] == NULL) throw ErrorClass("processVariables(): var element was never defined");
01049                                 varType = returnVarType(instanceData->variables->var[i]->type);
01050                                 switch (varType)
01051                                 {
01052                                 case 0: 
01053                                         {
01054                                         throw ErrorClass("unknown variable type");
01055                                         break;
01056                                         }
01057                                 case ENUM_VARTYPE_CONTINUOUS: 
01058                                         {
01059                                         break;
01060                                         }
01061                                 case ENUM_VARTYPE_BINARY: 
01062                                         {
01063                                         m_iNumberOfBinaryVariables++;
01064                                         break;
01065                                         }
01066                                 case ENUM_VARTYPE_INTEGER: 
01067                                         {
01068                                         m_iNumberOfIntegerVariables++;
01069                                         break;
01070                                         }
01071                                 case ENUM_VARTYPE_STRING: 
01072                                         {
01073                                         m_iNumberOfStringVariables++;
01074                                         break;
01075                                         }
01076                                 case ENUM_VARTYPE_SEMICONTINUOUS: 
01077                                         {
01078                                         m_iNumberOfSemiContinuousVariables++;
01079                                         break;
01080                                         }
01081                                 case ENUM_VARTYPE_SEMIINTEGER: 
01082                                         {
01083                                         m_iNumberOfSemiIntegerVariables++;
01084                                         break;
01085                                         }
01086                                 default: 
01087                                         {
01088                                         throw ErrorClass("variable type not yet implemented");
01089                                         break;
01090                                         }
01091                                 }
01092                                 m_mcVariableTypes[i] = instanceData->variables->var[i]->type;
01093                                 m_mdVariableLowerBounds[i] = instanceData->variables->var[i]->lb;
01094                                 m_mdVariableUpperBounds[i] = instanceData->variables->var[i]->ub;
01095                                 if(instanceData->variables->var[i]->name.length() > 0)
01096                                         m_msVariableNames[i] = instanceData->variables->var[i]->name;
01097                                 else
01098                                         m_msVariableNames[i] = "";
01099                         }
01100                 }
01101                 return true;
01102         } //end try
01103         catch(const ErrorClass& eclass){
01104                 throw ErrorClass( eclass.errormsg);
01105         } 
01106 }//processVariables
01107         
01108 string* OSInstance::getVariableNames() {
01109         processVariables();
01110         return m_msVariableNames;
01111 }//getVariableNames     
01112 
01113 char* OSInstance::getVariableTypes() {
01114         processVariables();
01115         return m_mcVariableTypes;
01116 }//getVariableTypes
01117 
01118 int OSInstance::getNumberOfBinaryVariables() {
01119         processVariables();
01120         return m_iNumberOfBinaryVariables;
01121 }//getNumberOfBinaryVariables
01122 
01123 int OSInstance::getNumberOfIntegerVariables() {
01124         processVariables();
01125         return m_iNumberOfIntegerVariables;
01126 }//getNumberOfIntegerVariables
01127 
01128 int OSInstance::getNumberOfSemiContinuousVariables() {
01129         processVariables();
01130         return m_iNumberOfSemiContinuousVariables;
01131 }//getNumberOfSemiContinuousVariables
01132 
01133 int OSInstance::getNumberOfSemiIntegerVariables() {
01134         processVariables();
01135         return m_iNumberOfSemiIntegerVariables;
01136 }//getNumberOfSemiIntegerVariables
01137 
01138 int OSInstance::getNumberOfStringVariables() {
01139         processVariables();
01140         return m_iNumberOfStringVariables;
01141 }//getNumberOfStringVariables
01142 
01143 double* OSInstance::getVariableLowerBounds() {
01144         processVariables();
01145         return m_mdVariableLowerBounds;
01146 }//getVariableLowerBounds
01147 
01148 double* OSInstance::getVariableUpperBounds() {
01149         processVariables();
01150         return m_mdVariableUpperBounds;
01151 }//getVariableUpperBounds
01152 
01153 
01154 int OSInstance::getObjectiveNumber(){
01155         if (m_iObjectiveNumber == -1){
01156                 if (instanceData == NULL || instanceData->objectives == NULL)
01157                         throw ErrorClass("data object undefined in method getObjectiveNumber()");
01158                 m_iObjectiveNumber = instanceData->objectives->numberOfObjectives;
01159         }
01160         return m_iObjectiveNumber;
01161 }//getObjectiveNumber
01162 
01163 bool OSInstance::processObjectives() {
01164         if(m_bProcessObjectives == true && bObjectivesModified == false) return true;
01165         //m_bProcessObjectives = true;
01166         int i = 0;
01167         int j = 0;
01168         int n = getObjectiveNumber();
01169         if (n == 0 || instanceData->objectives->obj == NULL) return true;
01170         try{
01171                 if (n > 0){
01172                         if(m_bProcessObjectives != true){
01173                                 m_msMaxOrMins = new string[n];
01174                                 m_miNumberOfObjCoef = new int[n];
01175                                 m_mdObjectiveConstants = new double[n];
01176                                 m_mdObjectiveWeights = new double[n];
01177                                 m_mObjectiveCoefficients = new SparseVector*[n];
01178                                 m_msObjectiveNames = new string[n];
01179                                 for(i = 0; i < n; i++){
01180                                         if(instanceData->objectives->obj[i] == NULL) throw ErrorClass("processObjectives(): obj element was never defined");
01181                                         m_mObjectiveCoefficients[i] = new SparseVector(instanceData->objectives->obj[i]->numberOfObjCoef);
01182                                         //m_mObjectiveCoefficients[i]->bDeleteArrays=false;
01183                                 }
01184                                 m_bProcessObjectives = true;
01185                         }
01186 
01187                         for(i = 0; i < n; i++){
01188                                 if(instanceData->objectives->obj[i] == NULL) throw ErrorClass("processObjectives(): obj element was never defined");
01189                                 if((instanceData->objectives->obj[i]->maxOrMin.compare("max") != 0) && (instanceData->objectives->obj[i]->maxOrMin.compare("min") != 0 )) throw ErrorClass("wrong objective maxOrMin");
01190                                 m_msMaxOrMins[i] = instanceData->objectives->obj[i]->maxOrMin;
01191                                 m_miNumberOfObjCoef[i] = instanceData->objectives->obj[i]->numberOfObjCoef;
01192                                 m_mdObjectiveConstants[i] = instanceData->objectives->obj[i]->constant;
01193                                 m_mdObjectiveWeights[i] = instanceData->objectives->obj[i]->weight;
01194                                 if(instanceData->objectives->obj[i]->coef == NULL && m_miNumberOfObjCoef[i] != 0){
01195                                         throw ErrorClass("objective coefficient number inconsistent with objective coefficient array");
01196                                 }
01197                                 if(instanceData->objectives->obj[i]->coef != NULL){
01198                                         for(j = 0; j < m_mObjectiveCoefficients[i]->number; j++){
01199                                                 m_mObjectiveCoefficients[i]->indexes[j] = instanceData->objectives->obj[i]->coef[j]->idx;
01200                                                 m_mObjectiveCoefficients[i]->values[j] = instanceData->objectives->obj[i]->coef[j]->value;                      
01201                                         }
01202                                 }
01203                                 if(instanceData->objectives->obj[i]->name.length() > 0)
01204                                         m_msObjectiveNames[i] = instanceData->objectives->obj[i]->name;
01205                                 else
01206                                         m_msObjectiveNames[i] = "";
01207                         }       
01208                 }
01209                 return true;
01210         } //end try
01211         catch(const ErrorClass& eclass){
01212                 throw ErrorClass( eclass.errormsg);
01213         }
01214 }//processObjectives
01215 
01216 string* OSInstance::getObjectiveNames() {
01217         processObjectives();
01218         return m_msObjectiveNames;
01219 }//getObjectiveNames
01220 
01221 string* OSInstance::getObjectiveMaxOrMins() {
01222         processObjectives();
01223         return m_msMaxOrMins;
01224 }//getObjectiveMaxOrMins
01225 
01226 int* OSInstance::getObjectiveCoefficientNumbers(){
01227         processObjectives();
01228         return m_miNumberOfObjCoef;
01229 }//getObjectiveCoefficientNumbers
01230 
01231 double* OSInstance::getObjectiveConstants() {
01232         processObjectives();
01233         return m_mdObjectiveConstants;
01234 }//getObjectiveConstants
01235 
01236 double* OSInstance::getObjectiveWeights() {
01237         processObjectives();
01238         return m_mdObjectiveWeights;
01239 }//getObjectiveWeights
01240 
01241 SparseVector** OSInstance::getObjectiveCoefficients() {
01242         processObjectives();
01243         return m_mObjectiveCoefficients;
01244 }//getObjectiveCoefficients
01245 
01246 
01247 double** OSInstance::getDenseObjectiveCoefficients() {
01248         if(m_bGetDenseObjectives == true && bObjectivesModified == false) return m_mmdDenseObjectiveCoefficients;
01249         int i, j, numobjcoef;
01250         SparseVector *sparsevec;
01251         int m = getObjectiveNumber();
01252         int n = getVariableNumber();
01253         if (m == 0 || instanceData->objectives->obj == NULL) return NULL;
01254         if (m_bGetDenseObjectives != true){
01255                 m_mmdDenseObjectiveCoefficients = new double*[m];
01256                 for(i = 0; i < m; i++){
01257                         m_mmdDenseObjectiveCoefficients[ i] = new double[n];
01258                 }
01259                 m_bGetDenseObjectives = true;
01260         }
01261 
01262         for(i = 0; i < m; i++){
01263                 sparsevec = this->getObjectiveCoefficients()[i];
01264                 for(j = 0; j < n; j++){
01265                         m_mmdDenseObjectiveCoefficients[ i][j] = 0.0;
01266                 }
01267                 sparsevec =  this->getObjectiveCoefficients()[i];
01268                 numobjcoef = sparsevec->number;
01269                 for(j = 0; j < numobjcoef; j++){
01270                         m_mmdDenseObjectiveCoefficients[i][ sparsevec->indexes[ j]]
01271                         = sparsevec->values[ j];
01272                 }
01273         }
01274         return m_mmdDenseObjectiveCoefficients;
01275 }//getDenseObjectiveCoefficients
01276 
01277 
01278 int OSInstance::getConstraintNumber(){
01279         if (m_iConstraintNumber == -1){
01280                 if (instanceData == NULL || instanceData->constraints == NULL)
01281                         throw ErrorClass("data object undefined in method getConstraintNumber()");
01282                 m_iConstraintNumber = instanceData->constraints->numberOfConstraints;
01283         }
01284         return m_iConstraintNumber;
01285 }//getConstraintNumber
01286 
01287 bool OSInstance::processConstraints() {
01288         if(m_bProcessConstraints == true && bConstraintsModified == false) return true;
01289         //m_bProcessConstraints = true;
01290         int i = 0;
01291         ostringstream outStr;
01292         int n = getConstraintNumber();
01293         if (n == 0 || instanceData->constraints->con == NULL) return true;
01294         try{
01295                 if(n > 0){
01296                         if(m_bProcessConstraints != true){
01297                                 m_mdConstraintLowerBounds = new double[n];
01298                                 m_mdConstraintUpperBounds = new double[n];
01299                                 m_mdConstraintConstants = new double[n];
01300                                 m_mcConstraintTypes = new char[n];
01301                                 m_msConstraintNames = new string[n];
01302                                 m_bProcessConstraints = true;
01303                         }
01304                         for(i = 0; i < n; i++){
01305                                 if(instanceData->constraints->con[i] == NULL) throw ErrorClass("processConstraints(): con element was never defined");
01306                                 m_mdConstraintLowerBounds[i] = instanceData->constraints->con[i]->lb;
01307                                 m_mdConstraintUpperBounds[i] = instanceData->constraints->con[i]->ub;
01308                                 m_mdConstraintConstants[i] = instanceData->constraints->con[i]->constant;
01309                                 if(m_mdConstraintLowerBounds[i] == OSDBL_MAX || m_mdConstraintUpperBounds[i] == -OSDBL_MAX) {
01310                                         outStr << "Constraint  " ;
01311                                         outStr << i;
01312                                         outStr << " is infeasible";
01313                                         throw ErrorClass( outStr.str() );
01314                                 }
01315                                 else if(m_mdConstraintLowerBounds[i] > m_mdConstraintUpperBounds[i]) {
01316                                         outStr << "Constraint  " ;
01317                                         outStr << i;
01318                                         outStr << " is infeasible";
01319                                         throw ErrorClass( outStr.str());
01320                                 }
01321                                 else if(m_mdConstraintLowerBounds[i] == -OSDBL_MAX && m_mdConstraintUpperBounds[i] == OSDBL_MAX)
01322                                         m_mcConstraintTypes[i] = 'U';
01323                                 else if(m_mdConstraintLowerBounds[i] == m_mdConstraintUpperBounds[i]) 
01324                                         m_mcConstraintTypes[i] = 'E';
01325                                 else if(m_mdConstraintLowerBounds[i] == -OSDBL_MAX)
01326                                         m_mcConstraintTypes[i] = 'L';
01327                                 else if(m_mdConstraintUpperBounds[i] == OSDBL_MAX)
01328                                         m_mcConstraintTypes[i] = 'G';
01329                                 else m_mcConstraintTypes[i] = 'R';
01330                                 if(instanceData->constraints->con[i]->name.length() > 0)
01331                                         m_msConstraintNames[i] = instanceData->constraints->con[i]->name;
01332                                 else
01333                                         m_msConstraintNames[i] = "";
01334                         }
01335                 }
01336                 return true;
01337         }
01338         catch(const ErrorClass& eclass){
01339                 throw ErrorClass( eclass.errormsg);
01340         }
01341 }//processConstraints
01342 
01343 
01344 string* OSInstance::getConstraintNames() {
01345         processConstraints();
01346         return m_msConstraintNames;
01347 }//getConstraintNames
01348 
01349 
01350 double* OSInstance::getConstraintLowerBounds() {
01351         processConstraints();
01352         return m_mdConstraintLowerBounds;
01353 }//getConstraintLowerBounds
01354 
01355 char* OSInstance::getConstraintTypes() {
01356         processConstraints();
01357         return m_mcConstraintTypes;
01358 }//getConstraintTypes
01359 
01360 double* OSInstance::getConstraintUpperBounds() {
01361         processConstraints();
01362         return m_mdConstraintUpperBounds;
01363 }//getConstraintUpperBounds
01364 
01365 
01366 int OSInstance::getLinearConstraintCoefficientNumber(){
01367         if(this->getVariableNumber() <= 0 || this->getConstraintNumber() <= 0) return 0;
01368         if(m_iLinearConstraintCoefficientNumber == -1){
01369                 if (instanceData == NULL || instanceData->linearConstraintCoefficients == NULL)
01370                         throw ErrorClass("data object undefined in method getLinearConstraintCoefficientNumber()");
01371                 m_iLinearConstraintCoefficientNumber = instanceData->linearConstraintCoefficients->numberOfValues;
01372         }
01373         return m_iLinearConstraintCoefficientNumber; 
01374 }//getLinearConstraintCoefficientNumber
01375 
01376 bool OSInstance::processLinearConstraintCoefficients() {
01377         if(m_bProcessLinearConstraintCoefficients == true && bAMatrixModified == false) return true;
01378         //m_bProcessLinearConstraintCoefficients = true;
01379         try{
01380                 int n = getLinearConstraintCoefficientNumber();
01381                 if((instanceData->linearConstraintCoefficients == NULL ) || (n == 0) ) return true;
01382                 //value array
01383                 if((instanceData->linearConstraintCoefficients->value == NULL ) || (n == 0) ) return true;
01384                 //index array
01385                 if((instanceData->linearConstraintCoefficients->colIdx != NULL && instanceData->linearConstraintCoefficients->colIdx->el != NULL) 
01386                 && (instanceData->linearConstraintCoefficients->rowIdx != NULL && instanceData->linearConstraintCoefficients->rowIdx->el != NULL))
01387                         throw ErrorClass("ambiguous linear constraint coefficient major");
01388                 else if(instanceData->linearConstraintCoefficients->value->el == NULL) return true;
01389                 else{
01390                         if(instanceData->linearConstraintCoefficients->rowIdx->el != NULL){
01391                                 m_bColumnMajor = true;
01392                                 if(m_bProcessLinearConstraintCoefficients != true){
01393                                         m_linearConstraintCoefficientsInColumnMajor = new SparseMatrix();
01394                                         m_linearConstraintCoefficientsInColumnMajor->bDeleteArrays = false;
01395                                         m_bProcessLinearConstraintCoefficients = true;
01396                                 }
01397                                 m_linearConstraintCoefficientsInColumnMajor->isColumnMajor = true;
01398                                 m_linearConstraintCoefficientsInColumnMajor->valueSize = n;
01399                                 m_linearConstraintCoefficientsInColumnMajor->startSize = instanceData->variables->numberOfVariables + 1;
01400                         }
01401                         else{ 
01402                                 m_bColumnMajor = false; 
01403                                 if(m_bProcessLinearConstraintCoefficients != true){
01404                                         m_linearConstraintCoefficientsInRowMajor = new SparseMatrix();
01405                                         m_linearConstraintCoefficientsInRowMajor->bDeleteArrays = false;
01406                                         m_bProcessLinearConstraintCoefficients = true;
01407                                 }
01408                                 m_linearConstraintCoefficientsInRowMajor->isColumnMajor = false;
01409                                 m_linearConstraintCoefficientsInRowMajor->valueSize = n;
01410                                 m_linearConstraintCoefficientsInRowMajor->startSize = instanceData->constraints->numberOfConstraints + 1;
01411                         }
01412                 }                       
01413                 if(m_bColumnMajor == true){
01414                         m_linearConstraintCoefficientsInColumnMajor->values = instanceData->linearConstraintCoefficients->value->el;
01415                         m_linearConstraintCoefficientsInColumnMajor->indexes = instanceData->linearConstraintCoefficients->rowIdx->el;
01416                         m_linearConstraintCoefficientsInColumnMajor->starts = instanceData->linearConstraintCoefficients->start->el;                    
01417                 }
01418                 else{
01419                         m_linearConstraintCoefficientsInRowMajor->values = instanceData->linearConstraintCoefficients->value->el;
01420                         m_linearConstraintCoefficientsInRowMajor->indexes = instanceData->linearConstraintCoefficients->colIdx->el;
01421                         m_linearConstraintCoefficientsInRowMajor->starts = instanceData->linearConstraintCoefficients->start->el;                                               
01422                 }
01423                 return true;
01424         }
01425         catch(const ErrorClass& eclass){
01426                 throw ErrorClass( eclass.errormsg);
01427         }
01428 }//processLinearConstraintCoefficients
01429 
01430 bool OSInstance::getLinearConstraintCoefficientMajor() {
01431         processLinearConstraintCoefficients();  
01432         return m_bColumnMajor;          
01433 }//getLinearConstraintCoefficientMajor
01434 
01435 SparseMatrix* OSInstance::getLinearConstraintCoefficientsInColumnMajor() {
01436         processLinearConstraintCoefficients();
01437         if(getVariableNumber() ==  0) return NULL;
01438         if(m_linearConstraintCoefficientsInColumnMajor != NULL) return m_linearConstraintCoefficientsInColumnMajor;
01439         if(!m_bColumnMajor){
01440                 if(m_linearConstraintCoefficientsInRowMajor == NULL) return NULL;
01441                 m_linearConstraintCoefficientsInColumnMajor = 
01442                         MathUtil::convertLinearConstraintCoefficientMatrixToTheOtherMajor(false,
01443                                           m_linearConstraintCoefficientsInRowMajor->startSize,
01444                                           m_linearConstraintCoefficientsInRowMajor->valueSize,
01445                                           m_linearConstraintCoefficientsInRowMajor->starts,
01446                                           m_linearConstraintCoefficientsInRowMajor->indexes,
01447                                           m_linearConstraintCoefficientsInRowMajor->values,
01448                                           getVariableNumber());
01449         }
01450         return m_linearConstraintCoefficientsInColumnMajor;             
01451 }//getLinearConstraintCoefficientsInColumnMajor
01452 
01453 SparseMatrix* OSInstance::getLinearConstraintCoefficientsInRowMajor() {
01454         processLinearConstraintCoefficients();
01455         if(m_linearConstraintCoefficientsInRowMajor != NULL) return m_linearConstraintCoefficientsInRowMajor;
01456         if(m_bColumnMajor){
01457                 if(m_linearConstraintCoefficientsInColumnMajor == NULL) return NULL;
01458                 m_linearConstraintCoefficientsInRowMajor = 
01459                 MathUtil::convertLinearConstraintCoefficientMatrixToTheOtherMajor(true,
01460                                   m_linearConstraintCoefficientsInColumnMajor->startSize,
01461                                   m_linearConstraintCoefficientsInColumnMajor->valueSize,
01462                                   m_linearConstraintCoefficientsInColumnMajor->starts,
01463                                   m_linearConstraintCoefficientsInColumnMajor->indexes,
01464                                   m_linearConstraintCoefficientsInColumnMajor->values,
01465                                   getConstraintNumber());
01466         }
01467         return m_linearConstraintCoefficientsInRowMajor; 
01468 }//getLinearConstraintCoefficientsInRowMajor
01469 
01470 
01471 int OSInstance::getNumberOfQuadraticTerms(){
01472         if(m_iQuadraticTermNumber == -1){
01473                 if (instanceData == NULL || instanceData->quadraticCoefficients == NULL)
01474                         throw ErrorClass("data object undefined in method getNumberOfQuadraticTerms()");
01475                 m_iQuadraticTermNumber = instanceData->quadraticCoefficients->numberOfQuadraticTerms;
01476         }
01477         return m_iQuadraticTermNumber;
01478 }//getNumberOfQuadraticTerms
01479 
01480 QuadraticTerms* OSInstance::getQuadraticTerms() {
01481         if(m_bProcessQuadraticTerms) return m_quadraticTerms;
01482         m_bProcessQuadraticTerms = true;
01483         int n = getNumberOfQuadraticTerms();
01484         if(instanceData->quadraticCoefficients->qTerm == NULL) return NULL;
01485         try{
01486                 int i = 0;
01487                 QuadraticCoefficients* quadraticCoefs = instanceData->quadraticCoefficients;
01488                         if(!quadraticCoefs->qTerm  && n != 0) 
01489                                         throw ErrorClass("quadratic term number inconsistent with quadratic term array");
01490                         m_quadraticTerms = new QuadraticTerms();
01491                         if(n > 0){
01492                                 m_quadraticTerms->rowIndexes = new int[n];
01493                                 m_quadraticTerms->varOneIndexes = new int[n];
01494                                 m_quadraticTerms->varTwoIndexes = new int[n];
01495                                 m_quadraticTerms->coefficients = new double[n];
01496                         }
01497                         for(i = 0; i < n; i++){
01498                                 m_quadraticTerms->rowIndexes[i]    = quadraticCoefs->qTerm[i]->idx;
01499                                 m_quadraticTerms->varOneIndexes[i] = quadraticCoefs->qTerm[i]->idxOne;
01500                                 m_quadraticTerms->varTwoIndexes[i] = quadraticCoefs->qTerm[i]->idxTwo;
01501                                 m_quadraticTerms->coefficients[i]  = quadraticCoefs->qTerm[i]->coef;
01502                         }
01503                 return m_quadraticTerms;
01504         }
01505         catch(const ErrorClass& eclass){
01506                 throw ErrorClass( eclass.errormsg);
01507         } 
01508 }//getQuadraticTerms
01509 
01510 int OSInstance::getNumberOfQuadraticRowIndexes() {
01511         if(m_bQuadraticRowIndexesProcessed == false) getQuadraticRowIndexes();
01512         return m_iNumberOfQuadraticRowIndexes;
01513 }//getNumberOfQuadraticRowIndexes
01514 
01515 int* OSInstance::getQuadraticRowIndexes() {
01516         if(m_bQuadraticRowIndexesProcessed == true) return m_miQuadRowIndexes;
01517         m_bQuadraticRowIndexesProcessed = true;
01518         int n = getNumberOfQuadraticTerms();    
01519         if(n <= 0) return NULL;
01520         QuadraticTerms *qTerms = NULL;
01521         qTerms = getQuadraticTerms();
01522         std::map<int, int> foundIdx;
01523         std::map<int, int>::iterator pos;
01524         int i;
01525         try{
01526                 for(i = 0; i < n; i++){
01527                         // add the terms
01528                         foundIdx[ qTerms->rowIndexes[ i] ];      
01529                 }
01530                 // now put the term into an array
01531                 m_iNumberOfQuadraticRowIndexes = foundIdx.size();
01532                 m_miQuadRowIndexes = new int[ m_iNumberOfQuadraticRowIndexes ]  ;
01533                 i = 0;
01534                 for(pos = foundIdx.begin(); pos != foundIdx.end(); ++pos){
01535                         m_miQuadRowIndexes[ i++] = pos->first;  
01536                 }
01537                 foundIdx.clear();       
01538                 return m_miQuadRowIndexes;
01539         }
01540         catch(const ErrorClass& eclass){
01541                 throw ErrorClass( eclass.errormsg);
01542         } 
01543 }//getQuadraticRowIndexes
01544 
01545 
01546 int OSInstance::getNumberOfNonlinearExpressions(){
01547         if(m_iNonlinearExpressionNumber == -1){
01548                 if (instanceData == NULL || instanceData->nonlinearExpressions == NULL)
01549                         throw ErrorClass("data object undefined in method getNumberOfNonlinearExpressions()");
01550                 m_iNonlinearExpressionNumber = instanceData->nonlinearExpressions->numberOfNonlinearExpressions;
01551         }
01552         return m_iNonlinearExpressionNumber;
01553 }//getNumberOfNonlinearExpressions
01554 
01555 int OSInstance::getNumberOfNonlinearExpressionTreeIndexes() {
01556         if(m_bNonlinearExpressionTreeIndexesProcessed == false) getNonlinearExpressionTreeIndexes();
01557         return m_iNumberOfNonlinearExpressionTreeIndexes;
01558 }//getNumberOfNonlinearExpressionTreeIndexes
01559 
01560 int* OSInstance::getNonlinearExpressionTreeIndexes(){
01561         if(m_bNonlinearExpressionTreeIndexesProcessed == true) return m_miNonlinearExpressionTreeIndexes;
01562         m_bNonlinearExpressionTreeIndexesProcessed = true;
01563         std::map<int, OSExpressionTree*> expTrees;
01564         expTrees = getAllNonlinearExpressionTrees();    
01565         std::map<int, OSExpressionTree*>::iterator pos;
01566         try{
01567                 // now put the term into an array
01568                 m_iNumberOfNonlinearExpressionTreeIndexes = expTrees.size();
01569                 m_miNonlinearExpressionTreeIndexes = new int[ m_iNumberOfNonlinearExpressionTreeIndexes ]       ;
01570                 int i = 0;
01571                 for(pos = expTrees.begin(); pos != expTrees.end(); ++pos){
01572                         m_miNonlinearExpressionTreeIndexes[ i++] = pos->first;  
01573                 }
01574                 expTrees.clear();       
01575                 return m_miNonlinearExpressionTreeIndexes;
01576         }
01577         catch(const ErrorClass& eclass){
01578                 throw ErrorClass( eclass.errormsg);
01579         } 
01580 }//getNonlinearExpressionTreeIndexes
01581 
01582 int OSInstance::getNumberOfNonlinearExpressionTreeModIndexes() {
01583         if(m_bNonlinearExpressionTreeModIndexesProcessed == false) getNonlinearExpressionTreeModIndexes();
01584         return m_iNumberOfNonlinearExpressionTreeModIndexes;
01585 }//getNumberOfNonlinearExpressionTreeModIndexes
01586 
01587 int* OSInstance::getNonlinearExpressionTreeModIndexes(){
01588         if(m_bNonlinearExpressionTreeModIndexesProcessed == true) return m_miNonlinearExpressionTreeModIndexes;
01589         m_bNonlinearExpressionTreeModIndexesProcessed = true;
01590         std::map<int, OSExpressionTree*> expTrees;
01591         expTrees = getAllNonlinearExpressionTreesMod(); 
01592         std::map<int, OSExpressionTree*>::iterator pos;
01593         try{
01594                 // now put the term into an array
01595                 m_iNumberOfNonlinearExpressionTreeModIndexes = expTrees.size();
01596                 m_miNonlinearExpressionTreeModIndexes = new int[ m_iNumberOfNonlinearExpressionTreeModIndexes ] ;
01597                 int i = 0;
01598                 for(pos = expTrees.begin(); pos != expTrees.end(); ++pos){
01599                         m_miNonlinearExpressionTreeModIndexes[ i++] = pos->first;       
01600                 }
01601                 expTrees.clear();       
01602                 return m_miNonlinearExpressionTreeModIndexes;
01603         }
01604         catch(const ErrorClass& eclass){
01605                 throw ErrorClass( eclass.errormsg);
01606         } 
01607 }//getNonlinearExpressionTreeModIndexes
01608 
01609 int OSInstance::getNumberOfNonlinearConstraints(){
01610         if( m_bProcessExpressionTrees == false )
01611                 getAllNonlinearExpressionTrees();
01612         return m_iConstraintNumberNonlinear;
01613 }//getNumberOfNonlinearConstraints
01614 
01615 int OSInstance::getNumberOfNonlinearObjectives(){
01616         if( m_bProcessExpressionTrees == false )
01617                 getAllNonlinearExpressionTrees();
01618         return m_iObjectiveNumberNonlinear;
01619 }//getNumberOfNonlinearObjectivess
01620 
01621 OSExpressionTree* OSInstance::getNonlinearExpressionTree(int rowIdx){
01622         if( m_bProcessExpressionTrees == false ){
01623                 getAllNonlinearExpressionTrees();
01624         } 
01625         if( m_mapExpressionTrees.find( rowIdx) != m_mapExpressionTrees.end()) return m_mapExpressionTrees[ rowIdx];
01626         else return NULL ;
01627         // check to make sure rowIdx has a nonlinear term and is in the map
01629         //std::map<int, OSExpressionTree*>::iterator pos;
01631         //      if(pos->first == rowIdx)return m_mapExpressionTrees[ rowIdx];
01632         //}
01633         // if we are rowIdx has no nonlinear terms so return a null
01634         //return NULL;
01635 }// getNonlinearExpressionTree for a specific index   
01636 
01637 OSExpressionTree* OSInstance::getNonlinearExpressionTreeMod(int rowIdx){
01638         if( m_bProcessExpressionTreesMod == false ){
01639                 getAllNonlinearExpressionTreesMod();
01640         } 
01641         if( m_mapExpressionTreesMod.find( rowIdx) != m_mapExpressionTreesMod.end()) return m_mapExpressionTreesMod[ rowIdx];
01642         else return NULL ;
01643         // check to make sure rowIdx has a nonlinear term and is in the map
01645         //std::map<int, OSExpressionTree*>::iterator pos;
01647         //      if(pos->first == rowIdx)return m_mapExpressionTrees[ rowIdx];
01648         //}
01649         // if we are rowIdx has no nonlinear terms so return a null
01650         //return NULL;
01651 }// getNonlinearExpressionTreeMod for a specific index 
01652 
01653 std::vector<OSnLNode*> OSInstance::getNonlinearExpressionTreeInPostfix( int rowIdx){
01654         //if( m_binitForAlgDiff == false) this->initForAlgDiff();
01655         if( m_bProcessExpressionTrees == false ) getAllNonlinearExpressionTrees();
01656         std::vector<OSnLNode*> postfixVec;
01657         try{
01658                 if( m_mapExpressionTrees.find( rowIdx) != m_mapExpressionTrees.end()){
01659                         OSExpressionTree* expTree = getNonlinearExpressionTree( rowIdx);
01660                         postfixVec = expTree->m_treeRoot->getPostfixFromExpressionTree();
01661                 }  
01662                 else{
01663                         throw ErrorClass("Error in getNonlinearExpressionTreeInPostfix, rowIdx not valid");
01664                 }
01665                 return postfixVec;      
01666         }
01667         catch(const ErrorClass& eclass){
01668                 throw ErrorClass( eclass.errormsg);
01669         } 
01670 }//getNonlinearExpressionTreeInPostfix
01671 
01672 std::string OSInstance::getNonlinearExpressionTreeInInfix( int rowIdx_){
01673         if( m_binitForAlgDiff == false) this->initForAlgDiff();
01674         if( m_bProcessExpressionTrees == false ) getAllNonlinearExpressionTrees();
01675         std::string resultString;
01676         resultString = "";
01677         unsigned int i;
01678         unsigned int j;
01679         unsigned int n;
01680         ostringstream outStr;
01681         std::vector<OSnLNode*> postfixVec;
01682         int rowIdx = rowIdx_;
01683         OSnLNode *nlnode = NULL ;
01684         OSnLNodeNumber *nlnodeNum = NULL;
01685         OSnLNodeVariable *nlnodeVar = NULL;
01686         OSnLNodeSum *nlnodeSum = NULL;
01687         OSnLNodeProduct *nlnodeProduct = NULL;
01688         OSnLNodeMin *nlnodeMin = NULL;
01689         OSnLNodeMax *nlnodeMax = NULL;
01690         std::string tmp1 = "";
01691         std::string tmp2 = "";
01692         std::string tmp3 = "";
01693         std::stack<OSnLNode*> opStack;
01694         std::stack<std::string> tmpStack;
01695         std::stack<std::string> sumStack;
01696         std::stack<std::string> productStack;
01697         std::stack<std::string> minStack;
01698         std::stack<std::string> maxStack;
01699         
01700         try{
01701                 if( m_mapExpressionTrees.find( rowIdx) != m_mapExpressionTrees.end()){
01702                         //get the nodes and separate into operators and operands, for now
01703                         //only the number and variable nodes are operator nodes
01704                         
01705                         OSExpressionTree* exptree = this->getNonlinearExpressionTree( rowIdx);
01706                         if(exptree != NULL) {
01707                                 postfixVec = this->getNonlinearExpressionTreeInPostfix( rowIdx);
01708                                 n  = postfixVec.size();
01709                                 //put vector in reverse order
01710                                 for (i = 0 ; i < n; i++){
01711                                         nlnode =  postfixVec[ n - 1 - i];
01712                                         opStack.push( nlnode);
01713                                         //std::cout << postfixVec[ i]->snodeName << std::endl;
01714                                 }
01715                                 
01716                                 #if 0
01717                                 std::cout << std::endl << std::endl << std::endl << std::endl;
01718                                 n = opStack.size();
01719                                 for(i = 0; i < n; i++){
01720                                         std::cout  << opStack.top()->getTokenName()  << std::endl;
01721                                 }
01722                                 
01723                                 #endif
01724                                 n = opStack.size();
01725                                 for(i = 0; i < n; i++){
01726                                         //std::cout << "NUMBER OF NODES LEFT =  " << operatorVec.size() << std::endl;
01727                                         nlnode = opStack.top();
01728                                         //std::cout << "EVALUATING NODE: " << nlnode->snodeName << std::endl;
01729                                         switch (nlnode->inodeInt) {
01730                                                 case OS_NUMBER:
01731                                                         nlnodeNum = (OSnLNodeNumber*)nlnode;
01732                                                         tmpStack.push( os_dtoa_format(nlnodeNum->value) );
01733                                                         break;
01734                                                         
01735                                                 case OS_PI:
01736                                                         tmpStack.push( "PI" );
01737                                                         break;
01738                                                         
01739                                                 case OS_E:
01740                                                         tmpStack.push( "E" );
01741                                                         break;
01742                                                         
01743                                                 case OS_VARIABLE:
01744                                                         outStr.str("");
01745                                                         // handle a variable
01746                                                         nlnodeVar = (OSnLNodeVariable*)nlnode;
01747                                                         // see if the coefficient is specified
01748                                                         if( (nlnodeVar->coef > 1.0) ||  (nlnodeVar->coef < 1.0) ){
01749                                                                 outStr << "(";
01750                                                                 outStr <<  os_dtoa_format(nlnodeVar->coef);
01751                                                                 outStr << "*x_";
01752                                                                 outStr << nlnodeVar->idx;
01753                                                                 outStr << ")";
01754                                                                 tmpStack.push(outStr.str() );
01755                                                                 //std::cout << "WE JUST PUSHED " << outStr.str() << std::endl;
01756                                                         }else{
01757                                                                 outStr << "x_";
01758                                                                 outStr << nlnodeVar->idx;
01759                                                                 tmpStack.push(outStr.str() );
01760                                                                 //std::cout << "WE JUST PUSHED " << outStr.str() << std::endl;
01761                                                         }
01762                                                         break;
01763                                                         
01764                                                 case OS_PLUS :
01765                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing plus operater");
01766                                                         tmp1 = tmpStack.top();
01767                                                         tmpStack.pop();
01768                                                         tmp2 = tmpStack.top();
01769                                                         tmpStack.pop();
01770                                                         tmpStack.push("(" + tmp2 +  " + "  + tmp1 + ")");
01771                                                         break;
01772                                                         
01773                                                 case OS_SUM :
01774                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing sum operater");
01775                                                         //std::cout << "INSIDE SUM NODE " << std::endl;
01776                                                         nlnodeSum = (OSnLNodeSum*)nlnode;
01777                                                         outStr.str("");
01778                                                         for(j = 0; j < nlnodeSum->inumberOfChildren; j++){
01779                                                                 sumStack.push( tmpStack.top() );
01780                                                                 tmpStack.pop();
01781                                                         }
01782                                                         outStr << "(";
01783                                                         for(j = 0; j < nlnodeSum->inumberOfChildren; j++){
01784                                                                 outStr << sumStack.top();
01785                                                                 if (j < nlnodeSum->inumberOfChildren - 1) outStr << " + ";
01786                                                                 sumStack.pop();
01787                                                         }
01788                                                         outStr << ")";
01789                                                         tmpStack.push( outStr.str() );
01790                                                         //std::cout << outStr.str() << std::endl;
01791                                                         break;
01792                                                         
01793                                                 case OS_MINUS :
01794                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing minus operater");
01795                                                         tmp1 = tmpStack.top();
01796                                                         tmpStack.pop();
01797                                                         tmp2 = tmpStack.top();
01798                                                         tmpStack.pop();
01799                                                         tmpStack.push("(" + tmp2 +  " - "  + tmp1 + ")");
01800                                                         break;                                          
01801                                                 
01802                                                 case OS_NEGATE :
01803                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- -- Problem writing negate operater");
01804                                                         tmp1 = tmpStack.top();
01805                                                         tmpStack.pop();
01806                                                         tmpStack.push( "-"+ tmp1 );
01807                                                         break;
01808                                                         
01809                                                 case OS_TIMES :
01810                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing times operater");
01811                                                         tmp1 = tmpStack.top();
01812                                                         tmpStack.pop();
01813                                                         tmp2 = tmpStack.top();
01814                                                         tmpStack.pop();
01815                                                         tmpStack.push("(" + tmp2 +  "*"  + tmp1 + ")");
01816                                                         break;
01817                                                 
01818                                                 case OS_DIVIDE :
01819                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing divide operater");
01820                                                         tmp1 = tmpStack.top();
01821                                                         tmpStack.pop();
01822                                                         tmp2 = tmpStack.top();
01823                                                         tmpStack.pop();
01824                                                         tmpStack.push("(" + tmp2 +  " / "  + tmp1 + ")");
01825                                                         break;
01826 
01827                                                 case OS_POWER :
01828                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing power operater");
01829                                                         tmp1 = tmpStack.top();
01830                                                         tmpStack.pop();
01831                                                         tmp2 = tmpStack.top();
01832                                                         tmpStack.pop();
01833                                                         tmpStack.push("(" + tmp2 +  " ^ "  + tmp1 + ")");
01834                                                         break;
01835                                                         
01836                                                 case OS_ABS :
01837                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing abs operater");
01838                                                         tmp1 = tmpStack.top();
01839                                                         tmpStack.pop();
01840                                                         tmpStack.push( "abs( "+ tmp1  + ")");
01841                                                         break;
01842                                                         
01843                                                 case OS_ERF :
01844                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing erf operater");
01845                                                         tmp1 = tmpStack.top();
01846                                                         tmpStack.pop();
01847                                                         tmpStack.push( "erf( "+ tmp1  + ")");
01848                                                         break;
01849 
01850                                                 
01851                                                 case OS_SQUARE :
01852                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing square operater ");
01853                                                         tmp1 = tmpStack.top();
01854                                                         tmpStack.pop();
01855                                                         tmpStack.push( "("+ tmp1  + ")^2");
01856                                                         break;
01857                                                                                                         
01858                                                 case OS_LN :
01859                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing ln operater");
01860                                                         tmp1 = tmpStack.top();
01861                                                         tmpStack.pop();
01862                                                         tmpStack.push( "ln( "+ tmp1  + ")");
01863                                                         break;
01864                                                         
01865                                                 case OS_EXP :
01866                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing exp operater");
01867                                                         tmp1 = tmpStack.top();
01868                                                         tmpStack.pop();
01869                                                         tmpStack.push( "exp( "+ tmp1  + ")");
01870                                                         break;
01871                                                         
01872                                                 case OS_SIN :
01873                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing sin operater");
01874                                                         tmp1 = tmpStack.top();
01875                                                         tmpStack.pop();
01876                                                         tmpStack.push( "sin( "+ tmp1  + ")");
01877                                                         break;
01878                                                         
01879                                                 case OS_COS :
01880                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing cos operater ");
01881                                                         tmp1 = tmpStack.top();
01882                                                         tmpStack.pop();
01883                                                         tmpStack.push( "cos( "+ tmp1  + ")");
01884                                                         break;
01885                                                         
01886                                                 case OS_SQRT :
01887                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing cos operater ");
01888                                                         tmp1 = tmpStack.top();
01889                                                         tmpStack.pop();
01890                                                         tmpStack.push( "sqrt( "+ tmp1  + ")");
01891                                                         break;
01892                                                         
01893                                                 case OS_MIN :
01894                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing min operater");
01895                                                         //std::cout << "INSIDE Min NODE " << std::endl;
01896                                                         nlnodeMin = (OSnLNodeMin*)nlnode;
01897                                                         outStr.str("");
01898                                                         for(j = 0; j < nlnodeMin->inumberOfChildren; j++){
01899                                                                 minStack.push( tmpStack.top() );
01900                                                                 tmpStack.pop();
01901                                                         }
01902                                                         outStr << "min(";
01903                                                         for(j = 0; j < nlnodeMin->inumberOfChildren; j++){
01904                                                                 outStr << minStack.top();
01905                                                                 if (j < nlnodeMin->inumberOfChildren - 1) outStr << " , ";
01906                                                                 minStack.pop();
01907                                                         }
01908                                                         outStr << ")";
01909                                                         tmpStack.push( outStr.str() );
01910                                                         break;
01911                                                         
01912                                                 case OS_MAX :
01913                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing max operater");
01914                                                         //std::cout << "INSIDE Max NODE " << std::endl;
01915                                                         nlnodeMax = (OSnLNodeMax*)nlnode;
01916                                                         outStr.str("");
01917                                                         for(j = 0; j < nlnodeMax->inumberOfChildren; j++){
01918                                                                 maxStack.push( tmpStack.top() );
01919                                                                 tmpStack.pop();
01920                                                         }
01921                                                         outStr << "max(";
01922                                                         for(j = 0; j < nlnodeMax->inumberOfChildren; j++){
01923                                                                 outStr << maxStack.top();
01924                                                                 if (j < nlnodeMax->inumberOfChildren - 1) outStr << " , ";
01925                                                                 maxStack.pop();
01926                                                         }
01927                                                         outStr << ")";
01928                                                         tmpStack.push( outStr.str() );
01929                                                         break;
01930                                                         
01931                                                 case OS_IF :
01932                                                 
01933                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing if operater ");
01934                                                         if(nlnode->inumberOfChildren != 3)throw  ErrorClass("The if node must have three children");
01935                                                         tmp1 = tmpStack.top();
01936                                                         tmpStack.pop();
01937                                                         tmp2 = tmpStack.top();
01938                                                         tmpStack.pop();
01939                                                         tmp3 = tmpStack.top();
01940                                                         tmpStack.pop();
01941                                                         tmpStack.push( "if(" + tmp3 + "," + tmp2 + "," + tmp1 +")" );
01942                                                         break;
01943                                                         
01944                                                 case OS_PRODUCT :
01945                                                         if( tmpStack.size() < nlnode->inumberOfChildren) throw  ErrorClass("There is an error in the OSExpression Tree -- Problem writing product operater");
01946                                                         //std::cout << "INSIDE Product NODE " << std::endl;
01947                                                         nlnodeProduct = (OSnLNodeProduct*)nlnode;
01948                                                         outStr.str("");
01949                                                         for(j = 0; j < nlnodeProduct->inumberOfChildren; j++){
01950                                                                 productStack.push( tmpStack.top() );
01951                                                                 tmpStack.pop();
01952                                                         }
01953                                                         outStr << "(";
01954                                                         for(j = 0; j < nlnodeProduct->inumberOfChildren; j++){
01955                                                                 outStr << productStack.top();
01956                                                                 if (j < nlnodeProduct->inumberOfChildren - 1) outStr << " * ";
01957                                                                 productStack.pop();
01958                                                         }
01959                                                         outStr << ")";
01960                                                         tmpStack.push( outStr.str() );
01961                                                         //std::cout << outStr.str() << std::endl;
01962                                                         break;
01963                                                         
01964                                                 default:
01965                                                          throw  ErrorClass("operator " + nlnode->getTokenName() + " not supported");
01966                                                 break;
01967                                   }
01968                                         opStack.pop();
01969                                 }
01970                                 postfixVec.clear();
01971                                 if(tmpStack.size() != 1) throw ErrorClass( "There is an error in the OSExpression Tree -- stack size should be 1 at end");
01972                                 resultString = tmpStack.top();
01973                                 //std::cout << resultString << std::endl;
01974                                 tmpStack.pop();
01975                                 
01976                                 return resultString;
01977                         }
01978                         else{
01979                                 //throw ErrorClass("Error in getNonlinearExpressionTreeInInfix, there is no expression tree for this index");
01980                                 return "";
01981                         }
01982                 }  
01983                 else{
01984                         throw ErrorClass("Error in getNonlinearExpressionTreeInInfix, rowIdx not valid");
01985                 }
01986                 return resultString;    
01987         }
01988         catch(const ErrorClass& eclass){
01989                 throw ErrorClass( eclass.errormsg);
01990         } 
01991 }//getNonlinearExpressionTreeInInfix
01992 
01993 
01994 std::string OSInstance::printModel( ){
01995         std::string resultString = "";
01996         ostringstream outStr;
01997         outStr << "";
01998         int numCon;
01999         int numObj;
02000         int numVar;
02001         int i;
02002         numCon = this->getConstraintNumber();
02003         numObj = this->getObjectiveNumber();
02004         numVar = this->getVariableNumber();
02005         this->initForAlgDiff( );
02006         outStr << std::endl;
02007         outStr << "Objectives:" << std::endl;           
02008         for(i = 0; i < numObj; i++){
02009                 if( this->getObjectiveMaxOrMins()[0] == "min"){
02010                         outStr << "min ";
02011                 }
02012                 else{
02013                         outStr << "max ";
02014                 }
02015                 outStr << this->printModel( i - numObj);
02016         }       
02017         outStr << std::endl;
02018         outStr << "Constraints:" << std::endl;          
02019         for(i = 0; i < numCon; i++){
02020                 outStr << this->printModel( i);
02021         }
02022         
02023         outStr << std::endl;
02024         outStr << "Variables:" << std::endl;
02025         if(m_bProcessVariables != true  || bVariablesModified == true ) this->processVariables();
02026         for(i = 0; i < numVar; i++){
02027 
02028 
02029         if(this->instanceData->variables->numberOfVariables > 0 && this->instanceData->variables->var[  i ]->name.size() > 0){
02030                                         outStr << this->instanceData->variables->var[  i ]->name;
02031                                         }else{
02032                                         outStr << "x_";
02033                                         outStr << i;
02034                                 }
02035 
02036 
02037                 //outStr << "x_";
02038                 //outStr << i;
02039                 outStr << " Type = " ;
02040                 outStr <<  m_mcVariableTypes[i];
02041                 outStr << "  Lower Bound =  ";
02042                 outStr << os_dtoa_format( m_mdVariableLowerBounds[i])  ;
02043                 outStr << "  Upper Bound =  ";
02044                 outStr << os_dtoa_format( m_mdVariableUpperBounds[i])  ;
02045                 outStr << std::endl;
02046         }       
02047         
02048         // if model was originally in column matrix form we need to delete the new
02049         // matrix stored by row
02050         
02051         //if( this->instanceData->linearConstraintCoefficients != NULL && this->instanceData->linearConstraintCoefficients->numberOfValues > 0){
02052         //      if(m_bColumnMajor == true){// we now have a duplication
02053         //              delete m_linearConstraintCoefficientsInRowMajor;
02054         //      }
02055         //}
02056         return outStr.str() ;
02057 }//printModel( )
02058 
02059 
02060 std::string OSInstance::printModel(int rowIdx ){
02061         std::string resultString = "";
02062         ostringstream outStr;
02063         outStr << "";
02064         //loop over the consraints first;
02065         int j;
02066         int row_nonz = 0;
02067         int obj_nonz = 0;
02068         int varIdx = 0;
02069         bool addedLinearTerm = false;
02070         
02071         // initialize all of the necessary nonlinear stuff
02072         this->initForAlgDiff( );        
02073         
02074         if( rowIdx >= 0){
02075                 if( m_bProcessConstraints != true || bConstraintsModified == true) this->processConstraints() ;
02076                 if( m_mdConstraintLowerBounds[ rowIdx] >  -OSDBL_MAX){
02077                         if(m_mdConstraintLowerBounds[ rowIdx] < m_mdConstraintUpperBounds[ rowIdx]){
02078                                 outStr << os_dtoa_format( m_mdConstraintLowerBounds[ rowIdx] );
02079                                 outStr << " <= ";
02080                         }
02081                 } 
02082                 //
02083                 if(this->instanceData->linearConstraintCoefficients != NULL && this->instanceData->linearConstraintCoefficients->numberOfValues > 0){
02084                         if(m_linearConstraintCoefficientsInRowMajor == NULL) 
02085                                 m_linearConstraintCoefficientsInRowMajor = this->getLinearConstraintCoefficientsInRowMajor();
02086                         row_nonz = m_linearConstraintCoefficientsInRowMajor->starts[ rowIdx + 1] - m_linearConstraintCoefficientsInRowMajor->starts[ rowIdx];
02087                         
02088                         for(j = 0; j < row_nonz; j++){
02089                                 varIdx =  m_linearConstraintCoefficientsInRowMajor->indexes[ m_linearConstraintCoefficientsInRowMajor->starts[ rowIdx]  + j];
02090                                 
02091                                 if(m_bSparseJacobianCalculated == false  ||  (m_mapExpressionTreesMod.find( rowIdx) == m_mapExpressionTreesMod.end() ) ||
02092                                         ( (*m_mapExpressionTreesMod[ rowIdx]->mapVarIdx).find( varIdx) == (*m_mapExpressionTreesMod[ rowIdx]->mapVarIdx).end()) ){                                      
02093                                         outStr << os_dtoa_format( m_linearConstraintCoefficientsInRowMajor->values[ m_linearConstraintCoefficientsInRowMajor->starts[ rowIdx]  + j] );
02094                                         outStr << "*";
02095 
02096                                 if(this->instanceData->variables->numberOfVariables > 0 && this->instanceData->variables->var[  varIdx ]->name.size() > 0){
02097                                         outStr << this->instanceData->variables->var[  varIdx ]->name;
02098                                 }else{
02099                                         outStr << "x_";
02100                                         outStr << varIdx;
02101                                 }
02102 
02103 
02104                                         if( j < row_nonz - 1) outStr << " + ";
02105                                         addedLinearTerm = true;
02106                                 }
02107                         }
02108                 }
02109         }else{// process an objective function
02110                 if(m_bProcessObjectives != true || bObjectivesModified == true)  this->processObjectives() ;
02111                 int obj_idx =  -rowIdx - 1;
02112                 obj_nonz = m_miNumberOfObjCoef[ obj_idx];
02113                 for(j = 0; j < obj_nonz; j++){
02114                         outStr << os_dtoa_format( m_mObjectiveCoefficients[obj_idx]->values[j] );
02115                         outStr << "*";
02116                         if(this->instanceData->variables->numberOfVariables > 0 && this->instanceData->variables->var[ m_mObjectiveCoefficients[obj_idx]->indexes[j] ]->name.size() > 0){
02117                                 outStr << this->instanceData->variables->var[ m_mObjectiveCoefficients[obj_idx]->indexes[j] ]->name;
02118                         }else{
02119                                 outStr << "x_";
02120                                 outStr << m_mObjectiveCoefficients[obj_idx]->indexes[j] ;
02121                         }
02122                         if( j < obj_nonz - 1) outStr << " + ";          
02123                 }
02124         }
02125         if( this->getNonlinearExpressionTree( rowIdx) != NULL){
02126                 if( (addedLinearTerm == true)  || (obj_nonz > 0) ) outStr << " + " ;
02127                 outStr << getNonlinearExpressionTreeInInfix( rowIdx);
02128                 //outStr << ")";
02129         }
02130                 
02131         if( rowIdx >= 0){
02132                 if( m_bProcessConstraints != true ) this->processConstraints() ;
02133                 if( m_mdConstraintUpperBounds[ rowIdx] <  OSDBL_MAX){
02134                         if(m_mdConstraintLowerBounds[ rowIdx] < m_mdConstraintUpperBounds[ rowIdx]){
02135                                 outStr << " <= ";
02136                          }else{
02137                                 outStr << " = ";
02138                          }
02139                         outStr << os_dtoa_format( m_mdConstraintUpperBounds[ rowIdx] ); 
02140                         outStr << "  ";
02141                         outStr << this->getConstraintNames()[ rowIdx] ;
02142                 }
02143         }
02144         outStr << std::endl;    
02145         resultString = outStr.str();
02146         return resultString;
02147 }//printModel( rowIdx )
02148 
02149 
02150 std::vector<OSnLNode*> OSInstance::getNonlinearExpressionTreeModInPostfix( int rowIdx){
02151         //if( m_binitForAlgDiff == false) this->initForAlgDiff();
02152         if( m_bProcessExpressionTreesMod == false ) getAllNonlinearExpressionTreesMod();
02153         std::vector<OSnLNode*> postfixVec;
02154         try{
02155                 if( m_mapExpressionTreesMod.find( rowIdx) != m_mapExpressionTreesMod.end()){
02156                         OSExpressionTree* expTree = getNonlinearExpressionTreeMod( rowIdx);
02157                         postfixVec = expTree->m_treeRoot->getPostfixFromExpressionTree();
02158                         
02159                 }  
02160                 else{
02161                         throw ErrorClass("Error in getNonlinearExpressionTreeModInPostfix, rowIdx not valid");
02162                 }
02163                 return postfixVec;      
02164         }
02165         catch(const ErrorClass& eclass){
02166                 throw ErrorClass( eclass.errormsg);
02167         } 
02168 }//getNonlinearExpressionTreeModInPostfix
02169 
02170 
02171 std::vector<OSnLNode*> OSInstance::getNonlinearExpressionTreeInPrefix( int rowIdx){
02172         //if( m_binitForAlgDiff == false) this->initForAlgDiff();
02173         if( m_bProcessExpressionTrees == false ) getAllNonlinearExpressionTrees();
02174         std::vector<OSnLNode*> prefixVec;
02175         try{
02176                 if( m_mapExpressionTrees.find( rowIdx) != m_mapExpressionTrees.end()){
02177                         OSExpressionTree* expTree = getNonlinearExpressionTree( rowIdx);
02178                         prefixVec = expTree->m_treeRoot->getPrefixFromExpressionTree();
02179                         
02180                 }  
02181                 else{
02182                         throw ErrorClass("Error in getNonlinearExpressionTreeInPrefix, rowIdx not valid");
02183                 }
02184                 return prefixVec;       
02185         }
02186         catch(const ErrorClass& eclass){
02187                 throw ErrorClass( eclass.errormsg);
02188         } 
02189 }//getNonlinearExpressionTreeInPrefix
02190 
02191 std::vector<OSnLNode*> OSInstance::getNonlinearExpressionTreeModInPrefix( int rowIdx){
02192         //if( m_binitForAlgDiff == false) this->initForAlgDiff();
02193         if( m_bProcessExpressionTreesMod == false ) getAllNonlinearExpressionTreesMod();
02194         std::vector<OSnLNode*> prefixVec;
02195         try{
02196                 if( m_mapExpressionTreesMod.find( rowIdx) != m_mapExpressionTreesMod.end()){
02197                         OSExpressionTree* expTree = getNonlinearExpressionTreeMod( rowIdx);
02198                         prefixVec = expTree->m_treeRoot->getPrefixFromExpressionTree();
02199                         
02200                 }  
02201                 else{
02202                         throw ErrorClass("Error in getNonlinearExpressionTreeInPrefix, rowIdx not valid");
02203                 }
02204                 return prefixVec;       
02205         }
02206         catch(const ErrorClass& eclass){
02207                 throw ErrorClass( eclass.errormsg);
02208         } 
02209 }//getNonlinearExpressionTreeInPrefix
02210 
02211 std::map<int, OSExpressionTree*> OSInstance::getAllNonlinearExpressionTrees(){
02212         //if( m_binitForAlgDiff == false) this->initForAlgDiff();
02213         if(m_bProcessExpressionTrees == true) return m_mapExpressionTrees;
02214         std::map<int, int> foundIdx;
02215         std::map<int, int>::iterator pos;
02216         OSnLNodePlus *nlNodePlus;
02217         OSExpressionTree *expTree;  
02218         m_iObjectiveNumberNonlinear = 0;   
02219         m_iConstraintNumberNonlinear = 0;    
02220         int i;   
02221         // important -- tell the nl nodes not to destroy the OSExpression Objects
02222         if( instanceData->nonlinearExpressions->numberOfNonlinearExpressions > 0 && instanceData->nonlinearExpressions->nl != NULL){
02223                 for( i = 0; i < instanceData->nonlinearExpressions->numberOfNonlinearExpressions; i++){
02224                         instanceData->nonlinearExpressions->nl[i]->m_bDeleteExpressionTree = false;
02225                 }
02226         }
02227         int index;  
02228         // kipp -- what should we return if instanceData->nonlinearExpressions->numberOfNonlinearExpressions is zero
02229         for(i = 0; i < instanceData->nonlinearExpressions->numberOfNonlinearExpressions; i++){
02230                 index = instanceData->nonlinearExpressions->nl[ i]->idx;
02231                 if(foundIdx.find( index) != foundIdx.end() ){  
02232                 //if(foundIdx[ index] > 0 ){ 
02233                         //std::cout << "OLD INDEX FOUND " << index << std::endl;
02234                         //std::cout << "foundIdx[ index] " << index << std::endl;
02235                         // found an existing index
02236                         // important -- at this time m_mapExpressionTrees[ index] points to 
02237                         // the last OSExpressionTree with this index, it does not point to the 
02238                         // the just found OSExpressionTree with this index
02239                         nlNodePlus = new OSnLNodePlus();
02240                         //expTree = new OSExpressionTree(); 
02241                         expTree =  instanceData->nonlinearExpressions->nl[ i]->osExpressionTree;
02242                         // set left child to old index and right child to new one 
02243                         nlNodePlus->m_mChildren[ 0] = m_mapExpressionTrees[ index]->m_treeRoot;
02244                         nlNodePlus->m_mChildren[ 1] = instanceData->nonlinearExpressions->nl[ i]->osExpressionTree->m_treeRoot;
02245                         // we must delete the Expression tree corresponding to the old index value but not the nl nodes
02246                         instanceData->nonlinearExpressions->nl[ foundIdx[ index]  ]->m_bDeleteExpressionTree = true;
02247                         instanceData->nonlinearExpressions->nl[ foundIdx[ index]  ]->osExpressionTree->bDestroyNlNodes = false;
02248                         //point to the new expression tree
02249                         m_mapExpressionTrees[ index] = expTree;
02250                         m_mapExpressionTrees[ index]->m_treeRoot = nlNodePlus;
02251                         foundIdx[ index] = i;
02252                 }
02253                 else{  
02254                         // we have a new index
02255                         m_mapExpressionTrees[ index] = instanceData->nonlinearExpressions->nl[ i]->osExpressionTree;
02256                         m_mapExpressionTrees[ index]->m_treeRoot = instanceData->nonlinearExpressions->nl[ i]->osExpressionTree->m_treeRoot;
02257                         foundIdx[ index] = i;
02258                 }
02259                 //foundIdx[ index]++;    
02260         }
02261         // count the number of constraints and objective functions with nonlinear terms.
02262         for(pos = foundIdx.begin(); pos != foundIdx.end(); ++pos){
02263                 if(pos->first == -1) {
02264                         m_iObjectiveNumberNonlinear++; 
02265                 }
02266                 else m_iConstraintNumberNonlinear++;
02267         }
02268         foundIdx.clear();
02269         m_bProcessExpressionTrees = true;
02270         return m_mapExpressionTrees;
02271 }// getAllNonlinearExpressionTrees
02272 
02273 std::map<int, OSExpressionTree*> OSInstance::getAllNonlinearExpressionTreesMod(){
02274         if( m_bProcessExpressionTreesMod == true ) return m_mapExpressionTreesMod;
02275         m_bProcessExpressionTreesMod = true;
02276         // make sure we have the modified map available
02277         if( m_bNonLinearStructuresInitialized == false) initializeNonLinearStructures( );
02278         return m_mapExpressionTreesMod;
02279 }// getAllNonlinearExpressionTreesMod
02280 
02281 
02282 std::string OSInstance::getTimeDomainFormat()
02283 {       if (instanceData->timeDomain == NULL)
02284                 return "";
02285         if (instanceData->timeDomain->interval != NULL)
02286                 return "interval";
02287         if (instanceData->timeDomain->stages != NULL)
02288                 return "stages";
02289         return "";
02290 }// getTimeDomainFormat
02291 
02292 int OSInstance::getTimeDomainStageNumber()
02293 {       if (instanceData->timeDomain == NULL)
02294                 return 1;
02295         if (instanceData->timeDomain->interval != NULL)
02296                 ; //throw an error
02297         if (instanceData->timeDomain->stages == NULL)
02298                 return 1;
02299         return instanceData->timeDomain->stages->numberOfStages;
02300 }// getTimeDomainStageNumber 
02301         
02302 std::string* OSInstance::getTimeDomainStageNames()
02303 {       if (instanceData->timeDomain == NULL)
02304                 return NULL;
02305         if (instanceData->timeDomain->interval != NULL)
02306                 return NULL; //throw an error
02307         if (instanceData->timeDomain->stages == NULL)
02308                 return NULL;
02309         if (m_msTimeDomainStageNames != NULL)
02310                 delete [] m_msTimeDomainStageNames;
02311         if (instanceData->timeDomain->stages->numberOfStages == 0)
02312                 return NULL;
02313         m_msTimeDomainStageNames = new std::string[instanceData->timeDomain->stages->numberOfStages]; 
02314         for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
02315                 m_msTimeDomainStageNames[i] = instanceData->timeDomain->stages->stage[i]->name;
02316         return m_msTimeDomainStageNames;
02317 }// getTimeDomainStageNames
02318         
02319 int* OSInstance::getTimeDomainStageNumberOfVariables()
02320 {       if (instanceData->timeDomain == NULL)
02321                 return NULL;
02322         if (instanceData->timeDomain->interval != NULL)
02323                 return NULL; //throw an error
02324         if (instanceData->timeDomain->stages == NULL)
02325                 return NULL;
02326         if (m_miTimeDomainStageVariableNumber != NULL) 
02327                 delete [] m_miTimeDomainStageVariableNumber;
02328         if (instanceData->timeDomain->stages->numberOfStages == 0)
02329                 return NULL;
02330         m_miTimeDomainStageVariableNumber = new int[instanceData->timeDomain->stages->numberOfStages];
02331         for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++) 
02332                 m_miTimeDomainStageVariableNumber[i] = instanceData->timeDomain->stages->stage[i]->variables->numberOfVariables;
02333         return m_miTimeDomainStageVariableNumber;
02334 }// getTimeDomainStageNumberOfVariables 
02335         
02336 int* OSInstance::getTimeDomainStageNumberOfConstraints()
02337 {       if (instanceData->timeDomain == NULL)
02338                 return NULL;
02339         if (instanceData->timeDomain->interval != NULL)
02340                 return NULL; //throw an error
02341         if (instanceData->timeDomain->stages == NULL)
02342                 return NULL;
02343         if (m_miTimeDomainStageConstraintNumber != NULL)
02344                 delete [] m_miTimeDomainStageConstraintNumber;
02345         if (instanceData->timeDomain->stages->numberOfStages == 0)
02346                 return NULL;
02347         m_miTimeDomainStageConstraintNumber = new int[instanceData->timeDomain->stages->numberOfStages];
02348         for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
02349                 m_miTimeDomainStageConstraintNumber[i] = instanceData->timeDomain->stages->stage[i]->constraints->numberOfConstraints;
02350         return m_miTimeDomainStageConstraintNumber;
02351 }// getTimeDomainStageNumberOfConstraints
02352 
02353 int* OSInstance::getTimeDomainStageNumberOfObjectives()
02354 {       if (instanceData->timeDomain == NULL)
02355                 return NULL;
02356         if (instanceData->timeDomain->interval != NULL)
02357                 return NULL; //throw an error
02358         if (instanceData->timeDomain->stages == NULL)
02359                 return NULL;
02360         if (m_miTimeDomainStageObjectiveNumber != NULL)
02361                 delete [] m_miTimeDomainStageObjectiveNumber;
02362         if (instanceData->timeDomain->stages->numberOfStages == 0)
02363                 return NULL;
02364         m_miTimeDomainStageObjectiveNumber = new int[instanceData->timeDomain->stages->numberOfStages];
02365         for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
02366                 m_miTimeDomainStageObjectiveNumber[i] = instanceData->timeDomain->stages->stage[i]->objectives->numberOfObjectives;
02367         return m_miTimeDomainStageObjectiveNumber;
02368 }// getTimeDomainStageNumberOfObjectives
02369         
02370 int** OSInstance::getTimeDomainStageVarList()
02371 {       if (instanceData->timeDomain == NULL)
02372                 return NULL;
02373         if (instanceData->timeDomain->interval != NULL)
02374                 return NULL; //throw an error
02375         if (instanceData->timeDomain->stages == NULL)
02376                 return NULL;
02377         if (m_miTimeDomainStageVariableNumber == NULL)
02378                 return NULL;
02379         if (m_mmiTimeDomainStageVarList != NULL){
02380                 for (int i = 0; i < m_iNumberOfTimeStages; i ++) 
02381                         delete[] m_mmiTimeDomainStageVarList[i];
02382                 delete[] m_mmiTimeDomainStageVarList;
02383                 m_mmiTimeDomainStageVarList = NULL;
02384         }
02385         
02386                 //delete [] m_mmiTimeDomainStageVarList;
02387         if (instanceData->timeDomain->stages->numberOfStages == 0)
02388                 return NULL;
02389         m_iNumberOfTimeStages = instanceData->timeDomain->stages->numberOfStages;
02390         m_mmiTimeDomainStageVarList = new int*[instanceData->timeDomain->stages->numberOfStages];
02391         int timeDomainStageNumberVar;
02392         for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
02393         {       
02394                 timeDomainStageNumberVar = m_miTimeDomainStageVariableNumber[i];
02395                 m_mmiTimeDomainStageVarList[i] = new int[ timeDomainStageNumberVar ];
02396                 if (instanceData->timeDomain->stages->stage[i]->variables->startIdx == -1)
02397                         for (int j = 0; j < m_miTimeDomainStageVariableNumber[i]; j++)
02398                                 m_mmiTimeDomainStageVarList[i][j] = instanceData->timeDomain->stages->stage[i]->variables->var[j]->idx;
02399                 else
02400                         for (int j = 0; j < m_miTimeDomainStageVariableNumber[i]; j++)
02401                                 m_mmiTimeDomainStageVarList[i][j] = instanceData->timeDomain->stages->stage[i]->variables->startIdx + j;
02402         }
02403         return m_mmiTimeDomainStageVarList;
02404 }// getTimeDomainStageVarList
02405         
02406 int** OSInstance::getTimeDomainStageConList()
02407 {       if (instanceData->timeDomain == NULL)
02408                 return NULL;
02409         if (instanceData->timeDomain->interval != NULL)
02410                 return NULL; //throw an error
02411         if (instanceData->timeDomain->stages == NULL)
02412                 return NULL;
02413         if (m_miTimeDomainStageConstraintNumber == NULL)
02414                 return NULL;
02415         if (m_mmiTimeDomainStageConList != NULL){
02416                 
02417                 for (int i = 0; i < m_iNumberOfTimeStages; i ++) 
02418                         delete[] m_mmiTimeDomainStageConList[i];
02419                 delete[] m_mmiTimeDomainStageConList;
02420                 m_mmiTimeDomainStageConList = NULL;             
02421         }
02422                 
02423         if (instanceData->timeDomain->stages->numberOfStages == 0)
02424                 return NULL;
02425         m_iNumberOfTimeStages = instanceData->timeDomain->stages->numberOfStages;
02426         m_mmiTimeDomainStageConList = new int*[instanceData->timeDomain->stages->numberOfStages];
02427         int numTimeDomainStageCon;
02428         for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
02429         {       
02430                 numTimeDomainStageCon = m_miTimeDomainStageConstraintNumber[i];
02431                 m_mmiTimeDomainStageConList[i] = new int[ numTimeDomainStageCon];
02432                 if (instanceData->timeDomain->stages->stage[i]->constraints->startIdx == -1)
02433                         for (int j = 0; j < m_miTimeDomainStageConstraintNumber[i]; j++)
02434                                 m_mmiTimeDomainStageConList[i][j] = instanceData->timeDomain->stages->stage[i]->constraints->con[j]->idx;
02435                 else
02436                         for (int j = 0; j < m_miTimeDomainStageConstraintNumber[i]; j++)
02437                                 m_mmiTimeDomainStageConList[i][j] = instanceData->timeDomain->stages->stage[i]->constraints->startIdx + j;
02438         }
02439         return m_mmiTimeDomainStageConList;
02440 }// getTimeDomainStageConList
02441 
02442 int** OSInstance::getTimeDomainStageObjList()
02443 {       if (instanceData->timeDomain == NULL)
02444                 return NULL;
02445         if (instanceData->timeDomain->interval != NULL)
02446                 return NULL; //throw an error
02447         if (instanceData->timeDomain->stages == NULL)
02448                 return NULL;
02449         if (m_miTimeDomainStageObjectiveNumber == NULL)
02450                 return NULL;
02451         if (m_mmiTimeDomainStageObjList != NULL){
02452                 for (int i = 0; i < m_iNumberOfTimeStages; i ++) 
02453                         delete[] m_mmiTimeDomainStageObjList[i];
02454                 delete[] m_mmiTimeDomainStageObjList;
02455                 m_mmiTimeDomainStageObjList = NULL;
02456         }
02457         if (instanceData->timeDomain->stages->numberOfStages == 0)
02458                 return NULL;
02459         m_iNumberOfTimeStages = instanceData->timeDomain->stages->numberOfStages;
02460         m_mmiTimeDomainStageObjList = new int*[instanceData->timeDomain->stages->numberOfStages];
02461         int numTimeDomainStageObjNum;
02462         for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
02463         {       
02464                 numTimeDomainStageObjNum = m_miTimeDomainStageObjectiveNumber[i];
02465                 m_mmiTimeDomainStageObjList[i] = new int[ numTimeDomainStageObjNum];
02466                 if (instanceData->timeDomain->stages->stage[i]->objectives->startIdx == 0)
02467                         for (int j = 0; j < m_miTimeDomainStageObjectiveNumber[i]; j++)
02468                                 m_mmiTimeDomainStageObjList[i][j] = instanceData->timeDomain->stages->stage[i]->objectives->obj[j]->idx;
02469                 else
02470                         for (int j = 0; j < m_miTimeDomainStageObjectiveNumber[i]; j++)
02471                                 m_mmiTimeDomainStageObjList[i][j] = instanceData->timeDomain->stages->stage[i]->objectives->startIdx - j;
02472         }
02473         return m_mmiTimeDomainStageObjList;
02474 }// getTimeDomainStageObjList
02475 
02476 double OSInstance::getTimeDomainIntervalStart()
02477 {       if (instanceData->timeDomain == NULL)
02478                 return 0.0;
02479         if (instanceData->timeDomain->stages != NULL)
02480                 return 0.0; //throw an error
02481         if (instanceData->timeDomain->interval == NULL)
02482                 return 0.0;
02483         return instanceData->timeDomain->interval->start;
02484 }// getTimeDomainIntervalStart
02485 
02486 double OSInstance::getTimeDomainIntervalHorizon()
02487 {       if (instanceData->timeDomain == NULL)
02488                 return 0.0;
02489         if (instanceData->timeDomain->stages != NULL)
02490                 return 0.0; //throw an error
02491         if (instanceData->timeDomain->interval == NULL)
02492                 return 0.0;
02493         return instanceData->timeDomain->interval->horizon;
02494 }// getTimeDomainIntervalHorizon
02495 
02496 
02497 
02498 
02499 // the set() methods
02500 
02501 bool OSInstance::setInstanceSource(string source){
02502         instanceHeader->source = source;
02503         return true;
02504 }//setInstanceSource
02505 
02506 bool OSInstance::setInstanceDescription(string description){
02507         instanceHeader->description = description;
02508         return true;
02509 }//setInstanceDescription
02510 
02511 
02512 bool OSInstance::setInstanceName(string name){
02513          instanceHeader->name = name;
02514          return true;
02515 }//setInstanceName
02516 
02517 
02518 bool OSInstance::setVariableNumber(int number){
02519         // this method assume osinstance->instanceData->variables is not null
02520         if(number < 0) return false;
02521         //if(instanceData->variables->numberOfVariables != -1  && instanceData->variables->numberOfVariables != number){
02522         //      delete[] instanceData->variables->var;
02523         //      instanceData->variables->var = NULL;
02524         //} 
02525         if(instanceData->variables == NULL) instanceData->variables = new Variables();
02526         instanceData->variables->numberOfVariables = number;
02527         if(instanceData->variables->var == NULL && number > 0){
02528                 instanceData->variables->var = new Variable*[number];
02529         }
02530         return true;
02531 }//setVariableNumber
02532 
02533 
02534 bool OSInstance::addVariable(int index, string name, double lowerBound, double upperBound, char type){
02535         if (verifyVarType(type) == false) type = 'C';
02536         instanceData->variables->var[index] = new Variable();
02537         if(index < 0 || instanceData->variables->numberOfVariables <= 0 || index >= instanceData->variables->numberOfVariables) return false;
02538         instanceData->variables->var[index]->name = name;
02539         instanceData->variables->var[index]->lb = lowerBound;
02540         instanceData->variables->var[index]->ub = upperBound;
02541         instanceData->variables->var[index]->type = type;
02542         //if(init != OSNAN) instanceData->variables->var[index]->init = init;
02543         //instanceData->variables->var[index]->initString = initString;
02544         return true;
02545 }//addVariable
02546 
02547 
02548 bool OSInstance::setVariables(int number, string *names, double *lowerBounds, 
02549         double *upperBounds, char *types){
02550         if(number <= 0) return false;
02551         try{
02552                 if(instanceData->variables == NULL){
02553                         throw ErrorClass("There is no variables object");
02554                 }
02555                 if(instanceData->variables->numberOfVariables != number){
02556                         throw ErrorClass("input number of variables not equal to number in class");
02557                 }
02558                 //instanceData->variables->var = new Variable*[number];
02559                 int i;
02560                 for(i = 0; i < number; i++){
02561                         instanceData->variables->var[ i] = new Variable();
02562                 }
02563                 if(names  != NULL){
02564                         for(i = 0; i < number; i++) instanceData->variables->var[i]->name = names[i];                           
02565                 }
02566                 if(lowerBounds != NULL){
02567                         for(i = 0; i < number; i++){
02568                                 instanceData->variables->var[i]->lb = lowerBounds[i];  
02569                         }
02570                 }
02571                 if(upperBounds != NULL){
02572                         for(i = 0; i < number; i++){
02573                                 instanceData->variables->var[i]->ub = upperBounds[i]; 
02574                         }
02575                 }
02576                 if(types != NULL){
02577                         for(i = 0; i < number; i++){
02578                                 instanceData->variables->var[i]->type = types[i];
02579                                 if(verifyVarType(types[i]) == false) types[i] = 'C';
02580                         } 
02581                 }
02582                 /*
02583                 if(inits != NULL){
02584                         for(i = 0; i < number; i++) instanceData->variables->var[i]->init = inits[i];                           
02585                 }
02586                 if(initsString != NULL){
02587                         for(i = 0; i < number; i++) instanceData->variables->var[i]->initString = initsString[i];                       
02588                 }
02589                 */
02590                 return true;
02591         }
02592         catch(const ErrorClass& eclass){
02593                 throw ErrorClass(  eclass.errormsg); 
02594         }
02595 }//setVariables
02596 
02597 // begin checking again with Jun Ma
02598 
02599 bool OSInstance::setObjectiveNumber(int number){
02600         if(number < 0) return false;
02601         if(instanceData->objectives == NULL) instanceData->objectives = new Objectives();
02602         if(number == 0){
02603                 instanceData->objectives->numberOfObjectives = 0;
02604                 instanceData->objectives->obj = 0;
02605                 return true;
02606         }
02607         instanceData->objectives->numberOfObjectives = number;
02608         instanceData->objectives->obj = new Objective*[number];                         
02609         return true;
02610 }//setObjectiveNumber
02611 
02612 bool OSInstance::addObjective(int index, string name, string maxOrMin, double constant, double weight, SparseVector *objectiveCoefficients){
02613         if(index >= 0 || instanceData->objectives->numberOfObjectives <= 0 || abs(index) > instanceData->objectives->numberOfObjectives) return false;
02614         int arrayIndex = abs(index) -1;
02615         if(instanceData->objectives->obj == NULL) return false;
02616         instanceData->objectives->obj[arrayIndex] = new Objective();
02617         instanceData->objectives->obj[arrayIndex]->name = name;
02618         if( (maxOrMin != "max") && (maxOrMin != "min") ) return false;
02619         else instanceData->objectives->obj[arrayIndex]->maxOrMin = maxOrMin;
02620         instanceData->objectives->obj[arrayIndex]->constant = constant;
02621         instanceData->objectives->obj[arrayIndex]->weight = weight;
02622         int n = objectiveCoefficients->number;                  
02623         instanceData->objectives->obj[arrayIndex]->numberOfObjCoef = n;
02624         if(n == 0){
02625                 instanceData->objectives->obj[arrayIndex]->coef = 0;
02626         }
02627         else{
02628                 int i = 0;
02629                 instanceData->objectives->obj[arrayIndex]->coef = new ObjCoef*[n];
02630                 for(i = 0; i < n; i++) instanceData->objectives->obj[arrayIndex]->coef[i] = new ObjCoef();
02631                 for(i = 0; i < n; i++){
02632                         instanceData->objectives->obj[arrayIndex]->coef[i]->idx = objectiveCoefficients->indexes[i];
02633                         instanceData->objectives->obj[arrayIndex]->coef[i]->value = objectiveCoefficients->values[i];   
02634                 }                       
02635         }
02636         return true;
02637 }//addObjective
02638 
02639 bool OSInstance::setObjectives(int number, string *names, string *maxOrMins, double *constants, double *weights, SparseVector **objectiveCoefficients){
02640         if(number < 0) return false;
02641         try{
02642                 if(instanceData->objectives == NULL){
02643                         throw ErrorClass("there is no objectives object");              
02644                 }               
02645                 if(instanceData->objectives->numberOfObjectives != number){
02646                         throw ErrorClass("input number of objective not equal to number in class");             
02647                 }
02648                 if(number == 0) return true;
02649                 int i = 0;
02650                 for(i = 0; i < number; i++)instanceData->objectives->obj[i] = new Objective();
02651                 int j = 0;
02652                 if(names != NULL){
02653                         for(i = 0; i < number; i++) instanceData->objectives->obj[i]->name = names[i];                          
02654                 }       
02655                 if(maxOrMins != NULL){
02656                         for(i = 0; i < number; i++){
02657                                 if(maxOrMins[i] == "" || (maxOrMins[i].compare("max") != 0 && maxOrMins[i].compare("min") !=0)) return false;
02658                                 instanceData->objectives->obj[i]->maxOrMin = maxOrMins[i];                      
02659                         }
02660                 }
02661                 if(constants != NULL){
02662                         for(i = 0; i < number; i++) instanceData->objectives->obj[i]->constant = constants[i];                          
02663                 }
02664                 if(weights != NULL){
02665                         for(i = 0; i < number; i++) instanceData->objectives->obj[i]->weight = weights[i];                      
02666                 }
02667                 if(objectiveCoefficients != NULL){
02668                         for(i = 0; i < number; i++){
02669                                 int n = (&objectiveCoefficients[i] == NULL || objectiveCoefficients[i]->indexes == NULL)?0:objectiveCoefficients[i]->number;            
02670                                 instanceData->objectives->obj[i]->numberOfObjCoef = n;
02671                                 if(n == 0){
02672                                         instanceData->objectives->obj[i]->coef = NULL;
02673                                 }
02674                                 else{
02675                                         instanceData->objectives->obj[i]->coef = new ObjCoef*[n];
02676                                         for(j = 0; j < n; j++){
02677                                                 instanceData->objectives->obj[i]->coef[j] = new ObjCoef();
02678                                                 instanceData->objectives->obj[i]->coef[j]->idx  = objectiveCoefficients[i]->indexes[j];
02679                                                 instanceData->objectives->obj[i]->coef[j]->value = objectiveCoefficients[i]->values[j];                         
02680                                         }                       
02681                                 }                                                       
02682                         }
02683                 }
02684                 return true;
02685         }
02686         catch(const ErrorClass& eclass){
02687                 throw ErrorClass(  eclass.errormsg); 
02688         }
02689 }//setObjectives
02690 
02691 
02692 bool OSInstance::setConstraintNumber(int number){
02693         if(number < 0) return false;
02694         if(instanceData->constraints == NULL) instanceData->constraints = new Constraints();
02695         if(number == 0){
02696                 instanceData->constraints->numberOfConstraints = 0;
02697                 instanceData->constraints->con = 0;
02698                 return true;
02699         }
02700         instanceData->constraints->numberOfConstraints = number;
02701         if(instanceData->constraints->con == 0 ){
02702                 instanceData->constraints->con = new Constraint*[number];
02703         }
02704         return true;
02705 }//setConstraintNumber
02706 
02707 bool OSInstance::addConstraint(int index, string name, double lowerBound, double upperBound, double constant) {
02708         instanceData->constraints->con[ index] = new Constraint();
02709         if(index < 0 || instanceData->constraints->numberOfConstraints <= 0 || index >= instanceData->constraints->numberOfConstraints) return false;
02710         instanceData->constraints->con[ index]->name = name;
02711         if(lowerBound != -OSDBL_MAX && lowerBound != -OSDBL_MAX) instanceData->constraints->con[ index]->lb = lowerBound;
02712         if(upperBound != OSDBL_MAX && upperBound != OSDBL_MAX)instanceData->constraints->con[ index]->ub = upperBound;
02713         instanceData->constraints->con[ index]->constant = constant;
02714         return true;
02715 }//addConstraint
02716 
02717 
02718 bool OSInstance::setConstraints(int number, string* names, double* lowerBounds, double* upperBounds, double* constants){
02719         if(number < 0) return false;
02720         if(number == 0){
02721                 // this is done in setConstraintNumber
02722                 //instanceData->constraints = new Constraints();
02723                 //instanceData->constraints->numberOfConstraints = 0;
02724                 //instanceData->constraints->con = NULL;
02725                 return true;
02726         }
02727         try{
02728                 
02729                 if(instanceData->constraints  == NULL){
02730                         throw ErrorClass("there is no constraints object");             
02731                 }               
02732                 if(instanceData->constraints->numberOfConstraints != number){
02733                         throw ErrorClass("input number of constraints not equal to number in class");           
02734                 }
02735                 int i = 0;
02736                 for(i = 0; i < number; i++){
02737                         instanceData->constraints->con[i] = new Constraint();
02738                 }
02739                 if(names != NULL){
02740                         for(i = 0; i < number; i++) instanceData->constraints->con[i]->name = names[i];                         
02741                 }
02742                 if(lowerBounds != NULL){
02743                         for(i = 0; i < number; i++){
02744                                 if(lowerBounds[i] != -OSDBL_MAX && lowerBounds[i] != -OSDBL_MAX)instanceData->constraints->con[i]->lb = lowerBounds[i]; 
02745                         }
02746                 }
02747                 if(upperBounds != NULL){
02748                         for(i = 0; i < number; i++){
02749                                 if(upperBounds[i] != OSDBL_MAX && upperBounds[i] != OSDBL_MAX)instanceData->constraints->con[i]->ub = upperBounds[i]; 
02750                         }
02751                 }   
02752                 if(constants != NULL){
02753                         for(i = 0; i < number; i++) instanceData->constraints->con[i]->constant = constants[i];                         
02754                 }
02755                 return true;
02756         }
02757         catch(const ErrorClass& eclass){
02758                 throw ErrorClass(  eclass.errormsg); 
02759         }
02760 }//setConstraints
02761 
02762 bool OSInstance::setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor, 
02763                 double* values, int valuesBegin, int valuesEnd, 
02764                 int* indexes, int indexesBegin, int indexesEnd,                         
02765                 int* starts, int startsBegin, int startsEnd)
02766 {
02767         if(numberOfValues < 0) return false;
02768         if(instanceData->linearConstraintCoefficients == NULL) 
02769                 instanceData->linearConstraintCoefficients = new LinearConstraintCoefficients() ;
02770         if(numberOfValues == 0) return true;
02771         if((values == 0 ) ||
02772            (valuesBegin < 0 || (valuesEnd - valuesBegin + 1) != numberOfValues) ||
02773            (indexes == 0) ||
02774            (indexesBegin < 0 || (indexesEnd - indexesBegin + 1) != numberOfValues) ||
02775            (starts == 0 ) ||
02776            (startsBegin < 0  || startsBegin >= startsEnd)) return false;
02777         instanceData->linearConstraintCoefficients->numberOfValues = numberOfValues;
02778         int i = 0;
02779         //starts
02780         if(instanceData->linearConstraintCoefficients->start == NULL) instanceData->linearConstraintCoefficients->start = new IntVector();
02781         if(startsBegin == 0 ){
02782                 instanceData->linearConstraintCoefficients->start->el = starts;
02783         }
02784         else{
02785                 instanceData->linearConstraintCoefficients->start->el = new int[startsEnd - startsBegin + 1];
02786                 int k = 0;
02787                 for(i = startsBegin; i <= startsEnd; i++){
02788                         instanceData->linearConstraintCoefficients->start->el[k] = starts[i];
02789                         k++;
02790                 }
02791         }                
02792         //values
02793         if(instanceData->linearConstraintCoefficients->value == NULL) instanceData->linearConstraintCoefficients->value = new DoubleVector();
02794         if(valuesBegin == 0 ){
02795                 instanceData->linearConstraintCoefficients->value->el = values;
02796         }
02797         else{
02798                 instanceData->linearConstraintCoefficients->value->el = new double[numberOfValues];
02799                 int k = 0;
02800                 for(i = valuesBegin; i <= valuesEnd; i++){
02801                         instanceData->linearConstraintCoefficients->value->el[k] = values[i];
02802                         k++;
02803                 }
02804         }
02805         //indexes
02806         if(instanceData->linearConstraintCoefficients->rowIdx == NULL) instanceData->linearConstraintCoefficients->rowIdx = new IntVector();
02807         if(instanceData->linearConstraintCoefficients->colIdx == NULL) instanceData->linearConstraintCoefficients->colIdx = new IntVector();
02808         if(isColumnMajor){
02809                 if(indexesBegin == 0 ){
02810                         instanceData->linearConstraintCoefficients->rowIdx->el = indexes;
02811                 }
02812                 else{
02813                         instanceData->linearConstraintCoefficients->rowIdx->el = new int[numberOfValues];
02814                         int k = 0;
02815                         for(i = indexesBegin; i <= indexesEnd; i++){
02816                                 instanceData->linearConstraintCoefficients->rowIdx->el[k] = indexes[i];
02817                                 k++;
02818                         }
02819                 }
02820         } 
02821         else{
02822                 if(indexesBegin == 0 ){
02823                         instanceData->linearConstraintCoefficients->colIdx->el = indexes;
02824                 }
02825                 else{
02826                         instanceData->linearConstraintCoefficients->colIdx->el = new int[numberOfValues];
02827                         int k = 0;
02828                         for(i = indexesBegin; i <= indexesEnd; i++){
02829                                 instanceData->linearConstraintCoefficients->colIdx->el[k] = indexes[i];
02830                                 k++;
02831                         }
02832                 }
02833         }
02834         return true;
02835 }//setLinearConstraintCoefficients
02836 
02837 bool OSInstance::setQuadraticTerms(int number, 
02838                 int* rowIndexes, int* varOneIndexes, int* varTwoIndexes, double* coefficients,
02839                 int begin, int end){
02840         if(number < 0) return false;
02841         if(number != (end - begin) + 1) return false;
02842         if(number == 0){
02843                 instanceData->quadraticCoefficients = 0;
02844                 return true;
02845         }
02846         if( ((end - begin + 1) != number) ||
02847            (rowIndexes == 0) ||                    
02848            (varOneIndexes == 0) ||
02849            (varTwoIndexes == 0) ||
02850            (coefficients == 0) ) return false;
02851         instanceData->quadraticCoefficients = new QuadraticCoefficients();
02852         instanceData->quadraticCoefficients->numberOfQuadraticTerms = number;
02853         int i = 0;
02854         instanceData->quadraticCoefficients->qTerm = new QuadraticTerm*[number];
02855         for(i = 0; i < number; i++) instanceData->quadraticCoefficients->qTerm[i] = new QuadraticTerm();
02856         int k = 0;
02857         for(i = begin; i <= end; i++){
02858                         instanceData->quadraticCoefficients->qTerm[k]->idx = rowIndexes[i];
02859                         instanceData->quadraticCoefficients->qTerm[k]->idxOne = varOneIndexes[i];
02860                         instanceData->quadraticCoefficients->qTerm[k]->idxTwo = varTwoIndexes[i];
02861                         instanceData->quadraticCoefficients->qTerm[k]->coef = coefficients[i];
02862                         k++;
02863         }
02864         return true;
02865 }//setQuadraticTerms
02866 
02867 bool OSInstance::setQuadraticTermsInNonlinearExpressions(int numQPTerms, int* rowIndexes, int* varOneIndexes, int* varTwoIndexes, double* coefficients){
02868                 instanceData->nonlinearExpressions->numberOfNonlinearExpressions = numQPTerms;
02869                 instanceData->nonlinearExpressions->nl = new Nl*[ numQPTerms ];
02870                 // define the vectors
02871                 OSnLNode *nlNodePoint;
02872                 OSnLNodeVariable *nlNodeVariablePoint;
02873                 std::vector<OSnLNode*> nlNodeVec;
02874                 //
02875                 //
02876                 int i;
02877                 for(i = 0; i < numQPTerms; i++){
02878                         instanceData->nonlinearExpressions->nl[ i] = new Nl();
02879                         instanceData->nonlinearExpressions->nl[ i]->idx = rowIndexes[ i];
02880                         instanceData->nonlinearExpressions->nl[ i]->osExpressionTree = new OSExpressionTree();
02881                 // create a variable nl node for x0
02882                 nlNodeVariablePoint = new OSnLNodeVariable();
02883                 nlNodeVariablePoint->idx = varOneIndexes[ i];
02884                 // give this variable the coefficient
02885                 nlNodeVariablePoint->coef = coefficients[ i];
02886                 nlNodeVec.push_back( nlNodeVariablePoint);
02887                 // create the nl node for x1
02888                 nlNodeVariablePoint = new OSnLNodeVariable();
02889                 nlNodeVariablePoint->idx = varTwoIndexes[ i];
02890                 nlNodeVec.push_back( nlNodeVariablePoint);
02891                 // create the nl node for *
02892                 nlNodePoint = new OSnLNodeTimes();
02893                 nlNodeVec.push_back( nlNodePoint);
02894                 // the vectors are in postfix format
02895                 // now the expression tree
02896                 instanceData->nonlinearExpressions->nl[ i]->osExpressionTree->m_treeRoot =
02897                         nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
02898                 nlNodeVec.clear();
02899                 }
02900         return true;
02901 }//setQuadraticTermsInNonlinearExpressions
02902 
02903 bool OSInstance::initializeNonLinearStructures( ){
02904         std::map<int, OSExpressionTree*>::iterator posMapExpTree;
02905         if( m_bNonLinearStructuresInitialized == true) return true;
02906         if( m_bProcessVariables == false) processVariables();
02907         if( m_bProcessObjectives == false) processObjectives();
02908         if( m_bProcessConstraints == false) processConstraints();
02909         m_iVariableNumber = instanceData->variables->numberOfVariables;
02910         m_iConstraintNumber = instanceData->constraints->numberOfConstraints;
02911         m_iObjectiveNumber = instanceData->objectives->numberOfObjectives;
02912         // get all of the expression trees
02913         if( m_bProcessExpressionTrees == false) getAllNonlinearExpressionTrees();
02914         // before proceeding get a copy of the map of the Expression Trees
02915         if( m_bDuplicateExpressionTreesMap == false) duplicateExpressionTreesMap();
02916         // now create all of the variable maps for each expression tree
02917         for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree){  
02918                 (posMapExpTree->second)->getVariableIndiciesMap() ;
02919         }
02920         // add the quadratic terms if necessary
02921         if(getNumberOfQuadraticTerms() > 0) addQTermsToExressionTree();
02922         // now get the map of all nonlinear variables
02923         getAllNonlinearVariablesIndexMap( );
02924         getDenseObjectiveCoefficients();
02925         m_mdConstraintFunctionValues = new double[ this->instanceData->constraints->numberOfConstraints];
02926         m_mdObjectiveFunctionValues = new double[ this->instanceData->objectives->numberOfObjectives];
02927         //m_mdObjGradient = new double[ this->instanceData->variables->numberOfVariables];
02928         m_bNonLinearStructuresInitialized = true;
02929         m_bProcessVariables = true;
02930         m_bProcessObjectives = true;
02931         m_bProcessConstraints = true;
02932         m_bProcessExpressionTrees = true;
02933         m_bDuplicateExpressionTreesMap = true;
02934         return true;
02935 }
02936 
02937 SparseJacobianMatrix *OSInstance::getJacobianSparsityPattern( ){
02938         //if(this->getVariableNumber() == 0 || this->getConstraintNumber() == 0) return NULL;
02939         // if already called return the sparse Jacobian
02940         // it is important that this method NOT get called twice -- if
02941         // there are linear terms in <linearConstraintCoefficients> that
02942         // also appear in <nonlinearExpressions> then they will keep getting added
02943         // to the modified expession tree with each call to this method
02944         if( m_bSparseJacobianCalculated == true) return m_sparseJacMatrix;
02945         //std::cout << "INSIDE GET JACOBIAN SPARSITY PATTERN" << std::endl;
02946         // determine if we are in column or row major
02947         getLinearConstraintCoefficientMajor();
02948         // make sure the data structures have been inialized
02949         if( m_bNonLinearStructuresInitialized == false) initializeNonLinearStructures( );
02950         try{
02951                 if( m_bColumnMajor == true){
02952                          if( getSparseJacobianFromColumnMajor( ) == false) throw ErrorClass("An error occurred in getSpareJacobianFromColumnMajor");
02953                 }
02954                 else {
02955                         if( getSparseJacobianFromRowMajor( ) == false) throw ErrorClass("An error occurred in getSpareJacobianFromRowMajor");
02956                 }
02957         }
02958         catch(const ErrorClass& eclass){
02959                 throw ErrorClass(  eclass.errormsg); 
02960         }
02961         // now fill in the arrays of the sparseJacMatrix
02962         m_sparseJacMatrix = new SparseJacobianMatrix();
02963         // we point to memory already created so don't 
02964         // destroy during garbage collection
02965         m_sparseJacMatrix->bDeleteArrays = false;
02966         m_sparseJacMatrix->valueSize =  m_iJacValueSize;
02967         m_sparseJacMatrix->starts = m_miJacStart;
02968         m_sparseJacMatrix->conVals = m_miJacNumConTerms;
02969         m_sparseJacMatrix->indexes = m_miJacIndex;
02970         m_sparseJacMatrix->values = m_mdJacValue;
02971         m_bSparseJacobianCalculated = true;
02972         return m_sparseJacMatrix;
02973 }//getJacobianSparsityPatter
02974 
02975 bool OSInstance::addQTermsToExressionTree(){
02976         int i, k, idx;
02977         // get the number of qTerms
02978         int numQTerms = instanceData->quadraticCoefficients->numberOfQuadraticTerms;    
02979         if(numQTerms <= 0 || m_bQTermsAdded == true) return true;
02980         OSnLNodeVariable* nlNodeVariableOne;
02981         OSnLNodeVariable* nlNodeVariableTwo;
02982         OSnLNodeTimes* nlNodeTimes;
02983         OSnLNodePlus* nlNodePlus;
02984         OSExpressionTree* expTree;
02985         getQuadraticTerms();    
02986         //std::cout << "PROCESSING QUADRATIC TERMS" << std::endl;
02987         for(i = 0; i < numQTerms; i++){
02988                 idx = m_quadraticTerms->rowIndexes[ i];
02989                 //std::cout << "PROCESSING QTERM  = "  << i <<std::endl;
02990                 // see if row idx is in the expression tree
02991                 if( m_mapExpressionTreesMod.find( idx) != m_mapExpressionTreesMod.end() ) {
02992                         // row idx is in the expression tree
02993                         // add the qTerm in row idx  to the expression tree     
02994                         // define two new OSnLVariable nodes, an OSnLnodeTimes, and OSnLnodePlus 
02995                         nlNodeVariableOne = new OSnLNodeVariable();
02996                         nlNodeVariableOne->idx = m_quadraticTerms->varOneIndexes[ i];
02997                         // see if the variable indexed by nlNodeVariableOne->idx is in the expression tree for row idx
02998                         // if not, add to mapVarIdx
02999                         expTree = m_mapExpressionTreesMod[ idx];
03000                         if(  expTree->m_bIndexMapGenerated == false) expTree->getVariableIndiciesMap();
03001                         if( (*expTree->mapVarIdx).find( nlNodeVariableOne->idx) == (*expTree->mapVarIdx).end()  ){
03002                                 // add to map
03003                                 k = (*expTree->mapVarIdx).size();
03004                                 (*expTree->mapVarIdx)[ nlNodeVariableOne->idx] =  k + 1;
03005                                 #ifdef DEBUG
03006                                 std::cout << "ADDED THE FOLLOWING VAIRABLE TO THE MAP" <<  nlNodeVariableOne->idx << std::endl;
03007                                 #endif
03008                         }
03009                         nlNodeVariableOne->coef = m_quadraticTerms->coefficients[ i];
03010                         nlNodeVariableTwo = new OSnLNodeVariable();
03011                         nlNodeVariableTwo->idx = m_quadraticTerms->varTwoIndexes[ i];
03012                         // see if the variable indexed by nlNodeVariableTwo->idx is in the expression tree for row idx
03013                         // if not, add to mapVarIdx
03014                         if( (*expTree->mapVarIdx).find( nlNodeVariableTwo->idx) == (*expTree->mapVarIdx).end()  ){
03015                                 // add to map
03016                                 k = (*expTree->mapVarIdx).size();
03017                                 (*expTree->mapVarIdx)[ nlNodeVariableTwo->idx] =  k + 1;
03018                                 #ifdef DEBUG
03019                                 std::cout << "ADDED THE FOLLOWING VAIRABLE TO THE MAP" <<  nlNodeVariableTwo->idx << std::endl;
03020                                 #endif
03021                         }
03022                         nlNodeVariableTwo->coef = 1.;
03023                         // now multiply the two new variable nodes together
03024                         nlNodeTimes = new OSnLNodeTimes();
03025                         nlNodeTimes->m_mChildren[ 0] = nlNodeVariableOne;
03026                         nlNodeTimes->m_mChildren[ 1] = nlNodeVariableTwo;               
03027                         // now add the result to the expression tree
03028                         nlNodePlus = new OSnLNodePlus();
03029                         nlNodePlus->m_mChildren[ 0] = expTree->m_treeRoot;
03030                         nlNodePlus->m_mChildren[ 1] = nlNodeTimes;
03031                         //expTree = new OSExpressionTree();
03032                         expTree->m_treeRoot = nlNodePlus ;
03033                         // get rid of old variable map
03034                         if(expTree->m_bIndexMapGenerated == true){
03035                                 delete expTree->mapVarIdx;
03036                                 expTree->mapVarIdx = NULL;
03037                                 expTree->m_bIndexMapGenerated = false;
03038                         }       
03039                         //expTree->m_bIndexMapGenerated = false;
03040                         //m_mapExpressionTreesMod[ idx ]  = expTree;    
03041                         //expTree->mapVarIdx = m_mapExpressionTreesMod[ idx]->mapVarIdx;
03042                 }
03043                 else{
03044                         // create the quadratic expression to add to the expression tree
03045                         nlNodeVariableOne = new OSnLNodeVariable();
03046                         nlNodeVariableOne->idx = m_quadraticTerms->varOneIndexes[ i];
03047                         nlNodeVariableOne->coef = m_quadraticTerms->coefficients[ i];
03048                         nlNodeVariableTwo = new OSnLNodeVariable();
03049                         nlNodeVariableTwo->idx = m_quadraticTerms->varTwoIndexes[ i];
03050                         nlNodeVariableTwo->coef = 1.;
03051                         // now multiply the two new variable nodes together
03052                         nlNodeTimes = new OSnLNodeTimes();
03053                         nlNodeTimes->m_mChildren[ 0] = nlNodeVariableOne;
03054                         nlNodeTimes->m_mChildren[ 1] = nlNodeVariableTwo;
03055                         // create a new expression tree corresponding to row idx.
03056                         expTree = new OSExpressionTree();                                               
03057                         expTree->m_treeRoot = nlNodeTimes ;
03058                         expTree->mapVarIdx = expTree->getVariableIndiciesMap();         
03059                         m_mapExpressionTreesMod[ idx ]  = expTree;
03060                         if(idx < 0){
03061                                 m_iObjectiveNumberNonlinear++;
03062                                 m_bProcessExpressionTrees = true;
03063                         }
03064                         else{
03065                                 m_iConstraintNumberNonlinear++;
03066                                 m_bProcessExpressionTrees = true;
03067                         }
03068                         #ifdef DEBUG
03069                         std::cout << "NUMBER OF EXPRESSION TREES = "  << m_mapExpressionTreesMod.size() <<std::endl;
03070                         std::cout << "NUMBER OF NONLINEAR OBJECTIVES = "  << getNumberOfNonlinearObjectives() <<std::endl;
03071                         #endif
03072                 } 
03073                 // if there were no nonlinear terms make this the expression tree
03074                 if(m_iNonlinearExpressionNumber <= 0) m_mapExpressionTrees = m_mapExpressionTreesMod;
03075                 m_bQTermsAdded =true;
03076         }
03077         return true;
03078 }
03079 
03080 double OSInstance::calculateFunctionValue(int idx, double *x, bool new_x){
03081         try{
03082 
03083                 int i, j;
03084                 double dvalue = 0;
03085                 if( m_binitForAlgDiff == false) initForAlgDiff();
03086                 if( m_bSparseJacobianCalculated == false) getJacobianSparsityPattern();
03087                 if(idx >= 0){ // we have a constraint
03088                         // make sure the index idx is valid
03089                         if( getConstraintNumber() <= idx  ) throw 
03090                         ErrorClass("constraint index not valid in OSInstance::calculateFunctionValue");
03091                         if( new_x == false) return *(m_mdConstraintFunctionValues + idx);
03092                         // get the nonlinear part
03093                         if( m_mapExpressionTreesMod.find( idx) != m_mapExpressionTreesMod.end() ){
03094                                 dvalue = m_mapExpressionTreesMod[ idx]->calculateFunction( x,  new_x);
03095                                 //dvalue = vdFunVals[ idx + 1];
03096                         }
03097                         // now the linear part
03098                         // be careful, loop over only the constant terms in sparseJacMatrix
03099                         i = m_sparseJacMatrix->starts[ idx];
03100                         j = m_sparseJacMatrix->starts[ idx + 1 ];
03101                         //while ( i <  j &&  (i - m_sparseJacMatrix->starts[ idx])  < m_sparseJacMatrix->conVals[ idx] ){
03102                         while ( (i - m_sparseJacMatrix->starts[ idx])  < m_sparseJacMatrix->conVals[ idx] ){
03103                                 //std::cout << "m_sparseJacMatrix->values[ i] " << m_sparseJacMatrix->values[ i] << std::endl;
03104                                 //std::cout << "m_sparseJacMatrix->indexes[ i] " << m_sparseJacMatrix->indexes[ i] << std::endl;
03105                                 dvalue += m_sparseJacMatrix->values[ i]*x[ m_sparseJacMatrix->indexes[ i] ];
03106                                 i++;
03107                         }       
03108                         // add in the constraint function constant
03109                         dvalue += m_mdConstraintConstants[ idx ];
03110                         return dvalue;
03111                 }
03112                 else{ // we have an objective function
03113                         // make sure the index idx is valid
03114                         if( getObjectiveNumber() <= ( abs( idx) - 1) ) throw 
03115                         ErrorClass("objective function index not valid in OSInstance::calculateFunctionValue");
03116                         if( new_x == false) return *(m_mdObjectiveFunctionValues + ( abs( idx) - 1));
03117                         // get the nonlinear part
03118                         if( m_mapExpressionTreesMod.find( idx) != m_mapExpressionTreesMod.end() ){
03119                                 dvalue = m_mapExpressionTreesMod[ idx]->calculateFunction( x,  new_x);
03120                         }
03121                         // get linear part
03122                         SparseVector **objCoef = getObjectiveCoefficients();
03123                         SparseVector *obj = objCoef[ abs( idx) - 1];
03124                         for(i = 0; i < obj->number; i++){
03125                                 dvalue += x[ obj->indexes[i]]*(obj->values[ i]);
03126                         }
03127                         // add in the objective function constant
03128                         dvalue += m_mdObjectiveConstants[ abs( idx) - 1 ];
03129                         // get the coefficients for objective function idx
03130                         *(m_mdObjectiveFunctionValues + ( abs( idx) - 1)) = dvalue;
03131                         return *(m_mdObjectiveFunctionValues + ( abs( idx) - 1));
03132                 }
03133         }
03134         catch(const ErrorClass& eclass){
03135                 throw ErrorClass( eclass.errormsg);
03136         } 
03137 }//calculateFunctionValue
03138 
03139 
03140 double *OSInstance::calculateAllConstraintFunctionValues( double* x, double *objLambda, double *conLambda,
03141         bool new_x, int highestOrder){  
03142         try{
03143                 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated)  ) 
03144                         getIterateResults(x, objLambda, conLambda, new_x,  highestOrder);
03145         }
03146         catch(const ErrorClass& eclass){
03147                 throw ErrorClass( eclass.errormsg);
03148         } 
03149         return m_mdConstraintFunctionValues;
03150 }//calculateAllConstraintFunctionValues
03151 
03152 
03153 double *OSInstance::calculateAllConstraintFunctionValues(double* x, bool new_x){
03154         try{
03155                 m_iHighestOrderEvaluated = -1;
03156                 if( new_x == false) return m_mdConstraintFunctionValues;
03157                 int idx, numConstraints;
03158                 numConstraints = getConstraintNumber();
03159                 // loop over all constraints
03160                 for(idx = 0; idx < numConstraints; idx++){
03161                         m_mdConstraintFunctionValues[ idx]  = calculateFunctionValue(idx, x, new_x);    
03162                 }
03163                 
03164         }
03165         catch(const ErrorClass& eclass){
03166                 throw ErrorClass( eclass.errormsg);
03167         } 
03168         return m_mdConstraintFunctionValues;    
03169 }//end calculateAllConstraintFunctionValues
03170 
03171 
03172 double *OSInstance::calculateAllObjectiveFunctionValues( double* x, double *objLambda, double *conLambda,
03173         bool new_x, int highestOrder){  
03174         try{
03175                 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated)  ) 
03176                         getIterateResults(x, objLambda, conLambda, new_x,  highestOrder);
03177         }
03178         catch(const ErrorClass& eclass){
03179                 throw ErrorClass( eclass.errormsg);
03180         } 
03181         return m_mdObjectiveFunctionValues;
03182 }//calculateAllConstraintFunctionValues
03183 
03184 
03185 double *OSInstance::calculateAllObjectiveFunctionValues( double* x, bool new_x){        
03186         try{
03187                 m_iHighestOrderEvaluated = -1;
03188                 
03189                 if( new_x == false) return m_mdObjectiveFunctionValues;
03190                 int idx, numObjectives;
03191                 numObjectives = getObjectiveNumber();
03192                 // loop over all constraints
03193                 for(idx = 0; idx < numObjectives; idx++){
03194                         m_mdObjectiveFunctionValues[ idx]  = calculateFunctionValue(-idx -1, x, new_x); 
03195                 }
03196         }
03197         catch(const ErrorClass& eclass){
03198                 throw ErrorClass( eclass.errormsg);
03199         } 
03200         return m_mdObjectiveFunctionValues;
03201 }//calculateAllObjectiveFunctionValues
03202 
03203 
03204 SparseJacobianMatrix *OSInstance::calculateAllConstraintFunctionGradients(double* x, double *objLambda, double *conLambda,
03205                 bool new_x, int highestOrder){
03206         try{
03207                 if(highestOrder < 1 ) throw ErrorClass("When calling calculateAllConstraintFunctionGradients highestOrder should be 1 or 2");
03208                 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated)  ) 
03209                         getIterateResults(x, objLambda, conLambda,  new_x,  highestOrder);
03210         }//end try
03211         catch(const ErrorClass& eclass){
03212                 throw ErrorClass( eclass.errormsg);
03213         } 
03214         return m_sparseJacMatrix;
03215 }//calculateAllConstraintFunctionGradients      
03216 
03217 
03218 
03219 SparseVector *OSInstance::calculateConstraintFunctionGradient(double* x, double *objLambda, double *conLambda,
03220                 int idx, bool new_x, int highestOrder){
03221         try{
03222                 if(highestOrder < 1 ) throw ErrorClass("When calling calculateConstraintFunctionGradient highestOrder should be 1 or 2");
03223                 if(idx < 0 || idx >= instanceData->constraints->numberOfConstraints ) 
03224                         throw ErrorClass("invalid index passed to calculateConstraintFunctionGrad");
03225                 SparseVector *sp;
03226                 sp = new SparseVector();
03227                 sp->bDeleteArrays = true;
03228                 int i;
03229                 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated)  ) 
03230                         getIterateResults(x, objLambda, conLambda,  new_x,  highestOrder);
03231                 sp->number = m_miJacStart[ idx + 1] - m_miJacStart[ idx];
03232                 sp->values = new double[ sp->number];
03233                 sp->indexes = new int[ sp->number];
03234                 for(i = 0; i < sp->number; i++){
03235                         sp->values[ i] = m_mdJacValue[ m_miJacStart[ idx] +  i];
03236                         sp->indexes[ i] = m_miJacIndex[ m_miJacStart[ idx] +  i];
03237                 }
03238                 return sp;
03239         }
03240         catch(const ErrorClass& eclass){
03241                 throw ErrorClass( eclass.errormsg);
03242         } 
03243 }//calculateConstraintFunctionGradient
03244 
03245 
03246 SparseVector *OSInstance::calculateConstraintFunctionGradient(double* x, int idx, bool new_x){
03247         try{
03248                 if(idx < 0 || idx >= instanceData->constraints->numberOfConstraints ) 
03249                         throw ErrorClass("invalid index passed to calculateConstraintFunctionGrad");
03250                 SparseVector *sp;
03251                 sp = new SparseVector();
03252                 sp->bDeleteArrays = true;
03253                 int i;
03254                 if( new_x == true || (1 > m_iHighestOrderEvaluated)  ) 
03255                         getIterateResults(x, NULL, NULL,  new_x,  1);
03256                 sp->number = m_miJacStart[ idx + 1] - m_miJacStart[ idx];
03257                 sp->values = new double[ sp->number];
03258                 sp->indexes = new int[ sp->number];
03259                 for(i = 0; i < sp->number; i++){
03260                         sp->values[ i] = m_mdJacValue[ m_miJacStart[ idx] +  i];
03261                         sp->indexes[ i] = m_miJacIndex[ m_miJacStart[ idx] + i];
03262                 }
03263                 return sp;
03264         }
03265         catch(const ErrorClass& eclass){
03266                 throw ErrorClass( eclass.errormsg);
03267         } 
03268 }//calculateConstraintFunctionGradient
03269 
03270 
03271 double **OSInstance::calculateAllObjectiveFunctionGradients(double* x, double *objLambda, double *conLambda,
03272                 bool new_x, int highestOrder){
03273         try{
03274                 if(highestOrder < 1 ) throw ErrorClass("When calling calculateAllObjectiveFunctionGradients highestOrder should be 1 or 2");
03275                 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated)  ) {
03276                         std::map<int, OSExpressionTree*>::iterator posMapExpTree;
03277                         for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree){
03278                                 if(posMapExpTree->first < 0){ // this nonlinear expression indexes an objective function
03279                                         m_mmdObjGradient[ abs( posMapExpTree->first) - 1 ] = calculateObjectiveFunctionGradient(x, objLambda, conLambda, 
03280                                                         posMapExpTree->first, new_x, highestOrder);
03281                                 }       
03282                         }
03283                 }
03284         }
03285         catch(const ErrorClass& eclass){
03286                 throw ErrorClass( eclass.errormsg);
03287         } 
03288         return m_mmdObjGradient;
03289 }// calculateAllObjectiveFunctionGradients
03290 
03291 double *OSInstance::calculateObjectiveFunctionGradient(double* x, double *objLambda, double *conLambda,
03292                 int objIdx, bool new_x, int highestOrder){
03293         /* if we are just doing an objective function gradient we should do a zero order forward sweep
03294          * and a reverse first order reverse sweep
03295          */
03296         try{
03297                 if(highestOrder < 1 ) throw ErrorClass("When calling calculateObjectiveFunctionGradient highestOrder should be 1 or 2");        
03298                 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated)  ) {
03299                         int domainIdx = 0;      
03300                         std::map<int, OSExpressionTree*>::iterator posMapExpTree;
03301                         std::map<int, int>::iterator posVarIndexMap;            
03302                         int iHighestOrderEvaluatedStore;
03303                         unsigned int i;
03304                         iHighestOrderEvaluatedStore = m_iHighestOrderEvaluated;
03305                         for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree){
03306                                 //kipp: modify for more than one obj
03307                                 if(posMapExpTree->first == objIdx){
03308                                         if( new_x == true ){
03309                                                 if( m_vdX.size() > 0) m_vdX.clear();
03310                                                 for(posVarIndexMap = m_mapAllNonlinearVariablesIndex.begin(); posVarIndexMap != m_mapAllNonlinearVariablesIndex.end(); ++posVarIndexMap){
03311                                                         m_vdX.push_back( x[ posVarIndexMap->first]) ;
03312                                                 }
03313                                                 if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true )  && (m_mapExpressionTreesMod.size() > 0) ) {
03314                                                         createOSADFun( m_vdX);
03315                                                 }                                               
03316                                         }       
03317                                         
03318                                         if(( new_x == true ) || (m_iHighestOrderEvaluated < 0) )this->forwardAD(0, m_vdX);
03319                                         
03320                                         if(( new_x == true ) || (m_iHighestOrderEvaluated < 1) ) {
03321                                                 m_vdRangeUnitVec[ domainIdx] = 1.;
03322                                                 m_vdYjacval = this->reverseAD(1, m_vdRangeUnitVec);
03323                                                 for(i = 0; i < m_iNumberOfNonlinearVariables; i++){
03324                                                         m_mmdObjGradient[ abs( objIdx) - 1 ][ m_miNonLinearVarsReverseMap[ i]] = m_vdYjacval[ i] + 
03325                                                                 m_mmdDenseObjectiveCoefficients[  abs( objIdx) - 1][ m_miNonLinearVarsReverseMap[ i]];
03326                                                 }
03327                                         }
03328                                         m_iHighestOrderEvaluated = iHighestOrderEvaluatedStore;
03329                                         m_vdRangeUnitVec[ domainIdx] = 0.;
03330                                         // exit the loop
03331                                         break;
03332                                 }
03333                                 domainIdx++;
03334                         }
03335                 }                       
03336         }
03337         catch(const ErrorClass& eclass){
03338                 throw ErrorClass( eclass.errormsg);
03339         } 
03340         return m_mmdObjGradient[abs( objIdx) - 1];
03341 }//calculateObjectiveFunctionGradient
03342 
03343 
03344 double *OSInstance::calculateObjectiveFunctionGradient(double* x, int objIdx, bool new_x){
03345         try{
03346                 int domainIdx = 0;      
03347                 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
03348                 std::map<int, int>::iterator posVarIndexMap;    
03349                 unsigned int i;
03350                 int  iHighestOrderEvaluatedStore;       
03351                 iHighestOrderEvaluatedStore = m_iHighestOrderEvaluated;
03352                 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree){
03353                         if(posMapExpTree->first == objIdx){
03354                                 if( new_x == true ){
03355                                         if( m_vdX.size() > 0) m_vdX.clear();
03356                                         for(posVarIndexMap = m_mapAllNonlinearVariablesIndex.begin(); posVarIndexMap != m_mapAllNonlinearVariablesIndex.end(); ++posVarIndexMap){
03357                                                 m_vdX.push_back( x[ posVarIndexMap->first]) ;
03358                                         }
03359                                         if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true )  && (m_mapExpressionTreesMod.size() > 0) ) {
03360                                                 createOSADFun( m_vdX);
03361                                         }                                               
03362                                 }       
03363                                 
03364                                 if(( new_x == true ) || (m_iHighestOrderEvaluated < 0) )this->forwardAD(0, m_vdX);
03365                                 
03366                                 if(( new_x == true ) || (m_iHighestOrderEvaluated < 1) ) {
03367                                         m_vdRangeUnitVec[ domainIdx] = 1.;
03368                                         m_vdYjacval = this->reverseAD(1, m_vdRangeUnitVec);
03369                                         for(i = 0; i < m_iNumberOfNonlinearVariables; i++){
03370                                                 m_mmdObjGradient[ abs( objIdx) - 1 ][ m_miNonLinearVarsReverseMap[ i]] = m_vdYjacval[ i] + 
03371                                                         m_mmdDenseObjectiveCoefficients[  abs( objIdx) - 1][ m_miNonLinearVarsReverseMap[ i]];
03372                                         }
03373                                 }
03374                                 m_iHighestOrderEvaluated = iHighestOrderEvaluatedStore;
03375                                 m_vdRangeUnitVec[ domainIdx] = 0.;
03376                                 // exit the loop
03377                                 break;
03378                         }
03379                         domainIdx++;
03380                 }
03381         }       
03382         catch(const ErrorClass& eclass){
03383                 throw ErrorClass( eclass.errormsg);
03384         } 
03385         return m_mmdObjGradient[abs( objIdx) - 1];
03386 }//calculateObjectiveFunctionGradient   
03387 
03388 SparseHessianMatrix *OSInstance::calculateLagrangianHessian( double* x, double *objLambda, double *conLambda,
03389         bool new_x, int highestOrder){
03390         try{
03391                 if(highestOrder != 2 ) throw ErrorClass("When calling calculateLagrangianHessian highestOrder should be 2");
03392                 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated)  ) {
03393                         //std::cout  << "CALL getIterateResults() FROM calculateLagrangianHessain" << std::endl;
03394                         getIterateResults(x, objLambda, conLambda,  new_x,  highestOrder);
03395                 }
03396         }
03397         catch(const ErrorClass& eclass){
03398                 throw ErrorClass( eclass.errormsg);
03399         } 
03400         return m_LagrangianSparseHessian;
03401 }//calculateLagrangianHessian
03402 
03403 SparseHessianMatrix *OSInstance::calculateHessian(double* x, int idx, bool new_x){
03404         try{
03405                 if(idx < -instanceData->objectives->numberOfObjectives || idx >= instanceData->constraints->numberOfConstraints ) 
03406                         throw ErrorClass("invalid index passed to calculateHessian");
03407                 double *objLambda = new double[ getObjectiveNumber() ];
03408                 double *conLambda = new double[ getConstraintNumber() ];
03409                 //std::cout << "NUMBER OF OBJECTIVES = " << getObjectiveNumber() << std::endl;
03410                 //std::cout << "NUMBER OF CONSTRAINTS = " << getConstraintNumber() << std::endl;
03411                 int i;
03412                 // initialize all to zero
03413                 for(i = 0; i < getObjectiveNumber(); i++){
03414                         objLambda[ i] = 0.0;
03415                 }
03416                 for(i = 0; i < getConstraintNumber(); i++){
03417                         conLambda[ i] = 0.0;
03418                 }
03419                 // see if we have the index of an objective function or a constraint
03420                 // and more corresponding component 1.0
03421                 if(idx < 0){
03422                         objLambda[ abs(idx) - 1] = 1.0;
03423                 }
03424                 else{
03425                         conLambda[ idx] = 1.0;
03426                 }
03427                 if( new_x == true || (2 > m_iHighestOrderEvaluated)  ) {
03428                         getIterateResults(x, objLambda, conLambda, new_x,  2);
03429 //                      std::cout  << "CALL getIterateResults() FROM calculateHessian" << std::endl;
03430                 }
03431                 delete[] objLambda;
03432                 delete[] conLambda;
03433         }
03434         catch(const ErrorClass& eclass){
03435                 throw ErrorClass( eclass.errormsg);
03436         } 
03437         return m_LagrangianSparseHessian;
03438 }//calculateHessian                     
03439 
03440 
03441 
03442 bool OSInstance::getSparseJacobianFromColumnMajor( ){
03443         // we assume column major matrix
03444         if( m_bColumnMajor == false) return false;
03445         int iNumRowStarts = getConstraintNumber() + 1;  
03446         int i,j, iTemp;
03447         int iNumVariableStarts = getVariableNumber() ;
03448         int *start = NULL;
03449         int *index = NULL;
03450         double *value = NULL;
03451         if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0){
03452                 start = this->instanceData->linearConstraintCoefficients->start->el;
03453                 index = this->instanceData->linearConstraintCoefficients->rowIdx->el;
03454                 value = this->instanceData->linearConstraintCoefficients->value->el;
03455         }
03456         m_miJacStart = new int[ iNumRowStarts];
03457         m_miJacNumConTerms = new int[ getConstraintNumber()];
03458         OSnLNodePlus *nlNodePlus;
03459         OSnLNodeVariable *nlNodeVariable;
03460         OSExpressionTree *expTree = NULL;
03461         // now initialize starts and variable index maps 
03462         for ( i = 0; i < iNumRowStarts; i++){                   
03463                 m_miJacStart [ i ] = 0;
03464                 // map the variables  in the nonlinear rows
03465                 if( m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() ){
03466                         // the following is equivalent to  m_treeRoot->getVariableIndexMap( i);
03467                         m_mapExpressionTreesMod[ i]->getVariableIndiciesMap();
03468                         
03469                 }
03470         }
03471         // only execute the following code if there are linear constraint coefficients
03472         if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0){
03473                 // i is indexing columns (variables) and j is indexing row numbers 
03474                 for (i = 0; i < iNumVariableStarts; i++){       
03475                         for (j = start[i]; j < start[ i + 1 ]; j++){
03476                                 // index[ j] is a row index, we have just found an occurance of row index[j]
03477                                 // therefore we increase by 1 (or push back) the start of the row indexed by index[j] + 1, 
03478                                 // i.e. the start of the next row
03479                                 // check to see if variable i is linear/constant in the row index[ j] 
03480                                 // if so, increment m_miJacStart[ index[j] + 1]
03481                                 //
03482                                 if( (m_mapExpressionTreesMod.find( index[ j]) != m_mapExpressionTreesMod.end() ) &&
03483                                         ( (*m_mapExpressionTreesMod[ index[ j]]->mapVarIdx).find( i) != (*m_mapExpressionTreesMod[ index[ j]]->mapVarIdx).end()) ){
03484                                         // variable i is appears in the expression tree for row index[ j]
03485                                         // add the coefficient corresponding to variable i in row index[ j] to the expression tree      
03486                                         // define a new OSnLVariable and OSnLnodePlus 
03487                                         // don't add a zero
03488                                         if( value[j] > 0 || value[j] < 0){
03489                                                 expTree = m_mapExpressionTreesMod[ index[j]  ];
03490                                                 nlNodeVariable = new OSnLNodeVariable();
03491                                                 nlNodeVariable->coef = value[ j];
03492                                                 nlNodeVariable->idx = i;
03493                                                 nlNodePlus = new OSnLNodePlus();
03494                                                 nlNodePlus->m_mChildren[ 0] = m_mapExpressionTreesMod[ index[ j] ]->m_treeRoot;
03495                                                 nlNodePlus->m_mChildren[ 1] = nlNodeVariable;
03496                                                 //expTree = new OSExpressionTree();
03497                                                 expTree->m_treeRoot = nlNodePlus ;
03498                                                 //expTree->mapVarIdx = m_mapExpressionTreesMod[ index[ j]]->mapVarIdx;
03499                                                 //m_mapExpressionTreesMod[ index[ j] ]  = expTree;      
03500                                                 //std::cout << m_mapExpressionTreesMod[ index[ j] ]->m_treeRoot->getNonlinearExpressionInXML() << std::endl;    
03501                                                 //std::cout << m_mapExpressionTrees[ index[ j] ]->m_treeRoot->getNonlinearExpressionInXML() << std::endl;
03502                                         }
03503                                 }
03504                                 else{ 
03505                                         m_miJacStart[ index[j] + 1] ++;
03506                                 }                               
03507                         }
03508                 }
03509         }
03510         // at this point, m_miJacStart[ i] holds the number of columns with a linear/constant nonzero in row i - 1
03511         // we are not done with the start indicies, if we are here, and we
03512         // knew the correct starting point of row i -1, the correct starting point
03513         // for row i is m_miJacStart[i] + m_miJacStart [i - 1]
03514         m_miJacStart[0] = 0;
03515         for (i = 1; i < iNumRowStarts; i++ ){
03516                 m_miJacNumConTerms[ i - 1] = m_miJacStart[i];
03517                 if( m_mapExpressionTreesMod.find( i - 1) != m_mapExpressionTreesMod.end() ){
03518                         m_miJacStart[i] += (m_miJacStart[i - 1] + (*m_mapExpressionTreesMod[ i - 1]->mapVarIdx).size() );
03519                 }
03520                 else{
03521                         m_miJacStart[i] += m_miJacStart[i - 1];
03522                 }       
03523         }
03524         // dimension miIndex and mdValue here
03525         m_iJacValueSize =       m_miJacStart[ iNumRowStarts - 1];
03526         m_miJacIndex = new int[  m_iJacValueSize];
03527         m_mdJacValue = new double[ m_iJacValueSize ];
03528         // now get the values of the constant terms if there are any
03529         if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0){
03530                 // loop over variables  
03531                 for (i = 0; i < iNumVariableStarts; i++){
03532                         // get row indices and values of the A matrix
03533                         // kipp -- should we have a check to see if start[i+1] > start[i]
03534                         for (j = start[i]; j < start[ i + 1 ]; j++){
03535                                 // store this variable index in every row where the variable appears
03536                                 // however, don't store this as constant term if it appears in mapVarIdx
03537                                 if( (m_mapExpressionTreesMod.find( index[ j]) == m_mapExpressionTreesMod.end() ) ||
03538                                         ( (*m_mapExpressionTreesMod[ index[ j]]->mapVarIdx).find( i) == (*m_mapExpressionTreesMod[ index[ j]]->mapVarIdx).end()) ){
03539                                         iTemp = m_miJacStart[ index[j]];
03540                                         m_miJacIndex[ iTemp] = i;
03541                                         m_mdJacValue[ iTemp] = value[j];
03542                                         m_miJacStart[ index[j]]++;      
03543                                 }               
03544                         }                       
03545                 } 
03546         }
03547         //
03548         std::map<int, int>::iterator posVarIdx;
03549         // m_miJacStart[ i] is now equal to the correct m_miJacStart[ i] + m_miJacNumConTerms[ i], so readjust
03550         for (i = 0; i < iNumRowStarts - 1; i++ ){
03551                 m_miJacStart[ i] = m_miJacStart [ i] - m_miJacNumConTerms[ i] ; 
03552                 iTemp = m_miJacStart[ i] + m_miJacNumConTerms[ i];
03553                 // if the row is in the list of expression trees read in idicies and values
03554                 if( m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() ){
03555                         for(posVarIdx = (*m_mapExpressionTreesMod[ i]->mapVarIdx).begin(); posVarIdx 
03556                         != (*m_mapExpressionTreesMod[ i]->mapVarIdx).end(); ++posVarIdx){
03557                                 m_miJacIndex[ iTemp] = posVarIdx->first;
03558                                 m_mdJacValue[ iTemp] = 0;
03559                                 iTemp++;
03560                         }
03561                 }
03562         }
03563         #ifdef DEBUG
03564         std::cout << "HERE ARE ROW STARTS:" << std::endl;
03565         for (i = 0; i < iNumRowStarts; i++ ){
03566                 std::cout <<  m_miJacStart[ i] << "  "; 
03567         }
03568         std::cout << std::endl << std::endl;
03569         std::cout << "HERE ARE VARIABLE INDICIES:" << std::endl;
03570         for (i = 0; i < m_miJacStart[ iNumRowStarts - 1]; i++ ){
03571                 std::cout <<  m_miJacIndex[ i] << "  "; 
03572         }
03573         std::cout << std::endl << std::endl;
03574         std::cout << "HERE ARE VALUES:" << std::endl;
03575         for (i = 0; i < m_miJacStart[ iNumRowStarts - 1]; i++ ){
03576                 std::cout <<  m_mdJacValue[ i] << "  "; 
03577         }
03578         std::cout << std::endl << std::endl;
03579 
03580         std::cout << "HERE ARE NUMBER OF CONSTANT TERMS:" << std::endl;
03581         for (i = 0; i < iNumRowStarts - 1; i++ ){
03582                 std::cout <<  m_miJacNumConTerms[ i ] << "  ";  
03583         }
03584         std::cout << std::endl << std::endl;
03585         #endif
03586         return true;
03587 }//getSparseJacobianFromColumnMajor
03588 
03589 
03590 
03591 bool OSInstance::getSparseJacobianFromRowMajor( ){
03592         // we assume row major matrix
03593         if( m_bColumnMajor == true) return false;
03594         int iNumJacRowStarts = getConstraintNumber() + 1;
03595         std::map<int, int>::iterator posVarIdx; 
03596         int i,j, k;
03597         int *start = NULL;
03598         int *index = NULL;
03599         double *value = NULL;
03600         if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0){
03601                 start = this->instanceData->linearConstraintCoefficients->start->el;
03602                 index = this->instanceData->linearConstraintCoefficients->colIdx->el;
03603                 value = this->instanceData->linearConstraintCoefficients->value->el;
03604         }
03605         m_miJacStart = new int[ iNumJacRowStarts];
03606         m_miJacNumConTerms = new int[ getConstraintNumber()];
03607         OSnLNodePlus *nlNodePlus;
03608         OSnLNodeVariable *nlNodeVariable;
03609         //OSExpressionTree *expTree = NULL;
03610         // now initialize starts and variable index maps 
03611         for ( i = 0; i < iNumJacRowStarts; i++){                        
03612                 m_miJacStart [ i ] = 0;
03613                 // map the variables  in the nonlinear rows
03614                 if( m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() ){
03615                         // the following is equivalent to  m_treeRoot->getVariableIndexMap( i);
03616                         m_mapExpressionTreesMod[ i]->getVariableIndiciesMap();
03617                         
03618                 }
03619         }
03620         int loopLimit =  getConstraintNumber();
03621         // only execute the following code if there are linear constraint coefficients
03622         // determine the number of terms in constraint with constant partial derivative
03623         if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0){
03624                 // i is indexing rows (constrains) and j is indexing column numbers 
03625                 for (i = 0; i < loopLimit; i++){
03626                         m_miJacNumConTerms[ i] = 0;
03627                         for (j = start[i]; j < start[ i + 1 ]; j++){
03628                                 // determine if variable index[j] appears in the Expression Tree for row i
03629                                 // if we pass if test below then variable i is in the expresssion tree and we add
03630                                 // the linear term to the expession tree
03631                                 if( (m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() ) &&
03632                                         ( (*m_mapExpressionTreesMod[ i]->mapVarIdx).find( index[ j]) != (*m_mapExpressionTreesMod[ i]->mapVarIdx).end()) ){
03633                                         // variable index[ j] appears in the expression tree for row i
03634                                         // add the coefficient corresponding to variable index[j] in row i to the expression tree       
03635                                         // define a new OSnLVariable and OSnLnodePlus 
03636                                         if(value[ j] > 0 || value[j] < 0){
03637                                                 nlNodeVariable = new OSnLNodeVariable();
03638                                                 nlNodeVariable->coef = value[ j];
03639                                                 nlNodeVariable->idx = index[ j];
03640                                                 nlNodePlus = new OSnLNodePlus();
03641                                                 nlNodePlus->m_mChildren[ 0] = m_mapExpressionTreesMod[ i ]->m_treeRoot;
03642                                                 nlNodePlus->m_mChildren[ 1] = nlNodeVariable;
03643                                                 //expTree = new OSExpressionTree();
03644                                                 //expTree->m_treeRoot = nlNodePlus ;
03645                                                 //expTree->mapVarIdx = m_mapExpressionTreesMod[ i]->mapVarIdx;
03646                                                 //m_mapExpressionTreesMod[ i ]  = expTree;      
03647                                                 m_mapExpressionTreesMod[ i ]->m_treeRoot = nlNodePlus;
03648                                         }
03649                                 }
03650                                 else{ 
03651                                         //the partial derivative of variable j in row i is constant
03652                                         m_miJacNumConTerms[ i]++;
03653                                 }                               
03654                         }
03655                 }
03656         }
03657         //
03658         m_miJacStart[0] = 0;
03659         for (i = 1; i < iNumJacRowStarts; i++ ){
03660                 if( m_mapExpressionTreesMod.find( i - 1) != m_mapExpressionTreesMod.end() ){
03661                         m_miJacStart[i] = m_miJacStart[i - 1] + (m_miJacNumConTerms[ i - 1] + (*m_mapExpressionTreesMod[ i - 1]->mapVarIdx).size() );
03662                 }
03663                 else{
03664                         m_miJacStart[i] = m_miJacStart[i - 1] + m_miJacNumConTerms[ i - 1];
03665                 }       
03666         }
03667         // we know how many constant terms and size of arrays
03668         // dimension miIndex and mdValue here
03669         m_iJacValueSize =       m_miJacStart[ iNumJacRowStarts - 1];
03670         m_miJacIndex = new int[  m_iJacValueSize];
03671         m_mdJacValue = new double[ m_iJacValueSize ];
03672         // now loop again and put in values and indicies
03673         // first put in the constant terms
03674         if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0){
03675                 for (i = 0; i < loopLimit; i++){
03676                         k = 0;
03677                         for (j = start[i]; j < start[ i + 1 ]; j++){
03678                                 if( (m_mapExpressionTreesMod.find( i) == m_mapExpressionTreesMod.end() ) ||
03679                                         ( (*m_mapExpressionTreesMod[ i]->mapVarIdx).find( index[ j]) == (*m_mapExpressionTreesMod[ i]->mapVarIdx).end()) ){
03680                                                 m_miJacIndex[ m_miJacStart[i] + k ] = index[ j];
03681                                                 m_mdJacValue[ m_miJacStart[i] + k ] = value[ j];
03682                                                 k++;
03683                                         }                       
03684                         }
03685                 }
03686         }
03687         // put in terms from the modified nonlinear expression tree
03688         for (i = 0; i < loopLimit; i++ ){
03689                 k = m_miJacStart[i] + m_miJacNumConTerms[ i ];
03690                 // if the row is in the list of expression trees read in idicies and values
03691                 if( m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() ){
03692                         for(posVarIdx = (*m_mapExpressionTreesMod[ i]->mapVarIdx).begin(); posVarIdx 
03693                         != (*m_mapExpressionTreesMod[ i]->mapVarIdx).end(); ++posVarIdx){
03694                                 m_miJacIndex[ k] = posVarIdx->first;
03695                                 m_mdJacValue[ k] = 0;
03696                                 k++;
03697                         }
03698                 }
03699         }
03700         #ifdef DEBUG
03701         std::cout << "HERE ARE ROW STARTS:" << std::endl;
03702         for (i = 0; i < iNumJacRowStarts; i++ ){
03703                 std::cout <<  m_miJacStart[ i] << "  "; 
03704         }
03705         std::cout << std::endl << std::endl;
03706         std::cout << "HERE ARE VARIABLE INDICIES:" << std::endl;
03707         for (i = 0; i < m_miJacStart[ iNumJacRowStarts - 1]; i++ ){
03708                 std::cout <<  m_miJacIndex[ i] << "  "; 
03709         }
03710         std::cout << std::endl << std::endl;
03711         std::cout << "HERE ARE VALUES:" << std::endl;
03712         for (i = 0; i < m_miJacStart[ iNumJacRowStarts - 1]; i++ ){
03713                 std::cout <<  m_mdJacValue[ i] << "  "; 
03714         }
03715         std::cout << std::endl << std::endl;
03716 
03717         std::cout << "HERE ARE NUMBER OF CONSTANT TERMS:" << std::endl;
03718         for (i = 0; i < iNumJacRowStarts - 1; i++ ){
03719                 std::cout <<  m_miJacNumConTerms[ i ] << "  ";  
03720         }
03721         std::cout << std::endl << std::endl;
03722         #endif
03723         return true;
03724 }//getSparseJacobianFromRowMajor
03725 
03726 OSExpressionTree* OSInstance::getLagrangianExpTree( ){
03727         if( m_bLagrangianExpTreeCreated == true) return m_LagrangianExpTree;
03728         // we calculate the Lagrangian for all the objectives and constraints
03729         // with nonlinear terms
03730         // first initialize everything for nonlinear work
03731         if(m_bSparseJacobianCalculated == false) getJacobianSparsityPattern( ); 
03732         std::map<int, OSExpressionTree*>::iterator posMapExpTree;
03733         OSnLNodeTimes* nlNodeTimes = NULL;
03734         OSnLNodeVariable* nlNodeVariable = NULL;
03735         OSnLNodeSum* nlNodeSum = NULL;
03736         int numChildren = 0;
03737         int rowIdx;
03738         // create the sum node
03739         nlNodeSum = new OSnLNodeSum();
03740         nlNodeSum->inumberOfChildren = m_mapExpressionTreesMod.size();
03741         nlNodeSum->m_mChildren = new OSnLNode*[ nlNodeSum->inumberOfChildren];
03742         // create and expression tree for the sum node
03743         m_LagrangianExpTree = new OSExpressionTree();
03744         m_LagrangianExpTree->m_treeRoot = nlNodeSum;
03745         // now create the children of the sum node
03746         for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree){
03747                 // this variable is the Lagrange multiplier
03748                 nlNodeVariable = new OSnLNodeVariable();
03749                 nlNodeVariable->coef = 1.;
03750                 // get the correct index --
03751                 // for rowIdx = 0, ..., m - 1 set idx = numVar + rowIdx
03752                 rowIdx = posMapExpTree->first;
03753                 if(rowIdx >= 0){
03754                         nlNodeVariable->idx = instanceData->variables->numberOfVariables + rowIdx;
03755                 }
03756                 else{
03757                         nlNodeVariable->idx = instanceData->variables->numberOfVariables + 
03758                         instanceData->constraints->numberOfConstraints + (abs(rowIdx) - 1);
03759                 }
03760                 // now create a times multiply the new variable times the root of the expression tree
03761                 nlNodeTimes = new OSnLNodeTimes();
03762                 nlNodeTimes->m_mChildren[ 0] = nlNodeVariable;
03763                 nlNodeTimes->m_mChildren[ 1] = m_mapExpressionTreesMod[ posMapExpTree->first ]->m_treeRoot;     
03764                 // the times node is the new child
03765                 nlNodeSum->m_mChildren[ numChildren] = nlNodeTimes;
03766                 numChildren++;
03767         }       
03768         // get a variable index map for the expression tree
03769         m_LagrangianExpTree->getVariableIndiciesMap();
03770         // print out the XML for this puppy
03771         //std::cout << "Here comes the Lagrangian Tree" << std::endl;
03772         //std::cout << m_LagrangianExpTree->m_treeRoot->getNonlinearExpressionInXML() << std::endl;
03773         //
03774         m_bLagrangianExpTreeCreated = true;
03775         return m_LagrangianExpTree;
03776 }//getLagrangianExpTree
03777 
03778 std::map<int, int> OSInstance::getAllNonlinearVariablesIndexMap( ){
03779         if(m_bAllNonlinearVariablesIndex == true) return m_mapAllNonlinearVariablesIndex;
03780         //loop over the map of expression tree and get a unique listing of all variables
03781         // put these in the map m_mapAllNonlinearVariablesIndex
03782         std::map<int, OSExpressionTree*>::iterator posMapExpTree;
03783         std::map<int, int>::iterator posVarIdx;
03784         OSExpressionTree *expTree;
03785         for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree){
03786                 // get the index map for the expression tree
03787                 expTree = posMapExpTree->second;
03788                 if(expTree->m_bIndexMapGenerated == false)expTree->getVariableIndiciesMap();
03789                 for(posVarIdx = (*expTree->mapVarIdx).begin(); posVarIdx != (*expTree->mapVarIdx).end(); ++posVarIdx){
03790                         if( m_mapAllNonlinearVariablesIndex.find( posVarIdx->first) == m_mapAllNonlinearVariablesIndex.end() ){
03791                         // add the variable to the Lagragian map
03792                         m_mapAllNonlinearVariablesIndex[ posVarIdx->first] = 1;
03793                         }
03794                 }
03795         }
03796         m_miNonLinearVarsReverseMap = new int[m_mapAllNonlinearVariablesIndex.size()];
03797         // now order appropriately
03798         int kount = 0;
03799         //std::cout << "HERE IS THE LAGRANGIANN VARIABLE MAPPING" << std::endl;
03800         for(posVarIdx = m_mapAllNonlinearVariablesIndex.begin(); posVarIdx !=m_mapAllNonlinearVariablesIndex.end(); ++posVarIdx){
03801                 posVarIdx->second = kount;
03802                 m_miNonLinearVarsReverseMap[ kount] = posVarIdx->first;
03803                 kount++;
03804                 #ifdef DEBUG
03805                 std::cout <<  "POSITION FIRST =  "  << posVarIdx->first ;
03806                 std::cout <<  "   POSITION SECOND = "  << posVarIdx->second << std::endl;
03807                 #endif
03808         }
03809         m_iNumberOfNonlinearVariables = kount;
03810         //std::cout <<  "NUMBER OF NONLINEAR VARIABLES =  "  << kount ;
03811         m_bAllNonlinearVariablesIndex = true;
03812         return m_mapAllNonlinearVariablesIndex;
03813 }//getAllNonlinearVariablesIndexMap     
03814 
03815 SparseHessianMatrix* OSInstance::getLagrangianHessianSparsityPattern( ){
03816         // fill in the nonzeros in the sparse Hessian
03817         if( m_bLagrangianSparseHessianCreated == true) return m_LagrangianSparseHessian;
03818         if( m_iNumberOfNonlinearVariables == 0) return NULL;
03819         if( m_binitForAlgDiff == false ) initForAlgDiff();
03820         unsigned int i = 0;
03821         unsigned int j;
03822         int numNonz = 0;
03823         // Create the CppAD function if necessary
03824         //
03825         std::vector<double> vx;
03826         std::map<int, int>::iterator posMap1, posMap2;  
03827         if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true )  && (m_mapExpressionTreesMod.size() > 0) ) {
03828                 for(posMap1 = m_mapAllNonlinearVariablesIndex.begin(); posMap1 != m_mapAllNonlinearVariablesIndex.end(); ++posMap1){
03829                         vx.push_back( 1.0) ;
03830                 }
03831                 createOSADFun( vx);
03832         }
03833         
03834         
03835         /*
03836         //
03837         // Use CppAD to do a forward sparsity calculation
03838         std::vector<bool> r(m_iNumberOfNonlinearVariables * m_iNumberOfNonlinearVariables);
03839         unsigned int j;
03840         for(i = 0; i < m_iNumberOfNonlinearVariables; i++) { 
03841                 for(j = 0; j < m_iNumberOfNonlinearVariables; j++)
03842                         r[ i * m_iNumberOfNonlinearVariables + j ] = false;
03843                         r[ i * m_iNumberOfNonlinearVariables + i] = true;
03844         }       
03845         // compute sparsity pattern for J(x) = F^{(1)} (x)
03846         (*Fad).ForSparseJac(m_iNumberOfNonlinearVariables, r);
03847         //
03848         //now the second derivative
03849         unsigned int m = m_mapExpressionTreesMod.size();
03850         std::vector<bool> e( m);
03851         //Vector s(m);
03852         for(i = 0; i < m; i++) e[i] = true;
03853         std::cout << "Computing Sparse Hessian" << std::endl;
03854         //m_vbLagHessNonz holds the sparsity pattern Lagrangian of the Hessian
03855         m_vbLagHessNonz = (*Fad).RevSparseHes(m_iNumberOfNonlinearVariables, e);
03856         for(i = 0; i < m_iNumberOfNonlinearVariables; i++){
03857                 //std::cout << "Row " << i << "  of Hessian " << std::endl;
03858                 for(j = i; j < m_iNumberOfNonlinearVariables; j++){
03859                         if(m_vbLagHessNonz[ i*m_iNumberOfNonlinearVariables + j]  == true) numNonz++;
03860                         //std::cout << m_vbLagHessNonz[ i*m_iNumberOfNonlinearVariables + j] <<  "  " ;
03861                 }
03862                 //std::cout << std::endl;
03863         }
03864         
03865         */
03866         // note this function call also fills in m_vbLagHessNonz
03867         numNonz = getADSparsityHessian();
03868         
03869         //std::cout << "Lagrangian Hessian Nonzeros = " << numNonz << std::endl;
03870         i = 0;
03871         // now that we have the dimension create SparseHessianMatrix (upper triangular)
03872         m_LagrangianSparseHessian = new SparseHessianMatrix();
03873         m_LagrangianSparseHessian->bDeleteArrays = true;
03874         m_LagrangianSparseHessian->hessDimension = numNonz;
03875         //m_LagrangianSparseHessian->hessDimension = m_vbLagHessNonz.size();
03876         m_LagrangianSparseHessian->hessRowIdx = new int[m_LagrangianSparseHessian->hessDimension];
03877         m_LagrangianSparseHessian->hessColIdx = new int[m_LagrangianSparseHessian->hessDimension];
03878         m_LagrangianSparseHessian->hessValues = new double[m_LagrangianSparseHessian->hessDimension];
03879         //std::cout << "HESSIAN DIMENSION = " << m_LagrangianSparseHessian->hessDimension << std::endl;
03880         numNonz = 0;
03881         for(posMap1 = m_mapAllNonlinearVariablesIndex.begin(); posMap1 != m_mapAllNonlinearVariablesIndex.end(); ++posMap1){
03882                 //std::cout << "posMap1->first  " << posMap1->first << std::endl;
03883                 j = i;
03884                 for(posMap2 = posMap1; posMap2 != m_mapAllNonlinearVariablesIndex.end(); ++posMap2){
03885                         //std::cout << "posMap2->first  " << posMap2->first << std::endl;
03886                         if(m_vbLagHessNonz[ i*m_iNumberOfNonlinearVariables + j] == true){
03887                                 *(m_LagrangianSparseHessian->hessRowIdx + numNonz) = posMap1->first;
03888                                 *(m_LagrangianSparseHessian->hessColIdx + numNonz) = posMap2->first;
03889                                 numNonz++;
03890                         }
03891                         //std::cout << m_vbLagHessNonz[ i*m_iNumberOfNonlinearVariables + j] <<  "  " << std::endl;
03892                         j++;
03893                 }
03894                 i++;
03895         }
03896         #ifdef DEBUG
03897         std::cout << "HESSIAN SPARSITY PATTERN" << std::endl;
03898         int kj;
03899         for(kj = 0; kj < m_LagrangianSparseHessian->hessDimension; kj++){
03900                 std::cout <<  "Row Index = " << *(m_LagrangianSparseHessian->hessRowIdx + kj) << std::endl;
03901                 std::cout <<  "Column Index = " << *(m_LagrangianSparseHessian->hessColIdx + kj) << std::endl;
03902         }
03903         #endif
03904         //
03905         m_bLagrangianSparseHessianCreated = true;
03906         return m_LagrangianSparseHessian;
03907 }//getLagrangianHessianSparsityPattern
03908 
03909 
03910 void OSInstance::duplicateExpressionTreesMap(){
03911         //std::cout << "I AM IN DUPLICATE EXPRSSION TREES MAP" << std::endl;
03912         // we do this so that we can keep the integrity of the original formulation
03913         if(m_bDuplicateExpressionTreesMap == false){ 
03914                 // first make sure the map was created
03915                 if( m_bProcessExpressionTrees == false) getAllNonlinearExpressionTrees();
03916                 m_mapExpressionTreesMod = m_mapExpressionTrees;
03917                 m_bDuplicateExpressionTreesMap = true;
03918                 return;
03919         }
03920         else{
03921                 return;
03922         }
03923 }//duplicateExpressionTreesMap
03924 
03925 
03926 
03927 bool OSInstance::getIterateResults( double *x, double *objLambda, double* conMultipliers, 
03928                 bool new_x, int highestOrder){
03929         try{ 
03930                 if( m_binitForAlgDiff == false) initForAlgDiff();
03931                 std::map<int, int>::iterator posVarIndexMap;
03932                 
03933                 if(new_x == true){
03934                         if( m_vdX.size() > 0) m_vdX.clear();
03935                         for(posVarIndexMap = m_mapAllNonlinearVariablesIndex.begin(); posVarIndexMap != m_mapAllNonlinearVariablesIndex.end(); ++posVarIndexMap){
03936                                 m_vdX.push_back( x[ posVarIndexMap->first]) ;
03937                         }
03938                         if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true )  && (m_mapExpressionTreesMod.size() > 0) ) {
03939                                 createOSADFun( m_vdX);
03940                         }       
03941                 }
03942                 else{ // make sure vector not empty -- this could happen if we have linear obj and nonlinear constraints
03943                         if( m_vdX.size() == 0) {
03944                                 for(posVarIndexMap = m_mapAllNonlinearVariablesIndex.begin(); posVarIndexMap != m_mapAllNonlinearVariablesIndex.end(); ++posVarIndexMap){
03945                                         m_vdX.push_back( x[ posVarIndexMap->first]) ;
03946                                 }
03947                                 if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true )  && (m_mapExpressionTreesMod.size() > 0) ) {
03948                                         createOSADFun( m_vdX);
03949                                 }       
03950                         }
03951                 }
03952                 switch( highestOrder){          
03953                         case 0: 
03954                                 if(new_x == true || m_iHighestOrderEvaluated < 0){      
03955                                         if(bUseExpTreeForFunEval == true){
03956                                                 calculateAllConstraintFunctionValues( x, new_x);
03957                                                 calculateAllObjectiveFunctionValues( x, new_x);
03958                                         }
03959                                         else{
03960                                                 getZeroOrderResults(x, objLambda, conMultipliers);
03961                                         }
03962 
03963                                 }
03964                                 break;  
03965                         case 1:
03966                                 if(new_x == true || m_iHighestOrderEvaluated < 0)       
03967                                         getZeroOrderResults(x, objLambda, conMultipliers);
03968                                 if(new_x == true || m_iHighestOrderEvaluated < 1)       
03969                                         getFirstOrderResults(x, objLambda, conMultipliers);
03970                                 break;
03971                         case 2: 
03972                                 if(new_x == true || m_iHighestOrderEvaluated < 0)       
03973                                         getZeroOrderResults(x, objLambda, conMultipliers);
03974                                 if(new_x == true || m_iHighestOrderEvaluated < 2)       
03975                                         getSecondOrderResults(x, objLambda, conMultipliers);
03976                                 break;
03977                         default:
03978                                 throw ErrorClass("Derivative should be order 0, 1, or 2");      
03979                 }//end switch
03980                 return true;
03981         }
03982         catch(const ErrorClass& eclass){
03983                 throw ErrorClass( eclass.errormsg);
03984         }  
03985 }//end getIterateResults
03986 
03987 
03988 bool OSInstance::getZeroOrderResults(double *x, double *objLambda, double *conMultipliers){
03989         try{ 
03990                 // initialize everything
03991                 int i, j, rowNum, objNum;
03992                 if( m_mapExpressionTreesMod.size() > 0){
03993                         m_vdYval = this->forwardAD(0, m_vdX);   
03994                 }
03995                 // now get all function and constraint values using forward result
03996                 for(rowNum = 0; rowNum < m_iConstraintNumber; rowNum++){
03997                         m_mdConstraintFunctionValues[ rowNum] = 0.0;
03998                         if( m_mapExpressionTreesMod.find( rowNum) != m_mapExpressionTreesMod.end() ){
03999                                 m_mdConstraintFunctionValues[ rowNum] = m_vdYval[  m_mapOSADFunRangeIndex[ rowNum]];
04000                         }
04001                         // now the linear part
04002                         // be careful, loop over only the constant terms in sparseJacMatrix
04003                         i = m_sparseJacMatrix->starts[ rowNum];
04004                         j = m_sparseJacMatrix->starts[ rowNum + 1 ];
04005                         while ( (i - m_sparseJacMatrix->starts[ rowNum])  < m_sparseJacMatrix->conVals[ rowNum] ){
04006                                 m_mdConstraintFunctionValues[ rowNum] += m_sparseJacMatrix->values[ i]*x[ m_sparseJacMatrix->indexes[ i] ];
04007                                 i++;
04008                         }       
04009                         // add in the constraint function constant
04010                         m_mdConstraintFunctionValues[ rowNum] += m_mdConstraintConstants[ rowNum ];
04011                         #ifdef DEBUG
04012                         std::cout << "Constraint " <<  rowNum << " function value =  " << m_mdConstraintFunctionValues[ rowNum ] << std::endl;
04013                         #endif
04014                 }
04015                 // now get the objective function values from the forward result
04016                 for(objNum = 0; objNum < m_iObjectiveNumber; objNum++){
04017                         m_mdObjectiveFunctionValues[ objNum] = 0.0;
04018                         if( m_mapExpressionTreesMod.find( -objNum -1) != m_mapExpressionTreesMod.end() ){
04019                                 m_mdObjectiveFunctionValues[ objNum] = m_vdYval[ objNum];
04020                         }
04021                         for(i = 0; i < m_iVariableNumber; i++){
04022                                 m_mdObjectiveFunctionValues[ objNum] += m_mmdDenseObjectiveCoefficients[ objNum][i]*x[ i];
04023                         }
04024                         #ifdef DEBUG
04025                         std::cout << "Objective " << objNum << " function value =  " << m_mdObjectiveFunctionValues[ objNum] << std::endl;
04026                         #endif
04027                 }
04028         return true;
04029         }//end try
04030         catch(const ErrorClass& eclass){
04031                 throw ErrorClass( eclass.errormsg);
04032         }  
04033 }//end getZeroOrderResults
04034 
04035 
04036 
04037 bool OSInstance::getFirstOrderResults(double *x, double *objLambda, double *conMultipliers){
04038         try{
04039                 // initialize everything
04040                 unsigned int i, j;
04041                 int rowNum,  jacIndex;
04042                 unsigned int jstart, jend;
04043                 int idx;
04044                 OSExpressionTree *expTree = NULL;
04045                 int domainIdx = 0;      
04046                 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
04047                 std::map<int, int>::iterator posVarIdx;
04048                 
04054                 if(m_iNumberOfNonlinearVariables >= m_mapExpressionTreesMod.size() ){
04055                         // calculate the gradient by doing a reverse sweep over each row
04056                         // loop over the constraints that have a nonlinear term and get their gradients
04057                         for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree){
04058                                 idx = posMapExpTree->first;
04059                                 // we are considering only constraints, not objective function
04060                                 if(idx >= 0){
04061                                         m_vdRangeUnitVec[ domainIdx] = 1.;
04062                                         m_mapExpressionTreesMod[ idx]->getVariableIndiciesMap(); 
04063                                         m_vdYjacval = this->reverseAD(1, m_vdRangeUnitVec);
04064                                         // check size
04065                                         jstart = m_miJacStart[ idx] + m_miJacNumConTerms[ idx];
04066                                         jend = m_miJacStart[ idx + 1 ];
04067                                         if( (*m_mapExpressionTreesMod[ idx]->mapVarIdx).size() != (jend - jstart)) throw 
04068                                         ErrorClass("number of partials not consistent");
04069                                         j = 0;
04070                                         jacIndex = 0;
04071                                         for(posVarIdx = m_mapAllNonlinearVariablesIndex.begin(); posVarIdx 
04072                                                 != m_mapAllNonlinearVariablesIndex.end(); ++posVarIdx){
04073                                                 //std::cout << "Constraint Function Jacobian Values" << "For Constraint  " << idx  << std::endl;
04074                                                 //std::cout << "Jac Val for index " << posVarIdx->first  << " = " << m_vdYjacval[ jacIndex] << std::endl;
04075                                                 //if(m_miJacIndex[ jstart] != posVarIdx->first) throw ErrorClass("error calculating Jacobian matrix");
04076                                                 // we are working with variable posVarIdx->first in the original variable space
04077                                                 // we need to see which variable this is in the individual constraint map
04078                                                 if( (*m_mapExpressionTreesMod[ idx]->mapVarIdx).find( posVarIdx->first) != (*m_mapExpressionTreesMod[ idx]->mapVarIdx).end()){
04079                                                         m_mdJacValue[ jstart] = m_vdYjacval[ jacIndex];
04080                                                         jstart++;
04081                                                         j++;
04082                                                 }
04083                                                 jacIndex++;
04084                                         }
04085                                         
04086                                         m_vdRangeUnitVec[ domainIdx] = 0.;
04087                                         domainIdx++;
04088                                 }
04089                                 else{    // we have an objective function
04090                                         
04091 
04092                                         domainIdx++;
04093                                 }
04094                         }
04095                 }
04096                 else{  
04097                         // calculate the gradients using a forward sweep over all the variables.                
04098                         for(i = 0; i < m_iNumberOfNonlinearVariables; i++){
04099                                 m_vdDomainUnitVec[i] = 1.;     
04100                                 rowNum = 0;
04101                                 if( m_mapExpressionTreesMod.size() > 0){          
04102                                         m_vdYjacval = this->forwardAD(1, m_vdDomainUnitVec);
04103                                 } 
04104                                 // fill in Jacobian here, we have column i 
04105                                 // start Jacobian calculation
04106                                 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree){
04107                                         idx = posMapExpTree->first;
04108                                         // we are considering only constraints, not objective function
04109                                         if(idx >= 0){
04110                                                 //figure out original variable this corresponds to
04111                                                 //then use (*m_mapExpressionTreesMod[ idx]->mapVarIdx) to figure out which variable it is within row idx
04112                                                 //m_mapAllNonlinearVariablesIndex
04113                                                 //std::cout << "This is the following variable in the expression tree  " <<  (*m_mapExpressionTreesMod[ idx]->mapVarIdx)[ m_miNonLinearVarsReverseMap[ i]]<< std::endl;                         
04114                                                 expTree = m_mapExpressionTreesMod[ idx];                
04115                                                 if( (*expTree->mapVarIdx).find( m_miNonLinearVarsReverseMap[ i]) != (*expTree->mapVarIdx).end()  ){             
04116                                                         jacIndex = (*m_mapExpressionTreesMod[ idx]->mapVarIdx)[ m_miNonLinearVarsReverseMap[ i]];
04117                                                         jstart = m_miJacStart[ idx] + m_miJacNumConTerms[ idx];
04118                                                         // kipp change 1 to number of objective functions
04119                                                         m_mdJacValue[ jstart + jacIndex] = m_vdYjacval[m_iObjectiveNumberNonlinear + rowNum];
04120                                                 }
04121                                                 rowNum++;
04122                                         }//end Jacobian calculation
04123                         }                       
04124                         //
04125                         m_vdDomainUnitVec[i] = 0.;
04126                         }
04127                 }
04128                 #ifdef DEBUG
04129                 int k;
04130                 std::cout  << "JACOBIAN DATA " << std::endl;
04131                 for(idx = 0; idx < m_iConstraintNumber; idx++){
04132                         for(k = *(m_sparseJacMatrix->starts + idx); k < *(m_sparseJacMatrix->starts + idx + 1); k++){
04133                                 std::cout << "row idx = " << idx <<  "  col idx = "<< *(m_sparseJacMatrix->indexes + k)
04134                                 << " value = " << *(m_sparseJacMatrix->values + k) << std::endl;
04135                         }
04136                 }
04137                 #endif
04138                 return true;
04139         }//end try
04140         catch(const ErrorClass& eclass){
04141                 throw ErrorClass( eclass.errormsg);
04142         } 
04143 }// end getFirstOrderResults
04144                         
04145 
04146 bool OSInstance::getSecondOrderResults(double *x, double *objLambda, double *conMultipliers){
04147         try{
04148                 // initialize everything
04149                 unsigned int i, j;
04150                 int rowNum,  jacIndex;
04151                 int jstart,  idx;
04152                 OSExpressionTree *expTree = NULL;
04153                 int hessValuesIdx = 0;  
04154                 if( m_bLagrangianSparseHessianCreated == false) getLagrangianHessianSparsityPattern( );
04155                 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
04156                 std::map<int, int>::iterator posVarIndexMap;
04157                 if( objLambda == NULL) throw ErrorClass("must have a multiplier for the objective function even if zero when calling getSecondOrderResults");
04158                 if( conMultipliers == NULL) throw ErrorClass("cannot have a null vector of lagrange multipliers when calling getSecondOrderResults -- okay if  zero");
04159                 if( m_vdLambda.size() > 0) m_vdLambda.clear();
04160                 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree){  
04161                         if( posMapExpTree->first >= 0){
04162                                 m_vdLambda.push_back( conMultipliers[ posMapExpTree->first]);
04163                         }
04164                         else{
04165                                 // kipp correct when there is more than one obj
04166                                 m_vdLambda.push_back( objLambda[ abs(posMapExpTree->first) - 1] );
04167                         }
04168                 }
04169                 for(i = 0; i < m_iNumberOfNonlinearVariables; i++){ 
04170                         m_vdDomainUnitVec[i] = 1.;     
04171                         rowNum = 0;
04172                         if( m_mapExpressionTreesMod.size() > 0){          
04173                                 m_vdYjacval = this->forwardAD(1, m_vdDomainUnitVec);
04174                         } 
04175                         // fill in Jacobian here, we have column i 
04176                         // start Jacobian calculation
04177                         for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree){
04178                                 idx = posMapExpTree->first;
04179                                 // we are considering only constraints, not objective function
04180                                 if(idx >= 0){
04181                                         //figure out original variable this corresponds to
04182                                         //then use (*m_mapExpressionTreesMod[ idx]->mapVarIdx) to figure out which variable it is within row idx
04183                                         //m_mapAllNonlinearVariablesIndex
04184                                         //std::cout << "This is the following variable in the expression tree  " <<  (*m_mapExpressionTreesMod[ idx]->mapVarIdx)[ m_miNonLinearVarsReverseMap[ i]]<< std::endl;                         
04185                                         expTree = m_mapExpressionTreesMod[ idx];                
04186                                         if( (*expTree->mapVarIdx).find( m_miNonLinearVarsReverseMap[ i]) != (*expTree->mapVarIdx).end()  ){             
04187                                                 jacIndex = (*m_mapExpressionTreesMod[ idx]->mapVarIdx)[ m_miNonLinearVarsReverseMap[ i]];
04188                                                 jstart = m_miJacStart[ idx] + m_miJacNumConTerms[ idx];
04189                                                 m_mdJacValue[ jstart + jacIndex] = m_vdYjacval[m_iObjectiveNumberNonlinear + rowNum];
04190                                         }
04191                                         rowNum++;
04192                                 }//end Jacobian calculation
04193                                 else{
04194                                         // see if we have the objective function of interest
04195                                         //kipp fix if more than one obj
04196                                                 m_mmdObjGradient[  (abs( idx) - 1)][ m_miNonLinearVarsReverseMap[ i]] = m_vdYjacval[ (abs( idx) - 1)] + 
04197                                                 m_mmdDenseObjectiveCoefficients[  (abs( idx) - 1)][ m_miNonLinearVarsReverseMap[ i]];                                   
04198                         }//end Obj gradient calculation 
04199                 }                       
04200                 // now calculate the Hessian
04201                 if( m_mapExpressionTreesMod.size() > 0){   
04202                         m_vdw = reverseAD(2, m_vdLambda);   // derivative of partial
04203                 }
04204                 for(j = i; j < m_iNumberOfNonlinearVariables; j++){
04205                         if( m_vbLagHessNonz[i*m_iNumberOfNonlinearVariables + j] == true){
04206                                 m_LagrangianSparseHessian->hessValues[ hessValuesIdx] =  m_vdw[  j*2 + 1];
04207                                 #ifdef DEBUG
04208                                 std::cout << "reverse 2 " << m_LagrangianSparseHessian->hessValues[ hessValuesIdx] << std::endl;
04209                                 #endif
04210                                 hessValuesIdx++;
04211                         }
04212                 }
04213                 //
04214                 //
04215                 m_vdDomainUnitVec[i] = 0.;
04216         }
04217         #ifdef DEBUG
04218         int k;
04219         std::cout  << "JACOBIAN DATA " << std::endl;
04220         for(idx = 0; idx < m_iConstraintNumber; idx++){
04221                 for(k = *(m_sparseJacMatrix->starts + idx); k < *(m_sparseJacMatrix->starts + idx + 1); k++){
04222                         std::cout << "row idx = " << idx <<  "  col idx = "<< *(m_sparseJacMatrix->indexes + k)
04223                         << " value = " << *(m_sparseJacMatrix->values + k) << std::endl;
04224                 }
04225         }
04226         #endif
04227         return true;
04228         }//end try
04229         catch(const ErrorClass& eclass){
04230                 throw ErrorClass( eclass.errormsg);
04231         } 
04232 }// end getSecondOrderResults
04233 
04234 bool OSInstance::initForAlgDiff(){
04235         if( m_binitForAlgDiff == true ) return true;
04236         initializeNonLinearStructures( );
04237         initObjGradients();
04238         getAllNonlinearVariablesIndexMap( );
04239         //if(m_bSparseJacobianCalculated  == false) getJacobianSparsityPattern();
04240         //see if we need to retape 
04241         //loop over expression tree and see if one requires it
04242         std::map<int, OSExpressionTree*>::iterator posMapExpTree;
04243         for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree){
04244                 if(posMapExpTree->second->bADMustReTape == true) m_bCppADMustReTape = true;
04245         }                               
04246 
04247         #ifdef DEBUG
04248         std::cout << "RETAPE ==  " << m_bCppADMustReTape << std::endl;
04249         #endif
04250         unsigned int i;
04251         for(i = 0; i < m_iNumberOfNonlinearVariables; i++){
04252                 m_vdDomainUnitVec.push_back( 0.0 );
04253         }
04254         for(i = 0; i < m_mapExpressionTreesMod.size(); i++){
04255                 m_vdRangeUnitVec.push_back( 0.0 );
04256         }
04257         m_binitForAlgDiff = true;
04258         //m_bSparseJacobianCalculated = true;
04259         //m_bProcessExpressionTrees = true;
04260         return true;
04261 }//end initForAlgDiff
04262 
04263 bool OSInstance::initObjGradients(){
04264         int i, j;
04265         int m, n;
04266         m = getObjectiveNumber();
04267         n = getVariableNumber();
04268         getDenseObjectiveCoefficients();
04269         if(m <= 0){
04270                 m_mmdObjGradient = NULL;
04271                 return true;
04272         }
04273         m_mmdObjGradient = new double*[m];
04274         for(i = 0; i < m; i++){
04275                 m_mmdObjGradient[i] = new double[n];
04276                 for(j = 0; j < n; j++){
04277                         m_mmdObjGradient[i][j] =  m_mmdDenseObjectiveCoefficients[ i][j];
04278                         #ifdef DEBUG
04279                         std::cout << "m_mmdObjGradient[i][j] = " << m_mmdObjGradient[i][j]  << std::endl;
04280                         #endif
04281                 }
04282         }
04283         return true;
04284 }//end initObjGradients
04293 bool OSInstance::setTimeDomain(std::string format)
04294 {   if ((format != "stages") && (format != "interval") && (format != "none")) 
04295                 return false;
04296         if (instanceData->timeDomain == NULL)
04297         {       instanceData->timeDomain = new TimeDomain();
04298         }
04299         if (format == "stages")
04300         {       if (instanceData->timeDomain->interval != NULL)
04301                 {       delete instanceData->timeDomain->interval;
04302                         instanceData->timeDomain->interval = NULL;
04303                 }
04304                 if (instanceData->timeDomain->stages == NULL)
04305                         instanceData->timeDomain->stages = new TimeDomainStages();
04306                 m_sTimeDomainFormat = format;
04307         }
04308         if (format == "interval")
04309         {       if (instanceData->timeDomain->stages != NULL)
04310                 {       delete instanceData->timeDomain->stages;
04311                         instanceData->timeDomain->stages = NULL;
04312                 }
04313                 if (instanceData->timeDomain->interval == NULL)
04314                         instanceData->timeDomain->interval = new TimeDomainInterval();
04315                 m_sTimeDomainFormat = format;
04316         }
04317         if (format == "none")
04318         {       if (instanceData->timeDomain->stages != NULL)
04319                 {       delete instanceData->timeDomain->stages;
04320                         instanceData->timeDomain->stages = NULL;
04321                 }
04322                 if (instanceData->timeDomain->interval != NULL)
04323                 {       delete instanceData->timeDomain->interval;
04324                         instanceData->timeDomain->interval = NULL;
04325                 }
04326                 m_sTimeDomainFormat = "";
04327         }
04328         return true;
04329 } //end setTimeDomain
04330 
04334 bool OSInstance::setTimeDomainStages(int number, std::string *names)
04335 {   if (instanceData->timeDomain == NULL)
04336                 instanceData->timeDomain = new TimeDomain();
04337         if (instanceData->timeDomain->interval != NULL)
04338                 return false;
04339         if (instanceData->timeDomain->stages == NULL)
04340         {       instanceData->timeDomain->stages = new TimeDomainStages;
04341         }
04342         else
04343         {       if (instanceData->timeDomain->stages->numberOfStages != number)
04344                 {       for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
04345                         {       if (instanceData->timeDomain->stages->stage[i]->variables != NULL)
04346                                 {       delete instanceData->timeDomain->stages->stage[i]->variables;
04347                                         instanceData->timeDomain->stages->stage[i]->variables = NULL;
04348                                 }
04349                                 if (instanceData->timeDomain->stages->stage[i]->constraints != NULL)
04350                                 {       delete instanceData->timeDomain->stages->stage[i]->constraints;
04351                                         instanceData->timeDomain->stages->stage[i]->constraints = NULL;
04352                                 }
04353                                 if (instanceData->timeDomain->stages->stage[i]->objectives != NULL)
04354                                 {       delete instanceData->timeDomain->stages->stage[i]->objectives;
04355                                         instanceData->timeDomain->stages->stage[i]->objectives = NULL;
04356                                 }
04357                                 delete instanceData->timeDomain->stages->stage[i];
04358                                 instanceData->timeDomain->stages->stage[i] = NULL;
04359                         }
04360                         delete []instanceData->timeDomain->stages->stage;
04361                         instanceData->timeDomain->stages->stage = NULL;
04362                 }
04363         }
04364         if (number != 0 )
04365         {       if (instanceData->timeDomain->stages->stage == NULL)
04366                         instanceData->timeDomain->stages->stage = new TimeDomainStage*[number];
04367                 for (int i = 0; i < number; i++)
04368                 {       instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
04369                 }
04370                 instanceData->timeDomain->stages->numberOfStages = number;
04371         }
04372         for (int i = 0; i < number; i++)
04373         //initial or empty vars, cons, objectives and set default to all objectives
04374         {       if (instanceData->timeDomain->stages->stage[i]->variables != NULL)
04375                 {       delete instanceData->timeDomain->stages->stage[i]->variables;
04376                         instanceData->timeDomain->stages->stage[i]->variables = NULL;
04377                 }
04378                 instanceData->timeDomain->stages->stage[i]->variables = new TimeDomainStageVariables();
04379                 if (instanceData->timeDomain->stages->stage[i]->constraints != NULL)
04380                 {       delete instanceData->timeDomain->stages->stage[i]->constraints;
04381                         instanceData->timeDomain->stages->stage[i]->constraints = NULL;
04382                 }
04383                 instanceData->timeDomain->stages->stage[i]->constraints = new TimeDomainStageConstraints();
04384                 if (instanceData->timeDomain->stages->stage[i]->objectives != NULL)
04385                 {       delete instanceData->timeDomain->stages->stage[i]->objectives;
04386                         instanceData->timeDomain->stages->stage[i]->objectives = NULL;
04387                 }
04388                 instanceData->timeDomain->stages->stage[i]->objectives = new TimeDomainStageObjectives();
04389                 instanceData->timeDomain->stages->stage[i]->objectives->numberOfObjectives = instanceData->objectives->numberOfObjectives;
04390                 instanceData->timeDomain->stages->stage[i]->objectives->obj = new TimeDomainStageObj*[instanceData->objectives->numberOfObjectives];
04391                 for (int j = 0; j < instanceData->objectives->numberOfObjectives; j++)
04392                 {       instanceData->timeDomain->stages->stage[i]->objectives->obj[j] = new TimeDomainStageObj();
04393                         instanceData->timeDomain->stages->stage[i]->objectives->obj[j]->idx = -(j+1);
04394                 }
04395                 if (names != NULL)
04396                         instanceData->timeDomain->stages->stage[i]->name = names[i];
04397         }
04398         return true;
04399 } //end setTimeDomainStages
04400 
04404 bool OSInstance::setTimeDomainStageVariablesOrdered(int numberOfStages, int *numberOfVariables, int *startIdx)
04405 {       if (instanceData->timeDomain == NULL)
04406                 instanceData->timeDomain = new TimeDomain();
04407         if (instanceData->timeDomain->interval != NULL)
04408                 return false;
04409         if (instanceData->timeDomain->stages == NULL)
04410                 instanceData->timeDomain->stages = new TimeDomainStages();      
04411         if (instanceData->timeDomain->stages != NULL)
04412         {       if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
04413                         (instanceData->timeDomain->stages->numberOfStages != 0))
04414                          return false;
04415         }
04416         if (instanceData->timeDomain->stages->numberOfStages == 0)
04417         {       instanceData->timeDomain->stages->numberOfStages = numberOfStages;
04418                 if (instanceData->timeDomain->stages->stage == NULL)
04419                         instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
04420                 for (int i = 0; i < numberOfStages; i++)
04421                         instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
04422         }
04423         int checksum = 0;
04424         for (int i = 0; i < numberOfStages; i++)
04425         //initial or empty vars, cons, objectives and set default to all objectives
04426         {       if (instanceData->timeDomain->stages->stage[i]->variables != NULL)
04427                 {       delete instanceData->timeDomain->stages->stage[i]->variables;
04428                         instanceData->timeDomain->stages->stage[i]->variables = NULL;
04429                 }
04430                 instanceData->timeDomain->stages->stage[i]->variables = new TimeDomainStageVariables();
04431                 instanceData->timeDomain->stages->stage[i]->variables->startIdx = startIdx[i];
04432                 instanceData->timeDomain->stages->stage[i]->variables->numberOfVariables = numberOfVariables[i];
04433                 checksum += numberOfVariables[i];
04434         }
04435         return (checksum == instanceData->variables->numberOfVariables);
04436 } //end setTimeDomainVariablesOrdered
04437 
04441 bool OSInstance::setTimeDomainStageVariablesUnordered(int numberOfStages, int *numberOfVariables, int **varIndex)
04442 {   if (instanceData->timeDomain == NULL)
04443                 instanceData->timeDomain = new TimeDomain();
04444         if (instanceData->timeDomain->interval != NULL)
04445                 return false;
04446         if (instanceData->timeDomain->stages == NULL)
04447                 instanceData->timeDomain->stages = new TimeDomainStages();      
04448         if (instanceData->timeDomain->stages != NULL)
04449         {       if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
04450                         (instanceData->timeDomain->stages->numberOfStages != 0))
04451                          return false;
04452         }
04453         if (instanceData->timeDomain->stages->numberOfStages == 0 )
04454         {       instanceData->timeDomain->stages->numberOfStages = numberOfStages;
04455                 if (instanceData->timeDomain->stages->stage == NULL)
04456                         instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
04457                 for (int i = 0; i < numberOfStages; i++)
04458                         instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
04459         }
04460         int checksum = 0;
04461         for (int i = 0; i < numberOfStages; i++)
04462         //initial or empty vars, cons, objectives and set default to all objectives
04463         {       if (instanceData->timeDomain->stages->stage[i]->variables != NULL)
04464                 {       delete instanceData->timeDomain->stages->stage[i]->variables;
04465                         instanceData->timeDomain->stages->stage[i]->variables = NULL;
04466                 }
04467                 instanceData->timeDomain->stages->stage[i]->variables = new TimeDomainStageVariables();
04468                 instanceData->timeDomain->stages->stage[i]->variables->numberOfVariables = numberOfVariables[i];
04469                 instanceData->timeDomain->stages->stage[i]->variables->var = new TimeDomainStageVar*[numberOfVariables[i]];
04470                 for (int j = 0; j < numberOfVariables[i]; j++)
04471                 {       instanceData->timeDomain->stages->stage[i]->variables->var[j] = new TimeDomainStageVar();
04472                         instanceData->timeDomain->stages->stage[i]->variables->var[j]->idx = varIndex[i][j];
04473                 }
04474                 checksum += numberOfVariables[i];
04475         }
04476         if (checksum != instanceData->variables->numberOfVariables) return false;
04477         int *checkvar = new int[instanceData->variables->numberOfVariables];
04478         for (int j = 0; j < instanceData->variables->numberOfVariables; j++)
04479                 checkvar[j] = -1;
04480         int k;
04481         for (int i = 0; i < numberOfStages; i++)
04482                 for (int j = 0; j < instanceData->timeDomain->stages->stage[i]->variables->numberOfVariables; j++)
04483                 {       k = instanceData->timeDomain->stages->stage[i]->variables->var[j]->idx;
04484                         if (checkvar[k] != -1) 
04485                         {       delete [] checkvar;
04486                                 checkvar = NULL;
04487                                 return false;
04488                         }
04489                         checkvar[k] = instanceData->timeDomain->stages->stage[i]->variables->var[j]->idx;
04490                 }
04491         delete [] checkvar;
04492         checkvar = NULL;                        
04493         return true;
04494 } //end setTimeDomainVariablesUnordered
04495 
04499 bool OSInstance::setTimeDomainStageConstraintsOrdered(int numberOfStages, int *numberOfConstraints, int *startIdx)
04500 {   if (instanceData->timeDomain == NULL)
04501                 instanceData->timeDomain = new TimeDomain();
04502         if (instanceData->timeDomain->interval != NULL)
04503                 return false;
04504         if (instanceData->timeDomain->stages == NULL)
04505                 instanceData->timeDomain->stages = new TimeDomainStages();      
04506         if (instanceData->timeDomain->stages != NULL)
04507         {       if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
04508                         (instanceData->timeDomain->stages->numberOfStages != 0))
04509                          return false;
04510         }
04511         if (instanceData->timeDomain->stages->numberOfStages == 0 )
04512         {       instanceData->timeDomain->stages->numberOfStages = numberOfStages;
04513                 if (instanceData->timeDomain->stages->stage == NULL)
04514                         instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
04515                 for (int i = 0; i < numberOfStages; i++)
04516                         instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
04517         }
04518         int checksum = 0;
04519         for (int i = 0; i < numberOfStages; i++)
04520         //initial or empty vars, cons, objectives and set default to all objectives
04521         {       if (instanceData->timeDomain->stages->stage[i]->constraints != NULL)
04522                 {       delete instanceData->timeDomain->stages->stage[i]->constraints;
04523                         instanceData->timeDomain->stages->stage[i]->constraints = NULL;
04524                 }
04525                 instanceData->timeDomain->stages->stage[i]->constraints = new TimeDomainStageConstraints();
04526                 instanceData->timeDomain->stages->stage[i]->constraints->startIdx = startIdx[i];
04527                 instanceData->timeDomain->stages->stage[i]->constraints->numberOfConstraints = numberOfConstraints[i];
04528                 checksum += numberOfConstraints[i];
04529         }
04530         return (checksum == instanceData->constraints->numberOfConstraints);
04531 } // end of setTimeStageConstraintsOrdered
04532 
04536 bool OSInstance::setTimeDomainStageConstraintsUnordered(int numberOfStages, int *numberOfConstraints, int **conIndex)
04537 {   if (instanceData->timeDomain == NULL)
04538                 instanceData->timeDomain = new TimeDomain();
04539         if (instanceData->timeDomain->interval != NULL)
04540                 return false;
04541         if (instanceData->timeDomain->stages == NULL)
04542                 instanceData->timeDomain->stages = new TimeDomainStages();      
04543         if (instanceData->timeDomain->stages != NULL)
04544         {       if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
04545                         (instanceData->timeDomain->stages->numberOfStages != 0))
04546                          return false;
04547         }
04548         if ( instanceData->timeDomain->stages->numberOfStages == 0 )
04549         {       instanceData->timeDomain->stages->numberOfStages = numberOfStages;
04550                 if (instanceData->timeDomain->stages->stage == NULL)
04551                         instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
04552                 for (int i = 0; i < numberOfStages; i++)
04553                         instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
04554         }
04555         int checksum = 0;
04556         for (int i = 0; i < numberOfStages; i++)
04557         //initial or empty vars, cons, objectives and set default to all objectives
04558         {       if (instanceData->timeDomain->stages->stage[i]->constraints != NULL)
04559                 {       delete instanceData->timeDomain->stages->stage[i]->constraints;
04560                         instanceData->timeDomain->stages->stage[i]->constraints = NULL;
04561                 }
04562                 instanceData->timeDomain->stages->stage[i]->constraints = new TimeDomainStageConstraints();
04563                 instanceData->timeDomain->stages->stage[i]->constraints->numberOfConstraints = numberOfConstraints[i];
04564                 instanceData->timeDomain->stages->stage[i]->constraints->con = new TimeDomainStageCon*[numberOfConstraints[i]];
04565                 for (int j = 0; j < numberOfConstraints[i]; j++)
04566                 {       instanceData->timeDomain->stages->stage[i]->constraints->con[j] = new TimeDomainStageCon();
04567                         instanceData->timeDomain->stages->stage[i]->constraints->con[j]->idx = conIndex[i][j];
04568                 }
04569                 checksum += numberOfConstraints[i];
04570         }
04571         if (checksum != instanceData->constraints->numberOfConstraints) return false;
04572         int *checkvar = new int[instanceData->constraints->numberOfConstraints];
04573         for (int j = 0; j < instanceData->constraints->numberOfConstraints; j++)
04574                 checkvar[j] = -1;
04575         int k;
04576         for (int i = 0; i < numberOfStages; i++)
04577                 for (int j = 0; j < instanceData->timeDomain->stages->stage[i]->constraints->numberOfConstraints; j++)
04578                 {       k = instanceData->timeDomain->stages->stage[i]->constraints->con[j]->idx;
04579                         if (checkvar[k] != -1) 
04580                         {       delete [] checkvar;
04581                                 checkvar = NULL;
04582                                 return false;
04583                         }
04584                         checkvar[k] = instanceData->timeDomain->stages->stage[i]->constraints->con[j]->idx;
04585                 }
04586         delete [] checkvar;
04587         checkvar = NULL;                        
04588         return true;
04589 }// end setTimeDomainStageConstraintsUnordered()
04590 
04594 bool OSInstance::setTimeDomainStageObjectivesOrdered(int numberOfStages, int *numberOfObjectives, int *startIdx)
04595 {   if (instanceData->timeDomain == NULL)
04596                 instanceData->timeDomain = new TimeDomain();
04597         if (instanceData->timeDomain->interval != NULL)
04598                 return false;
04599         if (instanceData->timeDomain->stages == NULL)
04600                 instanceData->timeDomain->stages = new TimeDomainStages();      
04601         if (instanceData->timeDomain->stages != NULL)
04602         {       if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
04603                         (instanceData->timeDomain->stages->numberOfStages != 0))
04604                          return false;
04605         }
04606         if (instanceData->timeDomain->stages->numberOfStages == 0)
04607         {       instanceData->timeDomain->stages->numberOfStages = numberOfStages;
04608                 if (instanceData->timeDomain->stages->stage == NULL)
04609                         instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
04610                 for (int i = 0; i < numberOfStages; i++)
04611                         instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
04612         }
04613         for (int i = 0; i < numberOfStages; i++)
04614         //initial or empty vars, cons, objectives and set default to all objectives
04615         {       if (instanceData->timeDomain->stages->stage[i]->objectives != NULL)
04616                 {       delete instanceData->timeDomain->stages->stage[i]->objectives;
04617                         instanceData->timeDomain->stages->stage[i]->objectives = NULL;
04618                 }
04619                 instanceData->timeDomain->stages->stage[i]->objectives = new TimeDomainStageObjectives();
04620                 instanceData->timeDomain->stages->stage[i]->objectives->startIdx = startIdx[i];
04621                 instanceData->timeDomain->stages->stage[i]->objectives->numberOfObjectives = numberOfObjectives[i];
04622         }
04623         return true;
04624 }
04625 
04629 bool OSInstance::setTimeDomainStageObjectivesUnordered(int numberOfStages, int *numberOfObjectives, int **objIndex)
04630 {   if (instanceData->timeDomain == NULL)
04631                 instanceData->timeDomain = new TimeDomain();
04632         if (instanceData->timeDomain->interval != NULL)
04633                 return false;
04634         if (instanceData->timeDomain->stages == NULL)
04635                 instanceData->timeDomain->stages = new TimeDomainStages();      
04636         if (instanceData->timeDomain->stages != NULL)
04637         {       if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
04638                         (instanceData->timeDomain->stages->numberOfStages != 0))
04639                          return false;
04640         }
04641         if (instanceData->timeDomain->stages->numberOfStages == 0)
04642         {       instanceData->timeDomain->stages->numberOfStages = numberOfStages;
04643                 if (instanceData->timeDomain->stages->stage == NULL)
04644                         instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
04645                 for (int i = 0; i < numberOfStages; i++)
04646                         instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
04647         }
04648         for (int i = 0; i < numberOfStages; i++)
04649         //initial or empty vars, cons, objectives and set default to all objectives
04650         {       if (instanceData->timeDomain->stages->stage[i]->objectives != NULL)
04651                 {       delete instanceData->timeDomain->stages->stage[i]->objectives;
04652                         instanceData->timeDomain->stages->stage[i]->objectives = NULL;
04653                 }
04654                 instanceData->timeDomain->stages->stage[i]->objectives = new TimeDomainStageObjectives();
04655                 instanceData->timeDomain->stages->stage[i]->objectives->numberOfObjectives = numberOfObjectives[i];
04656                 instanceData->timeDomain->stages->stage[i]->objectives->obj = new TimeDomainStageObj*[numberOfObjectives[i]];
04657                 for (int j = 0; j < numberOfObjectives[i]; j++)
04658                 {       instanceData->timeDomain->stages->stage[i]->objectives->obj[j] = new TimeDomainStageObj();
04659                         instanceData->timeDomain->stages->stage[i]->objectives->obj[j]->idx = objIndex[i][j];
04660                 }
04661         }
04662         int *checkvar = new int[instanceData->objectives->numberOfObjectives];
04663         for (int j = 0; j < instanceData->objectives->numberOfObjectives; j++)
04664                 checkvar[j] = 0;
04665         int k;
04666         for (int i = 0; i < numberOfStages; i++)
04667                 for (int j = 0; j < instanceData->timeDomain->stages->stage[i]->objectives->numberOfObjectives; j++)
04668                 {       k = -instanceData->timeDomain->stages->stage[i]->objectives->obj[j]->idx-1;
04669                         checkvar[k] = instanceData->timeDomain->stages->stage[i]->objectives->obj[j]->idx;
04670                 }
04671         for (int i = 0; i < instanceData->objectives->numberOfObjectives; i++)
04672                 if (checkvar[i] == 0)
04673                 {       delete [] checkvar;
04674                         checkvar = NULL;
04675                         return false;
04676                 }
04677         delete [] checkvar;
04678         checkvar = NULL;                        
04679         return true;
04680 }
04681 
04685 bool OSInstance::setTimeDomainInterval(double start, double horizon)
04686 {   if (instanceData->timeDomain == NULL)
04687                 instanceData->timeDomain = new TimeDomain();
04688         if (instanceData->timeDomain->stages != NULL)
04689                 return false;
04690         if (instanceData->timeDomain->interval == NULL)
04691                 instanceData->timeDomain->interval = new TimeDomainInterval();  
04692         instanceData->timeDomain->interval->start = start;
04693         instanceData->timeDomain->interval->horizon = horizon;
04694         return true;
04695 } //end setTimeDomainInterval
04696 
04697 
04698 bool OSInstance::createOSADFun(std::vector<double> vdX){
04699         try{
04700                 if(m_bOSADFunIsCreated == true) return true;
04701                 //if( m_bNonLinearStructuresInitialized == false) initializeNonLinearStructures( );
04702                 if(m_binitForAlgDiff == false) initForAlgDiff();
04703                 
04704                 //if( m_bAllNonlinearVariablesIndex == false) getAllNonlinearVariablesIndexMap( );
04705                 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
04706                 unsigned int i;
04707                 size_t n = vdX.size();
04708 #ifdef COIN_HAS_CPPAD
04709                 // declare a CppAD vector and fill it in
04710                 CppAD::vector< AD<double> > vdaX( n );
04711                 for(i = 0; i < n; i++){
04712                         vdaX[ i] = vdX[ i];
04713                         //std::cout << "vdX =  " << vdX[ i] << std::endl;
04714                 }
04715                 // declare the independent variables and start recording
04716                 CppAD::Independent( vdaX);
04722                 CppAD::vector< AD<double> > m_vFG;        
04723                 int kount = 0;
04724                 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree){  
04725                         m_vFG.push_back( (posMapExpTree->second)->m_treeRoot->constructADTape(&m_mapAllNonlinearVariablesIndex, &vdaX) );
04726                         //std::cout << "PUSHING BACK EXPRESSION NUMBER " << posMapExpTree->first << std::endl;
04727                         if( m_mapOSADFunRangeIndex.find( posMapExpTree->first) == m_mapOSADFunRangeIndex.end() ){
04728                                 // count which nonlinear obj/constraint this is
04729                                 m_mapOSADFunRangeIndex[ posMapExpTree->first] = kount;
04730                                 kount++;
04731                         }
04732                 }       
04733                 //create the function and stop recording
04734 //              std::cout << "create the function and stop recording"  << std::endl;
04735                 Fad = new CppAD::ADFun<double>(vdaX, m_vFG);
04736 //              std::cout << "range space dimension =  " << m_vFG.size() << std::endl;
04737                 // no forward sweeps done yet
04738                 m_iHighestTaylorCoeffOrder = -1;
04739                 m_bOSADFunIsCreated = true;
04740 #else
04741                 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
04742 #endif
04743                         
04744                 return true;
04745         }
04746         catch(const ErrorClass& eclass){
04747                 throw ErrorClass( eclass.errormsg);
04748         } 
04749 }//end createOSADFun
04750 
04751 
04752 std::vector<double> OSInstance::forwardAD(int p, std::vector<double> vdX){
04753         try{
04754                 // make sure a OSADFun has been created
04755                 if(m_bOSADFunIsCreated == false) createOSADFun( vdX);
04756                 if(p > (m_iHighestTaylorCoeffOrder + 1) ) throw 
04757                         ErrorClass( "trying to calculate a p order forward when p-1 Taylor coefficient not available");
04758                 // adjust the order of the Taylor coefficient
04759                 m_iHighestTaylorCoeffOrder = p; 
04760                 m_iHighestOrderEvaluated = p;
04761                 //for(int i  = 0; i < vdX.size(); i++){
04762                         //std::cout << "ForwardAD Primal Variables " << i   << " " << vdX[ i] << std::endl;
04763                 //}
04764 #ifdef COIN_HAS_CPPAD
04765                 return (*Fad).Forward(p, vdX);
04766 #else
04767                 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
04768 #endif
04769                 
04770         }
04771         catch(const ErrorClass& eclass){
04772                 throw ErrorClass( eclass.errormsg);
04773         }  
04774 }//end forwardAD
04775 
04776 
04777 std::vector<double> OSInstance::reverseAD(int p, std::vector<double> vdlambda){
04778         try{
04779 #ifndef COIN_HAS_CPPAD
04780                 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
04781 #endif
04782                 if(p == 0) throw 
04783                         ErrorClass( "reverseAD must have p >= 1");
04784                 if(p > (m_iHighestTaylorCoeffOrder + 1) ) throw 
04785                         ErrorClass( "trying to calculate a p order reverse when p-1 Taylor coefficient not available");
04786                 //for(int i  = 0; i < vdlambda.size(); i++){
04787                 //      std::cout << "ReverseAD Multiplier " << i   << " " << vdlambda[ i] << std::endl;
04788                 //}
04789                 m_iHighestOrderEvaluated = p;
04790 #ifdef COIN_HAS_CPPAD
04791                 return (*Fad).Reverse(p, vdlambda);
04792 #else
04793                 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
04794 #endif
04795                 
04796         }
04797         catch(const ErrorClass& eclass){
04798                 throw ErrorClass( eclass.errormsg);
04799         }  
04800 }//end forwardAD
04801 
04802 
04803 int  OSInstance::getADSparsityHessian(){
04804         
04805         unsigned int i;
04806         int numNonz;
04807         numNonz = 0;
04808         try{    
04809                 std::vector<bool> r(m_iNumberOfNonlinearVariables * m_iNumberOfNonlinearVariables);
04810                 unsigned int j;
04811                 for(i = 0; i < m_iNumberOfNonlinearVariables; i++) { 
04812                         for(j = 0; j < m_iNumberOfNonlinearVariables; j++)
04813                                 r[ i * m_iNumberOfNonlinearVariables + j ] = false;
04814                                 r[ i * m_iNumberOfNonlinearVariables + i] = true;
04815                 }       
04816                 // compute sparsity pattern for J(x) = F^{(1)} (x)
04817                 //should only be here if we have CppAD
04818 #ifdef COIN_HAS_CPPAD
04819                 (*Fad).ForSparseJac(m_iNumberOfNonlinearVariables, r);
04820 #else
04821                 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
04822 #endif
04823                 //
04824                 //now the second derivative
04825                 unsigned int m = m_mapExpressionTreesMod.size();
04826                 std::vector<bool> e( m);
04827                 //Vector s(m);
04828                 for(i = 0; i < m; i++) e[i] = true;
04829 //              std::cout << "Computing Sparse Hessian" << std::endl;
04830                 //m_vbLagHessNonz holds the sparsity pattern Lagrangian of the Hessian
04831 #ifdef COIN_HAS_CPPAD
04832                 m_vbLagHessNonz = (*Fad).RevSparseHes(m_iNumberOfNonlinearVariables, e);
04833 #else
04834                 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
04835 #endif
04836                 
04837                 for(i = 0; i < m_iNumberOfNonlinearVariables; i++){
04838                         //std::cout << "Row " << i << "  of Hessian " << std::endl;
04839                         for(j = i; j < m_iNumberOfNonlinearVariables; j++){
04840                                 if(m_vbLagHessNonz[ i*m_iNumberOfNonlinearVariables + j]  == true) numNonz++;
04841                                 //std::cout << m_vbLagHessNonz[ i*m_iNumberOfNonlinearVariables + j] <<  "  " ;
04842                         }
04843                         //std::cout << std::endl;
04844                 }       
04845                 return numNonz;
04846         }
04847         catch(const ErrorClass& eclass){
04848                 throw ErrorClass( eclass.errormsg);
04849         }
04850 }//end getADSparsityHessian()
04851 
04852 
04853 
04854 /***************************************************
04855  * methods to test whether two OSInstance objects 
04856  * or their components are equal to each other
04857  ***************************************************/
04858 bool OSInstance::IsEqual(OSInstance *that)
04859 {
04860         #ifdef DEBUG_ISEQUAL_ROUTINES
04861                 cout << "Start comparing in OSInstance" << endl;
04862         #endif
04863         if (this == NULL)
04864         {       if (that == NULL)
04865                         return true;
04866                 else
04867                 {
04868                         #ifdef DEBUG_ISEQUAL_ROUTINES
04869                                 cout << "First object is NULL, second is not" << endl;
04870                         #endif
04871                         return false;
04872                 }
04873         }
04874         else 
04875         {       if (that == NULL)
04876                 {
04877                         #ifdef DEBUG_ISEQUAL_ROUTINES
04878                                 cout << "Second object is NULL, first is not" << endl;
04879                         #endif
04880                         return false;
04881                 }
04882                 else    
04883                 {
04884                         if (!this->instanceHeader->IsEqual(that->instanceHeader))
04885                                 return false;
04886                         if (!this->instanceData->IsEqual(that->instanceData))
04887                                 return false;
04888 
04889                         return true;
04890                 }
04891         }
04892 }//OSInstance::IsEqual
04893 
04894 /*------------------------------------------*/
04895 bool InstanceHeader::IsEqual(InstanceHeader *that)
04896 {
04897         #ifdef DEBUG_ISEQUAL_ROUTINES
04898                 cout << "Start comparing in InstanceHeader" << endl;
04899         #endif
04900         if (this == NULL)
04901         {       if (that == NULL)
04902                         return true;
04903                 else
04904                 {
04905                         #ifdef DEBUG_ISEQUAL_ROUTINES
04906                                 cout << "First object is NULL, second is not" << endl;
04907                         #endif
04908                         return false;
04909                 }
04910         }
04911         else 
04912         {       if (that == NULL)
04913                 {
04914                         #ifdef DEBUG_ISEQUAL_ROUTINES
04915                                 cout << "Second object is NULL, first is not" << endl;
04916                         #endif
04917                         return false;
04918                 }
04919                 else    
04920                 {
04921                         if (this->description != that->description)
04922                                 return false;
04923                         if (this->name != that->name)
04924                                 return false;
04925                         if (this->source != that->source)
04926                                 return false;
04927 
04928                         return true;
04929                 }
04930         }
04931 }//InstanceHeader::IsEqual
04932 
04933 bool InstanceData::IsEqual(InstanceData *that)
04934 {
04935         #ifdef DEBUG_ISEQUAL_ROUTINES
04936                 cout << "Start comparing in InstanceData" << endl;
04937         #endif
04938         if (this == NULL)
04939         {       if (that == NULL)
04940                         return true;
04941                 else
04942                 {
04943                         #ifdef DEBUG_ISEQUAL_ROUTINES
04944                                 cout << "First object is NULL, second is not" << endl;
04945                         #endif
04946                         return false;
04947                 }
04948         }
04949         else 
04950         {       if (that == NULL)
04951                 {
04952                         #ifdef DEBUG_ISEQUAL_ROUTINES
04953                                 cout << "Second object is NULL, first is not" << endl;
04954                         #endif
04955                         return false;
04956                 }
04957                 else    
04958                 {
04959                         if (!this->variables->IsEqual(that->variables))
04960                                 return false;
04961                         if (!this->objectives->IsEqual(that->objectives))
04962                                 return false;
04963                         if (!this->constraints->IsEqual(that->constraints))
04964                                 return false;
04965                         if (!this->linearConstraintCoefficients->IsEqual(that->linearConstraintCoefficients))
04966                                 return false;
04967                         if (!this->quadraticCoefficients->IsEqual(that->quadraticCoefficients))
04968                                 return false;
04969                         if (!this->nonlinearExpressions->IsEqual(that->nonlinearExpressions))
04970                                 return false;
04971 
04972                         return true;
04973                 }
04974         }
04975 }//InstanceData::IsEqual
04976 
04977 bool Variables::IsEqual(Variables *that)
04978 {
04979         #ifdef DEBUG_ISEQUAL_ROUTINES
04980                 cout << "Start comparing in Variables" << endl;
04981         #endif
04982         if (this == NULL)
04983         {       if (that == NULL)
04984                         return true;
04985                 else
04986                 {
04987                         #ifdef DEBUG_ISEQUAL_ROUTINES
04988                                 cout << "First object is NULL, second is not" << endl;
04989                         #endif
04990                         return false;
04991                 }
04992         }
04993         else 
04994         {       if (that == NULL)
04995                 {
04996                         #ifdef DEBUG_ISEQUAL_ROUTINES
04997                                 cout << "Second object is NULL, first is not" << endl;
04998                         #endif
04999                         return false;
05000                 }
05001                 else    
05002                 {
05003                         if (this->numberOfVariables != that->numberOfVariables)
05004                                 return false;
05005                         for (int i=0; i<this->numberOfVariables; i++)
05006                                 if (!this->var[i]->IsEqual(that->var[i]))
05007                                         return false;
05008 
05009                         return true;
05010                 }
05011         }
05012 }//Variables::IsEqual
05013 
05014 bool Variable::IsEqual(Variable *that)
05015 {
05016         #ifdef DEBUG_ISEQUAL_ROUTINES
05017                 cout << "Start comparing in Variable" << endl;
05018         #endif
05019         if (this == NULL)
05020         {       if (that == NULL)
05021                         return true;
05022                 else
05023                 {
05024                         #ifdef DEBUG_ISEQUAL_ROUTINES
05025                                 cout << "First object is NULL, second is not" << endl;
05026                         #endif
05027                         return false;
05028                 }
05029         }
05030         else 
05031         {       if (that == NULL)
05032                 {
05033                         #ifdef DEBUG_ISEQUAL_ROUTINES
05034                                 cout << "Second object is NULL, first is not" << endl;
05035                         #endif
05036                         return false;
05037                 }
05038                 else    
05039                 {
05040                         if (this->lb != that->lb)
05041                                 return false;
05042                         if (this->ub != that->ub)
05043                                 return false;
05044                         if (this->type != that->type)
05045                                 return false;
05046                         if (this->name != that->name)
05047                                 return false;
05048 
05049                         return true;
05050                 }
05051         }
05052 }//Variable::IsEqual
05053 
05054 bool Objectives::IsEqual(Objectives *that)
05055 {
05056         #ifdef DEBUG_ISEQUAL_ROUTINES
05057                 cout << "Start comparing in Objectives" << endl;
05058         #endif
05059         if (this == NULL)
05060         {       if (that == NULL)
05061                         return true;
05062                 else
05063                 {
05064                         #ifdef DEBUG_ISEQUAL_ROUTINES
05065                                 cout << "First object is NULL, second is not" << endl;
05066                         #endif
05067                         return false;
05068                 }
05069         }
05070         else 
05071         {       if (that == NULL)
05072                 {
05073                         #ifdef DEBUG_ISEQUAL_ROUTINES
05074                                 cout << "Second object is NULL, first is not" << endl;
05075                         #endif
05076                         return false;
05077                 }
05078                 else    
05079                 {
05080                         if (this->numberOfObjectives != that->numberOfObjectives)
05081                                 return false;
05082                         for (int i=0; i<this->numberOfObjectives; i++)
05083                                 if (!this->obj[i]->IsEqual(that->obj[i]))
05084                                         return false;
05085 
05086                         return true;
05087                 }
05088         }
05089 }//Objectives::IsEqual
05090 
05091 bool Objective::IsEqual(Objective *that)
05092 {
05093         #ifdef DEBUG_ISEQUAL_ROUTINES
05094                 cout << "Start comparing in Objective" << endl;
05095         #endif
05096         if (this == NULL)
05097         {       if (that == NULL)
05098                         return true;
05099                 else
05100                 {
05101                         #ifdef DEBUG_ISEQUAL_ROUTINES
05102                                 cout << "First object is NULL, second is not" << endl;
05103                         #endif
05104                         return false;
05105                 }
05106         }
05107         else 
05108         {       if (that == NULL)
05109                 {
05110                         #ifdef DEBUG_ISEQUAL_ROUTINES
05111                                 cout << "Second object is NULL, first is not" << endl;
05112                         #endif
05113                         return false;
05114                 }
05115                 else    
05116                 {
05117                         if (this->name != that->name)
05118                                 return false;
05119                         if (this->maxOrMin != that->maxOrMin)
05120                                 return false;
05121                         if (this->constant != that->constant)
05122                                 return false;
05123                         if (this->weight != that->weight)
05124                                 return false;
05125                         if (this->numberOfObjCoef != that->numberOfObjCoef)
05126                                 return false;
05127 
05128                         for (int i=0; i<this->numberOfObjCoef; i++)
05129                                 if (!this->coef[i]->IsEqual(that->coef[i]))
05130                                         return false;
05131 
05132                         return true;
05133                 }
05134         }
05135 }//Objective::IsEqual
05136 
05137 bool ObjCoef::IsEqual(ObjCoef *that)
05138 {
05139         #ifdef DEBUG_ISEQUAL_ROUTINES
05140                 cout << "Start comparing in ObjCoef" << endl;
05141         #endif
05142         if (this == NULL)
05143         {       if (that == NULL)
05144                         return true;
05145                 else
05146                 {
05147                         #ifdef DEBUG_ISEQUAL_ROUTINES
05148                                 cout << "First object is NULL, second is not" << endl;
05149                         #endif
05150                         return false;
05151                 }
05152         }
05153         else 
05154         {       if (that == NULL)
05155                 {
05156                         #ifdef DEBUG_ISEQUAL_ROUTINES
05157                                 cout << "Second object is NULL, first is not" << endl;
05158                         #endif
05159                         return false;
05160                 }
05161                 else    
05162                 {
05163                         if (this->idx != that->idx)
05164                                 return false;
05165                         if (this->value != that->value)
05166                                 return false;
05167 
05168                         return true;
05169                 }
05170         }
05171 }//ObjCoef::IsEqual
05172 
05173 bool Constraints::IsEqual(Constraints *that)
05174 {
05175         #ifdef DEBUG_ISEQUAL_ROUTINES
05176                 cout << "Start comparing in Constraints" << endl;
05177         #endif
05178         if (this == NULL)
05179         {       if (that == NULL)
05180                         return true;
05181                 else
05182                 {
05183                         #ifdef DEBUG_ISEQUAL_ROUTINES
05184                                 cout << "First object is NULL, second is not" << endl;
05185                         #endif
05186                         return false;
05187                 }
05188         }
05189         else 
05190         {       if (that == NULL)
05191                 {
05192                         #ifdef DEBUG_ISEQUAL_ROUTINES
05193                                 cout << "Second object is NULL, first is not" << endl;
05194                         #endif
05195                         return false;
05196                 }
05197                 else    
05198                 {
05199                         if (this->numberOfConstraints != that->numberOfConstraints)
05200                                 return false;
05201                         for (int i=0; i<this->numberOfConstraints; i++)
05202                                 if (!this->con[i]->IsEqual(that->con[i]))
05203                                         return false;
05204 
05205                         return true;
05206                 }
05207         }
05208 }//Constraints::IsEqual
05209 
05210 bool Constraint::IsEqual(Constraint *that)
05211 {
05212         #ifdef DEBUG_ISEQUAL_ROUTINES
05213                 cout << "Start comparing in Constraint" << endl;
05214         #endif
05215         if (this == NULL)
05216         {       if (that == NULL)
05217                         return true;
05218                 else
05219                 {
05220                         #ifdef DEBUG_ISEQUAL_ROUTINES
05221                                 cout << "First object is NULL, second is not" << endl;
05222                         #endif
05223                         return false;
05224                 }
05225         }
05226         else 
05227         {       if (that == NULL)
05228                 {
05229                         #ifdef DEBUG_ISEQUAL_ROUTINES
05230                                 cout << "Second object is NULL, first is not" << endl;
05231                         #endif
05232                         return false;
05233                 }
05234                 else    
05235                 {
05236                         if (this->name != that->name)
05237                                 return false;
05238                         if (this->constant != that->constant)
05239                                 return false;
05240                         if (this->lb != that->lb)
05241                                 return false;
05242                         if (this->ub != that->ub)
05243                                 return false;
05244 
05245                         return true;
05246                 }
05247         }
05248 }//Constraint::IsEqual
05249 
05250 bool LinearConstraintCoefficients::IsEqual(LinearConstraintCoefficients *that)
05251 {
05252         #ifdef DEBUG_ISEQUAL_ROUTINES
05253                 cout << "Start comparing in LinearConstraintCoefficients" << endl;
05254         #endif
05255         if (this == NULL)
05256         {       if (that == NULL)
05257                         return true;
05258                 else
05259                 {
05260                         #ifdef DEBUG_ISEQUAL_ROUTINES
05261                                 cout << "First object is NULL, second is not" << endl;
05262                         #endif
05263                         return false;
05264                 }
05265         }
05266         else 
05267         {       if (that == NULL)
05268                 {
05269                         #ifdef DEBUG_ISEQUAL_ROUTINES
05270                                 cout << "Second object is NULL, first is not" << endl;
05271                         #endif
05272                         return false;
05273                 }
05274                 else    
05275                 {
05276                         if (this->numberOfValues != that->numberOfValues)
05277                                 return false;
05278 
05279                         if (!this->start->IsEqual(that->start))
05280                                 return false;
05281                         if (!this->rowIdx->IsEqual(that->rowIdx))
05282                                 return false;
05283                         if (!this->colIdx->IsEqual(that->colIdx))
05284                                 return false;
05285                         if (!this->value->IsEqual(that->value))
05286                                 return false;
05287 
05288                         return true;
05289                 }
05290         }
05291 }//LinearConstraintCoefficients::IsEqual
05292 
05293 bool QuadraticCoefficients::IsEqual(QuadraticCoefficients *that)
05294 {
05295         #ifdef DEBUG_ISEQUAL_ROUTINES
05296                 cout << "Start comparing in QuadraticCoefficients" << endl;
05297         #endif
05298         if (this == NULL)
05299         {       if (that == NULL)
05300                         return true;
05301                 else
05302                 {
05303                         #ifdef DEBUG_ISEQUAL_ROUTINES
05304                                 cout << "First object is NULL, second is not" << endl;
05305                         #endif
05306                         return false;
05307                 }
05308         }
05309         else 
05310         {       if (that == NULL)
05311                 {
05312                         #ifdef DEBUG_ISEQUAL_ROUTINES
05313                                 cout << "Second object is NULL, first is not" << endl;
05314                         #endif
05315                         return false;
05316                 }
05317                 else    
05318                 {
05319                         if (this->numberOfQuadraticTerms != that->numberOfQuadraticTerms)
05320                                 return false;
05321                         for (int i=0; i<this->numberOfQuadraticTerms; i++)
05322                                 if (!this->qTerm[i]->IsEqual(that->qTerm[i]))
05323                                         return false;
05324 
05325                         return true;
05326                 }
05327         }
05328 }//QuadraticCoefficients::IsEqual
05329 
05330 
05331 bool QuadraticTerm::IsEqual(QuadraticTerm *that)
05332 {
05333         #ifdef DEBUG_ISEQUAL_ROUTINES
05334                 cout << "Start comparing in QuadraticTerm" << endl;
05335         #endif
05336         if (this == NULL)
05337         {       if (that == NULL)
05338                         return true;
05339                 else
05340                 {
05341                         #ifdef DEBUG_ISEQUAL_ROUTINES
05342                                 cout << "First object is NULL, second is not" << endl;
05343                         #endif
05344                         return false;
05345                 }
05346         }
05347         else 
05348         {       if (that == NULL)
05349                 {
05350                         #ifdef DEBUG_ISEQUAL_ROUTINES
05351                                 cout << "Second object is NULL, first is not" << endl;
05352                         #endif
05353                         return false;
05354                 }
05355                 else    
05356                 {
05357                         if (this->idx != that->idx)
05358                                 return false;
05359                         if (this->idxOne != that->idxOne)
05360                                 return false;
05361                         if (this->idxTwo != that->idxTwo)
05362                                 return false;
05363                         if (this->coef != that->coef)
05364                                 return false;
05365 
05366                         return true;
05367                 }
05368         }
05369 }//QuadraticTerm::IsEqual
05370 
05371 bool NonlinearExpressions::IsEqual(NonlinearExpressions *that)
05372 {
05373         #ifdef DEBUG_ISEQUAL_ROUTINES
05374                 cout << "Start comparing in NonlinearExpressions" << endl;
05375         #endif
05376         if (this == NULL)
05377         {       if (that == NULL)
05378                         return true;
05379                 else
05380                 {
05381                         #ifdef DEBUG_ISEQUAL_ROUTINES
05382                                 cout << "First object is NULL, second is not" << endl;
05383                         #endif
05384                         return false;
05385                 }
05386         }
05387         else 
05388         {       if (that == NULL)
05389                 {
05390                         #ifdef DEBUG_ISEQUAL_ROUTINES
05391                                 cout << "Second object is NULL, first is not" << endl;
05392                         #endif
05393                         return false;
05394                 }
05395                 else    
05396                 {
05397                         if (this->numberOfNonlinearExpressions != that->numberOfNonlinearExpressions)
05398                                 return false;
05399 
05400                         for (int i=0; i<this->numberOfNonlinearExpressions; i++)
05401                                 if (!this->nl[i]->IsEqual(that->nl[i]))
05402                                         return false;
05403 
05404                         return true;
05405                 }
05406         }
05407 }//NonlinearExpressions::IsEqual
05408 
05409 
05410 
05411 bool Nl::IsEqual(Nl *that)
05412 {
05413         #ifdef DEBUG_ISEQUAL_ROUTINES
05414                 cout << "Start comparing in Nl" << endl;
05415         #endif
05416         if (this == NULL)
05417         {       if (that == NULL)
05418                         return true;
05419                 else
05420                 {
05421                         #ifdef DEBUG_ISEQUAL_ROUTINES
05422                                 cout << "First object is NULL, second is not" << endl;
05423                         #endif
05424                         return false;
05425                 }
05426         }
05427         else 
05428         {       if (that == NULL)
05429                 {
05430                         #ifdef DEBUG_ISEQUAL_ROUTINES
05431                                 cout << "Second object is NULL, first is not" << endl;
05432                         #endif
05433                         return false;
05434                 }
05435                 else    
05436                 {
05437                         if (this->idx != that->idx)
05438                                 return false;
05439                         if (!this->osExpressionTree->IsEqual(that->osExpressionTree))
05440                                 return false;
05441 
05442                         return true;
05443                 }
05444         }
05445 }//Nl::IsEqual
05446 
05447 

Generated on Thu Mar 31 03:12:50 2011 by  doxygen 1.4.7