예제 #1
0
 void getBounds(const Box<double>& box, double* lb, double* ub) {
     int n = mPolyObj->getDim();
     double* a = box.mA;
     double* b = box.mB;
     double emin = getMaxValue<double> ();
     double emax = getMinValue<double> ();
     fillHessBounds(a, b);
     for (int i = 0; i < n; i++) {
         double gl, gu, r = 0;
         gl = mHessBounds[i][2 * i];
         gu = mHessBounds[i][2 * i + 1];
         for (int j = 0; j < n; j++) {
             if (i != j) {
                 double l, u;
                 l = mHessBounds[i][2 * j];
                 u = mHessBounds[i][2 * j + 1];
                 double m = BNBMAX(BNBABS(l), BNBABS(u));
                 r += m;
             }
         }
         gl -= r;
         gu += r;
         emin = BNBMIN(emin, gl);
         emax = BNBMAX(emax, gu);
     }
     if (lb != NULL)
         *lb = emin;
     if (ub != NULL)
         *ub = emax;
 }
예제 #2
0
    /**
     * The search method
     * @param x on exit the feasible value
     * @return the value found
     */
    FT search(FT* x) {
        long long int nstep = 0;
        int n = mProb.mBox.mDim;
        SmartArrayPtr<FT> y(n);
        SmartArrayPtr<FT> z(n);
        VecUtils::vecCopy(n, x, (FT*) y);
        FT robjv = mProb.mObj->func((FT*) y);
        FT rconsv = mProb.mCons[0]->mObjective->func((FT*) y);

        for (int i = 0; i < mMaxLocalHops; i++) {
            mPert.perturb(y, z);
            FT objv = mProb.mObj->func((FT*) z);
            FT consv = mProb.mCons[0]->mObjective->func((FT*) z);
            if ((BNBABS(consv) <= mDelta) && (BNBABS(rconsv) <= mDelta)) {
                if (objv < robjv) {
                    robjv = objv;
                    rconsv = consv;
                    VecUtils::vecCopy(n, (FT*) z, (FT*) y);
                    i = 0;
                }
            } else if (BNBABS(consv) < BNBABS(rconsv)) {
                robjv = objv;
                rconsv = consv;
                VecUtils::vecCopy(n, (FT*) z, (FT*) y);
                i = 0;
            }
        }
        
        VecUtils::vecCopy(n, (FT*) y, (FT*) x);
        return robjv;
    }
예제 #3
0
 double getGIJ(int i, int j, double* a, double* b)
 {
   double rv, xa, xb, nu, kap;
   if(j == i) {
     double A, B, C;
     xa = a[i];
     xb = b[i];
     nu = mRNABase->getDiag()[i];
     kap = mRNABase->getKap()[i];        
     A = BNBABS(nu  - 3 * kap * xa * xa);
     B = BNBABS(nu  - 3 * kap * xb * xb);
     C = BNBMAX(A, B);
     if((xa < 0) && (xb > 0)) 
       rv = BNBMAX(BNBABS(nu),C);
     else 
       rv = C;
   } else if(j == (i - 1)) {
     rv = BNBABS(mRNABase->getSDiag()[i - 1]);
   } else if(j == (i + 1)) {
     rv = BNBABS(mRNABase->getSDiag()[i]);
   } else {
     rv = 0.;
   }
   rv *= 2.; 
   return rv;
 }
예제 #4
0
 /**
  * Cuts a (L2) ball with center c and radius r from rectangle
  * @param s split dimension
  * @param box box to split
  * @param c ball center
  * @param rq ball (square) radius
  * @param seg interval to extract accross split dimension
  */
 template <class FT> static bool cutL2(int s, const Box<FT> &box, const FT* c, FT rq, Segment<FT>& seg) {
     FT dq = 0.;
     int n = box.mDim;
     FT* a = box.mA;
     FT* b = box.mB;
     for (int i = 0; i < n; i++) {
         if (i != s) {
             FT di = BNBMAX(BNBABS(c[i] - a[i]), BNBABS(c[i] - b[i]));
             dq += di * di;
         }
     }
     if (dq >= rq)
         return false;
     else {
         double delt = sqrt(rq - dq);
         seg.mA = c[s] - delt;
         seg.mB = c[s] + delt;
         if ((seg.mA >= b[s]) || (seg.mB <= a[s]))
             return false;
         else {
             //printf("{%lf, %lf, %e} %s", seg.mB, a[s], seg.mB - a[s], (seg.mB > a[s]) ? "true" : "false");
             return true;
         }
     }
 }
