Xmipp  v3.23.11-Nereus
IntPoly.cpp
Go to the documentation of this file.
1 /*
2 
3 CONDOR 1.06 - COnstrained, Non-linear, Direct, parallel Optimization
4  using trust Region method for high-computing load,
5  noisy functions
6 Copyright (C) 2004 Frank Vanden Berghen
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation version 2
11 of the License.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 
22 If you want to include this tools in any commercial product,
23 you can contact the author at fvandenb@iridia.ulb.ac.be
24 
25 */
26 #include <stdio.h>
27 #include "Poly.h"
28 #include "Vector.h"
29 #include "tools.h"
30 #include "KeepBests.h"
31 #include "IntPoly.h"
32 
33 Vector LAGMAXModified(Vector G, Matrix H, double rho,double &VMAX);
34 
35 /*
36 Vector *GenerateData(double **valuesF, double rho,
37  Vector Base, double vBase, ObjectiveFunction *of )
38 // generate points to allow start of fitting a polynomial of second degree
39 // around point Base
40 {
41  int j,k,dim=Base.sz(), N=(dim+1)*(dim+2)/2;
42  double *vf=(double*)malloc(2*N*sizeof(double));
43  int *failed=(int*)(vf+N); // value objective function
44  *valuesF=vf;
45  Vector *ap=new Vector[ N-1 ], *cp=ap, cur; // ap: allPoints
46  // cp: current Point
47  double *sigma=(double*)malloc(dim*sizeof(double));
48 
49  for (j=0; j<dim;j++)
50  {
51  cur=Base.clone();
52  cur[j]+=rho;
53 
54  *(cp++)=cur;
55  }
56 
57  calculateNParallelJob(dim,vf,ap,of,failed);
58 
59  for (j=0; j<dim; j++)
60  {
61  cur=Base.clone();
62  if (*(vf++)<vBase) { cur[j]+=2*rho; sigma[j]=rho; }
63  else { cur[j]-=rho; sigma[j]=-rho; }
64  *(cp++)=cur;
65  }
66 
67  for (j=0; j<dim; j++)
68  {
69  for (k=0; k<j; k++)
70  {
71  cur=Base.clone();
72  cur[j]+=sigma[j];
73  cur[k]+=sigma[k];
74  *(cp++)=cur;
75  }
76  }
77 
78  free(sigma);
79 
80  // parallelize here !
81  calculateNParallelJob(N-dim-1,vf,ap+dim,of,failed);
82 
83  return ap;
84 }
85 */
86 
87 // Note:
88 // Vectors do come from outside. Newton Basis and associated permutation
89 // vector are generated internally and can be deleted.
90 
91 /*
92 double *InterPolynomial::NewtonCoefficient(double *yy)
93 {
94  // Initialize to local variables
95  unsigned N=nPtsUsed,i;
96  Polynomial *pp=NewtonBasis;
97  Vector *xx=NewtonPoints;
98  double *ts=(double*)calloc(N,sizeof(double)), *tt=ts;
99 
100  if (!ts)
101  {
102  printf("NewtonCoefficient : No mem\n");
103  getchar(); exit(251);
104  }
105 
106  for (i=0; i<N; i++) // 0th difference everywhere
107  *(tt++) = yy[i];
108 
109  unsigned deg=d->deg, Dim=d->dim, Nfrom, Nto, j, curDeg;
110  double *ti, *tj;
111 
112  for (curDeg=0, Nfrom=0; curDeg<deg; Nfrom=Nto )
113  {
114  Nto=Nfrom+choose( curDeg+Dim-1,Dim-1 );
115  for (ti=ts+Nto, i=Nto; i<N; i++,ti++)
116  {
117  for (tj=ts+Nfrom, j=Nfrom; j<Nto; j++, tj++)
118  *ti -= *tj ? // Evaluation takes time
119  *tj * (pp[j])( xx[i] ) : 0;
120  }
121  curDeg++;
122  }
123  return ts;
124 }
125 */
126 
127 void InterPolynomial::ComputeLagrangeBasis(double *yy, unsigned nPtsTotal)
128 {
129 #ifdef NOOBJECTIVEFUNCTION
130  const double eps = 1e-6;
131 #else
132  const double eps = 1e-10;
133 #endif
134  const double good = 1 ;
135  unsigned dim=d->dim,i;
136 
137  Vector *xx=NewtonPoints, xtemp;
138  Polynomial *pp= new Polynomial[nPtsUsed], *qq=pp;
139  NewtonBasis=pp;
140 
141  if (!pp)
142  {
143  printf("ComputeNewtonBasis( ... ) : Alloc for polynomials failed\n");
144  getchar(); exit(251);
145  }
146 
147  MultInd I(dim);
148  for (i=0; i<nPtsUsed; i++)
149  {
150  *(qq++)=Polynomial(I);
151  I++;
152  }
153 
154  unsigned k, kmax;
155  double v, vmax, vabs;
156 #ifdef VERBOSE
157  printf("Constructing first quadratic ... (N=%i)\n",nPtsUsed);
158 #endif
159  for (i=0; i<nPtsUsed; i++)
160  {
161 #ifdef VERBOSE
162  printf(".");
163 #endif
164  vmax = vabs = 0;
165  kmax = i;
166  if (i==0)
167  {
168  // to be sure point 0 is always taken:
169  vmax=(pp[0])( xx[0] );
170  } else
171  for (k=i; k<nPtsTotal; k++) // Pivoting
172  {
173  v=(pp[i])( xx[k] );
174  if (fabs(v) > vabs)
175  {
176  vmax = v;
177  vabs = condorAbs(v);
178  kmax = k;
179  }
180  if (fabs(v) > good ) break;
181  }
182 
183  // Now, check ...
184  if (fabs(vmax) < eps)
185  {
186  printf("Cannot construct Lagrange basis");
187  delete[] NewtonBasis;
188  NewtonBasis=NULL;
189  return;
190  }
191 
192  // exchange component i and k of NewtonPoints
193  // fast because of shallow copy
194  xtemp =xx[kmax];
195  xx[kmax]=xx[i];
196  xx[i] =xtemp;
197 
198  // exchange component i and k of newtonData
199  v =yy[kmax];
200  yy[kmax]=yy[i];
201  yy[i] =v;
202 
203  pp[i]/=vmax;
204  for (k=0; k<i; k++) pp[k] -= (pp[k])( xx[i] ) * pp[i];
205  for (k=i+1; k<nPtsUsed; k++) pp[k] -= (pp[k])( xx[i] ) * pp[i];
206 
207  // Next polynomial, break if necessary
208  }
209 #ifdef VERBOSE
210  printf("\n");
211 #endif
212 }
213 
214 int findK(double *ValuesF, int n, ObjectiveFunction *of, Vector *points)
215 {
216  // if (of->isConstrained) return 0;
217  // find index k of the best value of the function
218  double minimumValueF=INF;
219  int i,k=-1;
220  for (i=0; i<n; i++)
221  if ((ValuesF[i]<minimumValueF)
222  &&((of==NULL)||(of->isFeasible(points[i]))))
223  { k=i; minimumValueF=ValuesF[i]; }
224  if (k==-1) k=0;
225  return k;
226 }
227 
228 InterPolynomial::InterPolynomial( unsigned _deg, double rho,Vector _vBase, Matrix data, ObjectiveFunction *of)
229 // unsigned _nPtsTotal, Vector *_Pp, double *_Yp )
230  : Polynomial(data.nColumn()-2, _deg), M(0.0), nUpdateOfM(0), NewtonBasis(NULL), NewtonPoints(NULL), kbest(-1)
231 {
232  nPtsUsed=choose( _deg+d->dim,d->dim );
233  if (data.nLine()==0)
234  {
235  printf( "InterPolynomial::InterPolynomial( double *) : No data\n");
236  getchar(); exit(-1);
237  }
238 
239  // Generate basis
240  if (_vBase==Vector::emptyVector)
241  {
242  double rhosmall=rho*ROOT2*.5;
243  GenerateBasis(rho,rhosmall, data,NULL);
244  if (!NewtonBasis)
245  {
246  GenerateBasis(rho,rho*.5, data,of);
247  }
248  } else
249  {
250  int i,ii,j=0,k,mdim=dim();
251  double norm, *p, *pb=_vBase;
252  KeepBests kb(nPtsUsed*2,mdim);
253 // fprintf(stderr,"Value Objective=%e\n", vBase);
254 
255  ii=data.lineIndex(_vBase);
256  if (ii!=-1)
257  {
258  p=data[ii];
259  kb.add(0.0,p[mdim],p); j++;
260  }
261 
262  i=data.nLine();
263  while (i--)
264  {
265  if (i==ii) continue;
266  p=data[i];
267  if (p[mdim+1]) continue;
268  norm=0; k=mdim;
269  while (k--) norm+=sqr(p[k]-pb[k]);
270  if ((rho==0.0)||
271  ((norm<=4.00*rho*rho))//&&(norm>.25*rhomin*rhomin))
272  )
273  {
274  kb.add(norm,p[mdim], p); j++;
275  }
276  }
277  if (j<(int)nPtsUsed)
278  {
279  printf("Not Enough points in DB to build interpolation polynomial\n");
280  return;
281  }
282  // we have retained only the 2*nPtsUsed best points:
283  j=mmin(j,(int)(2*nPtsUsed));
284  NewtonPoints=new Vector[j];
285  valuesF=(double*)malloc(j*sizeof(double));
286  for (i=0; i<j; i++)
287  {
288  valuesF[i]=kb.getValue(i);
289  NewtonPoints[i]=Vector(mdim, kb.getOptValue(i));
290  }
291  //kbest=findK(valuesF,j,NULL,NewtonPoints);
292  //vBase=NewtonPoints[kbest].clone();
293  vBase=_vBase;
294  for (i=0; i<j; i++) NewtonPoints[i]-=vBase;
296  }
297  if (NewtonBasis==NULL) return;
298 
299 // test();
300 
301  // Compute Interpolant
302 // double *NewtonCoefPoly=NewtonCoefficient(_Yp);
303  double *NewtonCoefPoly=valuesF;
304 
305  double *coc= NewtonCoefPoly+nPtsUsed-1;
306  Polynomial *ppc= NewtonBasis+nPtsUsed-1;
307  this->copyFrom((Polynomial)(*coc * *ppc)); //take highest degree
308 
309  int i=nPtsUsed-1;
310  if (i)
311  while(i--)
312  (*this) += *(--coc) * *(--ppc);
313  // No reallocation here because of the order of
314  // the summation
315  //free(NewtonCoefPoly);
316 }
317 
318 #ifdef NOOBJECTIVEFUNCTION
319 int calculateNParallelJob(int n,double *vf,Vector *cp, ObjectiveFunction *of, int *notsuccess){return 0;}
320 #else
321 int calculateNParallelJob(int n,double *vf,Vector *cp, ObjectiveFunction *of, int *notsuccess);
322 #endif
323 
324 void InterPolynomial::GenerateBasis(double rho,double rhosmall,Matrix data,ObjectiveFunction *of)
325 // generate points to allow start of fitting a polynomial of second degree
326 // around point data[0]
327 {
328  if (deg()!=2)
329  {
330  printf("The GenerateBasis function only works for interpolation polynomials of degree 2.\n");
331  exit(244);
332  }
333  int i,j,k,l,dim=data.nColumn()-2, N=(dim+1)*(dim+2)/2,
334  *failed=(int*)malloc(N*sizeof(int)),nToCompute, nl=data.nLine(),best;
335  Vector Base=data.getLine(0,dim);
336  VectorInt vi(dim);
337  valuesF=(double*)malloc((N+dim)*sizeof(double));
338  double dBase=((double**)data)[0][dim],*vrho=valuesF+N,
339  *base=Base,*dcur,*p,*pb, norm, normbest, r; // value objective function
340 
341  NewtonPoints=new Vector[N];
342  Vector *cp, cur; // cp: current Point
343  NewtonPoints[N-1]=Base.clone(); valuesF[N-1]=dBase;
344 
345  // generate perfect sampling site
346  for (i=0; i<dim; i++)
347  {
348  cur=Base.clone();
349  cur[i]+=rho;
350  NewtonPoints[i]=cur;
351  vi[i]=i;
352  }
353 
354  nToCompute=0; cp=NewtonPoints;
355  // try to find good points in DB, which are near perfect sampling sites
356  for (i=0; i<dim; i++)
357  {
358  normbest=INF; pb=cp[i];
359  for (j=1; j<nl; j++)
360  {
361  p=data[j];
362  if (p[dim+1]) continue;
363  norm=0; k=dim;
364  while (k--) norm+=sqr(p[k]-pb[k]);
365  if (normbest>norm) { normbest=norm; best=j; }
366  }
367  if (normbest<rhosmall*rhosmall)
368  {
369  cp[i]=data.getLine(best,dim);
370  valuesF[i]=((double**)data)[best][dim];
371  } else
372  {
373  cur=cp[nToCompute]; cp[nToCompute]=cp[i]; cp[i]=cur;
374  l=vi[nToCompute]; vi[nToCompute]=vi[i]; vi[i]=l;
375  valuesF[i]=valuesF[nToCompute];
376  nToCompute++;
377  }
378  }
379 
380  if ((!of)&&(nToCompute)) { free(failed); free(valuesF); delete[] NewtonPoints; return; }
381 
382  // if some points have not been found in DB, start evaluation
383  while (nToCompute>0)
384  {
385  // parallelize here !
386  calculateNParallelJob(nToCompute,valuesF,NewtonPoints,of,failed);
387 
388  k=0;
389  for (j=0; j<nToCompute; j++)
390  {
391  of->saveValue(cp[j],valuesF[j],failed[j]);
392  if (failed[j])
393  {
394  dcur=cp[j];
395  for (i=0; i<dim; i++)
396  {
397  dcur[i]=base[i]+(base[i]-dcur[i])*.7;
398  }
399 
400  // group all missing values at the beginning (around NewtonPoints+dim)
401  cur=cp[k]; cp[k]=cp[j]; cp[j]=cur;
402  l=vi[k]; vi[k]=vi[j]; vi[j]=l;
403  valuesF[j]=valuesF[k];
404  k++;
405  }
406  }
407  nToCompute=k;
408  }
409 
410  // re-order vectors (based on vi)
411  for (j=0; j<dim-1; j++)
412  {
413  k=j; while (vi[k]!=j) k++;
414  if (k==j) continue;
415  cur=cp[k]; cp[k]=cp[j]; cp[j]=cur;
416  l=vi[k]; vi[k]=vi[j]; vi[j]=l;
417  r=valuesF[k]; valuesF[k]=valuesF[j]; valuesF[j]=r;
418  }
419 
420  // select again some good sampling sites
421  cp=NewtonPoints+dim;
422  for (j=0; j<dim; j++)
423  {
424  cur=Base.clone();
425  dcur=NewtonPoints[j];
426  r=dcur[j]-base[j];
427  if (valuesF[j]<dBase) { cur[j]+=r*2.0; vrho[j]=r; }
428  else { cur[j]-=r; vrho[j]=-r; }
429  *(cp++)=cur;
430  }
431  for (j=0; j<dim; j++)
432  {
433  for (k=0; k<j; k++)
434  {
435  cur=Base.clone();
436  cur[j]+=vrho[j];
437  cur[k]+=vrho[k];
438  *(cp++)=cur;
439  }
440  }
441 
442  nToCompute=0;
443  cp=NewtonPoints+dim;
444  // try to find good points in DB, which are near perfect sampling sites
445  for (i=0; i<N-dim-1; i++)
446  {
447  normbest=INF; pb=cp[i];
448  for (j=1; j<nl; j++)
449  {
450  p=data[j];
451  if (p[dim+1]) continue;
452  norm=0; k=dim;
453  while (k--) norm+=sqr(p[k]-pb[k]);
454  if (normbest>norm) { normbest=norm; best=j; }
455  }
456  if (normbest<rhosmall*rhosmall)
457  {
458  cp[i]=data.getLine(best,dim);
459  valuesF[i+dim]=((double**)data)[best][dim];
460  } else
461  {
462  cur=cp[nToCompute];
463  cp[nToCompute]=cp[i];
464  cp[i]=cur;
465  valuesF[i+dim]=valuesF[nToCompute+dim];
466  nToCompute++;
467  }
468  }
469 
470  if ((!of)&&(nToCompute)) { free(failed); free(valuesF); delete[] NewtonPoints; return; }
471 
472  while (nToCompute>0)
473  {
474  // parallelize here !
475  calculateNParallelJob(nToCompute,valuesF+dim,cp,of,failed);
476 
477  k=0;
478  for (j=0; j<nToCompute; j++)
479  {
480  of->saveValue(cp[j],valuesF[j+dim],failed[j]);
481  if (failed[j])
482  {
483  dcur=cp[j];
484  for (i=0; i<dim; i++)
485  {
486  dcur[i]=base[i]+(base[i]-dcur[i])*.65;
487  }
488 
489  // group all missing values at the beginning (around NewtonPoints+dim)
490  cur=cp[k];
491  cp[k]=cp[j];
492  cp[j]=cur;
493  valuesF[j+dim]=valuesF[k+dim];
494  k++;
495  }
496  }
497  nToCompute=k;
498 
499  }
500  free(failed);
501 
502  // "insertion sort" to always place best points first
503  for (i=0; i<N-1; i++)
504  {
505  kbest=findK(valuesF+i,N-i,of,NewtonPoints+i)+i;
506 
507  // to accept infeasible points for i>=1 :
508  of=NULL;
509 
512  }
513  kbest=0;
514  vBase=NewtonPoints[0].clone();
515  for (i=0; i<N; i++) NewtonPoints[i]-=vBase;
517 }
518 
519 void InterPolynomial::updateM(Vector newPoint, double valueF)
520 {
521  //not tested
522  unsigned i=nPtsUsed;
523  double sum=0,a;
525  Vector *xx=NewtonPoints;
526 
527  while (i--)
528  {
529  a=newPoint.euclidianDistance(xx[i]);
530  sum+=condorAbs(pp[i]( newPoint ))*a*a*a;
531  }
532  M=mmax(M, condorAbs((*this)(newPoint)-valueF)/sum);
533  nUpdateOfM++;
534 }
535 
537 {
538  unsigned i=nPtsUsed;
539  double sum=0,a;
541  Vector *xx=NewtonPoints;
542 
543  while (i--)
544  {
545  a=Point.euclidianDistance(xx[i]);
546  sum+=condorAbs(pp[i]( Point ))*a*a*a;
547  }
548  return M*sum;
549 }
550 
552  double rho, Vector pointToAdd, double *maxd)
553 {
554  //not tested
555 
556  // excludeFromT is set to k if not success from optim and we want
557  // to be sure that we keep the best point
558  // excludeFromT is set to -1 if we can replace the point x_k by
559  // pointToAdd(=x_k+d) because of the success of optim.
560 
561  // chosen t: the index of the point inside the newtonPoints
562  // which will be replaced.
563 
564  Vector *xx=NewtonPoints;
565  Vector XkHat;
566  if (excludeFromT>=0) XkHat=xx[excludeFromT];
567  else XkHat=pointToAdd;
568 
569  int t=-1, i, N=nPtsUsed;
570  double a, aa, maxa=-1.0, maxdd=0;
572 
573  // if (excludeFromT>=0) maxa=1.0;
574 
575  for (i=0; i<N; i++)
576  {
577  if (i==excludeFromT) continue;
578  aa=XkHat.euclidianDistance(xx[i]);
579  if (aa==0.0) return -1;
580  a=aa/rho;
581  // because of the next line, rho is important:
582  a=mmax(a*a*a,1.0);
583  a*=condorAbs(pp[i] (pointToAdd));
584 
585  if (a>maxa)
586  {
587  t=i; maxa=a; maxdd=aa;
588  }
589  }
590  if (maxd) *maxd=maxdd;
591  return t;
592 }
593 /*
594 void InterPolynomial::check(Vector Base, double (*f)( Vector ) )
595 {
596  int i,n=sz();
597  double r, bound;
598 
599  for (i=0; i<n; i++)
600  {
601  r=(*f)(NewtonPoints[i]+Base);
602  bound=(*this)(NewtonPoints[i]);
603  if ((condorAbs(bound-r)>1e-15)&&(condorAbs(bound-r)>1e-3*condorAbs(bound)))
604  {
605  printf("error\n");
606  test();
607  }
608 // for (j=0; j<n; j++)
609 // r=poly.NewtonBasis[j](poly.NewtonPoints[i]);
610  }
611 }
612 
613 void InterPolynomial::test()
614 {
615  unsigned i,j,n=d->n; Matrix M(n,n); double **m=M;
616  for (i=0; i<n; i++)
617  for (j=0; j<n; j++)
618  m[i][j]=NewtonBasis[i](NewtonPoints[j]);
619  M.print();
620 };
621 */
622 void InterPolynomial::replace(int t, Vector pointToAdd, double valueF)
623 {
624  //not tested
625  updateM(pointToAdd, valueF);
626  if (t<0) return;
627 
628  Vector *xx=NewtonPoints;
630  int i, N=nPtsUsed;
631  double t2=(pp[t]( pointToAdd ));
632 
633  if (t2==0) return;
634 
635  t1=pp[t]/=t2;
636 
637  for (i=0; i<t; i++) pp[i]-= pp[i]( pointToAdd )*t1;
638  for (i=t+1; i<N; i++) pp[i]-= pp[i]( pointToAdd )*t1;
639  xx[t].copyFrom(pointToAdd);
640 
641  // update the coefficents of general poly.
642 
643  valueF-=(*this)(pointToAdd);
644  if (condorAbs(valueF)>1e-11) (*this)+=valueF*pp[t];
645 
646 // test();
647 }
648 
649 int InterPolynomial::maybeAdd(Vector pointToAdd, unsigned k, double rho, double valueF)
650 // input: pointToAdd, k, rho, valueF
651 // output: updated polynomial
652 {
653 
654  unsigned i,N=nPtsUsed;
655  int j = 0;
656  Vector *xx=NewtonPoints, xk=xx[k];
657  double distMax=-1.0,dd;
658  /*
659  Polynomial *pp=NewtonBasis;
660  Vector *xx=NewtonPoints, xk=xx[k],vd;
661  Matrix H(n,n);
662  Vector GXk(n); //,D(n);
663 */
664  // find worst point/newton poly
665 
666  for (i=0; i<N; i++)
667  {
668  dd=xk.euclidianDistance(xx[i]);
669  if (dd>distMax) { j=i; distMax=dd; };
670  }
671  dd=xk.euclidianDistance(pointToAdd);
672 
673  // no tested:
674 
675  if (condorAbs(NewtonBasis[j](pointToAdd))*distMax*distMax*distMax/(dd*dd*dd)>1.0)
676  {
677  printf("good point found.\n");
678  replace(j, pointToAdd, valueF);
679  return 1;
680  }
681  return 0;
682 }
683 
684 int InterPolynomial::checkIfValidityIsInBound(Vector ddv, unsigned k, double bound, double rho)
685 // input: k,bound,rho
686 // output: j,ddv
687 {
688 
689  // check validity around x_k
690  // bound is epsilon in the paper
691  // return index of the worst point of J
692  // if (j==-1) then everything OK : next : trust region step
693  // else model step: replace x_j by x_k+d where d
694  // is calculated with LAGMAX
695 
696  unsigned i,N=nPtsUsed, n=dim();
697  int j;
699  Vector *xx=NewtonPoints, xk=xx[k],vd;
700  Vector Distance(N);
701  double *dist=Distance, *dd=dist, distMax, vmax, tmp;
702  Matrix H(n,n);
703  Vector GXk(n); //,D(n);
704 
705  for (i=0; i<N; i++) *(dd++)=xk.euclidianDistance(xx[i]);
706 
707  while (true)
708  {
709  dd=dist; j=-1; distMax=2*rho;
710  for (i=0; i<N; i++)
711  {
712  if (*dd>distMax) { j=i; distMax=*dd; };
713  dd++;
714  }
715  if (j<0) return -1;
716 
717  // to prevent to choose the same point once again:
718  dist[j]=0;
719 
720  pp[j].gradientHessian(xk,GXk,H);
721 // d=H.multiply(xk);
722 // d.add(G);
723 
724  tmp=M*distMax*distMax*distMax;
725 
726  if (tmp*rho*(GXk.euclidianNorm()+0.5*rho*H.frobeniusNorm())>=bound)
727  {
728 /* vd=L2NormMinimizer(pp[j], xk, rho);
729  vd+=xk;
730  vmax=condorAbs(pp[j](vd));
731 
732  Vector vd2=L2NormMinimizer(pp[j], xk, rho);
733  vd2+=xk;
734  double vmax2=condorAbs(pp[j](vd));
735 
736  if (vmax<vmax2) { vmax=vmax2; vd=vd2; }
737 */
738  vd=LAGMAXModified(GXk,H,rho,vmax);
739 // tmp=vd.euclidianNorm();
740  vd+=xk;
741  vmax=condorAbs(pp[j](vd));
742 
743  if (tmp*vmax>=bound) break;
744  }
745  }
746  if (j>=0) ddv.copyFrom(vd);
747  return j;
748 }
749 
751 // input: k,rho
752 // output: d,r
753 {
754  //not tested
755 
756  unsigned i, N=nPtsUsed, n=dim();
757  int ii, j,r=0;
759  Vector *xx=NewtonPoints, xk,vd;
760  Vector Distance(N);
761  double *dist=Distance, *dd=dist, distMax, vmax;
762  Matrix H(n,n);
763  Vector GXk(n);
764  if (k>=0) xk=xx[k]; else xk=*v;
765 
766  for (i=0; i<N; i++) *(dd++)=xk.euclidianDistance(xx[i]);
767 
768  for (ii=0; ii<d.nLine(); ii++)
769  {
770  dd=dist; j=-1;
771  distMax=-1.0;
772  for (i=0; i<N; i++)
773  {
774  if (*dd>distMax) { j=i; distMax=*dd; };
775  dd++;
776  }
777  // to prevent to choose the same point once again:
778  dist[j]=-1.0;
779 
780  if (distMax>2*rho) r++;
781  pp[j].gradientHessian(xk,GXk,H);
782  vd=LAGMAXModified(GXk,H,rho,vmax);
783  vd+=xk;
784 
785  d.setLine(ii,vd);
786  }
787  return r;
788 }
789 
791 {
792  if (translation==Vector::emptyVector) return;
793  vBase+=translation;
794  Polynomial::translate(translation);
795  int i=nPtsUsed;
796  while (i--) NewtonBasis[i].translate(translation);
797  i=nPtsUsed;
798  while (i--) if (NewtonPoints[i]==translation) NewtonPoints[i].zero();
799  else NewtonPoints[i]-=translation;
800 }
801 
802 
803 // to allow shallow copy:
804 
806 {
807  if (!d) return;
808  if (d->ref_count==1)
809  {
810  if (NewtonBasis)
811  {
812  delete[] NewtonBasis;
813  delete[] NewtonPoints;
814  free(valuesF);
815  }
816  }
817 }
818 
820 {
822 }
823 
825 {
826  // shallow copy for inter poly.
827  d=A.d;
830 // ValuesF=A.ValuesF;
831  M=A.M;
832  nPtsUsed=A.nPtsUsed;
834  (d->ref_count)++;
835 
836 }
837 
839 {
840  // shallow copy
841  if (this != &A)
842  {
844 
845  d=A.d;
848 // ValuesF=A.ValuesF;
849  M=A.M;
850  nPtsUsed=A.nPtsUsed;
852  kbest=A.kbest;
853  vBase=A.vBase;
854  valuesF=A.valuesF;
855 
856  (d->ref_count) ++ ;
857  }
858  return *this;
859 }
860 
861 InterPolynomial::InterPolynomial(unsigned _dim, unsigned _deg): Polynomial(_dim, _deg), M(0.0), nUpdateOfM(0)
862 {
863  nPtsUsed=choose( _deg+_dim,_dim );
866 }
867 
869 {
870  // a deep copy
871  InterPolynomial m(d->dim, d->deg);
872  m.copyFrom(*this);
873  return m;
874 }
875 
877 {
878  if (m.d->dim!=d->dim)
879  {
880  printf("poly: copyFrom: dim do not agree\n");
881  getchar(); exit(254);
882  }
883  if (m.d->deg!=d->deg)
884  {
885  printf("poly: copyFrom: degree do not agree\n");
886  getchar(); exit(254);
887  }
889  M=m.M;
890 // nPtsUsed=m.nPtsUsed; // not useful because dim and degree already agree.
892 // ValuesF
893 
894  int i=nPtsUsed;
895  while (i--)
896  {
897 // NewtonBasis[i]=m.NewtonBasis[i];
898 // NewtonPoints[i]=m.NewtonPoints[i];
901  }
902 }
903 
905 {
907 }
virtual void saveValue(Vector tmp, double valueOF, int nerror)
double interpError(Vector Point)
Definition: IntPoly.cpp:536
InterPolynomial & operator=(const InterPolynomial &A)
Definition: IntPoly.cpp:838
#define ROOT2
Definition: tools.h:45
int getGoodInterPolationSites(Matrix d, int k, double rho, Vector *v=NULL)
Definition: IntPoly.cpp:750
double getValue(int i)
Definition: KeepBests.cpp:147
unsigned long choose(unsigned n, unsigned k)
Definition: tools.cpp:38
Vector * NewtonPoints
Definition: IntPoly.h:47
void translate(Vector translation)
Definition: Poly.cpp:563
int * mmax
void GenerateBasis(double rho, double rhosmall, Matrix data, ObjectiveFunction *of)
Definition: IntPoly.cpp:324
int calculateNParallelJob(int n, double *vf, Vector *cp, ObjectiveFunction *of, int *notsuccess)
Definition: parallel.cpp:43
void destroyCurrentBuffer()
Definition: IntPoly.cpp:805
int findAGoodPointToReplace(int excludeFromT, double rho, Vector pointToAdd, double *modelStep=NULL)
Definition: IntPoly.cpp:551
void updateM(Vector newPoint, double valueF)
Definition: IntPoly.cpp:519
double rho
#define pp(s, x)
Definition: ml2d.cpp:473
Definition: Vector.h:37
unsigned dim()
Definition: Poly.h:62
double euclidianNorm()
Definition: Vector.cpp:222
double mmin(const double t1, const double t2)
Definition: tools.h:69
double euclidianDistance(Vector v)
Definition: Vector.cpp:244
unsigned deg()
Definition: Poly.h:63
T norm(const std::vector< T > &v)
Definition: vector_ops.h:399
Definition: point.h:32
int nColumn()
Definition: Matrix.h:84
cmache_1 eps
Vector clone()
Definition: Vector.cpp:207
void add(double key, double value)
Definition: KeepBests.cpp:98
#define i
int maybeAdd(Vector pointToAdd, unsigned k, double rho, double valueF)
Definition: IntPoly.cpp:649
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
void replace(int t, Vector pointToAdd, double valueF)
Definition: IntPoly.cpp:622
int nLine()
Definition: Matrix.h:83
double sqr(const double &t)
Definition: tools.h:99
void ComputeLagrangeBasis(double *, unsigned nPtsTotal)
Definition: IntPoly.cpp:127
int lineIndex(Vector r, int nn=0)
Definition: Matrix.cpp:1168
InterPolynomial clone()
Definition: IntPoly.cpp:868
char isFeasible(Vector vx, double *d=NULL)
unsigned nUpdateOfM
Definition: IntPoly.h:40
Vector LAGMAXModified(Vector G, Matrix H, double rho, double &VMAX)
Definition: MSSolver.cpp:40
void zero(int _i=0, int _n=0)
Definition: Vector.cpp:93
double * valuesF
Definition: IntPoly.h:48
double frobeniusNorm()
Definition: Matrix.cpp:1045
void gradientHessian(Vector P, Vector G, Matrix H)
Definition: Poly.cpp:515
double getOptValue(int i, int n)
Definition: KeepBests.cpp:163
Vector vBase
Definition: IntPoly.h:47
double condorAbs(const double t1)
Definition: tools.h:47
free((char *) ob)
void copyFrom(InterPolynomial a)
Definition: IntPoly.cpp:876
Definition: Matrix.h:38
Polynomial clone()
Definition: Poly.cpp:117
int findK(double *ValuesF, int n, ObjectiveFunction *of, Vector *points)
Definition: IntPoly.cpp:214
#define j
int m
PolynomialData * d
Definition: Poly.h:49
unsigned nPtsUsed
Definition: IntPoly.h:40
Vector getLine(int i, int n=0, int startCol=0)
Definition: Matrix.cpp:1094
int checkIfValidityIsInBound(Vector dd, unsigned k, double bound, double rho)
Definition: IntPoly.cpp:684
static Vector emptyVector
Definition: Vector.h:119
double M
Definition: IntPoly.h:39
void setLine(int i, Vector v, int n=0)
Definition: Matrix.cpp:1125
#define INF
Definition: svm.cpp:43
Polynomial * NewtonBasis
Definition: IntPoly.h:43
void copyFrom(Polynomial a)
Definition: Poly.cpp:125
void copyFrom(Vector r, int _n=0)
Definition: Vector.cpp:215
int * n
Polynomial()
Definition: Poly.h:54
InterPolynomial(unsigned _deg, double rho, Vector vBase, Matrix data, ObjectiveFunction *of)
Definition: IntPoly.cpp:228
doublereal * a
void translate(int k)