00001
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
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
00062
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
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
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
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
00233
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
00264
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
00310
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
00362
00363
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
00415
00416 delete instanceHeader;
00417 instanceHeader = NULL;
00418
00419 delete instanceData;
00420 instanceData = NULL;
00421 }
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
00446 type('C'),
00447 name("")
00448
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 }
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 }
00700
00701
00702 Nl::Nl()
00703 {
00704 idx = 0;
00705 osExpressionTree = NULL;
00706 m_bDeleteExpressionTree = true;
00707 }
00708
00709
00710 Nl::~Nl()
00711 {
00712 #ifdef DEBUG
00713 cout << "Inside the Nl Destructor" << endl;
00714 #endif
00715
00716
00717 if( m_bDeleteExpressionTree == true)
00718 {
00719 delete osExpressionTree;
00720 osExpressionTree = NULL;
00721 }
00722 }
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 }
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 }
00765
00766
00767 TimeDomainStageVar::TimeDomainStageVar():
00768 idx(0)
00769 {
00770 #ifdef DEBUG
00771 cout << "Inside the Stage Objectives Var Constructor" << endl;
00772 #endif
00773 }
00774
00775
00776 TimeDomainStageVar::~TimeDomainStageVar()
00777 {
00778 #ifdef DEBUG
00779 cout << "Inside the Stage Objectives Var Destructor" << endl;
00780 #endif
00781 }
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 }
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 }
00811
00812 TimeDomainStageCon::TimeDomainStageCon():
00813 idx(0)
00814 {
00815 #ifdef DEBUG
00816 cout << "Inside the Stage Objectives Con Constructor" << endl;
00817 #endif
00818 }
00819
00820
00821 TimeDomainStageCon::~TimeDomainStageCon()
00822 {
00823 #ifdef DEBUG
00824 cout << "Inside the Stage Objectives Con Destructor" << endl;
00825 #endif
00826 }
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 }
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 }
00856
00857 TimeDomainStageObj::TimeDomainStageObj():
00858 idx(0)
00859 {
00860 #ifdef DEBUG
00861 cout << "Inside the Stage Objectives Obj Constructor" << endl;
00862 #endif
00863 }
00864
00865
00866 TimeDomainStageObj::~TimeDomainStageObj()
00867 {
00868 #ifdef DEBUG
00869 cout << "Inside the Stage Objectives Obj Destructor" << endl;
00870 #endif
00871 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
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 }
01115
01116 bool OSInstance::processVariables()
01117 {
01118 if(m_bProcessVariables == true && bVariablesModified == false) return true;
01119
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 }
01196 catch(const ErrorClass& eclass)
01197 {
01198 throw ErrorClass( eclass.errormsg);
01199 }
01200 }
01201
01202 string* OSInstance::getVariableNames()
01203 {
01204 processVariables();
01205 return m_msVariableNames;
01206 }
01207
01208 char* OSInstance::getVariableTypes()
01209 {
01210 processVariables();
01211 return m_mcVariableTypes;
01212 }
01213
01214 int OSInstance::getNumberOfBinaryVariables()
01215 {
01216 processVariables();
01217 return m_iNumberOfBinaryVariables;
01218 }
01219
01220 int OSInstance::getNumberOfIntegerVariables()
01221 {
01222 processVariables();
01223 return m_iNumberOfIntegerVariables;
01224 }
01225
01226 int OSInstance::getNumberOfSemiContinuousVariables()
01227 {
01228 processVariables();
01229 return m_iNumberOfSemiContinuousVariables;
01230 }
01231
01232 int OSInstance::getNumberOfSemiIntegerVariables()
01233 {
01234 processVariables();
01235 return m_iNumberOfSemiIntegerVariables;
01236 }
01237
01238 int OSInstance::getNumberOfStringVariables()
01239 {
01240 processVariables();
01241 return m_iNumberOfStringVariables;
01242 }
01243
01244 double* OSInstance::getVariableLowerBounds()
01245 {
01246 processVariables();
01247 return m_mdVariableLowerBounds;
01248 }
01249
01250 double* OSInstance::getVariableUpperBounds()
01251 {
01252 processVariables();
01253 return m_mdVariableUpperBounds;
01254 }
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 }
01267
01268 bool OSInstance::processObjectives()
01269 {
01270 if(m_bProcessObjectives == true && bObjectivesModified == false) return true;
01271
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
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 }
01325 catch(const ErrorClass& eclass)
01326 {
01327 throw ErrorClass( eclass.errormsg);
01328 }
01329 }
01330
01331 string* OSInstance::getObjectiveNames()
01332 {
01333 processObjectives();
01334 return m_msObjectiveNames;
01335 }
01336
01337 string* OSInstance::getObjectiveMaxOrMins()
01338 {
01339 processObjectives();
01340 return m_msMaxOrMins;
01341 }
01342
01343 int* OSInstance::getObjectiveCoefficientNumbers()
01344 {
01345 processObjectives();
01346 return m_miNumberOfObjCoef;
01347 }
01348
01349 double* OSInstance::getObjectiveConstants()
01350 {
01351 processObjectives();
01352 return m_mdObjectiveConstants;
01353 }
01354
01355 double* OSInstance::getObjectiveWeights()
01356 {
01357 processObjectives();
01358 return m_mdObjectiveWeights;
01359 }
01360
01361 SparseVector** OSInstance::getObjectiveCoefficients()
01362 {
01363 processObjectives();
01364 return m_mObjectiveCoefficients;
01365 }
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 }
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 }
01415
01416 bool OSInstance::processConstraints()
01417 {
01418 if(m_bProcessConstraints == true && bConstraintsModified == false) return true;
01419
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 }
01479
01480
01481 string* OSInstance::getConstraintNames()
01482 {
01483 processConstraints();
01484 return m_msConstraintNames;
01485 }
01486
01487
01488 double* OSInstance::getConstraintLowerBounds()
01489 {
01490 processConstraints();
01491 return m_mdConstraintLowerBounds;
01492 }
01493
01494 char* OSInstance::getConstraintTypes()
01495 {
01496 processConstraints();
01497 return m_mcConstraintTypes;
01498 }
01499
01500 double* OSInstance::getConstraintUpperBounds()
01501 {
01502 processConstraints();
01503 return m_mdConstraintUpperBounds;
01504 }
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 }
01518
01519 bool OSInstance::processLinearConstraintCoefficients()
01520 {
01521 if(m_bProcessLinearConstraintCoefficients == true && bAMatrixModified == false) return true;
01522
01523 try
01524 {
01525 int n = getLinearConstraintCoefficientNumber();
01526 if((instanceData->linearConstraintCoefficients == NULL ) || (n == 0) ) return true;
01527
01528 if((instanceData->linearConstraintCoefficients->value == NULL ) || (n == 0) ) return true;
01529
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 }
01582
01583 bool OSInstance::getLinearConstraintCoefficientMajor()
01584 {
01585 processLinearConstraintCoefficients();
01586 return m_bColumnMajor;
01587 }
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 }
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 }
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 }
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 }
01674
01675 int OSInstance::getNumberOfQuadraticRowIndexes()
01676 {
01677 if(m_bQuadraticRowIndexesProcessed == false) getQuadraticRowIndexes();
01678 return m_iNumberOfQuadraticRowIndexes;
01679 }
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
01697 foundIdx[ qTerms->rowIndexes[ i] ];
01698 }
01699
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 }
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 }
01727
01728 int OSInstance::getNumberOfNonlinearExpressionTreeIndexes()
01729 {
01730 if(m_bNonlinearExpressionTreeIndexesProcessed == false) getNonlinearExpressionTreeIndexes();
01731 return m_iNumberOfNonlinearExpressionTreeIndexes;
01732 }
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
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 }
01759
01760 int OSInstance::getNumberOfNonlinearExpressionTreeModIndexes()
01761 {
01762 if(m_bNonlinearExpressionTreeModIndexesProcessed == false) getNonlinearExpressionTreeModIndexes();
01763 return m_iNumberOfNonlinearExpressionTreeModIndexes;
01764 }
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
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 }
01791
01792 int OSInstance::getNumberOfNonlinearConstraints()
01793 {
01794 if( m_bProcessExpressionTrees == false )
01795 getAllNonlinearExpressionTrees();
01796 return m_iConstraintNumberNonlinear;
01797 }
01798
01799 int OSInstance::getNumberOfNonlinearObjectives()
01800 {
01801 if( m_bProcessExpressionTrees == false )
01802 getAllNonlinearExpressionTrees();
01803 return m_iObjectiveNumberNonlinear;
01804 }
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
01816
01818
01819
01820
01821
01822 }
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
01834
01836
01837
01838
01839
01840 }
01841
01842 std::vector<OSnLNode*> OSInstance::getNonlinearExpressionTreeInPostfix( int rowIdx)
01843 {
01844
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 }
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
01900
01901
01902 OSExpressionTree* exptree = this->getNonlinearExpressionTree( rowIdx);
01903 if(exptree != NULL)
01904 {
01905 postfixVec = this->getNonlinearExpressionTreeInPostfix( rowIdx);
01906 n = postfixVec.size();
01907
01908 for (i = 0 ; i < n; i++)
01909 {
01910 nlnode = postfixVec[ n - 1 - i];
01911 opStack.push( nlnode);
01912
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
01928 nlnode = opStack.top();
01929
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
01948 nlnodeVar = (OSnLNodeVariable*)nlnode;
01949
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
01959 }
01960 else
01961 {
01962 outStr << "x_";
01963 outStr << nlnodeVar->idx;
01964 tmpStack.push(outStr.str() );
01965
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
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
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
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
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
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
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
02187 tmpStack.pop();
02188
02189 return resultString;
02190 }
02191 else
02192 {
02193
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 }
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
02263
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
02274
02275
02276
02277
02278
02279
02280
02281 return outStr.str() ;
02282 }
02283
02284
02285 std::string OSInstance::printModel(int rowIdx )
02286 {
02287 std::string resultString = "";
02288 ostringstream outStr;
02289 outStr << "";
02290
02291 int j;
02292 int row_nonz = 0;
02293 int obj_nonz = 0;
02294 int varIdx = 0;
02295 bool addedLinearTerm = false;
02296
02297
02298 this->initForAlgDiff( );
02299
02300 if( rowIdx >= 0)
02301 {
02302 if (rowIdx < this->getConstraintNumber())
02303 {
02304 if( m_bProcessConstraints != true || bConstraintsModified == true) this->processConstraints() ;
02305 if( m_mdConstraintLowerBounds[ rowIdx] > -OSDBL_MAX)
02306 {
02307 if(m_mdConstraintLowerBounds[ rowIdx] < m_mdConstraintUpperBounds[ rowIdx])
02308 {
02309 outStr << os_dtoa_format( m_mdConstraintLowerBounds[ rowIdx] );
02310 outStr << " <= ";
02311 }
02312 }
02313
02314 if(this->instanceData->linearConstraintCoefficients != NULL && this->instanceData->linearConstraintCoefficients->numberOfValues > 0)
02315 {
02316 if(m_linearConstraintCoefficientsInRowMajor == NULL)
02317 m_linearConstraintCoefficientsInRowMajor = this->getLinearConstraintCoefficientsInRowMajor();
02318 row_nonz = m_linearConstraintCoefficientsInRowMajor->starts[ rowIdx + 1] - m_linearConstraintCoefficientsInRowMajor->starts[ rowIdx];
02319
02320 for(j = 0; j < row_nonz; j++)
02321 {
02322 varIdx = m_linearConstraintCoefficientsInRowMajor->indexes[ m_linearConstraintCoefficientsInRowMajor->starts[ rowIdx] + j];
02323
02324 if(m_bSparseJacobianCalculated == false || (m_mapExpressionTreesMod.find( rowIdx) == m_mapExpressionTreesMod.end() ) ||
02325 ( (*m_mapExpressionTreesMod[ rowIdx]->mapVarIdx).find( varIdx) == (*m_mapExpressionTreesMod[ rowIdx]->mapVarIdx).end()) )
02326 {
02327 outStr << os_dtoa_format( m_linearConstraintCoefficientsInRowMajor->values[ m_linearConstraintCoefficientsInRowMajor->starts[ rowIdx] + j] );
02328 outStr << "*";
02329
02330 if(this->instanceData->variables->numberOfVariables > 0 && this->instanceData->variables->var[ varIdx ]->name.size() > 0)
02331 {
02332 outStr << this->instanceData->variables->var[ varIdx ]->name;
02333 }
02334 else
02335 {
02336 outStr << "x_";
02337 outStr << varIdx;
02338 }
02339
02340
02341 if( j < row_nonz - 1) outStr << " + ";
02342 addedLinearTerm = true;
02343 }
02344 }
02345 }
02346 }
02347 else
02348 return "row index not found; print command ignored\n";
02349 }
02350 else
02351 {
02352 if(m_bProcessObjectives != true || bObjectivesModified == true) this->processObjectives() ;
02353 int obj_idx = -rowIdx - 1;
02354 if (obj_idx < this->getObjectiveNumber())
02355 {
02356 obj_nonz = m_miNumberOfObjCoef[ obj_idx];
02357 for(j = 0; j < obj_nonz; j++)
02358 {
02359 outStr << os_dtoa_format( m_mObjectiveCoefficients[obj_idx]->values[j] );
02360 outStr << "*";
02361 if(this->instanceData->variables->numberOfVariables > 0 && this->instanceData->variables->var[ m_mObjectiveCoefficients[obj_idx]->indexes[j] ]->name.size() > 0)
02362 {
02363 outStr << this->instanceData->variables->var[ m_mObjectiveCoefficients[obj_idx]->indexes[j] ]->name;
02364 }
02365 else
02366 {
02367 outStr << "x_";
02368 outStr << m_mObjectiveCoefficients[obj_idx]->indexes[j] ;
02369 }
02370 if( j < obj_nonz - 1) outStr << " + ";
02371 }
02372 }
02373 else
02374 return "row index not found; print command ignored\n";
02375 }
02376 if( this->getNonlinearExpressionTree( rowIdx) != NULL)
02377 {
02378 if( (addedLinearTerm == true) || (obj_nonz > 0) ) outStr << " + " ;
02379 outStr << getNonlinearExpressionTreeInInfix( rowIdx);
02380
02381 }
02382
02383 if( rowIdx >= 0)
02384 {
02385 if( m_bProcessConstraints != true ) this->processConstraints() ;
02386 if( m_mdConstraintUpperBounds[ rowIdx] < OSDBL_MAX)
02387 {
02388 if(m_mdConstraintLowerBounds[ rowIdx] < m_mdConstraintUpperBounds[ rowIdx])
02389 {
02390 outStr << " <= ";
02391 }
02392 else
02393 {
02394 outStr << " = ";
02395 }
02396 outStr << os_dtoa_format( m_mdConstraintUpperBounds[ rowIdx] );
02397 outStr << " ";
02398 outStr << this->getConstraintNames()[ rowIdx] ;
02399 }
02400 }
02401 outStr << std::endl;
02402 resultString = outStr.str();
02403 return resultString;
02404 }
02405
02406
02407 std::vector<OSnLNode*> OSInstance::getNonlinearExpressionTreeModInPostfix( int rowIdx)
02408 {
02409
02410 if( m_bProcessExpressionTreesMod == false ) getAllNonlinearExpressionTreesMod();
02411 std::vector<OSnLNode*> postfixVec;
02412 try
02413 {
02414 if( m_mapExpressionTreesMod.find( rowIdx) != m_mapExpressionTreesMod.end())
02415 {
02416 OSExpressionTree* expTree = getNonlinearExpressionTreeMod( rowIdx);
02417 postfixVec = expTree->m_treeRoot->getPostfixFromExpressionTree();
02418
02419 }
02420 else
02421 {
02422 throw ErrorClass("Error in getNonlinearExpressionTreeModInPostfix, rowIdx not valid");
02423 }
02424 return postfixVec;
02425 }
02426 catch(const ErrorClass& eclass)
02427 {
02428 throw ErrorClass( eclass.errormsg);
02429 }
02430 }
02431
02432
02433 std::vector<OSnLNode*> OSInstance::getNonlinearExpressionTreeInPrefix( int rowIdx)
02434 {
02435
02436 if( m_bProcessExpressionTrees == false ) getAllNonlinearExpressionTrees();
02437 std::vector<OSnLNode*> prefixVec;
02438 try
02439 {
02440 if( m_mapExpressionTrees.find( rowIdx) != m_mapExpressionTrees.end())
02441 {
02442 OSExpressionTree* expTree = getNonlinearExpressionTree( rowIdx);
02443 prefixVec = expTree->m_treeRoot->getPrefixFromExpressionTree();
02444
02445 }
02446 else
02447 {
02448 throw ErrorClass("Error in getNonlinearExpressionTreeInPrefix, rowIdx not valid");
02449 }
02450 return prefixVec;
02451 }
02452 catch(const ErrorClass& eclass)
02453 {
02454 throw ErrorClass( eclass.errormsg);
02455 }
02456 }
02457
02458 std::vector<OSnLNode*> OSInstance::getNonlinearExpressionTreeModInPrefix( int rowIdx)
02459 {
02460
02461 if( m_bProcessExpressionTreesMod == false ) getAllNonlinearExpressionTreesMod();
02462 std::vector<OSnLNode*> prefixVec;
02463 try
02464 {
02465 if( m_mapExpressionTreesMod.find( rowIdx) != m_mapExpressionTreesMod.end())
02466 {
02467 OSExpressionTree* expTree = getNonlinearExpressionTreeMod( rowIdx);
02468 prefixVec = expTree->m_treeRoot->getPrefixFromExpressionTree();
02469
02470 }
02471 else
02472 {
02473 throw ErrorClass("Error in getNonlinearExpressionTreeInPrefix, rowIdx not valid");
02474 }
02475 return prefixVec;
02476 }
02477 catch(const ErrorClass& eclass)
02478 {
02479 throw ErrorClass( eclass.errormsg);
02480 }
02481 }
02482
02483 std::map<int, OSExpressionTree*> OSInstance::getAllNonlinearExpressionTrees()
02484 {
02485
02486 if(m_bProcessExpressionTrees == true) return m_mapExpressionTrees;
02487 std::map<int, int> foundIdx;
02488 std::map<int, int>::iterator pos;
02489 OSnLNodePlus *nlNodePlus;
02490 OSExpressionTree *expTree;
02491 m_iObjectiveNumberNonlinear = 0;
02492 m_iConstraintNumberNonlinear = 0;
02493 int i;
02494
02495 if( instanceData->nonlinearExpressions->numberOfNonlinearExpressions > 0 && instanceData->nonlinearExpressions->nl != NULL)
02496 {
02497 for( i = 0; i < instanceData->nonlinearExpressions->numberOfNonlinearExpressions; i++)
02498 {
02499 instanceData->nonlinearExpressions->nl[i]->m_bDeleteExpressionTree = false;
02500 }
02501 }
02502 int index;
02503
02504 for(i = 0; i < instanceData->nonlinearExpressions->numberOfNonlinearExpressions; i++)
02505 {
02506 index = instanceData->nonlinearExpressions->nl[ i]->idx;
02507 if(foundIdx.find( index) != foundIdx.end() )
02508 {
02509
02510
02511
02512
02513
02514
02515
02516 nlNodePlus = new OSnLNodePlus();
02517
02518 expTree = instanceData->nonlinearExpressions->nl[ i]->osExpressionTree;
02519
02520 nlNodePlus->m_mChildren[ 0] = m_mapExpressionTrees[ index]->m_treeRoot;
02521 nlNodePlus->m_mChildren[ 1] = instanceData->nonlinearExpressions->nl[ i]->osExpressionTree->m_treeRoot;
02522
02523 instanceData->nonlinearExpressions->nl[ foundIdx[ index] ]->m_bDeleteExpressionTree = true;
02524 instanceData->nonlinearExpressions->nl[ foundIdx[ index] ]->osExpressionTree->bDestroyNlNodes = false;
02525
02526 m_mapExpressionTrees[ index] = expTree;
02527 m_mapExpressionTrees[ index]->m_treeRoot = nlNodePlus;
02528 foundIdx[ index] = i;
02529 }
02530 else
02531 {
02532
02533 m_mapExpressionTrees[ index] = instanceData->nonlinearExpressions->nl[ i]->osExpressionTree;
02534 m_mapExpressionTrees[ index]->m_treeRoot = instanceData->nonlinearExpressions->nl[ i]->osExpressionTree->m_treeRoot;
02535 foundIdx[ index] = i;
02536 }
02537
02538 }
02539
02540 for(pos = foundIdx.begin(); pos != foundIdx.end(); ++pos)
02541 {
02542 if(pos->first == -1)
02543 {
02544 m_iObjectiveNumberNonlinear++;
02545 }
02546 else m_iConstraintNumberNonlinear++;
02547 }
02548 foundIdx.clear();
02549 m_bProcessExpressionTrees = true;
02550 return m_mapExpressionTrees;
02551 }
02552
02553 std::map<int, OSExpressionTree*> OSInstance::getAllNonlinearExpressionTreesMod()
02554 {
02555 if( m_bProcessExpressionTreesMod == true ) return m_mapExpressionTreesMod;
02556 m_bProcessExpressionTreesMod = true;
02557
02558 if( m_bNonLinearStructuresInitialized == false) initializeNonLinearStructures( );
02559 return m_mapExpressionTreesMod;
02560 }
02561
02562
02563 std::string OSInstance::getTimeDomainFormat()
02564 {
02565 if (instanceData->timeDomain == NULL)
02566 return "";
02567 if (instanceData->timeDomain->interval != NULL)
02568 return "interval";
02569 if (instanceData->timeDomain->stages != NULL)
02570 return "stages";
02571 return "";
02572 }
02573
02574 int OSInstance::getTimeDomainStageNumber()
02575 {
02576 if (instanceData->timeDomain == NULL)
02577 return 1;
02578 if (instanceData->timeDomain->interval != NULL)
02579 ;
02580 if (instanceData->timeDomain->stages == NULL)
02581 return 1;
02582 return instanceData->timeDomain->stages->numberOfStages;
02583 }
02584
02585 std::string* OSInstance::getTimeDomainStageNames()
02586 {
02587 if (instanceData->timeDomain == NULL)
02588 return NULL;
02589 if (instanceData->timeDomain->interval != NULL)
02590 return NULL;
02591 if (instanceData->timeDomain->stages == NULL)
02592 return NULL;
02593 if (m_msTimeDomainStageNames != NULL)
02594 delete [] m_msTimeDomainStageNames;
02595 if (instanceData->timeDomain->stages->numberOfStages == 0)
02596 return NULL;
02597 m_msTimeDomainStageNames = new std::string[instanceData->timeDomain->stages->numberOfStages];
02598 for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
02599 m_msTimeDomainStageNames[i] = instanceData->timeDomain->stages->stage[i]->name;
02600 return m_msTimeDomainStageNames;
02601 }
02602
02603
02604 int* OSInstance::getTimeDomainStageNumberOfVariables()
02605 {
02606 if (instanceData->timeDomain == NULL)
02607 return NULL;
02608 if (instanceData->timeDomain->interval != NULL)
02609 return NULL;
02610 if (instanceData->timeDomain->stages == NULL)
02611 return NULL;
02612 if (m_miTimeDomainStageVariableNumber != NULL)
02613 delete [] m_miTimeDomainStageVariableNumber;
02614 if (instanceData->timeDomain->stages->numberOfStages == 0)
02615 return NULL;
02616 m_miTimeDomainStageVariableNumber = new int[instanceData->timeDomain->stages->numberOfStages];
02617 for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
02618 m_miTimeDomainStageVariableNumber[i] = instanceData->timeDomain->stages->stage[i]->variables->numberOfVariables;
02619 return m_miTimeDomainStageVariableNumber;
02620 }
02621
02622 int* OSInstance::getTimeDomainStageNumberOfConstraints()
02623 {
02624 if (instanceData->timeDomain == NULL)
02625 return NULL;
02626 if (instanceData->timeDomain->interval != NULL)
02627 return NULL;
02628 if (instanceData->timeDomain->stages == NULL)
02629 return NULL;
02630 if (m_miTimeDomainStageConstraintNumber != NULL)
02631 delete [] m_miTimeDomainStageConstraintNumber;
02632 if (instanceData->timeDomain->stages->numberOfStages == 0)
02633 return NULL;
02634 m_miTimeDomainStageConstraintNumber = new int[instanceData->timeDomain->stages->numberOfStages];
02635 for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
02636 m_miTimeDomainStageConstraintNumber[i] = instanceData->timeDomain->stages->stage[i]->constraints->numberOfConstraints;
02637 return m_miTimeDomainStageConstraintNumber;
02638 }
02639
02640 int* OSInstance::getTimeDomainStageNumberOfObjectives()
02641 {
02642 if (instanceData->timeDomain == NULL)
02643 return NULL;
02644 if (instanceData->timeDomain->interval != NULL)
02645 return NULL;
02646 if (instanceData->timeDomain->stages == NULL)
02647 return NULL;
02648 if (m_miTimeDomainStageObjectiveNumber != NULL)
02649 delete [] m_miTimeDomainStageObjectiveNumber;
02650 if (instanceData->timeDomain->stages->numberOfStages == 0)
02651 return NULL;
02652 m_miTimeDomainStageObjectiveNumber = new int[instanceData->timeDomain->stages->numberOfStages];
02653 for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
02654 m_miTimeDomainStageObjectiveNumber[i] = instanceData->timeDomain->stages->stage[i]->objectives->numberOfObjectives;
02655 return m_miTimeDomainStageObjectiveNumber;
02656 }
02657
02658 int** OSInstance::getTimeDomainStageVarList()
02659 {
02660 if (instanceData->timeDomain == NULL)
02661 return NULL;
02662 if (instanceData->timeDomain->interval != NULL)
02663 return NULL;
02664 if (instanceData->timeDomain->stages == NULL)
02665 return NULL;
02666 if (m_miTimeDomainStageVariableNumber == NULL)
02667 return NULL;
02668 if (m_mmiTimeDomainStageVarList != NULL)
02669 {
02670 for (int i = 0; i < m_iNumberOfTimeStages; i ++)
02671 delete[] m_mmiTimeDomainStageVarList[i];
02672 delete[] m_mmiTimeDomainStageVarList;
02673 m_mmiTimeDomainStageVarList = NULL;
02674 }
02675
02676
02677 if (instanceData->timeDomain->stages->numberOfStages == 0)
02678 return NULL;
02679 m_iNumberOfTimeStages = instanceData->timeDomain->stages->numberOfStages;
02680 m_mmiTimeDomainStageVarList = new int*[instanceData->timeDomain->stages->numberOfStages];
02681 int timeDomainStageNumberVar;
02682 for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
02683 {
02684 timeDomainStageNumberVar = m_miTimeDomainStageVariableNumber[i];
02685 m_mmiTimeDomainStageVarList[i] = new int[ timeDomainStageNumberVar ];
02686 if (instanceData->timeDomain->stages->stage[i]->variables->startIdx == -1)
02687 for (int j = 0; j < m_miTimeDomainStageVariableNumber[i]; j++)
02688 m_mmiTimeDomainStageVarList[i][j] = instanceData->timeDomain->stages->stage[i]->variables->var[j]->idx;
02689 else
02690 for (int j = 0; j < m_miTimeDomainStageVariableNumber[i]; j++)
02691 m_mmiTimeDomainStageVarList[i][j] = instanceData->timeDomain->stages->stage[i]->variables->startIdx + j;
02692 }
02693 return m_mmiTimeDomainStageVarList;
02694 }
02695
02696 int** OSInstance::getTimeDomainStageConList()
02697 {
02698 if (instanceData->timeDomain == NULL)
02699 return NULL;
02700 if (instanceData->timeDomain->interval != NULL)
02701 return NULL;
02702 if (instanceData->timeDomain->stages == NULL)
02703 return NULL;
02704 if (m_miTimeDomainStageConstraintNumber == NULL)
02705 return NULL;
02706 if (m_mmiTimeDomainStageConList != NULL)
02707 {
02708
02709 for (int i = 0; i < m_iNumberOfTimeStages; i ++)
02710 delete[] m_mmiTimeDomainStageConList[i];
02711 delete[] m_mmiTimeDomainStageConList;
02712 m_mmiTimeDomainStageConList = NULL;
02713 }
02714
02715 if (instanceData->timeDomain->stages->numberOfStages == 0)
02716 return NULL;
02717 m_iNumberOfTimeStages = instanceData->timeDomain->stages->numberOfStages;
02718 m_mmiTimeDomainStageConList = new int*[instanceData->timeDomain->stages->numberOfStages];
02719 int numTimeDomainStageCon;
02720 for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
02721 {
02722 numTimeDomainStageCon = m_miTimeDomainStageConstraintNumber[i];
02723 m_mmiTimeDomainStageConList[i] = new int[ numTimeDomainStageCon];
02724 if (instanceData->timeDomain->stages->stage[i]->constraints->startIdx == -1)
02725 for (int j = 0; j < m_miTimeDomainStageConstraintNumber[i]; j++)
02726 m_mmiTimeDomainStageConList[i][j] = instanceData->timeDomain->stages->stage[i]->constraints->con[j]->idx;
02727 else
02728 for (int j = 0; j < m_miTimeDomainStageConstraintNumber[i]; j++)
02729 m_mmiTimeDomainStageConList[i][j] = instanceData->timeDomain->stages->stage[i]->constraints->startIdx + j;
02730 }
02731 return m_mmiTimeDomainStageConList;
02732 }
02733
02734 int** OSInstance::getTimeDomainStageObjList()
02735 {
02736 if (instanceData->timeDomain == NULL)
02737 return NULL;
02738 if (instanceData->timeDomain->interval != NULL)
02739 return NULL;
02740 if (instanceData->timeDomain->stages == NULL)
02741 return NULL;
02742 if (m_miTimeDomainStageObjectiveNumber == NULL)
02743 return NULL;
02744 if (m_mmiTimeDomainStageObjList != NULL)
02745 {
02746 for (int i = 0; i < m_iNumberOfTimeStages; i ++)
02747 delete[] m_mmiTimeDomainStageObjList[i];
02748 delete[] m_mmiTimeDomainStageObjList;
02749 m_mmiTimeDomainStageObjList = NULL;
02750 }
02751 if (instanceData->timeDomain->stages->numberOfStages == 0)
02752 return NULL;
02753 m_iNumberOfTimeStages = instanceData->timeDomain->stages->numberOfStages;
02754 m_mmiTimeDomainStageObjList = new int*[instanceData->timeDomain->stages->numberOfStages];
02755 int numTimeDomainStageObjNum;
02756 for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
02757 {
02758 numTimeDomainStageObjNum = m_miTimeDomainStageObjectiveNumber[i];
02759 m_mmiTimeDomainStageObjList[i] = new int[ numTimeDomainStageObjNum];
02760 if (instanceData->timeDomain->stages->stage[i]->objectives->startIdx == 0)
02761 for (int j = 0; j < m_miTimeDomainStageObjectiveNumber[i]; j++)
02762 m_mmiTimeDomainStageObjList[i][j] = instanceData->timeDomain->stages->stage[i]->objectives->obj[j]->idx;
02763 else
02764 for (int j = 0; j < m_miTimeDomainStageObjectiveNumber[i]; j++)
02765 m_mmiTimeDomainStageObjList[i][j] = instanceData->timeDomain->stages->stage[i]->objectives->startIdx - j;
02766 }
02767 return m_mmiTimeDomainStageObjList;
02768 }
02769
02770 double OSInstance::getTimeDomainIntervalStart()
02771 {
02772 if (instanceData->timeDomain == NULL)
02773 return 0.0;
02774 if (instanceData->timeDomain->stages != NULL)
02775 return 0.0;
02776 if (instanceData->timeDomain->interval == NULL)
02777 return 0.0;
02778 return instanceData->timeDomain->interval->start;
02779 }
02780
02781 double OSInstance::getTimeDomainIntervalHorizon()
02782 {
02783 if (instanceData->timeDomain == NULL)
02784 return 0.0;
02785 if (instanceData->timeDomain->stages != NULL)
02786 return 0.0;
02787 if (instanceData->timeDomain->interval == NULL)
02788 return 0.0;
02789 return instanceData->timeDomain->interval->horizon;
02790 }
02791
02792
02793
02794
02795
02796
02797 bool OSInstance::setInstanceSource(string source)
02798 {
02799 instanceHeader->source = source;
02800 return true;
02801 }
02802
02803 bool OSInstance::setInstanceDescription(string description)
02804 {
02805 instanceHeader->description = description;
02806 return true;
02807 }
02808
02809
02810 bool OSInstance::setInstanceName(string name)
02811 {
02812 instanceHeader->name = name;
02813 return true;
02814 }
02815
02816
02817 bool OSInstance::setVariableNumber(int number)
02818 {
02819
02820 if(number < 0) return false;
02821
02822
02823
02824
02825 if(instanceData->variables == NULL) instanceData->variables = new Variables();
02826 instanceData->variables->numberOfVariables = number;
02827 if(instanceData->variables->var == NULL && number > 0)
02828 {
02829 instanceData->variables->var = new Variable*[number];
02830 }
02831 return true;
02832 }
02833
02834
02835 bool OSInstance::addVariable(int index, string name, double lowerBound, double upperBound, char type)
02836 {
02837 if (verifyVarType(type) == false) type = 'C';
02838 instanceData->variables->var[index] = new Variable();
02839 if(index < 0 || instanceData->variables->numberOfVariables <= 0 || index >= instanceData->variables->numberOfVariables) return false;
02840 instanceData->variables->var[index]->name = name;
02841 instanceData->variables->var[index]->lb = lowerBound;
02842 instanceData->variables->var[index]->ub = upperBound;
02843 instanceData->variables->var[index]->type = type;
02844
02845
02846 return true;
02847 }
02848
02849
02850 bool OSInstance::setVariables(int number, string *names, double *lowerBounds,
02851 double *upperBounds, char *types)
02852 {
02853 if(number <= 0) return false;
02854 try
02855 {
02856 if(instanceData->variables == NULL)
02857 {
02858 throw ErrorClass("There is no variables object");
02859 }
02860 if(instanceData->variables->numberOfVariables != number)
02861 {
02862 throw ErrorClass("input number of variables not equal to number in class");
02863 }
02864
02865 int i;
02866 for(i = 0; i < number; i++)
02867 {
02868 instanceData->variables->var[ i] = new Variable();
02869 }
02870 if(names != NULL)
02871 {
02872 for(i = 0; i < number; i++) instanceData->variables->var[i]->name = names[i];
02873 }
02874 if(lowerBounds != NULL)
02875 {
02876 for(i = 0; i < number; i++)
02877 {
02878 instanceData->variables->var[i]->lb = lowerBounds[i];
02879 }
02880 }
02881 if(upperBounds != NULL)
02882 {
02883 for(i = 0; i < number; i++)
02884 {
02885 instanceData->variables->var[i]->ub = upperBounds[i];
02886 }
02887 }
02888 if(types != NULL)
02889 {
02890 for(i = 0; i < number; i++)
02891 {
02892 instanceData->variables->var[i]->type = types[i];
02893 if(verifyVarType(types[i]) == false) types[i] = 'C';
02894 }
02895 }
02896
02897
02898
02899
02900
02901
02902
02903
02904 return true;
02905 }
02906 catch(const ErrorClass& eclass)
02907 {
02908 throw ErrorClass( eclass.errormsg);
02909 }
02910 }
02911
02912
02913
02914 bool OSInstance::setObjectiveNumber(int number)
02915 {
02916 if(number < 0) return false;
02917 if(instanceData->objectives == NULL) instanceData->objectives = new Objectives();
02918 if(number == 0)
02919 {
02920 instanceData->objectives->numberOfObjectives = 0;
02921 instanceData->objectives->obj = 0;
02922 return true;
02923 }
02924 instanceData->objectives->numberOfObjectives = number;
02925 instanceData->objectives->obj = new Objective*[number];
02926 return true;
02927 }
02928
02929 bool OSInstance::addObjective(int index, string name, string maxOrMin, double constant, double weight, SparseVector *objectiveCoefficients)
02930 {
02931 if(index >= 0 || instanceData->objectives->numberOfObjectives <= 0 || abs(index) > instanceData->objectives->numberOfObjectives) return false;
02932 int arrayIndex = abs(index) -1;
02933 if(instanceData->objectives->obj == NULL) return false;
02934 instanceData->objectives->obj[arrayIndex] = new Objective();
02935 instanceData->objectives->obj[arrayIndex]->name = name;
02936 if( (maxOrMin != "max") && (maxOrMin != "min") ) return false;
02937 else instanceData->objectives->obj[arrayIndex]->maxOrMin = maxOrMin;
02938 instanceData->objectives->obj[arrayIndex]->constant = constant;
02939 instanceData->objectives->obj[arrayIndex]->weight = weight;
02940 int n = objectiveCoefficients->number;
02941 instanceData->objectives->obj[arrayIndex]->numberOfObjCoef = n;
02942 if(n == 0)
02943 {
02944 instanceData->objectives->obj[arrayIndex]->coef = 0;
02945 }
02946 else
02947 {
02948 int i = 0;
02949 instanceData->objectives->obj[arrayIndex]->coef = new ObjCoef*[n];
02950 for(i = 0; i < n; i++) instanceData->objectives->obj[arrayIndex]->coef[i] = new ObjCoef();
02951 for(i = 0; i < n; i++)
02952 {
02953 instanceData->objectives->obj[arrayIndex]->coef[i]->idx = objectiveCoefficients->indexes[i];
02954 instanceData->objectives->obj[arrayIndex]->coef[i]->value = objectiveCoefficients->values[i];
02955 }
02956 }
02957 return true;
02958 }
02959
02960 bool OSInstance::setObjectives(int number, string *names, string *maxOrMins, double *constants, double *weights, SparseVector **objectiveCoefficients)
02961 {
02962 if(number < 0) return false;
02963 try
02964 {
02965 if(instanceData->objectives == NULL)
02966 {
02967 throw ErrorClass("there is no objectives object");
02968 }
02969 if(instanceData->objectives->numberOfObjectives != number)
02970 {
02971 throw ErrorClass("input number of objective not equal to number in class");
02972 }
02973 if(number == 0) return true;
02974 int i = 0;
02975 for(i = 0; i < number; i++)instanceData->objectives->obj[i] = new Objective();
02976 int j = 0;
02977 if(names != NULL)
02978 {
02979 for(i = 0; i < number; i++) instanceData->objectives->obj[i]->name = names[i];
02980 }
02981 if(maxOrMins != NULL)
02982 {
02983 for(i = 0; i < number; i++)
02984 {
02985 if(maxOrMins[i] == "" || (maxOrMins[i].compare("max") != 0 && maxOrMins[i].compare("min") !=0)) return false;
02986 instanceData->objectives->obj[i]->maxOrMin = maxOrMins[i];
02987 }
02988 }
02989 if(constants != NULL)
02990 {
02991 for(i = 0; i < number; i++) instanceData->objectives->obj[i]->constant = constants[i];
02992 }
02993 if(weights != NULL)
02994 {
02995 for(i = 0; i < number; i++) instanceData->objectives->obj[i]->weight = weights[i];
02996 }
02997 if(objectiveCoefficients != NULL)
02998 {
02999 for(i = 0; i < number; i++)
03000 {
03001 int n = (&objectiveCoefficients[i] == NULL || objectiveCoefficients[i]->indexes == NULL)?0:objectiveCoefficients[i]->number;
03002 instanceData->objectives->obj[i]->numberOfObjCoef = n;
03003 if(n == 0)
03004 {
03005 instanceData->objectives->obj[i]->coef = NULL;
03006 }
03007 else
03008 {
03009 instanceData->objectives->obj[i]->coef = new ObjCoef*[n];
03010 for(j = 0; j < n; j++)
03011 {
03012 instanceData->objectives->obj[i]->coef[j] = new ObjCoef();
03013 instanceData->objectives->obj[i]->coef[j]->idx = objectiveCoefficients[i]->indexes[j];
03014 instanceData->objectives->obj[i]->coef[j]->value = objectiveCoefficients[i]->values[j];
03015 }
03016 }
03017 }
03018 }
03019 return true;
03020 }
03021 catch(const ErrorClass& eclass)
03022 {
03023 throw ErrorClass( eclass.errormsg);
03024 }
03025 }
03026
03027
03028 bool OSInstance::setConstraintNumber(int number)
03029 {
03030 if(number < 0) return false;
03031 if(instanceData->constraints == NULL) instanceData->constraints = new Constraints();
03032 if(number == 0)
03033 {
03034 instanceData->constraints->numberOfConstraints = 0;
03035 instanceData->constraints->con = 0;
03036 return true;
03037 }
03038 instanceData->constraints->numberOfConstraints = number;
03039 if(instanceData->constraints->con == 0 )
03040 {
03041 instanceData->constraints->con = new Constraint*[number];
03042 }
03043 return true;
03044 }
03045
03046 bool OSInstance::addConstraint(int index, string name, double lowerBound, double upperBound, double constant)
03047 {
03048 instanceData->constraints->con[ index] = new Constraint();
03049 if(index < 0 || instanceData->constraints->numberOfConstraints <= 0 || index >= instanceData->constraints->numberOfConstraints) return false;
03050 instanceData->constraints->con[ index]->name = name;
03051 if(lowerBound != -OSDBL_MAX && lowerBound != -OSDBL_MAX) instanceData->constraints->con[ index]->lb = lowerBound;
03052 if(upperBound != OSDBL_MAX && upperBound != OSDBL_MAX)instanceData->constraints->con[ index]->ub = upperBound;
03053 instanceData->constraints->con[ index]->constant = constant;
03054 return true;
03055 }
03056
03057
03058 bool OSInstance::setConstraints(int number, string* names, double* lowerBounds, double* upperBounds, double* constants)
03059 {
03060 if(number < 0) return false;
03061 if(number == 0)
03062 {
03063
03064
03065
03066
03067 return true;
03068 }
03069 try
03070 {
03071
03072 if(instanceData->constraints == NULL)
03073 {
03074 throw ErrorClass("there is no constraints object");
03075 }
03076 if(instanceData->constraints->numberOfConstraints != number)
03077 {
03078 throw ErrorClass("input number of constraints not equal to number in class");
03079 }
03080 int i = 0;
03081 for(i = 0; i < number; i++)
03082 {
03083 instanceData->constraints->con[i] = new Constraint();
03084 }
03085 if(names != NULL)
03086 {
03087 for(i = 0; i < number; i++) instanceData->constraints->con[i]->name = names[i];
03088 }
03089 if(lowerBounds != NULL)
03090 {
03091 for(i = 0; i < number; i++)
03092 {
03093 if(lowerBounds[i] != -OSDBL_MAX && lowerBounds[i] != -OSDBL_MAX)instanceData->constraints->con[i]->lb = lowerBounds[i];
03094 }
03095 }
03096 if(upperBounds != NULL)
03097 {
03098 for(i = 0; i < number; i++)
03099 {
03100 if(upperBounds[i] != OSDBL_MAX && upperBounds[i] != OSDBL_MAX)instanceData->constraints->con[i]->ub = upperBounds[i];
03101 }
03102 }
03103 if(constants != NULL)
03104 {
03105 for(i = 0; i < number; i++) instanceData->constraints->con[i]->constant = constants[i];
03106 }
03107 return true;
03108 }
03109 catch(const ErrorClass& eclass)
03110 {
03111 throw ErrorClass( eclass.errormsg);
03112 }
03113 }
03114
03115 bool OSInstance::setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor,
03116 double* values, int valuesBegin, int valuesEnd,
03117 int* indexes, int indexesBegin, int indexesEnd,
03118 int* starts, int startsBegin, int startsEnd)
03119 {
03120 if(numberOfValues < 0) return false;
03121 if(instanceData->linearConstraintCoefficients == NULL)
03122 instanceData->linearConstraintCoefficients = new LinearConstraintCoefficients() ;
03123 if(numberOfValues == 0) return true;
03124 if((values == 0 ) ||
03125 (valuesBegin < 0 || (valuesEnd - valuesBegin + 1) != numberOfValues) ||
03126 (indexes == 0) ||
03127 (indexesBegin < 0 || (indexesEnd - indexesBegin + 1) != numberOfValues) ||
03128 (starts == 0 ) ||
03129 (startsBegin < 0 || startsBegin >= startsEnd)) return false;
03130 instanceData->linearConstraintCoefficients->numberOfValues = numberOfValues;
03131 int i = 0;
03132
03133 if(instanceData->linearConstraintCoefficients->start == NULL) instanceData->linearConstraintCoefficients->start = new IntVector();
03134 if(startsBegin == 0 )
03135 {
03136 instanceData->linearConstraintCoefficients->start->el = starts;
03137 }
03138 else
03139 {
03140 instanceData->linearConstraintCoefficients->start->el = new int[startsEnd - startsBegin + 1];
03141 int k = 0;
03142 for(i = startsBegin; i <= startsEnd; i++)
03143 {
03144 instanceData->linearConstraintCoefficients->start->el[k] = starts[i];
03145 k++;
03146 }
03147 }
03148
03149 if(instanceData->linearConstraintCoefficients->value == NULL) instanceData->linearConstraintCoefficients->value = new DoubleVector();
03150 if(valuesBegin == 0 )
03151 {
03152 instanceData->linearConstraintCoefficients->value->el = values;
03153 }
03154 else
03155 {
03156 instanceData->linearConstraintCoefficients->value->el = new double[numberOfValues];
03157 int k = 0;
03158 for(i = valuesBegin; i <= valuesEnd; i++)
03159 {
03160 instanceData->linearConstraintCoefficients->value->el[k] = values[i];
03161 k++;
03162 }
03163 }
03164
03165 if(instanceData->linearConstraintCoefficients->rowIdx == NULL) instanceData->linearConstraintCoefficients->rowIdx = new IntVector();
03166 if(instanceData->linearConstraintCoefficients->colIdx == NULL) instanceData->linearConstraintCoefficients->colIdx = new IntVector();
03167 if(isColumnMajor)
03168 {
03169 if(indexesBegin == 0 )
03170 {
03171 instanceData->linearConstraintCoefficients->rowIdx->el = indexes;
03172 }
03173 else
03174 {
03175 instanceData->linearConstraintCoefficients->rowIdx->el = new int[numberOfValues];
03176 int k = 0;
03177 for(i = indexesBegin; i <= indexesEnd; i++)
03178 {
03179 instanceData->linearConstraintCoefficients->rowIdx->el[k] = indexes[i];
03180 k++;
03181 }
03182 }
03183 }
03184 else
03185 {
03186 if(indexesBegin == 0 )
03187 {
03188 instanceData->linearConstraintCoefficients->colIdx->el = indexes;
03189 }
03190 else
03191 {
03192 instanceData->linearConstraintCoefficients->colIdx->el = new int[numberOfValues];
03193 int k = 0;
03194 for(i = indexesBegin; i <= indexesEnd; i++)
03195 {
03196 instanceData->linearConstraintCoefficients->colIdx->el[k] = indexes[i];
03197 k++;
03198 }
03199 }
03200 }
03201 return true;
03202 }
03203
03204 bool OSInstance::setQuadraticTerms(int number,
03205 int* rowIndexes, int* varOneIndexes, int* varTwoIndexes, double* coefficients,
03206 int begin, int end)
03207 {
03208 if(number < 0) return false;
03209 if(number != (end - begin) + 1) return false;
03210 if(number == 0)
03211 {
03212 instanceData->quadraticCoefficients = 0;
03213 return true;
03214 }
03215 if( ((end - begin + 1) != number) ||
03216 (rowIndexes == 0) ||
03217 (varOneIndexes == 0) ||
03218 (varTwoIndexes == 0) ||
03219 (coefficients == 0) ) return false;
03220 instanceData->quadraticCoefficients = new QuadraticCoefficients();
03221 instanceData->quadraticCoefficients->numberOfQuadraticTerms = number;
03222 int i = 0;
03223 instanceData->quadraticCoefficients->qTerm = new QuadraticTerm*[number];
03224 for(i = 0; i < number; i++) instanceData->quadraticCoefficients->qTerm[i] = new QuadraticTerm();
03225 int k = 0;
03226 for(i = begin; i <= end; i++)
03227 {
03228 instanceData->quadraticCoefficients->qTerm[k]->idx = rowIndexes[i];
03229 instanceData->quadraticCoefficients->qTerm[k]->idxOne = varOneIndexes[i];
03230 instanceData->quadraticCoefficients->qTerm[k]->idxTwo = varTwoIndexes[i];
03231 instanceData->quadraticCoefficients->qTerm[k]->coef = coefficients[i];
03232 k++;
03233 }
03234 return true;
03235 }
03236
03237 bool OSInstance::setQuadraticTermsInNonlinearExpressions(int numQPTerms, int* rowIndexes, int* varOneIndexes, int* varTwoIndexes, double* coefficients)
03238 {
03239 instanceData->nonlinearExpressions->numberOfNonlinearExpressions = numQPTerms;
03240 instanceData->nonlinearExpressions->nl = new Nl*[ numQPTerms ];
03241
03242 OSnLNode *nlNodePoint;
03243 OSnLNodeVariable *nlNodeVariablePoint;
03244 std::vector<OSnLNode*> nlNodeVec;
03245
03246
03247 int i;
03248 for(i = 0; i < numQPTerms; i++)
03249 {
03250 instanceData->nonlinearExpressions->nl[ i] = new Nl();
03251 instanceData->nonlinearExpressions->nl[ i]->idx = rowIndexes[ i];
03252 instanceData->nonlinearExpressions->nl[ i]->osExpressionTree = new OSExpressionTree();
03253
03254 nlNodeVariablePoint = new OSnLNodeVariable();
03255 nlNodeVariablePoint->idx = varOneIndexes[ i];
03256
03257 nlNodeVariablePoint->coef = coefficients[ i];
03258 nlNodeVec.push_back( nlNodeVariablePoint);
03259
03260 nlNodeVariablePoint = new OSnLNodeVariable();
03261 nlNodeVariablePoint->idx = varTwoIndexes[ i];
03262 nlNodeVec.push_back( nlNodeVariablePoint);
03263
03264 nlNodePoint = new OSnLNodeTimes();
03265 nlNodeVec.push_back( nlNodePoint);
03266
03267
03268 instanceData->nonlinearExpressions->nl[ i]->osExpressionTree->m_treeRoot =
03269 nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
03270 nlNodeVec.clear();
03271 }
03272 return true;
03273 }
03274
03275 bool OSInstance::initializeNonLinearStructures( )
03276 {
03277 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
03278 if( m_bNonLinearStructuresInitialized == true) return true;
03279 if( m_bProcessVariables == false) processVariables();
03280 if( m_bProcessObjectives == false) processObjectives();
03281 if( m_bProcessConstraints == false) processConstraints();
03282 m_iVariableNumber = instanceData->variables->numberOfVariables;
03283 m_iConstraintNumber = instanceData->constraints->numberOfConstraints;
03284 m_iObjectiveNumber = instanceData->objectives->numberOfObjectives;
03285
03286 if( m_bProcessExpressionTrees == false) getAllNonlinearExpressionTrees();
03287
03288 if( m_bDuplicateExpressionTreesMap == false) duplicateExpressionTreesMap();
03289
03290 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
03291 {
03292 (posMapExpTree->second)->getVariableIndiciesMap() ;
03293 }
03294
03295 if(getNumberOfQuadraticTerms() > 0) addQTermsToExressionTree();
03296
03297 getAllNonlinearVariablesIndexMap( );
03298 getDenseObjectiveCoefficients();
03299 m_mdConstraintFunctionValues = new double[ this->instanceData->constraints->numberOfConstraints];
03300 m_mdObjectiveFunctionValues = new double[ this->instanceData->objectives->numberOfObjectives];
03301
03302 m_bNonLinearStructuresInitialized = true;
03303 m_bProcessVariables = true;
03304 m_bProcessObjectives = true;
03305 m_bProcessConstraints = true;
03306 m_bProcessExpressionTrees = true;
03307 m_bDuplicateExpressionTreesMap = true;
03308 return true;
03309 }
03310
03311 SparseJacobianMatrix *OSInstance::getJacobianSparsityPattern( )
03312 {
03313
03314
03315
03316
03317
03318
03319 if( m_bSparseJacobianCalculated == true) return m_sparseJacMatrix;
03320
03321
03322 getLinearConstraintCoefficientMajor();
03323
03324 if( m_bNonLinearStructuresInitialized == false) initializeNonLinearStructures( );
03325 try
03326 {
03327 if( m_bColumnMajor == true)
03328 {
03329 if( getSparseJacobianFromColumnMajor( ) == false) throw ErrorClass("An error occurred in getSpareJacobianFromColumnMajor");
03330 }
03331 else
03332 {
03333 if( getSparseJacobianFromRowMajor( ) == false) throw ErrorClass("An error occurred in getSpareJacobianFromRowMajor");
03334 }
03335 }
03336 catch(const ErrorClass& eclass)
03337 {
03338 throw ErrorClass( eclass.errormsg);
03339 }
03340
03341 m_sparseJacMatrix = new SparseJacobianMatrix();
03342
03343
03344 m_sparseJacMatrix->bDeleteArrays = false;
03345 m_sparseJacMatrix->valueSize = m_iJacValueSize;
03346 m_sparseJacMatrix->starts = m_miJacStart;
03347 m_sparseJacMatrix->conVals = m_miJacNumConTerms;
03348 m_sparseJacMatrix->indexes = m_miJacIndex;
03349 m_sparseJacMatrix->values = m_mdJacValue;
03350 m_bSparseJacobianCalculated = true;
03351 return m_sparseJacMatrix;
03352 }
03353
03354 bool OSInstance::addQTermsToExressionTree()
03355 {
03356 int i, k, idx;
03357
03358 int numQTerms = instanceData->quadraticCoefficients->numberOfQuadraticTerms;
03359 if(numQTerms <= 0 || m_bQTermsAdded == true) return true;
03360 OSnLNodeVariable* nlNodeVariableOne;
03361 OSnLNodeVariable* nlNodeVariableTwo;
03362 OSnLNodeTimes* nlNodeTimes;
03363 OSnLNodePlus* nlNodePlus;
03364 OSExpressionTree* expTree;
03365 getQuadraticTerms();
03366
03367 for(i = 0; i < numQTerms; i++)
03368 {
03369 idx = m_quadraticTerms->rowIndexes[ i];
03370
03371
03372 if( m_mapExpressionTreesMod.find( idx) != m_mapExpressionTreesMod.end() )
03373 {
03374
03375
03376
03377 nlNodeVariableOne = new OSnLNodeVariable();
03378 nlNodeVariableOne->idx = m_quadraticTerms->varOneIndexes[ i];
03379
03380
03381 expTree = m_mapExpressionTreesMod[ idx];
03382 if( expTree->m_bIndexMapGenerated == false) expTree->getVariableIndiciesMap();
03383 if( (*expTree->mapVarIdx).find( nlNodeVariableOne->idx) == (*expTree->mapVarIdx).end() )
03384 {
03385
03386 k = (*expTree->mapVarIdx).size();
03387 (*expTree->mapVarIdx)[ nlNodeVariableOne->idx] = k + 1;
03388 #ifdef DEBUG
03389 std::cout << "ADDED THE FOLLOWING VAIRABLE TO THE MAP" << nlNodeVariableOne->idx << std::endl;
03390 #endif
03391 }
03392 nlNodeVariableOne->coef = m_quadraticTerms->coefficients[ i];
03393 nlNodeVariableTwo = new OSnLNodeVariable();
03394 nlNodeVariableTwo->idx = m_quadraticTerms->varTwoIndexes[ i];
03395
03396
03397 if( (*expTree->mapVarIdx).find( nlNodeVariableTwo->idx) == (*expTree->mapVarIdx).end() )
03398 {
03399
03400 k = (*expTree->mapVarIdx).size();
03401 (*expTree->mapVarIdx)[ nlNodeVariableTwo->idx] = k + 1;
03402 #ifdef DEBUG
03403 std::cout << "ADDED THE FOLLOWING VAIRABLE TO THE MAP" << nlNodeVariableTwo->idx << std::endl;
03404 #endif
03405 }
03406 nlNodeVariableTwo->coef = 1.;
03407
03408 nlNodeTimes = new OSnLNodeTimes();
03409 nlNodeTimes->m_mChildren[ 0] = nlNodeVariableOne;
03410 nlNodeTimes->m_mChildren[ 1] = nlNodeVariableTwo;
03411
03412 nlNodePlus = new OSnLNodePlus();
03413 nlNodePlus->m_mChildren[ 0] = expTree->m_treeRoot;
03414 nlNodePlus->m_mChildren[ 1] = nlNodeTimes;
03415
03416 expTree->m_treeRoot = nlNodePlus ;
03417
03418 if(expTree->m_bIndexMapGenerated == true)
03419 {
03420 delete expTree->mapVarIdx;
03421 expTree->mapVarIdx = NULL;
03422 expTree->m_bIndexMapGenerated = false;
03423 }
03424
03425
03426
03427 }
03428 else
03429 {
03430
03431 nlNodeVariableOne = new OSnLNodeVariable();
03432 nlNodeVariableOne->idx = m_quadraticTerms->varOneIndexes[ i];
03433 nlNodeVariableOne->coef = m_quadraticTerms->coefficients[ i];
03434 nlNodeVariableTwo = new OSnLNodeVariable();
03435 nlNodeVariableTwo->idx = m_quadraticTerms->varTwoIndexes[ i];
03436 nlNodeVariableTwo->coef = 1.;
03437
03438 nlNodeTimes = new OSnLNodeTimes();
03439 nlNodeTimes->m_mChildren[ 0] = nlNodeVariableOne;
03440 nlNodeTimes->m_mChildren[ 1] = nlNodeVariableTwo;
03441
03442 expTree = new OSExpressionTree();
03443 expTree->m_treeRoot = nlNodeTimes ;
03444 expTree->mapVarIdx = expTree->getVariableIndiciesMap();
03445 m_mapExpressionTreesMod[ idx ] = expTree;
03446 if(idx < 0)
03447 {
03448 m_iObjectiveNumberNonlinear++;
03449 m_bProcessExpressionTrees = true;
03450 }
03451 else
03452 {
03453 m_iConstraintNumberNonlinear++;
03454 m_bProcessExpressionTrees = true;
03455 }
03456 #ifdef DEBUG
03457 std::cout << "NUMBER OF EXPRESSION TREES = " << m_mapExpressionTreesMod.size() <<std::endl;
03458 std::cout << "NUMBER OF NONLINEAR OBJECTIVES = " << getNumberOfNonlinearObjectives() <<std::endl;
03459 #endif
03460 }
03461
03462 if(m_iNonlinearExpressionNumber <= 0) m_mapExpressionTrees = m_mapExpressionTreesMod;
03463 m_bQTermsAdded =true;
03464 }
03465 return true;
03466 }
03467
03468 double OSInstance::calculateFunctionValue(int idx, double *x, bool new_x)
03469 {
03470 try
03471 {
03472
03473 int i, j;
03474 double dvalue = 0;
03475 if( m_binitForAlgDiff == false) initForAlgDiff();
03476 if( m_bSparseJacobianCalculated == false) getJacobianSparsityPattern();
03477 if(idx >= 0)
03478 {
03479
03480 if( getConstraintNumber() <= idx ) throw
03481 ErrorClass("constraint index not valid in OSInstance::calculateFunctionValue");
03482 if( new_x == false) return *(m_mdConstraintFunctionValues + idx);
03483
03484 if( m_mapExpressionTreesMod.find( idx) != m_mapExpressionTreesMod.end() )
03485 {
03486 dvalue = m_mapExpressionTreesMod[ idx]->calculateFunction( x, new_x);
03487
03488 }
03489
03490
03491 i = m_sparseJacMatrix->starts[ idx];
03492 j = m_sparseJacMatrix->starts[ idx + 1 ];
03493
03494 while ( (i - m_sparseJacMatrix->starts[ idx]) < m_sparseJacMatrix->conVals[ idx] )
03495 {
03496
03497
03498 dvalue += m_sparseJacMatrix->values[ i]*x[ m_sparseJacMatrix->indexes[ i] ];
03499 i++;
03500 }
03501
03502 dvalue += m_mdConstraintConstants[ idx ];
03503 return dvalue;
03504 }
03505 else
03506 {
03507
03508 if( getObjectiveNumber() <= ( abs( idx) - 1) ) throw
03509 ErrorClass("objective function index not valid in OSInstance::calculateFunctionValue");
03510 if( new_x == false) return *(m_mdObjectiveFunctionValues + ( abs( idx) - 1));
03511
03512 if( m_mapExpressionTreesMod.find( idx) != m_mapExpressionTreesMod.end() )
03513 {
03514 dvalue = m_mapExpressionTreesMod[ idx]->calculateFunction( x, new_x);
03515 }
03516
03517 SparseVector **objCoef = getObjectiveCoefficients();
03518 SparseVector *obj = objCoef[ abs( idx) - 1];
03519 for(i = 0; i < obj->number; i++)
03520 {
03521 dvalue += x[ obj->indexes[i]]*(obj->values[ i]);
03522 }
03523
03524 dvalue += m_mdObjectiveConstants[ abs( idx) - 1 ];
03525
03526 *(m_mdObjectiveFunctionValues + ( abs( idx) - 1)) = dvalue;
03527 return *(m_mdObjectiveFunctionValues + ( abs( idx) - 1));
03528 }
03529 }
03530 catch(const ErrorClass& eclass)
03531 {
03532 throw ErrorClass( eclass.errormsg);
03533 }
03534 }
03535
03536
03537 double *OSInstance::calculateAllConstraintFunctionValues( double* x, double *objLambda, double *conLambda,
03538 bool new_x, int highestOrder)
03539 {
03540 try
03541 {
03542 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated) )
03543 getIterateResults(x, objLambda, conLambda, new_x, highestOrder);
03544 }
03545 catch(const ErrorClass& eclass)
03546 {
03547 throw ErrorClass( eclass.errormsg);
03548 }
03549 return m_mdConstraintFunctionValues;
03550 }
03551
03552
03553 double *OSInstance::calculateAllConstraintFunctionValues(double* x, bool new_x)
03554 {
03555 try
03556 {
03557 m_iHighestOrderEvaluated = -1;
03558 if( new_x == false) return m_mdConstraintFunctionValues;
03559 int idx, numConstraints;
03560 numConstraints = getConstraintNumber();
03561
03562 for(idx = 0; idx < numConstraints; idx++)
03563 {
03564 m_mdConstraintFunctionValues[ idx] = calculateFunctionValue(idx, x, new_x);
03565 }
03566
03567 }
03568 catch(const ErrorClass& eclass)
03569 {
03570 throw ErrorClass( eclass.errormsg);
03571 }
03572 return m_mdConstraintFunctionValues;
03573 }
03574
03575
03576 double *OSInstance::calculateAllObjectiveFunctionValues( double* x, double *objLambda, double *conLambda,
03577 bool new_x, int highestOrder)
03578 {
03579 try
03580 {
03581 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated) )
03582 getIterateResults(x, objLambda, conLambda, new_x, highestOrder);
03583 }
03584 catch(const ErrorClass& eclass)
03585 {
03586 throw ErrorClass( eclass.errormsg);
03587 }
03588 return m_mdObjectiveFunctionValues;
03589 }
03590
03591
03592 double *OSInstance::calculateAllObjectiveFunctionValues( double* x, bool new_x)
03593 {
03594 try
03595 {
03596 m_iHighestOrderEvaluated = -1;
03597
03598 if( new_x == false) return m_mdObjectiveFunctionValues;
03599 int idx, numObjectives;
03600 numObjectives = getObjectiveNumber();
03601
03602 for(idx = 0; idx < numObjectives; idx++)
03603 {
03604 m_mdObjectiveFunctionValues[ idx] = calculateFunctionValue(-idx -1, x, new_x);
03605 }
03606 }
03607 catch(const ErrorClass& eclass)
03608 {
03609 throw ErrorClass( eclass.errormsg);
03610 }
03611 return m_mdObjectiveFunctionValues;
03612 }
03613
03614
03615 SparseJacobianMatrix *OSInstance::calculateAllConstraintFunctionGradients(double* x, double *objLambda, double *conLambda,
03616 bool new_x, int highestOrder)
03617 {
03618 try
03619 {
03620 if(highestOrder < 1 ) throw ErrorClass("When calling calculateAllConstraintFunctionGradients highestOrder should be 1 or 2");
03621 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated) )
03622 getIterateResults(x, objLambda, conLambda, new_x, highestOrder);
03623 }
03624 catch(const ErrorClass& eclass)
03625 {
03626 throw ErrorClass( eclass.errormsg);
03627 }
03628 return m_sparseJacMatrix;
03629 }
03630
03631
03632
03633 SparseVector *OSInstance::calculateConstraintFunctionGradient(double* x, double *objLambda, double *conLambda,
03634 int idx, bool new_x, int highestOrder)
03635 {
03636 try
03637 {
03638 if(highestOrder < 1 ) throw ErrorClass("When calling calculateConstraintFunctionGradient highestOrder should be 1 or 2");
03639 if(idx < 0 || idx >= instanceData->constraints->numberOfConstraints )
03640 throw ErrorClass("invalid index passed to calculateConstraintFunctionGrad");
03641 SparseVector *sp;
03642 sp = new SparseVector();
03643 sp->bDeleteArrays = true;
03644 int i;
03645 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated) )
03646 getIterateResults(x, objLambda, conLambda, new_x, highestOrder);
03647 sp->number = m_miJacStart[ idx + 1] - m_miJacStart[ idx];
03648 sp->values = new double[ sp->number];
03649 sp->indexes = new int[ sp->number];
03650 for(i = 0; i < sp->number; i++)
03651 {
03652 sp->values[ i] = m_mdJacValue[ m_miJacStart[ idx] + i];
03653 sp->indexes[ i] = m_miJacIndex[ m_miJacStart[ idx] + i];
03654 }
03655 return sp;
03656 }
03657 catch(const ErrorClass& eclass)
03658 {
03659 throw ErrorClass( eclass.errormsg);
03660 }
03661 }
03662
03663
03664 SparseVector *OSInstance::calculateConstraintFunctionGradient(double* x, int idx, bool new_x)
03665 {
03666 try
03667 {
03668 if(idx < 0 || idx >= instanceData->constraints->numberOfConstraints )
03669 throw ErrorClass("invalid index passed to calculateConstraintFunctionGrad");
03670 SparseVector *sp;
03671 sp = new SparseVector();
03672 sp->bDeleteArrays = true;
03673 int i;
03674 if( new_x == true || (1 > m_iHighestOrderEvaluated) )
03675 getIterateResults(x, NULL, NULL, new_x, 1);
03676 sp->number = m_miJacStart[ idx + 1] - m_miJacStart[ idx];
03677 sp->values = new double[ sp->number];
03678 sp->indexes = new int[ sp->number];
03679 for(i = 0; i < sp->number; i++)
03680 {
03681 sp->values[ i] = m_mdJacValue[ m_miJacStart[ idx] + i];
03682 sp->indexes[ i] = m_miJacIndex[ m_miJacStart[ idx] + i];
03683 }
03684 return sp;
03685 }
03686 catch(const ErrorClass& eclass)
03687 {
03688 throw ErrorClass( eclass.errormsg);
03689 }
03690 }
03691
03692
03693 double **OSInstance::calculateAllObjectiveFunctionGradients(double* x, double *objLambda, double *conLambda,
03694 bool new_x, int highestOrder)
03695 {
03696 try
03697 {
03698 if(highestOrder < 1 ) throw ErrorClass("When calling calculateAllObjectiveFunctionGradients highestOrder should be 1 or 2");
03699 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated) )
03700 {
03701 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
03702 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
03703 {
03704 if(posMapExpTree->first < 0)
03705 {
03706 m_mmdObjGradient[ abs( posMapExpTree->first) - 1 ] = calculateObjectiveFunctionGradient(x, objLambda, conLambda,
03707 posMapExpTree->first, new_x, highestOrder);
03708 }
03709 }
03710 }
03711 }
03712 catch(const ErrorClass& eclass)
03713 {
03714 throw ErrorClass( eclass.errormsg);
03715 }
03716 return m_mmdObjGradient;
03717 }
03718
03719 double *OSInstance::calculateObjectiveFunctionGradient(double* x, double *objLambda, double *conLambda,
03720 int objIdx, bool new_x, int highestOrder)
03721 {
03722
03723
03724
03725 try
03726 {
03727 if(highestOrder < 1 ) throw ErrorClass("When calling calculateObjectiveFunctionGradient highestOrder should be 1 or 2");
03728 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated) )
03729 {
03730 int domainIdx = 0;
03731 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
03732 std::map<int, int>::iterator posVarIndexMap;
03733 int iHighestOrderEvaluatedStore;
03734 unsigned int i;
03735 iHighestOrderEvaluatedStore = m_iHighestOrderEvaluated;
03736 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
03737 {
03738
03739 if(posMapExpTree->first == objIdx)
03740 {
03741 if( new_x == true )
03742 {
03743 if( m_vdX.size() > 0) m_vdX.clear();
03744 for(posVarIndexMap = m_mapAllNonlinearVariablesIndex.begin(); posVarIndexMap != m_mapAllNonlinearVariablesIndex.end(); ++posVarIndexMap)
03745 {
03746 m_vdX.push_back( x[ posVarIndexMap->first]) ;
03747 }
03748 if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true ) && (m_mapExpressionTreesMod.size() > 0) )
03749 {
03750 createOSADFun( m_vdX);
03751 }
03752 }
03753
03754 if(( new_x == true ) || (m_iHighestOrderEvaluated < 0) )this->forwardAD(0, m_vdX);
03755
03756 if(( new_x == true ) || (m_iHighestOrderEvaluated < 1) )
03757 {
03758 m_vdRangeUnitVec[ domainIdx] = 1.;
03759 m_vdYjacval = this->reverseAD(1, m_vdRangeUnitVec);
03760 for(i = 0; i < m_iNumberOfNonlinearVariables; i++)
03761 {
03762 m_mmdObjGradient[ abs( objIdx) - 1 ][ m_miNonLinearVarsReverseMap[ i]] = m_vdYjacval[ i] +
03763 m_mmdDenseObjectiveCoefficients[ abs( objIdx) - 1][ m_miNonLinearVarsReverseMap[ i]];
03764 }
03765 }
03766 m_iHighestOrderEvaluated = iHighestOrderEvaluatedStore;
03767 m_vdRangeUnitVec[ domainIdx] = 0.;
03768
03769 break;
03770 }
03771 domainIdx++;
03772 }
03773 }
03774 }
03775 catch(const ErrorClass& eclass)
03776 {
03777 throw ErrorClass( eclass.errormsg);
03778 }
03779 return m_mmdObjGradient[abs( objIdx) - 1];
03780 }
03781
03782
03783
03784 double *OSInstance::calculateObjectiveFunctionGradient(double* x, int objIdx, bool new_x)
03785 {
03786 try
03787 {
03788 int domainIdx = 0;
03789 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
03790 std::map<int, int>::iterator posVarIndexMap;
03791 unsigned int i;
03792 int iHighestOrderEvaluatedStore;
03793 iHighestOrderEvaluatedStore = m_iHighestOrderEvaluated;
03794 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
03795 {
03796 if(posMapExpTree->first == objIdx)
03797 {
03798 if( new_x == true )
03799 {
03800 if( m_vdX.size() > 0) m_vdX.clear();
03801 for(posVarIndexMap = m_mapAllNonlinearVariablesIndex.begin(); posVarIndexMap != m_mapAllNonlinearVariablesIndex.end(); ++posVarIndexMap)
03802 {
03803 m_vdX.push_back( x[ posVarIndexMap->first]) ;
03804 }
03805 if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true ) && (m_mapExpressionTreesMod.size() > 0) )
03806 {
03807 createOSADFun( m_vdX);
03808 }
03809 }
03810
03811 if(( new_x == true ) || (m_iHighestOrderEvaluated < 0) )this->forwardAD(0, m_vdX);
03812
03813 if(( new_x == true ) || (m_iHighestOrderEvaluated < 1) )
03814 {
03815 m_vdRangeUnitVec[ domainIdx] = 1.;
03816 m_vdYjacval = this->reverseAD(1, m_vdRangeUnitVec);
03817 for(i = 0; i < m_iNumberOfNonlinearVariables; i++)
03818 {
03819 m_mmdObjGradient[ abs( objIdx) - 1 ][ m_miNonLinearVarsReverseMap[ i]] = m_vdYjacval[ i] +
03820 m_mmdDenseObjectiveCoefficients[ abs( objIdx) - 1][ m_miNonLinearVarsReverseMap[ i]];
03821 }
03822 }
03823 m_iHighestOrderEvaluated = iHighestOrderEvaluatedStore;
03824 m_vdRangeUnitVec[ domainIdx] = 0.;
03825
03826 break;
03827 }
03828 domainIdx++;
03829 }
03830 }
03831 catch(const ErrorClass& eclass)
03832 {
03833 throw ErrorClass( eclass.errormsg);
03834 }
03835 return m_mmdObjGradient[abs( objIdx) - 1];
03836 }
03837
03838 SparseHessianMatrix *OSInstance::calculateLagrangianHessian( double* x, double *objLambda, double *conLambda,
03839 bool new_x, int highestOrder)
03840 {
03841 try
03842 {
03843 if(highestOrder != 2 ) throw ErrorClass("When calling calculateLagrangianHessian highestOrder should be 2");
03844 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated) )
03845 {
03846
03847 getIterateResults(x, objLambda, conLambda, new_x, highestOrder);
03848 }
03849 }
03850 catch(const ErrorClass& eclass)
03851 {
03852 throw ErrorClass( eclass.errormsg);
03853 }
03854 return m_LagrangianSparseHessian;
03855 }
03856
03857 SparseHessianMatrix *OSInstance::calculateHessian(double* x, int idx, bool new_x)
03858 {
03859 try
03860 {
03861 if(idx < -instanceData->objectives->numberOfObjectives || idx >= instanceData->constraints->numberOfConstraints )
03862 throw ErrorClass("invalid index passed to calculateHessian");
03863 double *objLambda = new double[ getObjectiveNumber() ];
03864 double *conLambda = new double[ getConstraintNumber() ];
03865
03866
03867 int i;
03868
03869 for(i = 0; i < getObjectiveNumber(); i++)
03870 {
03871 objLambda[ i] = 0.0;
03872 }
03873 for(i = 0; i < getConstraintNumber(); i++)
03874 {
03875 conLambda[ i] = 0.0;
03876 }
03877
03878
03879 if(idx < 0)
03880 {
03881 objLambda[ abs(idx) - 1] = 1.0;
03882 }
03883 else
03884 {
03885 conLambda[ idx] = 1.0;
03886 }
03887 if( new_x == true || (2 > m_iHighestOrderEvaluated) )
03888 {
03889 getIterateResults(x, objLambda, conLambda, new_x, 2);
03890
03891 }
03892 delete[] objLambda;
03893 delete[] conLambda;
03894 }
03895 catch(const ErrorClass& eclass)
03896 {
03897 throw ErrorClass( eclass.errormsg);
03898 }
03899 return m_LagrangianSparseHessian;
03900 }
03901
03902
03903
03904 bool OSInstance::getSparseJacobianFromColumnMajor( )
03905 {
03906
03907 if( m_bColumnMajor == false) return false;
03908 int iNumRowStarts = getConstraintNumber() + 1;
03909 int i,j, iTemp;
03910 int iNumVariableStarts = getVariableNumber() ;
03911 int *start = NULL;
03912 int *index = NULL;
03913 double *value = NULL;
03914 if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0)
03915 {
03916 start = this->instanceData->linearConstraintCoefficients->start->el;
03917 index = this->instanceData->linearConstraintCoefficients->rowIdx->el;
03918 value = this->instanceData->linearConstraintCoefficients->value->el;
03919 }
03920 m_miJacStart = new int[ iNumRowStarts];
03921 m_miJacNumConTerms = new int[ getConstraintNumber()];
03922 OSnLNodePlus *nlNodePlus;
03923 OSnLNodeVariable *nlNodeVariable;
03924 OSExpressionTree *expTree = NULL;
03925
03926 for ( i = 0; i < iNumRowStarts; i++)
03927 {
03928 m_miJacStart [ i ] = 0;
03929
03930 if( m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() )
03931 {
03932
03933 m_mapExpressionTreesMod[ i]->getVariableIndiciesMap();
03934
03935 }
03936 }
03937
03938 if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0)
03939 {
03940
03941 for (i = 0; i < iNumVariableStarts; i++)
03942 {
03943 for (j = start[i]; j < start[ i + 1 ]; j++)
03944 {
03945
03946
03947
03948
03949
03950
03951 if( (m_mapExpressionTreesMod.find( index[ j]) != m_mapExpressionTreesMod.end() ) &&
03952 ( (*m_mapExpressionTreesMod[ index[ j]]->mapVarIdx).find( i) != (*m_mapExpressionTreesMod[ index[ j]]->mapVarIdx).end()) )
03953 {
03954
03955
03956
03957
03958 if( value[j] > 0 || value[j] < 0)
03959 {
03960 expTree = m_mapExpressionTreesMod[ index[j] ];
03961 nlNodeVariable = new OSnLNodeVariable();
03962 nlNodeVariable->coef = value[ j];
03963 nlNodeVariable->idx = i;
03964 nlNodePlus = new OSnLNodePlus();
03965 nlNodePlus->m_mChildren[ 0] = m_mapExpressionTreesMod[ index[ j] ]->m_treeRoot;
03966 nlNodePlus->m_mChildren[ 1] = nlNodeVariable;
03967
03968 expTree->m_treeRoot = nlNodePlus ;
03969
03970
03971
03972
03973 }
03974 }
03975 else
03976 {
03977 m_miJacStart[ index[j] + 1] ++;
03978 }
03979 }
03980 }
03981 }
03982
03983
03984
03985
03986 m_miJacStart[0] = 0;
03987 for (i = 1; i < iNumRowStarts; i++ )
03988 {
03989 m_miJacNumConTerms[ i - 1] = m_miJacStart[i];
03990 if( m_mapExpressionTreesMod.find( i - 1) != m_mapExpressionTreesMod.end() )
03991 {
03992 m_miJacStart[i] += (m_miJacStart[i - 1] + (*m_mapExpressionTreesMod[ i - 1]->mapVarIdx).size() );
03993 }
03994 else
03995 {
03996 m_miJacStart[i] += m_miJacStart[i - 1];
03997 }
03998 }
03999
04000 m_iJacValueSize = m_miJacStart[ iNumRowStarts - 1];
04001 m_miJacIndex = new int[ m_iJacValueSize];
04002 m_mdJacValue = new double[ m_iJacValueSize ];
04003
04004 if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0)
04005 {
04006
04007 for (i = 0; i < iNumVariableStarts; i++)
04008 {
04009
04010
04011 for (j = start[i]; j < start[ i + 1 ]; j++)
04012 {
04013
04014
04015 if( (m_mapExpressionTreesMod.find( index[ j]) == m_mapExpressionTreesMod.end() ) ||
04016 ( (*m_mapExpressionTreesMod[ index[ j]]->mapVarIdx).find( i) == (*m_mapExpressionTreesMod[ index[ j]]->mapVarIdx).end()) )
04017 {
04018 iTemp = m_miJacStart[ index[j]];
04019 m_miJacIndex[ iTemp] = i;
04020 m_mdJacValue[ iTemp] = value[j];
04021 m_miJacStart[ index[j]]++;
04022 }
04023 }
04024 }
04025 }
04026
04027 std::map<int, int>::iterator posVarIdx;
04028
04029 for (i = 0; i < iNumRowStarts - 1; i++ )
04030 {
04031 m_miJacStart[ i] = m_miJacStart [ i] - m_miJacNumConTerms[ i] ;
04032 iTemp = m_miJacStart[ i] + m_miJacNumConTerms[ i];
04033
04034 if( m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() )
04035 {
04036 for(posVarIdx = (*m_mapExpressionTreesMod[ i]->mapVarIdx).begin(); posVarIdx
04037 != (*m_mapExpressionTreesMod[ i]->mapVarIdx).end(); ++posVarIdx)
04038 {
04039 m_miJacIndex[ iTemp] = posVarIdx->first;
04040 m_mdJacValue[ iTemp] = 0;
04041 iTemp++;
04042 }
04043 }
04044 }
04045 #ifdef DEBUG
04046 std::cout << "HERE ARE ROW STARTS:" << std::endl;
04047 for (i = 0; i < iNumRowStarts; i++ )
04048 {
04049 std::cout << m_miJacStart[ i] << " ";
04050 }
04051 std::cout << std::endl << std::endl;
04052 std::cout << "HERE ARE VARIABLE INDICIES:" << std::endl;
04053 for (i = 0; i < m_miJacStart[ iNumRowStarts - 1]; i++ )
04054 {
04055 std::cout << m_miJacIndex[ i] << " ";
04056 }
04057 std::cout << std::endl << std::endl;
04058 std::cout << "HERE ARE VALUES:" << std::endl;
04059 for (i = 0; i < m_miJacStart[ iNumRowStarts - 1]; i++ )
04060 {
04061 std::cout << m_mdJacValue[ i] << " ";
04062 }
04063 std::cout << std::endl << std::endl;
04064
04065 std::cout << "HERE ARE NUMBER OF CONSTANT TERMS:" << std::endl;
04066 for (i = 0; i < iNumRowStarts - 1; i++ )
04067 {
04068 std::cout << m_miJacNumConTerms[ i ] << " ";
04069 }
04070 std::cout << std::endl << std::endl;
04071 #endif
04072 return true;
04073 }
04074
04075
04076
04077 bool OSInstance::getSparseJacobianFromRowMajor( )
04078 {
04079
04080 if( m_bColumnMajor == true) return false;
04081 int iNumJacRowStarts = getConstraintNumber() + 1;
04082 std::map<int, int>::iterator posVarIdx;
04083 int i,j, k;
04084 int *start = NULL;
04085 int *index = NULL;
04086 double *value = NULL;
04087 if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0)
04088 {
04089 start = this->instanceData->linearConstraintCoefficients->start->el;
04090 index = this->instanceData->linearConstraintCoefficients->colIdx->el;
04091 value = this->instanceData->linearConstraintCoefficients->value->el;
04092 }
04093 m_miJacStart = new int[ iNumJacRowStarts];
04094 m_miJacNumConTerms = new int[ getConstraintNumber()];
04095 OSnLNodePlus *nlNodePlus;
04096 OSnLNodeVariable *nlNodeVariable;
04097
04098
04099 for ( i = 0; i < iNumJacRowStarts; i++)
04100 {
04101 m_miJacStart [ i ] = 0;
04102
04103 if( m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() )
04104 {
04105
04106 m_mapExpressionTreesMod[ i]->getVariableIndiciesMap();
04107
04108 }
04109 }
04110 int loopLimit = getConstraintNumber();
04111
04112
04113 if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0)
04114 {
04115
04116 for (i = 0; i < loopLimit; i++)
04117 {
04118 m_miJacNumConTerms[ i] = 0;
04119 for (j = start[i]; j < start[ i + 1 ]; j++)
04120 {
04121
04122
04123
04124 if( (m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() ) &&
04125 ( (*m_mapExpressionTreesMod[ i]->mapVarIdx).find( index[ j]) != (*m_mapExpressionTreesMod[ i]->mapVarIdx).end()) )
04126 {
04127
04128
04129
04130 if(value[ j] > 0 || value[j] < 0)
04131 {
04132 nlNodeVariable = new OSnLNodeVariable();
04133 nlNodeVariable->coef = value[ j];
04134 nlNodeVariable->idx = index[ j];
04135 nlNodePlus = new OSnLNodePlus();
04136 nlNodePlus->m_mChildren[ 0] = m_mapExpressionTreesMod[ i ]->m_treeRoot;
04137 nlNodePlus->m_mChildren[ 1] = nlNodeVariable;
04138
04139
04140
04141
04142 m_mapExpressionTreesMod[ i ]->m_treeRoot = nlNodePlus;
04143 }
04144 }
04145 else
04146 {
04147
04148 m_miJacNumConTerms[ i]++;
04149 }
04150 }
04151 }
04152 }
04153
04154 m_miJacStart[0] = 0;
04155 for (i = 1; i < iNumJacRowStarts; i++ )
04156 {
04157 if( m_mapExpressionTreesMod.find( i - 1) != m_mapExpressionTreesMod.end() )
04158 {
04159 m_miJacStart[i] = m_miJacStart[i - 1] + (m_miJacNumConTerms[ i - 1] + (*m_mapExpressionTreesMod[ i - 1]->mapVarIdx).size() );
04160 }
04161 else
04162 {
04163 m_miJacStart[i] = m_miJacStart[i - 1] + m_miJacNumConTerms[ i - 1];
04164 }
04165 }
04166
04167
04168 m_iJacValueSize = m_miJacStart[ iNumJacRowStarts - 1];
04169 m_miJacIndex = new int[ m_iJacValueSize];
04170 m_mdJacValue = new double[ m_iJacValueSize ];
04171
04172
04173 if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0)
04174 {
04175 for (i = 0; i < loopLimit; i++)
04176 {
04177 k = 0;
04178 for (j = start[i]; j < start[ i + 1 ]; j++)
04179 {
04180 if( (m_mapExpressionTreesMod.find( i) == m_mapExpressionTreesMod.end() ) ||
04181 ( (*m_mapExpressionTreesMod[ i]->mapVarIdx).find( index[ j]) == (*m_mapExpressionTreesMod[ i]->mapVarIdx).end()) )
04182 {
04183 m_miJacIndex[ m_miJacStart[i] + k ] = index[ j];
04184 m_mdJacValue[ m_miJacStart[i] + k ] = value[ j];
04185 k++;
04186 }
04187 }
04188 }
04189 }
04190
04191 for (i = 0; i < loopLimit; i++ )
04192 {
04193 k = m_miJacStart[i] + m_miJacNumConTerms[ i ];
04194
04195 if( m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() )
04196 {
04197 for(posVarIdx = (*m_mapExpressionTreesMod[ i]->mapVarIdx).begin(); posVarIdx
04198 != (*m_mapExpressionTreesMod[ i]->mapVarIdx).end(); ++posVarIdx)
04199 {
04200 m_miJacIndex[ k] = posVarIdx->first;
04201 m_mdJacValue[ k] = 0;
04202 k++;
04203 }
04204 }
04205 }
04206 #ifdef DEBUG
04207 std::cout << "HERE ARE ROW STARTS:" << std::endl;
04208 for (i = 0; i < iNumJacRowStarts; i++ )
04209 {
04210 std::cout << m_miJacStart[ i] << " ";
04211 }
04212 std::cout << std::endl << std::endl;
04213 std::cout << "HERE ARE VARIABLE INDICIES:" << std::endl;
04214 for (i = 0; i < m_miJacStart[ iNumJacRowStarts - 1]; i++ )
04215 {
04216 std::cout << m_miJacIndex[ i] << " ";
04217 }
04218 std::cout << std::endl << std::endl;
04219 std::cout << "HERE ARE VALUES:" << std::endl;
04220 for (i = 0; i < m_miJacStart[ iNumJacRowStarts - 1]; i++ )
04221 {
04222 std::cout << m_mdJacValue[ i] << " ";
04223 }
04224 std::cout << std::endl << std::endl;
04225
04226 std::cout << "HERE ARE NUMBER OF CONSTANT TERMS:" << std::endl;
04227 for (i = 0; i < iNumJacRowStarts - 1; i++ )
04228 {
04229 std::cout << m_miJacNumConTerms[ i ] << " ";
04230 }
04231 std::cout << std::endl << std::endl;
04232 #endif
04233 return true;
04234 }
04235
04236 OSExpressionTree* OSInstance::getLagrangianExpTree( )
04237 {
04238 if( m_bLagrangianExpTreeCreated == true) return m_LagrangianExpTree;
04239
04240
04241
04242 if(m_bSparseJacobianCalculated == false) getJacobianSparsityPattern( );
04243 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
04244 OSnLNodeTimes* nlNodeTimes = NULL;
04245 OSnLNodeVariable* nlNodeVariable = NULL;
04246 OSnLNodeSum* nlNodeSum = NULL;
04247 int numChildren = 0;
04248 int rowIdx;
04249
04250 nlNodeSum = new OSnLNodeSum();
04251 nlNodeSum->inumberOfChildren = m_mapExpressionTreesMod.size();
04252 nlNodeSum->m_mChildren = new OSnLNode*[ nlNodeSum->inumberOfChildren];
04253
04254 m_LagrangianExpTree = new OSExpressionTree();
04255 m_LagrangianExpTree->m_treeRoot = nlNodeSum;
04256
04257 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
04258 {
04259
04260 nlNodeVariable = new OSnLNodeVariable();
04261 nlNodeVariable->coef = 1.;
04262
04263
04264 rowIdx = posMapExpTree->first;
04265 if(rowIdx >= 0)
04266 {
04267 nlNodeVariable->idx = instanceData->variables->numberOfVariables + rowIdx;
04268 }
04269 else
04270 {
04271 nlNodeVariable->idx = instanceData->variables->numberOfVariables +
04272 instanceData->constraints->numberOfConstraints + (abs(rowIdx) - 1);
04273 }
04274
04275 nlNodeTimes = new OSnLNodeTimes();
04276 nlNodeTimes->m_mChildren[ 0] = nlNodeVariable;
04277 nlNodeTimes->m_mChildren[ 1] = m_mapExpressionTreesMod[ posMapExpTree->first ]->m_treeRoot;
04278
04279 nlNodeSum->m_mChildren[ numChildren] = nlNodeTimes;
04280 numChildren++;
04281 }
04282
04283 m_LagrangianExpTree->getVariableIndiciesMap();
04284
04285
04286
04287
04288 m_bLagrangianExpTreeCreated = true;
04289 return m_LagrangianExpTree;
04290 }
04291
04292 std::map<int, int> OSInstance::getAllNonlinearVariablesIndexMap( )
04293 {
04294 if(m_bAllNonlinearVariablesIndex == true) return m_mapAllNonlinearVariablesIndex;
04295
04296
04297 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
04298 std::map<int, int>::iterator posVarIdx;
04299 OSExpressionTree *expTree;
04300 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
04301 {
04302
04303 expTree = posMapExpTree->second;
04304 if(expTree->m_bIndexMapGenerated == false)expTree->getVariableIndiciesMap();
04305 for(posVarIdx = (*expTree->mapVarIdx).begin(); posVarIdx != (*expTree->mapVarIdx).end(); ++posVarIdx)
04306 {
04307 if( m_mapAllNonlinearVariablesIndex.find( posVarIdx->first) == m_mapAllNonlinearVariablesIndex.end() )
04308 {
04309
04310 m_mapAllNonlinearVariablesIndex[ posVarIdx->first] = 1;
04311 }
04312 }
04313 }
04314 m_miNonLinearVarsReverseMap = new int[m_mapAllNonlinearVariablesIndex.size()];
04315
04316 int kount = 0;
04317
04318 for(posVarIdx = m_mapAllNonlinearVariablesIndex.begin(); posVarIdx !=m_mapAllNonlinearVariablesIndex.end(); ++posVarIdx)
04319 {
04320 posVarIdx->second = kount;
04321 m_miNonLinearVarsReverseMap[ kount] = posVarIdx->first;
04322 kount++;
04323 #ifdef DEBUG
04324 std::cout << "POSITION FIRST = " << posVarIdx->first ;
04325 std::cout << " POSITION SECOND = " << posVarIdx->second << std::endl;
04326 #endif
04327 }
04328 m_iNumberOfNonlinearVariables = kount;
04329
04330 m_bAllNonlinearVariablesIndex = true;
04331 return m_mapAllNonlinearVariablesIndex;
04332 }
04333
04334 SparseHessianMatrix* OSInstance::getLagrangianHessianSparsityPattern( )
04335 {
04336
04337 if( m_bLagrangianSparseHessianCreated == true) return m_LagrangianSparseHessian;
04338 if( m_iNumberOfNonlinearVariables == 0) return NULL;
04339 if( m_binitForAlgDiff == false ) initForAlgDiff();
04340 unsigned int i = 0;
04341 unsigned int j;
04342 int numNonz = 0;
04343
04344
04345 std::vector<double> vx;
04346 std::map<int, int>::iterator posMap1, posMap2;
04347 if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true ) && (m_mapExpressionTreesMod.size() > 0) )
04348 {
04349 for(posMap1 = m_mapAllNonlinearVariablesIndex.begin(); posMap1 != m_mapAllNonlinearVariablesIndex.end(); ++posMap1)
04350 {
04351 vx.push_back( 1.0) ;
04352 }
04353 createOSADFun( vx);
04354 }
04355
04356
04357
04358
04359
04360
04361
04362
04363
04364
04365
04366
04367
04368
04369
04370
04371
04372
04373
04374
04375
04376
04377
04378
04379
04380
04381
04382
04383
04384
04385
04386
04387
04388
04389 numNonz = getADSparsityHessian();
04390
04391
04392 i = 0;
04393
04394 m_LagrangianSparseHessian = new SparseHessianMatrix();
04395 m_LagrangianSparseHessian->bDeleteArrays = true;
04396 m_LagrangianSparseHessian->hessDimension = numNonz;
04397
04398 m_LagrangianSparseHessian->hessRowIdx = new int[m_LagrangianSparseHessian->hessDimension];
04399 m_LagrangianSparseHessian->hessColIdx = new int[m_LagrangianSparseHessian->hessDimension];
04400 m_LagrangianSparseHessian->hessValues = new double[m_LagrangianSparseHessian->hessDimension];
04401
04402 numNonz = 0;
04403 for(posMap1 = m_mapAllNonlinearVariablesIndex.begin(); posMap1 != m_mapAllNonlinearVariablesIndex.end(); ++posMap1)
04404 {
04405
04406 j = i;
04407 for(posMap2 = posMap1; posMap2 != m_mapAllNonlinearVariablesIndex.end(); ++posMap2)
04408 {
04409
04410 if(m_vbLagHessNonz[ i*m_iNumberOfNonlinearVariables + j] == true)
04411 {
04412 *(m_LagrangianSparseHessian->hessRowIdx + numNonz) = posMap1->first;
04413 *(m_LagrangianSparseHessian->hessColIdx + numNonz) = posMap2->first;
04414 numNonz++;
04415 }
04416
04417 j++;
04418 }
04419 i++;
04420 }
04421 #ifdef DEBUG
04422 std::cout << "HESSIAN SPARSITY PATTERN" << std::endl;
04423 int kj;
04424 for(kj = 0; kj < m_LagrangianSparseHessian->hessDimension; kj++)
04425 {
04426 std::cout << "Row Index = " << *(m_LagrangianSparseHessian->hessRowIdx + kj) << std::endl;
04427 std::cout << "Column Index = " << *(m_LagrangianSparseHessian->hessColIdx + kj) << std::endl;
04428 }
04429 #endif
04430
04431 m_bLagrangianSparseHessianCreated = true;
04432 return m_LagrangianSparseHessian;
04433 }
04434
04435
04436 void OSInstance::duplicateExpressionTreesMap()
04437 {
04438
04439
04440 if(m_bDuplicateExpressionTreesMap == false)
04441 {
04442
04443 if( m_bProcessExpressionTrees == false) getAllNonlinearExpressionTrees();
04444 m_mapExpressionTreesMod = m_mapExpressionTrees;
04445 m_bDuplicateExpressionTreesMap = true;
04446 return;
04447 }
04448 else
04449 {
04450 return;
04451 }
04452 }
04453
04454
04455
04456 bool OSInstance::getIterateResults( double *x, double *objLambda, double* conMultipliers,
04457 bool new_x, int highestOrder)
04458 {
04459 try
04460 {
04461 if( m_binitForAlgDiff == false) initForAlgDiff();
04462 std::map<int, int>::iterator posVarIndexMap;
04463
04464 if(new_x == true)
04465 {
04466 if( m_vdX.size() > 0) m_vdX.clear();
04467 for(posVarIndexMap = m_mapAllNonlinearVariablesIndex.begin(); posVarIndexMap != m_mapAllNonlinearVariablesIndex.end(); ++posVarIndexMap)
04468 {
04469 m_vdX.push_back( x[ posVarIndexMap->first]) ;
04470 }
04471 if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true ) && (m_mapExpressionTreesMod.size() > 0) )
04472 {
04473 createOSADFun( m_vdX);
04474 }
04475 }
04476 else
04477 {
04478 if( m_vdX.size() == 0)
04479 {
04480 for(posVarIndexMap = m_mapAllNonlinearVariablesIndex.begin(); posVarIndexMap != m_mapAllNonlinearVariablesIndex.end(); ++posVarIndexMap)
04481 {
04482 m_vdX.push_back( x[ posVarIndexMap->first]) ;
04483 }
04484 if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true ) && (m_mapExpressionTreesMod.size() > 0) )
04485 {
04486 createOSADFun( m_vdX);
04487 }
04488 }
04489 }
04490 switch( highestOrder)
04491 {
04492 case 0:
04493 if(new_x == true || m_iHighestOrderEvaluated < 0)
04494 {
04495 if(bUseExpTreeForFunEval == true)
04496 {
04497 calculateAllConstraintFunctionValues( x, new_x);
04498 calculateAllObjectiveFunctionValues( x, new_x);
04499 }
04500 else
04501 {
04502 getZeroOrderResults(x, objLambda, conMultipliers);
04503 }
04504
04505 }
04506 break;
04507 case 1:
04508 if(new_x == true || m_iHighestOrderEvaluated < 0)
04509 getZeroOrderResults(x, objLambda, conMultipliers);
04510 if(new_x == true || m_iHighestOrderEvaluated < 1)
04511 getFirstOrderResults(x, objLambda, conMultipliers);
04512 break;
04513 case 2:
04514 if(new_x == true || m_iHighestOrderEvaluated < 0)
04515 getZeroOrderResults(x, objLambda, conMultipliers);
04516 if(new_x == true || m_iHighestOrderEvaluated < 2)
04517 getSecondOrderResults(x, objLambda, conMultipliers);
04518 break;
04519 default:
04520 throw ErrorClass("Derivative should be order 0, 1, or 2");
04521 }
04522 return true;
04523 }
04524 catch(const ErrorClass& eclass)
04525 {
04526 throw ErrorClass( eclass.errormsg);
04527 }
04528 }
04529
04530
04531 bool OSInstance::getZeroOrderResults(double *x, double *objLambda, double *conMultipliers)
04532 {
04533 try
04534 {
04535
04536 int i, j, rowNum, objNum;
04537 if( m_mapExpressionTreesMod.size() > 0)
04538 {
04539 m_vdYval = this->forwardAD(0, m_vdX);
04540 }
04541
04542 for(rowNum = 0; rowNum < m_iConstraintNumber; rowNum++)
04543 {
04544 m_mdConstraintFunctionValues[ rowNum] = 0.0;
04545 if( m_mapExpressionTreesMod.find( rowNum) != m_mapExpressionTreesMod.end() )
04546 {
04547 m_mdConstraintFunctionValues[ rowNum] = m_vdYval[ m_mapOSADFunRangeIndex[ rowNum]];
04548 }
04549
04550
04551 i = m_sparseJacMatrix->starts[ rowNum];
04552 j = m_sparseJacMatrix->starts[ rowNum + 1 ];
04553 while ( (i - m_sparseJacMatrix->starts[ rowNum]) < m_sparseJacMatrix->conVals[ rowNum] )
04554 {
04555 m_mdConstraintFunctionValues[ rowNum] += m_sparseJacMatrix->values[ i]*x[ m_sparseJacMatrix->indexes[ i] ];
04556 i++;
04557 }
04558
04559 m_mdConstraintFunctionValues[ rowNum] += m_mdConstraintConstants[ rowNum ];
04560 #ifdef DEBUG
04561 std::cout << "Constraint " << rowNum << " function value = " << m_mdConstraintFunctionValues[ rowNum ] << std::endl;
04562 #endif
04563 }
04564
04565 for(objNum = 0; objNum < m_iObjectiveNumber; objNum++)
04566 {
04567 m_mdObjectiveFunctionValues[ objNum] = 0.0;
04568 if( m_mapExpressionTreesMod.find( -objNum -1) != m_mapExpressionTreesMod.end() )
04569 {
04570 m_mdObjectiveFunctionValues[ objNum] = m_vdYval[ objNum];
04571 }
04572 for(i = 0; i < m_iVariableNumber; i++)
04573 {
04574 m_mdObjectiveFunctionValues[ objNum] += m_mmdDenseObjectiveCoefficients[ objNum][i]*x[ i];
04575 }
04576 #ifdef DEBUG
04577 std::cout << "Objective " << objNum << " function value = " << m_mdObjectiveFunctionValues[ objNum] << std::endl;
04578 #endif
04579 }
04580 return true;
04581 }
04582 catch(const ErrorClass& eclass)
04583 {
04584 throw ErrorClass( eclass.errormsg);
04585 }
04586 }
04587
04588
04589
04590 bool OSInstance::getFirstOrderResults(double *x, double *objLambda, double *conMultipliers)
04591 {
04592 try
04593 {
04594
04595 unsigned int i, j;
04596 int rowNum, jacIndex;
04597 unsigned int jstart, jend;
04598 int idx;
04599 OSExpressionTree *expTree = NULL;
04600 int domainIdx = 0;
04601 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
04602 std::map<int, int>::iterator posVarIdx;
04603
04609 if(m_iNumberOfNonlinearVariables >= m_mapExpressionTreesMod.size() )
04610 {
04611
04612
04613 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
04614 {
04615 idx = posMapExpTree->first;
04616
04617 if(idx >= 0)
04618 {
04619 m_vdRangeUnitVec[ domainIdx] = 1.;
04620 m_mapExpressionTreesMod[ idx]->getVariableIndiciesMap();
04621 m_vdYjacval = this->reverseAD(1, m_vdRangeUnitVec);
04622
04623 jstart = m_miJacStart[ idx] + m_miJacNumConTerms[ idx];
04624 jend = m_miJacStart[ idx + 1 ];
04625 if( (*m_mapExpressionTreesMod[ idx]->mapVarIdx).size() != (jend - jstart)) throw
04626 ErrorClass("number of partials not consistent");
04627 j = 0;
04628 jacIndex = 0;
04629 for(posVarIdx = m_mapAllNonlinearVariablesIndex.begin(); posVarIdx
04630 != m_mapAllNonlinearVariablesIndex.end(); ++posVarIdx)
04631 {
04632
04633
04634
04635
04636
04637 if( (*m_mapExpressionTreesMod[ idx]->mapVarIdx).find( posVarIdx->first) != (*m_mapExpressionTreesMod[ idx]->mapVarIdx).end())
04638 {
04639 m_mdJacValue[ jstart] = m_vdYjacval[ jacIndex];
04640 jstart++;
04641 j++;
04642 }
04643 jacIndex++;
04644 }
04645
04646 m_vdRangeUnitVec[ domainIdx] = 0.;
04647 domainIdx++;
04648 }
04649 else
04650 {
04651
04652
04653 domainIdx++;
04654 }
04655 }
04656 }
04657 else
04658 {
04659
04660 for(i = 0; i < m_iNumberOfNonlinearVariables; i++)
04661 {
04662 m_vdDomainUnitVec[i] = 1.;
04663 rowNum = 0;
04664 if( m_mapExpressionTreesMod.size() > 0)
04665 {
04666 m_vdYjacval = this->forwardAD(1, m_vdDomainUnitVec);
04667 }
04668
04669
04670 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
04671 {
04672 idx = posMapExpTree->first;
04673
04674 if(idx >= 0)
04675 {
04676
04677
04678
04679
04680 expTree = m_mapExpressionTreesMod[ idx];
04681 if( (*expTree->mapVarIdx).find( m_miNonLinearVarsReverseMap[ i]) != (*expTree->mapVarIdx).end() )
04682 {
04683 jacIndex = (*m_mapExpressionTreesMod[ idx]->mapVarIdx)[ m_miNonLinearVarsReverseMap[ i]];
04684 jstart = m_miJacStart[ idx] + m_miJacNumConTerms[ idx];
04685
04686 m_mdJacValue[ jstart + jacIndex] = m_vdYjacval[m_iObjectiveNumberNonlinear + rowNum];
04687 }
04688 rowNum++;
04689 }
04690 }
04691
04692 m_vdDomainUnitVec[i] = 0.;
04693 }
04694 }
04695 #ifdef DEBUG
04696 int k;
04697 std::cout << "JACOBIAN DATA " << std::endl;
04698 for(idx = 0; idx < m_iConstraintNumber; idx++)
04699 {
04700 for(k = *(m_sparseJacMatrix->starts + idx); k < *(m_sparseJacMatrix->starts + idx + 1); k++)
04701 {
04702 std::cout << "row idx = " << idx << " col idx = "<< *(m_sparseJacMatrix->indexes + k)
04703 << " value = " << *(m_sparseJacMatrix->values + k) << std::endl;
04704 }
04705 }
04706 #endif
04707 return true;
04708 }
04709 catch(const ErrorClass& eclass)
04710 {
04711 throw ErrorClass( eclass.errormsg);
04712 }
04713 }
04714
04715
04716 bool OSInstance::getSecondOrderResults(double *x, double *objLambda, double *conMultipliers)
04717 {
04718 try
04719 {
04720
04721 unsigned int i, j;
04722 int rowNum, jacIndex;
04723 int jstart, idx;
04724 OSExpressionTree *expTree = NULL;
04725 int hessValuesIdx = 0;
04726 if( m_bLagrangianSparseHessianCreated == false) getLagrangianHessianSparsityPattern( );
04727 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
04728 std::map<int, int>::iterator posVarIndexMap;
04729 if( objLambda == NULL) throw ErrorClass("must have a multiplier for the objective function even if zero when calling getSecondOrderResults");
04730 if( conMultipliers == NULL) throw ErrorClass("cannot have a null vector of lagrange multipliers when calling getSecondOrderResults -- okay if zero");
04731 if( m_vdLambda.size() > 0) m_vdLambda.clear();
04732 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
04733 {
04734 if( posMapExpTree->first >= 0)
04735 {
04736 m_vdLambda.push_back( conMultipliers[ posMapExpTree->first]);
04737 }
04738 else
04739 {
04740
04741 m_vdLambda.push_back( objLambda[ abs(posMapExpTree->first) - 1] );
04742 }
04743 }
04744 for(i = 0; i < m_iNumberOfNonlinearVariables; i++)
04745 {
04746 m_vdDomainUnitVec[i] = 1.;
04747 rowNum = 0;
04748 if( m_mapExpressionTreesMod.size() > 0)
04749 {
04750 m_vdYjacval = this->forwardAD(1, m_vdDomainUnitVec);
04751 }
04752
04753
04754 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
04755 {
04756 idx = posMapExpTree->first;
04757
04758 if(idx >= 0)
04759 {
04760
04761
04762
04763
04764 expTree = m_mapExpressionTreesMod[ idx];
04765 if( (*expTree->mapVarIdx).find( m_miNonLinearVarsReverseMap[ i]) != (*expTree->mapVarIdx).end() )
04766 {
04767 jacIndex = (*m_mapExpressionTreesMod[ idx]->mapVarIdx)[ m_miNonLinearVarsReverseMap[ i]];
04768 jstart = m_miJacStart[ idx] + m_miJacNumConTerms[ idx];
04769 m_mdJacValue[ jstart + jacIndex] = m_vdYjacval[m_iObjectiveNumberNonlinear + rowNum];
04770 }
04771 rowNum++;
04772 }
04773 else
04774 {
04775
04776
04777 m_mmdObjGradient[ (abs( idx) - 1)][ m_miNonLinearVarsReverseMap[ i]] = m_vdYjacval[ (abs( idx) - 1)] +
04778 m_mmdDenseObjectiveCoefficients[ (abs( idx) - 1)][ m_miNonLinearVarsReverseMap[ i]];
04779 }
04780 }
04781
04782 if( m_mapExpressionTreesMod.size() > 0)
04783 {
04784 m_vdw = reverseAD(2, m_vdLambda);
04785 }
04786 for(j = i; j < m_iNumberOfNonlinearVariables; j++)
04787 {
04788 if( m_vbLagHessNonz[i*m_iNumberOfNonlinearVariables + j] == true)
04789 {
04790 m_LagrangianSparseHessian->hessValues[ hessValuesIdx] = m_vdw[ j*2 + 1];
04791 #ifdef DEBUG
04792 std::cout << "reverse 2 " << m_LagrangianSparseHessian->hessValues[ hessValuesIdx] << std::endl;
04793 #endif
04794 hessValuesIdx++;
04795 }
04796 }
04797
04798
04799 m_vdDomainUnitVec[i] = 0.;
04800 }
04801 #ifdef DEBUG
04802 int k;
04803 std::cout << "JACOBIAN DATA " << std::endl;
04804 for(idx = 0; idx < m_iConstraintNumber; idx++)
04805 {
04806 for(k = *(m_sparseJacMatrix->starts + idx); k < *(m_sparseJacMatrix->starts + idx + 1); k++)
04807 {
04808 std::cout << "row idx = " << idx << " col idx = "<< *(m_sparseJacMatrix->indexes + k)
04809 << " value = " << *(m_sparseJacMatrix->values + k) << std::endl;
04810 }
04811 }
04812 #endif
04813 return true;
04814 }
04815 catch(const ErrorClass& eclass)
04816 {
04817 throw ErrorClass( eclass.errormsg);
04818 }
04819 }
04820
04821 bool OSInstance::initForAlgDiff()
04822 {
04823 if( m_binitForAlgDiff == true ) return true;
04824 initializeNonLinearStructures( );
04825 initObjGradients();
04826 getAllNonlinearVariablesIndexMap( );
04827
04828
04829
04830 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
04831 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
04832 {
04833 if(posMapExpTree->second->bADMustReTape == true) m_bCppADMustReTape = true;
04834 }
04835
04836 #ifdef DEBUG
04837 std::cout << "RETAPE == " << m_bCppADMustReTape << std::endl;
04838 #endif
04839 unsigned int i;
04840 for(i = 0; i < m_iNumberOfNonlinearVariables; i++)
04841 {
04842 m_vdDomainUnitVec.push_back( 0.0 );
04843 }
04844 for(i = 0; i < m_mapExpressionTreesMod.size(); i++)
04845 {
04846 m_vdRangeUnitVec.push_back( 0.0 );
04847 }
04848 m_binitForAlgDiff = true;
04849
04850
04851 return true;
04852 }
04853
04854 bool OSInstance::initObjGradients()
04855 {
04856 int i, j;
04857 int m, n;
04858 m = getObjectiveNumber();
04859 n = getVariableNumber();
04860 getDenseObjectiveCoefficients();
04861 if(m <= 0)
04862 {
04863 m_mmdObjGradient = NULL;
04864 return true;
04865 }
04866 m_mmdObjGradient = new double*[m];
04867 for(i = 0; i < m; i++)
04868 {
04869 m_mmdObjGradient[i] = new double[n];
04870 for(j = 0; j < n; j++)
04871 {
04872 m_mmdObjGradient[i][j] = m_mmdDenseObjectiveCoefficients[ i][j];
04873 #ifdef DEBUG
04874 std::cout << "m_mmdObjGradient[i][j] = " << m_mmdObjGradient[i][j] << std::endl;
04875 #endif
04876 }
04877 }
04878 return true;
04879 }
04888 bool OSInstance::setTimeDomain(std::string format)
04889 {
04890 if ((format != "stages") && (format != "interval") && (format != "none"))
04891 return false;
04892 if (instanceData->timeDomain == NULL)
04893 {
04894 instanceData->timeDomain = new TimeDomain();
04895 }
04896 if (format == "stages")
04897 {
04898 if (instanceData->timeDomain->interval != NULL)
04899 {
04900 delete instanceData->timeDomain->interval;
04901 instanceData->timeDomain->interval = NULL;
04902 }
04903 if (instanceData->timeDomain->stages == NULL)
04904 instanceData->timeDomain->stages = new TimeDomainStages();
04905 m_sTimeDomainFormat = format;
04906 }
04907 if (format == "interval")
04908 {
04909 if (instanceData->timeDomain->stages != NULL)
04910 {
04911 delete instanceData->timeDomain->stages;
04912 instanceData->timeDomain->stages = NULL;
04913 }
04914 if (instanceData->timeDomain->interval == NULL)
04915 instanceData->timeDomain->interval = new TimeDomainInterval();
04916 m_sTimeDomainFormat = format;
04917 }
04918 if (format == "none")
04919 {
04920 if (instanceData->timeDomain->stages != NULL)
04921 {
04922 delete instanceData->timeDomain->stages;
04923 instanceData->timeDomain->stages = NULL;
04924 }
04925 if (instanceData->timeDomain->interval != NULL)
04926 {
04927 delete instanceData->timeDomain->interval;
04928 instanceData->timeDomain->interval = NULL;
04929 }
04930 m_sTimeDomainFormat = "";
04931 }
04932 return true;
04933 }
04934
04938 bool OSInstance::setTimeDomainStages(int number, std::string *names)
04939 {
04940 if (instanceData->timeDomain == NULL)
04941 instanceData->timeDomain = new TimeDomain();
04942 if (instanceData->timeDomain->interval != NULL)
04943 return false;
04944 if (instanceData->timeDomain->stages == NULL)
04945 {
04946 instanceData->timeDomain->stages = new TimeDomainStages;
04947 }
04948 else
04949 {
04950 if (instanceData->timeDomain->stages->numberOfStages != number)
04951 {
04952 for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
04953 {
04954 if (instanceData->timeDomain->stages->stage[i]->variables != NULL)
04955 {
04956 delete instanceData->timeDomain->stages->stage[i]->variables;
04957 instanceData->timeDomain->stages->stage[i]->variables = NULL;
04958 }
04959 if (instanceData->timeDomain->stages->stage[i]->constraints != NULL)
04960 {
04961 delete instanceData->timeDomain->stages->stage[i]->constraints;
04962 instanceData->timeDomain->stages->stage[i]->constraints = NULL;
04963 }
04964 if (instanceData->timeDomain->stages->stage[i]->objectives != NULL)
04965 {
04966 delete instanceData->timeDomain->stages->stage[i]->objectives;
04967 instanceData->timeDomain->stages->stage[i]->objectives = NULL;
04968 }
04969 delete instanceData->timeDomain->stages->stage[i];
04970 instanceData->timeDomain->stages->stage[i] = NULL;
04971 }
04972 delete []instanceData->timeDomain->stages->stage;
04973 instanceData->timeDomain->stages->stage = NULL;
04974 }
04975 }
04976 if (number != 0 )
04977 {
04978 if (instanceData->timeDomain->stages->stage == NULL)
04979 instanceData->timeDomain->stages->stage = new TimeDomainStage*[number];
04980 for (int i = 0; i < number; i++)
04981 {
04982 instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
04983 }
04984 instanceData->timeDomain->stages->numberOfStages = number;
04985 }
04986 for (int i = 0; i < number; i++)
04987
04988 {
04989 if (instanceData->timeDomain->stages->stage[i]->variables != NULL)
04990 {
04991 delete instanceData->timeDomain->stages->stage[i]->variables;
04992 instanceData->timeDomain->stages->stage[i]->variables = NULL;
04993 }
04994 instanceData->timeDomain->stages->stage[i]->variables = new TimeDomainStageVariables();
04995 if (instanceData->timeDomain->stages->stage[i]->constraints != NULL)
04996 {
04997 delete instanceData->timeDomain->stages->stage[i]->constraints;
04998 instanceData->timeDomain->stages->stage[i]->constraints = NULL;
04999 }
05000 instanceData->timeDomain->stages->stage[i]->constraints = new TimeDomainStageConstraints();
05001 if (instanceData->timeDomain->stages->stage[i]->objectives != NULL)
05002 {
05003 delete instanceData->timeDomain->stages->stage[i]->objectives;
05004 instanceData->timeDomain->stages->stage[i]->objectives = NULL;
05005 }
05006 instanceData->timeDomain->stages->stage[i]->objectives = new TimeDomainStageObjectives();
05007 instanceData->timeDomain->stages->stage[i]->objectives->numberOfObjectives = instanceData->objectives->numberOfObjectives;
05008 instanceData->timeDomain->stages->stage[i]->objectives->obj = new TimeDomainStageObj*[instanceData->objectives->numberOfObjectives];
05009 for (int j = 0; j < instanceData->objectives->numberOfObjectives; j++)
05010 {
05011 instanceData->timeDomain->stages->stage[i]->objectives->obj[j] = new TimeDomainStageObj();
05012 instanceData->timeDomain->stages->stage[i]->objectives->obj[j]->idx = -(j+1);
05013 }
05014 if (names != NULL)
05015 instanceData->timeDomain->stages->stage[i]->name = names[i];
05016 }
05017 return true;
05018 }
05019
05023 bool OSInstance::setTimeDomainStageVariablesOrdered(int numberOfStages, int *numberOfVariables, int *startIdx)
05024 {
05025 if (instanceData->timeDomain == NULL)
05026 instanceData->timeDomain = new TimeDomain();
05027 if (instanceData->timeDomain->interval != NULL)
05028 return false;
05029 if (instanceData->timeDomain->stages == NULL)
05030 instanceData->timeDomain->stages = new TimeDomainStages();
05031 if (instanceData->timeDomain->stages != NULL)
05032 {
05033 if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
05034 (instanceData->timeDomain->stages->numberOfStages != 0))
05035 return false;
05036 }
05037 if (instanceData->timeDomain->stages->numberOfStages == 0)
05038 {
05039 instanceData->timeDomain->stages->numberOfStages = numberOfStages;
05040 if (instanceData->timeDomain->stages->stage == NULL)
05041 instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
05042 for (int i = 0; i < numberOfStages; i++)
05043 instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
05044 }
05045 int checksum = 0;
05046 for (int i = 0; i < numberOfStages; i++)
05047
05048 {
05049 if (instanceData->timeDomain->stages->stage[i]->variables != NULL)
05050 {
05051 delete instanceData->timeDomain->stages->stage[i]->variables;
05052 instanceData->timeDomain->stages->stage[i]->variables = NULL;
05053 }
05054 instanceData->timeDomain->stages->stage[i]->variables = new TimeDomainStageVariables();
05055 instanceData->timeDomain->stages->stage[i]->variables->startIdx = startIdx[i];
05056 instanceData->timeDomain->stages->stage[i]->variables->numberOfVariables = numberOfVariables[i];
05057 checksum += numberOfVariables[i];
05058 }
05059 return (checksum == instanceData->variables->numberOfVariables);
05060 }
05061
05065 bool OSInstance::setTimeDomainStageVariablesUnordered(int numberOfStages, int *numberOfVariables, int **varIndex)
05066 {
05067 if (instanceData->timeDomain == NULL)
05068 instanceData->timeDomain = new TimeDomain();
05069 if (instanceData->timeDomain->interval != NULL)
05070 return false;
05071 if (instanceData->timeDomain->stages == NULL)
05072 instanceData->timeDomain->stages = new TimeDomainStages();
05073 if (instanceData->timeDomain->stages != NULL)
05074 {
05075 if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
05076 (instanceData->timeDomain->stages->numberOfStages != 0))
05077 return false;
05078 }
05079 if (instanceData->timeDomain->stages->numberOfStages == 0 )
05080 {
05081 instanceData->timeDomain->stages->numberOfStages = numberOfStages;
05082 if (instanceData->timeDomain->stages->stage == NULL)
05083 instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
05084 for (int i = 0; i < numberOfStages; i++)
05085 instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
05086 }
05087 int checksum = 0;
05088 for (int i = 0; i < numberOfStages; i++)
05089
05090 {
05091 if (instanceData->timeDomain->stages->stage[i]->variables != NULL)
05092 {
05093 delete instanceData->timeDomain->stages->stage[i]->variables;
05094 instanceData->timeDomain->stages->stage[i]->variables = NULL;
05095 }
05096 instanceData->timeDomain->stages->stage[i]->variables = new TimeDomainStageVariables();
05097 instanceData->timeDomain->stages->stage[i]->variables->numberOfVariables = numberOfVariables[i];
05098 instanceData->timeDomain->stages->stage[i]->variables->var = new TimeDomainStageVar*[numberOfVariables[i]];
05099 for (int j = 0; j < numberOfVariables[i]; j++)
05100 {
05101 instanceData->timeDomain->stages->stage[i]->variables->var[j] = new TimeDomainStageVar();
05102 instanceData->timeDomain->stages->stage[i]->variables->var[j]->idx = varIndex[i][j];
05103 }
05104 checksum += numberOfVariables[i];
05105 }
05106 if (checksum != instanceData->variables->numberOfVariables) return false;
05107 int *checkvar = new int[instanceData->variables->numberOfVariables];
05108 for (int j = 0; j < instanceData->variables->numberOfVariables; j++)
05109 checkvar[j] = -1;
05110 int k;
05111 for (int i = 0; i < numberOfStages; i++)
05112 for (int j = 0; j < instanceData->timeDomain->stages->stage[i]->variables->numberOfVariables; j++)
05113 {
05114 k = instanceData->timeDomain->stages->stage[i]->variables->var[j]->idx;
05115 if (checkvar[k] != -1)
05116 {
05117 delete [] checkvar;
05118 checkvar = NULL;
05119 return false;
05120 }
05121 checkvar[k] = instanceData->timeDomain->stages->stage[i]->variables->var[j]->idx;
05122 }
05123 delete [] checkvar;
05124 checkvar = NULL;
05125 return true;
05126 }
05127
05131 bool OSInstance::setTimeDomainStageConstraintsOrdered(int numberOfStages, int *numberOfConstraints, int *startIdx)
05132 {
05133 if (instanceData->timeDomain == NULL)
05134 instanceData->timeDomain = new TimeDomain();
05135 if (instanceData->timeDomain->interval != NULL)
05136 return false;
05137 if (instanceData->timeDomain->stages == NULL)
05138 instanceData->timeDomain->stages = new TimeDomainStages();
05139 if (instanceData->timeDomain->stages != NULL)
05140 {
05141 if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
05142 (instanceData->timeDomain->stages->numberOfStages != 0))
05143 return false;
05144 }
05145 if (instanceData->timeDomain->stages->numberOfStages == 0 )
05146 {
05147 instanceData->timeDomain->stages->numberOfStages = numberOfStages;
05148 if (instanceData->timeDomain->stages->stage == NULL)
05149 instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
05150 for (int i = 0; i < numberOfStages; i++)
05151 instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
05152 }
05153 int checksum = 0;
05154 for (int i = 0; i < numberOfStages; i++)
05155
05156 {
05157 if (instanceData->timeDomain->stages->stage[i]->constraints != NULL)
05158 {
05159 delete instanceData->timeDomain->stages->stage[i]->constraints;
05160 instanceData->timeDomain->stages->stage[i]->constraints = NULL;
05161 }
05162 instanceData->timeDomain->stages->stage[i]->constraints = new TimeDomainStageConstraints();
05163 instanceData->timeDomain->stages->stage[i]->constraints->startIdx = startIdx[i];
05164 instanceData->timeDomain->stages->stage[i]->constraints->numberOfConstraints = numberOfConstraints[i];
05165 checksum += numberOfConstraints[i];
05166 }
05167 return (checksum == instanceData->constraints->numberOfConstraints);
05168 }
05169
05173 bool OSInstance::setTimeDomainStageConstraintsUnordered(int numberOfStages, int *numberOfConstraints, int **conIndex)
05174 {
05175 if (instanceData->timeDomain == NULL)
05176 instanceData->timeDomain = new TimeDomain();
05177 if (instanceData->timeDomain->interval != NULL)
05178 return false;
05179 if (instanceData->timeDomain->stages == NULL)
05180 instanceData->timeDomain->stages = new TimeDomainStages();
05181 if (instanceData->timeDomain->stages != NULL)
05182 {
05183 if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
05184 (instanceData->timeDomain->stages->numberOfStages != 0))
05185 return false;
05186 }
05187 if ( instanceData->timeDomain->stages->numberOfStages == 0 )
05188 {
05189 instanceData->timeDomain->stages->numberOfStages = numberOfStages;
05190 if (instanceData->timeDomain->stages->stage == NULL)
05191 instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
05192 for (int i = 0; i < numberOfStages; i++)
05193 instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
05194 }
05195 int checksum = 0;
05196 for (int i = 0; i < numberOfStages; i++)
05197
05198 {
05199 if (instanceData->timeDomain->stages->stage[i]->constraints != NULL)
05200 {
05201 delete instanceData->timeDomain->stages->stage[i]->constraints;
05202 instanceData->timeDomain->stages->stage[i]->constraints = NULL;
05203 }
05204 instanceData->timeDomain->stages->stage[i]->constraints = new TimeDomainStageConstraints();
05205 instanceData->timeDomain->stages->stage[i]->constraints->numberOfConstraints = numberOfConstraints[i];
05206 instanceData->timeDomain->stages->stage[i]->constraints->con = new TimeDomainStageCon*[numberOfConstraints[i]];
05207 for (int j = 0; j < numberOfConstraints[i]; j++)
05208 {
05209 instanceData->timeDomain->stages->stage[i]->constraints->con[j] = new TimeDomainStageCon();
05210 instanceData->timeDomain->stages->stage[i]->constraints->con[j]->idx = conIndex[i][j];
05211 }
05212 checksum += numberOfConstraints[i];
05213 }
05214 if (checksum != instanceData->constraints->numberOfConstraints) return false;
05215 int *checkvar = new int[instanceData->constraints->numberOfConstraints];
05216 for (int j = 0; j < instanceData->constraints->numberOfConstraints; j++)
05217 checkvar[j] = -1;
05218 int k;
05219 for (int i = 0; i < numberOfStages; i++)
05220 for (int j = 0; j < instanceData->timeDomain->stages->stage[i]->constraints->numberOfConstraints; j++)
05221 {
05222 k = instanceData->timeDomain->stages->stage[i]->constraints->con[j]->idx;
05223 if (checkvar[k] != -1)
05224 {
05225 delete [] checkvar;
05226 checkvar = NULL;
05227 return false;
05228 }
05229 checkvar[k] = instanceData->timeDomain->stages->stage[i]->constraints->con[j]->idx;
05230 }
05231 delete [] checkvar;
05232 checkvar = NULL;
05233 return true;
05234 }
05235
05239 bool OSInstance::setTimeDomainStageObjectivesOrdered(int numberOfStages, int *numberOfObjectives, int *startIdx)
05240 {
05241 if (instanceData->timeDomain == NULL)
05242 instanceData->timeDomain = new TimeDomain();
05243 if (instanceData->timeDomain->interval != NULL)
05244 return false;
05245 if (instanceData->timeDomain->stages == NULL)
05246 instanceData->timeDomain->stages = new TimeDomainStages();
05247 if (instanceData->timeDomain->stages != NULL)
05248 {
05249 if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
05250 (instanceData->timeDomain->stages->numberOfStages != 0))
05251 return false;
05252 }
05253 if (instanceData->timeDomain->stages->numberOfStages == 0)
05254 {
05255 instanceData->timeDomain->stages->numberOfStages = numberOfStages;
05256 if (instanceData->timeDomain->stages->stage == NULL)
05257 instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
05258 for (int i = 0; i < numberOfStages; i++)
05259 instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
05260 }
05261 for (int i = 0; i < numberOfStages; i++)
05262
05263 {
05264 if (instanceData->timeDomain->stages->stage[i]->objectives != NULL)
05265 {
05266 delete instanceData->timeDomain->stages->stage[i]->objectives;
05267 instanceData->timeDomain->stages->stage[i]->objectives = NULL;
05268 }
05269 instanceData->timeDomain->stages->stage[i]->objectives = new TimeDomainStageObjectives();
05270 instanceData->timeDomain->stages->stage[i]->objectives->startIdx = startIdx[i];
05271 instanceData->timeDomain->stages->stage[i]->objectives->numberOfObjectives = numberOfObjectives[i];
05272 }
05273 return true;
05274 }
05275
05279 bool OSInstance::setTimeDomainStageObjectivesUnordered(int numberOfStages, int *numberOfObjectives, int **objIndex)
05280 {
05281 if (instanceData->timeDomain == NULL)
05282 instanceData->timeDomain = new TimeDomain();
05283 if (instanceData->timeDomain->interval != NULL)
05284 return false;
05285 if (instanceData->timeDomain->stages == NULL)
05286 instanceData->timeDomain->stages = new TimeDomainStages();
05287 if (instanceData->timeDomain->stages != NULL)
05288 {
05289 if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
05290 (instanceData->timeDomain->stages->numberOfStages != 0))
05291 return false;
05292 }
05293 if (instanceData->timeDomain->stages->numberOfStages == 0)
05294 {
05295 instanceData->timeDomain->stages->numberOfStages = numberOfStages;
05296 if (instanceData->timeDomain->stages->stage == NULL)
05297 instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
05298 for (int i = 0; i < numberOfStages; i++)
05299 instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
05300 }
05301 for (int i = 0; i < numberOfStages; i++)
05302
05303 {
05304 if (instanceData->timeDomain->stages->stage[i]->objectives != NULL)
05305 {
05306 delete instanceData->timeDomain->stages->stage[i]->objectives;
05307 instanceData->timeDomain->stages->stage[i]->objectives = NULL;
05308 }
05309 instanceData->timeDomain->stages->stage[i]->objectives = new TimeDomainStageObjectives();
05310 instanceData->timeDomain->stages->stage[i]->objectives->numberOfObjectives = numberOfObjectives[i];
05311 instanceData->timeDomain->stages->stage[i]->objectives->obj = new TimeDomainStageObj*[numberOfObjectives[i]];
05312 for (int j = 0; j < numberOfObjectives[i]; j++)
05313 {
05314 instanceData->timeDomain->stages->stage[i]->objectives->obj[j] = new TimeDomainStageObj();
05315 instanceData->timeDomain->stages->stage[i]->objectives->obj[j]->idx = objIndex[i][j];
05316 }
05317 }
05318 int *checkvar = new int[instanceData->objectives->numberOfObjectives];
05319 for (int j = 0; j < instanceData->objectives->numberOfObjectives; j++)
05320 checkvar[j] = 0;
05321 int k;
05322 for (int i = 0; i < numberOfStages; i++)
05323 for (int j = 0; j < instanceData->timeDomain->stages->stage[i]->objectives->numberOfObjectives; j++)
05324 {
05325 k = -instanceData->timeDomain->stages->stage[i]->objectives->obj[j]->idx-1;
05326 checkvar[k] = instanceData->timeDomain->stages->stage[i]->objectives->obj[j]->idx;
05327 }
05328 for (int i = 0; i < instanceData->objectives->numberOfObjectives; i++)
05329 if (checkvar[i] == 0)
05330 {
05331 delete [] checkvar;
05332 checkvar = NULL;
05333 return false;
05334 }
05335 delete [] checkvar;
05336 checkvar = NULL;
05337 return true;
05338 }
05339
05343 bool OSInstance::setTimeDomainInterval(double start, double horizon)
05344 {
05345 if (instanceData->timeDomain == NULL)
05346 instanceData->timeDomain = new TimeDomain();
05347 if (instanceData->timeDomain->stages != NULL)
05348 return false;
05349 if (instanceData->timeDomain->interval == NULL)
05350 instanceData->timeDomain->interval = new TimeDomainInterval();
05351 instanceData->timeDomain->interval->start = start;
05352 instanceData->timeDomain->interval->horizon = horizon;
05353 return true;
05354 }
05355
05356
05357 bool OSInstance::createOSADFun(std::vector<double> vdX)
05358 {
05359 try
05360 {
05361 if(m_bOSADFunIsCreated == true) return true;
05362
05363 if(m_binitForAlgDiff == false) initForAlgDiff();
05364
05365
05366 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
05367 unsigned int i;
05368 size_t n = vdX.size();
05369 #ifdef COIN_HAS_CPPAD
05370
05371 CppAD::vector< CppAD::AD<double> > vdaX( n );
05372 for(i = 0; i < n; i++)
05373 {
05374 vdaX[ i] = vdX[ i];
05375
05376 }
05377
05378 CppAD::Independent( vdaX);
05384 CppAD::vector< CppAD::AD<double> > m_vFG;
05385 int kount = 0;
05386 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
05387 {
05388 m_vFG.push_back( (posMapExpTree->second)->m_treeRoot->constructADTape(&m_mapAllNonlinearVariablesIndex, &vdaX) );
05389
05390 if( m_mapOSADFunRangeIndex.find( posMapExpTree->first) == m_mapOSADFunRangeIndex.end() )
05391 {
05392
05393 m_mapOSADFunRangeIndex[ posMapExpTree->first] = kount;
05394 kount++;
05395 }
05396 }
05397
05398
05399 Fad = new CppAD::ADFun<double>(vdaX, m_vFG);
05400
05401
05402 m_iHighestTaylorCoeffOrder = -1;
05403 m_bOSADFunIsCreated = true;
05404 #else
05405 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
05406 #endif
05407
05408 return true;
05409 }
05410 catch(const ErrorClass& eclass)
05411 {
05412 throw ErrorClass( eclass.errormsg);
05413 }
05414 }
05415
05416
05417 std::vector<double> OSInstance::forwardAD(int p, std::vector<double> vdX)
05418 {
05419 try
05420 {
05421
05422 if(m_bOSADFunIsCreated == false) createOSADFun( vdX);
05423 if(p > (m_iHighestTaylorCoeffOrder + 1) ) throw
05424 ErrorClass( "trying to calculate a p order forward when p-1 Taylor coefficient not available");
05425
05426 m_iHighestTaylorCoeffOrder = p;
05427 m_iHighestOrderEvaluated = p;
05428
05429
05430
05431 #ifdef COIN_HAS_CPPAD
05432 return (*Fad).Forward(p, vdX);
05433 #else
05434 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
05435 #endif
05436
05437 }
05438 catch(const ErrorClass& eclass)
05439 {
05440 throw ErrorClass( eclass.errormsg);
05441 }
05442 }
05443
05444
05445 std::vector<double> OSInstance::reverseAD(int p, std::vector<double> vdlambda)
05446 {
05447 try
05448 {
05449 #ifndef COIN_HAS_CPPAD
05450 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
05451 #endif
05452 if(p == 0) throw
05453 ErrorClass( "reverseAD must have p >= 1");
05454 if(p > (m_iHighestTaylorCoeffOrder + 1) ) throw
05455 ErrorClass( "trying to calculate a p order reverse when p-1 Taylor coefficient not available");
05456
05457
05458
05459 m_iHighestOrderEvaluated = p;
05460 #ifdef COIN_HAS_CPPAD
05461 return (*Fad).Reverse(p, vdlambda);
05462 #else
05463 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
05464 #endif
05465
05466 }
05467 catch(const ErrorClass& eclass)
05468 {
05469 throw ErrorClass( eclass.errormsg);
05470 }
05471 }
05472
05473
05474 int OSInstance::getADSparsityHessian()
05475 {
05476
05477 unsigned int i;
05478 int numNonz;
05479 numNonz = 0;
05480 try
05481 {
05482 std::vector<bool> r(m_iNumberOfNonlinearVariables * m_iNumberOfNonlinearVariables);
05483 unsigned int j;
05484 for(i = 0; i < m_iNumberOfNonlinearVariables; i++)
05485 {
05486 for(j = 0; j < m_iNumberOfNonlinearVariables; j++)
05487 r[ i * m_iNumberOfNonlinearVariables + j ] = false;
05488 r[ i * m_iNumberOfNonlinearVariables + i] = true;
05489 }
05490
05491
05492 #ifdef COIN_HAS_CPPAD
05493 (*Fad).ForSparseJac(m_iNumberOfNonlinearVariables, r);
05494 #else
05495 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
05496 #endif
05497
05498
05499 unsigned int m = m_mapExpressionTreesMod.size();
05500 std::vector<bool> e( m);
05501
05502 for(i = 0; i < m; i++) e[i] = true;
05503
05504
05505 #ifdef COIN_HAS_CPPAD
05506 m_vbLagHessNonz = (*Fad).RevSparseHes(m_iNumberOfNonlinearVariables, e);
05507 #else
05508 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
05509 #endif
05510
05511 for(i = 0; i < m_iNumberOfNonlinearVariables; i++)
05512 {
05513
05514 for(j = i; j < m_iNumberOfNonlinearVariables; j++)
05515 {
05516 if(m_vbLagHessNonz[ i*m_iNumberOfNonlinearVariables + j] == true) numNonz++;
05517
05518 }
05519
05520 }
05521 return numNonz;
05522 }
05523 catch(const ErrorClass& eclass)
05524 {
05525 throw ErrorClass( eclass.errormsg);
05526 }
05527 }
05528
05529
05530
05531
05532
05533
05534
05535 bool OSInstance::IsEqual(OSInstance *that)
05536 {
05537 #ifdef DEBUG_ISEQUAL_ROUTINES
05538 cout << "Start comparing in OSInstance" << endl;
05539 #endif
05540 if (this == NULL)
05541 {
05542 if (that == NULL)
05543 return true;
05544 else
05545 {
05546 #ifdef DEBUG_ISEQUAL_ROUTINES
05547 cout << "First object is NULL, second is not" << endl;
05548 #endif
05549 return false;
05550 }
05551 }
05552 else
05553 {
05554 if (that == NULL)
05555 {
05556 #ifdef DEBUG_ISEQUAL_ROUTINES
05557 cout << "Second object is NULL, first is not" << endl;
05558 #endif
05559 return false;
05560 }
05561 else
05562 {
05563 if (!this->instanceHeader->IsEqual(that->instanceHeader))
05564 return false;
05565 if (!this->instanceData->IsEqual(that->instanceData))
05566 return false;
05567
05568 return true;
05569 }
05570 }
05571 }
05572
05573
05574 bool InstanceHeader::IsEqual(InstanceHeader *that)
05575 {
05576 #ifdef DEBUG_ISEQUAL_ROUTINES
05577 cout << "Start comparing in InstanceHeader" << endl;
05578 #endif
05579 if (this == NULL)
05580 {
05581 if (that == NULL)
05582 return true;
05583 else
05584 {
05585 #ifdef DEBUG_ISEQUAL_ROUTINES
05586 cout << "First object is NULL, second is not" << endl;
05587 #endif
05588 return false;
05589 }
05590 }
05591 else
05592 {
05593 if (that == NULL)
05594 {
05595 #ifdef DEBUG_ISEQUAL_ROUTINES
05596 cout << "Second object is NULL, first is not" << endl;
05597 #endif
05598 return false;
05599 }
05600 else
05601 {
05602 if (this->description != that->description)
05603 return false;
05604 if (this->name != that->name)
05605 return false;
05606 if (this->source != that->source)
05607 return false;
05608
05609 return true;
05610 }
05611 }
05612 }
05613
05614 bool InstanceData::IsEqual(InstanceData *that)
05615 {
05616 #ifdef DEBUG_ISEQUAL_ROUTINES
05617 cout << "Start comparing in InstanceData" << endl;
05618 #endif
05619 if (this == NULL)
05620 {
05621 if (that == NULL)
05622 return true;
05623 else
05624 {
05625 #ifdef DEBUG_ISEQUAL_ROUTINES
05626 cout << "First object is NULL, second is not" << endl;
05627 #endif
05628 return false;
05629 }
05630 }
05631 else
05632 {
05633 if (that == NULL)
05634 {
05635 #ifdef DEBUG_ISEQUAL_ROUTINES
05636 cout << "Second object is NULL, first is not" << endl;
05637 #endif
05638 return false;
05639 }
05640 else
05641 {
05642 if (!this->variables->IsEqual(that->variables))
05643 return false;
05644 if (!this->objectives->IsEqual(that->objectives))
05645 return false;
05646 if (!this->constraints->IsEqual(that->constraints))
05647 return false;
05648 if (!this->linearConstraintCoefficients->IsEqual(that->linearConstraintCoefficients))
05649 return false;
05650 if (!this->quadraticCoefficients->IsEqual(that->quadraticCoefficients))
05651 return false;
05652 if (!this->nonlinearExpressions->IsEqual(that->nonlinearExpressions))
05653 return false;
05654
05655 return true;
05656 }
05657 }
05658 }
05659
05660 bool Variables::IsEqual(Variables *that)
05661 {
05662 #ifdef DEBUG_ISEQUAL_ROUTINES
05663 cout << "Start comparing in Variables" << endl;
05664 #endif
05665 if (this == NULL)
05666 {
05667 if (that == NULL)
05668 return true;
05669 else
05670 {
05671 #ifdef DEBUG_ISEQUAL_ROUTINES
05672 cout << "First object is NULL, second is not" << endl;
05673 #endif
05674 return false;
05675 }
05676 }
05677 else
05678 {
05679 if (that == NULL)
05680 {
05681 #ifdef DEBUG_ISEQUAL_ROUTINES
05682 cout << "Second object is NULL, first is not" << endl;
05683 #endif
05684 return false;
05685 }
05686 else
05687 {
05688 if (this->numberOfVariables != that->numberOfVariables)
05689 return false;
05690 for (int i=0; i<this->numberOfVariables; i++)
05691 if (!this->var[i]->IsEqual(that->var[i]))
05692 return false;
05693
05694 return true;
05695 }
05696 }
05697 }
05698
05699 bool Variable::IsEqual(Variable *that)
05700 {
05701 #ifdef DEBUG_ISEQUAL_ROUTINES
05702 cout << "Start comparing in Variable" << endl;
05703 #endif
05704 if (this == NULL)
05705 {
05706 if (that == NULL)
05707 return true;
05708 else
05709 {
05710 #ifdef DEBUG_ISEQUAL_ROUTINES
05711 cout << "First object is NULL, second is not" << endl;
05712 #endif
05713 return false;
05714 }
05715 }
05716 else
05717 {
05718 if (that == NULL)
05719 {
05720 #ifdef DEBUG_ISEQUAL_ROUTINES
05721 cout << "Second object is NULL, first is not" << endl;
05722 #endif
05723 return false;
05724 }
05725 else
05726 {
05727 if (this->lb != that->lb)
05728 return false;
05729 if (this->ub != that->ub)
05730 return false;
05731 if (this->type != that->type)
05732 return false;
05733 if (this->name != that->name)
05734 return false;
05735
05736 return true;
05737 }
05738 }
05739 }
05740
05741 bool Objectives::IsEqual(Objectives *that)
05742 {
05743 #ifdef DEBUG_ISEQUAL_ROUTINES
05744 cout << "Start comparing in Objectives" << endl;
05745 #endif
05746 if (this == NULL)
05747 {
05748 if (that == NULL)
05749 return true;
05750 else
05751 {
05752 #ifdef DEBUG_ISEQUAL_ROUTINES
05753 cout << "First object is NULL, second is not" << endl;
05754 #endif
05755 return false;
05756 }
05757 }
05758 else
05759 {
05760 if (that == NULL)
05761 {
05762 #ifdef DEBUG_ISEQUAL_ROUTINES
05763 cout << "Second object is NULL, first is not" << endl;
05764 #endif
05765 return false;
05766 }
05767 else
05768 {
05769 if (this->numberOfObjectives != that->numberOfObjectives)
05770 return false;
05771 for (int i=0; i<this->numberOfObjectives; i++)
05772 if (!this->obj[i]->IsEqual(that->obj[i]))
05773 return false;
05774
05775 return true;
05776 }
05777 }
05778 }
05779
05780 bool Objective::IsEqual(Objective *that)
05781 {
05782 #ifdef DEBUG_ISEQUAL_ROUTINES
05783 cout << "Start comparing in Objective" << endl;
05784 #endif
05785 if (this == NULL)
05786 {
05787 if (that == NULL)
05788 return true;
05789 else
05790 {
05791 #ifdef DEBUG_ISEQUAL_ROUTINES
05792 cout << "First object is NULL, second is not" << endl;
05793 #endif
05794 return false;
05795 }
05796 }
05797 else
05798 {
05799 if (that == NULL)
05800 {
05801 #ifdef DEBUG_ISEQUAL_ROUTINES
05802 cout << "Second object is NULL, first is not" << endl;
05803 #endif
05804 return false;
05805 }
05806 else
05807 {
05808 if (this->name != that->name)
05809 return false;
05810 if (this->maxOrMin != that->maxOrMin)
05811 return false;
05812 if (this->constant != that->constant)
05813 return false;
05814 if (this->weight != that->weight)
05815 return false;
05816 if (this->numberOfObjCoef != that->numberOfObjCoef)
05817 return false;
05818
05819 for (int i=0; i<this->numberOfObjCoef; i++)
05820 if (!this->coef[i]->IsEqual(that->coef[i]))
05821 return false;
05822
05823 return true;
05824 }
05825 }
05826 }
05827
05828 bool ObjCoef::IsEqual(ObjCoef *that)
05829 {
05830 #ifdef DEBUG_ISEQUAL_ROUTINES
05831 cout << "Start comparing in ObjCoef" << endl;
05832 #endif
05833 if (this == NULL)
05834 {
05835 if (that == NULL)
05836 return true;
05837 else
05838 {
05839 #ifdef DEBUG_ISEQUAL_ROUTINES
05840 cout << "First object is NULL, second is not" << endl;
05841 #endif
05842 return false;
05843 }
05844 }
05845 else
05846 {
05847 if (that == NULL)
05848 {
05849 #ifdef DEBUG_ISEQUAL_ROUTINES
05850 cout << "Second object is NULL, first is not" << endl;
05851 #endif
05852 return false;
05853 }
05854 else
05855 {
05856 if (this->idx != that->idx)
05857 return false;
05858 if (this->value != that->value)
05859 return false;
05860
05861 return true;
05862 }
05863 }
05864 }
05865
05866 bool Constraints::IsEqual(Constraints *that)
05867 {
05868 #ifdef DEBUG_ISEQUAL_ROUTINES
05869 cout << "Start comparing in Constraints" << endl;
05870 #endif
05871 if (this == NULL)
05872 {
05873 if (that == NULL)
05874 return true;
05875 else
05876 {
05877 #ifdef DEBUG_ISEQUAL_ROUTINES
05878 cout << "First object is NULL, second is not" << endl;
05879 #endif
05880 return false;
05881 }
05882 }
05883 else
05884 {
05885 if (that == NULL)
05886 {
05887 #ifdef DEBUG_ISEQUAL_ROUTINES
05888 cout << "Second object is NULL, first is not" << endl;
05889 #endif
05890 return false;
05891 }
05892 else
05893 {
05894 if (this->numberOfConstraints != that->numberOfConstraints)
05895 return false;
05896 for (int i=0; i<this->numberOfConstraints; i++)
05897 if (!this->con[i]->IsEqual(that->con[i]))
05898 return false;
05899
05900 return true;
05901 }
05902 }
05903 }
05904
05905 bool Constraint::IsEqual(Constraint *that)
05906 {
05907 #ifdef DEBUG_ISEQUAL_ROUTINES
05908 cout << "Start comparing in Constraint" << endl;
05909 #endif
05910 if (this == NULL)
05911 {
05912 if (that == NULL)
05913 return true;
05914 else
05915 {
05916 #ifdef DEBUG_ISEQUAL_ROUTINES
05917 cout << "First object is NULL, second is not" << endl;
05918 #endif
05919 return false;
05920 }
05921 }
05922 else
05923 {
05924 if (that == NULL)
05925 {
05926 #ifdef DEBUG_ISEQUAL_ROUTINES
05927 cout << "Second object is NULL, first is not" << endl;
05928 #endif
05929 return false;
05930 }
05931 else
05932 {
05933 if (this->name != that->name)
05934 return false;
05935 if (this->constant != that->constant)
05936 return false;
05937 if (this->lb != that->lb)
05938 return false;
05939 if (this->ub != that->ub)
05940 return false;
05941
05942 return true;
05943 }
05944 }
05945 }
05946
05947 bool LinearConstraintCoefficients::IsEqual(LinearConstraintCoefficients *that)
05948 {
05949 #ifdef DEBUG_ISEQUAL_ROUTINES
05950 cout << "Start comparing in LinearConstraintCoefficients" << endl;
05951 #endif
05952 if (this == NULL)
05953 {
05954 if (that == NULL)
05955 return true;
05956 else
05957 {
05958 #ifdef DEBUG_ISEQUAL_ROUTINES
05959 cout << "First object is NULL, second is not" << endl;
05960 #endif
05961 return false;
05962 }
05963 }
05964 else
05965 {
05966 if (that == NULL)
05967 {
05968 #ifdef DEBUG_ISEQUAL_ROUTINES
05969 cout << "Second object is NULL, first is not" << endl;
05970 #endif
05971 return false;
05972 }
05973 else
05974 {
05975 if (this->numberOfValues != that->numberOfValues)
05976 return false;
05977
05978 if (!this->start->IsEqual(that->start))
05979 return false;
05980 if (!this->rowIdx->IsEqual(that->rowIdx))
05981 return false;
05982 if (!this->colIdx->IsEqual(that->colIdx))
05983 return false;
05984 if (!this->value->IsEqual(that->value))
05985 return false;
05986
05987 return true;
05988 }
05989 }
05990 }
05991
05992 bool QuadraticCoefficients::IsEqual(QuadraticCoefficients *that)
05993 {
05994 #ifdef DEBUG_ISEQUAL_ROUTINES
05995 cout << "Start comparing in QuadraticCoefficients" << endl;
05996 #endif
05997 if (this == NULL)
05998 {
05999 if (that == NULL)
06000 return true;
06001 else
06002 {
06003 #ifdef DEBUG_ISEQUAL_ROUTINES
06004 cout << "First object is NULL, second is not" << endl;
06005 #endif
06006 return false;
06007 }
06008 }
06009 else
06010 {
06011 if (that == NULL)
06012 {
06013 #ifdef DEBUG_ISEQUAL_ROUTINES
06014 cout << "Second object is NULL, first is not" << endl;
06015 #endif
06016 return false;
06017 }
06018 else
06019 {
06020 if (this->numberOfQuadraticTerms != that->numberOfQuadraticTerms)
06021 return false;
06022 for (int i=0; i<this->numberOfQuadraticTerms; i++)
06023 if (!this->qTerm[i]->IsEqual(that->qTerm[i]))
06024 return false;
06025
06026 return true;
06027 }
06028 }
06029 }
06030
06031
06032 bool QuadraticTerm::IsEqual(QuadraticTerm *that)
06033 {
06034 #ifdef DEBUG_ISEQUAL_ROUTINES
06035 cout << "Start comparing in QuadraticTerm" << endl;
06036 #endif
06037 if (this == NULL)
06038 {
06039 if (that == NULL)
06040 return true;
06041 else
06042 {
06043 #ifdef DEBUG_ISEQUAL_ROUTINES
06044 cout << "First object is NULL, second is not" << endl;
06045 #endif
06046 return false;
06047 }
06048 }
06049 else
06050 {
06051 if (that == NULL)
06052 {
06053 #ifdef DEBUG_ISEQUAL_ROUTINES
06054 cout << "Second object is NULL, first is not" << endl;
06055 #endif
06056 return false;
06057 }
06058 else
06059 {
06060 if (this->idx != that->idx)
06061 return false;
06062 if (this->idxOne != that->idxOne)
06063 return false;
06064 if (this->idxTwo != that->idxTwo)
06065 return false;
06066 if (this->coef != that->coef)
06067 return false;
06068
06069 return true;
06070 }
06071 }
06072 }
06073
06074 bool NonlinearExpressions::IsEqual(NonlinearExpressions *that)
06075 {
06076 #ifdef DEBUG_ISEQUAL_ROUTINES
06077 cout << "Start comparing in NonlinearExpressions" << endl;
06078 #endif
06079 if (this == NULL)
06080 {
06081 if (that == NULL)
06082 return true;
06083 else
06084 {
06085 #ifdef DEBUG_ISEQUAL_ROUTINES
06086 cout << "First object is NULL, second is not" << endl;
06087 #endif
06088 return false;
06089 }
06090 }
06091 else
06092 {
06093 if (that == NULL)
06094 {
06095 #ifdef DEBUG_ISEQUAL_ROUTINES
06096 cout << "Second object is NULL, first is not" << endl;
06097 #endif
06098 return false;
06099 }
06100 else
06101 {
06102 if (this->numberOfNonlinearExpressions != that->numberOfNonlinearExpressions)
06103 return false;
06104
06105 for (int i=0; i<this->numberOfNonlinearExpressions; i++)
06106 if (!this->nl[i]->IsEqual(that->nl[i]))
06107 return false;
06108
06109 return true;
06110 }
06111 }
06112 }
06113
06114
06115
06116 bool Nl::IsEqual(Nl *that)
06117 {
06118 #ifdef DEBUG_ISEQUAL_ROUTINES
06119 cout << "Start comparing in Nl" << endl;
06120 #endif
06121 if (this == NULL)
06122 {
06123 if (that == NULL)
06124 return true;
06125 else
06126 {
06127 #ifdef DEBUG_ISEQUAL_ROUTINES
06128 cout << "First object is NULL, second is not" << endl;
06129 #endif
06130 return false;
06131 }
06132 }
06133 else
06134 {
06135 if (that == NULL)
06136 {
06137 #ifdef DEBUG_ISEQUAL_ROUTINES
06138 cout << "Second object is NULL, first is not" << endl;
06139 #endif
06140 return false;
06141 }
06142 else
06143 {
06144 if (this->idx != that->idx)
06145 return false;
06146 if (!this->osExpressionTree->IsEqual(that->osExpressionTree))
06147 return false;
06148
06149 return true;
06150 }
06151 }
06152 }
06153
06154