예제 #5
0
int main(int argc, char** argv) {
    Box<double> box(2);
    initBox(box);
    Supp supp(1);
    Obj obj;
    UnconsRecStore<double> rs(3, 2);
    EigenCutFactory<double> fact(&rs, &supp, &obj, 0.68);
    std::vector< Cut<double> > cuts;
    fact.getCuts(box, cuts);

    supp.setLB(-1);
    ObjOne objone;
    EigenCutFactory<double> factone(&rs, &supp, &objone, 0.28);
    factone.getCuts(box, cuts);

    supp.setLB(0);
    ObjTwo objtwo;
    EigenCutFactory<double> facttwo(&rs, &supp, &objtwo, 0.5);
    facttwo.getCuts(box, cuts);
    for (auto o : cuts) {
        std::cout << CutUtils<double> ::toString(o) << "\n";
    }

    BNB_ASSERT((cuts[0].mType == Cut<double>::CutType::OUTER_BALL) && (BNBABS(cuts[0].mR - 0.8) < 0.001) && (cuts[0].mC[0] == 0) && (cuts[0].mC[1] == 0));
    BNB_ASSERT((cuts[1].mType == Cut<double>::CutType::INNER_BALL) && (BNBABS(cuts[1].mR - 1.6) < 0.001) && (cuts[1].mC[0] == 2) && (cuts[1].mC[1] == 0));
    BNB_ASSERT((cuts[2].mType == Cut<double>::CutType::LINEAR) &&(cuts[2].mR == 2) && (cuts[2].mC[0] == 1) && (cuts[2].mC[1] == 1));
    return 0;
}
예제 #6
0
 template<class T> static T minAbs(int n, const T *x, int *pos) {
     T mv = BNBABS(x[0]);
     int p = 0;
     for (int i = 0; i < n; i++) {
         if (BNBABS(x[i]) < mv) {
             *pos = i;
             mv = BNBABS(x[i]);
         }
     }
     if (pos)
         *pos = p;
     return mv;
 }
예제 #7
0
 /**
  * Solves qubic equation ax^3 + bx^2 + cx + d = 0
  * @param roots roots found
  * @return number of roots (should be 1 or 3)
  */
 int solve(FT* roots)
 {
   double q = (mA < 0) ? -1 : 1; 
   mA *= q;
   mB *= q;
   mC *= q;
   mD *= q;
   FT D = mB * mB - 3. * mA * mC;
   FT T = BNBMAX((BNBABS(mB) + BNBABS(mC) + BNBABS(mD)) / BNBABS(mA), 1.);
   int nr;
   if(D <= 0.) {
       nr = 1;
       findRoot(-T, T, roots);
   } else if(D > 0.) {
     FT Q = sqrt(D);
     FT x1 = (-mB - Q) / (3. * mA);
     FT x2 = (-mB + Q) / (3. * mA);
     FT f1 = f(x1);
     FT f2 = f(x2);
     if(f1 < 0.) {
       nr = 1;
       findRoot(x2, T, roots);
     } else if(f1 == 0.) {
       roots[0] = x1;
       if(f2 == 0.) {
         nr = 1;
       } else {
         nr = 2;
         findRoot(x2, T, roots + 1);
       }
     } else if(f1 > 0.) {
       findRoot(-T, x1, roots);
       if(f2 > 0.) {
         nr = 1;
       } else if(f2 == 0.) {
         nr = 2;
         roots[1] = x2;
       } else if(f2 < 0.) {
         nr = 3;
         findRoot(x1, x2, roots + 1);
         findRoot(x2, T, roots + 2);
       }
     }
   }
   mA *= q;
   mB *= q;
   mC *= q;
   mD *= q;
   return nr;
 }
예제 #8
0
 bool check(FT* x) {
   bool rv = true;
   if(!mEq.empty()) {
     int sz = mEq.size();
     for(int i = 0; i < sz; i ++) {
       FT v =  mEq[i].mPF->func(x);
       if(BNBABS(v) > mEq[i].mEps) {
         rv = false;
         break;
       }
     }
     
   }
   
   if(!mIneq.empty()) {
     int sz = mIneq.size();
     
     for(int i = 0; i < sz; i ++) {
       FT v =  mIneq[i].mPF->func(x);
       if(v > mIneq[i].mEps){
         rv = false;
         break;
       }
     }        
   }
   return rv;
 }
예제 #9
0
 template<class T> static T vecDistAbs(int n, const T* x, const T* y) {
     T v = 0.;
     for (int i = 0; i < n; i++) {
         T u = BNBABS(y[i] - x[i]);
         v = BNBMAX(u, v);
     }
     return v;
 }
