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(TaoInt 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, TaoInt &n){
 62:   TaoInt nn;
 63:   int info;
 64:   double *xx;
 65:   TaoFunctionBegin;
 66:   info = this->taox->GetDoubles(&xx,&nn);CHKERRQ(info);
 67:   x=xx; n=nn;
 68:   info = this->taox->RestoreDoubles(&xx,&nn);  CHKERRQ(info);
 69:   TaoFunctionReturn(0);
 70: }

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

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


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


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


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

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


128: /*@C
129:    ComputeObjectiveAndGradient - Compute the objective function and its gradient.

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

136:    Output Parameters:

138: .  f - function value
139: .  g - gradient vector

141:    Level: beginner

143: @*/
144: // virtual int TaoABCApplication::ComputeObjectiveAndGradient(double*x,TaoInt n,double*f,double *g){}


149: /*@C
150:    StartingPoint - Define the starting point for the solver.

152:    Collective on TAO_SOLVER

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

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

161:    Level: beginner

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


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

188: int TaoABCApplication::EvaluateVariableBounds(TaoVec *txl, TaoVec *txu){
189:   TaoInt n;
190:   int info;
191:   double *xl,*xu;
192:   TaoVecDoubleArray *xxll=(TaoVecDoubleArray*)txl, *xxuu=(TaoVecDoubleArray*)txu;
193:   TaoFunctionBegin;
194:   
195:   info = xxll->GetDoubles(&xl,&n);CHKERRQ(info);
196:   info = xxuu->GetDoubles(&xu,&n);CHKERRQ(info);
197:   info = this->ComputeVariableBounds(xl, xu,n);CHKERRQ(info);
198:   info = xxll->RestoreDoubles(&xu,&n);CHKERRQ(info);
199:   info = xxuu->RestoreDoubles(&xl,&n);  CHKERRQ(info);

201:   TaoFunctionReturn(0);
202: }


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

210:    Collective on TAO_SOLVER

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

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

220:    Level: beginner

222: @*/
223: int TaoABCApplication::ComputeVariableBounds(double *xl, double *xu, TaoInt n){
224:   TaoInt  i;
225:   TaoFunctionBegin;
226:   for (i=0;i<n;i++){ 
227:     xl[i]=TAO_NINFINITY;
228:     xu[i]=TAO_INFINITY;
229:   }
230:   TaoFunctionReturn(0);
231: }