CombinationSampler.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.rng.sampling;

  18. import org.apache.commons.rng.UniformRandomProvider;

  19. /**
  20.  * Class for representing <a href="https://en.wikipedia.org/wiki/Combination">combinations</a>
  21.  * of a sequence of integers.
  22.  *
  23.  * <p>A combination is a selection of items from a collection, such that (unlike
  24.  * permutations) the order of selection <strong>does not matter</strong>. This
  25.  * sampler can be used to generate a combination in an unspecified order and is
  26.  * faster than the corresponding {@link PermutationSampler}.</p>
  27.  *
  28.  * <p>Note that the sample order is unspecified. For example a sample
  29.  * combination of 2 from 4 may return {@code [0,1]} or {@code [1,0]} as the two are
  30.  * equivalent, and the order of a given combination may change in subsequent samples.</p>
  31.  *
  32.  * <p>The sampler can be used to generate indices to select subsets where the
  33.  * order of the subset is not important.</p>
  34.  *
  35.  * <p>Sampling uses {@link UniformRandomProvider#nextInt(int)}.</p>
  36.  *
  37.  * @see PermutationSampler
  38.  */
  39. public class CombinationSampler implements SharedStateObjectSampler<int[]> {
  40.     /** Domain of the combination. */
  41.     private final int[] domain;
  42.     /** The number of steps of a full shuffle to perform. */
  43.     private final int steps;
  44.     /**
  45.      * The section to copy the domain from after a partial shuffle.
  46.      */
  47.     private final boolean upper;
  48.     /** RNG. */
  49.     private final UniformRandomProvider rng;

  50.     /**
  51.      * Creates a generator of combinations.
  52.      *
  53.      * <p>The {@link #sample()} method will generate an integer array of
  54.      * length {@code k} whose entries are selected randomly, without
  55.      * repetition, from the integers 0, 1, ..., {@code n}-1 (inclusive).
  56.      * The returned array represents a combination of {@code n} taken
  57.      * {@code k}.
  58.      *
  59.      * <p>In contrast to a permutation, the returned array is <strong>not
  60.      * guaranteed</strong> to be in a random order. The {@link #sample()}
  61.      * method returns the array in an unspecified order.
  62.      *
  63.      * <p>If {@code n <= 0} or {@code k <= 0} or {@code k > n} then no combination
  64.      * is required and an exception is raised.
  65.      *
  66.      * @param rng Generator of uniformly distributed random numbers.
  67.      * @param n   Domain of the combination.
  68.      * @param k   Size of the combination.
  69.      * @throws IllegalArgumentException if {@code n <= 0} or {@code k <= 0} or
  70.      *                                  {@code k > n}.
  71.      */
  72.     public CombinationSampler(UniformRandomProvider rng,
  73.                               int n,
  74.                               int k) {
  75.         SubsetSamplerUtils.checkSubset(n, k);
  76.         domain = PermutationSampler.natural(n);
  77.         // The sample can be optimised by only performing the first k or (n - k) steps
  78.         // from a full Fisher-Yates shuffle from the end of the domain to the start.
  79.         // The upper positions will then contain a random sample from the domain. The
  80.         // lower half is then by definition also a random sample (just not in a random order).
  81.         // The sample is then picked using the upper or lower half depending which
  82.         // makes the number of steps smaller.
  83.         upper = k <= n / 2;
  84.         steps = upper ? k : n - k;
  85.         this.rng = rng;
  86.     }

  87.     /**
  88.      * @param rng Generator of uniformly distributed random numbers.
  89.      * @param source Source to copy.
  90.      */
  91.     private CombinationSampler(UniformRandomProvider rng,
  92.                                CombinationSampler source) {
  93.         // Do not clone the domain. This ensures:
  94.         // 1. Thread safety as the domain may be shuffled during the clone
  95.         //    and a shuffle swap step can result in duplicates and missing elements
  96.         //    in the array.
  97.         // 2. If the argument RNG is an exact match for the RNG in the source
  98.         //    then the output sequence will differ unless the domain is currently
  99.         //    in natural order.
  100.         domain = PermutationSampler.natural(source.domain.length);
  101.         steps = source.steps;
  102.         upper = source.upper;
  103.         this.rng = rng;
  104.     }

  105.     /**
  106.      * Return a combination of {@code k} whose entries are selected randomly,
  107.      * without repetition, from the integers 0, 1, ..., {@code n}-1 (inclusive).
  108.      *
  109.      * <p>The order of the returned array is not guaranteed to be in a random order
  110.      * as the order of a combination <strong>does not matter</strong>.
  111.      *
  112.      * @return a random combination.
  113.      */
  114.     @Override
  115.     public int[] sample() {
  116.         return SubsetSamplerUtils.partialSample(domain, steps, rng, upper);
  117.     }

  118.     /**
  119.      * {@inheritDoc}
  120.      *
  121.      * @since 1.3
  122.      */
  123.     @Override
  124.     public CombinationSampler withUniformRandomProvider(UniformRandomProvider rng) {
  125.         return new CombinationSampler(rng, this);
  126.     }
  127. }