Java Code Examples for org.apache.commons.math3.random.RandomDataGenerator#nextPermutation()

The following examples show how to use org.apache.commons.math3.random.RandomDataGenerator#nextPermutation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UtilFunctions.java    From systemds with Apache License 2.0 5 votes vote down vote up
public static int[] getSortedSampleIndexes(int range, int sampleSize, long seed) {
	RandomDataGenerator rng = new RandomDataGenerator();
	if (seed != -1){
		rng.reSeed(seed);
	}
	int[] sample = rng.nextPermutation(range, sampleSize);
	Arrays.sort(sample);
	return sample;
}
 
Example 2
Source File: UtilFunctions.java    From systemds with Apache License 2.0 5 votes vote down vote up
public static int[] getSortedSampleIndexes(int range, int sampleSize, long seed) {
	RandomDataGenerator rng = new RandomDataGenerator();
	if (seed != -1){
		rng.reSeed(seed);
	}
	int[] sample = rng.nextPermutation(range, sampleSize);
	Arrays.sort(sample);
	return sample;
}
 
Example 3
Source File: MathUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Make sure that permuted arrays do not hash to the same value.
 */
@Test
public void testPermutedArrayHash() {
    double[] original = new double[10];
    double[] permuted = new double[10];
    RandomDataGenerator random = new RandomDataGenerator();

    // Generate 10 distinct random values
    for (int i = 0; i < 10; i++) {
        final RealDistribution u = new UniformRealDistribution(i + 0.5, i + 0.75);
        original[i] = u.sample();
    }

    // Generate a random permutation, making sure it is not the identity
    boolean isIdentity = true;
    do {
        int[] permutation = random.nextPermutation(10, 10);
        for (int i = 0; i < 10; i++) {
            if (i != permutation[i]) {
                isIdentity = false;
            }
            permuted[i] = original[permutation[i]];
        }
    } while (isIdentity);

    // Verify that permuted array has different hash
    Assert.assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
}
 
Example 4
Source File: MathUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Make sure that permuted arrays do not hash to the same value.
 */
@Test
public void testPermutedArrayHash() {
    double[] original = new double[10];
    double[] permuted = new double[10];
    RandomDataGenerator random = new RandomDataGenerator();

    // Generate 10 distinct random values
    for (int i = 0; i < 10; i++) {
        final RealDistribution u = new UniformRealDistribution(i + 0.5, i + 0.75);
        original[i] = u.sample();
    }

    // Generate a random permutation, making sure it is not the identity
    boolean isIdentity = true;
    do {
        int[] permutation = random.nextPermutation(10, 10);
        for (int i = 0; i < 10; i++) {
            if (i != permutation[i]) {
                isIdentity = false;
            }
            permuted[i] = original[permutation[i]];
        }
    } while (isIdentity);

    // Verify that permuted array has different hash
    Assert.assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
}
 
Example 5
Source File: MathUtil.java    From picard with MIT License 5 votes vote down vote up
/**
 * permute the input array randomly (using a RandomDataGenerator)
 *
 * @param array input array
 * @param rdg   a RandomDataGenerator for drawing a permutation from
 * @return a newly allocated array with a permuted version of the original data.
 */
public static double[] permute(double[] array, RandomDataGenerator rdg) {

    final int n = array.length;
    final double[] retVal = new double[n];
    final int[] randomPermutation = rdg.nextPermutation(n, n);
    for (int i = 0; i < n; i++) {
        retVal[i] = array[randomPermutation[i]];
    }
    return retVal;
}
 
Example 6
Source File: GridSearch.java    From oryx with Apache License 2.0 4 votes vote down vote up
/**
 * @param ranges ranges of hyperparameters to try, one per hyperparameters
 * @param howMany how many combinations of hyperparameters to return
 * @return combinations of concrete hyperparameter values. For example, for 5 parameters each
 *  with 3 values to try, the total number of combinations returned could be up to pow(3,5)
 *  or 243. The number could be less if the ranges do not actually have that many distinct
 *  values. If {@code howMany} is smaller than the total number of combinations, a random
 *  subset of all combinations are returned. The order is shuffled randomly. If no parameters
 *  are specified or {@code perParam} is 0, a single empty combination is returned.
 */
static List<List<?>> chooseHyperParameterCombos(List<HyperParamValues<?>> ranges, int howMany) {
  // Put some reasonable upper limit on the number of combos
  Preconditions.checkArgument(howMany > 0 && howMany <= MAX_COMBOS);
  
  int numParams = ranges.size();
  int perParam = chooseValuesPerHyperParam(ranges, howMany);
  if (numParams == 0 || perParam == 0) {
    return Collections.singletonList(Collections.emptyList());
  }
  
  int howManyCombos = 1;
  List<List<?>> paramRanges = new ArrayList<>(numParams);
  for (HyperParamValues<?> range : ranges) {
    List<?> values = range.getTrialValues(perParam);
    paramRanges.add(values);
    howManyCombos *= values.size();
  }

  List<List<?>> allCombinations = new ArrayList<>(howManyCombos);
  for (int combo = 0; combo < howManyCombos; combo++) {
    List<Object> combination = new ArrayList<>(numParams);
    for (int param = 0; param < numParams; param++) {
      int whichValueToTry = combo;
      for (int i = 0; i < param; i++) {
        whichValueToTry /= paramRanges.get(i).size();
      }
      whichValueToTry %= paramRanges.get(param).size();
      combination.add(paramRanges.get(param).get(whichValueToTry));
    }
    allCombinations.add(combination);
  }

  if (howMany >= howManyCombos) {
    Collections.shuffle(allCombinations);
    return allCombinations;
  }
  RandomDataGenerator rdg = new RandomDataGenerator(RandomManager.getRandom());
  int[] indices = rdg.nextPermutation(howManyCombos, howMany);
  List<List<?>> result = new ArrayList<>(indices.length);
  for (int i = 0; i < indices.length; i++) {
    result.add(allCombinations.get(i));
  }
  Collections.shuffle(result);
  return result;
}