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

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

Generated on Sun Mar 20 03:06:18 2011 by  doxygen 1.4.7