Actual source code: neldermead.c

petsc-3.8.4 2018-03-24
Report Typos and Errors
  1:  #include <../src/tao/unconstrained/impls/neldermead/neldermead.h>
  2:  #include <petscvec.h>


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

 15:   for (i=1;i<dim;i++) {
 16:     index = indices[i];
 17:     val = values[index];
 18:     for (j=i-1; j>=0 && values[indices[j]] > val; j--) {
 19:       indices[j+1] = indices[j];
 20:     }
 21:     indices[j+1] = index;
 22:   }
 23:   return(0);
 24: }


 27: /*------------------------------------------------------------*/
 28: static PetscErrorCode NelderMeadReplace(TAO_NelderMead *nm, PetscInt index, Vec Xmu, PetscReal f)
 29: {

 33:   /*  Add new vector's fraction of average */
 34:   VecAXPY(nm->Xbar,nm->oneOverN,Xmu);
 35:   VecCopy(Xmu,nm->simplex[index]);
 36:   nm->f_values[index] = f;

 38:   NelderMeadSort(nm);

 40:   /*  Subtract last vector from average */
 41:   VecAXPY(nm->Xbar,-nm->oneOverN,nm->simplex[nm->indices[nm->N]]);
 42:   return(0);
 43: }

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

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

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

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

 76:   if (tao->setupcalled) {
 77:     VecDestroyVecs(nm->N+1,&nm->simplex);
 78:     VecDestroy(&nm->Xmuc);
 79:     VecDestroy(&nm->Xmue);
 80:     VecDestroy(&nm->Xmur);
 81:     VecDestroy(&nm->Xbar);
 82:   }
 83:   PetscFree(nm->indices);
 84:   PetscFree(nm->f_values);
 85:   PetscFree(tao->data);
 86:   tao->data = 0;
 87:   return(0);
 88: }

 90: /*------------------------------------------------------------*/
 91: static PetscErrorCode TaoSetFromOptions_NM(PetscOptionItems *PetscOptionsObject,Tao tao)
 92: {
 93:   TAO_NelderMead *nm = (TAO_NelderMead*)tao->data;

 97:   PetscOptionsHead(PetscOptionsObject,"Nelder-Mead options");
 98:   PetscOptionsReal("-tao_nm_lamda","initial step length","",nm->lamda,&nm->lamda,NULL);
 99:   PetscOptionsReal("-tao_nm_mu","mu","",nm->mu_oc,&nm->mu_oc,NULL);
100:   nm->mu_ic = -nm->mu_oc;
101:   nm->mu_r = nm->mu_oc*2.0;
102:   nm->mu_e = nm->mu_oc*4.0;
103:   PetscOptionsTail();
104:   return(0);
105: }

107: /*------------------------------------------------------------*/
108: static PetscErrorCode TaoView_NM(Tao tao,PetscViewer viewer)
109: {
110:   TAO_NelderMead *nm = (TAO_NelderMead*)tao->data;
111:   PetscBool      isascii;

115:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
116:   if (isascii) {
117:     PetscViewerASCIIPushTab(viewer);
118:     PetscViewerASCIIPrintf(viewer,"expansions: %D\n",nm->nexpand);
119:     PetscViewerASCIIPrintf(viewer,"reflections: %D\n",nm->nreflect);
120:     PetscViewerASCIIPrintf(viewer,"inside contractions: %D\n",nm->nincontract);
121:     PetscViewerASCIIPrintf(viewer,"outside contractionss: %D\n",nm->noutcontract);
122:     PetscViewerASCIIPrintf(viewer,"Shrink steps: %D\n",nm->nshrink);
123:     PetscViewerASCIIPopTab(viewer);
124:   }
125:   return(0);
126: }

