//------------------------------------------------------------------------------------------------ /// <summary> /// <para>Static method to generate the power set (the set of all possible subsets) for given number of /// possible states/worlds defined in the frame of discernment.</para> /// <para>WARNING: DO NOT CALL THIS METHOD WITH A NUMBER OF ATOMS TOO BIG OR IT WILL HARM YOUR POOR /// COMPUTER.</para> /// </summary> /// <param name="numberOfAtoms">The number of possible states/worlds in the frame of /// discernment.</param> /// <returns>Returns a new discrete set containing all the possible discrete elements of the /// size "numberOfAtoms". This corresponds to the power set.</returns> /// <exception cref="ArgumentOutOfRangeException">Thrown if the number of atoms required is /// null or negative.</exception> public static DiscreteSet GeneratePowerSet(int numberOfAtoms) { //Checking argument: if (numberOfAtoms <= 0) { throw new ArgumentOutOfRangeException("numberOfAtoms. The number of atoms cannot be null or negative!"); } //Init: DiscreteSet s = new DiscreteSet(); uint[] numbers = new uint[numberOfAtoms / DiscreteElement.NB_BITS_UINT + 1]; uint limit = (1U << (numberOfAtoms % DiscreteElement.NB_BITS_UINT)) - 1; for (int i = 0; i < numbers.Length; i++) { numbers[0] = 0; } s.Add(new DiscreteElement(numberOfAtoms, numbers)); //Generate: while (numbers[numbers.Length - 1] < limit) { int i = 0; while (numbers[i] == uint.MaxValue) { numbers[i] = 0; i++; } numbers[i]++; s.Add(new DiscreteElement(numberOfAtoms, numbers)); } return(s); }
//------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------ /* * Static methods */ #region Static utility methods /// <summary> /// Static method to generate the set of atoms given a certain number of atoms (the number of /// possible states/worlds defined in the frame of discernment). The generated DiscreteElements /// will have a size of the number of atoms required. /// </summary> /// <param name="numberOfAtoms">The number of atoms to generate.</param> /// <returns>Returns a new discrete set containing all the atoms for elements of the size /// "numberOfAtoms".</returns> /// <exception cref="ArgumentOutOfRangeException">Thrown if the number of atoms required is /// null of negative.</exception> public static DiscreteSet GenerateSetOfAtoms(int numberOfAtoms) { //Checking argument: if (numberOfAtoms <= 0) { throw new ArgumentOutOfRangeException("numberOfAtoms. The number of atoms cannot be null or negative!"); } //Init: DiscreteSet s = new DiscreteSet(); uint number = 0; uint[] numbers = new uint[numberOfAtoms / DiscreteElement.NB_BITS_UINT + 1]; for (int i = 0; i < numbers.Length; i++) { numbers[i] = 0; } //Generate Elements: for (int i = 0; i < numberOfAtoms; i++) { number = 1U << (i % DiscreteElement.NB_BITS_UINT); numbers[i / DiscreteElement.NB_BITS_UINT] = number; s.Add(new DiscreteElement(numberOfAtoms, numbers)); numbers[i / DiscreteElement.NB_BITS_UINT] = 0; } return(s); }
//------------------------------------------------------------------------------------------------ /// <summary> /// <para>Static method to generate the set of all subsets of the given discrete element.</para> /// <para>WARNING: DO NOT CALL THIS METHOD WITH AN ELEMENT TOO BIG (A HIGH NUMBER OF POSSIBLE /// STATES/WORLD) OR IT WILL HARM YOUR POOR COMPUTER.</para> /// </summary> /// <param name="e">The element to generate the set from.</param> /// <returns>Returns a new discrete set containing all the subsets of the given element.</returns> public static DiscreteSet GenerateSetOfSubsets(DiscreteElement e) { DiscreteSet toReturn = new DiscreteSet(); DiscreteElementEnumerator enumerator = new DiscreteElementEnumerator(e.Size); foreach (DiscreteElement el in enumerator) { if (el.IsASubset(e)) { toReturn.Add(el); } } return(toReturn); }
//------------------------------------------------------------------------------------------------ /// <summary> /// <para>Static method to generate a partial power set containing all the possible discrete elements /// of a given size (nbOfAtoms) with a specified maximal cardinal.</para> /// <para>WARNING: DO NOT CALL THIS METHOD WITH A NUMBER OF ATOMS TOO BIG OR IT WILL HARM YOUR POOR /// COMPUTER.</para> /// </summary> /// <param name="numberOfAtoms">The number of possible states/worlds defined in the frame /// of discernment.</param> /// <param name="maxCard">The maximal cardinal for the created elements.</param> /// <returns>Returns a new set </returns> public static DiscreteSet GeneratePartialPowerSet(int numberOfAtoms, int maxCard) { DiscreteSet s = new DiscreteSet(); DiscreteElementEnumerator enumerator = new DiscreteElementEnumerator(numberOfAtoms); foreach (DiscreteElement e in enumerator) { if (e.Card <= maxCard) { s.Add(e); } } return(s); }