Example usage for org.apache.commons.math3.random RandomAdaptor RandomAdaptor

List of usage examples for org.apache.commons.math3.random RandomAdaptor RandomAdaptor

Introduction

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

Prototype

public RandomAdaptor(RandomGenerator randomGenerator) 

Source Link

Document

Construct a RandomAdaptor wrapping the supplied RandomGenerator.

Usage

From source file:fingerprints.helper.RandomNumber.java

/**
 * Mersenne Twister Random Number for a hashcode within a range between 0 to maximum
 *
 * @param maximum//from  w  w  w  .  j ava 2  s .  c o m
 * @param hashCode
 * @return
 */
public static long generateMersenneTwisterRandomNumber(int maximum, long hashCode) {
    RandomGenerator rg = new RandomAdaptor(new MersenneTwister(hashCode));
    return rg.nextInt(maximum);
}

From source file:fingerprints.helper.RandomNumber.java

/**
 * Mersenne Twister Random Number//from  www .j  a  v  a2  s  . com
 *
 * @param maximum
 * @return
 */
public static long generateMersenneTwisterRandomNumber(int maximum) {
    RandomGenerator rg = new RandomAdaptor(new MersenneTwister());
    return rg.nextInt(maximum);
}

From source file:edu.byu.nlp.util.Counters.java

/**
 * Get the n entries with the largest value based on some comparator. 
 * Used by Counter's argMaxList method. 
 *///from   ww w  . j ava2s  . c om
public static <E, V extends Comparable<V>> List<E> argMaxList(Set<Entry<E, V>> entrySet, int topn,
        RandomGenerator rnd) {
    topn = (topn > 0) ? topn : entrySet.size();

    List<Entry<E, V>> entries = Lists.newArrayList(entrySet);
    // shuffle to ensure that ties are broken randomly
    if (rnd != null) {
        Collections.shuffle(entries, new RandomAdaptor(rnd));
    }
    // sort to ensure most voted-for options are at the beginning
    Collections.sort(entries, new Comparator<Entry<E, V>>() {
        @Override
        public int compare(Entry<E, V> o1, Entry<E, V> o2) {
            return (o2.getValue()).compareTo(o1.getValue()); // descending order
        }
    });
    // pull out the top n values
    List<E> vals = Lists.newArrayList();
    for (int i = 0; i < Math.min(topn, entries.size()); i++) {
        vals.add(entries.get(i).getKey());
    }
    return vals;
}

From source file:com.github.rinde.logistics.pdptw.mas.route.RandomRoutePlanner.java

/**
 * Creates a random route planner using the specified random seed.
 * @param seed The random seed./*from   w w w .j a va 2s  .c  o m*/
 */
public RandomRoutePlanner(long seed) {
    LOGGER.info("constructor {}", seed);
    assignedParcels = LinkedHashMultiset.create();
    current = Optional.absent();
    rng = new RandomAdaptor(new MersenneTwister(seed));
}

From source file:edu.byu.nlp.util.IntArrays.java

public static int[] shuffled(int[] arr, RandomGenerator rnd) {
    // int[] -> List
    List<Integer> tmp = Lists.newArrayListWithCapacity(arr.length);
    for (int i = 0; i < arr.length; i++) {
        tmp.add(arr[i]);// w  w w .j  av  a 2  s .c  o m
    }
    // shuffle
    Collections.shuffle(tmp, new RandomAdaptor(rnd));
    // List -> int[] (wish there were a better way to do this)
    int[] arr2 = new int[tmp.size()];
    for (int i = 0; i < tmp.size(); i++) {
        arr2[i] = tmp.get(i);
    }
    return arr2;
}

From source file:edu.byu.nlp.al.RandomMeasurementSelector.java

/**
 * @param modelBuilder/*from   ww  w .ja va  2s.c  om*/
 * @param annotations 
 */
