List of usage examples for org.apache.commons.math3.random RandomDataGenerator nextPermutation
public int[] nextPermutation(int n, int k) throws NumberIsTooLargeException, NotStrictlyPositiveException
Uses a 2-cycle permutation shuffle.
From source file:com.cloudera.oryx.ml.param.GridSearch.java
/** * @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. *//* ww w . j a va 2s . c o m*/ 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; }
From source file:com.cloudera.oryx.ml.param.HyperParamRanges.java
/** * @param ranges ranges of hyperparameters to try, one per hyperparameters * @param howMany how many combinations of hyperparameters to return * @param perParam how many different hyperparameter values to try per hyperparameter * @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. *///from www . j a v a 2 s . c o m public static List<List<Number>> chooseHyperParameterCombos(Collection<HyperParamRange> ranges, int howMany, int perParam) { Preconditions.checkArgument(howMany > 0); Preconditions.checkArgument(perParam >= 0); int numParams = ranges.size(); if (numParams == 0 || perParam == 0) { return Collections.singletonList(Collections.<Number>emptyList()); } // Put some reasonable upper limit on the number of combos Preconditions.checkArgument(Math.pow(perParam, numParams) <= MAX_COMBOS); int howManyCombos = 1; List<List<Number>> paramRanges = new ArrayList<>(numParams); for (HyperParamRange range : ranges) { List<Number> values = range.getTrialValues(perParam); paramRanges.add(values); howManyCombos *= values.size(); } List<List<Number>> allCombinations = new ArrayList<>(howManyCombos); for (int combo = 0; combo < howManyCombos; combo++) { List<Number> 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<Number>> result = new ArrayList<>(indices.length); for (int i = 0; i < indices.length; i++) { result.add(allCombinations.get(i)); } Collections.shuffle(result); return result; }
From source file:com.anhth12.lambda.ml.param.HyperParams.java
public static List<List<?>> chooseHyperParameterCombos(Collection<HyperParamValues<?>> ranges, int howMany, int perParam) { Preconditions.checkArgument(howMany > 0); Preconditions.checkArgument(perParam >= 0); int numParams = ranges.size(); if (numParams == 0 || perParam == 0) { return Collections.<List<?>>singletonList(Collections.emptyList()); }/*from w w w . j a va 2 s.c o m*/ // Put some reasonable upper limit on the number of combos Preconditions.checkArgument(Math.pow(perParam, numParams) <= MAX_COMBOS); 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; }
From source file:com.cloudera.oryx.ml.param.HyperParams.java
/** * @param ranges ranges of hyperparameters to try, one per hyperparameters * @param howMany how many combinations of hyperparameters to return * @param perParam how many different hyperparameter values to try per hyperparameter * @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. *//* ww w. ja v a 2 s. co m*/ public static List<List<?>> chooseHyperParameterCombos(Collection<HyperParamValues<?>> ranges, int howMany, int perParam) { Preconditions.checkArgument(howMany > 0); Preconditions.checkArgument(perParam >= 0); int numParams = ranges.size(); if (numParams == 0 || perParam == 0) { return Collections.<List<?>>singletonList(Collections.emptyList()); } // Put some reasonable upper limit on the number of combos Preconditions.checkArgument(Math.pow(perParam, numParams) <= MAX_COMBOS); 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; }
From source file:classif.random.DTWKNNClassifierRandom.java
@Override protected void buildSpecificClassifier(Instances data) { indexPrototypeInTrainData = new ArrayList<Integer>(); ArrayList<String> classes = new ArrayList<String>(classedData.keySet()); RandomDataGenerator gen = new RandomDataGenerator(); for (String clas : classes) { ArrayList<Sequence> cData = classedData.get(clas); // if the class is empty, skip it if (cData.isEmpty()) continue; int maxElements = Math.min(nbPrototypesPerClass, cData.size()); int[] selectedElements = gen.nextPermutation(cData.size(), maxElements); for (int i = 0; i < selectedElements.length; i++) { int indexInFullData = indexClassedDataInFullData.get(clas).get(selectedElements[i]); indexPrototypeInTrainData.add(indexInFullData); // System.out.println("prototype "+i+" of class "+clas+" is element "+selectedElements[i]+" index in data="+indexInFullData); ClassedSequence prot = new ClassedSequence(cData.get(selectedElements[i]), clas); prototypes.add(prot);/* w ww . ja v a 2s. com*/ } } }
From source file:classif.Prototyper.java
/** * If the Prototyper doesn't return as many prototypes as required, then add * random data to fill the prototype list. */// www .j av a 2 s .c om private void addMissingPrototypesRandom() { int nbPrototypesNeeded = nbPrototypesPerClass * classedData.keySet().size(); if (prototypes.size() < nbPrototypesNeeded) { // ~ needs to be filled RandomDataGenerator randGen = new RandomDataGenerator(); int nbMissing = nbPrototypesNeeded - prototypes.size(); int nbToAdd = Math.min(nbMissing, sequences.length);// if less data // than // prototypes // asked int[] indexFillins = randGen.nextPermutation(sequences.length, nbToAdd); for (int index : indexFillins) { ClassedSequence s = new ClassedSequence(sequences[index], classMap[index]); prototypes.add(s); } } }
From source file:gdsc.core.clustering.optics.OPTICSManager.java
/** * Compute (a sample of) the k-nearest neighbour distance for objects from the data * The plot of the sorted k-distance can be used to pick the generating distance. Or it can be done * automatically using a % noise threshold. * <p>/*w w w. j av a2 s. c o m*/ * See: * Jrg Sander, Martin Ester, Hans-Peter Kriegel, Xiaowei Xu * Density-Based Clustering in Spatial Databases: The Algorithm GDBSCAN and Its Applications * Data Mining and Knowledge Discovery, 1998. * * @param k * the k (automatically bounded between 1 and size-1) * @param samples * the number of samples (set to below 1 to compute all samples) * @param cache * Set to true to cache the KD-tree used for the nearest neighbour search * @return the k-nearest neighbour distances */ public float[] nearestNeighbourDistance(int k, int samples, boolean cache) { int size = xcoord.length; if (size < 2) return new float[0]; long time = System.currentTimeMillis(); // Optionally compute all samples if (samples <= 0) samples = size; // Bounds check k if (k < 1) k = 1; else if (k >= size) k = size - 1; final int n = Math.min(samples, size); float[] d = new float[n]; if (tracker != null) { tracker.log("Computing %d nearest-neighbour distances, samples=%d", k, n); tracker.progress(0, n); } int[] indices; if (n <= size) { // Compute all indices = Utils.newArray(n, 0, 1); } else { // Random sample RandomDataGenerator r = new RandomDataGenerator(); indices = r.nextPermutation(size, n); } // Use a KDtree to allow search of the space if (tree == null) { tree = new SimpleFloatKdTree2D.SqrEuclid2D(); for (int i = 0; i < size; i++) tree.addPoint(new float[] { xcoord[i], ycoord[i] }); } // Note: The k-nearest neighbour search will include the actual point so increment by 1 k++; for (int i = 0; i < n; i++) { if (tracker != null) tracker.progress(i, n); int index = indices[i]; float[] location = new float[] { xcoord[index], ycoord[index] }; // The tree will use the squared distance so compute the root d[i] = (float) (Math.sqrt(tree.nearestNeighbor(location, k)[0])); } if (tracker != null) { time = System.currentTimeMillis() - time; tracker.log("Finished KNN computation (Time = " + Utils.timeToString(time) + ")"); tracker.progress(1); } if (!cache) tree = null; return d; }
From source file:org.apache.sysml.runtime.compress.PlanningBinPacker.java
/** * shuffling the items to make some potential for having bins of different * sizes when consecutive columns are of close cardinalities * /*from ww w . j a va 2 s .c o m*/ * @return key: available space, value: list of the bins that have that free * space */ public TreeMap<Float, List<List<Integer>>> packFirstFitShuffled() { RandomDataGenerator rnd = new RandomDataGenerator(); int[] permutation = rnd.nextPermutation(_items.size(), _items.size()); List<Integer> shuffledItems = new ArrayList<Integer>(_items.size()); List<Float> shuffledWeights = new ArrayList<Float>(_items.size()); for (int ix : permutation) { shuffledItems.add(_items.get(ix)); shuffledWeights.add(_itemWeights.get(ix)); } return packFirstFit(shuffledItems, shuffledWeights); }
From source file:org.bhave.network.model.impl.DefaultBAForestModel.java
@Override public void generateNetwork() { resetModel();/*from ww w .j a v a 2s .c o m*/ // get configuration values int n = config.getInt(PARAM_NUM_NODES); // pool representing the links int[] linkPool = new int[2 * (n - 1)]; // seed network with 1 connection linkPool[0] = 0; linkPool[1] = 1; int numLinks = 1; // create the other edges for (int v = 2; v < n; v++) { int index = 2 * numLinks; linkPool[index] = v; int r = random.nextInt(index); linkPool[index + 1] = linkPool[r]; numLinks++; } Node[] nodes = new Node[n]; // add nodes to the network for (int i = 0; i < n; i++) { Node newNode = network.createNode(); network.addNode(newNode); nodes[i] = newNode; } // use the existing random number generator to shuffle our nodes RandomDataGenerator randomPerm = new RandomDataGenerator(random); int[] perm = randomPerm.nextPermutation(n, n); // add links to the Network for (int i = 0; i < linkPool.length; i += 2) { Node node1 = nodes[perm[linkPool[i]]]; Node node2 = nodes[perm[linkPool[i + 1]]]; network.addLink(node1, node2); } }
From source file:org.bhave.network.model.impl.DefaultBAModel.java
@Override public void generateNetwork() { // get configuration values int n = config.getInt(PARAM_NUM_NODES); int d = config.getInt(PARAM_MIN_DEG); Node[] nodes = new Node[n]; // add nodes to the network for (int i = 0; i < n; i++) { Node newNode = network.createNode(); network.addNode(newNode);/*from w w w .j av a2s .c om*/ nodes[i] = newNode; } // use the existing random number generator to shuffle our nodeArray RandomDataGenerator randomPerm = new RandomDataGenerator(random); int[] perm = randomPerm.nextPermutation(n, n); // from now on instead of using nodes[i] you use nodes[perm[i]] to get // the node indicated by the permutation // pool for node scores int[] scores = new int[n]; int numLinks = 0; // add 2 nodes network.addLink(nodes[perm[0]], nodes[perm[1]]); scores[0] = 1; scores[1] = 1; numLinks = 1; // add the rest of the nodes for (int v = 2; v < n; v++) { // current nodes involved HashSet<Integer> current = new HashSet<>(); int maxLimit = numLinks * 2; int i = 0; // add d nodes for (i = 0; i < d && i < v; i++) { int tempMaxLimit = maxLimit; for (Integer e : current) { tempMaxLimit -= scores[e]; } int r = random.nextInt(tempMaxLimit); int p = preferentialAttachment(scores, r, tempMaxLimit, current); network.addLink(nodes[perm[v]], nodes[perm[p]]); numLinks++; current.add(p); } scores[v] += i; for (Integer e : current) { scores[e]++; } } }