Actual source code: asils.c

petsc-3.12.5 2020-03-29
Report Typos and Errors
  1:  #include <../src/tao/complementarity/impls/ssls/ssls.h>
  2: /*
  3:    Context for ASXLS
  4:      -- active-set      - reduced matrices formed
  5:                           - inherit properties of original system
  6:      -- semismooth (S)  - function not differentiable
  7:                         - merit function continuously differentiable
  8:                         - Fischer-Burmeister reformulation of complementarity
  9:                           - Billups composition for two finite bounds
 10:      -- infeasible (I)  - iterates not guaranteed to remain within bounds
 11:      -- feasible (F)    - iterates guaranteed to remain within bounds
 12:      -- linesearch (LS) - Armijo rule on direction

 14:    Many other reformulations are possible and combinations of
 15:    feasible/infeasible and linesearch/trust region are possible.

 17:    Basic theory
 18:      Fischer-Burmeister reformulation is semismooth with a continuously
 19:      differentiable merit function and strongly semismooth if the F has
 20:      lipschitz continuous derivatives.

 22:      Every accumulation point generated by the algorithm is a stationary
 23:      point for the merit function.  Stationary points of the merit function
 24:      are solutions of the complementarity problem if
 25:        a.  the stationary point has a BD-regular subdifferential, or
 26:        b.  the Schur complement F'/F'_ff is a P_0-matrix where ff is the
 27:            index set corresponding to the free variables.

 29:      If one of the accumulation points has a BD-regular subdifferential then
 30:        a.  the entire sequence converges to this accumulation point at
 31:            a local q-superlinear rate
 32:        b.  if in addition the reformulation is strongly semismooth near
 33:            this accumulation point, then the algorithm converges at a
 34:            local q-quadratic rate.

 36:    The theory for the feasible version follows from the feasible descent
 37:    algorithm framework.

 39:    References:
 40:      Billups, "Algorithms for Complementarity Problems and Generalized
 41:        Equations," Ph.D thesis, University of Wisconsin  Madison, 1995.
 42:      De Luca, Facchinei, Kanzow, "A Semismooth Equation Approach to the
 43:        Solution of Nonlinear Complementarity Problems," Mathematical
 44:        Programming, 75, 1996.
 45:      Ferris, Kanzow, Munson, "Feasible Descent Algorithms for Mixed
 46:        Complementarity Problems," Mathematical Programming, 86,
 47:        1999.
 48:      Fischer, "A Special Newton type Optimization Method," Optimization,
 49:        24, 1992
 50:      Munson, Facchinei, Ferris, Fischer, Kanzow, "The Semismooth Algorithm
 51:        for Large Scale Complementarity Problems," Technical Report,
 52:        University of Wisconsin  Madison, 1999.
 53: */


 56: static PetscErrorCode TaoSetUp_ASILS(Tao tao)
 57: {
 58:   TAO_SSLS       *asls = (TAO_SSLS *)tao->data;

 62:   VecDuplicate(tao->solution,&tao->gradient);
 63:   VecDuplicate(tao->solution,&tao->stepdirection);
 64:   VecDuplicate(tao->solution,&asls->ff);
 65:   VecDuplicate(tao->solution,&asls->dpsi);
 66:   VecDuplicate(tao->solution,&asls->da);
 67:   VecDuplicate(tao->solution,&asls->db);
 68:   VecDuplicate(tao->solution,&asls->t1);
 69:   VecDuplicate(tao->solution,&asls->t2);
 70:   asls->fixed = NULL;
 71:   asls->free = NULL;
 72:   asls->J_sub = NULL;
 73:   asls->Jpre_sub = NULL;
 74:   asls->w = NULL;
 75:   asls->r1 = NULL;
 76:   asls->r2 = NULL;
 77:   asls->r3 = NULL;
 78:   asls->dxfree = NULL;
 79:   return(0);
 80: }

 82: static PetscErrorCode Tao_ASLS_FunctionGradient(TaoLineSearch ls, Vec X, PetscReal *fcn,  Vec G, void *ptr)
 83: {
 84:   Tao            tao = (Tao)ptr;
 85:   TAO_SSLS       *asls = (TAO_SSLS *)tao->data;

 89:   TaoComputeConstraints(tao, X, tao->constraints);
 90:   VecFischer(X,tao->constraints,tao->XL,tao->XU,asls->ff);
 91:   VecNorm(asls->ff,NORM_2,&asls->merit);
 92:   *fcn = 0.5*asls->merit*asls->merit;

 94:   TaoComputeJacobian(tao,tao->solution,tao->jacobian,tao->jacobian_pre);
 95:   MatDFischer(tao->jacobian, tao->solution, tao->constraints,tao->XL, tao->XU, asls->t1, asls->t2,asls->da, asls->db);
 96:   VecPointwiseMult(asls->t1, asls->ff, asls->db);
 97:   MatMultTranspose(tao->jacobian,asls->t1,G);
 98:   VecPointwiseMult(asls->t1, asls->ff, asls->da);
 99:   VecAXPY(G,1.0,asls->t1);
100:   return(0);
101: }

