Example usage for org.apache.commons.math3.random RandomVectorGenerator nextVector

List of usage examples for org.apache.commons.math3.random RandomVectorGenerator nextVector

Introduction

In this page you can find the example usage for org.apache.commons.math3.random RandomVectorGenerator nextVector.

Prototype

double[] nextVector();

Source Link

Document

Generate a random vector.

Usage

From source file:gdsc.core.clustering.optics.ProjectedMoleculeSpace.java

/**
 * Create random projections, project points and put points into sets of size
 * about minSplitSize/2.// w  ww .  j  a  v  a  2 s.c  om
 *
 * @param minSplitSize
 *            minimum size for which a point set is further
 *            partitioned (roughly corresponds to minPts in OPTICS)
 */
public void computeSets(int minSplitSize) {
    splitSets = new TurboList<Split>();

    // Edge cases
    if (minSplitSize < 2 || size <= 1)
        return;

    if (size == 2) {
        // No point performing projections and splits
        splitSets.add(new Split(0, new int[] { 0, 1 }));
        return;
    }

    final int dim = 2;

    // FastOPTICS paper states you can use c0*log(N) sets and c1*log(N) projections.
    // The ELKI framework increase this for the number of dimensions. However I have stuck
    // with the original (as it is less so will be faster).
    // Note: In most computer science contexts log is in base 2.
    int nPointSetSplits, nProject1d;

    nPointSetSplits = getNumberOfSplitSets(nSplits, size);
    nProject1d = getNumberOfProjections(nProjections, size);

    // perform O(log N+log dim) splits of the entire point sets projections
    //nPointSetSplits = (int) (logOProjectionConst * log2(size * dim + 1));
    // perform O(log N+log dim) projections of the point set onto a random line
    //nProject1d = (int) (logOProjectionConst * log2(size * dim + 1));

    if (nPointSetSplits < 1 || nProject1d < 1)
        return; // Nothing to do

    // perform projections of points
    float[][] projectedPoints = new float[nProject1d][];

    long time = System.currentTimeMillis();
    setUpProgress(nProject1d);
    if (tracker != null) {
        tracker.log("Computing projections ...");
    }

    // Multi-thread this for speed
    int nThreads = Math.min(this.nThreads, nPointSetSplits);
    final TurboList<Thread> threads = new TurboList<Thread>(nThreads);

    final BlockingQueue<ProjectionJob> projectionJobs = new ArrayBlockingQueue<ProjectionJob>(nThreads * 2);
    final TurboList<ProjectionWorker> projectionWorkers = new TurboList<ProjectionWorker>(nThreads);
    for (int i = 0; i < nThreads; i++) {
        final ProjectionWorker worker = new ProjectionWorker(projectionJobs, projectedPoints);
        final Thread t = new Thread(worker);
        projectionWorkers.addf(worker);
        threads.addf(t);
        t.start();
    }

    // Create random vectors or uniform distribution
    RandomVectorGenerator vectorGen = (useRandomVectors) ? new UnitSphereRandomVectorGenerator(2, rand) : null;
    final double increment = Math.PI / nProject1d;
    for (int i = 0; i < nProject1d; i++) {
        // Create a random unit vector
        double[] currRp;
        if (useRandomVectors) {
            currRp = vectorGen.nextVector();
        } else {
            // For a 2D vector we can just uniformly distribute them around a semi-circle
            currRp = new double[dim];
            double a = i * increment;
            currRp[0] = Math.sin(a);
            currRp[1] = Math.cos(a);
        }
        put(projectionJobs, new ProjectionJob(i, currRp));
    }
    // Finish all the worker threads by passing in a null job
    for (int i = 0; i < nThreads; i++) {
        put(projectionJobs, new ProjectionJob(-1, null));
    }

    // Wait for all to finish
    for (int i = 0; i < nThreads; i++) {
        try {
            threads.get(i).join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    threads.clear();

    if (tracker != null) {
        tracker.progress(1);
        long time2 = System.currentTimeMillis();
        tracker.log("Computed projections ... " + Utils.timeToString(time2 - time));
        time = time2;
        tracker.log("Splitting data ...");
    }

    // split entire point set, reuse projections by shuffling them
    int[] proind = Utils.newArray(nProject1d, 0, 1);
    setUpProgress(nPointSetSplits);

    // The splits do not have to be that random so we can use a pseudo random sequence.
    // The sets will be randomly sized between 1 and minSplitSize. Ensure we have enough 
    // numbers for all the splits.
    double expectedSetSize = (1 + minSplitSize) * 0.5;
    int expectedSets = (int) Math.round(size / expectedSetSize);
    pseudoRandom = new TurboRandomGenerator(Math.max(200, minSplitSize + 2 * expectedSets), rand);

    // Multi-thread this for speed
    final BlockingQueue<SplitJob> splitJobs = new ArrayBlockingQueue<SplitJob>(nThreads * 2);
    final TurboList<SplitWorker> splitWorkers = new TurboList<SplitWorker>(nThreads);
    for (int i = 0; i < nThreads; i++) {
        final SplitWorker worker = new SplitWorker(splitJobs, minSplitSize);
        final Thread t = new Thread(worker);
        splitWorkers.addf(worker);
        threads.addf(t);
        t.start();
    }

    for (int i = 0; i < nPointSetSplits; i++) {
        // shuffle projections
        float[][] shuffledProjectedPoints = new float[nProject1d][];
        pseudoRandom.shuffle(proind);
        for (int j = 0; j < nProject1d; j++) {
            shuffledProjectedPoints[j] = projectedPoints[proind[j]];
        }

        // New random generator
        TurboRandomGenerator rand = (TurboRandomGenerator) pseudoRandom.clone();
        rand.setSeed(i);

        put(splitJobs, new SplitJob(i, shuffledProjectedPoints, rand));
    }

    // Finish all the worker threads by passing in a null job
    for (int i = 0; i < nThreads; i++) {
        put(splitJobs, new SplitJob(-1, null, null));
    }

    // Wait for all to finish
    int total = 0;
    for (int i = 0; i < nThreads; i++) {
        try {
            threads.get(i).join();
            total += splitWorkers.get(i).splitSets.size();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    threads.clear();

    // Merge the split-sets
    splitSets = splitWorkers.get(0).splitSets;
    splitSets.ensureCapacity(total);
    for (int i = 1; i < nThreads; i++)
        splitSets.addAll(splitWorkers.get(i).splitSets);

    if (tracker != null) {
        time = System.currentTimeMillis() - time;
        tracker.log("Split data ... " + Utils.timeToString(time));
        tracker.progress(1);
    }
}

From source file:yaphyre.core.samplers.HaltonSampler.java

private void setupSampler(int numberOfSamples) {
    RandomVectorGenerator vectorGenerator = new HaltonSequenceGenerator(2);
    unitSquareSamples = Lists.newArrayListWithCapacity(numberOfSamples);
    for (int i = 0; i < numberOfSamples; i++) {
        unitSquareSamples.add(Point2D.of(vectorGenerator.nextVector()));
    }//from  w  w  w.ja  va  2 s  .com
    unitSquareSamples = Collections.unmodifiableList(unitSquareSamples);
}