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