예제 #10
0
     FT honestGradLipzicConstant(const FT* a, const FT* b) 
     {
       FT nor;
       FT lnor;
       FT unor;
       int n;
       n = mN * mD;
       nor = 0.;
       lnor = 0.;
       unor = 0.;
       for(int i = 0; i < n; i ++) {
         fillHessRowBounds(a, b, i);
         FT nori = 0.;
	 FT lnori = mHessRowLB[i];
	 FT unori = mHessRowUB[i];
         for(int j = 0; j < n; j ++)  {
           nori += BNBMAX(BNBABS(mHessRowLB[j]), BNBABS(mHessRowUB[j]));
	   if(i != j) {
	     lnori -= BNBMAX(BNBABS(mHessRowLB[j]), BNBABS(mHessRowUB[j]));
	     unori += BNBMAX(BNBABS(mHessRowLB[j]), BNBABS(mHessRowUB[j]));
	   }
         }
         if(nori > nor) 
           nor = nori;
	 if(lnori < lnor)
	   lnor = lnori;
	 if(unori > unor)
	   unor = unori;
       }
       //printf("nor = %lf, lnor = %lf, unor = %lf\n", nor, lnor, unor);
       //return nor;
       return BNBABS(lnor);
     }
예제 #11
0
int testMethod(LocalBoxOptimizer<double>& opt) {
    Sqsum obj;
    opt.setObjective(&obj);
    Box<double> box(2);
    opt.setBox(box);
    box.mA[0] = -1;
    box.mA[1] = -3;
    box.mB[0] = 1;
    box.mB[1] = -1;
    double x[2] = {.25, -1.25};
    double v;
    double eps = 0.001;
    opt.search(x, &v);
    if (BNBMAX(BNBABS(x[0] - 0), BNBABS(x[1] - (-1))) < eps)
        return 0;
    else
        return 1;
}
예제 #12
0
 void getBounds(const Box<double>& box, double* lb, double* ub) {
     int n;
     n = box.mDim;
     double* a = box.mA;
     double* b = box.mB;
     double emax = getMinValue<double>();
     double emin = getMaxValue<double>();
     for (int i = 0; i < n; i++) {
         double lemax;
         double lemin;
         double dlo = 0;
         double dup = 0;
         if (i != (n - 1)) {
             BNBInterval<double>::sqr(a[i], b[i], &dlo, &dup);
             BNBInterval<double>::diff(1200 * dlo, 1200 * dup, 400 * a[i + 1], 400 * a[i + 1], &dlo, &dup);
             dlo += 2;
             dup += 2;
         }
         if (i != 0) {
             dlo += 200;
             dup += 200;
         }
         lemin = dlo;
         lemax = dup;
         if (i != (n - 1)) {
             double xabs = BNBMAX(BNBABS(a[i]), BNBABS(b[i]));
             lemin -= 400 * xabs;
             lemax += 400 * xabs;
         }
         if (i != 0) {
             double xabs = BNBMAX(BNBABS(a[i - 1]), BNBABS(b[i - 1]));
             lemax += 400 * xabs;
             lemin -= 400 * xabs;
         }
         emin = BNBMIN(emin, lemin);
         emax = BNBMAX(emax, lemax);
     }
     if (lb)
         *lb = emin;
     if (ub)
         *ub = emax;
 }
예제 #13
0
 double getGradCompLipzConstant(double* a, double* b, int i)
 {
   double vmin, vmax, v, u;
   double maxax, minax;
   double maxsin, minsin;
   double maxcos, mincos;
   BNBInterval<double>::sqr(a[i], b[i], &minax, &maxax);
   maxax *= mA;
   minax *= mA;
   vmin = vmax = 2;
   BNBInterval<double>::sin(minax, maxax, &minsin, &maxsin);
   vmin += 2 * mA * minsin;
   vmax += 2 * mA * maxsin;
   BNBInterval<double>::cos(minax, maxax, &mincos, &maxcos);
   BNBInterval<double>::mult(minax, maxax, mincos, maxcos, &u, &v);
   vmin += 4 * mA * u;
   vmax += 4 * mA * v;
   v = BNBMAX(BNBABS(vmin), BNBABS(vmax));
   return v;
 }
예제 #14
0
    double getLpzConst(const Box<double>& box) {
        int n;
        double norm;
        double *a = box.mA;
        double *b = box.mB;
        norm = 0.;
        n = box.mDim;
        for (int i = 0; i < n; i++) {
            double v;
            double xm;

            v = 0.;
            xm = BNBMAX(BNBABS(a[i]), BNBABS(b[i]));
            if (i != 0) {
                double xmp;
                xmp = BNBMAX(BNBABS(a[i - 1]), BNBABS(b[i - 1]));
                v += 200. * (xm + xmp * xmp);
            }
            if (i != (n - 1)) {
                double xmn;
                xmn = BNBMAX(BNBABS(a[i + 1]), BNBABS(b[i + 1]));
                v += 400. * xm * xm * xm + 400. * xm * xmn + 2. * xm + 2.;
            }
            norm += BNBSQR(v);
        }
        return sqrt(norm);
        //return 1;
    }