public RandomMeasurementSelector(MeasurementModelBuilder modelBuilder, Dataset dataset,
        EmpiricalAnnotations<SparseFeatureVector, Integer> annotations, RandomGenerator rnd) {
    // we want to add all measurements that are not already taken (used as seed set contained in dataset)
    // FIXME: this is horrifically inefficient! Fix it! 
    for (FlatInstance<SparseFeatureVector, Integer> meas : annotations.getMeasurements()) {
        if (!dataset.getMeasurements().contains(meas)) {
            candidates.add(meas);
        }
    }
    for (Multimap<Integer, FlatInstance<SparseFeatureVector, Integer>> perAnnotatorAnnotations : annotations
            .getPerInstancePerAnnotatorAnnotations().values()) {
        for (FlatInstance<SparseFeatureVector, Integer> meas : perAnnotatorAnnotations.values()) {
            candidates.add(meas);
        }
    }
    Collections.shuffle(candidates, new RandomAdaptor(rnd));
}

From source file:com.github.rinde.logistics.pdptw.mas.comm.AuctionCommModel.java

AuctionCommModel(AuctionStopCondition<T> sc, Clock c, long maxAuctDurMs, @Nullable RandomGenerator r) {
    stopCondition = sc;/*w ww.  j  a v a2  s.co m*/
    parcelAuctioneerMap = new LinkedHashMap<>();
    maxAuctionDurationMs = maxAuctDurMs;
    rng = r == null ? null : new RandomAdaptor(r);

    eventDispatcher = new EventDispatcher(EventType.values());
    if (c instanceof RealtimeClockController) {
        clock = (RealtimeClockController) c;
    } else {
        clock = null;
    }

    numAuctions = new AtomicInteger();
}

From source file:com.github.rinde.rinsim.central.RandomSolver.java

@Override
public ImmutableList<ImmutableList<Parcel>> solve(GlobalStateObject state) {
    checkArgument(!state.getVehicles().isEmpty(), "Need at least one vehicle.");
    final LinkedListMultimap<VehicleStateObject, Parcel> map = LinkedListMultimap.create();

    final Set<Parcel> available = newLinkedHashSet(state.getAvailableParcels());
    final Set<Parcel> destinations = newLinkedHashSet();
    for (final VehicleStateObject vso : state.getVehicles()) {
        destinations.addAll(vso.getDestination().asSet());
    }/* w  w w .j a v a 2s . c  o  m*/
    available.removeAll(destinations);

    // do random assignment of available parcels
    for (final Parcel p : available) {
        final int index = randomGenerator.nextInt(state.getVehicles().size());
        map.put(state.getVehicles().get(index), p);
        map.put(state.getVehicles().get(index), p);
    }

    final ImmutableList.Builder<ImmutableList<Parcel>> builder = ImmutableList.builder();
    // insert contents, shuffle ordering, insert destination if applicable
    for (final VehicleStateObject vso : state.getVehicles()) {
        final List<Parcel> assigned = newArrayList(map.get(vso));
        final List<Parcel> conts = newArrayList(vso.getContents());
        conts.removeAll(vso.getDestination().asSet());
        assigned.addAll(conts);
        if (vso.getDestination().isPresent()
                && state.getAvailableParcels().contains(vso.getDestination().get())) {
            assigned.add(vso.getDestination().get());
        }
        Collections.shuffle(assigned, new RandomAdaptor(randomGenerator));
        if (vso.getDestination().isPresent()) {
            assigned.add(0, vso.getDestination().get());
        }
        builder.add(ImmutableList.copyOf(assigned));
    }
    return builder.build();
}

From source file:edu.byu.nlp.util.Iterables2.java

public static <E> Iterable<E> shuffled(final Iterable<E> iterable, RandomGenerator rnd) {
    ArrayList<E> retval = Lists.newArrayList(iterable);
    Collections.shuffle(retval, new RandomAdaptor(rnd));
    return retval;
}

From source file:com.github.rinde.rinsim.examples.demo.swarm.DemoPanel.java

@Override
public void handleEvent(@Nullable Event event) {
    assert event != null;
    final Iterator<Point> points = SwarmDemo
            .measureString(((Text) event.widget).getText(), SwarmDemo.FONT_SIZE, SPACING, 0).iterator();
    final List<Vehicle> vs = newArrayList(vehicles);
    if (event.type == SWT.DefaultSelection) {
        Collections.shuffle(vs, new RandomAdaptor(rng));
    }//from w  w  w  .  ja v  a  2  s.  co m
    for (final Vehicle v : vs) {
        if (points.hasNext()) {
            v.setDestination(points.next());
        } else {
            v.setInactive();
        }
    }
}