コード例 #1
0
inline size_t RStarTreeDescentHeuristic::ChooseDescentNode(
    const TreeType* node,
    const TreeType* insertedNode)
{
  // Convenience typedef.
  typedef typename TreeType::ElemType ElemType;

  std::vector<ElemType> scores(node->NumChildren());
  std::vector<ElemType> vols(node->NumChildren());
  ElemType minScore = std::numeric_limits<ElemType>::max();
  size_t bestIndex = 0;
  bool tied = false;

  for (size_t i = 0; i < node->NumChildren(); i++)
  {
    ElemType v1 = 1.0;
    ElemType v2 = 1.0;
    for (size_t j = 0; j < node->Children()[i]->Bound().Dim(); j++)
    {
      v1 *= node->Children()[i]->Bound()[j].Width();
      v2 *= node->Children()[i]->Bound()[j].Contains(insertedNode->Bound()[j]) ? node->Children()[i]->Bound()[j].Width() :
              (insertedNode->Bound()[j].Contains(node->Children()[i]->Bound()[j]) ? insertedNode->Bound()[j].Width() : (insertedNode->Bound()[j].Lo() < node->Children()[i]->Bound()[j].Lo() ? (node->Children()[i]->Bound()[j].Hi() - insertedNode->Bound()[j].Lo()) : (insertedNode->Bound()[j].Hi() - node->Children()[i]->Bound()[j].Lo())));
    }

    assert(v2 - v1 >= 0);
    vols[i] = v1;
    scores[i] = v2 - v1;

    if (v2 - v1 < minScore)
    {
      minScore = v2 - v1;
      bestIndex = i;
    }
    else if (v2 - v1 == minScore)
    {
      tied = true;
    }
  }

  if (tied)
  {
    // We break ties by choosing the smallest bound.
    ElemType minVol = std::numeric_limits<ElemType>::max();
    bestIndex = 0;
    for (size_t i = 0; i < scores.size(); i++)
    {
      if (scores[i] == minScore)
      {
        if (vols[i] < minVol)
        {
          minVol = vols[i];
          bestIndex = i;
        }
      }
    }
  }

  return bestIndex;
}
コード例 #2
0
ファイル: qmtc.hpp プロジェクト: kevinushey/nt2
 void compute( const FUNC& f, const X & ranges, const o_t & o)
 {
     init(o);
     itab_t facts =  ranges(nt2::_,begin_+1, nt2::_)-ranges(nt2::_,begin_, nt2::_);
     itab_t vols = nt2::prod(facts);
     input_t total_vol = globalasum1(vols);
     itab_t coefs = real_t(maxfunccnt_)*nt2::abs(vols)*nt2::rec(total_vol);
     BOOST_ASSERT_MSG(size(ranges, 2) == 2, "ranges must be a nx2xm expression");
     size_t l = size(ranges, 3);
     res_ = nt2::Zero<value_t>();
     for(size_t i=1; i <= l; ++i)
     {
         nbpts_ = coefs(i);
         res_ += compute(f, ranges(nt2::_,nt2::_,i), vols(i), facts(i));
         fcnt_+= nbpts_;
     }
 }
コード例 #3
0
ファイル: sabrvolsurface.cpp プロジェクト: 21hub/QuantLib
    std::vector<Volatility> SabrVolSurface::volatilitySpreads(const Date& d) const {

        Size nOptionsTimes = optionTimes_.size();
        Size nAtmRateSpreads = atmRateSpreads_.size();
        std::vector<Volatility> interpolatedVols(nAtmRateSpreads);

        std::vector<Volatility> vols(nOptionsTimes); // the volspread at a given strike
        for (Size i=0; i<nAtmRateSpreads; ++i) {
            for (Size j=0; j<nOptionsTimes; ++j) {
                vols[j] = (**volSpreads_[j][i]).value();
            }
            LinearInterpolation interpolator(optionTimes_.begin(), optionTimes_.end(),
                                             vols.begin());
            interpolatedVols[i] = interpolator(timeFromReference(d),true);
        }
        return interpolatedVols;
    }
