Actual source code: taoabcapp.c

      1: #include "src/tao_impl.h"     /*I  "tao_solver.h"  I*/
     2:  #include taoabcapp.h
     3:  #include src/vector/tvecdouble.h

      7: TaoABCApplication::TaoABCApplication(){
      8:   this->taox=0;
      9:   return;
     10: }

     15: TaoABCApplication::~TaoABCApplication(){
     16:   int info;
     17:   info = TaoVecDestroy(this->taox);
     18:   if (info) return;
     19:   return;
     20: }

     25: /* @C
     26:    SetDimension - Set the number of variables in this application.

     28:    Input Parameters:
     29: .  n - the number of variables

     31:    Note:  This method should be called only once and be called before
     32:    the application is set to the solver.

     34:    Note: This routine creates a variables vector.  Applications derived
     35:    from this object may create a TaoVec object and set the 'taox' field
     36:    another way.

     38:    Level: beginner

     40: @ */
     41: int TaoABCApplication::SetNumberOfVariables(int n){
     42:   TaoFunctionBegin;
     43:   this->taox = new TaoVecDoubleArray(n);
     44:   TaoFunctionReturn(0);
     45: }

     49: /* @C
     50:    GetSolutionAndGradient - Get the solution and and gradient arrays.

     52:    Output Parameters:

     54: +  n - the length of the arrays, which equals the number of variables
     55: .  x - variables vector
     56: -  g - gradient vector

     58:    Level: beginner

     60: @ */
     61: int TaoABCApplication::GetSolution(double* &x, int &n){
     62:   int info, nn;
     63:   double *xx;
     64:   TaoFunctionBegin;
     65:   info = this->taox->GetDoubles(&xx,&nn);CHKERRQ(info);
     66:   x=xx; n=nn;
     67:   info = this->taox->RestoreDoubles(&xx,&nn);  CHKERRQ(info);
     68:   TaoFunctionReturn(0);
     69: }

     73: int TaoABCApplication::GetVariableVector(TaoVec **xx){

     75:   TaoFunctionBegin;
     76:   *xx= this->taox;
     77:   TaoFunctionReturn(0);
     78: }

     83: int TaoABCApplication::EvaluateObjectiveFunction(TaoVec *xx, double *ff){
     84:   TaoFunctionBegin;
     85:   TaoFunctionReturn(1);
     86: }

     91: int TaoABCApplication::EvaluateGradient(TaoVec *tx, TaoVec *tg){
     92:   int   info,n;
     93:   double *xptr,*gptr;
     94:   double ff;
     95:   TaoVecDoubleArray *xx=(TaoVecDoubleArray*)tx, *gg=(TaoVecDoubleArray*)tg;
     96:   TaoFunctionBegin;
     97:   info = xx->GetDoubles(&xptr,&n);CHKERRQ(info);
     98:   info = gg->GetDoubles(&gptr,&n);CHKERRQ(info);
     99:   info = this->ComputeObjectiveAndGradient(xptr,n,&ff,gptr);CHKERRQ(info);
    100:   info = gg->RestoreDoubles(&gptr,&n);CHKERRQ(info);
    101:   info = xx->RestoreDoubles(&xptr,&n);  CHKERRQ(info);
    102:   TaoFunctionReturn(0);
    103: }

    108: int TaoABCApplication::EvaluateObjectiveAndGradient(TaoVec *tx, double *ff, TaoVec *tg){
    109:   int     info,n;
    110:   double *xptr, *gptr;
    111:   TaoVecDoubleArray *xx=(TaoVecDoubleArray*)tx, *gg=(TaoVecDoubleArray*)tg;

    113:   TaoFunctionBegin;
    114:   info = xx->GetDoubles(&xptr,&n);CHKERRQ(info);
    115:   info = gg->GetDoubles(&gptr,&n);CHKERRQ(info);
    116:   info = this->ComputeObjectiveAndGradient(xptr,n,ff,gptr);CHKERRQ(info);
    117:   info = gg->RestoreDoubles(&gptr,&n);CHKERRQ(info);
    118:   info = xx->RestoreDoubles(&xptr,&n);  CHKERRQ(info);
    119:   TaoFunctionReturn(0);
    120: }

    125: /*@C
    126:    ComputeObjectiveAndGradient - Compute the objective function and its gradient.

    128:    Input Parameters:
    129: .  x - an array with the point at which the function and gradient should be evalutated.
    130: .  g - an array that will contain the gradient vector.
    131: +  n - the length of the arrays, which equals the number of variables

    133:    Output Parameters:

    135: .  f - function value
    136: .  g - gradient vector

    138:    Level: beginner

    140: @*/
    141: // virtual int TaoABCApplication::ComputeObjectiveAndGradient(double*x,int n,double*f,double *g){}

    146: /*@C
    147:    StartingPoint - Define the starting point for the solver.

    149:    Collective on TAO_SOLVER

    151:    Input Parameters:
    152: +  x - vector to store initial variables
    153: -  n - length of variable vector array

    155:    Note: This virtual method should be implemented in the application
    156:    if a starting point other than 0 is desired.

    158:    Level: beginner

    160: @*/
    161: int TaoABCApplication::StartingPoint(double *x, int n){
    162:   int  i;
    163:   TaoFunctionBegin;
    164:   for (i=0;i<n;i++) x[i]=0.0;
    165:   TaoFunctionReturn(0);
    166: }

    171: int TaoABCApplication::InitializeVariables(TaoVec *tx){
    172:   int info,n;
    173:   double *xptr;
    174:   TaoVecDoubleArray *xx=(TaoVecDoubleArray*)tx;
    175:   TaoFunctionBegin;
    176:   info = xx->GetDoubles(&xptr,&n);CHKERRQ(info);
    177:   info = this->StartingPoint(xptr,n);CHKERRQ(info);
    178:   info = xx->RestoreDoubles(&xptr,&n);CHKERRQ(info);
    179:   TaoFunctionReturn(0);
    180: }

    184: int TaoABCApplication::EvaluateVariableBounds(TaoVec *txl, TaoVec *txu){
    185:   int info,n;
    186:   double *xl,*xu;
    187:   TaoVecDoubleArray *xxll=(TaoVecDoubleArray*)txl, *xxuu=(TaoVecDoubleArray*)txu;
    188:   TaoFunctionBegin;
    189:
    190:   info = xxll->GetDoubles(&xl,&n);CHKERRQ(info);
    191:   info = xxuu->GetDoubles(&xu,&n);CHKERRQ(info);
    192:   info = this->ComputeVariableBounds(xl, xu,n);CHKERRQ(info);
    193:   info = xxll->RestoreDoubles(&xu,&n);CHKERRQ(info);
    194:   info = xxuu->RestoreDoubles(&xl,&n);  CHKERRQ(info);

    196:   TaoFunctionReturn(0);
    197: }

    202: /*@C
    203:    StartingPoint - Define the lower and upper bounds on the variables.

    205:    Collective on TAO_SOLVER

    207:    Input Parameters:
    208: +  xl - array to store lower bounds
    209: .  xu - array to store upper bounds
    210: -  n - length of these arrays, which equals the number of variables

    212:    Note:  This virtual method should be implemented by the application
    213:    if there are lower or upper bounds on the variables.

    215:    Level: beginner

    217: @*/
    218: int TaoABCApplication::ComputeVariableBounds(double *xl, double *xu, int n){
    219:   int  i;
    220:   TaoFunctionBegin;
    221:   for (i=0;i<n;i++){
    222:     xl[i]=TAO_NINFINITY;
    223:     xu[i]=TAO_INFINITY;
    224:   }
    225:   TaoFunctionReturn(0);
    226: }