/* Program usage: mpiexec -n 1 rosenbrock1 [-help] [all TAO options] */ /* Include "petsctao.h" so we can use TAO solvers. */ #include static char help[] = "This example demonstrates use of the TAO package to \n\ solve an unconstrained minimization problem on a single processor. We \n\ minimize the extended Rosenbrock function: \n\ sum_{i=0}^{n/2-1} (alpha*(x_{2i+1}-x_{2i}^2)^2 + (1-x_{2i})^2) \n\ or the chained Rosenbrock function:\n\ sum_{i=0}^{n-1} alpha*(x_{i+1} - x_i^2)^2 + (1 - x_i)^2\n"; /*T Concepts: TAO^Solving an unconstrained minimization problem Routines: TaoCreate(); Routines: TaoSetType(); TaoSetObjectiveAndGradient(); Routines: TaoSetHessian(); Routines: TaoSetSolution(); Routines: TaoSetFromOptions(); Routines: TaoSolve(); Routines: TaoDestroy(); Processors: 1 T*/ /* User-defined application context - contains data needed by the application-provided call-back routines that evaluate the function, gradient, and hessian. */ typedef struct { PetscInt n; /* dimension */ PetscReal alpha; /* condition parameter */ PetscBool chained; } AppCtx; /* -------------- User-defined routines ---------- */ PetscErrorCode FormFunctionGradient(Tao,Vec,PetscReal*,Vec,void*); PetscErrorCode FormHessian(Tao,Vec,Mat,Mat,void*); int main(int argc,char **argv) { PetscReal zero=0.0; Vec x; /* solution vector */ Mat H; Tao tao; /* Tao solver context */ PetscBool flg, test_lmvm = PETSC_FALSE; PetscMPIInt size; /* number of processes running */ AppCtx user; /* user-defined application context */ KSP ksp; PC pc; Mat M; Vec in, out, out2; PetscReal mult_solve_dist; /* Initialize TAO and PETSc */ CHKERRQ(PetscInitialize(&argc,&argv,(char*)0,help)); CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size)); PetscCheck(size == 1,PETSC_COMM_WORLD,PETSC_ERR_WRONG_MPI_SIZE,"Incorrect number of processors"); /* Initialize problem parameters */ user.n = 2; user.alpha = 99.0; user.chained = PETSC_FALSE; /* Check for command line arguments to override defaults */ CHKERRQ(PetscOptionsGetInt(NULL,NULL,"-n",&user.n,&flg)); CHKERRQ(PetscOptionsGetReal(NULL,NULL,"-alpha",&user.alpha,&flg)); CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-chained",&user.chained,&flg)); CHKERRQ(PetscOptionsGetBool(NULL,NULL,"-test_lmvm",&test_lmvm,&flg)); /* Allocate vectors for the solution and gradient */ CHKERRQ(VecCreateSeq(PETSC_COMM_SELF,user.n,&x)); CHKERRQ(MatCreateSeqBAIJ(PETSC_COMM_SELF,2,user.n,user.n,1,NULL,&H)); /* The TAO code begins here */ /* Create TAO solver with desired solution method */ CHKERRQ(TaoCreate(PETSC_COMM_SELF,&tao)); CHKERRQ(TaoSetType(tao,TAOLMVM)); /* Set solution vec and an initial guess */ CHKERRQ(VecSet(x, zero)); CHKERRQ(TaoSetSolution(tao,x)); /* Set routines for function, gradient, hessian evaluation */ CHKERRQ(TaoSetObjectiveAndGradient(tao,NULL,FormFunctionGradient,&user)); CHKERRQ(TaoSetHessian(tao,H,H,FormHessian,&user)); /* Test the LMVM matrix */ if (test_lmvm) { CHKERRQ(PetscOptionsSetValue(NULL, "-tao_type", "bqnktr")); } /* Check for TAO command line options */ CHKERRQ(TaoSetFromOptions(tao)); /* SOLVE THE APPLICATION */ CHKERRQ(TaoSolve(tao)); /* Test the LMVM matrix */ if (test_lmvm) { CHKERRQ(TaoGetKSP(tao, &ksp)); CHKERRQ(KSPGetPC(ksp, &pc)); CHKERRQ(PCLMVMGetMatLMVM(pc, &M)); CHKERRQ(VecDuplicate(x, &in)); CHKERRQ(VecDuplicate(x, &out)); CHKERRQ(VecDuplicate(x, &out2)); CHKERRQ(VecSet(in, 1.0)); CHKERRQ(MatMult(M, in, out)); CHKERRQ(MatSolve(M, out, out2)); CHKERRQ(VecAXPY(out2, -1.0, in)); CHKERRQ(VecNorm(out2, NORM_2, &mult_solve_dist)); if (mult_solve_dist < 1.e-11) { CHKERRQ(PetscPrintf(PetscObjectComm((PetscObject)tao), "error between LMVM MatMult and MatSolve: < 1.e-11\n")); } else if (mult_solve_dist < 1.e-6) { CHKERRQ(PetscPrintf(PetscObjectComm((PetscObject)tao), "error between LMVM MatMult and MatSolve: < 1.e-6\n")); } else { CHKERRQ(PetscPrintf(PetscObjectComm((PetscObject)tao), "error between LMVM MatMult and MatSolve: %e\n", (double)mult_solve_dist)); } CHKERRQ(VecDestroy(&in)); CHKERRQ(VecDestroy(&out)); CHKERRQ(VecDestroy(&out2)); } CHKERRQ(TaoDestroy(&tao)); CHKERRQ(VecDestroy(&x)); CHKERRQ(MatDestroy(&H)); CHKERRQ(PetscFinalize()); return 0; } /* -------------------------------------------------------------------- */ /* FormFunctionGradient - Evaluates the function, f(X), and gradient, G(X). Input Parameters: . tao - the Tao context . X - input vector . ptr - optional user-defined context, as set by TaoSetFunctionGradient() Output Parameters: . G - vector containing the newly evaluated gradient . f - function value Note: Some optimization methods ask for the function and the gradient evaluation at the same time. Evaluating both at once may be more efficient that evaluating each separately. */ PetscErrorCode FormFunctionGradient(Tao tao,Vec X,PetscReal *f, Vec G,void *ptr) { AppCtx *user = (AppCtx *) ptr; PetscInt i,nn=user->n/2; PetscReal ff=0,t1,t2,alpha=user->alpha; PetscScalar *g; const PetscScalar *x; PetscFunctionBeginUser; /* Get pointers to vector data */ CHKERRQ(VecGetArrayRead(X,&x)); CHKERRQ(VecGetArray(G,&g)); /* Compute G(X) */ if (user->chained) { g[0] = 0; for (i=0; in-1; i++) { t1 = x[i+1] - x[i]*x[i]; ff += PetscSqr(1 - x[i]) + alpha*t1*t1; g[i] += -2*(1 - x[i]) + 2*alpha*t1*(-2*x[i]); g[i+1] = 2*alpha*t1; } } else { for (i=0; ialpha; PetscReal v[2][2]; const PetscScalar *x; PetscBool assembled; PetscFunctionBeginUser; /* Zero existing matrix entries */ CHKERRQ(MatAssembled(H,&assembled)); if (assembled) CHKERRQ(MatZeroEntries(H)); /* Get a pointer to vector data */ CHKERRQ(VecGetArrayRead(X,&x)); /* Compute H(X) entries */ if (user->chained) { CHKERRQ(MatZeroEntries(H)); for (i=0; in-1; i++) { PetscScalar t1 = x[i+1] - x[i]*x[i]; v[0][0] = 2 + 2*alpha*(t1*(-2) - 2*x[i]); v[0][1] = 2*alpha*(-2*x[i]); v[1][0] = 2*alpha*(-2*x[i]); v[1][1] = 2*alpha*t1; ind[0] = i; ind[1] = i+1; CHKERRQ(MatSetValues(H,2,ind,2,ind,v[0],ADD_VALUES)); } } else { for (i=0; in/2; i++) { v[1][1] = 2*alpha; v[0][0] = -4*alpha*(x[2*i+1]-3*x[2*i]*x[2*i]) + 2; v[1][0] = v[0][1] = -4.0*alpha*x[2*i]; ind[0]=2*i; ind[1]=2*i+1; CHKERRQ(MatSetValues(H,2,ind,2,ind,v[0],INSERT_VALUES)); } } CHKERRQ(VecRestoreArrayRead(X,&x)); /* Assemble matrix */ CHKERRQ(MatAssemblyBegin(H,MAT_FINAL_ASSEMBLY)); CHKERRQ(MatAssemblyEnd(H,MAT_FINAL_ASSEMBLY)); CHKERRQ(PetscLogFlops(9.0*user->n/2.0)); PetscFunctionReturn(0); } /*TEST build: requires: !complex test: args: -tao_smonitor -tao_type nls -tao_gatol 1.e-4 requires: !single test: suffix: 2 args: -tao_smonitor -tao_type lmvm -tao_gatol 1.e-3 test: suffix: 3 args: -tao_smonitor -tao_type ntr -tao_gatol 1.e-4 requires: !single test: suffix: 4 args: -tao_smonitor -tao_type ntr -tao_mf_hessian -tao_ntr_pc_type none -tao_gatol 1.e-4 test: suffix: 5 args: -tao_smonitor -tao_type bntr -tao_gatol 1.e-4 test: suffix: 6 args: -tao_smonitor -tao_type bntl -tao_gatol 1.e-4 test: suffix: 7 args: -tao_smonitor -tao_type bnls -tao_gatol 1.e-4 test: suffix: 8 args: -tao_smonitor -tao_type bntr -tao_bnk_max_cg_its 3 -tao_gatol 1.e-4 test: suffix: 9 args: -tao_smonitor -tao_type bntl -tao_bnk_max_cg_its 3 -tao_gatol 1.e-4 test: suffix: 10 args: -tao_smonitor -tao_type bnls -tao_bnk_max_cg_its 3 -tao_gatol 1.e-4 test: suffix: 11 args: -test_lmvm -tao_max_it 10 -tao_bqnk_mat_type lmvmbroyden test: suffix: 12 args: -test_lmvm -tao_max_it 10 -tao_bqnk_mat_type lmvmbadbroyden test: suffix: 13 args: -test_lmvm -tao_max_it 10 -tao_bqnk_mat_type lmvmsymbroyden test: suffix: 14 args: -test_lmvm -tao_max_it 10 -tao_bqnk_mat_type lmvmbfgs test: suffix: 15 args: -test_lmvm -tao_max_it 10 -tao_bqnk_mat_type lmvmdfp test: suffix: 16 args: -test_lmvm -tao_max_it 10 -tao_bqnk_mat_type lmvmsr1 test: suffix: 17 args: -tao_smonitor -tao_gatol 1e-4 -tao_type bqnls test: suffix: 18 args: -tao_smonitor -tao_gatol 1e-4 -tao_type blmvm test: suffix: 19 args: -tao_smonitor -tao_gatol 1e-4 -tao_type bqnktr -tao_bqnk_mat_type lmvmsr1 test: suffix: 20 args: -tao_monitor -tao_gatol 1e-4 -tao_type blmvm -tao_ls_monitor test: suffix: 21 args: -test_lmvm -tao_max_it 10 -tao_bqnk_mat_type lmvmsymbadbroyden test: suffix: 22 args: -tao_max_it 1 -tao_converged_reason test: suffix: 23 args: -tao_max_funcs 0 -tao_converged_reason test: suffix: 24 args: -tao_gatol 10 -tao_converged_reason test: suffix: 25 args: -tao_grtol 10 -tao_converged_reason test: suffix: 26 args: -tao_gttol 10 -tao_converged_reason test: suffix: 27 args: -tao_steptol 10 -tao_converged_reason test: suffix: 28 args: -tao_fmin 10 -tao_converged_reason TEST*/