Actual source code: neldermead.c

  1: #include <../src/tao/unconstrained/impls/neldermead/neldermead.h>
  2: #include <petscvec.h>

  4: static PetscErrorCode NelderMeadSort(TAO_NelderMead *nm)
  5: {
  6:   PetscReal *values  = nm->f_values;
  7:   PetscInt  *indices = nm->indices;
  8:   PetscInt   dim     = nm->N + 1;
  9:   PetscInt   i, j, index;
 10:   PetscReal  val;

 12:   PetscFunctionBegin;
 13:   for (i = 1; i < dim; i++) {
 14:     index = indices[i];
 15:     val   = values[index];
 16:     for (j = i - 1; j >= 0 && values[indices[j]] > val; j--) indices[j + 1] = indices[j];
 17:     indices[j + 1] = index;
 18:   }
 19:   PetscFunctionReturn(PETSC_SUCCESS);
 20: }

 22: // Xbar = average of the best N vertices, so nm->indices must be sorted
 23: static PetscErrorCode NelderMeadComputeCentroid(TAO_NelderMead *nm)
 24: {
 25:   PetscFunctionBegin;
 26:   PetscCall(VecSet(nm->Xbar, 0.0));
 27:   for (PetscInt i = 0; i < nm->N; i++) PetscCall(VecAXPY(nm->Xbar, 1.0, nm->simplex[nm->indices[i]]));
 28:   PetscCall(VecScale(nm->Xbar, nm->oneOverN));
 29:   PetscFunctionReturn(PETSC_SUCCESS);
 30: }

 32: static PetscErrorCode NelderMeadReplace(TAO_NelderMead *nm, PetscInt index, Vec Xmu, PetscReal f)
 33: {
 34:   PetscFunctionBegin;
 35:   /*  Add new vector's fraction of average */
 36:   PetscCall(VecAXPY(nm->Xbar, nm->oneOverN, Xmu));
 37:   PetscCall(VecCopy(Xmu, nm->simplex[index]));
 38:   nm->f_values[index] = f;

 40:   PetscCall(NelderMeadSort(nm));

 42:   /*  Subtract last vector from average */
 43:   PetscCall(VecAXPY(nm->Xbar, -nm->oneOverN, nm->simplex[nm->indices[nm->N]]));
 44:   PetscFunctionReturn(PETSC_SUCCESS);
 45: }

 47: static PetscErrorCode TaoSetUp_NM(Tao tao)
 48: {
 49:   TAO_NelderMead *nm = (TAO_NelderMead *)tao->data;
 50:   PetscInt        n;

 52:   PetscFunctionBegin;
 53:   PetscCall(VecGetSize(tao->solution, &n));
 54:   nm->N        = n;
 55:   nm->oneOverN = 1.0 / n;
 56:   PetscCall(VecDuplicateVecs(tao->solution, nm->N + 1, &nm->simplex));
 57:   PetscCall(PetscMalloc1(nm->N + 1, &nm->f_values));
 58:   PetscCall(PetscMalloc1(nm->N + 1, &nm->indices));
 59:   PetscCall(VecDuplicate(tao->solution, &nm->Xbar));
 60:   PetscCall(VecDuplicate(tao->solution, &nm->Xmur));
 61:   PetscCall(VecDuplicate(tao->solution, &nm->Xmue));
 62:   PetscCall(VecDuplicate(tao->solution, &nm->Xmuc));

 64:   tao->gradient = NULL;
 65:   tao->step     = 0;
 66:   PetscFunctionReturn(PETSC_SUCCESS);
 67: }

 69: static PetscErrorCode TaoDestroy_NM(Tao tao)
 70: {
 71:   TAO_NelderMead *nm = (TAO_NelderMead *)tao->data;

 73:   PetscFunctionBegin;
 74:   if (tao->setupcalled) {
 75:     PetscCall(VecDestroyVecs(nm->N + 1, &nm->simplex));
 76:     PetscCall(VecDestroy(&nm->Xmuc));
 77:     PetscCall(VecDestroy(&nm->Xmue));
 78:     PetscCall(VecDestroy(&nm->Xmur));
 79:     PetscCall(VecDestroy(&nm->Xbar));
 80:   }
 81:   PetscCall(PetscFree(nm->indices));
 82:   PetscCall(PetscFree(nm->f_values));
 83:   PetscCall(PetscFree(tao->data));
 84:   PetscFunctionReturn(PETSC_SUCCESS);
 85: }

 87: static PetscErrorCode TaoSetFromOptions_NM(Tao tao, PetscOptionItems PetscOptionsObject)
 88: {
 89:   TAO_NelderMead *nm = (TAO_NelderMead *)tao->data;

 91:   PetscFunctionBegin;
 92:   PetscOptionsHeadBegin(PetscOptionsObject, "Nelder-Mead options");
 93:   PetscCall(PetscOptionsDeprecated("-tao_nm_lamda", "-tao_nm_lambda", "3.18.4", NULL));
 94:   PetscCall(PetscOptionsReal("-tao_nm_lambda", "initial step length", "", nm->lambda, &nm->lambda, NULL));
 95:   PetscCall(PetscOptionsReal("-tao_nm_mu", "mu", "", nm->mu_oc, &nm->mu_oc, NULL));
 96:   nm->mu_ic = -nm->mu_oc;
 97:   nm->mu_r  = nm->mu_oc * 2.0;
 98:   nm->mu_e  = nm->mu_oc * 4.0;
 99:   PetscOptionsHeadEnd();
100:   PetscFunctionReturn(PETSC_SUCCESS);
101: }