103: static PetscErrorCode TaoDestroy_ASILS(Tao tao)
104: {
105:   TAO_SSLS       *ssls = (TAO_SSLS *)tao->data;

109:   VecDestroy(&ssls->ff);
110:   VecDestroy(&ssls->dpsi);
111:   VecDestroy(&ssls->da);
112:   VecDestroy(&ssls->db);
113:   VecDestroy(&ssls->w);
114:   VecDestroy(&ssls->t1);
115:   VecDestroy(&ssls->t2);
116:   VecDestroy(&ssls->r1);
117:   VecDestroy(&ssls->r2);
118:   VecDestroy(&ssls->r3);
119:   VecDestroy(&ssls->dxfree);
120:   MatDestroy(&ssls->J_sub);
121:   MatDestroy(&ssls->Jpre_sub);
122:   ISDestroy(&ssls->fixed);
123:   ISDestroy(&ssls->free);
124:   PetscFree(tao->data);
125:   return(0);
126: }

128: static PetscErrorCode TaoSolve_ASILS(Tao tao)
129: {
130:   TAO_SSLS                     *asls = (TAO_SSLS *)tao->data;
131:   PetscReal                    psi,ndpsi, normd, innerd, t=0;
132:   PetscInt                     nf;
133:   PetscErrorCode               ierr;
134:   TaoLineSearchConvergedReason ls_reason;

137:   /* Assume that Setup has been called!
138:      Set the structure for the Jacobian and create a linear solver. */

140:   TaoComputeVariableBounds(tao);
141:   TaoLineSearchSetObjectiveAndGradientRoutine(tao->linesearch,Tao_ASLS_FunctionGradient,tao);
142:   TaoLineSearchSetObjectiveRoutine(tao->linesearch,Tao_SSLS_Function,tao);

144:   /* Calculate the function value and fischer function value at the
145:      current iterate */
146:   TaoLineSearchComputeObjectiveAndGradient(tao->linesearch,tao->solution,&psi,asls->dpsi);
147:   VecNorm(asls->dpsi,NORM_2,&ndpsi);

149:   tao->reason = TAO_CONTINUE_ITERATING;
150:   while (1) {
151:     /* Check the termination criteria */
152:     PetscInfo3(tao,"iter %D, merit: %g, ||dpsi||: %g\n",tao->niter, (double)asls->merit,  (double)ndpsi);
153:     TaoLogConvergenceHistory(tao,asls->merit,ndpsi,0.0,tao->ksp_its);
154:     TaoMonitor(tao,tao->niter,asls->merit,ndpsi,0.0,t);
155:     (*tao->ops->convergencetest)(tao,tao->cnvP);
156:     if (TAO_CONTINUE_ITERATING != tao->reason) break;
157: 
158:     /* Call general purpose update function */
159:     if (tao->ops->update) {
160:       (*tao->ops->update)(tao, tao->niter, tao->user_update);
161:     }
162:     tao->niter++;

164:     /* We are going to solve a linear system of equations.  We need to
165:        set the tolerances for the solve so that we maintain an asymptotic
166:        rate of convergence that is superlinear.
167:        Note: these tolerances are for the reduced system.  We really need
168:        to make sure that the full system satisfies the full-space conditions.

170:        This rule gives superlinear asymptotic convergence
171:        asls->atol = min(0.5, asls->merit*sqrt(asls->merit));
172:        asls->rtol = 0.0;

174:        This rule gives quadratic asymptotic convergence
175:        asls->atol = min(0.5, asls->merit*asls->merit);
176:        asls->rtol = 0.0;

178:        Calculate a free and fixed set of variables.  The fixed set of
179:        variables are those for the d_b is approximately equal to zero.
180:        The definition of approximately changes as we approach the solution
181:        to the problem.

183:        No one rule is guaranteed to work in all cases.  The following
184:        definition is based on the norm of the Jacobian matrix.  If the
185:        norm is large, the tolerance becomes smaller. */
186:     MatNorm(tao->jacobian,NORM_1,&asls->identifier);
187:     asls->identifier = PetscMin(asls->merit, 1e-2) / (1 + asls->identifier);

189:     VecSet(asls->t1,-asls->identifier);
190:     VecSet(asls->t2, asls->identifier);

192:     ISDestroy(&asls->fixed);
193:     ISDestroy(&asls->free);
194:     VecWhichBetweenOrEqual(asls->t1, asls->db, asls->t2, &asls->fixed);
195:     ISComplementVec(asls->fixed,asls->t1, &asls->free);

197:     ISGetSize(asls->fixed,&nf);
198:     PetscInfo1(tao,"Number of fixed variables: %D\n", nf);

200:     /* We now have our partition.  Now calculate the direction in the
201:        fixed variable space. */
202:     TaoVecGetSubVec(asls->ff, asls->fixed, tao->subset_type, 0.0, &asls->r1);
203:     TaoVecGetSubVec(asls->da, asls->fixed, tao->subset_type, 1.0, &asls->r2);
204:     VecPointwiseDivide(asls->r1,asls->r1,asls->r2);
205:     VecSet(tao->stepdirection,0.0);
206:     VecISAXPY(tao->stepdirection, asls->fixed,1.0,asls->r1);

208:     /* Our direction in the Fixed Variable Set is fixed.  Calculate the
209:        information needed for the step in the Free Variable Set.  To
210:        do this, we need to know the diagonal perturbation and the
211:        right hand side. */

213:     TaoVecGetSubVec(asls->da, asls->free, tao->subset_type, 0.0, &asls->r1);
214:     TaoVecGetSubVec(asls->ff, asls->free, tao->subset_type, 0.0, &asls->r2);
215:     TaoVecGetSubVec(asls->db, asls->free, tao->subset_type, 1.0, &asls->r3);
216:     VecPointwiseDivide(asls->r1,asls->r1, asls->r3);
217:     VecPointwiseDivide(asls->r2,asls->r2, asls->r3);

219:     /* r1 is the diagonal perturbation
220:        r2 is the right hand side
221:        r3 is no longer needed

223:        Now need to modify r2 for our direction choice in the fixed
224:        variable set:  calculate t1 = J*d, take the reduced vector
225:        of t1 and modify r2. */

227:     MatMult(tao->jacobian, tao->stepdirection, asls->t1);
228:     TaoVecGetSubVec(asls->t1,asls->free,tao->subset_type,0.0,&asls->r3);
229:     VecAXPY(asls->r2, -1.0, asls->r3);

231:     /* Calculate the reduced problem matrix and the direction */
232:     if (!asls->w && (tao->subset_type == TAO_SUBSET_MASK || tao->subset_type == TAO_SUBSET_MATRIXFREE)) {
233:       VecDuplicate(tao->solution, &asls->w);
234:     }
235:     TaoMatGetSubMat(tao->jacobian, asls->free, asls->w, tao->subset_type,&asls->J_sub);
236:     if (tao->jacobian != tao->jacobian_pre) {
237:       TaoMatGetSubMat(tao->jacobian_pre, asls->free, asls->w, tao->subset_type, &asls->Jpre_sub);
238:     } else {
239:       MatDestroy(&asls->Jpre_sub);
240:       asls->Jpre_sub = asls->J_sub;
241:       PetscObjectReference((PetscObject)(asls->Jpre_sub));
242:     }
243:     MatDiagonalSet(asls->J_sub, asls->r1,ADD_VALUES);
244:     TaoVecGetSubVec(tao->stepdirection, asls->free, tao->subset_type, 0.0, &asls->dxfree);
245:     VecSet(asls->dxfree, 0.0);

247:     /* Calculate the reduced direction.  (Really negative of Newton
248:        direction.  Therefore, rest of the code uses -d.) */
249:     KSPReset(tao->ksp);
250:     KSPSetOperators(tao->ksp, asls->J_sub, asls->Jpre_sub);
251:     KSPSolve(tao->ksp, asls->r2, asls->dxfree);
252:     KSPGetIterationNumber(tao->ksp,&tao->ksp_its);
253:     tao->ksp_tot_its+=tao->ksp_its;

255:     /* Add the direction in the free variables back into the real direction. */
256:     VecISAXPY(tao->stepdirection, asls->free, 1.0,asls->dxfree);

258:     /* Check the real direction for descent and if not, use the negative
259:        gradient direction. */
260:     VecNorm(tao->stepdirection, NORM_2, &normd);
261:     VecDot(tao->stepdirection, asls->dpsi, &innerd);

263:     if (innerd <= asls->delta*PetscPowReal(normd, asls->rho)) {
264:       PetscInfo1(tao,"Gradient direction: %5.4e.\n", (double)innerd);
265:       PetscInfo1(tao, "Iteration %D: newton direction not descent\n", tao->niter);
266:       VecCopy(asls->dpsi, tao->stepdirection);
267:       VecDot(asls->dpsi, tao->stepdirection, &innerd);
268:     }

270:     VecScale(tao->stepdirection, -1.0);
271:     innerd = -innerd;

273:     /* We now have a correct descent direction.  Apply a linesearch to
274:        find the new iterate. */
275:     TaoLineSearchSetInitialStepLength(tao->linesearch, 1.0);
276:     TaoLineSearchApply(tao->linesearch, tao->solution, &psi,asls->dpsi, tao->stepdirection, &t, &ls_reason);
277:     VecNorm(asls->dpsi, NORM_2, &ndpsi);
278:   }
279:   return(0);
280: }