예제 #15
0
    void getEigenBounds(const FT* a, const FT* b, FT& emin, FT& emax)
    {
      int n;
      n = mN * mD;
      emin = 0.;
      emax = 0.;
      for(int i = 0; i < n; i ++) {
	fillHessRowBounds(a, b, i);
	FT lnori = mHessRowLB[i];
	FT unori = mHessRowUB[i];
	for(int j = 0; j < n; j ++)  {
	  if(i != j) {
	    lnori -= BNBMAX(BNBABS(mHessRowLB[j]), BNBABS(mHessRowUB[j]));
	    unori += BNBMAX(BNBABS(mHessRowLB[j]), BNBABS(mHessRowUB[j]));
	  }
	}
	if(lnori < emin)
	  emin = lnori;
	if(unori > emax)
	  emax = unori;
      }
    }
예제 #16
0
    bool stopnow(double xdiff, double fdiff, double* x, double* grad, double fval, int n) {
        //        std::cout << "n = " << n << "f = " << fval << ", gnorm = " << gnorm << ", xdiff = " << xdiff << ", fdiff = " << fdiff << "\n";
        double gnorm = 0;
        double const eps = 0.00000001;
        for (int i = 0; i < mBox.mDim; i++) {
            if (BNBABS(x[i] - mBox.mA[i]) <= eps) {
                if (grad[i] < 0)
                    gnorm += grad[i] * grad[i];
            } else if (BNBABS(x[i] - mBox.mB[i]) <= eps) {
                if (grad[i] > 0)
                    gnorm += grad[i] * grad[i];
            } else {
                gnorm += grad[i] * grad[i];
            }
        }
        std::cout << "n = " << n << "f = " << fval << ", gnorm = " << gnorm << ", xdiff = " << xdiff << ", fdiff = " << fdiff << "\n";

        if (gnorm < 0.01) {
            return true;
        } else if (n > 100000) {
            return true;
        } else
            return false;
    }
예제 #17
0
파일: f.hpp 프로젝트: yesyestian/BNB-solver
 double getFuncLipzConstant(double* a, double* b)
 {
   double v = 0.;
   double u;
   u =  BNBMAX(BNBABS(0.8356891 * a[4] + 37.293239), BNBABS(0.8356891 * b[4] + 37.293239));
   v += u * u;
   u =  BNBMAX(BNBABS(2. * 5.3578547 * a[2]), BNBABS(2. * 5.3578547 * b[2]));
   v += u * u;    
   u =  BNBMAX(BNBABS(0.8356891 * a[0]), BNBABS(0.8356891 * b[0]));
   v += u * u;    
   return sqrt(v);
 }
예제 #18
0
 void findRoot(double a, double b, double* root)
 {
   printf("[%lf, %lf]\n", a, b);
   int i = 0;
   for(;;) {
     double x = 0.5 * (a + b);
     double res = f(x);
     if(BNBABS(res) < mEps) {
       *root = x;          
       break;
     } else if(res * f(a) < 0){
       b = x;
     } else {
       a = x;
     }      
     i ++;
   }
   printf("i = %d, root = %lf\n", i, *root);
 }
예제 #19
0
 FT search(FT* x, FT* dir)
 {
  FT a, b, c, FA, fa, fb, fc, da, dc, db, df, delt, dnorm;
  int i;
  const int maxEnlarge = 1000;
  const double reduce = 0.0001;
  
  dnorm = VecUtils::vecNormTwo(mObj->getDim(), dir);
  for(int i = 0; i < mObj->getDim(); i ++)
    dir[i] /= dnorm;
  FA = fa = mObj->func(x);
  mObj->grad(x, mG);
  a = 0;
  b = mRightEnd;
  df = da = VecUtils::vecScalarMult(mObj->getDim(), mG, dir);
   i = 0;
  if(da > 0) {
    VecUtils::revert(mObj->getDim(), dir);
    da *= -1;
    df = da;       
  } 
  if(BNBABS(da) <= mEps) {
    return a;
  }     
  for(;;) {
    FT f;
    i ++;
    VecUtils::vecSaxpy(mObj->getDim(), x, dir, b, mX);
    fb = mObj->func(mX);
    if(fb > fa + mRho * da * (b - a))
      break;
    BNB_ASSERT(i < maxEnlarge);
    b = a + 2 * (b - a);
  }
  i = 0;
  for(;;) {
    i ++;
    c = a - 0.5 * (b - a) / ((fb - fa) / ((b - a) * df) - 1.);
    delt = reduce * (b - a);
    if((c < a + delt ) || (c > b - delt))
      c = a + .5 * (b - a);         
    VecUtils::vecSaxpy(mObj->getDim(), x, dir, c, mX);
    fc = mObj->func(mX);
    if(i > mMaxIters) 
      break;
    if(fc > FA + mRho * da * c) {
      // TMP!!!
      mObj->grad(mX, mG);
      double tdc = VecUtils::vecScalarMult(mObj->getDim(), mG, dir);
      b = c;
      fb = fc;
    } else {
      mObj->grad(mX, mG);
      dc = VecUtils::vecScalarMult(mObj->getDim(), mG, dir);
      if(dc > 0) {
        b = c;
        fb = fc;
      } else if(dc > mSigma * da)
        break;
      else {
        a = c;
        fa = fc;
        df = dc;
      }
    }
  }
  return c;
 }
