Actual source code: matlab_ls_test.c
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;
29: PetscObjectSetName((PetscObject)X,"X");
30: PetscMatlabEnginePut(user->mengine,(PetscObject)X);
31: PetscMatlabEngineEvaluate(user->mengine,"F = func(X);");
32: PetscObjectSetName((PetscObject)F,"F");
33: PetscMatlabEngineGet(user->mengine,(PetscObject)F);
34: return 0;
35: }
37: static PetscErrorCode EvaluateJacobian(Tao tao, Vec X, Mat J, Mat JPre, void *ptr)
38: {
39: AppCtx *user = (AppCtx *)ptr;
41: PetscObjectSetName((PetscObject)X,"X");
42: PetscMatlabEnginePut(user->mengine,(PetscObject)X);
43: PetscMatlabEngineEvaluate(user->mengine,"J = jac(X);");
44: PetscObjectSetName((PetscObject)J,"J");
45: PetscMatlabEngineGet(user->mengine,(PetscObject)J);
46: return 0;
47: }
49: static PetscErrorCode TaoPounders(AppCtx *user)
50: {
51: Tao tao;
52: Vec X, F;
53: Mat J;
54: char buf[1024];
57: /* Set the values for the algorithm options we want to use */
58: sprintf(buf,"%d",user->npmax);
59: PetscOptionsSetValue(NULL,"-tao_pounders_npmax",buf);
60: sprintf(buf,"%5.4e",user->delta);
61: PetscOptionsSetValue(NULL,"-tao_pounders_delta",buf);
63: /* Create the TAO objects and set the type */
64: TaoCreate(PETSC_COMM_SELF,&tao);
66: /* Create starting point and initialize */
67: VecCreateSeq(PETSC_COMM_SELF,user->n,&X);
68: PetscObjectSetName((PetscObject)X,"X0");
69: PetscMatlabEngineGet(user->mengine,(PetscObject)X);
70: TaoSetSolution(tao,X);
72: /* Create residuals vector and set residual function */
73: VecCreateSeq(PETSC_COMM_SELF,user->m,&F);
74: PetscObjectSetName((PetscObject)F,"F");
75: TaoSetResidualRoutine(tao,F,EvaluateResidual,(void*)user);
77: /* Create Jacobian matrix and set residual Jacobian routine */
78: MatCreateSeqAIJ(PETSC_COMM_WORLD,user->m,user->n,user->n,NULL,&J);
79: PetscObjectSetName((PetscObject)J,"J");
80: TaoSetJacobianResidualRoutine(tao,J,J,EvaluateJacobian,(void*)user);
82: /* Solve the problem */
83: TaoSetType(tao,TAOPOUNDERS);
84: TaoSetMaximumFunctionEvaluations(tao,user->nfmax);
85: TaoSetFromOptions(tao);
86: TaoSolve(tao);
88: /* Finish the problem */
89: MatDestroy(&J);
90: VecDestroy(&X);
91: VecDestroy(&F);
92: TaoDestroy(&tao);
93: return 0;
94: }
96: int main(int argc, char **argv)
97: {
98: AppCtx user;
99: PetscScalar tmp;
100: PetscInt prob_id = 0;
101: PetscBool flg, testall = PETSC_FALSE;
102: int i, i0, imax;
104: PetscInitialize(&argc,&argv,(char*)0,help);
105: PetscOptionsGetBool(NULL,NULL,"-test_all",&testall,NULL);
106: PetscOptionsGetInt(NULL,NULL,"-prob_id",&prob_id,&flg);
107: if (!testall) {
108: if (!flg) {
109: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Problem number must be specified with -prob_id");
110: } else if ((prob_id < 1) || (prob_id > 53)) {
111: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Problem number must be between 1 and 53!");
112: } else {
113: PetscPrintf(PETSC_COMM_SELF,"Running problem %d\n",prob_id);
114: }
115: } else {
116: PetscPrintf(PETSC_COMM_SELF,"Running all problems\n");
117: }
119: PetscMatlabEngineCreate(PETSC_COMM_SELF,NULL,&user.mengine);
120: PetscMatlabEngineEvaluate(user.mengine,"TestingInitialize");
122: if (testall) {
123: i0 = 1;
124: imax = 53;
125: } else {
126: i0 = (int)prob_id;
127: imax = (int)prob_id;
128: }
130: for (i = i0; i <= imax; ++i) {
131: PetscPrintf(PETSC_COMM_SELF,"%d\n",i);
132: PetscMatlabEngineEvaluate(user.mengine,"np = %d; ProblemInitialize",i);
133: PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"n");
134: user.n = (int)tmp;
135: PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"m");
136: user.m = (int)tmp;
137: PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"nfmax");
138: user.nfmax = (int)tmp;
139: PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"npmax");
140: user.npmax = (int)tmp;
141: PetscMatlabEngineGetArray(user.mengine,1,1,&tmp,"delta");
142: user.delta = (double)tmp;
144: /* Ignore return code for now -- do not stop testing on inf or nan errors */
145: TaoPounders(&user);
147: PetscMatlabEngineEvaluate(user.mengine,"ProblemFinalize");
148: }
150: PetscMatlabEngineEvaluate(user.mengine,"TestingFinalize");
151: PetscMatlabEngineDestroy(&user.mengine);
152: PetscFinalize();
153: return 0;
154: }
156: /*TEST
158: build:
159: requires: matlab_engine
161: test:
162: localrunfiles: more_wild_probs TestingInitialize.m TestingFinalize.m ProblemInitialize.m ProblemFinalize.m
163: args: -tao_smonitor -prob_id 5
165: TEST*/