Actual source code: cg.c
1: /*$Id: cg.c 1.94 05/02/11 14:10:19-06:00 benson@rockies.mcs.anl.gov $*/
3: #include "cg.h" /*I "tao_solver.h" I*/
5: /* ---------------------------------------------------------- */
8: int TaoSetUp_CG(TAO_SOLVER tao,void *solver){
10: TAO_CG *cg = (TAO_CG *) solver;
11: TaoVec *xx;
12: int info;
14: TaoFunctionBegin;
15:
16: info = TaoGetSolution(tao,&xx);CHKERRQ(info);
17:
18: info = xx->Clone(&cg->G); CHKERRQ(info);
19: info = xx->Clone(&cg->Work); CHKERRQ(info);
20: info = xx->Clone(&cg->DX); CHKERRQ(info);
21: info = xx->Clone(&cg->Gprev); CHKERRQ(info);
23: info = TaoSetLagrangianGradientVector(tao,cg->G);CHKERRQ(info);
24: info = TaoSetStepDirectionVector(tao,cg->DX);CHKERRQ(info);
26: TaoFunctionReturn(0);
27: }
29: /* ---------------------------------------------------------- */
32: int TaoDestroy_CG(TAO_SOLVER tao, void *solver)
33: {
34: TAO_CG *cg = (TAO_CG *) solver;
35: int info;
37: TaoFunctionBegin;
39: info = TaoVecDestroy(cg->Work);CHKERRQ(info);
40: info = TaoVecDestroy(cg->DX);CHKERRQ(info);
41: info = TaoVecDestroy(cg->Gprev);CHKERRQ(info);
42: info = TaoVecDestroy(cg->G);CHKERRQ(info);
44: info = TaoSetLagrangianGradientVector(tao,0);CHKERRQ(info);
45: info = TaoSetStepDirectionVector(tao,0);CHKERRQ(info);
47: TaoFunctionReturn(0);
48: }
51: /*------------------------------------------------------------*/
54: int TaoSetOptions_CG(TAO_SOLVER tao, void*solver)
55: {
56: TAO_CG *cg = (TAO_CG *)solver;
57: double dtmp;
58: int info;
59: TaoTruth flg;
61: TaoFunctionBegin;
63: info = TaoOptionsHead("Nonlinear Conjugate Gradient method for unconstrained optimization");CHKERRQ(info);
65: info = TaoOptionDouble("-tao_cg_restart","Restart method based on orthogonality of successive gradients","TaoCGSetRestartTol",cg->eta,&dtmp, &flg);CHKERRQ(info);
66: if (flg){
67: info =TaoCGSetRestartTol(tao,dtmp);
68: }
70: info = TaoLineSearchSetFromOptions(tao);CHKERRQ(info);
71: info = TaoOptionsTail();CHKERRQ(info);
73: TaoFunctionReturn(0);
74: }
77: /*------------------------------------------------------------*/
80: int TaoView_CG(TAO_SOLVER tao,void* solver)
81: {
82: TAO_CG *cg = (TAO_CG *)solver;
83: int info;
85: TaoFunctionBegin;
87: info = TaoPrintInt(tao," cg restarts=%d,",cg->restarts);CHKERRQ(info);
88: info = TaoPrintDouble(tao," tao_cg_restart=%g\n",cg->eta);CHKERRQ(info);
89: info = TaoLineSearchView(tao);CHKERRQ(info);
91: TaoFunctionReturn(0);
92: }
95: /* ---------------------------------------------------------- */
98: /*@C
99: TaoCGSetRestartTol - Set the nonlinear conjugate gradient restart
100: tolerance. The algorithm restarts when the gradient at the current
101: point, g(x^k), and the gradient of the previous point, g^{k-1},
102: satisfy the following inequality:
103: | g(x^k)^T g(x^{k-1}) | / g(x^k)^T g(x^k) > eta.
105: Collective on TAO_SOLVER
107: Input Parameters:
108: + tao - the TAO_SOLVER context
109: - eta - the number of previous points and gradients to be used.
111: Output Parameter:
113: Level: intermediate
115: Options Database Keys:
116: . -tao_cg_restart <eta> - sets the restart tolerance
118: @*/
119: int TaoCGSetRestartTol(TAO_SOLVER tao,double eta)
120: {
121: TAO_CG *cg;
122: int info;
124: TaoFunctionBegin;
125: info = TaoGetSolverContext(tao,"tao_cg_fr",(void**)&cg); CHKERRQ(info);
126: if (!cg){
127: info = TaoGetSolverContext(tao,"tao_cg_pr",(void**)&cg); CHKERRQ(info);
128: }
129: if (!cg){
130: info = TaoGetSolverContext(tao,"tao_cg_prp",(void**)&cg); CHKERRQ(info);
131: }
132: if (cg){
133: cg->eta=TaoMax(eta,0.0);
134: }
135: TaoFunctionReturn(0);
136: }