Actual source code: taofloatapp.c

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

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


 15: TaoABCFloatApplication::~TaoABCFloatApplication(){
 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 TaoABCFloatApplication::SetNumberOfVariables(int n){
 42:   TaoFunctionBegin;
 43:   this->taox = new TaoVecFloatArray(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 TaoABCFloatApplication::GetSolutionAndGradient(float* &x, float* &g, int &n){
 62:   int info, nn;
 63:   float *xx;
 64:   TaoFunctionBegin;
 65:   info = this->taox->GetFloats(&xx,&nn);CHKERRQ(info);
 66:   x=xx; n=nn;
 67:   g=0;
 68:   info = this->taox->RestoreFloats(&xx,&nn);  CHKERRQ(info);
 69:   TaoFunctionReturn(0);
 70: }

 74: int TaoABCFloatApplication::GetVariableVector(TaoVec **xx){

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


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


 92: int TaoABCFloatApplication::EvaluateGradient(TaoVec *tx, TaoVec *tg){
 93:   int   info,n;
 94:   float *xptr,*gptr;
 95:   float ff;
 96:   TaoVecFloatArray* xx =  (TaoVecFloatArray*)(tx);
 97:   TaoVecFloatArray* gg =  (TaoVecFloatArray*)(tg);
 98:   TaoFunctionBegin;
 99:   info = xx->GetFloats(&xptr,&n);CHKERRQ(info);
100:   info = gg->GetFloats(&gptr,&n);CHKERRQ(info);
101:   info = this->ComputeObjectiveAndGradient(xptr,n,&ff,gptr);CHKERRQ(info);
102:   info = gg->RestoreFloats(&gptr,&n);CHKERRQ(info);
103:   info = xx->RestoreFloats(&xptr,&n);  CHKERRQ(info);
104:   TaoFunctionReturn(0);
105: }


110: int TaoABCFloatApplication::EvaluateObjectiveAndGradient(TaoVec *tx, double *ff, TaoVec *tg){
111:   int     info,n;
112:   float *xptr, *gptr;
113:   float f;
114:   TaoVecFloatArray* xx =  (TaoVecFloatArray*)(tx);
115:   TaoVecFloatArray* gg =  (TaoVecFloatArray*)(tg);

117:   TaoFunctionBegin;
118:   info = xx->GetFloats(&xptr,&n);CHKERRQ(info);
119:   info = gg->GetFloats(&gptr,&n);CHKERRQ(info);
120:   info = this->ComputeObjectiveAndGradient(xptr,n,&f,gptr);CHKERRQ(info);
121:   info = gg->RestoreFloats(&gptr,&n);CHKERRQ(info);
122:   info = xx->RestoreFloats(&xptr,&n);  CHKERRQ(info);
123:   *ff=(double)f;
124:   TaoFunctionReturn(0);
125: }


130: /*@C
131:    ComputeObjectiveAndGradient - Compute the objective function and its gradient.

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

138:    Output Parameters:

140: .  f - function value
141: .  g - gradient vector

143:    Level: beginner

145: @*/
146: // virtual int TaoABCFloatApplication::ComputeObjectiveAndGradient(float*x,int n,float*f,float *g){}


151: /*@C
152:    StartingPoint - Define the starting point for the solver.

154:    Collective on TAO_SOLVER

156:    Input Parameters:
157: +  x - vector to store initial variables
158: -  n - length of variable vector array

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

163:    Level: beginner

165: @*/
166: int TaoABCFloatApplication::StartingPoint(float *x, int n){
167:   int  i;
168:   TaoFunctionBegin;
169:   for (i=0;i<n;i++) x[i]=0.0;
170:   TaoFunctionReturn(0);
171: }


176: int TaoABCFloatApplication::InitializeVariables(TaoVec *tx){
177:   int info,n;
178:   float *xptr;
179:   TaoVecFloatArray* xx =  (TaoVecFloatArray*)(tx);
180:   TaoFunctionBegin;
181:   info = xx->GetFloats(&xptr,&n);CHKERRQ(info);
182:   info = this->StartingPoint(xptr,n);CHKERRQ(info);
183:   info = xx->RestoreFloats(&xptr,&n);CHKERRQ(info);
184:   TaoFunctionReturn(0);
185: }

189: int TaoABCFloatApplication::EvaluateVariableBounds(TaoVec *txl, TaoVec *txu){
190:   int info,n;
191:   float *xl,*xu;
192:   TaoVecFloatArray* xxll =  (TaoVecFloatArray*)(txl);
193:   TaoVecFloatArray* xxuu =  (TaoVecFloatArray*)(txu);

195:   TaoFunctionBegin;
196: 
197:   info = xxll->GetFloats(&xl,&n);CHKERRQ(info);
198:   info = xxuu->GetFloats(&xu,&n);CHKERRQ(info);
199:   info = this->ComputeVariableBounds(xl, xu,n);CHKERRQ(info);
200:   info = xxuu->RestoreFloats(&xu,&n);CHKERRQ(info);
201:   info = xxll->RestoreFloats(&xl,&n);  CHKERRQ(info);
202: 
203:   TaoFunctionReturn(0);
204: }


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

212:    Collective on TAO_SOLVER

214:    Input Parameters:
215: +  xl - array to store lower bounds
216: .  xu - array to store upper bounds
217: -  n - length of these arrays, which equals the number of variables

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

222:    Level: beginner

224: @*/
225: int TaoABCFloatApplication::ComputeVariableBounds(float *xl, float *xu, int n){
226:   int  i;
227:   TaoFunctionBegin;
228:   for (i=0;i<n;i++){
229:     xl[i]=TAO_NINFINITY;
230:     xu[i]=TAO_INFINITY;
231:   }
232:   TaoFunctionReturn(0);
233: }