//------------------------------------------------------------------------------------------------ /// <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> /// Creates the union of the current discrete set with the given one. Does not modify the current discrete /// set, returns a new one instead. Strictly identical to <see cref="Union"/>. /// </summary> /// <param name="s">The discrete set to create the union with.</param> /// <returns>Returns a new discrete set which is the union/disjunction of the current discrete set with /// the given one.</returns> /// <exception cref="IncompatibleDiscreteElementSizeException">Thrown if the two sets contain elements /// that are defined on different frames of discernment.</exception> public DiscreteSet Disjunction(DiscreteSet s) { try { return((DiscreteSet)(base.Disjunction(s))); } catch (IncompatibleSetException) { throw new IncompatibleDiscreteElementSizeException("The Elements of two DiscreteSets should be of the same size to perform the disjunction!"); } }
//------------------------------------------------------------------------------------------------ /// <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); }
/// <summary> /// Compares the current discrete set with the given one. /// </summary> /// <param name="s">The set to compare to.</param> /// <returns>Returns true if both sets contain the exact same elements, false otherwise.</returns> public bool Equals(DiscreteSet s) { //Checking: if (s == null) { return(false); } if (Card != s.Card || !this.IsCompatible(s)) { return(false); } //Compare: return(this.IsASubset(s) && s.IsASubset(this)); }
//------------------------------------------------------------------------------------------------ /// <summary> /// <para>Returns the focal elements that have the minimum value for the given criterion and which respect /// the maximum cardinal constraint given. It compromises between the criterion and the maximum cardinality. /// For more details, refer to "M. Dominici et al., Experiences in managing uncertainty and ignorance in a /// lightly instrumented smart home, 2012".</para> /// <para>DISCLAIMER: This function may be completely useless but who knows?!</para> /// <para>Calls AMassFunction.GetMin() with the power set as parameter for the set.</para> /// <para>WARNING: DO NOT CALL THIS METHOD WITH A MASS FUNCTIONS DEFINED ON A FRAME OF DISCERNMENT THAT /// IS TOO BIG OR IT WILL HARM YOUR POOR COMPUTER.</para> /// </summary> /// <param name="f">The criterion function (typically M, Bel, BetP, Pl or Q).</param> /// <param name="maxCard">The maximum cardinality the returned maxima may have.</param> /// <returns>Returns the list of found maxima as a list of FocalElements.</returns> /// <exception cref="ArgumentOutOfRangeException">Thrown if the maxCard argument is null or negative.</exception> /// <exception cref="EmptyMassFunctionException">Thrown if the mass function does not contain any focal element.</exception> /// <exception cref="IncompatibleSetException">Thrown if the given set is incompatible with the focal elements /// of the current mass function.</exception> public List <FocalElement <DiscreteElement> > GetMin(CriterionFunction f, int maxCard) { return(this.GetMin(f, maxCard, DiscreteSet.GeneratePowerSet(NbPossibleWorlds))); }
//------------------------------------------------------------------------------------------------ /// <summary> /// Builds a new discrete set which is a deep copy of the given one. /// </summary> /// <param name="s">The discrete set to copy.</param> public DiscreteSet(DiscreteSet s) : this(s.Elements.ToArray()) { }
//------------------------------------------------------------------------------------------------ /// <summary> /// Creates the union of the current discrete set with the given one. Does not modify the current discrete /// set, returns a new one instead. Strictly identical to <see cref="Disjunction"/>. /// </summary> /// <param name="s">The discrete set to create the union with.</param> /// <returns>Returns a new discrete set which is the union/disjunction of the current discrete set with /// the given one.</returns> /// <exception cref="IncompatibleDiscreteElementSizeException">Thrown if the two sets contain elements /// that are defined on different frames of discernment.</exception> public DiscreteSet Union(DiscreteSet s) { return(this.Disjunction(s)); }
//------------------------------------------------------------------------------------------------ /// <summary> /// Intersects the current discrete set with the given one. Does not modify the current set, returns a new /// one instead. Strictly identical to <see cref="Conjunction"/>. /// </summary> /// <param name="s">The discrete set to intersect the current one with.</param> /// <returns>Returns a new discrete set which is the intersection/conjunction of the current discrete set /// with the given one.</returns> /// <exception cref="IncompatibleDiscreteElementSizeException">Thrown if the two sets contain elements /// that are defined on different frames of discernment.</exception> public DiscreteSet Intersection(DiscreteSet s) { return(this.Conjunction(s)); }