Actual source code: matlab_ls_test.c

petsc-3.12.2 2019-11-22
Report Typos and Errors
  1: static char help[] = "TAO/Pounders Matlab Testing on the More'-Wild Benchmark Problems\n\
  2: The interface calls:\n\
  3:     TestingInitialize.m to initialize the problem set\n\
  4:     ProblemInitialize.m to initialize each instance\n\
  5:     ProblemFinalize.m to store the performance data for the instance solved\n\
  6:     TestingFinalize.m to store the entire set of performance data\n\
  7: \n\
  8: TestingPlot.m is called outside of TAO/Pounders to produce a performance profile\n\
  9: of the results compared to the Matlab fminsearch algorithm.\n";

 11:  #include <petsctao.h>
 12:  #include <petscmatlab.h>

 14: typedef struct {
 15:   PetscMatlabEngine mengine;

 17:   double delta;           /* Initial trust region radius */

 19:   int n;                  /* Number of inputs */
 20:   int m;                  /* Number of outputs */
 21:   int nfmax;              /* Maximum function evaluations */
 22:   int npmax;              /* Maximum interpolation points */
 23: } AppCtx;

 25: static PetscErrorCode EvaluateResidual(Tao tao, Vec X, Vec F, void *ptr)
 26: {
 27:   AppCtx         *user = (AppCtx *)ptr;
 28:   PetscErrorCode  ierr;

 31:   PetscObjectSetName((PetscObject)X,"X");
 32:   PetscMatlabEnginePut(user->mengine,(PetscObject)X);
 33:   PetscMatlabEngineEvaluate(user->mengine,"F = func(X);");
 34:   PetscObjectSetName((PetscObject)F,"F");
 35:   PetscMatlabEngineGet(user->mengine,(PetscObject)F);
 36:   return(0);
 37: }

 39: static PetscErrorCode EvaluateJacobian(Tao tao, Vec X, Mat J, Mat JPre, void *ptr)
 40: {
 41:   AppCtx         *user = (AppCtx *)ptr;

 45:   PetscObjectSetName((PetscObject)X,"X");
 46:   PetscMatlabEnginePut(user->mengine,(PetscObject)X);
 47:   PetscMatlabEngineEvaluate(user->mengine,"J = jac(X);");
 48:   PetscObjectSetName((PetscObject)J,"J");
 49:   PetscMatlabEngineGet(user->mengine,(PetscObject)J);
 50:   return(0);
 51: }

 53: static PetscErrorCode TaoPounders(AppCtx *user)
 54: {
 56:   Tao            tao;
 57:   Vec            X, F;
 58:   Mat            J;
 59:   char           buf[1024];


 63:   /* Set the values for the algorithm options we want to use */
 64:   sprintf(buf,"%d",user->npmax);
 65:   PetscOptionsSetValue(NULL,"-tao_pounders_npmax",buf);
 66:   sprintf(buf,"%5.4e",user->delta);
 67:   PetscOptionsSetValue(NULL,"-tao_pounders_delta",buf);

 69:   /* Create the TAO objects and set the type */
 70:   TaoCreate(PETSC_COMM_SELF,&tao);

 72:   /* Create starting point and initialize */
 73:   VecCreateSeq(PETSC_COMM_SELF,user->n,&X);
 74:   PetscObjectSetName((PetscObject)X,"X0");
 75:   PetscMatlabEngineGet(user->mengine,(PetscObject)X);
 76:   TaoSetInitialVector(tao,X);

 78:   /* Create residuals vector and set residual function */
 79:   VecCreateSeq(PETSC_COMM_SELF,user->m,&F);
 80:   PetscObjectSetName((PetscObject)F,"F");
 81:   TaoSetResidualRoutine(tao,F,EvaluateResidual,(void*)user);

 83:   /* Create Jacobian matrix and set residual Jacobian routine */
 84:   MatCreateSeqAIJ(PETSC_COMM_WORLD,user->m,user->n,user->n,NULL,&J);
 85:   PetscObjectSetName((PetscObject)J,"J");
 86:   TaoSetJacobianResidualRoutine(tao,J,J,EvaluateJacobian,(void*)user);

 88:   /* Solve the problem */
 89:   TaoSetType(tao,TAOPOUNDERS);
 90:   TaoSetMaximumFunctionEvaluations(tao,user->nfmax);
 91:   TaoSetFromOptions(tao);
 92:   TaoSolve(tao);

 94:   /* Finish the problem */
 95:   MatDestroy(&J);
 96:   VecDestroy(&X);
 97:   VecDestroy(&F);
 98:   TaoDestroy(&tao);
 99:   return(0);
100: }

102: int main(int argc, char **argv)
103: {
104:   AppCtx         user;
106:   PetscScalar    tmp;
107:   PetscInt       prob_id = 0;
108:   PetscBool      flg, testall = PETSC_FALSE;
109:   int            i, i0, imax;

111:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
112:   PetscOptionsGetBool(NULL,NULL,"-test_all",&testall,NULL);
113:   PetscOptionsGetInt(NULL,NULL,"-prob_id",&prob_id,&flg);
114:   if (!testall) {
115:     if (!flg) {
116:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Problem number must be specified with -prob_id");
117:     } else if ((prob_id < 1) || (prob_id > 53)) {
118:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Problem number must be between 1 and 53!");
119:     } else {
120:       PetscPrintf(PETSC_COMM_SELF,"Running problem %d\n",prob_id);
121:     }
122:   } else {
123:     PetscPrintf(PETSC_COMM_SELF,"Running all problems\n");
124:   }

126:   PetscMatlabEngineCreate(PETSC_COMM_SELF,NULL,&user.mengine);
127:   PetscMatlabEngineEvaluate(user.mengine,"TestingInitialize");

129:   if (testall) {
130:     i0 = 1;
131:     imax = 53;
132:   } else {
133:     i0 = (int)prob_id;
134:     imax = (int)prob_id;
135:   }

137:   for (i = i0; i <= imax; ++i) {
138:       PetscPrintf(PETSC_COMM_SELF,"%d\n",i);
139:       PetscMatlabEngineEvaluate(user.mengine,"np = %d; ProblemInitialize",i);
140:       PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"n");
141:       user.n = (int)tmp;
142:       PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"m");
143:       user.m = (int)tmp;
144:       PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"nfmax");
145:       user.nfmax = (int)tmp;
146:       PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"npmax");
147:       user.npmax = (int)tmp;
148:       PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"delta");
149:       user.delta = (double)tmp;

151:       /* Ignore return code for now -- do not stop testing on inf or nan errors */
152:       TaoPounders(&user);

154:       PetscMatlabEngineEvaluate(user.mengine,"ProblemFinalize");
155:     }

157:   PetscMatlabEngineEvaluate(user.mengine,"TestingFinalize");
158:   PetscMatlabEngineDestroy(&user.mengine);
159:   PetscFinalize();
160:   return ierr;
161: }

163: /*TEST

165:    build:
166:       requires: matlab

168:    test:
169:       localrunfiles: more_wild_probs TestingInitialize.m TestingFinalize.m ProblemInitialize.m ProblemFinalize.m
170:       args: -tao_smonitor -prob_id 5

172: TEST*/