source: ip/src/main/java/geniusweb/ip/general/RandomSubsetOfGivenSet.java@ 47

Last change on this file since 47 was 47, checked in by bart, 3 years ago

Refactor to help reusing partiesserver.

File size: 3.6 KB
Line 
1package geniusweb.ip.general;
2
3import java.util.Random;
4
5public class RandomSubsetOfGivenSet {
6 final private int[] givenSet;
7 final private int numOfElementsInGivenSet;
8 final private int sizeOfSubsetToBeSampled;
9 final private boolean subsetEqualsGivenSet;
10 private int[] availableIndicesToChooseFrom;
11 private Random randomGenerator;
12
13 // ******************************************************************************************************
14
15 /**
16 * The constructor:
17 *
18 * @param givenSet: The set from which subsets will be
19 * sampled
20 * @param numOfElementsInGivenSet: the number of elements in the set from
21 * which subsets will be sampled
22 * @param sizeOfSubsetToBeSampled: the size of every subset that will be
23 * sampled.
24 */
25 public RandomSubsetOfGivenSet(int[] givenSet, int numOfElementsInGivenSet,
26 int sizeOfSubsetToBeSampled) {
27 this.givenSet = givenSet;
28 this.numOfElementsInGivenSet = numOfElementsInGivenSet;
29 this.sizeOfSubsetToBeSampled = sizeOfSubsetToBeSampled;
30 this.randomGenerator = new Random();
31 this.availableIndicesToChooseFrom = new int[numOfElementsInGivenSet];
32 if (sizeOfSubsetToBeSampled == numOfElementsInGivenSet)
33 subsetEqualsGivenSet = true;
34 else
35 subsetEqualsGivenSet = false;
36 }
37
38 // ******************************************************************************************************
39
40 /**
41 * Returns random subset of size: "sizeOfSubsetToBeSampled", taken from the
42 * set: "givenSet"
43 */
44 public int[] getSubsetInByteFormat() {
45 if (this.subsetEqualsGivenSet) {
46 int[] copyOfGivenSet = new int[numOfElementsInGivenSet];
47 for (int i = 0; i < numOfElementsInGivenSet; i++) {
48 copyOfGivenSet[i] = givenSet[i];
49 }
50 return copyOfGivenSet;
51 } else {
52 // this.randomGenerator = new Random();
53 for (int i = numOfElementsInGivenSet - 1; i >= 0; i--) {
54 availableIndicesToChooseFrom[i] = i;
55 }
56 int[] subsetInByteFormat = new int[sizeOfSubsetToBeSampled];
57 int indexInSubset = 0;
58 for (int i = numOfElementsInGivenSet
59 - 1; i >= numOfElementsInGivenSet
60 - sizeOfSubsetToBeSampled; i--) {
61 int j = this.randomGenerator.nextInt(i + 1);
62 subsetInByteFormat[indexInSubset] = givenSet[availableIndicesToChooseFrom[j]];
63 indexInSubset++;
64 availableIndicesToChooseFrom[j] = availableIndicesToChooseFrom[i];
65 }
66 return subsetInByteFormat;
67 }
68 }
69
70 // ******************************************************************************************************
71
72 /**
73 * Returns random subset of size: "sizeOfSubsetToBeSampled", taken from the
74 * set: "givenSet"
75 */
76 public int getSubsetInBitFormat() {
77 if (this.subsetEqualsGivenSet) {
78 int copyOfGivenSet = 0;
79 for (int i = 0; i < numOfElementsInGivenSet; i++) {
80 // Add agent "givenSet[i]" to "copyOfGivenSet"
81 copyOfGivenSet += 1 << (givenSet[i] - 1);
82 }
83 return copyOfGivenSet;
84 } else {
85 // this.randomGenerator = new Random();
86 for (int i = numOfElementsInGivenSet - 1; i >= 0; i--) {
87 availableIndicesToChooseFrom[i] = i;
88 }
89 int subsetInBitFormat = 0;
90 int indexInSubset = 0;
91 for (int i = numOfElementsInGivenSet
92 - 1; i >= numOfElementsInGivenSet
93 - sizeOfSubsetToBeSampled; i--) {
94 int j = this.randomGenerator.nextInt(i + 1);
95 // Add agent "givenSet[ availableIndicesToChooseFrom[ j ] ]" to
96 // "subsetInBitFormat"
97 subsetInBitFormat += 1 << (givenSet[availableIndicesToChooseFrom[j]]
98 - 1);
99 indexInSubset++;
100 availableIndicesToChooseFrom[j] = availableIndicesToChooseFrom[i];
101 }
102 return subsetInBitFormat;
103 }
104 }
105}
Note: See TracBrowser for help on using the repository browser.