コード例 #4
0
ファイル: volume.hpp プロジェクト: goldnd/CGoGN
typename PFP::REAL totalVolume(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
{
    // allocate a vector of 1 accumulator for each thread
    std::vector<typename PFP::REAL> vols(CGoGN::Parallel::NumberOfThreads-1, 0.0);

    // foreach volume
    CGoGN::Parallel::foreach_cell<VOLUME>(map, [&] (Vol v, unsigned int thr)
    {
        // add volume to the thread accumulator
        vols[thr-1] += convexPolyhedronVolume<PFP>(map, v, position) ;
    });

    // compute the sum of volumes
    typename PFP::REAL total(0);
    for (int i=0; i< CGoGN::Parallel::NumberOfThreads-1; ++i )
        total += vols[i];

    return total;
}
コード例 #5
0
void testVolSort()
{
	vector< unsigned int > findVolOrder( const vector< double >& vols );
	vector< double > vols( 8 );
	vols[0] = 7;
	vols[1] = 8;
	vols[2] = 6;
	vols[3] = 5;
	vols[4] = 1;
	vols[5] = 2;
	vols[6] = 3;
	vols[7] = 4;
	vector< unsigned int > order = findVolOrder( vols );
	// The order is the rank of the volume entry, largest should be 0.
	assert( order[0] == 1 );
	assert( order[1] == 0 );
	assert( order[2] == 2 );
	assert( order[3] == 3 );
	assert( order[4] == 7 );
	assert( order[5] == 6 );
	assert( order[6] == 5 );
	assert( order[7] == 4 );

	// This is a sequence which failed in a model test, despite the
	// above test working.
	vols.resize(5);
	vols[0] = 1e-15;
	vols[1] = 3e-15;
	vols[2] = -1;
	vols[3] = 2e-15;
	vols[4] = 5e-15;
	order = findVolOrder( vols );
	assert( order[0] == 4 );
	assert( order[1] == 1 );
	assert( order[2] == 3 );
	assert( order[3] == 0 );
	assert( order[4] == 2 );
}
コード例 #6
0
inline size_t RStarTreeDescentHeuristic::ChooseDescentNode(
    const TreeType* node,
    const arma::vec& point)
{
  // Convenience typedef.
  typedef typename TreeType::ElemType ElemType;

  bool tiedOne = false;
  std::vector<ElemType> originalScores(node->NumChildren());
  ElemType origMinScore = std::numeric_limits<ElemType>::max();

  if (node->Children()[0]->IsLeaf())
  {
    // If its children are leaf nodes, use minimum overlap to choose.
    size_t bestIndex = 0;

    for (size_t i = 0; i < node->NumChildren(); i++)
    {
      ElemType sc = 0;
      for (size_t j = 0; j < node->NumChildren(); j++)
      {
        if (j != i)
        {
          ElemType overlap = 1.0;
          ElemType newOverlap = 1.0;
          for (size_t k = 0; k < node->Bound().Dim(); k++)
          {
            ElemType newHigh = std::max(point[k],
                node->Children()[i]->Bound()[k].Hi());
            ElemType newLow = std::min(point[k],
                node->Children()[i]->Bound()[k].Lo());
            overlap *= node->Children()[i]->Bound()[k].Hi() < node->Children()[j]->Bound()[k].Lo() || node->Children()[i]->Bound()[k].Lo() > node->Children()[j]->Bound()[k].Hi() ? 0 : std::min(node->Children()[i]->Bound()[k].Hi(), node->Children()[j]->Bound()[k].Hi()) - std::max(node->Children()[i]->Bound()[k].Lo(), node->Children()[j]->Bound()[k].Lo());
            newOverlap *= newHigh < node->Children()[j]->Bound()[k].Lo() || newLow > node->Children()[j]->Bound()[k].Hi() ? 0 : std::min(newHigh, node->Children()[j]->Bound()[k].Hi()) - std::max(newLow, node->Children()[j]->Bound()[k].Lo());
          }
          sc += newOverlap - overlap;
        }
      }

      originalScores[i] = sc;
      if (sc < origMinScore)
      {
        origMinScore = sc;
        bestIndex = i;
      }
      else if (sc == origMinScore)
      {
        tiedOne = true;
      }
    }

    if (!tiedOne)
      return bestIndex;
  }

  // We do this if it is not on the second level or if there was a tie.
  std::vector<ElemType> scores(node->NumChildren());
  if (tiedOne)
  {
    // If the first heuristic was tied, we need to eliminate garbage values.
    for (size_t i = 0; i < scores.size(); i++)
      scores[i] = std::numeric_limits<ElemType>::max();
  }

  std::vector<ElemType> vols(node->NumChildren());
  ElemType minScore = std::numeric_limits<ElemType>::max();
  size_t bestIndex = 0;
  bool tied = false;

  for (size_t i = 0; i < node->NumChildren(); i++)
  {
    if (!tiedOne || originalScores[i] == origMinScore)
    {
      ElemType v1 = 1.0;
      ElemType v2 = 1.0;
      for (size_t j = 0; j < node->Bound().Dim(); j++)
      {
        v1 *= node->Children()[i]->Bound()[j].Width();
        v2 *= node->Children()[i]->Bound()[j].Contains(point[j]) ? node->Children()[i]->Bound()[j].Width() : (node->Children()[i]->Bound()[j].Hi() < point[j] ? (point[j] - node->Children()[i]->Bound()[j].Lo()) :
            (node->Children()[i]->Bound()[j].Hi() - point[j]));
      }

      assert(v2 - v1 >= 0);
      vols[i] = v1;
      scores[i] = v2 - v1;

      if (v2 - v1 < minScore)
      {
        minScore = v2 - v1;
        bestIndex = i;
      }
      else if (v2 - v1 == minScore)
      {
        tied = true;
      }
    }
  }

  if (tied)
  {
    // We break ties by choosing the smallest bound.
    ElemType minVol = std::numeric_limits<ElemType>::max();
    bestIndex = 0;
    for (size_t i = 0; i < scores.size(); i++)
    {
      if (scores[i] == minScore)
      {
        if (vols[i] < minVol)
        {
          minVol = vols[i];
          bestIndex = i;
        }
      }
    }
  }

  return bestIndex;
}