예제 #20
0
main(int argc, char* argv[])
{
  typedef GOFactory < double > Fact;
  typedef WTraverse < Fact > Traverse;
  typedef BNBSeqSolver < Traverse > Solver;
  typedef std::priority_queue <Fact::Set>  TQ;
  
  int n;
  double eps = .001;
  bool rv = true;
  GOInitialData<double> id;
  n = 2;
  Sqsum obj(n);
  id.mObj = &obj;
  id.mA = (double*) malloc(n * sizeof(double));
  id.mB = (double*) malloc(n * sizeof(double));
  id.mA[0] = -10.;
  id.mA[1] = -10.;
  id.mB[0] = 50.;
  id.mB[1] = 10.;
  LipzGradDiscarder<double> dscg;
  Fact fact;
  SegVecDecomp<Fact> decomp;
  dscg.setEps(eps);
  dscg.setObjective(&obj);
  dscg.setInitialData(&id);
  dscg.getOptions() |= LipzGradDiscarder<double>::Options::CHECK_GRAD;
  dscg.getOptions() |= LipzGradDiscarder<double>::Options::CHECK_GRAD_COMP;
  dscg.getOptions() |= LipzGradDiscarder<double>::Options::DO_GRAD_BALL_CUT;
  dscg.getOptions() |= LipzGradDiscarder<double>::Options::DO_PRIMARY_BALL_CUT;
  dscg.getOptions() |= LipzGradDiscarder<double>::Options::DO_UNCONS_BALL_CUT;

  
  fact.setInitialData(&id);
  fact.addDiscarder(&dscg);
  fact.addDiscarder(&decomp);
  Solver solvertry(&fact);
  solvertry.setMST(1);
  solvertry.solve();
  TQ tq = solvertry.getCandidateProblemsList();
  if(tq.size() != 2) {
    BNB_ERROR_REPORT("wrong number of boxes (should be two)\n");
  }
  Fact::Set s = tq.top();
  double er = BNBABS(s.mA[0] - (-10.));
  er +=  BNBABS(s.mA[1] - (-10.0));
  er +=  BNBABS(s.mB[0] - 2.679550);
  er +=  BNBABS(s.mB[1] - 0.0);
  tq.pop();
  s = tq.top();
  er += BNBABS(s.mA[0] - (-10.));
  er +=  BNBABS(s.mA[1] - (.0));
  er +=  BNBABS(s.mB[0] - 2.679550);
  er +=  BNBABS(s.mB[1] - 10.0);
  tq.pop();
  if(er > 0.001) {
    BNB_ERROR_REPORT("wrong box\n");
  } 

  Solver solver(&fact);
 // solver.setDebugOptions(Solver::Options::PRINT_RESULTING_STAT);
  solver.solve();  
  Fact::Solution sol = solver.getSolutionContainer()->top();
//  printf("Sol val: %lf\n", sol.getValue());
  if(sol.getValue() > eps) {
    BNB_ERROR_REPORT("wrong solution value\n");
  }
  return 0;
}
예제 #21
0
 template<class T> static T vecNormOne(int n, const T* x) {
     T v = 0.;
     for (int i = 0; i < n; i++)
         v = BNBMAX(BNBABS(x[i]), v);
     return v;
 }
