Changeset 7387675 in tol


Ignore:
Timestamp:
Feb 18, 2013, 2:42:06 PM (14 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
861d63d
Parents:
09c91c1
Message:

New built-in functions AutoScale and Gradient2

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@5625 be4846ae-f051-483a-831f-b64e7d08527f

Location:
tol/bmath/bfunct
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tol/bmath/bfunct/nrealfun.cpp

    r09c91c1 r7387675  
    2626#include <tol/tol_matrix.h>
    2727#include <tol/tol_brealfun.h>
     28#include <tol/tol_bfibonac.h>
    2829#include <tol/tol_btimer.h>
    2930#include <tol/tol_bstat.h>
    … …  
    7879
    7980//--------------------------------------------------------------------
     81class BProyectionFunction : public BRRFunction
     82
     83/*! Evaluates the real to real function
     84 *
     85 *    f(x) = F(P + x H)
     86 *
     87 * where F is a real vector to real function and P is the initial point
     88 * and H is the direction vector.
     89 */
     90//--------------------------------------------------------------------
     91{
     92 public:
     93  BRnRFunction& F_;
     94  BArray<BDat>  P_;
     95  BArray<BDat>  H_;
     96  BProyectionFunction(BRnRFunction& F,
     97                      const BArray<BDat>& P,
     98                      const BArray<BDat>& H)
     99  : F_(F), P_(P), H_(H) {}
     100 ~BProyectionFunction() {};
     101  void Evaluate(BDat& f, const BDat& x)
     102  {
     103    BArray<BDat> Q = P_;
     104    for(int i=0; i<Q.Size(); i++) { Q(i) += x * H_(i); }
     105    f = F_[Q];
     106  }
     107};
     108
     109//--------------------------------------------------------------------
     110class BPartialFunction : public BRRFunction
     111
     112/*! Evaluates the real to real function
     113 *
     114 *    f(x) = F(P + x*E[k])
     115 *
     116 * where F is a real vector to real function and P is the initial point
     117 * and E[k] is the direction vector of k-th coordinate.
     118 */
     119//--------------------------------------------------------------------
     120{
     121 public:
     122  BRnRFunction& F_;
     123  BArray<BDat>  P_;
     124  int k_;
     125  BPartialFunction(BRnRFunction& F, const BArray<BDat>& P, int k)
     126  : F_(F), P_(P), k_(k) {}
     127 ~BPartialFunction() {};
     128  void Evaluate(BDat& f, const BDat& x)
     129  {
     130    BArray<BDat> Q = P_;
     131    Q(k_) += x;
     132    f = F_[Q];
     133  }
     134};
     135
     136//--------------------------------------------------------------------
     137class BPartialAbsDifFunction : public BRRFunction
     138
     139/*! Evaluates the real to real function
     140 *
     141 *    f(x) = Abs(Abs(F(P + x*E[k])-F(P))-fDist)
     142 *
     143 * where F is a real vector to real function and P is the initial point
     144 * and E[k] is the direction vector of k-th coordinate.
     145 */
     146//--------------------------------------------------------------------
     147{
     148 public:
     149  BRnRFunction& F_;
     150  BArray<BDat>  P_;
     151  BDat fDist_;
     152  BDat fP_;
     153  int k_;
     154  BPartialAbsDifFunction(BRnRFunction& F, const BArray<BDat>& P, BDat fDist, int k)
     155  : F_(F), P_(P), fDist_(fDist), k_(k)
     156  {
     157    fP_ = F_[P_];
     158  }
     159 ~BPartialAbsDifFunction() {};
     160  void Evaluate(BDat& f, const BDat& x)
     161  {
     162    BArray<BDat> Q = P_;
     163    Q(k_) += x;
     164    BDat f1 = Abs(Abs(F_[Q]-fP_)-fDist_);
     165    Q(k_) -= 2*x;
     166    BDat f2 = Abs(Abs(F_[Q]-fP_)-fDist_);
     167    if(f1.IsUnknown()) { f=f2; }
     168    else if(f2.IsUnknown()) { f=f1; }
     169    else if(f1<=f2) { f=f1; }
     170    else { f=f2; }
     171  }
     172};
     173
     174
     175//--------------------------------------------------------------------
    80176// BRnRFunction functions.
    81177//--------------------------------------------------------------------
    82178
    83 
    84 //--------------------------------------------------------------------
    85 void BRnRFunction::Gradient (const BArray<BDat>& x, BArray<BDat>& G)
     179//--------------------------------------------------------------------
     180bool BRnRFunction::AutoScale (
     181  const BArray<BDat>& x,
     182  const BArray<BDat>& xMin,
     183  const BArray<BDat>& xMax,
     184  BDat fDist,
     185  BDat tolerance,
     186  BArray<BDat>& S)
    86187
    87188/*! Returns the numerical Gradient vector of this function in a point
    … …  
    89190//--------------------------------------------------------------------
    90191{
     192  S.ReallocBuffer(n_);
     193  int k;
     194  fDist = Abs(fDist);
     195  for(k=0; k<n_; k++)
     196  {
     197    BPartialAbsDifFunction Fk(*this, x, fDist, k);
     198    BDat fd;
     199    BDat hMin = 0;
     200    BDat hMax = Maximum(Abs(x[k]-xMin[k]),Abs(x[k]-xMax[k]));
     201    S[k] = BFibonacci::Minimum(&Fk,fd,hMin,hMax,tolerance);
     202  }
     203  return(true);
     204/*
     205  BDat f, f0, df, a,b,c;
     206  BBool ok;
     207  int i,j;
     208  (*this).Evaluate(f0, x); ok = f0.IsFinite();
     209  if(!ok) { return(false); }
     210  fDist = Abs(fDist);
     211  for(i=0; i<n_; i++)
     212  {
     213    BDat x0 = x[i];
     214    BDat h = Distance();
     215    x[i] = x0+h;
     216    (*this).Evaluate(f, x); ok = f.IsFinite();
     217    if(!ok)
     218    {
     219      h*=-1;
     220      x[i] = x0+h;
     221      (*this).Evaluate(f, x); ok = f.IsFinite();
     222    }
     223    df = Abs(f-f0);
     224    a=x0;
     225    b=x[i];
     226    for(j=0;j<maxIter;j++)
     227    {
     228      if(!ok) { break; }
     229      if(Abs(df-fDist)<=tolerance) { break; }
     230      else if(df>fDist)            { c; }
     231      else                         { s*=2; }
     232      x[i] = x0+h;
     233      (*this).Evaluate(f, x); ok = f.IsFinite();
     234      df = Abs(f-f0);
     235    }
     236    S[i]=Abs(h);
     237    x[i] = x0;
     238  }
     239*/
     240}
     241
     242//--------------------------------------------------------------------
     243void BRnRFunction::Gradient (const BArray<BDat>& x, const BArray<BDat>& scale, BArray<BDat>& G)
     244
     245/*! Returns the numerical Gradient vector of this function in a point
     246 */
     247//--------------------------------------------------------------------
     248{
    91249  G.ReallocBuffer(n_);
    92   BDat f1, f_1, f2, f_2;
    93   BDat h = Distance();
     250  BDat f0, f1, f_1, f2, f_2;
     251  BBool ok0,ok1,ok_1,ok2,ok_2;
     252  BDat h1 = Distance();
    94253  int i;
     254  bool scaled = scale.Size()==x.Size();
    95255  for(i=0; i<n_; i++)
    96256  {
     257    BDat h = h1*((scaled)?scale[i]:1);
    97258    x[i]+=h;
    98     (*this).Evaluate(f1, x);
     259    (*this).Evaluate(f1, x); ok1 = f1.IsFinite();
    99260    x[i]+=h;
    100     (*this).Evaluate(f2, x);
     261    (*this).Evaluate(f2, x); ok2 = f2.IsFinite();
    101262    x[i]-=3*h;
    102     (*this).Evaluate(f_1, x);
     263    (*this).Evaluate(f_1, x); ok_1 = f_1.IsFinite();
    103264    x[i]-=h;
    104     (*this).Evaluate(f_2, x);
    105     G[i]=(f_2-8*f_1+8*f1-f2)/(12*h);
     265    (*this).Evaluate(f_2, x); ok_2 = f_2.IsFinite();
    106266    x[i]+=2*h;
    107   }
    108 }
    109 
     267    if(ok_2&&ok_1&&ok1&&ok2)
     268    {
     269      G[i]=(f_2-8*f_1+8*f1-f2)/(h*12);
     270    }
     271    else
     272    {
     273      (*this).Evaluate(f0, x); ok0 = f0.IsFinite();
     274      if(ok_2&&ok_1&&ok0&&ok1)
     275      {
     276        G[i]=(-2*f_2+9*f_1-18*f0+11*f1)/(h*6);
     277      }
     278      else if(ok_1&&ok0&&ok1&&ok2)
     279      {
     280        G[i]=(-2*f_1-3*f0+6*f1-f2)/(h*6);
     281      }
     282      else if(ok_2&&ok_1&&ok0)
     283      {
     284        G[i]=(f_2-4*f_1+3*f0)/(h*2);
     285      }
     286      else if(ok0&&ok1&&ok2)
     287      {
     288        G[i]=(-3*f0+4*f1-f2)/(h*2);
     289      }
     290      else
     291      {
     292        G[i]=BDat::Unknown();
     293      }
     294    }
     295  }
     296}
     297
     298
     299//--------------------------------------------------------------------
     300void BRnRFunction::Gradient2 (const BArray<BDat>& x, const BArray<BDat>& scale, BArray<BDat>& G2)
     301
     302/*! Returns the numerical second Gradient vector of this function in a point
     303 */
     304//--------------------------------------------------------------------
     305{
     306  G2.ReallocBuffer(n_);
     307  BDat f0, f1, f_1, f2, f_2;
     308  BBool ok0,ok1,ok_1,ok2,ok_2;
     309  BDat h1 = Distance();
     310  int i;
     311  bool scaled = scale.Size()==x.Size();
     312  for(i=0; i<n_; i++)
     313  {
     314    BDat h = h1*((scaled)?scale[i]:1);
     315    BDat h2 = h*h;
     316    BDat h2_12 = 12*h2;
     317    BDat x0 = x[i];
     318    (*this).Evaluate(f0, x); ok0 = f0.IsFinite();
     319    x[i]=x0+h;
     320    (*this).Evaluate(f1, x); ok1 = f1.IsFinite();
     321    x[i]=x0+2*h;
     322    (*this).Evaluate(f2, x); ok2 = f2.IsFinite();
     323    x[i]=x0-h;
     324    (*this).Evaluate(f_1, x); ok_1 = f_1.IsFinite();
     325    x[i]=x0-2*h;
     326    (*this).Evaluate(f_2, x); ok_2 = f_2.IsFinite();
     327    x[i]=x0;
     328    if(ok_2&&ok_1&&ok0&&ok1&&ok2)
     329    {
     330      G2[i]=(-f_2+16*f_1-30*f0+16*f1-f2)/h2_12;
     331    }
     332    else if(ok_1&&ok0&&ok1)
     333    {
     334      G2[i]=(f_1-2*f0+f1)/h2;
     335    }
     336    else if(ok0&&ok1&&ok2)
     337    {
     338      G2[i]=(f0-2*f1+f2)/h2;
     339    }
     340    else if(ok_2&&ok_1&&ok0)
     341    {
     342      G2[i]=(f_2-2*f_1+f0)/h2;
     343    }
     344    else
     345    {
     346      G2[i]=BDat::Unknown();
     347    }
     348  }
     349}
     350
     351//--------------------------------------------------------------------
     352void BRnRFunction::Gradient (const BArray<BDat>& x, BArray<BDat>& G)
     353//--------------------------------------------------------------------
     354{
     355  BArray<BDat> scale;
     356  Gradient(x,scale,G);
     357}
     358
     359//--------------------------------------------------------------------
     360void BRnRFunction::Gradient2 (const BArray<BDat>& x, BArray<BDat>& G2)
     361//--------------------------------------------------------------------
     362{
     363  BArray<BDat> scale;
     364  Gradient2(x,scale,G2);
     365}
    110366
    111367//--------------------------------------------------------------------
    … …  
    12531509
    12541510
    1255 //--------------------------------------------------------------------
    1256 class BProyectionFunction : public BRRFunction
    1257 
    1258 /*! Evaluates the real to real function
    1259  *
    1260  *    f(x) = F(P + x H)
    1261  *
    1262  * where F is a real vector to real function and P is the initial point
    1263  * and H is the direction vector.
    1264  */
    1265 //--------------------------------------------------------------------
    1266 {
    1267  public:
    1268     BRnRFunction& F_;
    1269     BArray<BDat>        P_;
    1270     BArray<BDat>        H_;
    1271    
    1272     BProyectionFunction(      BRnRFunction& F,
    1273                         const BArray<BDat>& P,
    1274                         const BArray<BDat>& H)
    1275         : F_(F), P_(P), H_(H)
    1276         {
    1277         }
    1278    
    1279     ~BProyectionFunction();
    1280    
    1281     void Evaluate(BDat& f, const BDat& x)
    1282         {
    1283             BArray<BDat> Q = P_;
    1284             for(int i=0; i<Q.Size(); i++) { Q(i) += x * H_(i); }
    1285             f = F_[Q];
    1286         }
    1287 };
    1288 
    1289 
    12901511
    12911512//--------------------------------------------------------------------
  • tol/bmath/bfunct/tol_brealfun.h

    r09c91c1 r7387675  
    9696
    9797  // Operations:
    98   virtual void Gradient (const BArray<BDat>&  x, BArray<BDat>&  G);
    99   virtual void Hessian(const BArray<BDat>& x, BMatrix<BDat>& H);
     98  bool AutoScale (
     99    const BArray<BDat>& x,
     100    const BArray<BDat>& xMin,
     101    const BArray<BDat>& xMax,
     102    BDat fDist,
     103    BDat tolerance,
     104    BArray<BDat>& S);
     105
     106  virtual void Gradient (const BArray<BDat>& x, const BArray<BDat>& scale, BArray<BDat>&  G);
     107  virtual void Gradient2(const BArray<BDat>& x, const BArray<BDat>& scale, BArray<BDat>&  G2);
     108  virtual void Gradient (const BArray<BDat>& x, BArray<BDat>&  G);
     109  virtual void Gradient2(const BArray<BDat>& x, BArray<BDat>&  G2);
     110  virtual void Hessian  (const BArray<BDat>& x, BMatrix<BDat>& H);
    100111  virtual BDat Dimension();
    101112};
Note: See TracChangeset for help on using the changeset viewer.