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

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

Generated on Thu Sep 22 03:06:01 2011 by  doxygen 1.4.7