Lines Matching refs:nm
4 static PetscErrorCode NelderMeadSort(TAO_NelderMead *nm)
6 PetscReal *values = nm->f_values;
7 PetscInt *indices = nm->indices;
8 PetscInt dim = nm->N + 1;
22 static PetscErrorCode NelderMeadReplace(TAO_NelderMead *nm, PetscInt index, Vec Xmu, PetscReal f)
26 PetscCall(VecAXPY(nm->Xbar, nm->oneOverN, Xmu));
27 PetscCall(VecCopy(Xmu, nm->simplex[index]));
28 nm->f_values[index] = f;
30 PetscCall(NelderMeadSort(nm));
33 PetscCall(VecAXPY(nm->Xbar, -nm->oneOverN, nm->simplex[nm->indices[nm->N]]));
39 TAO_NelderMead *nm = (TAO_NelderMead *)tao->data;
44 nm->N = n;
45 nm->oneOverN = 1.0 / n;
46 PetscCall(VecDuplicateVecs(tao->solution, nm->N + 1, &nm->simplex));
47 PetscCall(PetscMalloc1(nm->N + 1, &nm->f_values));
48 PetscCall(PetscMalloc1(nm->N + 1, &nm->indices));
49 PetscCall(VecDuplicate(tao->solution, &nm->Xbar));
50 PetscCall(VecDuplicate(tao->solution, &nm->Xmur));
51 PetscCall(VecDuplicate(tao->solution, &nm->Xmue));
52 PetscCall(VecDuplicate(tao->solution, &nm->Xmuc));
61 TAO_NelderMead *nm = (TAO_NelderMead *)tao->data;
65 PetscCall(VecDestroyVecs(nm->N + 1, &nm->simplex));
66 PetscCall(VecDestroy(&nm->Xmuc));
67 PetscCall(VecDestroy(&nm->Xmue));
68 PetscCall(VecDestroy(&nm->Xmur));
69 PetscCall(VecDestroy(&nm->Xbar));
71 PetscCall(PetscFree(nm->indices));
72 PetscCall(PetscFree(nm->f_values));
79 TAO_NelderMead *nm = (TAO_NelderMead *)tao->data;
84 PetscCall(PetscOptionsReal("-tao_nm_lambda", "initial step length", "", nm->lambda, &nm->lambda, NULL));
85 PetscCall(PetscOptionsReal("-tao_nm_mu", "mu", "", nm->mu_oc, &nm->mu_oc, NULL));
86 nm->mu_ic = -nm->mu_oc;
87 nm->mu_r = nm->mu_oc * 2.0;
88 nm->mu_e = nm->mu_oc * 4.0;
95 TAO_NelderMead *nm = (TAO_NelderMead *)tao->data;
102 PetscCall(PetscViewerASCIIPrintf(viewer, "expansions: %" PetscInt_FMT "\n", nm->nexpand));
103 PetscCall(PetscViewerASCIIPrintf(viewer, "reflections: %" PetscInt_FMT "\n", nm->nreflect));
104 PetscCall(PetscViewerASCIIPrintf(viewer, "inside contractions: %" PetscInt_FMT "\n", nm->nincontract));
105 PetscCall(PetscViewerASCIIPrintf(viewer, "outside contractionss: %" PetscInt_FMT "\n", nm->noutcontract));
106 PetscCall(PetscViewerASCIIPrintf(viewer, "Shrink steps: %" PetscInt_FMT "\n", nm->nshrink));
114 TAO_NelderMead *nm = (TAO_NelderMead *)tao->data;
117 Vec Xmur = nm->Xmur, Xmue = nm->Xmue, Xmuc = nm->Xmuc, Xbar = nm->Xbar;
123 nm->nshrink = 0;
124 nm->nreflect = 0;
125 nm->nincontract = 0;
126 nm->noutcontract = 0;
127 nm->nexpand = 0;
131 PetscCall(VecCopy(tao->solution, nm->simplex[0]));
132 PetscCall(TaoComputeObjective(tao, nm->simplex[0], &nm->f_values[0]));
133 nm->indices[0] = 0;
134 for (i = 1; i < nm->N + 1; i++) {
135 PetscCall(VecCopy(tao->solution, nm->simplex[i]));
136 PetscCall(VecGetOwnershipRange(nm->simplex[i], &low, &high));
138 PetscCall(VecGetArray(nm->simplex[i], &x));
139 x[i - 1 - low] += nm->lambda;
140 PetscCall(VecRestoreArray(nm->simplex[i], &x));
143 PetscCall(TaoComputeObjective(tao, nm->simplex[i], &nm->f_values[i]));
144 nm->indices[i] = i;
148 PetscCall(NelderMeadSort(nm));
150 for (i = 0; i < nm->N; i++) PetscCall(VecAXPY(Xbar, 1.0, nm->simplex[nm->indices[i]]));
151 PetscCall(VecScale(Xbar, nm->oneOverN));
158 PetscCall(VecCopy(nm->simplex[nm->indices[0]], tao->solution));
159 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));
160 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));
165 PetscCall(VecAXPBYPCZ(Xmur, 1 + nm->mu_r, -nm->mu_r, 0, Xbar, nm->simplex[nm->indices[nm->N]]));
168 if (nm->f_values[nm->indices[0]] <= fr && fr < nm->f_values[nm->indices[nm->N - 1]]) {
170 nm->nreflect++;
172 PetscCall(NelderMeadReplace(nm, nm->indices[nm->N], Xmur, fr));
173 } else if (fr < nm->f_values[nm->indices[0]]) {
175 nm->nexpand++;
177 PetscCall(VecAXPBYPCZ(Xmue, 1 + nm->mu_e, -nm->mu_e, 0, Xbar, nm->simplex[nm->indices[nm->N]]));
180 PetscCall(NelderMeadReplace(nm, nm->indices[nm->N], Xmue, fe));
182 PetscCall(NelderMeadReplace(nm, nm->indices[nm->N], Xmur, fr));
184 } else if (nm->f_values[nm->indices[nm->N - 1]] <= fr && fr < nm->f_values[nm->indices[nm->N]]) {
186 nm->noutcontract++;
188 PetscCall(VecAXPBYPCZ(Xmuc, 1 + nm->mu_oc, -nm->mu_oc, 0, Xbar, nm->simplex[nm->indices[nm->N]]));
191 if (fc <= fr) PetscCall(NelderMeadReplace(nm, nm->indices[nm->N], Xmuc, fc));
195 nm->nincontract++;
197 PetscCall(VecAXPBYPCZ(Xmuc, 1 + nm->mu_ic, -nm->mu_ic, 0, Xbar, nm->simplex[nm->indices[nm->N]]));
199 if (fc < nm->f_values[nm->indices[nm->N]]) PetscCall(NelderMeadReplace(nm, nm->indices[nm->N], Xmuc, fc));
204 nm->nshrink++;
207 for (i = 1; i < nm->N + 1; i++) {
208 PetscCall(VecAXPBY(nm->simplex[nm->indices[i]], 1.5, -0.5, nm->simplex[nm->indices[0]]));
209 PetscCall(TaoComputeObjective(tao, nm->simplex[nm->indices[i]], &nm->f_values[nm->indices[i]]));
211 PetscCall(VecAXPBY(Xbar, 1.5 * nm->oneOverN, -0.5, nm->simplex[nm->indices[0]]));
214 PetscCall(VecAXPY(Xbar, nm->oneOverN, nm->simplex[nm->indices[nm->N]]));
215 PetscCall(NelderMeadSort(nm));
217 PetscCall(VecAXPY(Xbar, -nm->oneOverN, nm->simplex[nm->indices[nm->N]]));
235 TAO_NelderMead *nm;
238 PetscCall(PetscNew(&nm));
239 tao->data = (void *)nm;
252 nm->simplex = NULL;
253 nm->lambda = 1;
255 nm->mu_ic = -0.5;
256 nm->mu_oc = 0.5;
257 nm->mu_r = 1.0;
258 nm->mu_e = 2.0;