103: static PetscErrorCode TaoView_NM(Tao tao, PetscViewer viewer)
104: {
105:   TAO_NelderMead *nm = (TAO_NelderMead *)tao->data;
106:   PetscBool       isascii;

108:   PetscFunctionBegin;
109:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
110:   if (isascii) {
111:     PetscCall(PetscViewerASCIIPushTab(viewer));
112:     PetscCall(PetscViewerASCIIPrintf(viewer, "expansions: %" PetscInt_FMT "\n", nm->nexpand));
113:     PetscCall(PetscViewerASCIIPrintf(viewer, "reflections: %" PetscInt_FMT "\n", nm->nreflect));
114:     PetscCall(PetscViewerASCIIPrintf(viewer, "inside contractions: %" PetscInt_FMT "\n", nm->nincontract));
115:     PetscCall(PetscViewerASCIIPrintf(viewer, "outside contractionss: %" PetscInt_FMT "\n", nm->noutcontract));
116:     PetscCall(PetscViewerASCIIPrintf(viewer, "Shrink steps: %" PetscInt_FMT "\n", nm->nshrink));
117:     PetscCall(PetscViewerASCIIPopTab(viewer));
118:   }
119:   PetscFunctionReturn(PETSC_SUCCESS);
120: }

122: static PetscErrorCode TaoSolve_NM(Tao tao)
123: {
124:   TAO_NelderMead *nm = (TAO_NelderMead *)tao->data;
125:   PetscReal      *x;
126:   Vec             Xmur = nm->Xmur, Xmue = nm->Xmue, Xmuc = nm->Xmuc, Xbar = nm->Xbar;
127:   PetscReal       fr, fe, fc;
128:   PetscInt        shrink;
129:   PetscInt        low, high;

131:   PetscFunctionBegin;
132:   nm->nshrink      = 0;
133:   nm->nreflect     = 0;
134:   nm->nincontract  = 0;
135:   nm->noutcontract = 0;
136:   nm->nexpand      = 0;

138:   if (tao->XL || tao->XU || tao->ops->computebounds) PetscCall(PetscInfo(tao, "WARNING: Variable bounds have been set but will be ignored by NelderMead algorithm\n"));

140:   PetscCall(VecCopy(tao->solution, nm->simplex[0]));
141:   PetscCall(TaoComputeObjective(tao, nm->simplex[0], &nm->f_values[0]));
142:   nm->indices[0] = 0;
143:   for (PetscInt i = 1; i < nm->N + 1; i++) {
144:     PetscCall(VecCopy(tao->solution, nm->simplex[i]));
145:     PetscCall(VecGetOwnershipRange(nm->simplex[i], &low, &high));
146:     if (i - 1 >= low && i - 1 < high) {
147:       PetscCall(VecGetArray(nm->simplex[i], &x));
148:       x[i - 1 - low] += nm->lambda;
149:       PetscCall(VecRestoreArray(nm->simplex[i], &x));
150:     }

152:     PetscCall(TaoComputeObjective(tao, nm->simplex[i], &nm->f_values[i]));
153:     nm->indices[i] = i;
154:   }

156:   /*  Xbar  = (Sum of all simplex vectors - worst vector)/N */
157:   PetscCall(NelderMeadSort(nm));
158:   PetscCall(NelderMeadComputeCentroid(nm));
159:   tao->reason = TAO_CONTINUE_ITERATING;
160:   while (1) {
161:     /* Call general purpose update function */
162:     PetscTryTypeMethod(tao, update, tao->niter, tao->user_update);
163:     ++tao->niter;
164:     shrink = 0;
165:     PetscCall(VecCopy(nm->simplex[nm->indices[0]], tao->solution));
166:     PetscCall(TaoLogConvergenceHistory(tao, nm->f_values[nm->indices[0]], nm->f_values[nm->indices[nm->N]] - nm->f_values[nm->indices[0]], 0.0, tao->ksp_its));
167:     PetscCall(TaoMonitor(tao, tao->niter, nm->f_values[nm->indices[0]], nm->f_values[nm->indices[nm->N]] - nm->f_values[nm->indices[0]], 0.0, 1.0));
168:     PetscUseTypeMethod(tao, convergencetest, tao->cnvP);
169:     if (tao->reason != TAO_CONTINUE_ITERATING) break;

171:     /* x(mu) = (1 + mu)Xbar - mu*X_N+1 */
172:     PetscCall(VecAXPBYPCZ(Xmur, 1 + nm->mu_r, -nm->mu_r, 0, Xbar, nm->simplex[nm->indices[nm->N]]));
173:     PetscCall(TaoComputeObjective(tao, Xmur, &fr));

175:     if (nm->f_values[nm->indices[0]] <= fr && fr < nm->f_values[nm->indices[nm->N - 1]]) {
176:       /*  reflect */
177:       nm->nreflect++;
178:       PetscCall(PetscInfo(0, "Reflect\n"));
179:       PetscCall(NelderMeadReplace(nm, nm->indices[nm->N], Xmur, fr));
180:     } else if (fr < nm->f_values[nm->indices[0]]) {
181:       /*  expand */
182:       nm->nexpand++;
183:       PetscCall(PetscInfo(0, "Expand\n"));
184:       PetscCall(VecAXPBYPCZ(Xmue, 1 + nm->mu_e, -nm->mu_e, 0, Xbar, nm->simplex[nm->indices[nm->N]]));
185:       PetscCall(TaoComputeObjective(tao, Xmue, &fe));
186:       if (fe < fr) {
187:         PetscCall(NelderMeadReplace(nm, nm->indices[nm->N], Xmue, fe));
188:       } else {
189:         PetscCall(NelderMeadReplace(nm, nm->indices[nm->N], Xmur, fr));
190:       }
191:     } else if (nm->f_values[nm->indices[nm->N - 1]] <= fr && fr < nm->f_values[nm->indices[nm->N]]) {
192:       /* outside contraction */
193:       nm->noutcontract++;
194:       PetscCall(PetscInfo(0, "Outside Contraction\n"));
195:       PetscCall(VecAXPBYPCZ(Xmuc, 1 + nm->mu_oc, -nm->mu_oc, 0, Xbar, nm->simplex[nm->indices[nm->N]]));

197:       PetscCall(TaoComputeObjective(tao, Xmuc, &fc));
198:       if (fc <= fr) PetscCall(NelderMeadReplace(nm, nm->indices[nm->N], Xmuc, fc));
199:       else shrink = 1;
200:     } else {
201:       /* inside contraction */
202:       nm->nincontract++;
203:       PetscCall(PetscInfo(0, "Inside Contraction\n"));
204:       PetscCall(VecAXPBYPCZ(Xmuc, 1 + nm->mu_ic, -nm->mu_ic, 0, Xbar, nm->simplex[nm->indices[nm->N]]));
205:       PetscCall(TaoComputeObjective(tao, Xmuc, &fc));
206:       if (fc < nm->f_values[nm->indices[nm->N]]) PetscCall(NelderMeadReplace(nm, nm->indices[nm->N], Xmuc, fc));
207:       else shrink = 1;
208:     }

210:     if (shrink) {
211:       nm->nshrink++;
212:       PetscCall(PetscInfo(0, "Shrink\n"));

214:       for (PetscInt i = 1; i < nm->N + 1; i++) {
215:         PetscCall(VecAXPBY(nm->simplex[nm->indices[i]], 1.5, -0.5, nm->simplex[nm->indices[0]]));
216:         PetscCall(TaoComputeObjective(tao, nm->simplex[nm->indices[i]], &nm->f_values[nm->indices[i]]));
217:       }
218:       PetscCall(NelderMeadSort(nm));
219:       PetscCall(NelderMeadComputeCentroid(nm));
220:     }
221:   }
222:   PetscFunctionReturn(PETSC_SUCCESS);
223: }