128: /*------------------------------------------------------------*/
129: static PetscErrorCode TaoSolve_NM(Tao tao)
130: {
131:   PetscErrorCode     ierr;
132:   TAO_NelderMead     *nm = (TAO_NelderMead*)tao->data;
133:   TaoConvergedReason reason;
134:   PetscReal          *x;
135:   PetscInt           i;
136:   Vec                Xmur=nm->Xmur, Xmue=nm->Xmue, Xmuc=nm->Xmuc, Xbar=nm->Xbar;
137:   PetscReal          fr,fe,fc;
138:   PetscInt           shrink;
139:   PetscInt           low,high;

142:   nm->nshrink =      0;
143:   nm->nreflect =     0;
144:   nm->nincontract =  0;
145:   nm->noutcontract = 0;
146:   nm->nexpand =      0;

148:   if (tao->XL || tao->XU || tao->ops->computebounds) {
149:     PetscPrintf(((PetscObject)tao)->comm,"WARNING: Variable bounds have been set but will be ignored by NelderMead algorithm\n");
150:   }

152:   VecCopy(tao->solution,nm->simplex[0]);
153:   TaoComputeObjective(tao,nm->simplex[0],&nm->f_values[0]);
154:   nm->indices[0]=0;
155:   for (i=1;i<nm->N+1;i++){
156:     VecCopy(tao->solution,nm->simplex[i]);
157:     VecGetOwnershipRange(nm->simplex[i],&low,&high);
158:     if (i-1 >= low && i-1 < high) {
159:       VecGetArray(nm->simplex[i],&x);
160:       x[i-1-low] += nm->lamda;
161:       VecRestoreArray(nm->simplex[i],&x);
162:     }

164:     TaoComputeObjective(tao,nm->simplex[i],&nm->f_values[i]);
165:     nm->indices[i] = i;
166:   }

168:   /*  Xbar  = (Sum of all simplex vectors - worst vector)/N */
169:   NelderMeadSort(nm);
170:   VecSet(Xbar,0.0);
171:   for (i=0;i<nm->N;i++) {
172:     VecAXPY(Xbar,1.0,nm->simplex[nm->indices[i]]);
173:   }
174:   VecScale(Xbar,nm->oneOverN);
175:   reason = TAO_CONTINUE_ITERATING;
176:   while (1) {
177:     shrink = 0;
178:     VecCopy(nm->simplex[nm->indices[0]],tao->solution);
179:     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,&reason);
180:     if (reason != TAO_CONTINUE_ITERATING) break;

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

186:     if (nm->f_values[nm->indices[0]] <= fr && fr < nm->f_values[nm->indices[nm->N-1]]) {
187:       /*  reflect */
188:       nm->nreflect++;
189:       PetscInfo(0,"Reflect\n");
190:       NelderMeadReplace(nm,nm->indices[nm->N],Xmur,fr);
191:     } else if (fr < nm->f_values[nm->indices[0]]) {
192:       /*  expand */
193:       nm->nexpand++;
194:       PetscInfo(0,"Expand\n");
195:       VecAXPBYPCZ(Xmue,1+nm->mu_e,-nm->mu_e,0,Xbar,nm->simplex[nm->indices[nm->N]]);
196:       TaoComputeObjective(tao,Xmue,&fe);
197:       if (fe < fr) {
198:         NelderMeadReplace(nm,nm->indices[nm->N],Xmue,fe);
199:       } else {
200:         NelderMeadReplace(nm,nm->indices[nm->N],Xmur,fr);
201:       }
202:     } else if (nm->f_values[nm->indices[nm->N-1]] <= fr && fr < nm->f_values[nm->indices[nm->N]]) {
203:       /* outside contraction */
204:       nm->noutcontract++;
205:       PetscInfo(0,"Outside Contraction\n");
206:       VecAXPBYPCZ(Xmuc,1+nm->mu_oc,-nm->mu_oc,0,Xbar,nm->simplex[nm->indices[nm->N]]);

208:       TaoComputeObjective(tao,Xmuc,&fc);
209:       if (fc <= fr) {
210:         NelderMeadReplace(nm,nm->indices[nm->N],Xmuc,fc);
211:       } else shrink=1;
212:     } else {
213:       /* inside contraction */
214:       nm->nincontract++;
215:       PetscInfo(0,"Inside Contraction\n");
216:       VecAXPBYPCZ(Xmuc,1+nm->mu_ic,-nm->mu_ic,0,Xbar,nm->simplex[nm->indices[nm->N]]);
217:       TaoComputeObjective(tao,Xmuc,&fc);
218:       if (fc < nm->f_values[nm->indices[nm->N]]) {
219:         NelderMeadReplace(nm,nm->indices[nm->N],Xmuc,fc);
220:       } else shrink = 1;
221:     }

223:     if (shrink) {
224:       nm->nshrink++;
225:       PetscInfo(0,"Shrink\n");

227:       for (i=1;i<nm->N+1;i++) {
228:         VecAXPBY(nm->simplex[nm->indices[i]],1.5,-0.5,nm->simplex[nm->indices[0]]);
229:         TaoComputeObjective(tao,nm->simplex[nm->indices[i]], &nm->f_values[nm->indices[i]]);
230:       }
231:       VecAXPBY(Xbar,1.5*nm->oneOverN,-0.5,nm->simplex[nm->indices[0]]);

233:       /*  Add last vector's fraction of average */
234:       VecAXPY(Xbar,nm->oneOverN,nm->simplex[nm->indices[nm->N]]);
235:       NelderMeadSort(nm);
236:       /*  Subtract new last vector from average */
237:       VecAXPY(Xbar,-nm->oneOverN,nm->simplex[nm->indices[nm->N]]);
238:     }
239:   }
240:   return(0);
241: }

243: /* ---------------------------------------------------------- */
244: /*MC
245:  TAONM - Nelder-Mead solver for derivative free, unconstrained minimization

247:  Options Database Keys:
248: + -tao_nm_lamda - initial step length
249: . -tao_nm_mu - expansion/contraction factor

251:  Level: beginner
252: M*/

254: PETSC_EXTERN PetscErrorCode TaoCreate_NM(Tao tao)
255: {
256:   TAO_NelderMead *nm;

260:   PetscNewLog(tao,&nm);
261:   tao->data = (void*)nm;

263:   tao->ops->setup = TaoSetUp_NM;
264:   tao->ops->solve = TaoSolve_NM;
265:   tao->ops->view = TaoView_NM;
266:   tao->ops->setfromoptions = TaoSetFromOptions_NM;
267:   tao->ops->destroy = TaoDestroy_NM;

269:   /* Override default settings (unless already changed) */
270:   if (!tao->max_it_changed) tao->max_it = 2000;
271:   if (!tao->max_funcs_changed) tao->max_funcs = 4000;

273:   nm->simplex = 0;
274:   nm->lamda = 1;

276:   nm->mu_ic = -0.5;
277:   nm->mu_oc = 0.5;
278:   nm->mu_r = 1.0;
279:   nm->mu_e = 2.0;

281:   return(0);
282: }