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 if( instanceData->quadraticCoefficients->numberOfQuadraticTerms == 0 )
03209 return true;
03210 delete instanceData->quadraticCoefficients;
03211 instanceData->quadraticCoefficients = new QuadraticCoefficients();
03212 return true;
03213 }
03214 if( ((end - begin + 1) != number) ||
03215 (rowIndexes == 0) ||
03216 (varOneIndexes == 0) ||
03217 (varTwoIndexes == 0) ||
03218 (coefficients == 0) ) return false;
03219 if (instanceData->quadraticCoefficients == NULL)
03220 instanceData->quadraticCoefficients = new QuadraticCoefficients();
03221
03222 instanceData->quadraticCoefficients->numberOfQuadraticTerms = number;
03223 int i = 0;
03224
03225 if (instanceData->quadraticCoefficients->qTerm != NULL)
03226 delete [] instanceData->quadraticCoefficients->qTerm;
03227
03228 instanceData->quadraticCoefficients->qTerm = new QuadraticTerm*[number];
03229 for(i = 0; i < number; i++) instanceData->quadraticCoefficients->qTerm[i] = new QuadraticTerm();
03230 int k = 0;
03231 for(i = begin; i <= end; i++)
03232 {
03233 instanceData->quadraticCoefficients->qTerm[k]->idx = rowIndexes[i];
03234 instanceData->quadraticCoefficients->qTerm[k]->idxOne = varOneIndexes[i];
03235 instanceData->quadraticCoefficients->qTerm[k]->idxTwo = varTwoIndexes[i];
03236 instanceData->quadraticCoefficients->qTerm[k]->coef = coefficients[i];
03237 k++;
03238 }
03239 return true;
03240 }
03241
03242 bool OSInstance::setQuadraticTermsInNonlinearExpressions(int numQPTerms, int* rowIndexes, int* varOneIndexes, int* varTwoIndexes, double* coefficients)
03243 {
03244 instanceData->nonlinearExpressions->numberOfNonlinearExpressions = numQPTerms;
03245 instanceData->nonlinearExpressions->nl = new Nl*[ numQPTerms ];
03246
03247 OSnLNode *nlNodePoint;
03248 OSnLNodeVariable *nlNodeVariablePoint;
03249 std::vector<OSnLNode*> nlNodeVec;
03250
03251
03252 int i;
03253 for(i = 0; i < numQPTerms; i++)
03254 {
03255 instanceData->nonlinearExpressions->nl[ i] = new Nl();
03256 instanceData->nonlinearExpressions->nl[ i]->idx = rowIndexes[ i];
03257 instanceData->nonlinearExpressions->nl[ i]->osExpressionTree = new OSExpressionTree();
03258
03259 nlNodeVariablePoint = new OSnLNodeVariable();
03260 nlNodeVariablePoint->idx = varOneIndexes[ i];
03261
03262 nlNodeVariablePoint->coef = coefficients[ i];
03263 nlNodeVec.push_back( nlNodeVariablePoint);
03264
03265 nlNodeVariablePoint = new OSnLNodeVariable();
03266 nlNodeVariablePoint->idx = varTwoIndexes[ i];
03267 nlNodeVec.push_back( nlNodeVariablePoint);
03268
03269 nlNodePoint = new OSnLNodeTimes();
03270 nlNodeVec.push_back( nlNodePoint);
03271
03272
03273 instanceData->nonlinearExpressions->nl[ i]->osExpressionTree->m_treeRoot =
03274 nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
03275 nlNodeVec.clear();
03276 }
03277 return true;
03278 }
03279
03280 bool OSInstance::setNonlinearExpressions(int nexpr, Nl** root)
03281 {
03282 if(nexpr < 0) return false;
03283
03284 if(nexpr == 0)
03285 {
03286 instanceData->nonlinearExpressions = 0;
03287 return true;
03288 }
03289
03290
03291 instanceData->nonlinearExpressions->numberOfNonlinearExpressions = nexpr;
03292 instanceData->nonlinearExpressions->nl = new Nl*[nexpr];
03293
03294 for (int i=0; i < nexpr; i++)
03295 {
03296 instanceData->nonlinearExpressions->nl[i] = new Nl();
03297 instanceData->nonlinearExpressions->nl[i]->idx = root[i]->idx;
03298 instanceData->nonlinearExpressions->nl[i]->osExpressionTree = new OSExpressionTree();
03299 instanceData->nonlinearExpressions->nl[i]->osExpressionTree->m_treeRoot = root[i]->osExpressionTree->m_treeRoot->copyNodeAndDescendants();
03300 }
03301 return true;
03302 }
03303
03304
03305 bool OSInstance::initializeNonLinearStructures( )
03306 {
03307 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
03308 if( m_bNonLinearStructuresInitialized == true) return true;
03309 if( m_bProcessVariables == false) processVariables();
03310 if( m_bProcessObjectives == false) processObjectives();
03311 if( m_bProcessConstraints == false) processConstraints();
03312 m_iVariableNumber = instanceData->variables->numberOfVariables;
03313 m_iConstraintNumber = instanceData->constraints->numberOfConstraints;
03314 m_iObjectiveNumber = instanceData->objectives->numberOfObjectives;
03315
03316 if( m_bProcessExpressionTrees == false) getAllNonlinearExpressionTrees();
03317
03318 if( m_bDuplicateExpressionTreesMap == false) duplicateExpressionTreesMap();
03319
03320 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
03321 {
03322 (posMapExpTree->second)->getVariableIndicesMap() ;
03323 }
03324
03325 if(getNumberOfQuadraticTerms() > 0) addQTermsToExpressionTree();
03326
03327 getAllNonlinearVariablesIndexMap( );
03328 getDenseObjectiveCoefficients();
03329 m_mdConstraintFunctionValues = new double[ this->instanceData->constraints->numberOfConstraints];
03330 m_mdObjectiveFunctionValues = new double[ this->instanceData->objectives->numberOfObjectives];
03331
03332 m_bNonLinearStructuresInitialized = true;
03333 m_bProcessVariables = true;
03334 m_bProcessObjectives = true;
03335 m_bProcessConstraints = true;
03336 m_bProcessExpressionTrees = true;
03337 m_bDuplicateExpressionTreesMap = true;
03338 return true;
03339 }
03340
03341 SparseJacobianMatrix *OSInstance::getJacobianSparsityPattern( )
03342 {
03343
03344
03345
03346
03347
03348
03349 if( m_bSparseJacobianCalculated == true) return m_sparseJacMatrix;
03350
03351
03352 getLinearConstraintCoefficientMajor();
03353
03354 if( m_bNonLinearStructuresInitialized == false) initializeNonLinearStructures( );
03355 try
03356 {
03357 if( m_bColumnMajor == true)
03358 {
03359 if( getSparseJacobianFromColumnMajor( ) == false) throw ErrorClass("An error occurred in getSpareJacobianFromColumnMajor");
03360 }
03361 else
03362 {
03363 if( getSparseJacobianFromRowMajor( ) == false) throw ErrorClass("An error occurred in getSpareJacobianFromRowMajor");
03364 }
03365 }
03366 catch(const ErrorClass& eclass)
03367 {
03368 throw ErrorClass( eclass.errormsg);
03369 }
03370
03371 m_sparseJacMatrix = new SparseJacobianMatrix();
03372
03373
03374 m_sparseJacMatrix->bDeleteArrays = false;
03375 m_sparseJacMatrix->valueSize = m_iJacValueSize;
03376 m_sparseJacMatrix->starts = m_miJacStart;
03377 m_sparseJacMatrix->conVals = m_miJacNumConTerms;
03378 m_sparseJacMatrix->indexes = m_miJacIndex;
03379 m_sparseJacMatrix->values = m_mdJacValue;
03380 m_bSparseJacobianCalculated = true;
03381 return m_sparseJacMatrix;
03382 }
03383
03384 bool OSInstance::addQTermsToExressionTree()
03385 {
03386 return addQTermsToExpressionTree();
03387 }
03388
03389 bool OSInstance::addQTermsToExpressionTree()
03390 {
03391 std::ostringstream outStr;
03392 int i, k, idx;
03393
03394 int numQTerms = instanceData->quadraticCoefficients->numberOfQuadraticTerms;
03395 if(numQTerms <= 0 || m_bQTermsAdded == true) return true;
03396 OSnLNodeVariable* nlNodeVariableOne;
03397 OSnLNodeVariable* nlNodeVariableTwo;
03398 OSnLNodeTimes* nlNodeTimes;
03399 OSnLNodePlus* nlNodePlus;
03400 OSExpressionTree* expTree;
03401 getQuadraticTerms();
03402
03403 for(i = 0; i < numQTerms; i++)
03404 {
03405 idx = m_quadraticTerms->rowIndexes[ i];
03406
03407
03408 if( m_mapExpressionTreesMod.find( idx) != m_mapExpressionTreesMod.end() )
03409 {
03410
03411
03412
03413 nlNodeVariableOne = new OSnLNodeVariable();
03414 nlNodeVariableOne->idx = m_quadraticTerms->varOneIndexes[ i];
03415
03416
03417 expTree = m_mapExpressionTreesMod[ idx];
03418 if( expTree->m_bIndexMapGenerated == false) expTree->getVariableIndicesMap();
03419 if( (*expTree->mapVarIdx).find( nlNodeVariableOne->idx) == (*expTree->mapVarIdx).end() )
03420 {
03421
03422 k = (*expTree->mapVarIdx).size();
03423 (*expTree->mapVarIdx)[ nlNodeVariableOne->idx] = k + 1;
03424 #ifndef NDEBUG
03425 outStr.str("");
03426 outStr.clear();
03427 outStr << "ADDED THE FOLLOWING VARIABLE TO THE MAP" << nlNodeVariableOne->idx << std::endl;
03428 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
03429 #endif
03430 }
03431 nlNodeVariableOne->coef = m_quadraticTerms->coefficients[ i];
03432 nlNodeVariableTwo = new OSnLNodeVariable();
03433 nlNodeVariableTwo->idx = m_quadraticTerms->varTwoIndexes[ i];
03434
03435
03436 if( (*expTree->mapVarIdx).find( nlNodeVariableTwo->idx) == (*expTree->mapVarIdx).end() )
03437 {
03438
03439 k = (*expTree->mapVarIdx).size();
03440 (*expTree->mapVarIdx)[ nlNodeVariableTwo->idx] = k + 1;
03441 #ifndef NDEBUG
03442 outStr.str("");
03443 outStr.clear();
03444 outStr << "ADDED THE FOLLOWING VARIABLE TO THE MAP" << nlNodeVariableTwo->idx << std::endl;
03445 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
03446 #endif
03447 }
03448 nlNodeVariableTwo->coef = 1.;
03449
03450 nlNodeTimes = new OSnLNodeTimes();
03451 nlNodeTimes->m_mChildren[ 0] = nlNodeVariableOne;
03452 nlNodeTimes->m_mChildren[ 1] = nlNodeVariableTwo;
03453
03454 nlNodePlus = new OSnLNodePlus();
03455 nlNodePlus->m_mChildren[ 0] = expTree->m_treeRoot;
03456 nlNodePlus->m_mChildren[ 1] = nlNodeTimes;
03457
03458 expTree->m_treeRoot = nlNodePlus ;
03459
03460 if(expTree->m_bIndexMapGenerated == true)
03461 {
03462 delete expTree->mapVarIdx;
03463 expTree->mapVarIdx = NULL;
03464 expTree->m_bIndexMapGenerated = false;
03465 }
03466
03467
03468
03469 }
03470 else
03471 {
03472
03473 nlNodeVariableOne = new OSnLNodeVariable();
03474 nlNodeVariableOne->idx = m_quadraticTerms->varOneIndexes[ i];
03475 nlNodeVariableOne->coef = m_quadraticTerms->coefficients[ i];
03476 nlNodeVariableTwo = new OSnLNodeVariable();
03477 nlNodeVariableTwo->idx = m_quadraticTerms->varTwoIndexes[ i];
03478 nlNodeVariableTwo->coef = 1.;
03479
03480 nlNodeTimes = new OSnLNodeTimes();
03481 nlNodeTimes->m_mChildren[ 0] = nlNodeVariableOne;
03482 nlNodeTimes->m_mChildren[ 1] = nlNodeVariableTwo;
03483
03484 expTree = new OSExpressionTree();
03485 expTree->m_treeRoot = nlNodeTimes ;
03486 expTree->mapVarIdx = expTree->getVariableIndicesMap();
03487 m_mapExpressionTreesMod[ idx ] = expTree;
03488 if(idx < 0)
03489 {
03490 m_iObjectiveNumberNonlinear++;
03491 m_bProcessExpressionTrees = true;
03492 }
03493 else
03494 {
03495 m_iConstraintNumberNonlinear++;
03496 m_bProcessExpressionTrees = true;
03497 }
03498 #ifndef NDEBUG
03499 outStr.str("");
03500 outStr.clear();
03501 outStr << "NUMBER OF EXPRESSION TREES = " << m_mapExpressionTreesMod.size() <<std::endl;
03502 outStr << "NUMBER OF NONLINEAR OBJECTIVES = " << getNumberOfNonlinearObjectives() <<std::endl;
03503 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
03504 #endif
03505 }
03506
03507 if(m_iNonlinearExpressionNumber <= 0) m_mapExpressionTrees = m_mapExpressionTreesMod;
03508 m_bQTermsAdded =true;
03509 }
03510 return true;
03511 }
03512
03513
03514 std::string OSInstance::printModel( )
03515 {
03516 std::string resultString = "";
03517 ostringstream outStr;
03518 outStr << "";
03519 int numCon;
03520 int numObj;
03521 int numVar;
03522 int i;
03523 numCon = this->getConstraintNumber();
03524 numObj = this->getObjectiveNumber();
03525 numVar = this->getVariableNumber();
03526 this->initForAlgDiff( );
03527 outStr << std::endl;
03528 std::string *objMaxOrMin = getObjectiveMaxOrMins();
03529 outStr << "Objectives:" << std::endl;
03530 for(i = 0; i < numObj; i++)
03531 {
03532 outStr << objMaxOrMin[i] << ' ';
03533 outStr << this->printModel(-i-1);
03534 }
03535 outStr << std::endl;
03536 outStr << "Constraints:" << std::endl;
03537 for(i = 0; i < numCon; i++)
03538 {
03539 outStr << this->printModel( i);
03540 }
03541
03542 outStr << std::endl;
03543 outStr << "Variables:" << std::endl;
03544 if(m_bProcessVariables != true || bVariablesModified == true ) this->processVariables();
03545 for(i = 0; i < numVar; i++)
03546 {
03547 if(this->instanceData->variables->numberOfVariables > 0 && this->instanceData->variables->var[ i ]->name.size() > 0)
03548 {
03549 outStr << this->instanceData->variables->var[ i ]->name << ": ";
03550 }
03551
03552 {
03553 outStr << "x_";
03554 outStr << i;
03555 }
03556
03557
03558
03559
03560 outStr << " Type = " ;
03561 outStr << m_mcVariableTypes[i];
03562 outStr << " Lower Bound = ";
03563 outStr << os_dtoa_format( m_mdVariableLowerBounds[i]) ;
03564 outStr << " Upper Bound = ";
03565 outStr << os_dtoa_format( m_mdVariableUpperBounds[i]) ;
03566 outStr << std::endl;
03567 }
03568
03569
03570
03571
03572
03573
03574
03575
03576
03577 return outStr.str() ;
03578 }
03579
03580
03581 std::string OSInstance::printModel(int rowIdx )
03582 {
03583 std::string resultString = "";
03584 ostringstream outStr;
03585 outStr << "";
03586
03587 int j;
03588 int row_nonz = 0;
03589 int obj_nonz = 0;
03590 int varIdx = 0;
03591 bool addedLinearTerm = false;
03592
03593
03594 this->initForAlgDiff( );
03595
03596 if( rowIdx >= 0)
03597 {
03598 if (rowIdx < this->getConstraintNumber())
03599 {
03600 outStr << this->getConstraintNames()[ rowIdx] ;
03601 outStr << " ";
03602 if( m_bProcessConstraints != true || bConstraintsModified == true) this->processConstraints() ;
03603 if( m_mdConstraintLowerBounds[ rowIdx] > -OSDBL_MAX)
03604 {
03605 if(m_mdConstraintLowerBounds[ rowIdx] < m_mdConstraintUpperBounds[ rowIdx])
03606 {
03607 outStr << os_dtoa_format( m_mdConstraintLowerBounds[ rowIdx] );
03608 outStr << " <= ";
03609 }
03610 }
03611
03612 if(this->instanceData->linearConstraintCoefficients != NULL && this->instanceData->linearConstraintCoefficients->numberOfValues > 0)
03613 {
03614 if(m_linearConstraintCoefficientsInRowMajor == NULL)
03615 m_linearConstraintCoefficientsInRowMajor = this->getLinearConstraintCoefficientsInRowMajor();
03616 row_nonz = m_linearConstraintCoefficientsInRowMajor->starts[ rowIdx + 1] - m_linearConstraintCoefficientsInRowMajor->starts[ rowIdx];
03617
03618 for(j = 0; j < row_nonz; j++)
03619 {
03620 varIdx = m_linearConstraintCoefficientsInRowMajor->indexes[ m_linearConstraintCoefficientsInRowMajor->starts[ rowIdx] + j];
03621
03622 if(m_bSparseJacobianCalculated == false || (m_mapExpressionTreesMod.find( rowIdx) == m_mapExpressionTreesMod.end() ) ||
03623 ( (*m_mapExpressionTreesMod[ rowIdx]->mapVarIdx).find( varIdx) == (*m_mapExpressionTreesMod[ rowIdx]->mapVarIdx).end()) )
03624 {
03625 outStr << os_dtoa_format( m_linearConstraintCoefficientsInRowMajor->values[ m_linearConstraintCoefficientsInRowMajor->starts[ rowIdx] + j] );
03626 outStr << "*";
03627
03628
03629
03630
03631
03632
03633 {
03634 outStr << "x_";
03635 outStr << varIdx;
03636 }
03637
03638
03639 if( j < row_nonz - 1) outStr << " + ";
03640 addedLinearTerm = true;
03641 }
03642 }
03643 }
03644 }
03645 else
03646 return "row index not found; print command ignored\n";
03647 }
03648 else
03649 {
03650 if(m_bProcessObjectives != true || bObjectivesModified == true) this->processObjectives() ;
03651 int obj_idx = -rowIdx - 1;
03652 if (obj_idx < this->getObjectiveNumber())
03653 {
03654 obj_nonz = m_miNumberOfObjCoef[ obj_idx];
03655 for(j = 0; j < obj_nonz; j++)
03656 {
03657 outStr << os_dtoa_format( m_mObjectiveCoefficients[obj_idx]->values[j] );
03658 outStr << "*";
03659
03660
03661
03662
03663
03664 {
03665 outStr << "x_";
03666 outStr << m_mObjectiveCoefficients[obj_idx]->indexes[j] ;
03667 }
03668 if( j < obj_nonz - 1) outStr << " + ";
03669 }
03670 }
03671 else
03672 return "row index not found; print command ignored\n";
03673 }
03674 if( this->getNonlinearExpressionTree( rowIdx) != NULL)
03675 {
03676 if( (addedLinearTerm == true) || (obj_nonz > 0) ) outStr << " + " ;
03677 outStr << getNonlinearExpressionTreeInInfix( rowIdx);
03678
03679 }
03680
03681 if( rowIdx >= 0)
03682 {
03683 if( m_bProcessConstraints != true ) this->processConstraints() ;
03684 if( m_mdConstraintUpperBounds[ rowIdx] < OSDBL_MAX)
03685 {
03686 if(m_mdConstraintLowerBounds[ rowIdx] < m_mdConstraintUpperBounds[ rowIdx])
03687 {
03688 outStr << " <= ";
03689 }
03690 else
03691 {
03692 outStr << " = ";
03693 }
03694 outStr << os_dtoa_format( m_mdConstraintUpperBounds[ rowIdx] );
03695 }
03696 }
03697 outStr << std::endl;
03698 resultString = outStr.str();
03699 return resultString;
03700 }
03701
03702
03703 double OSInstance::calculateFunctionValue(int idx, double *x, bool new_x)
03704 {
03705 try
03706 {
03707 int i, j;
03708 double dvalue = 0;
03709 if( m_binitForAlgDiff == false) initForAlgDiff();
03710 if( m_bSparseJacobianCalculated == false) getJacobianSparsityPattern();
03711 if(idx >= 0)
03712 {
03713
03714 if( getConstraintNumber() <= idx ) throw
03715 ErrorClass("constraint index not valid in OSInstance::calculateFunctionValue");
03716 if( new_x == false) return *(m_mdConstraintFunctionValues + idx);
03717
03718 if( m_mapExpressionTreesMod.find( idx) != m_mapExpressionTreesMod.end() )
03719 {
03720 dvalue = m_mapExpressionTreesMod[ idx]->calculateFunction( x, new_x);
03721
03722 }
03723
03724
03725 i = m_sparseJacMatrix->starts[ idx];
03726 j = m_sparseJacMatrix->starts[ idx + 1 ];
03727
03728 while ( (i - m_sparseJacMatrix->starts[ idx]) < m_sparseJacMatrix->conVals[ idx] )
03729 {
03730
03731
03732 dvalue += m_sparseJacMatrix->values[ i]*x[ m_sparseJacMatrix->indexes[ i] ];
03733 i++;
03734 }
03735
03736 dvalue += m_mdConstraintConstants[ idx ];
03737 return dvalue;
03738 }
03739 else
03740 {
03741
03742 if( getObjectiveNumber() <= ( abs( idx) - 1) ) throw
03743 ErrorClass("objective function index not valid in OSInstance::calculateFunctionValue");
03744 if( new_x == false) return *(m_mdObjectiveFunctionValues + ( abs( idx) - 1));
03745
03746 if( m_mapExpressionTreesMod.find( idx) != m_mapExpressionTreesMod.end() )
03747 {
03748 dvalue = m_mapExpressionTreesMod[ idx]->calculateFunction( x, new_x);
03749 }
03750
03751 SparseVector **objCoef = getObjectiveCoefficients();
03752 SparseVector *obj = objCoef[ abs( idx) - 1];
03753 for(i = 0; i < obj->number; i++)
03754 {
03755 dvalue += x[ obj->indexes[i]]*(obj->values[ i]);
03756 }
03757
03758 dvalue += m_mdObjectiveConstants[ abs( idx) - 1 ];
03759
03760 *(m_mdObjectiveFunctionValues + ( abs( idx) - 1)) = dvalue;
03761 return *(m_mdObjectiveFunctionValues + ( abs( idx) - 1));
03762 }
03763 }
03764
03765 catch(const ErrorClass& eclass)
03766 {
03767 throw ErrorClass( eclass.errormsg);
03768 }
03769 }
03770
03771
03772 double *OSInstance::calculateAllConstraintFunctionValues( double* x, double *objLambda, double *conLambda,
03773 bool new_x, int highestOrder)
03774 {
03775 try
03776 {
03777 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated) )
03778 getIterateResults(x, objLambda, conLambda, new_x, highestOrder);
03779 }
03780 catch(const ErrorClass& eclass)
03781 {
03782 throw ErrorClass( eclass.errormsg);
03783 }
03784 return m_mdConstraintFunctionValues;
03785 }
03786
03787
03788 double *OSInstance::calculateAllConstraintFunctionValues(double* x, bool new_x)
03789 {
03790 try
03791 {
03792 m_iHighestOrderEvaluated = -1;
03793 if( new_x == false) return m_mdConstraintFunctionValues;
03794 int idx, numConstraints;
03795 numConstraints = getConstraintNumber();
03796
03797 for(idx = 0; idx < numConstraints; idx++)
03798 {
03799 m_mdConstraintFunctionValues[ idx] = calculateFunctionValue(idx, x, new_x);
03800 }
03801
03802 }
03803 catch(const ErrorClass& eclass)
03804 {
03805 throw ErrorClass( eclass.errormsg);
03806 }
03807 return m_mdConstraintFunctionValues;
03808 }
03809
03810
03811 double *OSInstance::calculateAllObjectiveFunctionValues( double* x, double *objLambda, double *conLambda,
03812 bool new_x, int highestOrder)
03813 {
03814 try
03815 {
03816 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated) )
03817 getIterateResults(x, objLambda, conLambda, new_x, highestOrder);
03818 }
03819 catch(const ErrorClass& eclass)
03820 {
03821 throw ErrorClass( eclass.errormsg);
03822 }
03823 return m_mdObjectiveFunctionValues;
03824 }
03825
03826
03827 double *OSInstance::calculateAllObjectiveFunctionValues( double* x, bool new_x)
03828 {
03829 try
03830 {
03831 m_iHighestOrderEvaluated = -1;
03832
03833 if( new_x == false) return m_mdObjectiveFunctionValues;
03834 int idx, numObjectives;
03835 numObjectives = getObjectiveNumber();
03836
03837 for(idx = 0; idx < numObjectives; idx++)
03838 {
03839 m_mdObjectiveFunctionValues[ idx] = calculateFunctionValue(-idx -1, x, new_x);
03840 }
03841 }
03842 catch(const ErrorClass& eclass)
03843 {
03844 throw ErrorClass( eclass.errormsg);
03845 }
03846 return m_mdObjectiveFunctionValues;
03847 }
03848
03849
03850 SparseJacobianMatrix *OSInstance::calculateAllConstraintFunctionGradients(double* x, double *objLambda, double *conLambda,
03851 bool new_x, int highestOrder)
03852 {
03853 try
03854
03855 {
03856 if(highestOrder < 1 ) throw ErrorClass("When calling calculateAllConstraintFunctionGradients highestOrder should be 1 or 2");
03857 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated) )
03858 getIterateResults(x, objLambda, conLambda, new_x, highestOrder);
03859 }
03860 catch(const ErrorClass& eclass)
03861 {
03862 throw ErrorClass( eclass.errormsg);
03863 }
03864 return m_sparseJacMatrix;
03865 }
03866
03867
03868
03869 SparseVector *OSInstance::calculateConstraintFunctionGradient(double* x, double *objLambda, double *conLambda,
03870 int idx, bool new_x, int highestOrder)
03871 {
03872 try
03873 {
03874 if(highestOrder < 1 ) throw ErrorClass("When calling calculateConstraintFunctionGradient highestOrder should be 1 or 2");
03875 if(idx < 0 || idx >= instanceData->constraints->numberOfConstraints )
03876 throw ErrorClass("invalid index passed to calculateConstraintFunctionGrad");
03877 SparseVector *sp;
03878 sp = new SparseVector();
03879 sp->bDeleteArrays = true;
03880 int i;
03881 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated) )
03882 getIterateResults(x, objLambda, conLambda, new_x, highestOrder);
03883 sp->number = m_miJacStart[ idx + 1] - m_miJacStart[ idx];
03884 sp->values = new double[ sp->number];
03885 sp->indexes = new int[ sp->number];
03886 for(i = 0; i < sp->number; i++)
03887 {
03888 sp->values[ i] = m_mdJacValue[ m_miJacStart[ idx] + i];
03889 sp->indexes[ i] = m_miJacIndex[ m_miJacStart[ idx] + i];
03890 }
03891 return sp;
03892 }
03893 catch(const ErrorClass& eclass)
03894 {
03895 throw ErrorClass( eclass.errormsg);
03896 }
03897 }
03898
03899
03900 SparseVector *OSInstance::calculateConstraintFunctionGradient(double* x, int idx, bool new_x)
03901 {
03902 try
03903 {
03904 if(idx < 0 || idx >= instanceData->constraints->numberOfConstraints )
03905 throw ErrorClass("invalid index passed to calculateConstraintFunctionGrad");
03906 SparseVector *sp;
03907 sp = new SparseVector();
03908 sp->bDeleteArrays = true;
03909 int i;
03910 if( new_x == true || (1 > m_iHighestOrderEvaluated) )
03911 getIterateResults(x, NULL, NULL, new_x, 1);
03912 sp->number = m_miJacStart[ idx + 1] - m_miJacStart[ idx];
03913 sp->values = new double[ sp->number];
03914 sp->indexes = new int[ sp->number];
03915 for(i = 0; i < sp->number; i++)
03916 {
03917 sp->values[ i] = m_mdJacValue[ m_miJacStart[ idx] + i];
03918 sp->indexes[i] = m_miJacIndex[ m_miJacStart[ idx] + i];
03919 }
03920 return sp;
03921 }
03922 catch(const ErrorClass& eclass)
03923 {
03924 throw ErrorClass( eclass.errormsg);
03925 }
03926 }
03927
03928
03929 double **OSInstance::calculateAllObjectiveFunctionGradients(double* x, double *objLambda, double *conLambda,
03930 bool new_x, int highestOrder)
03931 {
03932 try
03933 {
03934 if(highestOrder < 1 ) throw ErrorClass("When calling calculateAllObjectiveFunctionGradients highestOrder should be 1 or 2");
03935 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated) )
03936 {
03937 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
03938 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
03939 {
03940 if(posMapExpTree->first < 0)
03941 {
03942 m_mmdObjGradient[ abs( posMapExpTree->first) - 1 ] = calculateObjectiveFunctionGradient(x, objLambda, conLambda,
03943 posMapExpTree->first, new_x, highestOrder);
03944 }
03945 }
03946 }
03947 }
03948 catch(const ErrorClass& eclass)
03949 {
03950 throw ErrorClass( eclass.errormsg);
03951 }
03952 return m_mmdObjGradient;
03953 }
03954
03955 double *OSInstance::calculateObjectiveFunctionGradient(double* x, double *objLambda, double *conLambda,
03956 int objIdx, bool new_x, int highestOrder)
03957 {
03958
03959
03960
03961 try
03962 {
03963 if(highestOrder < 1 ) throw ErrorClass("When calling calculateObjectiveFunctionGradient highestOrder should be 1 or 2");
03964 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated) )
03965 {
03966 int domainIdx = 0;
03967 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
03968 std::map<int, int>::iterator posVarIndexMap;
03969 int iHighestOrderEvaluatedStore;
03970 unsigned int i;
03971 iHighestOrderEvaluatedStore = m_iHighestOrderEvaluated;
03972 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
03973 {
03974
03975 if(posMapExpTree->first == objIdx)
03976 {
03977 if( new_x == true )
03978 {
03979 if( m_vdX.size() > 0) m_vdX.clear();
03980 for(posVarIndexMap = m_mapAllNonlinearVariablesIndex.begin(); posVarIndexMap != m_mapAllNonlinearVariablesIndex.end(); ++posVarIndexMap)
03981 {
03982 m_vdX.push_back( x[ posVarIndexMap->first]) ;
03983 }
03984 if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true ) && (m_mapExpressionTreesMod.size() > 0) )
03985 {
03986 createOSADFun( m_vdX);
03987 }
03988 }
03989
03990 if(( new_x == true ) || (m_iHighestOrderEvaluated < 0) )this->forwardAD(0, m_vdX);
03991
03992 if(( new_x == true ) || (m_iHighestOrderEvaluated < 1) )
03993 {
03994 m_vdRangeUnitVec[ domainIdx] = 1.;
03995 m_vdYjacval = this->reverseAD(1, m_vdRangeUnitVec);
03996 for(i = 0; i < m_iNumberOfNonlinearVariables; i++)
03997 {
03998 m_mmdObjGradient[ abs( objIdx) - 1 ][ m_miNonLinearVarsReverseMap[ i]] = m_vdYjacval[ i] +
03999 m_mmdDenseObjectiveCoefficients[ abs( objIdx) - 1][ m_miNonLinearVarsReverseMap[ i]];
04000 }
04001 }
04002 m_iHighestOrderEvaluated = iHighestOrderEvaluatedStore;
04003 m_vdRangeUnitVec[ domainIdx] = 0.;
04004
04005 break;
04006 }
04007 domainIdx++;
04008 }
04009 }
04010 }
04011 catch(const ErrorClass& eclass)
04012 {
04013 throw ErrorClass( eclass.errormsg);
04014 }
04015 return m_mmdObjGradient[abs( objIdx) - 1];
04016 }
04017
04018
04019
04020 double *OSInstance::calculateObjectiveFunctionGradient(double* x, int objIdx, bool new_x)
04021 {
04022 try
04023 {
04024 int domainIdx = 0;
04025 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
04026 std::map<int, int>::iterator posVarIndexMap;
04027 unsigned int i;
04028 int iHighestOrderEvaluatedStore;
04029 iHighestOrderEvaluatedStore = m_iHighestOrderEvaluated;
04030 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
04031 {
04032 if(posMapExpTree->first == objIdx)
04033 {
04034 if( new_x == true )
04035 {
04036 if( m_vdX.size() > 0) m_vdX.clear();
04037 for(posVarIndexMap = m_mapAllNonlinearVariablesIndex.begin(); posVarIndexMap != m_mapAllNonlinearVariablesIndex.end(); ++posVarIndexMap)
04038 {
04039 m_vdX.push_back( x[ posVarIndexMap->first]) ;
04040 }
04041 if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true ) && (m_mapExpressionTreesMod.size() > 0) )
04042 {
04043 createOSADFun( m_vdX);
04044 }
04045 }
04046
04047 if(( new_x == true ) || (m_iHighestOrderEvaluated < 0) )this->forwardAD(0, m_vdX);
04048
04049 if(( new_x == true ) || (m_iHighestOrderEvaluated < 1) )
04050 {
04051 m_vdRangeUnitVec[ domainIdx] = 1.;
04052 m_vdYjacval = this->reverseAD(1, m_vdRangeUnitVec);
04053 for(i = 0; i < m_iNumberOfNonlinearVariables; i++)
04054 {
04055 m_mmdObjGradient[ abs( objIdx) - 1 ][ m_miNonLinearVarsReverseMap[ i]] = m_vdYjacval[ i] +
04056 m_mmdDenseObjectiveCoefficients[ abs( objIdx) - 1][ m_miNonLinearVarsReverseMap[ i]];
04057 }
04058 }
04059 m_iHighestOrderEvaluated = iHighestOrderEvaluatedStore;
04060 m_vdRangeUnitVec[ domainIdx] = 0.;
04061
04062 break;
04063 }
04064 domainIdx++;
04065 }
04066 }
04067 catch(const ErrorClass& eclass)
04068 {
04069 throw ErrorClass( eclass.errormsg);
04070 }
04071 return m_mmdObjGradient[abs( objIdx) - 1];
04072 }
04073
04074 SparseHessianMatrix *OSInstance::calculateLagrangianHessian( double* x, double *objLambda, double *conLambda,
04075 bool new_x, int highestOrder)
04076 {
04077 try
04078 {
04079 if(highestOrder != 2 ) throw ErrorClass("When calling calculateLagrangianHessian highestOrder should be 2");
04080 if( new_x == true || (highestOrder > m_iHighestOrderEvaluated) )
04081 {
04082
04083 getIterateResults(x, objLambda, conLambda, new_x, highestOrder);
04084 }
04085 }
04086 catch(const ErrorClass& eclass)
04087 {
04088 throw ErrorClass( eclass.errormsg);
04089 }
04090 return m_LagrangianSparseHessian;
04091 }
04092
04093 SparseHessianMatrix *OSInstance::calculateHessian(double* x, int idx, bool new_x)
04094 {
04095 try
04096 {
04097 if(idx < -instanceData->objectives->numberOfObjectives || idx >= instanceData->constraints->numberOfConstraints )
04098 throw ErrorClass("invalid index passed to calculateHessian");
04099 double *objLambda = new double[ getObjectiveNumber() ];
04100 double *conLambda = new double[ getConstraintNumber() ];
04101
04102
04103 int i;
04104
04105 for(i = 0; i < getObjectiveNumber(); i++)
04106 {
04107 objLambda[ i] = 0.0;
04108 }
04109 for(i = 0; i < getConstraintNumber(); i++)
04110 {
04111 conLambda[ i] = 0.0;
04112 }
04113
04114
04115 if(idx < 0)
04116 {
04117 objLambda[ abs(idx) - 1] = 1.0;
04118 }
04119 else
04120 {
04121 conLambda[ idx] = 1.0;
04122 }
04123
04124 if( new_x == true || (2 > m_iHighestOrderEvaluated) )
04125 {
04126 getIterateResults(x, objLambda, conLambda, new_x, 2);
04127
04128 }
04129 delete[] objLambda;
04130 delete[] conLambda;
04131 }
04132 catch(const ErrorClass& eclass)
04133 {
04134 throw ErrorClass( eclass.errormsg);
04135 }
04136 return m_LagrangianSparseHessian;
04137 }
04138
04139
04140
04141 bool OSInstance::getSparseJacobianFromColumnMajor( )
04142 {
04143 std::ostringstream outStr;
04144
04145
04146 if( m_bColumnMajor == false) return false;
04147 int iNumRowStarts = getConstraintNumber() + 1;
04148 int i,j, iTemp;
04149 int iNumVariableStarts = getVariableNumber() ;
04150 int *start = NULL;
04151 int *index = NULL;
04152 double *value = NULL;
04153
04154 if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0)
04155 {
04156 start = this->instanceData->linearConstraintCoefficients->start->el;
04157 index = this->instanceData->linearConstraintCoefficients->rowIdx->el;
04158 value = this->instanceData->linearConstraintCoefficients->value->el;
04159 }
04160 m_miJacStart = new int[ iNumRowStarts];
04161 m_miJacNumConTerms = new int[ getConstraintNumber()];
04162 OSnLNodePlus *nlNodePlus;
04163 OSnLNodeVariable *nlNodeVariable;
04164 OSExpressionTree *expTree = NULL;
04165
04166 for ( i = 0; i < iNumRowStarts; i++)
04167 {
04168 m_miJacStart [ i ] = 0;
04169
04170 if( m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() )
04171 {
04172
04173 m_mapExpressionTreesMod[ i]->getVariableIndicesMap();
04174
04175 }
04176 }
04177
04178 if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0)
04179 {
04180
04181 for (i = 0; i < iNumVariableStarts; i++)
04182 {
04183 for (j = start[i]; j < start[ i + 1 ]; j++)
04184 {
04185
04186
04187
04188
04189
04190
04191 if( (m_mapExpressionTreesMod.find( index[ j]) != m_mapExpressionTreesMod.end() ) &&
04192 ( (*m_mapExpressionTreesMod[ index[ j]]->mapVarIdx).find( i) != (*m_mapExpressionTreesMod[ index[ j]]->mapVarIdx).end()) )
04193 {
04194
04195
04196
04197
04198 if( value[j] > 0 || value[j] < 0)
04199 {
04200 expTree = m_mapExpressionTreesMod[ index[j] ];
04201 nlNodeVariable = new OSnLNodeVariable();
04202 nlNodeVariable->coef = value[ j];
04203 nlNodeVariable->idx = i;
04204 nlNodePlus = new OSnLNodePlus();
04205 nlNodePlus->m_mChildren[ 0] = m_mapExpressionTreesMod[ index[ j] ]->m_treeRoot;
04206 nlNodePlus->m_mChildren[ 1] = nlNodeVariable;
04207
04208 expTree->m_treeRoot = nlNodePlus ;
04209
04210
04211
04212
04213 }
04214 }
04215 else
04216 {
04217 m_miJacStart[ index[j] + 1] ++;
04218 }
04219 }
04220 }
04221 }
04222
04223
04224
04225
04226 m_miJacStart[0] = 0;
04227 for (i = 1; i < iNumRowStarts; i++ )
04228 {
04229 m_miJacNumConTerms[ i - 1] = m_miJacStart[i];
04230 if( m_mapExpressionTreesMod.find( i - 1) != m_mapExpressionTreesMod.end() )
04231 {
04232 m_miJacStart[i] += (m_miJacStart[i - 1] + (*m_mapExpressionTreesMod[ i - 1]->mapVarIdx).size() );
04233 }
04234 else
04235 {
04236 m_miJacStart[i] += m_miJacStart[i - 1];
04237 }
04238 }
04239
04240 m_iJacValueSize = m_miJacStart[ iNumRowStarts - 1];
04241 m_miJacIndex = new int[ m_iJacValueSize];
04242 m_mdJacValue = new double[ m_iJacValueSize ];
04243
04244 if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0)
04245 {
04246
04247 for (i = 0; i < iNumVariableStarts; i++)
04248 {
04249
04250
04251 for (j = start[i]; j < start[ i + 1 ]; j++)
04252 {
04253
04254
04255 if( (m_mapExpressionTreesMod.find( index[ j]) == m_mapExpressionTreesMod.end() ) ||
04256 ( (*m_mapExpressionTreesMod[ index[ j]]->mapVarIdx).find( i) == (*m_mapExpressionTreesMod[ index[ j]]->mapVarIdx).end()) )
04257 {
04258 iTemp = m_miJacStart[ index[j]];
04259 m_miJacIndex[ iTemp] = i;
04260 m_mdJacValue[ iTemp] = value[j];
04261 m_miJacStart[ index[j]]++;
04262 }
04263 }
04264 }
04265 }
04266
04267 std::map<int, int>::iterator posVarIdx;
04268
04269 for (i = 0; i < iNumRowStarts - 1; i++ )
04270 {
04271 m_miJacStart[ i] = m_miJacStart [ i] - m_miJacNumConTerms[ i] ;
04272 iTemp = m_miJacStart[ i] + m_miJacNumConTerms[ i];
04273
04274 if( m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() )
04275 {
04276 for(posVarIdx = (*m_mapExpressionTreesMod[ i]->mapVarIdx).begin(); posVarIdx
04277 != (*m_mapExpressionTreesMod[ i]->mapVarIdx).end(); ++posVarIdx)
04278 {
04279 m_miJacIndex[ iTemp] = posVarIdx->first;
04280 m_mdJacValue[ iTemp] = 0;
04281 iTemp++;
04282 }
04283 }
04284 }
04285 #ifndef NDEBUG
04286 outStr.str("");
04287 outStr.clear();
04288 outStr << "HERE ARE ROW STARTS:" << std::endl;
04289 for (i = 0; i < iNumRowStarts; i++ )
04290 {
04291 outStr << m_miJacStart[ i] << " ";
04292 }
04293 outStr << std::endl << std::endl;
04294 outStr << "HERE ARE VARIABLE INDICES:" << std::endl;
04295 for (i = 0; i < m_miJacStart[ iNumRowStarts - 1]; i++ )
04296 {
04297 outStr << m_miJacIndex[ i] << " ";
04298 }
04299 outStr << std::endl << std::endl;
04300 outStr << "HERE ARE VALUES:" << std::endl;
04301 for (i = 0; i < m_miJacStart[ iNumRowStarts - 1]; i++ )
04302 {
04303 outStr << m_mdJacValue[ i] << " ";
04304 }
04305 outStr << std::endl << std::endl;
04306
04307 outStr << "HERE ARE NUMBER OF CONSTANT TERMS:" << std::endl;
04308 for (i = 0; i < iNumRowStarts - 1; i++ )
04309 {
04310 outStr << m_miJacNumConTerms[ i ] << " ";
04311 }
04312 outStr << std::endl << std::endl;
04313 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
04314 #endif
04315 return true;
04316 }
04317
04318
04319
04320 bool OSInstance::getSparseJacobianFromRowMajor( )
04321 {
04322 std::ostringstream outStr;
04323
04324
04325 if( m_bColumnMajor == true) return false;
04326 int iNumJacRowStarts = getConstraintNumber() + 1;
04327 std::map<int, int>::iterator posVarIdx;
04328 int i,j, k;
04329 int *start = NULL;
04330 int *index = NULL;
04331 double *value = NULL;
04332
04333 if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0)
04334 {
04335 start = this->instanceData->linearConstraintCoefficients->start->el;
04336 index = this->instanceData->linearConstraintCoefficients->colIdx->el;
04337 value = this->instanceData->linearConstraintCoefficients->value->el;
04338 }
04339 m_miJacStart = new int[ iNumJacRowStarts];
04340 m_miJacNumConTerms = new int[ getConstraintNumber()];
04341 OSnLNodePlus *nlNodePlus;
04342 OSnLNodeVariable *nlNodeVariable;
04343
04344
04345 for ( i = 0; i < iNumJacRowStarts; i++)
04346 {
04347 m_miJacStart [ i ] = 0;
04348
04349 if( m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() )
04350 {
04351
04352 m_mapExpressionTreesMod[ i]->getVariableIndicesMap();
04353
04354 }
04355 }
04356 int loopLimit = getConstraintNumber();
04357
04358
04359 if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0)
04360 {
04361
04362 for (i = 0; i < loopLimit; i++)
04363 {
04364 m_miJacNumConTerms[ i] = 0;
04365 for (j = start[i]; j < start[ i + 1 ]; j++)
04366 {
04367
04368
04369
04370 if( (m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() ) &&
04371 ( (*m_mapExpressionTreesMod[ i]->mapVarIdx).find( index[ j]) != (*m_mapExpressionTreesMod[ i]->mapVarIdx).end()) )
04372 {
04373
04374
04375
04376 if(value[ j] > 0 || value[j] < 0)
04377 {
04378 nlNodeVariable = new OSnLNodeVariable();
04379 nlNodeVariable->coef = value[ j];
04380 nlNodeVariable->idx = index[ j];
04381 nlNodePlus = new OSnLNodePlus();
04382 nlNodePlus->m_mChildren[ 0] = m_mapExpressionTreesMod[ i ]->m_treeRoot;
04383 nlNodePlus->m_mChildren[ 1] = nlNodeVariable;
04384
04385
04386
04387
04388 m_mapExpressionTreesMod[ i ]->m_treeRoot = nlNodePlus;
04389 }
04390 }
04391 else
04392 {
04393
04394 m_miJacNumConTerms[ i]++;
04395 }
04396 }
04397 }
04398 }
04399
04400 m_miJacStart[0] = 0;
04401 for (i = 1; i < iNumJacRowStarts; i++ )
04402 {
04403 if( m_mapExpressionTreesMod.find( i - 1) != m_mapExpressionTreesMod.end() )
04404 {
04405 m_miJacStart[i] = m_miJacStart[i - 1] + (m_miJacNumConTerms[ i - 1] + (*m_mapExpressionTreesMod[ i - 1]->mapVarIdx).size() );
04406 }
04407 else
04408 {
04409 m_miJacStart[i] = m_miJacStart[i - 1] + m_miJacNumConTerms[ i - 1];
04410 }
04411 }
04412
04413
04414 m_iJacValueSize = m_miJacStart[ iNumJacRowStarts - 1];
04415 m_miJacIndex = new int[ m_iJacValueSize];
04416 m_mdJacValue = new double[ m_iJacValueSize ];
04417
04418
04419 if(this->instanceData->linearConstraintCoefficients->numberOfValues > 0)
04420 {
04421 for (i = 0; i < loopLimit; i++)
04422 {
04423 k = 0;
04424 for (j = start[i]; j < start[ i + 1 ]; j++)
04425 {
04426 if( (m_mapExpressionTreesMod.find( i) == m_mapExpressionTreesMod.end() ) ||
04427 ( (*m_mapExpressionTreesMod[ i]->mapVarIdx).find( index[ j]) == (*m_mapExpressionTreesMod[ i]->mapVarIdx).end()) )
04428 {
04429 m_miJacIndex[ m_miJacStart[i] + k ] = index[ j];
04430 m_mdJacValue[ m_miJacStart[i] + k ] = value[ j];
04431 k++;
04432 }
04433 }
04434 }
04435 }
04436
04437 for (i = 0; i < loopLimit; i++ )
04438 {
04439 k = m_miJacStart[i] + m_miJacNumConTerms[ i ];
04440
04441 if( m_mapExpressionTreesMod.find( i) != m_mapExpressionTreesMod.end() )
04442 {
04443 for(posVarIdx = (*m_mapExpressionTreesMod[ i]->mapVarIdx).begin(); posVarIdx
04444 != (*m_mapExpressionTreesMod[ i]->mapVarIdx).end(); ++posVarIdx)
04445 {
04446 m_miJacIndex[ k] = posVarIdx->first;
04447 m_mdJacValue[ k] = 0;
04448 k++;
04449 }
04450 }
04451 }
04452 #ifndef NDEBUG
04453 outStr.str("");
04454 outStr.clear();
04455 outStr << "HERE ARE ROW STARTS:" << std::endl;
04456 for (i = 0; i < iNumJacRowStarts; i++ )
04457 {
04458 outStr << m_miJacStart[ i] << " ";
04459 }
04460 outStr << std::endl << std::endl;
04461 outStr << "HERE ARE VARIABLE INDICES:" << std::endl;
04462 for (i = 0; i < m_miJacStart[ iNumJacRowStarts - 1]; i++ )
04463 {
04464 outStr << m_miJacIndex[ i] << " ";
04465 }
04466 outStr << std::endl << std::endl;
04467 outStr << "HERE ARE VALUES:" << std::endl;
04468 for (i = 0; i < m_miJacStart[ iNumJacRowStarts - 1]; i++ )
04469 {
04470 outStr << m_mdJacValue[ i] << " ";
04471 }
04472 outStr << std::endl << std::endl;
04473
04474 outStr << "HERE ARE NUMBER OF CONSTANT TERMS:" << std::endl;
04475 for (i = 0; i < iNumJacRowStarts - 1; i++ )
04476 {
04477 outStr << m_miJacNumConTerms[ i ] << " ";
04478 }
04479 outStr << std::endl << std::endl;
04480 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
04481 #endif
04482 return true;
04483 }
04484
04485 OSExpressionTree* OSInstance::getLagrangianExpTree( )
04486 {
04487 if( m_bLagrangianExpTreeCreated == true) return m_LagrangianExpTree;
04488
04489
04490
04491 if(m_bSparseJacobianCalculated == false) getJacobianSparsityPattern( );
04492 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
04493 OSnLNodeTimes* nlNodeTimes = NULL;
04494 OSnLNodeVariable* nlNodeVariable = NULL;
04495 OSnLNodeSum* nlNodeSum = NULL;
04496 int numChildren = 0;
04497 int rowIdx;
04498
04499 nlNodeSum = new OSnLNodeSum();
04500 nlNodeSum->inumberOfChildren = m_mapExpressionTreesMod.size();
04501 nlNodeSum->m_mChildren = new OSnLNode*[ nlNodeSum->inumberOfChildren];
04502
04503 m_LagrangianExpTree = new OSExpressionTree();
04504 m_LagrangianExpTree->m_treeRoot = nlNodeSum;
04505
04506 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
04507 {
04508
04509 nlNodeVariable = new OSnLNodeVariable();
04510 nlNodeVariable->coef = 1.;
04511
04512
04513 rowIdx = posMapExpTree->first;
04514 if(rowIdx >= 0)
04515 {
04516 nlNodeVariable->idx = instanceData->variables->numberOfVariables + rowIdx;
04517 }
04518 else
04519 {
04520 nlNodeVariable->idx = instanceData->variables->numberOfVariables +
04521 instanceData->constraints->numberOfConstraints + (abs(rowIdx) - 1);
04522 }
04523
04524 nlNodeTimes = new OSnLNodeTimes();
04525 nlNodeTimes->m_mChildren[ 0] = nlNodeVariable;
04526 nlNodeTimes->m_mChildren[ 1] = m_mapExpressionTreesMod[ posMapExpTree->first ]->m_treeRoot;
04527
04528 nlNodeSum->m_mChildren[ numChildren] = nlNodeTimes;
04529 numChildren++;
04530 }
04531
04532 m_LagrangianExpTree->getVariableIndicesMap();
04533
04534
04535
04536
04537 m_bLagrangianExpTreeCreated = true;
04538 return m_LagrangianExpTree;
04539 }
04540
04541 std::map<int, int> OSInstance::getAllNonlinearVariablesIndexMap( )
04542 {
04543 std::ostringstream outStr;
04544
04545 if(m_bAllNonlinearVariablesIndex == true) return m_mapAllNonlinearVariablesIndex;
04546
04547
04548 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
04549 std::map<int, int>::iterator posVarIdx;
04550 OSExpressionTree *expTree;
04551 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
04552 {
04553
04554 expTree = posMapExpTree->second;
04555 if(expTree->m_bIndexMapGenerated == false)expTree->getVariableIndicesMap();
04556 for(posVarIdx = (*expTree->mapVarIdx).begin(); posVarIdx != (*expTree->mapVarIdx).end(); ++posVarIdx)
04557 {
04558 if( m_mapAllNonlinearVariablesIndex.find( posVarIdx->first) == m_mapAllNonlinearVariablesIndex.end() )
04559 {
04560
04561 m_mapAllNonlinearVariablesIndex[ posVarIdx->first] = 1;
04562 }
04563 }
04564 }
04565 m_miNonLinearVarsReverseMap = new int[m_mapAllNonlinearVariablesIndex.size()];
04566
04567 int kount = 0;
04568
04569 for(posVarIdx = m_mapAllNonlinearVariablesIndex.begin(); posVarIdx !=m_mapAllNonlinearVariablesIndex.end(); ++posVarIdx)
04570 {
04571 posVarIdx->second = kount;
04572 m_miNonLinearVarsReverseMap[ kount] = posVarIdx->first;
04573 kount++;
04574 #ifndef NDEBUG
04575 outStr.str("");
04576 outStr.clear();
04577 outStr << "POSITION FIRST = " << posVarIdx->first ;
04578 outStr << " POSITION SECOND = " << posVarIdx->second << std::endl;
04579 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
04580 #endif
04581 }
04582 m_iNumberOfNonlinearVariables = kount;
04583
04584 m_bAllNonlinearVariablesIndex = true;
04585 return m_mapAllNonlinearVariablesIndex;
04586 }
04587
04588 SparseHessianMatrix* OSInstance::getLagrangianHessianSparsityPattern( )
04589 {
04590 std::ostringstream outStr;
04591
04592
04593 if( m_bLagrangianSparseHessianCreated == true) return m_LagrangianSparseHessian;
04594 if( m_iNumberOfNonlinearVariables == 0) return NULL;
04595 if( m_binitForAlgDiff == false ) initForAlgDiff();
04596 unsigned int i = 0;
04597 unsigned int j;
04598 int numNonz = 0;
04599
04600
04601 std::vector<double> vx;
04602 std::map<int, int>::iterator posMap1, posMap2;
04603 if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true ) && (m_mapExpressionTreesMod.size() > 0) )
04604 {
04605 for(posMap1 = m_mapAllNonlinearVariablesIndex.begin(); posMap1 != m_mapAllNonlinearVariablesIndex.end(); ++posMap1)
04606 {
04607 vx.push_back( 1.0) ;
04608 }
04609 createOSADFun( vx);
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
04643
04644
04645 numNonz = getADSparsityHessian();
04646
04647
04648 i = 0;
04649
04650 m_LagrangianSparseHessian = new SparseHessianMatrix();
04651 m_LagrangianSparseHessian->bDeleteArrays = true;
04652 m_LagrangianSparseHessian->hessDimension = numNonz;
04653
04654 m_LagrangianSparseHessian->hessRowIdx = new int[m_LagrangianSparseHessian->hessDimension];
04655 m_LagrangianSparseHessian->hessColIdx = new int[m_LagrangianSparseHessian->hessDimension];
04656 m_LagrangianSparseHessian->hessValues = new double[m_LagrangianSparseHessian->hessDimension];
04657
04658 numNonz = 0;
04659 for(posMap1 = m_mapAllNonlinearVariablesIndex.begin(); posMap1 != m_mapAllNonlinearVariablesIndex.end(); ++posMap1)
04660 {
04661
04662 j = i;
04663 for(posMap2 = posMap1; posMap2 != m_mapAllNonlinearVariablesIndex.end(); ++posMap2)
04664 {
04665
04666 if(m_vbLagHessNonz[ i*m_iNumberOfNonlinearVariables + j] == true)
04667 {
04668 *(m_LagrangianSparseHessian->hessRowIdx + numNonz) = posMap1->first;
04669 *(m_LagrangianSparseHessian->hessColIdx + numNonz) = posMap2->first;
04670 numNonz++;
04671 }
04672
04673 j++;
04674 }
04675 i++;
04676 }
04677 #ifndef NDEBUG
04678 outStr.str("");
04679 outStr.clear();
04680 outStr << "HESSIAN SPARSITY PATTERN" << std::endl;
04681 int kj;
04682 for(kj = 0; kj < m_LagrangianSparseHessian->hessDimension; kj++)
04683 {
04684 outStr << "Row Index = " << *(m_LagrangianSparseHessian->hessRowIdx + kj) << std::endl;
04685 outStr << "Column Index = " << *(m_LagrangianSparseHessian->hessColIdx + kj) << std::endl;
04686 }
04687 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
04688 #endif
04689
04690 m_bLagrangianSparseHessianCreated = true;
04691 return m_LagrangianSparseHessian;
04692 }
04693
04694
04695 void OSInstance::duplicateExpressionTreesMap()
04696 {
04697
04698
04699 if(m_bDuplicateExpressionTreesMap == false)
04700 {
04701
04702 if( m_bProcessExpressionTrees == false) getAllNonlinearExpressionTrees();
04703 m_mapExpressionTreesMod = m_mapExpressionTrees;
04704 m_bDuplicateExpressionTreesMap = true;
04705 return;
04706 }
04707 else
04708 {
04709 return;
04710 }
04711 }
04712
04713
04714
04715 bool OSInstance::getIterateResults( double *x, double *objLambda, double* conMultipliers,
04716 bool new_x, int highestOrder)
04717 {
04718 try
04719 {
04720 if( m_binitForAlgDiff == false) initForAlgDiff();
04721 std::map<int, int>::iterator posVarIndexMap;
04722
04723 if(new_x == true)
04724 {
04725 if( m_vdX.size() > 0) m_vdX.clear();
04726 for(posVarIndexMap = m_mapAllNonlinearVariablesIndex.begin(); posVarIndexMap != m_mapAllNonlinearVariablesIndex.end(); ++posVarIndexMap)
04727 {
04728 m_vdX.push_back( x[ posVarIndexMap->first]) ;
04729 }
04730 if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true ) && (m_mapExpressionTreesMod.size() > 0) )
04731 {
04732 createOSADFun( m_vdX);
04733 }
04734 }
04735 else
04736
04737 {
04738 if( m_vdX.size() == 0)
04739 {
04740 for(posVarIndexMap = m_mapAllNonlinearVariablesIndex.begin(); posVarIndexMap != m_mapAllNonlinearVariablesIndex.end(); ++posVarIndexMap)
04741 {
04742 m_vdX.push_back( x[ posVarIndexMap->first]) ;
04743 }
04744 if( (m_bOSADFunIsCreated == false || m_bCppADMustReTape == true ) && (m_mapExpressionTreesMod.size() > 0) )
04745 {
04746 createOSADFun( m_vdX);
04747 }
04748 }
04749 }
04750 switch( highestOrder)
04751 {
04752 case 0:
04753 if(new_x == true || m_iHighestOrderEvaluated < 0)
04754 {
04755 if(bUseExpTreeForFunEval == true)
04756 {
04757 calculateAllConstraintFunctionValues( x, new_x);
04758 calculateAllObjectiveFunctionValues( x, new_x);
04759 }
04760 else
04761 {
04762 getZeroOrderResults(x, objLambda, conMultipliers);
04763 }
04764
04765 }
04766 break;
04767 case 1:
04768 if(new_x == true || m_iHighestOrderEvaluated < 0)
04769 getZeroOrderResults(x, objLambda, conMultipliers);
04770 if(new_x == true || m_iHighestOrderEvaluated < 1)
04771 getFirstOrderResults(x, objLambda, conMultipliers);
04772 break;
04773 case 2:
04774 if(new_x == true || m_iHighestOrderEvaluated < 0)
04775 getZeroOrderResults(x, objLambda, conMultipliers);
04776 if(new_x == true || m_iHighestOrderEvaluated < 2)
04777 getSecondOrderResults(x, objLambda, conMultipliers);
04778 break;
04779 default:
04780 throw ErrorClass("Derivative should be order 0, 1, or 2");
04781 }
04782 return true;
04783 }
04784 catch(const ErrorClass& eclass)
04785 {
04786 throw ErrorClass( eclass.errormsg);
04787 }
04788 }
04789
04790
04791 bool OSInstance::getZeroOrderResults(double *x, double *objLambda, double *conMultipliers)
04792 {
04793 std::ostringstream outStr;
04794
04795 try
04796 {
04797
04798 int i, j, rowNum, objNum;
04799 if( m_mapExpressionTreesMod.size() > 0)
04800 {
04801 m_vdYval = this->forwardAD(0, m_vdX);
04802 }
04803
04804 for(rowNum = 0; rowNum < m_iConstraintNumber; rowNum++)
04805 {
04806 m_mdConstraintFunctionValues[ rowNum] = 0.0;
04807 if( m_mapExpressionTreesMod.find( rowNum) != m_mapExpressionTreesMod.end() )
04808 {
04809 m_mdConstraintFunctionValues[ rowNum] = m_vdYval[ m_mapOSADFunRangeIndex[ rowNum]];
04810 }
04811
04812
04813 i = m_sparseJacMatrix->starts[ rowNum];
04814 j = m_sparseJacMatrix->starts[ rowNum + 1 ];
04815 while ( (i - m_sparseJacMatrix->starts[ rowNum]) < m_sparseJacMatrix->conVals[ rowNum] )
04816 {
04817 m_mdConstraintFunctionValues[ rowNum] += m_sparseJacMatrix->values[ i]*x[ m_sparseJacMatrix->indexes[ i] ];
04818 i++;
04819 }
04820
04821 m_mdConstraintFunctionValues[ rowNum] += m_mdConstraintConstants[ rowNum ];
04822 #ifndef NDEBUG
04823 outStr.str("");
04824 outStr.clear();
04825 outStr << "Constraint " << rowNum << " function value = " << m_mdConstraintFunctionValues[ rowNum ] << std::endl;
04826 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
04827 #endif
04828 }
04829
04830 for(objNum = 0; objNum < m_iObjectiveNumber; objNum++)
04831 {
04832 m_mdObjectiveFunctionValues[ objNum] = 0.0;
04833 if( m_mapExpressionTreesMod.find( -objNum -1) != m_mapExpressionTreesMod.end() )
04834 {
04835 m_mdObjectiveFunctionValues[ objNum] = m_vdYval[ objNum];
04836 }
04837 for(i = 0; i < m_iVariableNumber; i++)
04838 {
04839 m_mdObjectiveFunctionValues[ objNum] += m_mmdDenseObjectiveCoefficients[ objNum][i]*x[ i];
04840 }
04841 #ifndef NDEBUG
04842 outStr.str("");
04843 outStr.clear();
04844 outStr << "Objective " << objNum << " function value = " << m_mdObjectiveFunctionValues[ objNum] << std::endl;
04845 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
04846 #endif
04847 }
04848 return true;
04849 }
04850 catch(const ErrorClass& eclass)
04851 {
04852 throw ErrorClass( eclass.errormsg);
04853 }
04854 }
04855
04856
04857
04858 bool OSInstance::getFirstOrderResults(double *x, double *objLambda, double *conMultipliers)
04859 {
04860 std::ostringstream outStr;
04861
04862 try
04863 {
04864
04865 unsigned int i, j;
04866 int rowNum, jacIndex;
04867 unsigned int jstart, jend;
04868 int idx;
04869 OSExpressionTree *expTree = NULL;
04870 int domainIdx = 0;
04871 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
04872 std::map<int, int>::iterator posVarIdx;
04873
04879 if(m_iNumberOfNonlinearVariables >= m_mapExpressionTreesMod.size() )
04880 {
04881
04882
04883 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
04884 {
04885 idx = posMapExpTree->first;
04886
04887 if(idx >= 0)
04888 {
04889 m_vdRangeUnitVec[ domainIdx] = 1.;
04890 m_mapExpressionTreesMod[ idx]->getVariableIndicesMap();
04891 m_vdYjacval = this->reverseAD(1, m_vdRangeUnitVec);
04892
04893 jstart = m_miJacStart[ idx] + m_miJacNumConTerms[ idx];
04894 jend = m_miJacStart[ idx + 1 ];
04895 if( (*m_mapExpressionTreesMod[ idx]->mapVarIdx).size() != (jend - jstart)) throw
04896 ErrorClass("number of partials not consistent");
04897 j = 0;
04898 jacIndex = 0;
04899 for(posVarIdx = m_mapAllNonlinearVariablesIndex.begin(); posVarIdx
04900 != m_mapAllNonlinearVariablesIndex.end(); ++posVarIdx)
04901 {
04902
04903
04904
04905
04906
04907 if( (*m_mapExpressionTreesMod[ idx]->mapVarIdx).find( posVarIdx->first) != (*m_mapExpressionTreesMod[ idx]->mapVarIdx).end())
04908 {
04909 m_mdJacValue[ jstart] = m_vdYjacval[ jacIndex];
04910 jstart++;
04911 j++;
04912 }
04913 jacIndex++;
04914 }
04915
04916 m_vdRangeUnitVec[ domainIdx] = 0.;
04917 domainIdx++;
04918 }
04919 else
04920 {
04921
04922
04923 domainIdx++;
04924 }
04925 }
04926 }
04927 else
04928 {
04929
04930 for(i = 0; i < m_iNumberOfNonlinearVariables; i++)
04931 {
04932 m_vdDomainUnitVec[i] = 1.;
04933 rowNum = 0;
04934 if( m_mapExpressionTreesMod.size() > 0)
04935 {
04936 m_vdYjacval = this->forwardAD(1, m_vdDomainUnitVec);
04937 }
04938
04939
04940 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
04941 {
04942 idx = posMapExpTree->first;
04943
04944 if(idx >= 0)
04945 {
04946
04947
04948
04949
04950 expTree = m_mapExpressionTreesMod[ idx];
04951 if( (*expTree->mapVarIdx).find( m_miNonLinearVarsReverseMap[ i]) != (*expTree->mapVarIdx).end() )
04952 {
04953 jacIndex = (*m_mapExpressionTreesMod[ idx]->mapVarIdx)[ m_miNonLinearVarsReverseMap[ i]];
04954 jstart = m_miJacStart[ idx] + m_miJacNumConTerms[ idx];
04955
04956 m_mdJacValue[ jstart + jacIndex] = m_vdYjacval[m_iObjectiveNumberNonlinear + rowNum];
04957 }
04958 rowNum++;
04959 }
04960 }
04961
04962 m_vdDomainUnitVec[i] = 0.;
04963 }
04964 }
04965 #ifndef NDEBUG
04966 outStr.str("");
04967 outStr.clear();
04968 int k;
04969 outStr << "JACOBIAN DATA " << std::endl;
04970 for(idx = 0; idx < m_iConstraintNumber; idx++)
04971 {
04972 for(k = *(m_sparseJacMatrix->starts + idx); k < *(m_sparseJacMatrix->starts + idx + 1); k++)
04973 {
04974 outStr << "row idx = " << idx << " col idx = "<< *(m_sparseJacMatrix->indexes + k)
04975 << " value = " << *(m_sparseJacMatrix->values + k) << std::endl;
04976 }
04977 }
04978 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
04979 #endif
04980 return true;
04981 }
04982 catch(const ErrorClass& eclass)
04983 {
04984 throw ErrorClass( eclass.errormsg);
04985 }
04986 }
04987
04988
04989 bool OSInstance::getSecondOrderResults(double *x, double *objLambda, double *conMultipliers)
04990 {
04991 std::ostringstream outStr;
04992
04993 try
04994 {
04995
04996 unsigned int i, j;
04997 int rowNum, jacIndex;
04998 int jstart, idx;
04999 OSExpressionTree *expTree = NULL;
05000 int hessValuesIdx = 0;
05001 if( m_bLagrangianSparseHessianCreated == false) getLagrangianHessianSparsityPattern( );
05002 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
05003 std::map<int, int>::iterator posVarIndexMap;
05004 if( objLambda == NULL) throw ErrorClass("must have a multiplier for the objective function even if zero when calling getSecondOrderResults");
05005 if( conMultipliers == NULL) throw ErrorClass("cannot have a null vector of lagrange multipliers when calling getSecondOrderResults -- okay if zero");
05006 if( m_vdLambda.size() > 0) m_vdLambda.clear();
05007 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
05008 {
05009 if( posMapExpTree->first >= 0)
05010 {
05011 m_vdLambda.push_back( conMultipliers[ posMapExpTree->first]);
05012 }
05013 else
05014 {
05015
05016 m_vdLambda.push_back( objLambda[ abs(posMapExpTree->first) - 1] );
05017 }
05018 }
05019 for(i = 0; i < m_iNumberOfNonlinearVariables; i++)
05020 {
05021 m_vdDomainUnitVec[i] = 1.;
05022 rowNum = 0;
05023 if( m_mapExpressionTreesMod.size() > 0)
05024 {
05025 m_vdYjacval = this->forwardAD(1, m_vdDomainUnitVec);
05026 }
05027
05028
05029 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
05030 {
05031 idx = posMapExpTree->first;
05032
05033 if(idx >= 0)
05034 {
05035
05036
05037
05038
05039 expTree = m_mapExpressionTreesMod[ idx];
05040 if( (*expTree->mapVarIdx).find( m_miNonLinearVarsReverseMap[ i]) != (*expTree->mapVarIdx).end() )
05041 {
05042 jacIndex = (*m_mapExpressionTreesMod[ idx]->mapVarIdx)[ m_miNonLinearVarsReverseMap[ i]];
05043 jstart = m_miJacStart[ idx] + m_miJacNumConTerms[ idx];
05044 m_mdJacValue[ jstart + jacIndex] = m_vdYjacval[m_iObjectiveNumberNonlinear + rowNum];
05045 }
05046 rowNum++;
05047 }
05048 else
05049 {
05050
05051
05052 m_mmdObjGradient[ (abs( idx) - 1)][ m_miNonLinearVarsReverseMap[ i]] = m_vdYjacval[ (abs( idx) - 1)] +
05053 m_mmdDenseObjectiveCoefficients[ (abs( idx) - 1)][ m_miNonLinearVarsReverseMap[ i]];
05054 }
05055 }
05056
05057 if( m_mapExpressionTreesMod.size() > 0)
05058 {
05059 m_vdw = reverseAD(2, m_vdLambda);
05060 }
05061 for(j = i; j < m_iNumberOfNonlinearVariables; j++)
05062 {
05063 if( m_vbLagHessNonz[i*m_iNumberOfNonlinearVariables + j] == true)
05064 {
05065 m_LagrangianSparseHessian->hessValues[ hessValuesIdx] = m_vdw[ j*2 + 1];
05066 #ifndef NDEBUG
05067 outStr.str("");
05068 outStr.clear();
05069 outStr << "reverse 2 " << m_LagrangianSparseHessian->hessValues[ hessValuesIdx] << std::endl;
05070 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
05071 #endif
05072 hessValuesIdx++;
05073 }
05074 }
05075
05076
05077 m_vdDomainUnitVec[i] = 0.;
05078 }
05079 #ifndef NDEBUG
05080 outStr.str("");
05081 outStr.clear();
05082 int k;
05083 outStr << "JACOBIAN DATA " << std::endl;
05084 for(idx = 0; idx < m_iConstraintNumber; idx++)
05085 {
05086 for(k = *(m_sparseJacMatrix->starts + idx); k < *(m_sparseJacMatrix->starts + idx + 1); k++)
05087 {
05088 outStr << "row idx = " << idx << " col idx = "<< *(m_sparseJacMatrix->indexes + k)
05089 << " value = " << *(m_sparseJacMatrix->values + k) << std::endl;
05090 }
05091 }
05092 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
05093 #endif
05094 return true;
05095 }
05096 catch(const ErrorClass& eclass)
05097 {
05098 throw ErrorClass( eclass.errormsg);
05099 }
05100 }
05101
05102 bool OSInstance::initForAlgDiff()
05103 {
05104 std::ostringstream outStr;
05105
05106 if( m_binitForAlgDiff == true ) return true;
05107 initializeNonLinearStructures( );
05108 initObjGradients();
05109 getAllNonlinearVariablesIndexMap( );
05110
05111
05112
05113 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
05114 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
05115 {
05116 if(posMapExpTree->second->bADMustReTape == true) m_bCppADMustReTape = true;
05117 }
05118
05119 #ifndef NDEBUG
05120 outStr << "RETAPE == " << m_bCppADMustReTape << std::endl;
05121 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
05122 #endif
05123 unsigned int i;
05124 for(i = 0; i < m_iNumberOfNonlinearVariables; i++)
05125 {
05126 m_vdDomainUnitVec.push_back( 0.0 );
05127 }
05128 for(i = 0; i < m_mapExpressionTreesMod.size(); i++)
05129 {
05130 m_vdRangeUnitVec.push_back( 0.0 );
05131 }
05132 m_binitForAlgDiff = true;
05133
05134
05135 return true;
05136 }
05137
05138 bool OSInstance::initObjGradients()
05139 {
05140 std::ostringstream outStr;
05141
05142 int i, j;
05143 int m, n;
05144 m = getObjectiveNumber();
05145 n = getVariableNumber();
05146 getDenseObjectiveCoefficients();
05147 if(m <= 0)
05148 {
05149 m_mmdObjGradient = NULL;
05150 return true;
05151 }
05152 m_mmdObjGradient = new double*[m];
05153 for(i = 0; i < m; i++)
05154 {
05155 m_mmdObjGradient[i] = new double[n];
05156 for(j = 0; j < n; j++)
05157 {
05158 m_mmdObjGradient[i][j] = m_mmdDenseObjectiveCoefficients[ i][j];
05159 #ifndef NDEBUG
05160 outStr << "m_mmdObjGradient[i][j] = " << m_mmdObjGradient[i][j] << std::endl;
05161 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_detailed_trace, outStr.str());
05162 #endif
05163 }
05164 }
05165 return true;
05166 }
05175 bool OSInstance::setTimeDomain(std::string format)
05176 {
05177 if ((format != "stages") && (format != "interval") && (format != "none"))
05178 return false;
05179 if (instanceData->timeDomain == NULL)
05180 {
05181 instanceData->timeDomain = new TimeDomain();
05182 }
05183 if (format == "stages")
05184 {
05185 if (instanceData->timeDomain->interval != NULL)
05186 {
05187 delete instanceData->timeDomain->interval;
05188 instanceData->timeDomain->interval = NULL;
05189 }
05190 if (instanceData->timeDomain->stages == NULL)
05191 instanceData->timeDomain->stages = new TimeDomainStages();
05192 m_sTimeDomainFormat = format;
05193 }
05194 if (format == "interval")
05195 {
05196 if (instanceData->timeDomain->stages != NULL)
05197 {
05198 delete instanceData->timeDomain->stages;
05199 instanceData->timeDomain->stages = NULL;
05200 }
05201 if (instanceData->timeDomain->interval == NULL)
05202 instanceData->timeDomain->interval = new TimeDomainInterval();
05203 m_sTimeDomainFormat = format;
05204 }
05205 if (format == "none")
05206 {
05207 if (instanceData->timeDomain->stages != NULL)
05208 {
05209 delete instanceData->timeDomain->stages;
05210 instanceData->timeDomain->stages = NULL;
05211 }
05212 if (instanceData->timeDomain->interval != NULL)
05213 {
05214 delete instanceData->timeDomain->interval;
05215 instanceData->timeDomain->interval = NULL;
05216 }
05217 m_sTimeDomainFormat = "";
05218 }
05219 return true;
05220 }
05221
05225 bool OSInstance::setTimeDomainStages(int number, std::string *names)
05226 {
05227 if (instanceData->timeDomain == NULL)
05228 instanceData->timeDomain = new TimeDomain();
05229 if (instanceData->timeDomain->interval != NULL)
05230 return false;
05231 if (instanceData->timeDomain->stages == NULL)
05232 {
05233 instanceData->timeDomain->stages = new TimeDomainStages;
05234 }
05235 else
05236 {
05237 if (instanceData->timeDomain->stages->numberOfStages != number)
05238 {
05239 for (int i = 0; i < instanceData->timeDomain->stages->numberOfStages; i++)
05240 {
05241 if (instanceData->timeDomain->stages->stage[i]->variables != NULL)
05242 {
05243 delete instanceData->timeDomain->stages->stage[i]->variables;
05244 instanceData->timeDomain->stages->stage[i]->variables = NULL;
05245 }
05246 if (instanceData->timeDomain->stages->stage[i]->constraints != NULL)
05247 {
05248 delete instanceData->timeDomain->stages->stage[i]->constraints;
05249 instanceData->timeDomain->stages->stage[i]->constraints = NULL;
05250 }
05251 if (instanceData->timeDomain->stages->stage[i]->objectives != NULL)
05252 {
05253 delete instanceData->timeDomain->stages->stage[i]->objectives;
05254 instanceData->timeDomain->stages->stage[i]->objectives = NULL;
05255 }
05256 delete instanceData->timeDomain->stages->stage[i];
05257 instanceData->timeDomain->stages->stage[i] = NULL;
05258 }
05259 delete []instanceData->timeDomain->stages->stage;
05260 instanceData->timeDomain->stages->stage = NULL;
05261 }
05262 }
05263 if (number != 0 )
05264 {
05265 if (instanceData->timeDomain->stages->stage == NULL)
05266 instanceData->timeDomain->stages->stage = new TimeDomainStage*[number];
05267 for (int i = 0; i < number; i++)
05268 {
05269 instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
05270 }
05271 instanceData->timeDomain->stages->numberOfStages = number;
05272 }
05273 for (int i = 0; i < number; i++)
05274
05275 {
05276 if (instanceData->timeDomain->stages->stage[i]->variables != NULL)
05277 {
05278 delete instanceData->timeDomain->stages->stage[i]->variables;
05279 instanceData->timeDomain->stages->stage[i]->variables = NULL;
05280 }
05281 instanceData->timeDomain->stages->stage[i]->variables = new TimeDomainStageVariables();
05282 if (instanceData->timeDomain->stages->stage[i]->constraints != NULL)
05283 {
05284 delete instanceData->timeDomain->stages->stage[i]->constraints;
05285 instanceData->timeDomain->stages->stage[i]->constraints = NULL;
05286 }
05287 instanceData->timeDomain->stages->stage[i]->constraints = new TimeDomainStageConstraints();
05288 if (instanceData->timeDomain->stages->stage[i]->objectives != NULL)
05289 {
05290 delete instanceData->timeDomain->stages->stage[i]->objectives;
05291 instanceData->timeDomain->stages->stage[i]->objectives = NULL;
05292 }
05293 instanceData->timeDomain->stages->stage[i]->objectives = new TimeDomainStageObjectives();
05294 instanceData->timeDomain->stages->stage[i]->objectives->numberOfObjectives = instanceData->objectives->numberOfObjectives;
05295 instanceData->timeDomain->stages->stage[i]->objectives->obj = new TimeDomainStageObj*[instanceData->objectives->numberOfObjectives];
05296 for (int j = 0; j < instanceData->objectives->numberOfObjectives; j++)
05297 {
05298 instanceData->timeDomain->stages->stage[i]->objectives->obj[j] = new TimeDomainStageObj();
05299 instanceData->timeDomain->stages->stage[i]->objectives->obj[j]->idx = -(j+1);
05300 }
05301 if (names != NULL)
05302 instanceData->timeDomain->stages->stage[i]->name = names[i];
05303 }
05304 return true;
05305 }
05306
05310 bool OSInstance::setTimeDomainStageVariablesOrdered(int numberOfStages, int *numberOfVariables, int *startIdx)
05311 {
05312 if (instanceData->timeDomain == NULL)
05313 instanceData->timeDomain = new TimeDomain();
05314 if (instanceData->timeDomain->interval != NULL)
05315 return false;
05316 if (instanceData->timeDomain->stages == NULL)
05317 instanceData->timeDomain->stages = new TimeDomainStages();
05318 if (instanceData->timeDomain->stages != NULL)
05319 {
05320 if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
05321 (instanceData->timeDomain->stages->numberOfStages != 0))
05322 return false;
05323 }
05324 if (instanceData->timeDomain->stages->numberOfStages == 0)
05325 {
05326 instanceData->timeDomain->stages->numberOfStages = numberOfStages;
05327 if (instanceData->timeDomain->stages->stage == NULL)
05328 instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
05329 for (int i = 0; i < numberOfStages; i++)
05330 instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
05331 }
05332 int checksum = 0;
05333 for (int i = 0; i < numberOfStages; i++)
05334
05335 {
05336 if (instanceData->timeDomain->stages->stage[i]->variables != NULL)
05337 {
05338 delete instanceData->timeDomain->stages->stage[i]->variables;
05339 instanceData->timeDomain->stages->stage[i]->variables = NULL;
05340 }
05341 instanceData->timeDomain->stages->stage[i]->variables = new TimeDomainStageVariables();
05342 instanceData->timeDomain->stages->stage[i]->variables->startIdx = startIdx[i];
05343 instanceData->timeDomain->stages->stage[i]->variables->numberOfVariables = numberOfVariables[i];
05344 checksum += numberOfVariables[i];
05345 }
05346 return (checksum == instanceData->variables->numberOfVariables);
05347 }
05348
05352 bool OSInstance::setTimeDomainStageVariablesUnordered(int numberOfStages, int *numberOfVariables, int **varIndex)
05353 {
05354 if (instanceData->timeDomain == NULL)
05355 instanceData->timeDomain = new TimeDomain();
05356 if (instanceData->timeDomain->interval != NULL)
05357 return false;
05358 if (instanceData->timeDomain->stages == NULL)
05359 instanceData->timeDomain->stages = new TimeDomainStages();
05360 if (instanceData->timeDomain->stages != NULL)
05361 {
05362 if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
05363 (instanceData->timeDomain->stages->numberOfStages != 0))
05364 return false;
05365 }
05366 if (instanceData->timeDomain->stages->numberOfStages == 0 )
05367 {
05368 instanceData->timeDomain->stages->numberOfStages = numberOfStages;
05369 if (instanceData->timeDomain->stages->stage == NULL)
05370 instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
05371 for (int i = 0; i < numberOfStages; i++)
05372 instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
05373 }
05374 int checksum = 0;
05375 for (int i = 0; i < numberOfStages; i++)
05376
05377 {
05378 if (instanceData->timeDomain->stages->stage[i]->variables != NULL)
05379 {
05380 delete instanceData->timeDomain->stages->stage[i]->variables;
05381 instanceData->timeDomain->stages->stage[i]->variables = NULL;
05382 }
05383 instanceData->timeDomain->stages->stage[i]->variables = new TimeDomainStageVariables();
05384 instanceData->timeDomain->stages->stage[i]->variables->numberOfVariables = numberOfVariables[i];
05385 instanceData->timeDomain->stages->stage[i]->variables->var = new TimeDomainStageVar*[numberOfVariables[i]];
05386 for (int j = 0; j < numberOfVariables[i]; j++)
05387 {
05388 instanceData->timeDomain->stages->stage[i]->variables->var[j] = new TimeDomainStageVar();
05389 instanceData->timeDomain->stages->stage[i]->variables->var[j]->idx = varIndex[i][j];
05390 }
05391 checksum += numberOfVariables[i];
05392 }
05393 if (checksum != instanceData->variables->numberOfVariables) return false;
05394 int *checkvar = new int[instanceData->variables->numberOfVariables];
05395 for (int j = 0; j < instanceData->variables->numberOfVariables; j++)
05396 checkvar[j] = -1;
05397 int k;
05398 for (int i = 0; i < numberOfStages; i++)
05399 for (int j = 0; j < instanceData->timeDomain->stages->stage[i]->variables->numberOfVariables; j++)
05400 {
05401 k = instanceData->timeDomain->stages->stage[i]->variables->var[j]->idx;
05402 if (checkvar[k] != -1)
05403 {
05404 delete [] checkvar;
05405 checkvar = NULL;
05406 return false;
05407 }
05408 checkvar[k] = instanceData->timeDomain->stages->stage[i]->variables->var[j]->idx;
05409 }
05410 delete [] checkvar;
05411 checkvar = NULL;
05412 return true;
05413 }
05414
05418 bool OSInstance::setTimeDomainStageConstraintsOrdered(int numberOfStages, int *numberOfConstraints, int *startIdx)
05419 {
05420 if (instanceData->timeDomain == NULL)
05421 instanceData->timeDomain = new TimeDomain();
05422 if (instanceData->timeDomain->interval != NULL)
05423 return false;
05424 if (instanceData->timeDomain->stages == NULL)
05425 instanceData->timeDomain->stages = new TimeDomainStages();
05426 if (instanceData->timeDomain->stages != NULL)
05427 {
05428 if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
05429 (instanceData->timeDomain->stages->numberOfStages != 0))
05430 return false;
05431 }
05432 if (instanceData->timeDomain->stages->numberOfStages == 0 )
05433 {
05434 instanceData->timeDomain->stages->numberOfStages = numberOfStages;
05435 if (instanceData->timeDomain->stages->stage == NULL)
05436 instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
05437 for (int i = 0; i < numberOfStages; i++)
05438 instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
05439 }
05440 int checksum = 0;
05441 for (int i = 0; i < numberOfStages; i++)
05442
05443 {
05444 if (instanceData->timeDomain->stages->stage[i]->constraints != NULL)
05445 {
05446 delete instanceData->timeDomain->stages->stage[i]->constraints;
05447 instanceData->timeDomain->stages->stage[i]->constraints = NULL;
05448 }
05449 instanceData->timeDomain->stages->stage[i]->constraints = new TimeDomainStageConstraints();
05450 instanceData->timeDomain->stages->stage[i]->constraints->startIdx = startIdx[i];
05451 instanceData->timeDomain->stages->stage[i]->constraints->numberOfConstraints = numberOfConstraints[i];
05452 checksum += numberOfConstraints[i];
05453 }
05454 return (checksum == instanceData->constraints->numberOfConstraints);
05455 }
05456
05460 bool OSInstance::setTimeDomainStageConstraintsUnordered(int numberOfStages, int *numberOfConstraints, int **conIndex)
05461 {
05462 if (instanceData->timeDomain == NULL)
05463 instanceData->timeDomain = new TimeDomain();
05464 if (instanceData->timeDomain->interval != NULL)
05465 return false;
05466 if (instanceData->timeDomain->stages == NULL)
05467 instanceData->timeDomain->stages = new TimeDomainStages();
05468 if (instanceData->timeDomain->stages != NULL)
05469 {
05470 if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
05471 (instanceData->timeDomain->stages->numberOfStages != 0))
05472 return false;
05473 }
05474 if ( instanceData->timeDomain->stages->numberOfStages == 0 )
05475 {
05476 instanceData->timeDomain->stages->numberOfStages = numberOfStages;
05477 if (instanceData->timeDomain->stages->stage == NULL)
05478 instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
05479 for (int i = 0; i < numberOfStages; i++)
05480 instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
05481 }
05482 int checksum = 0;
05483 for (int i = 0; i < numberOfStages; i++)
05484
05485 {
05486 if (instanceData->timeDomain->stages->stage[i]->constraints != NULL)
05487 {
05488 delete instanceData->timeDomain->stages->stage[i]->constraints;
05489 instanceData->timeDomain->stages->stage[i]->constraints = NULL;
05490 }
05491 instanceData->timeDomain->stages->stage[i]->constraints = new TimeDomainStageConstraints();
05492 instanceData->timeDomain->stages->stage[i]->constraints->numberOfConstraints = numberOfConstraints[i];
05493 instanceData->timeDomain->stages->stage[i]->constraints->con = new TimeDomainStageCon*[numberOfConstraints[i]];
05494 for (int j = 0; j < numberOfConstraints[i]; j++)
05495 {
05496 instanceData->timeDomain->stages->stage[i]->constraints->con[j] = new TimeDomainStageCon();
05497 instanceData->timeDomain->stages->stage[i]->constraints->con[j]->idx = conIndex[i][j];
05498 }
05499 checksum += numberOfConstraints[i];
05500 }
05501 if (checksum != instanceData->constraints->numberOfConstraints) return false;
05502 int *checkvar = new int[instanceData->constraints->numberOfConstraints];
05503 for (int j = 0; j < instanceData->constraints->numberOfConstraints; j++)
05504 checkvar[j] = -1;
05505 int k;
05506 for (int i = 0; i < numberOfStages; i++)
05507 for (int j = 0; j < instanceData->timeDomain->stages->stage[i]->constraints->numberOfConstraints; j++)
05508 {
05509 k = instanceData->timeDomain->stages->stage[i]->constraints->con[j]->idx;
05510 if (checkvar[k] != -1)
05511 {
05512 delete [] checkvar;
05513 checkvar = NULL;
05514 return false;
05515 }
05516 checkvar[k] = instanceData->timeDomain->stages->stage[i]->constraints->con[j]->idx;
05517 }
05518 delete [] checkvar;
05519 checkvar = NULL;
05520 return true;
05521 }
05522
05526 bool OSInstance::setTimeDomainStageObjectivesOrdered(int numberOfStages, int *numberOfObjectives, int *startIdx)
05527 {
05528 if (instanceData->timeDomain == NULL)
05529 instanceData->timeDomain = new TimeDomain();
05530 if (instanceData->timeDomain->interval != NULL)
05531 return false;
05532 if (instanceData->timeDomain->stages == NULL)
05533 instanceData->timeDomain->stages = new TimeDomainStages();
05534 if (instanceData->timeDomain->stages != NULL)
05535 {
05536 if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
05537 (instanceData->timeDomain->stages->numberOfStages != 0))
05538 return false;
05539 }
05540 if (instanceData->timeDomain->stages->numberOfStages == 0)
05541 {
05542 instanceData->timeDomain->stages->numberOfStages = numberOfStages;
05543 if (instanceData->timeDomain->stages->stage == NULL)
05544 instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
05545 for (int i = 0; i < numberOfStages; i++)
05546 instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
05547 }
05548 for (int i = 0; i < numberOfStages; i++)
05549
05550 {
05551 if (instanceData->timeDomain->stages->stage[i]->objectives != NULL)
05552 {
05553 delete instanceData->timeDomain->stages->stage[i]->objectives;
05554 instanceData->timeDomain->stages->stage[i]->objectives = NULL;
05555 }
05556 instanceData->timeDomain->stages->stage[i]->objectives = new TimeDomainStageObjectives();
05557 instanceData->timeDomain->stages->stage[i]->objectives->startIdx = startIdx[i];
05558 instanceData->timeDomain->stages->stage[i]->objectives->numberOfObjectives = numberOfObjectives[i];
05559 }
05560 return true;
05561 }
05562
05566 bool OSInstance::setTimeDomainStageObjectivesUnordered(int numberOfStages, int *numberOfObjectives, int **objIndex)
05567 {
05568 if (instanceData->timeDomain == NULL)
05569 instanceData->timeDomain = new TimeDomain();
05570 if (instanceData->timeDomain->interval != NULL)
05571 return false;
05572 if (instanceData->timeDomain->stages == NULL)
05573 instanceData->timeDomain->stages = new TimeDomainStages();
05574 if (instanceData->timeDomain->stages != NULL)
05575 {
05576 if ((instanceData->timeDomain->stages->numberOfStages != numberOfStages) &&
05577 (instanceData->timeDomain->stages->numberOfStages != 0))
05578 return false;
05579 }
05580 if (instanceData->timeDomain->stages->numberOfStages == 0)
05581 {
05582 instanceData->timeDomain->stages->numberOfStages = numberOfStages;
05583 if (instanceData->timeDomain->stages->stage == NULL)
05584 instanceData->timeDomain->stages->stage = new TimeDomainStage*[numberOfStages];
05585 for (int i = 0; i < numberOfStages; i++)
05586 instanceData->timeDomain->stages->stage[i] = new TimeDomainStage();
05587 }
05588 for (int i = 0; i < numberOfStages; i++)
05589
05590 {
05591 if (instanceData->timeDomain->stages->stage[i]->objectives != NULL)
05592 {
05593 delete instanceData->timeDomain->stages->stage[i]->objectives;
05594 instanceData->timeDomain->stages->stage[i]->objectives = NULL;
05595 }
05596 instanceData->timeDomain->stages->stage[i]->objectives = new TimeDomainStageObjectives();
05597 instanceData->timeDomain->stages->stage[i]->objectives->numberOfObjectives = numberOfObjectives[i];
05598 instanceData->timeDomain->stages->stage[i]->objectives->obj = new TimeDomainStageObj*[numberOfObjectives[i]];
05599 for (int j = 0; j < numberOfObjectives[i]; j++)
05600 {
05601 instanceData->timeDomain->stages->stage[i]->objectives->obj[j] = new TimeDomainStageObj();
05602 instanceData->timeDomain->stages->stage[i]->objectives->obj[j]->idx = objIndex[i][j];
05603 }
05604 }
05605 int *checkvar = new int[instanceData->objectives->numberOfObjectives];
05606 for (int j = 0; j < instanceData->objectives->numberOfObjectives; j++)
05607 checkvar[j] = 0;
05608 int k;
05609 for (int i = 0; i < numberOfStages; i++)
05610 for (int j = 0; j < instanceData->timeDomain->stages->stage[i]->objectives->numberOfObjectives; j++)
05611 {
05612 k = -instanceData->timeDomain->stages->stage[i]->objectives->obj[j]->idx-1;
05613 checkvar[k] = instanceData->timeDomain->stages->stage[i]->objectives->obj[j]->idx;
05614 }
05615 for (int i = 0; i < instanceData->objectives->numberOfObjectives; i++)
05616 if (checkvar[i] == 0)
05617 {
05618 delete [] checkvar;
05619 checkvar = NULL;
05620 return false;
05621 }
05622 delete [] checkvar;
05623 checkvar = NULL;
05624 return true;
05625 }
05626
05630 bool OSInstance::setTimeDomainInterval(double start, double horizon)
05631 {
05632 if (instanceData->timeDomain == NULL)
05633 instanceData->timeDomain = new TimeDomain();
05634 if (instanceData->timeDomain->stages != NULL)
05635 return false;
05636 if (instanceData->timeDomain->interval == NULL)
05637 instanceData->timeDomain->interval = new TimeDomainInterval();
05638 instanceData->timeDomain->interval->start = start;
05639 instanceData->timeDomain->interval->horizon = horizon;
05640 return true;
05641 }
05642
05643
05644 bool OSInstance::createOSADFun(std::vector<double> vdX)
05645 {
05646 try
05647 {
05648 if(m_bOSADFunIsCreated == true) return true;
05649
05650 if(m_binitForAlgDiff == false) initForAlgDiff();
05651
05652
05653 std::map<int, OSExpressionTree*>::iterator posMapExpTree;
05654 unsigned int i;
05655 size_t n = vdX.size();
05656 #ifdef COIN_HAS_CPPAD
05657
05658 CppAD::vector< CppAD::AD<double> > vdaX( n );
05659 for(i = 0; i < n; i++)
05660 {
05661 vdaX[ i] = vdX[ i];
05662
05663 }
05664
05665 CppAD::Independent( vdaX);
05671 CppAD::vector< CppAD::AD<double> > m_vFG;
05672 int kount = 0;
05673 for(posMapExpTree = m_mapExpressionTreesMod.begin(); posMapExpTree != m_mapExpressionTreesMod.end(); ++posMapExpTree)
05674 {
05675 m_vFG.push_back( (posMapExpTree->second)->m_treeRoot->constructADTape(&m_mapAllNonlinearVariablesIndex, &vdaX) );
05676
05677 if( m_mapOSADFunRangeIndex.find( posMapExpTree->first) == m_mapOSADFunRangeIndex.end() )
05678 {
05679
05680 m_mapOSADFunRangeIndex[ posMapExpTree->first] = kount;
05681 kount++;
05682 }
05683 }
05684
05685
05686 Fad = new CppAD::ADFun<double>(vdaX, m_vFG);
05687
05688
05689 m_iHighestTaylorCoeffOrder = -1;
05690 m_bOSADFunIsCreated = true;
05691 #else
05692 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
05693 #endif
05694
05695 return true;
05696 }
05697 catch(const ErrorClass& eclass)
05698 {
05699 throw ErrorClass( eclass.errormsg);
05700 }
05701 }
05702
05703
05704 std::vector<double> OSInstance::forwardAD(int p, std::vector<double> vdX)
05705 {
05706 try
05707 {
05708
05709 if(m_bOSADFunIsCreated == false) createOSADFun( vdX);
05710 if(p > (m_iHighestTaylorCoeffOrder + 1) ) throw
05711 ErrorClass( "trying to calculate a p order forward when p-1 Taylor coefficient not available");
05712
05713 m_iHighestTaylorCoeffOrder = p;
05714 m_iHighestOrderEvaluated = p;
05715
05716
05717
05718 #ifdef COIN_HAS_CPPAD
05719 return (*Fad).Forward(p, vdX);
05720 #else
05721 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
05722 #endif
05723
05724 }
05725 catch(const ErrorClass& eclass)
05726 {
05727 throw ErrorClass( eclass.errormsg);
05728 }
05729 }
05730
05731
05732 std::vector<double> OSInstance::reverseAD(int p, std::vector<double> vdlambda)
05733 {
05734 try
05735 {
05736 #ifndef COIN_HAS_CPPAD
05737 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
05738 #endif
05739 if(p == 0) throw
05740 ErrorClass( "reverseAD must have p >= 1");
05741 if(p > (m_iHighestTaylorCoeffOrder + 1) ) throw
05742 ErrorClass( "trying to calculate a p order reverse when p-1 Taylor coefficient not available");
05743
05744
05745
05746 m_iHighestOrderEvaluated = p;
05747 #ifdef COIN_HAS_CPPAD
05748 return (*Fad).Reverse(p, vdlambda);
05749 #else
05750 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
05751 #endif
05752
05753 }
05754 catch(const ErrorClass& eclass)
05755 {
05756 throw ErrorClass( eclass.errormsg);
05757 }
05758 }
05759
05760
05761 int OSInstance::getADSparsityHessian()
05762 {
05763
05764 unsigned int i;
05765 int numNonz;
05766 numNonz = 0;
05767 try
05768 {
05769 std::vector<bool> r(m_iNumberOfNonlinearVariables * m_iNumberOfNonlinearVariables);
05770 unsigned int j;
05771 for(i = 0; i < m_iNumberOfNonlinearVariables; i++)
05772 {
05773 for(j = 0; j < m_iNumberOfNonlinearVariables; j++)
05774 r[ i * m_iNumberOfNonlinearVariables + j ] = false;
05775 r[ i * m_iNumberOfNonlinearVariables + i] = true;
05776 }
05777
05778
05779 #ifdef COIN_HAS_CPPAD
05780 (*Fad).ForSparseJac(m_iNumberOfNonlinearVariables, r);
05781 #else
05782 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
05783 #endif
05784
05785
05786 unsigned int m = m_mapExpressionTreesMod.size();
05787 std::vector<bool> e( m);
05788
05789 for(i = 0; i < m; i++) e[i] = true;
05790
05791
05792 #ifdef COIN_HAS_CPPAD
05793 m_vbLagHessNonz = (*Fad).RevSparseHes(m_iNumberOfNonlinearVariables, e);
05794 #else
05795 throw ErrorClass( "Error: An Algorithmic Differentiation Package Not Available");
05796 #endif
05797
05798 for(i = 0; i < m_iNumberOfNonlinearVariables; i++)
05799 {
05800
05801 for(j = i; j < m_iNumberOfNonlinearVariables; j++)
05802 {
05803 if(m_vbLagHessNonz[ i*m_iNumberOfNonlinearVariables + j] == true) numNonz++;
05804
05805 }
05806
05807 }
05808 return numNonz;
05809 }
05810 catch(const ErrorClass& eclass)
05811 {
05812 throw ErrorClass( eclass.errormsg);
05813 }
05814 }
05815
05816
05817
05818
05819
05820
05821
05822 bool OSInstance::IsEqual(OSInstance *that)
05823 {
05824 #ifndef NDEBUG
05825 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug, "Start comparing in OSInstance");
05826 #endif
05827 if (this == NULL)
05828 {
05829 if (that == NULL)
05830 return true;
05831 else
05832 {
05833 #ifndef NDEBUG
05834 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
05835 "First object is NULL, second is not");
05836 #endif
05837 return false;
05838 }
05839 }
05840 else
05841 {
05842 if (that == NULL)
05843 {
05844 #ifndef NDEBUG
05845 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
05846 "Second object is NULL, first is not");
05847 #endif
05848 return false;
05849 }
05850 else
05851 {
05852 if (!this->instanceHeader->IsEqual(that->instanceHeader))
05853 return false;
05854 if (!this->instanceData->IsEqual(that->instanceData))
05855 return false;
05856
05857 return true;
05858 }
05859 }
05860 }
05861
05862
05863 bool InstanceData::IsEqual(InstanceData *that)
05864 {
05865 #ifndef NDEBUG
05866 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug, "Start comparing in InstanceData");
05867 #endif
05868 if (this == NULL)
05869 {
05870 if (that == NULL)
05871 return true;
05872 else
05873 {
05874 #ifndef NDEBUG
05875 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
05876 "First object is NULL, second is not");
05877 #endif
05878 return false;
05879 }
05880 }
05881 else
05882 {
05883 if (that == NULL)
05884 {
05885 #ifndef NDEBUG
05886 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
05887 "Second object is NULL, first is not");
05888 #endif
05889 return false;
05890 }
05891 else
05892 {
05893 if (!this->variables->IsEqual(that->variables))
05894 return false;
05895 if (!this->objectives->IsEqual(that->objectives))
05896 return false;
05897 if (!this->constraints->IsEqual(that->constraints))
05898 return false;
05899 if (!this->linearConstraintCoefficients->IsEqual(that->linearConstraintCoefficients))
05900 return false;
05901 if (!this->quadraticCoefficients->IsEqual(that->quadraticCoefficients))
05902 return false;
05903 if (!this->nonlinearExpressions->IsEqual(that->nonlinearExpressions))
05904 return false;
05905
05906 return true;
05907 }
05908 }
05909 }
05910
05911 bool Variables::IsEqual(Variables *that)
05912 {
05913 #ifndef NDEBUG
05914 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug, "Start comparing in Variables");
05915 #endif
05916 if (this == NULL)
05917 {
05918 if (that == NULL)
05919 return true;
05920 else
05921 {
05922 #ifndef NDEBUG
05923 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
05924 "First object is NULL, second is not");
05925 #endif
05926 return false;
05927 }
05928 }
05929 else
05930 {
05931 if (that == NULL)
05932 {
05933 #ifndef NDEBUG
05934 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
05935 "Second object is NULL, first is not");
05936 #endif
05937 return false;
05938 }
05939 else
05940 {
05941 if (this->numberOfVariables != that->numberOfVariables)
05942 return false;
05943 for (int i=0; i<this->numberOfVariables; i++)
05944 if (!this->var[i]->IsEqual(that->var[i]))
05945 return false;
05946
05947 return true;
05948 }
05949 }
05950 }
05951
05952 bool Variable::IsEqual(Variable *that)
05953 {
05954 #ifndef NDEBUG
05955 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug, "Start comparing in Variable");
05956 #endif
05957 if (this == NULL)
05958 {
05959 if (that == NULL)
05960 return true;
05961 else
05962 {
05963 #ifndef NDEBUG
05964 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
05965 "First object is NULL, second is not");
05966 #endif
05967 return false;
05968 }
05969 }
05970 else
05971 {
05972 if (that == NULL)
05973 {
05974 #ifndef NDEBUG
05975 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
05976 "Second object is NULL, first is not");
05977 #endif
05978 return false;
05979 }
05980 else
05981 {
05982 if (this->lb != that->lb)
05983 return false;
05984 if (this->ub != that->ub)
05985 return false;
05986 if (this->type != that->type)
05987 return false;
05988 if (this->name != that->name)
05989 return false;
05990
05991 return true;
05992 }
05993 }
05994 }
05995
05996 bool Objectives::IsEqual(Objectives *that)
05997 {
05998 #ifndef NDEBUG
05999 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug, "Start comparing in Objectives");
06000 #endif
06001 if (this == NULL)
06002 {
06003 if (that == NULL)
06004 return true;
06005 else
06006 {
06007 #ifndef NDEBUG
06008 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06009 "First object is NULL, second is not");
06010 #endif
06011 return false;
06012 }
06013 }
06014 else
06015 {
06016 if (that == NULL)
06017 {
06018 #ifndef NDEBUG
06019 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06020 "Second object is NULL, first is not");
06021 #endif
06022 return false;
06023 }
06024 else
06025 {
06026 if (this->numberOfObjectives != that->numberOfObjectives)
06027 return false;
06028 for (int i=0; i<this->numberOfObjectives; i++)
06029 if (!this->obj[i]->IsEqual(that->obj[i]))
06030 return false;
06031
06032 return true;
06033 }
06034 }
06035 }
06036
06037 bool Objective::IsEqual(Objective *that)
06038 {
06039 #ifndef NDEBUG
06040 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug, "Start comparing in Objective");
06041 #endif
06042 if (this == NULL)
06043 {
06044 if (that == NULL)
06045 return true;
06046 else
06047 {
06048 #ifndef NDEBUG
06049 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06050 "First object is NULL, second is not");
06051 #endif
06052 return false;
06053 }
06054 }
06055 else
06056 {
06057 if (that == NULL)
06058 {
06059 #ifndef NDEBUG
06060 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06061 "Second object is NULL, first is not");
06062 #endif
06063 return false;
06064 }
06065 else
06066 {
06067 if (this->name != that->name)
06068 return false;
06069 if (this->maxOrMin != that->maxOrMin)
06070 return false;
06071 if (this->constant != that->constant)
06072 return false;
06073 if (!isEqual(this->weight, that->weight))
06074 return false;
06075 if (this->numberOfObjCoef != that->numberOfObjCoef)
06076 return false;
06077
06078 for (int i=0; i<this->numberOfObjCoef; i++)
06079 if (!this->coef[i]->IsEqual(that->coef[i]))
06080 return false;
06081
06082 return true;
06083 }
06084 }
06085 }
06086
06087 bool ObjCoef::IsEqual(ObjCoef *that)
06088 {
06089 #ifndef NDEBUG
06090 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug, "Start comparing in ObjCoef");
06091 #endif
06092 if (this == NULL)
06093 {
06094 if (that == NULL)
06095 return true;
06096 else
06097 {
06098 #ifndef NDEBUG
06099 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06100 "First object is NULL, second is not");
06101 #endif
06102 return false;
06103 }
06104 }
06105 else
06106 {
06107 if (that == NULL)
06108 {
06109 #ifndef NDEBUG
06110 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06111 "Second object is NULL, first is not");
06112 #endif
06113 return false;
06114 }
06115 else
06116 {
06117 if (this->idx != that->idx)
06118 return false;
06119 if (this->value != that->value)
06120 return false;
06121
06122 return true;
06123 }
06124 }
06125 }
06126
06127 bool Constraints::IsEqual(Constraints *that)
06128 {
06129 #ifndef NDEBUG
06130 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug, "Start comparing in Constraints");
06131 #endif
06132 if (this == NULL)
06133 {
06134 if (that == NULL)
06135 return true;
06136 else
06137 {
06138 #ifndef NDEBUG
06139 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06140 "First object is NULL, second is not");
06141 #endif
06142 return false;
06143 }
06144 }
06145 else
06146 {
06147 if (that == NULL)
06148 {
06149 #ifndef NDEBUG
06150 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06151 "Second object is NULL, first is not");
06152 #endif
06153 return false;
06154 }
06155 else
06156 {
06157 if (this->numberOfConstraints != that->numberOfConstraints)
06158 return false;
06159 for (int i=0; i<this->numberOfConstraints; i++)
06160 if (!this->con[i]->IsEqual(that->con[i]))
06161 return false;
06162
06163 return true;
06164 }
06165 }
06166 }
06167
06168 bool Constraint::IsEqual(Constraint *that)
06169 {
06170 #ifndef NDEBUG
06171 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug, "Start comparing in Constraint");
06172 #endif
06173 if (this == NULL)
06174 {
06175 if (that == NULL)
06176 return true;
06177 else
06178 {
06179 #ifndef NDEBUG
06180 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06181 "First object is NULL, second is not");
06182 #endif
06183 return false;
06184 }
06185 }
06186 else
06187 {
06188 if (that == NULL)
06189 {
06190 #ifndef NDEBUG
06191 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06192 "Second object is NULL, first is not");
06193 #endif
06194 return false;
06195 }
06196 else
06197 {
06198 if (this->name != that->name)
06199 return false;
06200 if (this->constant != that->constant)
06201 return false;
06202 if (this->lb != that->lb)
06203 return false;
06204 if (this->ub != that->ub)
06205 return false;
06206
06207 return true;
06208 }
06209 }
06210 }
06211
06212 bool LinearConstraintCoefficients::IsEqual(LinearConstraintCoefficients *that)
06213 {
06214 #ifndef NDEBUG
06215 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug, "Start comparing in LinearConstraintCoefficients");
06216 #endif
06217 if (this == NULL)
06218 {
06219 if (that == NULL)
06220 return true;
06221 else
06222 {
06223 #ifndef NDEBUG
06224 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06225 "First object is NULL, second is not");
06226 #endif
06227 return false;
06228 }
06229 }
06230 else
06231 {
06232 if (that == NULL)
06233 {
06234 #ifndef NDEBUG
06235 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06236 "Second object is NULL, first is not");
06237 #endif
06238 return false;
06239 }
06240 else
06241 {
06242 if (this->numberOfValues != that->numberOfValues)
06243 return false;
06244
06245 if (!this->start->IsEqual(that->start))
06246 return false;
06247 if (!this->rowIdx->IsEqual(that->rowIdx))
06248 return false;
06249 if (!this->colIdx->IsEqual(that->colIdx))
06250 return false;
06251 if (!this->value->IsEqual(that->value))
06252 return false;
06253
06254 return true;
06255 }
06256 }
06257 }
06258
06259 bool QuadraticCoefficients::IsEqual(QuadraticCoefficients *that)
06260 {
06261 #ifndef NDEBUG
06262 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug, "Start comparing in QuadraticCoefficients");
06263 #endif
06264 if (this == NULL)
06265 {
06266 if (that == NULL)
06267 return true;
06268 else
06269 {
06270 #ifndef NDEBUG
06271 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06272 "First object is NULL, second is not");
06273 #endif
06274 return false;
06275 }
06276 }
06277 else
06278 {
06279 if (that == NULL)
06280 {
06281 #ifndef NDEBUG
06282 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06283 "Second object is NULL, first is not");
06284 #endif
06285 return false;
06286 }
06287 else
06288 {
06289 if (this->numberOfQuadraticTerms != that->numberOfQuadraticTerms)
06290 return false;
06291 for (int i=0; i<this->numberOfQuadraticTerms; i++)
06292 if (!this->qTerm[i]->IsEqual(that->qTerm[i]))
06293 return false;
06294
06295 return true;
06296 }
06297 }
06298 }
06299
06300
06301 bool QuadraticTerm::IsEqual(QuadraticTerm *that)
06302 {
06303 #ifndef NDEBUG
06304 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug, "Start comparing in QuadraticTerm");
06305 #endif
06306 if (this == NULL)
06307 {
06308 if (that == NULL)
06309 return true;
06310 else
06311 {
06312 #ifndef NDEBUG
06313 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06314 "First object is NULL, second is not");
06315 #endif
06316 return false;
06317 }
06318 }
06319 else
06320 {
06321 if (that == NULL)
06322 {
06323 #ifndef NDEBUG
06324 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06325 "Second object is NULL, first is not");
06326 #endif
06327 return false;
06328 }
06329 else
06330 {
06331 if (this->idx != that->idx)
06332 return false;
06333 if (this->idxOne != that->idxOne)
06334 return false;
06335 if (this->idxTwo != that->idxTwo)
06336 return false;
06337 if (this->coef != that->coef)
06338 return false;
06339
06340 return true;
06341 }
06342 }
06343 }
06344
06345 bool NonlinearExpressions::IsEqual(NonlinearExpressions *that)
06346 {
06347 #ifndef NDEBUG
06348 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug, "Start comparing in NonlinearExpressions");
06349 #endif
06350 if (this == NULL)
06351 {
06352 if (that == NULL)
06353 return true;
06354 else
06355 {
06356 #ifndef NDEBUG
06357 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06358 "First object is NULL, second is not");
06359 #endif
06360 return false;
06361 }
06362 }
06363 else
06364 {
06365 if (that == NULL)
06366 {
06367 #ifndef NDEBUG
06368 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06369 "Second object is NULL, first is not");
06370 #endif
06371 return false;
06372 }
06373 else
06374 {
06375 if (this->numberOfNonlinearExpressions != that->numberOfNonlinearExpressions)
06376 return false;
06377
06378 for (int i=0; i<this->numberOfNonlinearExpressions; i++)
06379 if (!this->nl[i]->IsEqual(that->nl[i]))
06380 return false;
06381
06382 return true;
06383 }
06384 }
06385 }
06386
06387
06388
06389 bool Nl::IsEqual(Nl *that)
06390 {
06391 #ifndef NDEBUG
06392 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug, "Start comparing in Nl");
06393 #endif
06394 if (this == NULL)
06395 {
06396 if (that == NULL)
06397 return true;
06398 else
06399 {
06400 #ifndef NDEBUG
06401 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06402 "First object is NULL, second is not");
06403 #endif
06404 return false;
06405 }
06406 }
06407 else
06408 {
06409 if (that == NULL)
06410 {
06411 #ifndef NDEBUG
06412 osoutput->OSPrint(ENUM_OUTPUT_AREA_OSInstance, ENUM_OUTPUT_LEVEL_debug,
06413 "Second object is NULL, first is not");
06414 #endif
06415 return false;
06416 }
06417 else
06418 {
06419 if (this->idx != that->idx)
06420 return false;
06421 if (!this->osExpressionTree->IsEqual(that->osExpressionTree))
06422 return false;
06423
06424 return true;
06425 }
06426 }
06427 }
06428
06429