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