Actual source code: bnls.c
1: /*$Id: s.bnls.c 1.128 02/08/16 18:01:18-05:00 benson@rockies.mcs.anl.gov $*/
3: #include "bnls.h" /*I "tao_solver.h" I*/
9: static int TaoSolve_BNLS(TAO_SOLVER tao, void*solver){
11: TAO_BNLS *bnls = (TAO_BNLS *)solver;
12: int info,lsflag,iter=0;
13: TaoTerminateReason reason=TAO_CONTINUE_ITERATING;
14: double f,f_full,gnorm,gdx,stepsize=1.0;
15: TaoTruth success;
16: TaoVec *XU, *XL;
17: TaoVec *X, *G=bnls->G, *PG=bnls->PG;
18: TaoVec *R=bnls->R, *DXFree=bnls->DXFree;
19: TaoVec *DX=bnls->DX, *Work=bnls->Work;
20: TaoMat *H, *Hsub=bnls->Hsub;
21: TaoIndexSet *FreeVariables = bnls->FreeVariables;
23: TaoFunctionBegin;
25: /* Check if upper bound greater than lower bound. */
26: info = TaoGetSolution(tao,&X);CHKERRQ(info); bnls->X=X;
27: info = TaoGetVariableBounds(tao,&XL,&XU);CHKERRQ(info);
28: info = TaoEvaluateVariableBounds(tao,XL,XU); CHKERRQ(info);
29: info = TaoGetHessian(tao,&H);CHKERRQ(info); bnls->H=H;
31: /* Project the current point onto the feasible set */
32: info = X->Median(XL,X,XU); CHKERRQ(info);
33:
34: info = TaoComputeMeritFunctionGradient(tao,X,&f,G);CHKERRQ(info);
35:
36: while (reason==TAO_CONTINUE_ITERATING){
37:
38: /* Project the gradient and calculate the norm */
39: info = PG->BoundGradientProjection(G,XL,X,XU);CHKERRQ(info);
40: info = PG->Norm2(&gnorm); CHKERRQ(info);
41:
42: info = TaoMonitor(tao,iter++,f,gnorm,0.0,stepsize,&reason);
43: CHKERRQ(info);
44: if (reason!=TAO_CONTINUE_ITERATING) break;
46: info = FreeVariables->WhichEqual(PG,G); CHKERRQ(info);
48: info = TaoComputeHessian(tao,X,H);CHKERRQ(info);
49:
50: /* Create a reduced linear system */
52: info = R->SetReducedVec(G,FreeVariables);CHKERRQ(info);
53: info = R->Negate();CHKERRQ(info);
55: info = DXFree->SetReducedVec(DX,FreeVariables);CHKERRQ(info);
56: info = DXFree->SetToZero(); CHKERRQ(info);
57:
58: info = Hsub->SetReducedMatrix(H,FreeVariables,FreeVariables);CHKERRQ(info);
60: bnls->gamma_factor /= 2;
61: success = TAO_FALSE;
63: while (success==TAO_FALSE) {
64:
65: /* Approximately solve the reduced linear system */
66: info = TaoPreLinearSolve(tao,Hsub);CHKERRQ(info);
67: info = TaoLinearSolve(tao,Hsub,R,DXFree,&success);CHKERRQ(info);
69: info = DX->SetToZero(); CHKERRQ(info);
70: info = DX->ReducedXPY(DXFree,FreeVariables);CHKERRQ(info);
71: info = DX->Dot(G,&gdx); CHKERRQ(info);
73: if (gdx>=0 || success==TAO_FALSE) { /* Modify diagonal of Hessian if not a descent direction */
74: bnls->gamma_factor *= 2;
75: bnls->gamma = bnls->gamma_factor*(gnorm);
76: #if !defined(PETSC_USE_COMPLEX)
77: info=PetscInfo2(tao,"TaoSolve_NLS: modify diagonal (assume same nonzero structure), gamma_factor=%g, gamma=%g\n",bnls->gamma_factor,bnls->gamma);
78: CHKERRQ(info);
79: #else
80: info=PetscInfo3(tao,"TaoSolve_NLS: modify diagonal (asuume same nonzero structure), gamma_factor=%g, gamma=%g\n",
81: bnls->gamma_factor,PetscReal(bnls->gamma));CHKERRQ(info);
82: #endif
83: info = Hsub->ShiftDiagonal(bnls->gamma);CHKERRQ(info);
84: success = TAO_FALSE;
85:
86: } else {
87: success = TAO_TRUE;
88: }
90: }
91:
92: stepsize=1.0;
93: info = TaoLineSearchApply(tao,X,G,DX,Work,
94: &f,&f_full,&stepsize,&lsflag);
95: CHKERRQ(info);
97:
98: } /* END MAIN LOOP */
100: TaoFunctionReturn(0);
101: }
104: /*------------------------------------------------------------*/
107: static int TaoSetDown_BNLS(TAO_SOLVER tao, void*solver)
108: {
109: TAO_BNLS *bnls = (TAO_BNLS *)solver;
110: int info;
111: /* Free allocated memory in BNLS structure */
112: TaoFunctionBegin;
113:
114: info = TaoVecDestroy(bnls->DX);CHKERRQ(info);bnls->DX=0;
115: info = TaoVecDestroy(bnls->Work);CHKERRQ(info);
116: info = TaoVecDestroy(bnls->DXFree);CHKERRQ(info);
117: info = TaoVecDestroy(bnls->R);CHKERRQ(info);
118: info = TaoVecDestroy(bnls->G);CHKERRQ(info);
119: info = TaoVecDestroy(bnls->PG);CHKERRQ(info);
120: info = TaoVecDestroy(bnls->XL);CHKERRQ(info);
121: info = TaoVecDestroy(bnls->XU);CHKERRQ(info);
122:
123: info = TaoIndexSetDestroy(bnls->FreeVariables);CHKERRQ(info);
124: info = TaoMatDestroy(bnls->Hsub);CHKERRQ(info);
125: info = TaoDestroyLinearSolver(tao);CHKERRQ(info);
127: TaoFunctionReturn(0);
128: }
130: /*------------------------------------------------------------*/
133: static int TaoSetOptions_BNLS(TAO_SOLVER tao, void*solver)
134: {
135: int info,ival;
136: TaoTruth flg;
138: TaoFunctionBegin;
140: info = TaoOptionsHead("Newton Line Search Method for bound constrained optimization");CHKERRQ(info);
142: info = TaoOptionInt("-redistribute","Redistribute Free variables (> 1 processors, only)","TaoPetscISType",1,&ival,&flg); CHKERRQ(info);
144: info = TaoOptionName("-submatrixfree","Mask full matrix instead of extract submatrices","TaoPetscISType",&flg); CHKERRQ(info);
146: info = TaoOptionsTail();CHKERRQ(info);
147: info = TaoLineSearchSetFromOptions(tao);CHKERRQ(info);
149: TaoFunctionReturn(0);
150: }
152: /*------------------------------------------------------------*/
155: static int TaoView_BNLS(TAO_SOLVER tao,void*solver)
156: {
157: int info;
159: TaoFunctionBegin;
160: info = TaoLineSearchView(tao);CHKERRQ(info);
161: TaoFunctionReturn(0);
162: }
165: /* ---------------------------------------------------------- */
168: static int TaoSetUp_BNLS(TAO_SOLVER tao, void*solver){
170: int info;
171: TAO_BNLS *bnls = (TAO_BNLS *)solver;
172: TaoVec* X;
173: TaoMat *HH;
175: TaoFunctionBegin;
176: info = TaoGetSolution(tao,&bnls->X);CHKERRQ(info); X=bnls->X;
177: info = TaoGetHessian(tao,&bnls->H);CHKERRQ(info); HH=bnls->H;
179: /* Allocate some arrays */
180: info = X->Clone(&bnls->DX); CHKERRQ(info);
181: info = X->Clone(&bnls->Work); CHKERRQ(info);
182: info = X->Clone(&bnls->DXFree); CHKERRQ(info);
183: info = X->Clone(&bnls->R); CHKERRQ(info);
184: info = X->Clone(&bnls->G); CHKERRQ(info);
185: info = X->Clone(&bnls->PG); CHKERRQ(info);
186: info = X->Clone(&bnls->XL); CHKERRQ(info);
187: info = X->Clone(&bnls->XU); CHKERRQ(info);
189: info = TaoSetLagrangianGradientVector(tao,bnls->PG);CHKERRQ(info);
190: info = TaoSetStepDirectionVector(tao,bnls->DX);CHKERRQ(info);
191: info = TaoSetVariableBounds(tao,bnls->XL,bnls->XU);CHKERRQ(info);
193: info = X->CreateIndexSet(&bnls->FreeVariables); CHKERRQ(info);
194: info = bnls->H->CreateReducedMatrix(bnls->FreeVariables,bnls->FreeVariables,&bnls->Hsub); CHKERRQ(info);
196: info = TaoCreateLinearSolver(tao,HH,100,0); CHKERRQ(info);
198: info = TaoCheckFGH(tao);CHKERRQ(info);
200: TaoFunctionReturn(0);
201: }
203: /*------------------------------------------------------------*/
206: int TaoGetDualVariables_BNLS(TAO_SOLVER tao, TaoVec* DXL, TaoVec* DXU, void* solver)
207: {
209: TAO_BNLS *bnls = (TAO_BNLS *) solver;
210: TaoVec *G=bnls->G,*GP=bnls->Work;
211: int info;
213: TaoFunctionBegin;
215: info = DXL->Waxpby(-1,G,1.0,GP); CHKERRQ(info);
216: info = DXU->SetToZero(); CHKERRQ(info);
217: info = DXL->PointwiseMaximum(DXL,DXU); CHKERRQ(info);
219: info = DXU->Waxpby(-1.0,GP,1.0,G); CHKERRQ(info);
220: info = DXU->Axpy(1.0,DXL); CHKERRQ(info);
222: TaoFunctionReturn(0);
223: }
226: /*------------------------------------------------------------*/
230: int TaoCreate_BNLS(TAO_SOLVER tao)
231: {
232: TAO_BNLS *bnls;
233: int info;
235: TaoFunctionBegin;
237: info = TaoNew(TAO_BNLS,&bnls); CHKERRQ(info);
238: info = PetscLogObjectMemory(tao,sizeof(TAO_BNLS)); CHKERRQ(info);
240: info=TaoSetTaoSolveRoutine(tao,TaoSolve_BNLS,(void*)bnls); CHKERRQ(info);
241: info=TaoSetTaoSetUpDownRoutines(tao,TaoSetUp_BNLS,TaoSetDown_BNLS); CHKERRQ(info);
242: info=TaoSetTaoOptionsRoutine(tao,TaoSetOptions_BNLS); CHKERRQ(info);
243: info=TaoSetTaoViewRoutine(tao,TaoView_BNLS); CHKERRQ(info);
244: info=TaoSetTaoDualVariablesRoutine(tao,TaoGetDualVariables_BNLS); CHKERRQ(info);
246: info = TaoSetMaximumIterates(tao,500); CHKERRQ(info);
247: info = TaoSetTolerances(tao,1e-12,1e-12,0,0); CHKERRQ(info);
249: /* Initialize pointers and variables */
251: bnls->gamma = 0.0;
252: bnls->gamma_factor = 0.01;
253: bnls->DX=0;
254: bnls->DXFree=0;
255: bnls->R=0;
256: bnls->Work=0;
257: bnls->FreeVariables=0;
258: bnls->Hsub=0;
260: info = TaoCreateMoreThuenteBoundLineSearch(tao,0,0.9); CHKERRQ(info);
262: TaoFunctionReturn(0);
263: }