예제 #22
0
파일: f.hpp 프로젝트: yesyestian/BNB-solver
  void getEigenBounds(const double* a, const double* b, double& emin, double& emax)
  {
    double l;
    double u;
    double t;
    emin = emax = 0;
    l = 2 * mA3 * a[3] + 2 * mA4 * a[2];
    u = 2 * mA3 * b[3] + 2 * mA4 * b[2];
    t = BNBMAX(BNBABS(mA1 * a[3] + 2. * mA4 * a[0]),BNBABS(mA1 * b[3] + 2. * mA4 * b[0]));    
    l -= t;
    u += t;    
    t =  BNBMAX(BNBABS(mA1 * a[2] + 2. * mA3 * a[0]),BNBABS(mA1 * b[2] + 2. * mA3* b[0]));
    l -= t;
    u += t;
    emin = BNBMIN(l, emin);
    emax = BNBMAX(u, emax);
    
    l = 2 * mA2 * a[2];
    u = 2 * mA2 * b[2];
    emin = BNBMIN(l, emin);
    emax = BNBMAX(u, emax);

    l = 2. * mA2 * a[1];
    u = 2. * mA2 * b[1];
    t = BNBMAX(BNBABS(mA1 * a[3] + 2. * mA4 * a[0]),BNBABS(mA1 * b[3] + 2. * mA4 * b[0]));    
    l -= t;
    u += t;    
    t = BNBMAX(BNBABS(2. * mA2 * a[2]), BNBABS(2. * mA2 * b[2]));
    l -= t;
    u += t;    
    t = BNBMAX(BNBABS(mA1 * a[0]), BNBABS(mA1 * b[0]));
    l -= t;
    u += t;    
    emin = BNBMIN(l, emin);
    emax = BNBMAX(u, emax);
    
    l = u = 0;
    t =  BNBMAX(BNBABS(mA1 * a[2] + 2. * mA3 * a[0]),BNBABS(mA1 * b[2] + 2. * mA3 * b[0]));
    l -= t;
    u += t;    
    t = BNBMAX(BNBABS(mA1 * a[0]), BNBABS(mA1 * b[0]));
    l -= t;
    u += t;    
    emin = BNBMIN(l, emin);
    emax = BNBMAX(u, emax);
  }
