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;
105: PetscInitialize(&argc, &argv, (char *)0, help);
106: PetscOptionsGetBool(NULL, NULL, "-test_all", &testall, NULL);
107: PetscOptionsGetInt(NULL, NULL, "-prob_id", &prob_id, &flg);
108: if (!testall) {
109: if (!flg) {
110: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Problem number must be specified with -prob_id");
111: } else if ((prob_id < 1) || (prob_id > 53)) {
112: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Problem number must be between 1 and 53!");
113: } else {
114: PetscPrintf(PETSC_COMM_SELF, "Running problem %d\n", prob_id);
115: }
116: } else {
117: PetscPrintf(PETSC_COMM_SELF, "Running all problems\n");
118: }
120: PetscMatlabEngineCreate(PETSC_COMM_SELF, NULL, &user.mengine);
121: PetscMatlabEngineEvaluate(user.mengine, "TestingInitialize");
123: if (testall) {
124: i0 = 1;
125: imax = 53;
126: } else {
127: i0 = (int)prob_id;
128: imax = (int)prob_id;
129: }
131: for (i = i0; i <= imax; ++i) {
132: PetscPrintf(PETSC_COMM_SELF, "%d\n", i);
133: PetscMatlabEngineEvaluate(user.mengine, "np = %d; ProblemInitialize", i);
134: PetscMatlabEngineGetArray(user.mengine, 1, 1, &tmp, "n");
135: user.n = (int)tmp;
136: PetscMatlabEngineGetArray(user.mengine, 1, 1, &tmp, "m");
137: user.m = (int)tmp;
138: PetscMatlabEngineGetArray(user.mengine, 1, 1, &tmp, "nfmax");
139: user.nfmax = (int)tmp;
140: PetscMatlabEngineGetArray(user.mengine, 1, 1, &tmp, "npmax");
141: user.npmax = (int)tmp;
142: PetscMatlabEngineGetArray(user.mengine, 1, 1, &tmp, "delta");
143: user.delta = (double)tmp;
145: /* Ignore return code for now -- do not stop testing on inf or nan errors */
146: TaoPounders(&user);
148: PetscMatlabEngineEvaluate(user.mengine, "ProblemFinalize");
149: }
151: PetscMatlabEngineEvaluate(user.mengine, "TestingFinalize");
152: PetscMatlabEngineDestroy(&user.mengine);
153: PetscFinalize();
154: return 0;
155: }
157: /*TEST
159: build:
160: requires: matlab
162: test:
163: localrunfiles: more_wild_probs TestingInitialize.m TestingFinalize.m ProblemInitialize.m ProblemFinalize.m
164: args: -tao_smonitor -prob_id 5
166: TEST*/