282: /* ---------------------------------------------------------- */
283: /*MC
284:    TAOASILS - Active-set infeasible linesearch algorithm for solving
285:        complementarity constraints

287:    Options Database Keys:
288: + -tao_ssls_delta - descent test fraction
289: - -tao_ssls_rho - descent test power

291:   Level: beginner
292: M*/
293: PETSC_EXTERN PetscErrorCode TaoCreate_ASILS(Tao tao)
294: {
295:   TAO_SSLS       *asls;
297:   const char     *armijo_type = TAOLINESEARCHARMIJO;

300:   PetscNewLog(tao,&asls);
301:   tao->data = (void*)asls;
302:   tao->ops->solve = TaoSolve_ASILS;
303:   tao->ops->setup = TaoSetUp_ASILS;
304:   tao->ops->view = TaoView_SSLS;
305:   tao->ops->setfromoptions = TaoSetFromOptions_SSLS;
306:   tao->ops->destroy = TaoDestroy_ASILS;
307:   tao->subset_type = TAO_SUBSET_SUBVEC;
308:   asls->delta = 1e-10;
309:   asls->rho = 2.1;
310:   asls->fixed = NULL;
311:   asls->free = NULL;
312:   asls->J_sub = NULL;
313:   asls->Jpre_sub = NULL;
314:   asls->w = NULL;
315:   asls->r1 = NULL;
316:   asls->r2 = NULL;
317:   asls->r3 = NULL;
318:   asls->t1 = NULL;
319:   asls->t2 = NULL;
320:   asls->dxfree = NULL;

322:   asls->identifier = 1e-5;

324:   TaoLineSearchCreate(((PetscObject)tao)->comm, &tao->linesearch);
325:   PetscObjectIncrementTabLevel((PetscObject)tao->linesearch, (PetscObject)tao, 1);
326:   TaoLineSearchSetType(tao->linesearch, armijo_type);
327:   TaoLineSearchSetOptionsPrefix(tao->linesearch,tao->hdr.prefix);
328:   TaoLineSearchSetFromOptions(tao->linesearch);

330:   KSPCreate(((PetscObject)tao)->comm, &tao->ksp);
331:   PetscObjectIncrementTabLevel((PetscObject)tao->ksp, (PetscObject)tao, 1);
332:   KSPSetOptionsPrefix(tao->ksp,tao->hdr.prefix);
333:   KSPSetFromOptions(tao->ksp);

335:   /* Override default settings (unless already changed) */
336:   if (!tao->max_it_changed) tao->max_it = 2000;
337:   if (!tao->max_funcs_changed) tao->max_funcs = 4000;
338:   if (!tao->gttol_changed) tao->gttol = 0;
339:   if (!tao->grtol_changed) tao->grtol = 0;
340: #if defined(PETSC_USE_REAL_SINGLE)
341:   if (!tao->gatol_changed) tao->gatol = 1.0e-6;
342:   if (!tao->fmin_changed)  tao->fmin = 1.0e-4;
343: #else
344:   if (!tao->gatol_changed) tao->gatol = 1.0e-16;
345:   if (!tao->fmin_changed) tao->fmin = 1.0e-8;
346: #endif
347:   return(0);
348: }