예제 #23
0
	FT search(FT* x, FT* dir){
		#ifdef PLS_DEBUG
			std::cout << "Powell line search initiated: " << "x: ";
			for(int j = 0; j < dim; ++j){
				std:: cout << x[j] << " ";
			};
			std::cout << "; dir: ";
			for(int j = 0; j < dim; ++j){
				std:: cout << dir[j] << " ";
			};
			
			std::cout << std::endl;
		#endif

		FT denom;
		//~ VecUtils::vecCopy<FT>(dim, x,x1);
		//Step 1
		x1 = 0.0;
		k = 0;
		for(;;){
			
			//Step2
			x2 = x1 + mDx;
			//Step3
			VecUtils::vecSaxpy<FT>(dim, x, dir, x1, mX);
			f1 = mObj->func(mX);
			#ifdef PLS_DEBUG
				std::cout << "Mark 1" << std::endl;
			#endif
			VecUtils::vecSaxpy<FT>(dim, x, dir, x2, mX);
			f2 = mObj->func(mX);
			#ifdef PLS_DEBUG
				std::cout << "Mark 2" << std::endl;
			#endif
			//Step4
			if (f1>f2){
				x3 = x2 + mDx;
			} else {
				x3 = x1 - mDx;
			};
			//Step5
			VecUtils::vecSaxpy<FT>(dim, x, dir, x3, mX);
			f3 = mObj->func(mX);
			#ifdef PLS_DEBUG
				std::cout << "Mark 3: "<< f3 << std::endl;
			
			#endif

			
			for (;;){
				k++;
				if ( k > maxK ) return xmin;
				//Step6
				if (f1<f2) {
					if (f1<f3) {
						xmin = x1;
						fmin = f1;
						imin = 1;
					} else {
						xmin = x3;
						fmin = f3;
						imin = 3;
					} 
				} else {
					if (f2<f3) {
						xmin = x2;
						fmin = f2;
						imin = 2;
					} else {
						xmin = x3;
						fmin = f3;
						imin = 3;
					} 
				}
				#ifdef PLS_DEBUG
					std::cout << "Mark 4" << std::endl;
				#endif

				//Step7
				denom = 2.0 * ((x2 - x3)*f1 + (x3 - x1)*f2 + (x1 - x2)*f3);
				#ifdef PLS_DEBUG
					std::cout << "Mark 5" << std::endl;
				#endif

				if (denom > M_EPS) {
				//~ if (denom >= 0.0) {
					#ifdef PLS_DEBUG
						std::cout << "Mark 6" << std::endl;
					#endif
					
					if (imin!=3) {
						return xmin;
					} else {
						x1 = x3;
						break; //Goto step2
					}
				#ifdef PLS_DEBUG
					std::cout << "Mark 7" << std::endl;
				#endif
				} else {
					xx = ((x2*x2 - x3*x3)*f1 + (x3*x3 - x1*x1)*f2 + (x1*x1 - x2*x2)*f3) / denom;
					VecUtils::vecSaxpy<FT>(dim, x, dir, xx, mX);
					#ifdef PLS_DEBUG
						std::cout << "Mark 8" << std::endl;
						for(int j = 0; j < dim; ++j){
							std::cout << mX[j] << " ";
						};
						std::cout << std::endl;
					#endif

					ff = mObj->func(mX);
					
					#ifdef PLS_DEBUG
						std::cout << "-------------------------------------------------------------------------------------" << std::endl;;
						std::cout << "pls: " << "x1: "<< x1 << "; f1: " << f1 << "; X1: ";
						#ifdef PLS_FULL_DEBUG
						for(int j = 0; j < dim; ++j){
							std:: cout << mX[j] << " ";
						};
						#endif
						std::cout << std::endl;

					
						std::cout << "pls: " << "x2: "<< x2 << "; f2: " << f2 << "; X2: ";
						#ifdef PLS_FULL_DEBUG
						for(int j = 0; j < dim; ++j){
							std:: cout << mX[j] << " ";
						};
						#endif
						std::cout << std::endl;
					
					
						std::cout << "pls: " << "x3: "<< x3 << "; f3: " << f3 << "; X3: ";
						#ifdef PLS_FULL_DEBUG
						for(int j = 0; j < dim; ++j){
							std:: cout << mX[j] << " ";
						};
						#endif
						std::cout << std::endl;

						std::cout << "pls: " << "xx: "<< xx << "; ff: " << ff << "; XX: ";
						#ifdef PLS_FULL_DEBUG
						for(int j = 0; j < dim; ++j){
							std:: cout << mX[j] << " ";
						};
						#endif
						std::cout << std::endl;
						std::cout << "pls: " << "xmin: "<< xmin << "; fmin: " << fmin << std::endl;					
					#endif


					//Step8
					
					
					if ((((BNBABS(ff) > EPS) &&(BNBABS((fmin - ff)/ff) < mEps1)) || (BNBABS(fmin - ff) < mEps1)) && (((BNBABS(xx)>EPS) && (BNBABS((xmin - xx)/xx) < mEps2)) || (BNBABS(xmin - xx) < mEps2)) ){
						//Step 8a
						if (ff < fmin) {
							return xx;
						} else {
							return xmin;
						};
						
					} else {
						//Step 8b
						if (x3>x1) {
							if ((x1<xx) and (xx < x2)) {
								if (ff < fmin) {
									x3 = x2;
									f3 = f2;
									x2 = xx;
									f2 = ff;
								} else {
									x1 = xx;
									f1 = ff;
								}
							} else 
							if ((x2<xx) and (xx < x3)){
								if ((ff>fmin) and (imin == 2)) {
									x3 = xx;
									f3 = ff;
								} else {
									x1 = x2;
									f1 = f2;
									x2 = xx;
									f2 = ff;
								}
							} else {
								//Step  8c
								if (ff < fmin) {
									x1 = xx;
									break; //goto 2
								} else {
									x1 = xmin;
									break; //goto 2
								}
							}
						} else {
							if ((x3 < xx) and(xx < x1)) {
								if ((ff>fmin) and (imin == 1)) {
									x3 = x2;
									f3 = f2;
									x2 = x1;
									f2 = f1;
									x1 = xx;
									f1 = ff;
								} else {
									x2 = x1;
									f2 = f1;
									x1 = xx;
									f1 = ff;
								}
							} else 
							if ((x1<xx) and (xx < x2)){
								if (ff < fmin){
									x3 = x2;
									f3 = f2;
									x2 = xx;
									f2 = ff;
								} else {
									x2 = xx;
									f2 = ff;
								}
							} else {
								//Step  8c
								if (ff < fmin) {
									x1 = xx;
									break; //goto 2
								} else {
									x1 = xmin;
									break; //goto 2
								}
							}
							
						}
						
					}
				}
			}
		}
	}
