00001
00019 #include "OSKnitroSolver.h"
00020 #include "OSCommonUtil.h"
00021
00022 using std::cout;
00023 using std::endl;
00024 using std::ostringstream;
00025 using namespace std;
00026
00027 #if defined(_WIN32)
00028 #include <windows.h>
00029 #else
00030 #include <dlfcn.h>
00031 #endif
00032
00033
00034
00035
00036
00037
00038 static NlpProblemDef * g_pTheNlpProblemDefInstance = NULL;
00039
00040
00041 static NlpProblemDef * g_pOptProblem = NULL;
00042
00043 static int wrapperEvalFC (const int evalRequestCode,
00044 const int n,
00045 const int m,
00046 const int nnzJ,
00047 const int nnzH,
00048 const double * const daX,
00049 const double * const daLambda,
00050 double * const dObj,
00051 double * const daC,
00052 double * const daG,
00053 double * const daJ,
00054 double * const daH,
00055 double * const daHV,
00056 void * userParams)
00057 {
00058 if (g_pOptProblem == NULL)
00059 {
00060 cout << "*** Problem not defined <wrapperEvalFC>\n";
00061 return( -1 );
00062 }
00063 if (evalRequestCode != KTR_RC_EVALFC)
00064 {
00065 cout << "*** Bad request code " << evalRequestCode
00066 << " <wrapperEvalFC>\n";
00067 return( -1 );
00068 }
00069 return( g_pOptProblem->evalFC (daX, dObj, daC, userParams) );
00070 }
00071
00072
00073
00074
00075
00079 static int wrapperEvalGA (const int evalRequestCode,
00080 const int n,
00081 const int m,
00082 const int nnzJ,
00083 const int nnzH,
00084 const double * const daX,
00085 const double * const daLambda,
00086 double * const dObj,
00087 double * const daC,
00088 double * const daG,
00089 double * const daJ,
00090 double * const daH,
00091 double * const daHV,
00092 void * userParams)
00093 {
00094 if (g_pOptProblem == NULL)
00095 {
00096 cout << "*** Problem not defined <wrapperEvalGA>\n";
00097 return( -1 );
00098 }
00099 if (evalRequestCode != KTR_RC_EVALGA)
00100 {
00101 cout << "*** Bad request code " << evalRequestCode
00102 << " <wrapperEvalGA>\n";
00103 return( -1 );
00104 }
00105 return( g_pOptProblem->evalGA (daX, daG, daJ, userParams) );
00106 }
00107
00108
00109
00110
00111
00115 static int wrapperEvalHorHV (const int evalRequestCode,
00116 const int n,
00117 const int m,
00118 const int nnzJ,
00119 const int nnzH,
00120 const double * const daX,
00121 const double * const daLambda,
00122 double * const dObj,
00123 double * const daC,
00124 double * const daG,
00125 double * const daJ,
00126 double * const daH,
00127 double * const daHV,
00128 void * userParams)
00129 {
00130 if (g_pOptProblem == NULL)
00131 {
00132 cout << "*** Problem not defined <wrapperEvalHorHV>\n";
00133 return( -1 );
00134 }
00135 if (evalRequestCode == KTR_RC_EVALH)
00136 {
00137 if (g_pOptProblem->areDerivativesImplemented (nCAN_COMPUTE_H) == false)
00138 {
00139 cout << "*** This problem not evaluate H <wrapperEvalHorHV>\n";
00140 return( -1 );
00141 }
00142 return( g_pOptProblem->evalH (daX, daLambda, daH, userParams) );
00143 }
00144 else if (evalRequestCode == KTR_RC_EVALHV)
00145 {
00146 if (g_pOptProblem->areDerivativesImplemented (nCAN_COMPUTE_HV) == false)
00147 {
00148 cout << "*** This problem not evaluate H*v <wrapperEvalHorHV>\n";
00149 return( -1 );
00150 }
00151 return( g_pOptProblem->evalHV (daX, daLambda, daHV, userParams) );
00152 }
00153 else
00154 {
00155 cout << "*** Bad request code " << evalRequestCode
00156 << " <wrapperEvalHorHV>\n";
00157 return( -1 );
00158 }
00159 }
00160
00161
00162
00163
00164
00165
00166
00167 #ifdef __cplusplus
00168 extern "C"
00169 {
00170 #if defined(_WIN32)
00171 __declspec(dllexport) NlpProblemDef * getNlpProblemDef (void)
00172 #else
00173 NlpProblemDef * getNlpProblemDef (void)
00174 #endif
00175 {
00176 if (g_pTheNlpProblemDefInstance == NULL)
00177 g_pTheNlpProblemDefInstance = new KnitroProblem(NULL, NULL);
00178
00179 return( g_pTheNlpProblemDefInstance );
00180 }
00181 }
00182 #endif
00183
00184
00185 KnitroProblem::KnitroProblem(OSInstance *osinstance_, OSResult *osresult_ )
00186 {
00187
00188 _daXInit = NULL;
00189 osinstance = osinstance_;
00190 osresult = osresult_;
00191 }
00192
00193
00194
00195 KnitroProblem::~KnitroProblem()
00196 {
00197 delete [] _daXInit;
00198 _daXInit = NULL;
00199 }
00200
00201
00202
00203
00204
00205
00206
00207 int KnitroProblem::getN (void)
00208 {
00209 return( _nN );
00210 }
00211
00212
00213 int KnitroProblem::getM (void)
00214 {
00215 return( _nM );
00216 }
00217
00218
00219 void KnitroProblem::getInitialX (double * const daX)
00220 {
00221 if (_daXInit == NULL)
00222 {
00223 cout << "*** Must call 'loadProblemIntoKnitro' before 'KnitroSolver::getInitialX'\n";
00224 exit( EXIT_FAILURE );
00225 }
00226
00227 for (int i = 0; i < _nN; i++)
00228 {
00229 daX[i] = _daXInit[i];
00230 }
00231 return;
00232 }
00233
00234
00235
00236
00240 bool KnitroProblem::loadProblemIntoKnitro (KTR_context_ptr kc)
00241 {
00242 int i;
00243 if(osinstance->getObjectiveNumber() <= 0) throw ErrorClass("Knitro NEEDS AN OBJECTIVE FUNCTION");
00244
00245 _nN = osinstance->getVariableNumber();
00246
00247 _nM = osinstance->getConstraintNumber();
00248 cout << "number variables !!!!!!!!!!!!!!!!!!!!!!!!!!!" << _nN << endl;
00249 cout << "number constraints !!!!!!!!!!!!!!!!!!!!!!!!!!!" << _nM << endl;
00250 try
00251 {
00252 osinstance->initForAlgDiff( );
00253 }
00254 catch(const ErrorClass& eclass)
00255 {
00256 knitroErrorMsg = eclass.errormsg;
00257 throw;
00258 }
00259
00260
00261 double * mdVarLB = osinstance->getVariableLowerBounds();
00262 std::cout << "GET BOUNDS INFORMATION FOR KNITRO!!!!!!!!!!!!!!!" << std::endl;
00263
00264 double * mdVarUB = osinstance->getVariableUpperBounds();
00265
00266 _daXLo = new double[_nN];
00267 _daXUp = new double[_nN];
00268 for(i = 0; i < _nN; i++)
00269 {
00270 if( mdVarLB[ i] == -OSDBL_MAX) _daXLo[i] = -KTR_INFBOUND;
00271 else _daXLo[i] = mdVarLB[ i];
00272 if( mdVarUB[ i] == OSDBL_MAX) _daXUp[i] = KTR_INFBOUND;
00273 else _daXUp[i] = mdVarUB[ i];
00274
00275
00276 }
00277
00278
00279 double * mdConLB = osinstance->getConstraintLowerBounds();
00280
00281 double * mdConUB = osinstance->getConstraintUpperBounds();
00282
00283 _naCType = new int[ _nM];
00284 _daCLo = new double[ _nM];
00285 _daCUp = new double[ _nM];
00286 _naCType[0] = KTR_CONTYPE_LINEAR;
00287 for(i = 0; i < _nM; i++)
00288 {
00289 if( mdConLB[ i] == -OSDBL_MAX) _daCLo[i] = -KTR_INFBOUND;
00290 else _daCLo[i] = mdConLB[ i];
00291 if( mdConUB[ i] == OSDBL_MAX) _daCUp[i] = KTR_INFBOUND;
00292 else _daCUp[i] = mdConUB[ i];
00293
00294
00295 _naCType[i] = KTR_CONTYPE_GENERAL;
00296 }
00297
00298
00299 osinstance->bUseExpTreeForFunEval = true;
00300 std::cout << "Call sparse jacobian" << std::endl;
00301 SparseJacobianMatrix *sparseJacobian = NULL;
00302 try
00303 {
00304 sparseJacobian = osinstance->getJacobianSparsityPattern();
00305 }
00306 catch(const ErrorClass& eclass)
00307 {
00308 knitroErrorMsg = eclass.errormsg;
00309 throw;
00310 }
00311 std::cout << "Done calling sparse jacobian" << std::endl;
00312 _nNnzJ = sparseJacobian->valueSize;
00313
00314 _naJacIndexVars = new int[_nNnzJ];
00315 _naJacIndexCons = new int[_nNnzJ];
00316 int k, idx;
00317 i = 0;
00318 for(idx = 0; idx < _nM; idx++)
00319 {
00320 for(k = *(sparseJacobian->starts + idx); k < *(sparseJacobian->starts + idx + 1); k++)
00321 {
00322 _naJacIndexCons[i] = idx;
00323 _naJacIndexVars[i] = *(sparseJacobian->indexes + k);
00324
00325
00326 i++;
00327 }
00328 }
00329
00330
00331
00332 if( (osinstance->getNumberOfNonlinearExpressions() == 0) && (osinstance->getNumberOfQuadraticTerms() == 0) )
00333 {
00334 cout << "This is a linear program" << endl;
00335 _nNnzH = 0;
00336 }
00337 else
00338 {
00339 std::cout << "Get Lagrangain Hessian Sparsity Pattern " << std::endl;
00340 SparseHessianMatrix *sparseHessian = osinstance->getLagrangianHessianSparsityPattern();
00341 std::cout << "Done Getting Lagrangain Hessian Sparsity Pattern " << std::endl;
00342 _nNnzH = sparseHessian->hessDimension;
00343 _naHessRows = new int[_nNnzH];
00344 _naHessCols = new int[_nNnzH];
00345 for(i = 0; i < _nNnzH; i++)
00346 {
00347 _naHessCols[i] = *(sparseHessian->hessColIdx + i);
00348 _naHessRows[i] = *(sparseHessian->hessRowIdx + i);
00349 cout << "ROW HESS IDX !!!!!!!!!!!!!!!!!!!!!!!!!!!" << _naHessRows[i] << endl;
00350 cout << "COL HESS IDX !!!!!!!!!!!!!!!!!!!!!!!!!!!" << _naHessCols[i] << endl;
00351 }
00352 }
00353
00354
00355
00356
00357
00358 double *mdXInit = osinstance->getVariableInitialValues();
00359 _daXInit = new double[_nN];
00360 if( mdXInit != NULL)
00361 {
00362 for(i = 0; i < _nN; i++)
00363 {
00364 if( CommonUtil::ISOSNAN( mdXInit[ i]) == true)
00365 {
00366 _daXInit[ i] = 1.7171;
00367 }
00368 else _daXInit[ i] = mdXInit[ i];
00369 }
00370 }
00371 else
00372 {
00373 for(i = 0; i < _nN; i++)
00374 {
00375 _daXInit[ i] = 1.7171;
00376 }
00377 }
00378
00379
00380 double * daLambdaInit = new double[_nM + _nN];
00381 for (i = 0; i < _nM + _nN; i++)
00382 daLambdaInit[i] = 0.0;
00383
00384
00385 int iObjSense = KTR_OBJGOAL_MINIMIZE;
00386 if( osinstance->instanceData->objectives->obj[ 0]->maxOrMin.compare("max") == 0)
00387 {
00388 iObjSense = KTR_OBJGOAL_MAXIMIZE;
00389 }
00390 int iObjType = KTR_OBJTYPE_GENERAL;
00391 i = KTR_init_problem (kc, _nN,
00392 iObjSense, iObjType,
00393 _daXLo, _daXUp,
00394 _nM, _naCType, _daCLo, _daCUp,
00395 _nNnzJ, _naJacIndexVars, _naJacIndexCons,
00396 _nNnzH, _naHessRows, _naHessCols,
00397 _daXInit, daLambdaInit);
00398
00399 delete [] _daXLo;
00400 delete [] _daXUp;
00401 delete [] _naCType;
00402 delete [] _daCLo;
00403 delete [] _daCUp;
00404 delete [] _naJacIndexVars;
00405 delete [] _naJacIndexCons;
00406 delete [] _naHessRows;
00407 delete [] _naHessCols;
00408 delete [] daLambdaInit;
00409
00410 if (i != 0)
00411 {
00412 cout << "*** KTR_init_problem() returned " << i << "\n";
00413 return( false );
00414 }
00415
00416 return( true );
00417 }
00418
00419
00420
00421
00422 bool KnitroProblem::areDerivativesImplemented(const DerivativesImplementedType nWhichDers)
00423 {
00424 if (nWhichDers == nCAN_COMPUTE_GA)
00425 return( true );
00426 if (nWhichDers == nCAN_COMPUTE_H)
00427 return( true );
00428 if (nWhichDers == nCAN_COMPUTE_HV)
00429 return( false);
00430 return( false );
00431 }
00432
00433
00434
00435
00436
00437 int KnitroProblem::evalFC (const double * const daX,
00438 double * const dObj,
00439 double * const daC,
00440 void * userParams)
00441 {
00442 try
00443 {
00444 *dObj = osinstance->calculateAllObjectiveFunctionValues((double*)daX, NULL, NULL, true, 0 )[0];
00445 }
00446 catch(const ErrorClass& eclass)
00447 {
00448 knitroErrorMsg = eclass.errormsg;
00449 return (-1);
00450 }
00451 if( CommonUtil::ISOSNAN( *dObj )) return (-1);
00452
00453
00454
00455 try
00456 {
00457 double *conVals = osinstance->calculateAllConstraintFunctionValues((double*)daX, NULL, NULL, true, 0 );
00458 int i;
00459 for(i = 0; i < _nM; i++)
00460 {
00461 if( CommonUtil::ISOSNAN( (double)conVals[ i] ) ) return (-1);
00462 daC[i] = conVals[ i] ;
00463 }
00464 }
00465 catch(const ErrorClass& eclass)
00466 {
00467 knitroErrorMsg = eclass.errormsg;
00468 return (-1);
00469 }
00470
00471
00472 return( 0 );
00473 }
00474
00475
00476
00477
00478
00479 int KnitroProblem::evalGA (const double * const daX,
00480 double * const daG,
00481 double * const daJ,
00482 void * userParams)
00483 {
00484 try
00485 {
00486 double *objGrad = osinstance->calculateAllObjectiveFunctionGradients((double*)daX, NULL, NULL, true, 1 )[0];
00487 int i;
00488 std::cout << "EVALUATING OBJ GRADIENT" << std::endl;
00489 for(i = 0; i < _nN; i++)
00490 {
00491 if( CommonUtil::ISOSNAN( (double)objGrad[ i] ) ) return (-1);
00492 daG[i] = objGrad[ i] ;
00493 }
00494 }
00495 catch(const ErrorClass& eclass)
00496 {
00497 knitroErrorMsg = eclass.errormsg;
00498 return (-1);
00499 }
00500
00501 SparseJacobianMatrix *sparseJacobian;
00502 std::cout << "EVALUATING JACOBIAN" << std::endl;
00503 try
00504 {
00505 sparseJacobian = osinstance->calculateAllConstraintFunctionGradients((double*)daX, NULL, NULL, true, 1);
00506 }
00507 catch(const ErrorClass& eclass)
00508 {
00509 knitroErrorMsg = eclass.errormsg;
00510 return (-1);
00511 }
00512
00513 for(int i = 0; i < _nNnzJ; i++)
00514 {
00515 daJ[ i] = sparseJacobian->values[i];
00516
00517 cout << "daJ[i]:!!!!!!!!!!!! " << daJ[ i] << endl;
00518
00519 }
00520
00521 return( 0 );
00522 }
00523
00524
00525
00526
00527
00528 int KnitroProblem::evalH (const double * const daX,
00529 const double * const daLambda,
00530 double * const daH,
00531 void * userParams)
00532 {
00533 SparseHessianMatrix *sparseHessian;
00534 int i;
00535 double* objMultipliers = new double[1];
00536 objMultipliers[0] = 1;
00537 try
00538 {
00539 sparseHessian = osinstance->calculateLagrangianHessian((double*)daX, objMultipliers, (double*)daLambda , true, 2);
00540 }
00541 catch(const ErrorClass& eclass)
00542 {
00543 knitroErrorMsg = eclass.errormsg;
00544 return (-1);
00545 }
00546 for(i = 0; i < _nNnzH; i++)
00547 {
00548 daH[ i] = *(sparseHessian->hessValues + i);
00549 std::cout << "Hessian Value = " << daH[ i] << std::endl;
00550 }
00551 delete[] objMultipliers;
00552 return( 0 );
00553 }
00554
00555
00556
00557
00558
00559 int KnitroProblem::evalHV (const double * const daX,
00560 const double * const daLambda,
00561 double * const daHV,
00562 void * userParams)
00563 {
00564
00565 return( 0 );
00566 }
00567
00568
00569
00570
00571 KnitroSolver::KnitroSolver()
00572 {
00573 osrlwriter = new OSrLWriter();
00574 osresult = new OSResult();
00575 knitroErrorMsg = "";
00576
00577 }
00578
00579
00580 void KnitroSolver::buildSolverInstance() throw (ErrorClass)
00581 {
00582 try
00583 {
00584
00585 }
00586 catch(const ErrorClass& eclass)
00587 {
00588 std::cout << "THERE IS AN ERROR" << std::endl;
00589 osresult->setGeneralMessage( eclass.errormsg);
00590 osresult->setGeneralStatusType( "error");
00591 osrl = osrlwriter->writeOSrL( osresult);
00592 throw ErrorClass( osrl) ;
00593 }
00594 }
00595
00596
00597
00598
00599 void Knitro::setSolverOptions() throw (ErrorClass)
00600 {
00601 try
00602 {
00603
00604 }
00605 catch(const ErrorClass& eclass)
00606 {
00607 std::cout << "THERE IS AN ERROR" << std::endl;
00608 osresult->setGeneralMessage( eclass.errormsg);
00609 osresult->setGeneralStatusType( "error");
00610 osrl = osrlwriter->writeOSrL( osresult);
00611 throw ErrorClass( osrl) ;
00612 }
00613 }
00614
00615 KnitroSolver::~KnitroSolver()
00616 {
00617 #ifdef DEBUG
00618 cout << "inside KnitroSolver destructor" << endl;
00619 #endif
00620 delete osresult;
00621 osresult = NULL;
00622 delete osrlwriter;
00623 osrlwriter = NULL;
00624
00625
00626 #ifdef DEBUG
00627 cout << "leaving KnitroSolver destructor" << endl;
00628 #endif
00629 }
00630
00631 void KnitroSolver::solve() throw (ErrorClass)
00632 {
00633 try
00634 {
00635 OSiLReader* osilreader = NULL;
00636
00637 if(osil.length() == 0 && osinstance == NULL) throw ErrorClass("there is no instance");
00638 clock_t start, finish;
00639 double duration;
00640 start = clock();
00641 bool newOSiLReader = false;
00642 if( osinstance == NULL)
00643 {
00644 osilreader = new OSiLReader();
00645 osinstance = osilreader->readOSiL( osil);
00646 newOSiLReader = true;
00647 }
00648 OSiLWriter osilwriter;
00649
00650 if(osinstance->getVariableNumber() <= 0)throw ErrorClass("Knitro requires decision variables");
00651 finish = clock();
00652 duration = (double) (finish - start) / CLOCKS_PER_SEC;
00653 cout << "Parsing took (seconds): "<< duration << endl;
00654
00655
00656
00657
00658 NlpProblemDef *pOptProb = new KnitroProblem(osinstance, osresult);
00659 bool bWantToSolve;
00660
00661
00662 KTR_context *kc;
00663 kc = KTR_new();
00664 if (kc == NULL)
00665 {
00666 cout << "*** KTR_new failed, maybe a license issue?\n";
00667 exit( EXIT_FAILURE );
00668 }
00669
00670
00671 KTR_load_param_file (kc, "knitro.opt");
00672
00673 if (pOptProb->loadProblemIntoKnitro ( kc) == false)
00674 {
00675 cout << "*** loadProblemIntoKnitro failed\n";
00676 exit( EXIT_FAILURE );
00677 }
00678
00679
00680
00681
00682
00683 g_pOptProblem = pOptProb;
00684 if (KTR_set_func_callback (kc, wrapperEvalFC) != 0)
00685 {
00686 cout << "*** KTR_set_func_callback failed\n";
00687 exit( EXIT_FAILURE );
00688 }
00689 if (pOptProb->areDerivativesImplemented (nCAN_COMPUTE_GA) == true)
00690 {
00691 if (KTR_set_grad_callback (kc, wrapperEvalGA) != 0)
00692 {
00693 cout << "*** KTR_set_grad_callback failed\n";
00694 exit( EXIT_FAILURE );
00695 }
00696 }
00697 if ((pOptProb->areDerivativesImplemented (nCAN_COMPUTE_H) == true)
00698 || (pOptProb->areDerivativesImplemented (nCAN_COMPUTE_HV) == true))
00699 {
00700 if (KTR_set_hess_callback (kc, wrapperEvalHorHV) != 0)
00701 {
00702 cout << "*** KTR_set_hess_callback failed\n";
00703 exit( EXIT_FAILURE );
00704 }
00705 }
00706
00707
00708 double * daX = new double[pOptProb->getN()];
00709 double * daLambda = new double[pOptProb->getM() + pOptProb->getN()];
00710 double* mdObjValues = new double[1];
00711 bWantToSolve = true;
00712 if (bWantToSolve == true)
00713 {
00714
00715 double dFinalObj;
00716 int nStatus = KTR_solve (kc, daX, daLambda, 0, &dFinalObj,
00717 NULL, NULL, NULL, NULL, NULL, NULL);
00718 std::cout << "dFinalObj = " << dFinalObj << std::endl;
00719 cout << "*** Final KNITRO status = " << nStatus << "\n";
00720 cout << "dax[0] = " << daX[0] << "\n";
00721
00722
00723 int solIdx = 0;
00724
00725 std::string message = "Knitro solver finishes to the end.";
00726 std::string solutionDescription = "";
00727
00728
00729 if(osresult->setServiceName( "Knitro solver service") != true)
00730 throw ErrorClass("OSResult error: setServiceName");
00731 if(osresult->setInstanceName( osinstance->getInstanceName()) != true)
00732 throw ErrorClass("OSResult error: setInstanceName");
00733
00734
00735
00736
00737
00738 if(osresult->setVariableNumber( osinstance->getVariableNumber()) != true)
00739 throw ErrorClass("OSResult error: setVariableNumer");
00740 if(osresult->setObjectiveNumber( 1) != true)
00741 throw ErrorClass("OSResult error: setObjectiveNumber");
00742 if(osresult->setConstraintNumber( osinstance->getConstraintNumber()) != true)
00743 throw ErrorClass("OSResult error: setConstraintNumber");
00744 if(osresult->setSolutionNumber( 1) != true)
00745 throw ErrorClass("OSResult error: setSolutionNumer");
00746
00747
00748 if(osresult->setGeneralMessage( message) != true)
00749 throw ErrorClass("OSResult error: setGeneralMessage");
00750 std::cout << "START CASES " << 0 << endl;
00751 switch( nStatus)
00752 {
00753 case 0:
00754 std::cout << "WE ARE IN CASE " << 0 << endl;
00755 solutionDescription = "LOCALLY OPTIMAL SOLUTION FOUND[KNITRO STATUS 0]: Knitro found a locally optimal point which satisfies the stopping criterion.If the problem is convex (for example, a linear program), then this point corresponds to a globally optimal solution.";
00756 osresult->setSolutionStatus(solIdx, "locallyOptimal", solutionDescription);
00757 std::cout << "SET SOLUTION STATUS " << endl;
00758 osresult->setPrimalVariableValuesDense(solIdx, daX);
00759 std::cout << "SET PRIMAL VALUES " << endl;
00760 osresult->setDualVariableValuesDense(solIdx, daLambda);
00761 mdObjValues[0] = dFinalObj;
00762 osresult->setObjectiveValuesDense(solIdx, mdObjValues);
00763 std::cout << "SET OBJECTIVE VALUES " << endl;
00764 break;
00765 case -1:
00766 solutionDescription = "Iteration limit reached[KNITRO STATUS -1]: The iteration limit was reached before being able to satisfy the required stopping criteria.";
00767 osresult->setSolutionStatus(solIdx, "stoppedByLimit", solutionDescription);
00768 osresult->setPrimalVariableValuesDense(solIdx, daX);
00769 osresult->setDualVariableValuesDense(solIdx, daLambda);
00770 mdObjValues[0] = dFinalObj;
00771 osresult->setObjectiveValuesDense(solIdx, mdObjValues);
00772 break;
00773 case -2:
00774 solutionDescription = "Convergence to an infeasible point[KNITRO STATUS -2]: Problem may be locally infeasible.The algorithm has converged to an infeasible point from which it cannot further decrease the infeasibility measure. This happens when the problem is infeasible, but may also occur on occasion for feasible problems with nonlinear constraints or badly scaled problems. It is recommended to try various initial points. If this occurs for a variety of initial points, it is likely the problem is infeasible.";
00775 osresult->setSolutionStatus(solIdx, "infeasible", solutionDescription);
00776 break;
00777 case -3:
00778 solutionDescription = "Problem appears to be unbounded[KNITRO STATUS -3]: Iterate is feasible and objective magnitude > objrange. The objective function appears to be decreasing without bound, while satisfying the constraints.If the problem really is bounded, increase the size of the parameter objrange to avoid terminating with this message.";
00779 osresult->setSolutionStatus(solIdx, "unbounded", solutionDescription);
00780 break;
00781 case -4:
00782 solutionDescription = "Relative change in solution estimate < xtol[KNITRO STATUS -4]: The relative change in the solution estimate is less than that specified by the paramater xtol.To try to get more accuracy one may decrease xtol. If xtol is very small already, it is an indication that no more significant progress can be made. If the current point is feasible, it is possible it may be optimal, however the stopping tests cannot be satisfied (perhaps because of degeneracy, ill-conditioning or bad scaling).";
00783 osresult->setSolutionStatus(solIdx, "stoppedByLimit", solutionDescription);
00784 osresult->setPrimalVariableValuesDense(solIdx, daX);
00785 osresult->setDualVariableValuesDense(solIdx, daLambda);
00786 mdObjValues[0] = dFinalObj;
00787 osresult->setObjectiveValuesDense(solIdx, mdObjValues);
00788 break;
00789 case -5:
00790 solutionDescription = "Current solution estimate cannot be improved. Point appears to be optimal, but desired accuracy could not be achieved.[KNITRO STATUS -5]: No more progress can be made, but the stopping tests are close to being satisfied (within a factor of 100) and so the current approximate solution is believed to be optimal.";
00791 osresult->setSolutionStatus(solIdx, "locallyOptimal", solutionDescription);
00792 osresult->setPrimalVariableValuesDense(solIdx, daX);
00793 osresult->setDualVariableValuesDense(solIdx, daLambda);
00794 mdObjValues[0] = dFinalObj;
00795 osresult->setObjectiveValuesDense(solIdx, mdObjValues);
00796 break;
00797 case -6:
00798 solutionDescription = "Time limit reached[KNITRO STATUS -6]: The time limit was reached before being able to satisfy the required stopping criteria.";
00799 osresult->setSolutionStatus(solIdx, "stoppedByLimit", solutionDescription);
00800 osresult->setPrimalVariableValuesDense(solIdx, daX);
00801 osresult->setDualVariableValuesDense(solIdx, daLambda);
00802 mdObjValues[0] = dFinalObj;
00803 osresult->setObjectiveValuesDense(solIdx, mdObjValues);
00804 break;
00805 case -50:
00806 case -51:
00807 case -52:
00808 case -53:
00809 case -54:
00810 case -55:
00811 case -56:
00812 case -57:
00813 case -58:
00814 case -59:
00815 case -60:
00816 solutionDescription = "Input Error[KNITRO STATUS -50 to -60]: Termination values in this range imply some input error. If outlev>0 details of this error will be printed to standard output or the file knitro.log depending on the value of outmode.";
00817 osresult->setSolutionStatus(solIdx, "error", solutionDescription);
00818 break;
00819 case -90:
00820 solutionDescription = "Callback function error[KNITRO STATUS -90]: This termination value indicates that an error (i.e., negative return value) occurred in a user provided callback routine.";
00821 osresult->setSolutionStatus(solIdx, "error", solutionDescription);
00822 break;
00823 case -97:
00824 solutionDescription = "LP solver error[KNITRO STATUS -97]: This termination value indicates that an unrecoverable error occurred in the LP solver used in the active-set algorithm preventing the optimization from continuing.";
00825 osresult->setSolutionStatus(solIdx, "error", solutionDescription);
00826 break;
00827 case -98:
00828 solutionDescription = "Evaluation error[KNITRO STATUS -98]: This termination value indicates that an evaluation error occurred (e.g., divide by 0, taking the square root of a negative number), preventing the optimization from continuing.";
00829 osresult->setSolutionStatus(solIdx, "error", solutionDescription);
00830 break;
00831 case -99:
00832 solutionDescription = "Not enough memory available to solve problem[KNITRO STATUS -99]: This termination value indicates that there was not enough memory available to solve the problem.";
00833 osresult->setSolutionStatus(solIdx, "error", solutionDescription);
00834 break;
00835 default:
00836 solutionDescription = "OTHER[KNITRO]: other unknown solution status from Knitro solver";
00837 osresult->setSolutionStatus(solIdx, "other", solutionDescription);
00838 }
00839
00840 osresult->setGeneralStatusType("normal");
00841 osrl = osrlwriter->writeOSrL( osresult);
00842 }
00843 else
00844 {
00845
00846 pOptProb->getInitialX (daX);
00847 KTR_check_first_ders (kc, daX, 2, 1.0e-14, 1.0e-14,
00848 0, 0.0, NULL, NULL, NULL, NULL);
00849 }
00850
00851 if(newOSiLReader == true)
00852 {
00853 delete osilreader;
00854 osilreader = NULL;
00855 }
00856 delete [] daX;
00857 delete [] daLambda;
00858 delete [] mdObjValues;
00859
00860 KTR_free( &kc);
00861 delete pOptProb;
00862
00863
00864
00865 }
00866 catch(const ErrorClass& eclass)
00867 {
00868 osresult->setGeneralMessage( eclass.errormsg);
00869 osresult->setGeneralStatusType( "error");
00870 osrl = osrlwriter->writeOSrL( osresult);
00871
00872 }
00873 }
00874
00875
00876 void KnitroSolver::dataEchoCheck()
00877 {
00878
00879 int i;
00880
00881
00882 cout << "This is problem: " << osinstance->getInstanceName() << endl;
00883 cout << "The problem source is: " << osinstance->getInstanceSource() << endl;
00884 cout << "The problem description is: " << osinstance->getInstanceDescription() << endl;
00885 cout << "number of variables = " << osinstance->getVariableNumber() << endl;
00886 cout << "number of Rows = " << osinstance->getConstraintNumber() << endl;
00887
00888
00889 if(osinstance->getVariableNumber() > 0)
00890 {
00891 for(i = 0; i < osinstance->getVariableNumber(); i++)
00892 {
00893 if(osinstance->getVariableNames() != NULL) cout << "variable Names " << osinstance->getVariableNames()[ i] << endl;
00894 if(osinstance->getVariableTypes() != NULL) cout << "variable Types " << osinstance->getVariableTypes()[ i] << endl;
00895 if(osinstance->getVariableLowerBounds() != NULL) cout << "variable Lower Bounds " << osinstance->getVariableLowerBounds()[ i] << endl;
00896 if(osinstance->getVariableUpperBounds() != NULL) cout << "variable Upper Bounds " << osinstance->getVariableUpperBounds()[i] << endl;
00897 }
00898 }
00899
00900
00901 if(osinstance->getVariableNumber() > 0 || osinstance->instanceData->objectives->obj != NULL || osinstance->instanceData->objectives->numberOfObjectives > 0)
00902 {
00903 if( osinstance->getObjectiveMaxOrMins()[0] == "min") cout << "problem is a minimization" << endl;
00904 else cout << "problem is a maximization" << endl;
00905 for(i = 0; i < osinstance->getVariableNumber(); i++)
00906 {
00907 cout << "OBJ COEFFICIENT = " << osinstance->getDenseObjectiveCoefficients()[0][i] << endl;
00908 }
00909 }
00910
00911 if(osinstance->getConstraintNumber() > 0)
00912 {
00913 for(i = 0; i < osinstance->getConstraintNumber(); i++)
00914 {
00915 if(osinstance->getConstraintNames() != NULL) cout << "row name = " << osinstance->getConstraintNames()[i] << endl;
00916 if(osinstance->getConstraintLowerBounds() != NULL) cout << "row lower bound = " << osinstance->getConstraintLowerBounds()[i] << endl;
00917 if(osinstance->getConstraintUpperBounds() != NULL) cout << "row upper bound = " << osinstance->getConstraintUpperBounds()[i] << endl;
00918 }
00919 }
00920
00921
00922 cout << endl;
00923 cout << "number of nonzeros = " << osinstance->getLinearConstraintCoefficientNumber() << endl;
00924 for(i = 0; i <= osinstance->getVariableNumber(); i++)
00925 {
00926 cout << "Start Value = " << osinstance->getLinearConstraintCoefficientsInColumnMajor()->starts[ i] << endl;
00927 }
00928 cout << endl;
00929 for(i = 0; i < osinstance->getLinearConstraintCoefficientNumber(); i++)
00930 {
00931 cout << "Index Value = " << osinstance->getLinearConstraintCoefficientsInColumnMajor()->indexes[i] << endl;
00932 cout << "Nonzero Value = " << osinstance->getLinearConstraintCoefficientsInColumnMajor()->values[i] << endl;
00933 }
00934
00935
00936 cout << "number of qterms = " << osinstance->getNumberOfQuadraticTerms() << endl;
00937 for(int i = 0; i < osinstance->getNumberOfQuadraticTerms(); i++)
00938 {
00939 cout << "Row Index = " << osinstance->getQuadraticTerms()->rowIndexes[i] << endl;
00940 cout << "Var Index 1 = " << osinstance->getQuadraticTerms()->varOneIndexes[ i] << endl;
00941 cout << "Var Index 2 = " << osinstance->getQuadraticTerms()->varTwoIndexes[ i] << endl;
00942 cout << "Coefficient = " << osinstance->getQuadraticTerms()->coefficients[ i] << endl;
00943 }
00944 }
00945
00946
00947 NlpProblemDef::~NlpProblemDef (void)
00948 {
00949
00950
00951 }
00952
00953
00954
00955