xref: /petsc/src/tao/complementarity/impls/asls/asfls.c (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
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
13 
14    Many other reformulations are possible and combinations of
15    feasible/infeasible and linesearch/trust region are possible.
16 
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.
21 
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.
28 
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.
35 
36    The theory for the feasible version follows from the feasible descent
37    algorithm framework.
38 
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, pages 407439, 1996.
45 . * -  Ferris, Kanzow, Munson, "Feasible Descent Algorithms for Mixed
46        Complementarity Problems," Mathematical Programming, 86,
47        pages 475497, 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 */
54 
55 static PetscErrorCode TaoSetUp_ASFLS(Tao tao) {
56   TAO_SSLS *asls = (TAO_SSLS *)tao->data;
57 
58   PetscFunctionBegin;
59   PetscCall(VecDuplicate(tao->solution, &tao->gradient));
60   PetscCall(VecDuplicate(tao->solution, &tao->stepdirection));
61   PetscCall(VecDuplicate(tao->solution, &asls->ff));
62   PetscCall(VecDuplicate(tao->solution, &asls->dpsi));
63   PetscCall(VecDuplicate(tao->solution, &asls->da));
64   PetscCall(VecDuplicate(tao->solution, &asls->db));
65   PetscCall(VecDuplicate(tao->solution, &asls->t1));
66   PetscCall(VecDuplicate(tao->solution, &asls->t2));
67   PetscCall(VecDuplicate(tao->solution, &asls->w));
68   asls->fixed    = NULL;
69   asls->free     = NULL;
70   asls->J_sub    = NULL;
71   asls->Jpre_sub = NULL;
72   asls->r1       = NULL;
73   asls->r2       = NULL;
74   asls->r3       = NULL;
75   asls->dxfree   = NULL;
76   PetscFunctionReturn(0);
77 }
78 
79 static PetscErrorCode Tao_ASLS_FunctionGradient(TaoLineSearch ls, Vec X, PetscReal *fcn, Vec G, void *ptr) {
80   Tao       tao  = (Tao)ptr;
81   TAO_SSLS *asls = (TAO_SSLS *)tao->data;
82 
83   PetscFunctionBegin;
84   PetscCall(TaoComputeConstraints(tao, X, tao->constraints));
85   PetscCall(VecFischer(X, tao->constraints, tao->XL, tao->XU, asls->ff));
86   PetscCall(VecNorm(asls->ff, NORM_2, &asls->merit));
87   *fcn = 0.5 * asls->merit * asls->merit;
88   PetscCall(TaoComputeJacobian(tao, tao->solution, tao->jacobian, tao->jacobian_pre));
89 
90   PetscCall(MatDFischer(tao->jacobian, tao->solution, tao->constraints, tao->XL, tao->XU, asls->t1, asls->t2, asls->da, asls->db));
91   PetscCall(VecPointwiseMult(asls->t1, asls->ff, asls->db));
92   PetscCall(MatMultTranspose(tao->jacobian, asls->t1, G));
93   PetscCall(VecPointwiseMult(asls->t1, asls->ff, asls->da));
94   PetscCall(VecAXPY(G, 1.0, asls->t1));
95   PetscFunctionReturn(0);
96 }
97 
98 static PetscErrorCode TaoDestroy_ASFLS(Tao tao) {
99   TAO_SSLS *ssls = (TAO_SSLS *)tao->data;
100 
101   PetscFunctionBegin;
102   PetscCall(VecDestroy(&ssls->ff));
103   PetscCall(VecDestroy(&ssls->dpsi));
104   PetscCall(VecDestroy(&ssls->da));
105   PetscCall(VecDestroy(&ssls->db));
106   PetscCall(VecDestroy(&ssls->w));
107   PetscCall(VecDestroy(&ssls->t1));
108   PetscCall(VecDestroy(&ssls->t2));
109   PetscCall(VecDestroy(&ssls->r1));
110   PetscCall(VecDestroy(&ssls->r2));
111   PetscCall(VecDestroy(&ssls->r3));
112   PetscCall(VecDestroy(&ssls->dxfree));
113   PetscCall(MatDestroy(&ssls->J_sub));
114   PetscCall(MatDestroy(&ssls->Jpre_sub));
115   PetscCall(ISDestroy(&ssls->fixed));
116   PetscCall(ISDestroy(&ssls->free));
117   PetscCall(KSPDestroy(&tao->ksp));
118   PetscCall(PetscFree(tao->data));
119   PetscFunctionReturn(0);
120 }
121 
122 static PetscErrorCode TaoSolve_ASFLS(Tao tao) {
123   TAO_SSLS                    *asls = (TAO_SSLS *)tao->data;
124   PetscReal                    psi, ndpsi, normd, innerd, t = 0;
125   PetscInt                     nf;
126   TaoLineSearchConvergedReason ls_reason;
127 
128   PetscFunctionBegin;
129   /* Assume that Setup has been called!
130      Set the structure for the Jacobian and create a linear solver. */
131 
132   PetscCall(TaoComputeVariableBounds(tao));
133   PetscCall(TaoLineSearchSetObjectiveAndGradientRoutine(tao->linesearch, Tao_ASLS_FunctionGradient, tao));
134   PetscCall(TaoLineSearchSetObjectiveRoutine(tao->linesearch, Tao_SSLS_Function, tao));
135   PetscCall(TaoLineSearchSetVariableBounds(tao->linesearch, tao->XL, tao->XU));
136 
137   PetscCall(VecMedian(tao->XL, tao->solution, tao->XU, tao->solution));
138 
139   /* Calculate the function value and fischer function value at the
140      current iterate */
141   PetscCall(TaoLineSearchComputeObjectiveAndGradient(tao->linesearch, tao->solution, &psi, asls->dpsi));
142   PetscCall(VecNorm(asls->dpsi, NORM_2, &ndpsi));
143 
144   tao->reason = TAO_CONTINUE_ITERATING;
145   while (1) {
146     /* Check the converged criteria */
147     PetscCall(PetscInfo(tao, "iter %" PetscInt_FMT ", merit: %g, ||dpsi||: %g\n", tao->niter, (double)asls->merit, (double)ndpsi));
148     PetscCall(TaoLogConvergenceHistory(tao, asls->merit, ndpsi, 0.0, tao->ksp_its));
149     PetscCall(TaoMonitor(tao, tao->niter, asls->merit, ndpsi, 0.0, t));
150     PetscUseTypeMethod(tao, convergencetest, tao->cnvP);
151     if (TAO_CONTINUE_ITERATING != tao->reason) break;
152 
153     /* Call general purpose update function */
154     PetscTryTypeMethod(tao, update, tao->niter, tao->user_update);
155     tao->niter++;
156 
157     /* We are going to solve a linear system of equations.  We need to
158        set the tolerances for the solve so that we maintain an asymptotic
159        rate of convergence that is superlinear.
160        Note: these tolerances are for the reduced system.  We really need
161        to make sure that the full system satisfies the full-space conditions.
162 
163        This rule gives superlinear asymptotic convergence
164        asls->atol = min(0.5, asls->merit*sqrt(asls->merit));
165        asls->rtol = 0.0;
166 
167        This rule gives quadratic asymptotic convergence
168        asls->atol = min(0.5, asls->merit*asls->merit);
169        asls->rtol = 0.0;
170 
171        Calculate a free and fixed set of variables.  The fixed set of
172        variables are those for the d_b is approximately equal to zero.
173        The definition of approximately changes as we approach the solution
174        to the problem.
175 
176        No one rule is guaranteed to work in all cases.  The following
177        definition is based on the norm of the Jacobian matrix.  If the
178        norm is large, the tolerance becomes smaller. */
179     PetscCall(MatNorm(tao->jacobian, NORM_1, &asls->identifier));
180     asls->identifier = PetscMin(asls->merit, 1e-2) / (1 + asls->identifier);
181 
182     PetscCall(VecSet(asls->t1, -asls->identifier));
183     PetscCall(VecSet(asls->t2, asls->identifier));
184 
185     PetscCall(ISDestroy(&asls->fixed));
186     PetscCall(ISDestroy(&asls->free));
187     PetscCall(VecWhichBetweenOrEqual(asls->t1, asls->db, asls->t2, &asls->fixed));
188     PetscCall(ISComplementVec(asls->fixed, asls->t1, &asls->free));
189 
190     PetscCall(ISGetSize(asls->fixed, &nf));
191     PetscCall(PetscInfo(tao, "Number of fixed variables: %" PetscInt_FMT "\n", nf));
192 
193     /* We now have our partition.  Now calculate the direction in the
194        fixed variable space. */
195     PetscCall(TaoVecGetSubVec(asls->ff, asls->fixed, tao->subset_type, 0.0, &asls->r1));
196     PetscCall(TaoVecGetSubVec(asls->da, asls->fixed, tao->subset_type, 1.0, &asls->r2));
197     PetscCall(VecPointwiseDivide(asls->r1, asls->r1, asls->r2));
198     PetscCall(VecSet(tao->stepdirection, 0.0));
199     PetscCall(VecISAXPY(tao->stepdirection, asls->fixed, 1.0, asls->r1));
200 
201     /* Our direction in the Fixed Variable Set is fixed.  Calculate the
202        information needed for the step in the Free Variable Set.  To
203        do this, we need to know the diagonal perturbation and the
204        right hand side. */
205 
206     PetscCall(TaoVecGetSubVec(asls->da, asls->free, tao->subset_type, 0.0, &asls->r1));
207     PetscCall(TaoVecGetSubVec(asls->ff, asls->free, tao->subset_type, 0.0, &asls->r2));
208     PetscCall(TaoVecGetSubVec(asls->db, asls->free, tao->subset_type, 1.0, &asls->r3));
209     PetscCall(VecPointwiseDivide(asls->r1, asls->r1, asls->r3));
210     PetscCall(VecPointwiseDivide(asls->r2, asls->r2, asls->r3));
211 
212     /* r1 is the diagonal perturbation
213        r2 is the right hand side
214        r3 is no longer needed
215 
216        Now need to modify r2 for our direction choice in the fixed
217        variable set:  calculate t1 = J*d, take the reduced vector
218        of t1 and modify r2. */
219 
220     PetscCall(MatMult(tao->jacobian, tao->stepdirection, asls->t1));
221     PetscCall(TaoVecGetSubVec(asls->t1, asls->free, tao->subset_type, 0.0, &asls->r3));
222     PetscCall(VecAXPY(asls->r2, -1.0, asls->r3));
223 
224     /* Calculate the reduced problem matrix and the direction */
225     PetscCall(TaoMatGetSubMat(tao->jacobian, asls->free, asls->w, tao->subset_type, &asls->J_sub));
226     if (tao->jacobian != tao->jacobian_pre) {
227       PetscCall(TaoMatGetSubMat(tao->jacobian_pre, asls->free, asls->w, tao->subset_type, &asls->Jpre_sub));
228     } else {
229       PetscCall(MatDestroy(&asls->Jpre_sub));
230       asls->Jpre_sub = asls->J_sub;
231       PetscCall(PetscObjectReference((PetscObject)(asls->Jpre_sub)));
232     }
233     PetscCall(MatDiagonalSet(asls->J_sub, asls->r1, ADD_VALUES));
234     PetscCall(TaoVecGetSubVec(tao->stepdirection, asls->free, tao->subset_type, 0.0, &asls->dxfree));
235     PetscCall(VecSet(asls->dxfree, 0.0));
236 
237     /* Calculate the reduced direction.  (Really negative of Newton
238        direction.  Therefore, rest of the code uses -d.) */
239     PetscCall(KSPReset(tao->ksp));
240     PetscCall(KSPSetOperators(tao->ksp, asls->J_sub, asls->Jpre_sub));
241     PetscCall(KSPSolve(tao->ksp, asls->r2, asls->dxfree));
242     PetscCall(KSPGetIterationNumber(tao->ksp, &tao->ksp_its));
243     tao->ksp_tot_its += tao->ksp_its;
244 
245     /* Add the direction in the free variables back into the real direction. */
246     PetscCall(VecISAXPY(tao->stepdirection, asls->free, 1.0, asls->dxfree));
247 
248     /* Check the projected real direction for descent and if not, use the negative
249        gradient direction. */
250     PetscCall(VecCopy(tao->stepdirection, asls->w));
251     PetscCall(VecScale(asls->w, -1.0));
252     PetscCall(VecBoundGradientProjection(asls->w, tao->solution, tao->XL, tao->XU, asls->w));
253     PetscCall(VecNorm(asls->w, NORM_2, &normd));
254     PetscCall(VecDot(asls->w, asls->dpsi, &innerd));
255 
256     if (innerd >= -asls->delta * PetscPowReal(normd, asls->rho)) {
257       PetscCall(PetscInfo(tao, "Gradient direction: %5.4e.\n", (double)innerd));
258       PetscCall(PetscInfo(tao, "Iteration %" PetscInt_FMT ": newton direction not descent\n", tao->niter));
259       PetscCall(VecCopy(asls->dpsi, tao->stepdirection));
260       PetscCall(VecDot(asls->dpsi, tao->stepdirection, &innerd));
261     }
262 
263     PetscCall(VecScale(tao->stepdirection, -1.0));
264     innerd = -innerd;
265 
266     /* We now have a correct descent direction.  Apply a linesearch to
267        find the new iterate. */
268     PetscCall(TaoLineSearchSetInitialStepLength(tao->linesearch, 1.0));
269     PetscCall(TaoLineSearchApply(tao->linesearch, tao->solution, &psi, asls->dpsi, tao->stepdirection, &t, &ls_reason));
270     PetscCall(VecNorm(asls->dpsi, NORM_2, &ndpsi));
271   }
272   PetscFunctionReturn(0);
273 }
274 
275 /* ---------------------------------------------------------- */
276 /*MC
277    TAOASFLS - Active-set feasible linesearch algorithm for solving
278        complementarity constraints
279 
280    Options Database Keys:
281 + -tao_ssls_delta - descent test fraction
282 - -tao_ssls_rho - descent test power
283 
284    Level: beginner
285 M*/
286 PETSC_EXTERN PetscErrorCode TaoCreate_ASFLS(Tao tao) {
287   TAO_SSLS   *asls;
288   const char *armijo_type = TAOLINESEARCHARMIJO;
289 
290   PetscFunctionBegin;
291   PetscCall(PetscNewLog(tao, &asls));
292   tao->data                = (void *)asls;
293   tao->ops->solve          = TaoSolve_ASFLS;
294   tao->ops->setup          = TaoSetUp_ASFLS;
295   tao->ops->view           = TaoView_SSLS;
296   tao->ops->setfromoptions = TaoSetFromOptions_SSLS;
297   tao->ops->destroy        = TaoDestroy_ASFLS;
298   tao->subset_type         = TAO_SUBSET_SUBVEC;
299   asls->delta              = 1e-10;
300   asls->rho                = 2.1;
301   asls->fixed              = NULL;
302   asls->free               = NULL;
303   asls->J_sub              = NULL;
304   asls->Jpre_sub           = NULL;
305   asls->w                  = NULL;
306   asls->r1                 = NULL;
307   asls->r2                 = NULL;
308   asls->r3                 = NULL;
309   asls->t1                 = NULL;
310   asls->t2                 = NULL;
311   asls->dxfree             = NULL;
312   asls->identifier         = 1e-5;
313 
314   PetscCall(TaoLineSearchCreate(((PetscObject)tao)->comm, &tao->linesearch));
315   PetscCall(PetscObjectIncrementTabLevel((PetscObject)tao->linesearch, (PetscObject)tao, 1));
316   PetscCall(TaoLineSearchSetType(tao->linesearch, armijo_type));
317   PetscCall(TaoLineSearchSetOptionsPrefix(tao->linesearch, tao->hdr.prefix));
318   PetscCall(TaoLineSearchSetFromOptions(tao->linesearch));
319 
320   PetscCall(KSPCreate(((PetscObject)tao)->comm, &tao->ksp));
321   PetscCall(PetscObjectIncrementTabLevel((PetscObject)tao->ksp, (PetscObject)tao, 1));
322   PetscCall(KSPSetOptionsPrefix(tao->ksp, tao->hdr.prefix));
323   PetscCall(KSPSetFromOptions(tao->ksp));
324 
325   /* Override default settings (unless already changed) */
326   if (!tao->max_it_changed) tao->max_it = 2000;
327   if (!tao->max_funcs_changed) tao->max_funcs = 4000;
328   if (!tao->gttol_changed) tao->gttol = 0;
329   if (!tao->grtol_changed) tao->grtol = 0;
330 #if defined(PETSC_USE_REAL_SINGLE)
331   if (!tao->gatol_changed) tao->gatol = 1.0e-6;
332   if (!tao->fmin_changed) tao->fmin = 1.0e-4;
333 #else
334   if (!tao->gatol_changed) tao->gatol = 1.0e-16;
335   if (!tao->fmin_changed) tao->fmin = 1.0e-8;
336 #endif
337   PetscFunctionReturn(0);
338 }
339