예제 #24
0
    /**
     * Calculate Hessian matrix
     * @param x point
     * @param H hessian 
     * @return dimension (may be less then n with possible constraints)
     */
    int hessn(const double* X, double* H)
    {
      class Sparse {
        public :
          typedef std::map<unsigned int, double> Vec;
          static double mult(Vec& u, Vec& v)
          {
            double s = 0.;
            Vec::const_iterator it = u.begin();
            for(;it != u.end(); it ++) {
              const unsigned int i = it->first;
              if(v.find(i) != v.end()) {
                double t = it->second;
                s += t * v[i];
              }
            }
            return s;  
          }
          
          static void print(const Vec& u)
          {
            Vec::const_iterator it = u.begin();
            for(;it != u.end(); it ++) {
              printf("v[%d] = %lf\n", it->first, it->second);
            }
          }
      };
      int n = BASE::getDim();
      int m = n - 1;
      int k = 0;
      double t = BNBABS(X[k]);
      double *diag, *sdiag, *kap;
      BNB_ASSERT(mRNABase);
      kap = mRNABase->getKap();
      diag = mRNABase->getDiag();
      sdiag = mRNABase->getSDiag();
      Sparse::Vec wk;
      projectPoint(X, mAux);
      double* x = mAux;
      for(int i = 0; i < n; i ++) {
        if(BNBABS(x[i]) > t) {
          k = i;
          t = BNBABS(x[k]);
        }                    
      }
      t =  diag[k] * x[k] - kap[k] * x[k] * x[k] * x[k];
      if(k != (n - 1)) {
        t += sdiag[k] * x[k + 1];
      }
      if(k != 0) {
        t += sdiag[k - 1] * x[k - 1];
      }
      double lam = t / x[k];

      wk[k] = 2. * diag[k] - 6. * kap[k] * x[k] * x[k] - 2. * lam;
      if(k != 0)
        wk[k - 1] = 2. * sdiag[k - 1];
      if(k != (n - 1))
        wk[k + 1] = 2. * sdiag[k];
      for(int i = 0; i < m; i ++) {
        for(int j = 0; j < m; j ++) {
          double s = 0.;
          Sparse::Vec u, v, wI, z;
          int I, J;
          I = (i < k) ? i : i + 1;
          J = (j < k) ? j : j + 1;
          u[I] = 1.;
          u[k] = -x[I] / x[k];            
          v[J] = 1.;
          v[k] = -x[J] / x[k];    
          wI[I] = 2. * diag[I] - 6. * kap[I] * x[I] * x[I] - 2. * lam;
          if(I != 0)
            wI[I - 1] = 2. * sdiag[I - 1];
          if(I != (n - 1))
            wI[I + 1] = 2. * sdiag[I];
          z[k] = Sparse::mult(wk, v);
          z[I] = Sparse::mult(wI, v);
          s = Sparse::mult(u, z);
          H[i * m + j] = s;
        }
      }
      return m;
    }
예제 #25
0
 double getHessLipzConstant(double* a, double* b)
 {
   double l, u;
   BNBInterval<double>::cos(a[0], b[0], &l, &u);
   return BNBMAX(BNBABS(l), BNBABS(u));      
 }
예제 #26
0
 double getHessLipzConstant(double* a, double* b)
 {
     double v, A = a[0], B = b[0], x, y;
     BNBInterval<double>::polynom(mN - 3, mDDDF, A, B, &x, &y);
     return BNBMAX(BNBABS(x), BNBABS(y));
 }
예제 #27
0
 /**
   * Perform search
   * @param x start point and result
   * @param v pointer to the resulting value
   * @return true if search converged and false otherwise
   */
  bool search(FT* x, FT* v)
  {
    int iters, n, attempts;
    iters = 0;
    attempts = 0;
    n = LocalOptimizer<FT>::mObj->getDim();
    do {
      FT u, uold, norm;
      u = LocalOptimizer<FT>::mObj->func(x);
      uold = u;
      LocalOptimizer<FT>::mObj->grad(x, mG);
      norm = VecUtils::vecNormOne(n, mG);
      mDrops = (10. / norm) + 1;        
      mMu = BNBMAX(0.01, .01 / norm);
      for(int i = 0; i < mDrops; i ++) {
        FT z;
        generate(x);
        z = LocalOptimizer<FT>::mObj->func(mX);
        if(z < u) {
          u = z;
          VecUtils::vecCopy(n, mX, mY);
        }
      }
      if(u < uold) {
        printf("Iters = %d, drops = %d, norm = %lf, mu = %lf, attempts = %d: improved from %lf to %lf\n", iters, mDrops, norm, mMu, attempts, uold, u);
        VecUtils::vecSaxpy(n, mY, x, -1., mG);
        VecUtils::vecCopy(n, mY, x);
        
        attempts = 0;
      } else {          
        if(norm < mEps) {
          printf("Iters = %d, attempts = %d, norm = %lf, val = %lf \n", iters, attempts, norm, u);
          *v = u;
          return true;
        } else if(attempts >= mMaxAttempts) {
          FT a, step, s,  sold;
          a = 0;
          step = .0001;
          s = sold = LocalOptimizer<FT>::mObj->func(x);
          do {
            FT gamma;
            sold = BNBMIN(s, sold);
            gamma = (a + step) / norm;
            VecUtils::vecSaxpy(n, x, mG, -gamma, mX);
            s = LocalOptimizer<FT>::mObj->func(mX);
            if(s < sold) {
              a += step;
            } else {
              step *= 0.5;
            }
          } while((BNBABS(sold - s) > 0.0001) || (s > sold));
          if(iters % 1000 == 0)
            printf("Grad=>Iters = %d, attempts = %d, norm = %lf, val = %lf, step = %lf \n", iters, attempts, norm, s, step);
          VecUtils::vecCopy(n, mX, x);
          attempts = 0;
          /*
          *v = s;
          return true;
          */
        } else
          attempts ++;
      }
    } while((mMaxIters == -1) || (iters ++ < mMaxIters));
    return false;
  }