예제 #1
0
void ZopfliBlockSplitLZ77(const ZopfliOptions* options,
                          const ZopfliLZ77Store* lz77, size_t maxblocks,
                          size_t** splitpoints, size_t* npoints) {
  size_t lstart, lend;
  size_t i;
  size_t llpos = 0;
  size_t numblocks = 1;
  unsigned char* done;
  double splitcost, origcost;

  if (lz77->size < 10) return;  /* This code fails on tiny files. */

  done = (unsigned char*)malloc(lz77->size);
  if (!done) exit(-1); /* Allocation failed. */
  for (i = 0; i < lz77->size; i++) done[i] = 0;

  lstart = 0;
  lend = lz77->size;
  for (;;) {
    SplitCostContext c;

    if (maxblocks > 0 && numblocks >= maxblocks) {
      break;
    }

    c.lz77 = lz77;
    c.start = lstart;
    c.end = lend;
    assert(lstart < lend);
    llpos = FindMinimum(SplitCost, &c, lstart + 1, lend, &splitcost);

    assert(llpos > lstart);
    assert(llpos < lend);

    origcost = EstimateCost(lz77, lstart, lend);

    if (splitcost > origcost || llpos == lstart + 1 || llpos == lend) {
      done[lstart] = 1;
    } else {
      AddSorted(llpos, splitpoints, npoints);
      numblocks++;
    }

    if (!FindLargestSplittableBlock(
        lz77->size, done, *splitpoints, *npoints, &lstart, &lend)) {
      break;  /* No further split will probably reduce compression. */
    }

    if (lend - lstart < 10) {
      break;
    }
  }

  if (options->verbose) {
    PrintBlockSplitPoints(lz77, *splitpoints, *npoints);
  }

  free(done);
}
예제 #2
0
gcc_pure
static NearestAirspace
FindHorizontal(const GeoPoint &location,
               const Airspaces &airspace_database,
               const AirspacePredicate &predicate)
{
  const auto &projection = airspace_database.GetProjection();
  return FindMinimum(airspace_database, location, 30000, predicate,
                     [&location, &projection](const AbstractAirspace &airspace){
                       return CalculateNearestAirspaceHorizontal(location, projection, airspace);
                     },
                     CompareNearestAirspace());
}
vector<float> cRotationalOffset::GetMinRadius( const Contour &contour )
{
    Contour::iterator i;

    mpOutput.clear();
    CartisanToPolar( contour );
    /* Sort in pairs ascending order.
    By default sort function sort on the basis of 1st element of pairs.
    Thats exactly we want !! Sort radius on the basis of theta.
    Therefore no need to provide external Compare function. ;)  */
    sort( mThetaRad.begin(), mThetaRad.end() );
    FindMinimum();
    mThetaRad.clear();

    return mpOutput;
}
예제 #4
0
const AbstractAirspace *
FindSoonestAirspace(const Airspaces &airspaces,
                    const AircraftState &state,
                    const AirspaceAircraftPerformance &perf,
                    const AirspacePredicate &predicate,
                    const double max_time)
{
  const auto &projection = airspaces.GetProjection();
  const auto range = perf.GetMaxSpeed() * max_time;
  return FindMinimum(airspaces, state.location, range, predicate,
                     [&state, &perf, max_time,
                      &projection](const AbstractAirspace &airspace){
                       return CalculateSoonestAirspace(state, perf, max_time,
                                                       projection, airspace);
                     },
                     CompareSoonestAirspace()).airspace;
}
예제 #5
0
static gmx_bool UpdateWeights(int nlim, t_expanded *expand, df_history_t *dfhist,
                              int fep_state, real *scaled_lamee, real *weighted_lamee, gmx_int64_t step)
{
    gmx_bool  bSufficientSamples;
    int       i;
    int       n0, np1, nm1, nval, min_nvalm, min_nvalp, maxc;
    real      omega_m1_0, omega_p1_m1, omega_m1_p1, omega_p1_0, clam_osum;
    real      de, de_function;
    real      cnval, zero_sum_weights;
    real     *omegam_array, *weightsm_array, *omegap_array, *weightsp_array, *varm_array, *varp_array, *dwp_array, *dwm_array;
    real      clam_varm, clam_varp, clam_weightsm, clam_weightsp, clam_minvar;
    real     *lam_variance, *lam_dg;
    double   *p_k;
    double    pks = 0;
    real      chi_m1_0, chi_p1_0, chi_m2_0, chi_p2_0, chi_p1_m1, chi_p2_m1, chi_m1_p1, chi_m2_p1;

    /* if we have equilibrated the weights, exit now */
    if (dfhist->bEquil)
    {
        return FALSE;
    }

    if (CheckIfDoneEquilibrating(nlim, expand, dfhist, step))
    {
        dfhist->bEquil = TRUE;
        /* zero out the visited states so we know how many equilibrated states we have
           from here on out.*/
        for (i = 0; i < nlim; i++)
        {
            dfhist->n_at_lam[i] = 0;
        }
        return TRUE;
    }

    /* If we reached this far, we have not equilibrated yet, keep on
       going resetting the weights */

    if (EWL(expand->elamstats))
    {
        if (expand->elamstats == elamstatsWL)  /* Standard Wang-Landau */
        {
            dfhist->sum_weights[fep_state] -= dfhist->wl_delta;
            dfhist->wl_histo[fep_state]    += 1.0;
        }
        else if (expand->elamstats == elamstatsWWL) /* Weighted Wang-Landau */
        {
            snew(p_k, nlim);

            /* first increment count */
            GenerateGibbsProbabilities(weighted_lamee, p_k, &pks, 0, nlim-1);
            for (i = 0; i < nlim; i++)
            {
                dfhist->wl_histo[i] += (real)p_k[i];
            }

            /* then increment weights (uses count) */
            pks = 0.0;
            GenerateWeightedGibbsProbabilities(weighted_lamee, p_k, &pks, nlim, dfhist->wl_histo, dfhist->wl_delta);

            for (i = 0; i < nlim; i++)
            {
                dfhist->sum_weights[i] -= dfhist->wl_delta*(real)p_k[i];
            }
            /* Alternate definition, using logarithms. Shouldn't make very much difference! */
            /*
               real di;
               for (i=0;i<nlim;i++)
               {
                di = (real)1.0 + dfhist->wl_delta*(real)p_k[i];
                dfhist->sum_weights[i] -= log(di);
               }
             */
            sfree(p_k);
        }

        zero_sum_weights =  dfhist->sum_weights[0];
        for (i = 0; i < nlim; i++)
        {
            dfhist->sum_weights[i] -= zero_sum_weights;
        }
    }

    if (expand->elamstats == elamstatsBARKER || expand->elamstats == elamstatsMETROPOLIS || expand->elamstats == elamstatsMINVAR)
    {

        de_function = 0;  /* to get rid of warnings, but this value will not be used because of the logic */
        maxc        = 2*expand->c_range+1;

        snew(lam_dg, nlim);
        snew(lam_variance, nlim);

        snew(omegap_array, maxc);
        snew(weightsp_array, maxc);
        snew(varp_array, maxc);
        snew(dwp_array, maxc);

        snew(omegam_array, maxc);
        snew(weightsm_array, maxc);
        snew(varm_array, maxc);
        snew(dwm_array, maxc);

        /* unpack the current lambdas -- we will only update 2 of these */

        for (i = 0; i < nlim-1; i++)
        {   /* only through the second to last */
            lam_dg[i]       = dfhist->sum_dg[i+1] - dfhist->sum_dg[i];
            lam_variance[i] = gmx::square(dfhist->sum_variance[i+1]) - gmx::square(dfhist->sum_variance[i]);
        }

        /* accumulate running averages */
        for (nval = 0; nval < maxc; nval++)
        {
            /* constants for later use */
            cnval = (real)(nval-expand->c_range);
            /* actually, should be able to rewrite it w/o exponential, for better numerical stability */
            if (fep_state > 0)
            {
                de = std::exp(cnval - (scaled_lamee[fep_state]-scaled_lamee[fep_state-1]));
                if (expand->elamstats == elamstatsBARKER || expand->elamstats == elamstatsMINVAR)
                {
                    de_function = 1.0/(1.0+de);
                }
                else if (expand->elamstats == elamstatsMETROPOLIS)
                {
                    if (de < 1.0)
                    {
                        de_function = 1.0;
                    }
                    else
                    {
                        de_function = 1.0/de;
                    }
                }
                dfhist->accum_m[fep_state][nval]  += de_function;
                dfhist->accum_m2[fep_state][nval] += de_function*de_function;
            }

            if (fep_state < nlim-1)
            {
                de = std::exp(-cnval + (scaled_lamee[fep_state+1]-scaled_lamee[fep_state]));
                if (expand->elamstats == elamstatsBARKER || expand->elamstats == elamstatsMINVAR)
                {
                    de_function = 1.0/(1.0+de);
                }
                else if (expand->elamstats == elamstatsMETROPOLIS)
                {
                    if (de < 1.0)
                    {
                        de_function = 1.0;
                    }
                    else
                    {
                        de_function = 1.0/de;
                    }
                }
                dfhist->accum_p[fep_state][nval]  += de_function;
                dfhist->accum_p2[fep_state][nval] += de_function*de_function;
            }

            /* Metropolis transition and Barker transition (unoptimized Bennett) acceptance weight determination */

            n0  = dfhist->n_at_lam[fep_state];
            if (fep_state > 0)
            {
                nm1 = dfhist->n_at_lam[fep_state-1];
            }
            else
            {
                nm1 = 0;
            }
            if (fep_state < nlim-1)
            {
                np1 = dfhist->n_at_lam[fep_state+1];
            }
            else
            {
                np1 = 0;
            }

            /* logic SHOULD keep these all set correctly whatever the logic, but apparently it can't figure it out. */
            chi_m1_0 = chi_p1_0 = chi_m2_0 = chi_p2_0 = chi_p1_m1 = chi_p2_m1 = chi_m1_p1 = chi_m2_p1 = 0;

            if (n0 > 0)
            {
                chi_m1_0 = dfhist->accum_m[fep_state][nval]/n0;
                chi_p1_0 = dfhist->accum_p[fep_state][nval]/n0;
                chi_m2_0 = dfhist->accum_m2[fep_state][nval]/n0;
                chi_p2_0 = dfhist->accum_p2[fep_state][nval]/n0;
            }

            if ((fep_state > 0 ) && (nm1 > 0))
            {
                chi_p1_m1 = dfhist->accum_p[fep_state-1][nval]/nm1;
                chi_p2_m1 = dfhist->accum_p2[fep_state-1][nval]/nm1;
            }

            if ((fep_state < nlim-1) && (np1 > 0))
            {
                chi_m1_p1 = dfhist->accum_m[fep_state+1][nval]/np1;
                chi_m2_p1 = dfhist->accum_m2[fep_state+1][nval]/np1;
            }

            omega_m1_0    = 0;
            omega_p1_0    = 0;
            clam_weightsm = 0;
            clam_weightsp = 0;
            clam_varm     = 0;
            clam_varp     = 0;

            if (fep_state > 0)
            {
                if (n0 > 0)
                {
                    omega_m1_0 = chi_m2_0/(chi_m1_0*chi_m1_0) - 1.0;
                }
                if (nm1 > 0)
                {
                    omega_p1_m1 = chi_p2_m1/(chi_p1_m1*chi_p1_m1) - 1.0;
                }
                if ((n0 > 0) && (nm1 > 0))
                {
                    clam_weightsm = (std::log(chi_m1_0) - std::log(chi_p1_m1)) + cnval;
                    clam_varm     = (1.0/n0)*(omega_m1_0) + (1.0/nm1)*(omega_p1_m1);
                }
            }

            if (fep_state < nlim-1)
            {
                if (n0 > 0)
                {
                    omega_p1_0 = chi_p2_0/(chi_p1_0*chi_p1_0) - 1.0;
                }
                if (np1 > 0)
                {
                    omega_m1_p1 = chi_m2_p1/(chi_m1_p1*chi_m1_p1) - 1.0;
                }
                if ((n0 > 0) && (np1 > 0))
                {
                    clam_weightsp = (std::log(chi_m1_p1) - std::log(chi_p1_0)) + cnval;
                    clam_varp     = (1.0/np1)*(omega_m1_p1) + (1.0/n0)*(omega_p1_0);
                }
            }

            if (n0 > 0)
            {
                omegam_array[nval]             = omega_m1_0;
            }
            else
            {
                omegam_array[nval]             = 0;
            }
            weightsm_array[nval]           = clam_weightsm;
            varm_array[nval]               = clam_varm;
            if (nm1 > 0)
            {
                dwm_array[nval]  = fabs( (cnval + std::log((1.0*n0)/nm1)) - lam_dg[fep_state-1] );
            }
            else
            {
                dwm_array[nval]  = fabs( cnval - lam_dg[fep_state-1] );
            }

            if (n0 > 0)
            {
                omegap_array[nval]             = omega_p1_0;
            }
            else
            {
                omegap_array[nval]             = 0;
            }
            weightsp_array[nval]           = clam_weightsp;
            varp_array[nval]               = clam_varp;
            if ((np1 > 0) && (n0 > 0))
            {
                dwp_array[nval]  = fabs( (cnval + std::log((1.0*np1)/n0)) - lam_dg[fep_state] );
            }
            else
            {
                dwp_array[nval]  = fabs( cnval - lam_dg[fep_state] );
            }

        }

        /* find the C's closest to the old weights value */

        min_nvalm     = FindMinimum(dwm_array, maxc);
        omega_m1_0    = omegam_array[min_nvalm];
        clam_weightsm = weightsm_array[min_nvalm];
        clam_varm     = varm_array[min_nvalm];

        min_nvalp     = FindMinimum(dwp_array, maxc);
        omega_p1_0    = omegap_array[min_nvalp];
        clam_weightsp = weightsp_array[min_nvalp];
        clam_varp     = varp_array[min_nvalp];

        clam_osum   = omega_m1_0 + omega_p1_0;
        clam_minvar = 0;
        if (clam_osum > 0)
        {
            clam_minvar = 0.5*std::log(clam_osum);
        }

        if (fep_state > 0)
        {
            lam_dg[fep_state-1]       = clam_weightsm;
            lam_variance[fep_state-1] = clam_varm;
        }

        if (fep_state < nlim-1)
        {
            lam_dg[fep_state]       = clam_weightsp;
            lam_variance[fep_state] = clam_varp;
        }

        if (expand->elamstats == elamstatsMINVAR)
        {
            bSufficientSamples = TRUE;
            /* make sure they are all past a threshold */
            for (i = 0; i < nlim; i++)
            {
                if (dfhist->n_at_lam[i] < expand->minvarmin)
                {
                    bSufficientSamples = FALSE;
                }
            }
            if (bSufficientSamples)
            {
                dfhist->sum_minvar[fep_state] = clam_minvar;
                if (fep_state == 0)
                {
                    for (i = 0; i < nlim; i++)
                    {
                        dfhist->sum_minvar[i] += (expand->minvar_const-clam_minvar);
                    }
                    expand->minvar_const          = clam_minvar;
                    dfhist->sum_minvar[fep_state] = 0.0;
                }
                else
                {
                    dfhist->sum_minvar[fep_state] -= expand->minvar_const;
                }
            }
        }

        /* we need to rezero minvar now, since it could change at fep_state = 0 */
        dfhist->sum_dg[0]       = 0.0;
        dfhist->sum_variance[0] = 0.0;
        dfhist->sum_weights[0]  = dfhist->sum_dg[0] + dfhist->sum_minvar[0]; /* should be zero */

        for (i = 1; i < nlim; i++)
        {
            dfhist->sum_dg[i]       = lam_dg[i-1] + dfhist->sum_dg[i-1];
            dfhist->sum_variance[i] = std::sqrt(lam_variance[i-1] + gmx::square(dfhist->sum_variance[i-1]));
            dfhist->sum_weights[i]  = dfhist->sum_dg[i] + dfhist->sum_minvar[i];
        }

        sfree(lam_dg);
        sfree(lam_variance);

        sfree(omegam_array);
        sfree(weightsm_array);
        sfree(varm_array);
        sfree(dwm_array);

        sfree(omegap_array);
        sfree(weightsp_array);
        sfree(varp_array);
        sfree(dwp_array);
    }
    return FALSE;
}
예제 #6
0
void ZopfliBlockSplitLZ77(const ZopfliOptions* options,
                          const unsigned short* litlens,
                          const unsigned short* dists,
                          size_t llsize, size_t maxblocks,
                          size_t** splitpoints, size_t* npoints, size_t startnpoints) {
  size_t lstart, lend;
  size_t i;
  size_t llpos = 0;
  size_t numblocks = 1;
  unsigned char* done;
  double splitcost, origcost;

  if (llsize < 10) return;  /* This code fails on tiny files. */

  done = (unsigned char*)malloc(llsize);
  if (!done) exit(-1); /* Allocation failed. */
  for (i = 0; i < llsize; i++) done[i] = 0;

  lstart = 0;
  lend = llsize;
  for (;;) {
    SplitCostContext c;

    if (maxblocks > 0 && numblocks >= maxblocks) {
      break;
    }

    c.litlens = litlens;
    c.dists = dists;
    c.llsize = llsize;
    c.start = lstart;
    c.end = lend;
    c.ohh = options->optimizehuffmanheader;
    assert(lstart < lend);
    llpos = FindMinimum(SplitCost, &c, lstart + 1, lend,options);

    assert(llpos > lstart);
    assert(llpos < lend);

    splitcost = EstimateCost(litlens, dists, lstart, llpos, c.ohh) +
        EstimateCost(litlens, dists, llpos, lend, c.ohh);
    origcost = EstimateCost(litlens, dists, lstart, lend, c.ohh);

    if (splitcost > origcost || llpos == lstart + 1 || llpos == lend) {
      done[lstart] = 1;
    } else {
      AddSorted(llpos, splitpoints, npoints);
      ++numblocks;
      if(options->verbose>0 && options->verbose<5) fprintf(stderr,"Initializing blocks: %lu    \r",(unsigned long)(startnpoints+numblocks));
    }

    if (!FindLargestSplittableBlock(
        llsize, done, *splitpoints, *npoints, &lstart, &lend)) {
      break;  /* No further split will probably reduce compression. */
    }

    if (lend - lstart < 10) {
      break;
    }
  }

  if (options->verbose>3) {
    PrintBlockSplitPoints(litlens, dists, llsize, *splitpoints, *npoints);
  }

  if(options->verbose>2) {
    fprintf(stderr, "Total blocks: %lu                 \n\n",(unsigned long)numblocks);
  }


  free(done);
}
예제 #7
0
파일: Explore.c 프로젝트: aperloff/TAMUWW
static bool Explore(count iregion, cSamples *samples, cint depth, cint flags)
{
#define SPLICE (flags & 1)
#define HAVESAMPLES (flags & 2)

  TYPEDEFREGION;

  count n, dim, comp, maxcomp;
  Extrema extrema[NCOMP];
  Result *r;
  real *x, *f;
  real halfvol, maxerr;
  Region *region;
  Bounds *bounds;
  Result *result;

  /* needed as of gcc 3.3 to make gcc correctly address region #@$&! */
  sizeof(*region);

  if( SPLICE ) {
    if( nregions_ == size_ ) {
      size_ += CHUNKSIZE;
      ReAlloc(voidregion_, size_*sizeof(Region));
    }
    VecCopy(region_[nregions_].bounds, region_[iregion].bounds);
    iregion = nregions_++;
  }
  region = &region_[iregion];
  bounds = region->bounds;
  result = region->result;

  for( comp = 0; comp < ncomp_; ++comp ) {
    Extrema *e = &extrema[comp];
    e->fmin = INFTY;
    e->fmax = -INFTY;
    e->xmin = e->xmax = NULL;
  }

  if( !HAVESAMPLES ) {
    real vol = 1;
    for( dim = 0; dim < ndim_; ++dim ) {
      cBounds *b = &bounds[dim];
      vol *= b->upper - b->lower;
    }
    region->vol = vol;

    for( comp = 0; comp < ncomp_; ++comp ) {
      Result *r = &result[comp];
      r->fmin = INFTY;
      r->fmax = -INFTY;
    }

    x = xgiven_;
    f = fgiven_;
    n = ngiven_;
    if( nextra_ ) n += SampleExtra(bounds);

    for( ; n; --n ) {
      for( dim = 0; dim < ndim_; ++dim ) {
        cBounds *b = &bounds[dim];
        if( x[dim] < b->lower || x[dim] > b->upper ) goto skip;
      }
      for( comp = 0; comp < ncomp_; ++comp ) {
        Extrema *e = &extrema[comp];
        creal y = f[comp];
        if( y < e->fmin ) e->fmin = y, e->xmin = x;
        if( y > e->fmax ) e->fmax = y, e->xmax = x;
      }
skip:
      x += ldxgiven_;
      f += ncomp_;
    }

    samples->sampler(samples, bounds, vol);
  }

  x = samples->x;
  f = samples->f;
  for( n = samples->n; n; --n ) {
    for( comp = 0; comp < ncomp_; ++comp ) {
      Extrema *e = &extrema[comp];
      creal y = *f++;
      if( y < e->fmin ) e->fmin = y, e->xmin = x;
      if( y > e->fmax ) e->fmax = y, e->xmax = x;
    }
    x += ndim_;
  }
  neval_opt_ -= neval_;

  halfvol = .5*region->vol;
  maxerr = -INFTY;
  maxcomp = -1;

  for( comp = 0; comp < ncomp_; ++comp ) {
    Extrema *e = &extrema[comp];
    Result *r = &result[comp];
    real xtmp[NDIM], ftmp, err;

    if( e->xmin ) {	/* not all NaNs */
      selectedcomp_ = comp;

      sign_ = 1;
      VecCopy(xtmp, e->xmin);
      ftmp = FindMinimum(bounds, xtmp, e->fmin);
      if( ftmp < r->fmin ) {
        r->fmin = ftmp;
        VecCopy(r->xmin, xtmp);
      }

      sign_ = -1;
      VecCopy(xtmp, e->xmax);
      ftmp = -FindMinimum(bounds, xtmp, -e->fmax);
      if( ftmp > r->fmax ) {
        r->fmax = ftmp;
        VecCopy(r->xmax, xtmp);
      }
    }

    r->avg = samples->avg[comp];
    r->err = samples->err[comp];
    r->spread = halfvol*(r->fmax - r->fmin);

    err = r->spread/Max(fabs(r->avg), NOTZERO);
    if( err > maxerr ) {
      maxerr = err;
      maxcomp = comp;
    }
  }

  neval_opt_ += neval_;

  if( maxcomp == -1 ) {		/* all NaNs */
    region->depth = 0;
    return false;
  }

  region->cutcomp = maxcomp;
  r = &region->result[maxcomp];
  if( halfvol*(r->fmin + r->fmax) > r->avg ) {
    region->fminor = r->fmin;
    region->fmajor = r->fmax;
    region->xmajor = r->xmax - (real *)region->result;
  }
  else {
    region->fminor = r->fmax;
    region->fmajor = r->fmin;
    region->xmajor = r->xmin - (real *)region->result;
  }

  region->depth = IDim(depth);

  if( !HAVESAMPLES ) {
    if( samples->weight*r->spread < r->err ||
        r->spread < totals_[maxcomp].secondspread ) region->depth = 0;
    if( region->depth == 0 )
      for( comp = 0; comp < ncomp_; ++comp )
        totals_[comp].secondspread =
          Max(totals_[comp].secondspread, result[comp].spread);
  }

  if( region->depth ) Split(iregion, region->depth);
  return true;
}
예제 #8
0
파일: Parabola.hpp 프로젝트: hengne/d0wmass
 void Filter() { FillMatrix(); FindMinimum(); }                       // Apply the filter