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