225: /*MC
226:  TAONM - Nelder-Mead solver for derivative free, unconstrained minimization

228:  Options Database Keys:
229: + -tao_nm_lambda - initial step length
230: - -tao_nm_mu     - expansion/contraction factor

232:  Level: beginner

234: .seealso: `Tao`, `TAONLS`, `TAONTL`, `TaoType`, `TaoCreate()`
235: M*/

237: PETSC_EXTERN PetscErrorCode TaoCreate_NM(Tao tao)
238: {
239:   TAO_NelderMead *nm;

241:   PetscFunctionBegin;
242:   PetscCall(PetscNew(&nm));
243:   tao->data = (void *)nm;

245:   tao->ops->setup          = TaoSetUp_NM;
246:   tao->ops->solve          = TaoSolve_NM;
247:   tao->ops->view           = TaoView_NM;
248:   tao->ops->setfromoptions = TaoSetFromOptions_NM;
249:   tao->ops->destroy        = TaoDestroy_NM;

251:   /* Override default settings (unless already changed) */
252:   PetscObjectParameterSetDefault(tao, max_it, 2000);
253:   PetscObjectParameterSetDefault(tao, max_funcs, 4000);

255:   nm->simplex = NULL;
256:   nm->lambda  = 1;

258:   nm->mu_ic = -0.5;
259:   nm->mu_oc = 0.5;
260:   nm->mu_r  = 1.0;
261:   nm->mu_e  = 2.0;
262:   PetscFunctionReturn(PETSC_SUCCESS);
263: }