Actual source code: kt.c

      1: #include "src/bound/impls/kt/kt.h"
      2: #include "src/tao_impl.h"

      4: static int TaoSetDown_KT(TAO_SOLVER, void *);
      5: static int TaoMonitor_KT(TAO_SOLVER my_tao, void *solver);

      7: /* ---------------------------------------------------------- */
     10: int TaoSetUp_KT(TAO_SOLVER tao,void *solver)
     11: {
     12:   TAO_KT *kt = (TAO_KT *)solver;
     13:   TaoVec *xx;
     14:   TaoTruth flg;
     15:   int info;

     17:   TaoFunctionBegin;
     18:   info = TaoGetSolution(tao,&xx); CHKERRQ(info);

     20:   // Create new complementarity solver
     21:   info = TaoOptionString("-tao_kt_method",0,0,kt->comp_method,
     22:                          kt->comp_method,256,&flg);
     23:   CHKERRQ(info);
     24:   if (kt->csolver==0){
     25:     info = TaoCreateFull(kt->comp_method,"t",tao->comm, &kt->csolver);
     26:     CHKERRQ(info);
     27:   }
     28:   kt->ktapp = new TaoKTApplication(tao,kt->csolver);
     29:   kt->ktapp->SetItUp1(); CHKERRQ(info);
     30:   kt->ktapp->SetItUp2(); CHKERRQ(info);
     31:   kt->setupcalled=1;
     32:
     33:   TaoFunctionReturn(0);
     34: }

     36: /* ---------------------------------------------------------- */
     39: static int TaoSetDown_KT(TAO_SOLVER tao, void *solver)
     40: {
     41:   TAO_KT *kt = (TAO_KT *)solver;
     42:   int info;

     44:   TaoFunctionBegin;
     45:   info = TaoDestroy(kt->csolver); CHKERRQ(info);
     46:   info = TaoDestroyApplication(kt->ktapp); CHKERRQ(info);
     47:   kt->setupcalled=0;
     48:   kt->csolver=0;
     49:   kt->ktapp=0;
     50:   TaoFunctionReturn(0);
     51: }

     55: static int TaoMonitor_KT(TAO_SOLVER complementaritytao, void *solver)
     56: {
     57:   TaoKTApplication* ktapp= (TaoKTApplication*)solver;
     58:   TaoTerminateReason reason;
     59:   double f, fnorm, cnorm, xdiff;
     60:   int iterate, info;

     62:   TaoFunctionBegin;
     63:   info = TaoGetSolutionStatus(complementaritytao, &iterate, &f, &fnorm,
     64:                              &cnorm, &xdiff, &reason); CHKERRQ(info);
     65:   f=ktapp->func;
     66:   info = TaoMonitor(ktapp->orig,iterate,f,fnorm,cnorm,xdiff,&reason); CHKERRQ(info);
     67:   info = TaoSetTerminationReason(complementaritytao,reason); CHKERRQ(info);
     68:   TaoFunctionReturn(0);
     69: }

     71: /*------------------------------------------------------------*/
     74: static int TaoSolve_KT(TAO_SOLVER tao, void *solver)
     75: {
     76:   TAO_KT *kt = (TAO_KT *)solver;
     77:   int info;

     79:   TaoFunctionBegin;
     80:   info = TaoSolve(kt->csolver);  CHKERRQ(info);
     81:   TaoFunctionReturn(0);
     82: }

     84: /*------------------------------------------------------------*/
     87: static int TaoSetOptions_KT(TAO_SOLVER tao, void*solver)
     88: {
     89:   TAO_KT *kt = (TAO_KT *)solver;
     90:   TAO_SOLVER csolver=kt->csolver;
     91:   TaoTruth flg;
     92:   int info;

     94:   TaoFunctionBegin;
     95:   info = TaoOptionString("-tao_kt_method",
     96:                          "Set method for solving kt conditions",
     97:                          "TaoKTSetMethod",kt->comp_method,
     98:                          kt->comp_method,256,&flg);
     99:   CHKERRQ(info);

    101:   if (csolver==0){
    102:     info = TaoCreateFull(kt->comp_method,"t",tao->comm, &kt->csolver);
    103:     CHKERRQ(info);
    104:   }
    105:   csolver=kt->csolver;
    106:   if (csolver->setfromoptions) {
    107:     info = (*csolver->setfromoptions)(csolver,csolver->data); CHKERRQ(info);
    108:   }
    109:   TaoFunctionReturn(0);
    110: }

    112: /*------------------------------------------------------------*/
    115: static int TaoView_KT(TAO_SOLVER tao,void* solver)
    116: {
    117:   TAO_KT   *kt = (TAO_KT *)solver;
    118:   int      info;

    120:   TaoFunctionBegin;
    121:   info = TaoPrintString(tao,"  kt method=%s\n", kt->comp_method);
    122:          CHKERRQ(info);
    123:   TaoFunctionReturn(0);
    124: }

    129: int TaoCreate_KT(TAO_SOLVER tao)
    130: {
    131:   TAO_KT *kt;
    132:   int    info;

    134:   TaoFunctionBegin;

    136:   info = TaoNew(TAO_KT,&kt); CHKERRQ(info);

    138:   kt->csolver = 0;
    139:   kt->setupcalled=0;
    140:   info = TaoStrcpy(kt->comp_method, "tao_ssils"); CHKERRQ(info);

    142:   info=TaoSetTaoSolveRoutine(tao,TaoSolve_KT,(void*)kt); CHKERRQ(info);
    143:   info=TaoSetTaoSetUpDownRoutines(tao,TaoSetUp_KT,TaoSetDown_KT); CHKERRQ(info);
    144:   info=TaoSetTaoOptionsRoutine(tao,TaoSetOptions_KT); CHKERRQ(info);
    145:   info=TaoSetTaoViewRoutine(tao,TaoView_KT); CHKERRQ(info);

    147:   info = TaoSetMaximumIterates(tao,2000); CHKERRQ(info);
    148:   info = TaoSetMaximumFunctionEvaluations(tao,4000); CHKERRQ(info);

    150:   info = TaoSetTolerances(tao,0,0,0,0); CHKERRQ(info);
    151:   info = TaoSetGradientTolerances(tao,1.0e-12,0.0,0.0); CHKERRQ(info);

    153:   TaoFunctionReturn(0);
    154: }

    159: TaoKTApplication::TaoKTApplication(TAO_SOLVER outertao, TAO_SOLVER innertao)
    160: {
    161:   this->orig=outertao;
    162:   this->csolver=innertao;
    163:   return;
    164: }

    168: int TaoKTApplication::SetItUp1()
    169: {
    170:   int info;
    171:   TaoFunctionBegin;
    172:   info = TaoSetConvergenceTest(this->csolver, TAO_NULL, TAO_NULL); CHKERRQ(info);
    173:   info = TaoSetTolerances(this->csolver,0,0,0,0); CHKERRQ(info);
    174:   info = TaoSetGradientTolerances(this->csolver,0.0,0.0,0.0); CHKERRQ(info);
    175:   info = TaoSetMaximumIterates(this->csolver,0); CHKERRQ(info);
    176:   info = TaoSetMaximumFunctionEvaluations(this->csolver,0); CHKERRQ(info);
    177:   info = TaoSetFunctionLowerBound(this->csolver,0); CHKERRQ(info);
    178:   info = TaoSetMonitor(this->csolver, TaoMonitor_KT, (void*)this); CHKERRQ(info);
    179:   TaoFunctionReturn(0);
    180: }

    184: int TaoKTApplication::SetItUp2()
    185: {
    186:   int info;
    187:   TaoVec *x1,*x2;
    188:   TaoFunctionBegin;
    189:   info = TaoSetApplication(this->csolver,this); CHKERRQ(info);
    190:   info = TaoGetVariableBounds(this->csolver,&x1,&x2); CHKERRQ(info);
    191:   info = TaoSetVariableBounds(this->orig,x1,x2); CHKERRQ(info);
    192:   info = TaoGetStepDirectionVector(this->csolver,&x1); CHKERRQ(info);
    193:   info = TaoSetStepDirectionVector(this->orig,x1); CHKERRQ(info);
    194:   TaoFunctionReturn(0);
    195: }

    199: int TaoKTApplication::EvaluateConstraints(TaoVec *x, TaoVec *f)
    200: {
    201:   int info;
    202:   TaoFunctionBegin;
    203:   info = TaoComputeFunctionGradient(this->orig, x, &this->func, f);
    204:          CHKERRQ(info);
    205:   info = TaoSetLagrangianGradientVector(this->orig,f); CHKERRQ(info);
    206:   TaoFunctionReturn(0);
    207: }

    211: int TaoKTApplication::EvaluateJacobian(TaoVec *x, TaoMat *J)
    212: {
    213:   int info;
    214:   TaoFunctionBegin;
    215:   info = TaoComputeHessian(this->orig, x, J); CHKERRQ(info);
    216:   TaoFunctionReturn(0);
    217: }

    221: int TaoKTApplication::InitializeVariables(TaoVec *x)
    222: {
    223:   int info;
    224:   TaoApplication* theapp;
    225:   TaoFunctionBegin;
    226:   info = TaoGetApplication(this->orig, &theapp); CHKERRQ(info);
    227:   info = theapp->InitializeVariables(x); CHKERRQ(info);
    228:   TaoFunctionReturn(0);
    229: }

    233: int TaoKTApplication::GetVariableVector(TaoVec **x)
    234: {
    235:   int info;
    236:   TaoFunctionBegin;
    237:   info = TaoGetSolution(this->orig, x); CHKERRQ(info);
    238:   TaoFunctionReturn(0);
    239: }

    243: int TaoKTApplication::EvaluateVariableBounds(TaoVec *l, TaoVec *u)
    244: {
    245:   int info;
    246:   TaoFunctionBegin;
    247:   info = TaoEvaluateVariableBounds(this->orig, l, u); CHKERRQ(info);
    248:   TaoFunctionReturn(0);
    249: }

    253: int TaoKTApplication::GetJacobianMatrix(TaoMat **J)
    254: {
    255:   int info;
    256:   TaoFunctionBegin;
    257:   info = TaoGetHessian(this->orig, J); CHKERRQ(info);
    258:   TaoFunctionReturn(0);
    259: }

    263: int TaoKTApplication::GetLinearSolver(TaoMat *H, int stype, TaoLinearSolver **tksp) {
    264:   int info;
    265:   TaoFunctionBegin;
    266:   info = TaoCreateLinearSolver(this->orig, H, stype,tksp); CHKERRQ(info);
    267:   TaoFunctionReturn(0);
    268: }