Powered By Blogger

Friday, August 24, 2012

How Can We Get the Power Set of a Set by Using of Excel?


The purpose of this article is to obtain all subsets of a set by using of Classical or Fuzzy set theory and a VB code written by Myrna Larson. The power set has many applications in the fields of engineering such as control valves in piping and process engineering, control keys in electrical engineering, field data analysis in geophysics (does not different among the arrangements of Wenner, Schlumberger or Polygon - Net in resistivity test or Geoelectric investigation) and data analysis of NDT tests.
And so we can find the applications of power set in financial management, strategic management and risk management. For instance, I would like to refer you to my article of “EMFPS: Efficient Portfolio of Assets (The Optimization for Risk, Return and Probability)” posted on link http://emfps.blogspot.com/2011/10/emfps-efficient-portfolio-of-assets.html where I stated: “....To change Rp(i) and probabilities simultaneously into above limited range, we should obtain all Permutations without Repetition by using of VB codes in excel...”
The concept of a set is the collection of the numbers or elements where we consider capital letters for them such as A, B, C,...
For example, set A has 4 elements as follows:
A = {1,2,3,4}
If all elements of set B are included in set A, we can say that set B is a subset of set A.
If B = {2,4}, set B is a subset of set A.
The power set of set A is a set such as C which is contained all subsets of set A even empty set and set A as follows:
C = {{}, {1}, {2}, {3}, {4}, {1,2}, {1,3}, {1,4}, {2,3}, {2,4}, {3,4}, {1,2,3}, {1,2,4}, {1,3,4}, {2,3,4}, {1,2,3,4}}
As we see, the total elements of set C is equal to (2^n) where “n” is all elements of set A.
How can we get the power set of a set by using of excel?
Methodology
Assume that set A is a subset of Universe set U. In the reference with Characteristic function (membership function) in set theory which is the base of Fuzzy set theory, if each element of set A is a member of set U, we will assign 1 as membership function otherwise 0 (zero) as follows:








Where:  x is member of set U.
Example:
If we have:  A = {1,,3,7,9} and   U = {1,2,3,4,5,6,7,8,9}
By using of membership function, we can write set A as follows:
A = {(1,1), (2,0), (3,1), (4,0), (5,0), (6,0), (7,1),(8,0), (9,1)}
According to above mentioned, there is very important rule that we should consider when we want to use from excel. This note is below cited:
Rule: If we have not set U, we have to consider 1 or 0 for each element of set A.
Let me start step by step this method by using of excel:
Ø  Type “c” into cell A1 in your spreadsheet
Ø  Type into cell A2 the number of elements in your set
Ø   Regarding to above rule, for each element of your set, you should type 0 and 1 respectively into cell A3, A4, A5, A6 and so on. For example, if your set has 5 elements, you should fill cells as follows:

A1 = c
A2 = 5
A3 = 0
A4 = 1
A5 = 0
A6 = 1
A7 = 0
A8 = 1
A9 = 0
A10 = 1
A11 = 0
A12 = 1



Ø  Now, we can run macro from VB code written by Myrna Larson which is as follows:
Option Explicit

 'Written by Myrna Larson - Microsoft Excel MVP
Dim vAllItems As Variant
Dim Buffer() As String
Dim BufferPtr As Long
Dim Results As Worksheet


Sub ListPermutations()
    Dim Rng As Range
    Dim PopSize As Integer
    Dim SetSize As Integer
    Dim Which As String
    Dim N As Double
    Const BufferSize As Long = 4096
    
    
    Set Rng = Range(Range("A1"), Cells(Rows.Count, "A").End(xlUp))
    
    PopSize = Rng.Cells.Count - 1
    If PopSize < 1 Then Goto DataError
    
    SetSize = Rng.Cells(2).Value
    If SetSize > PopSize Then Goto DataError
    
    Which = UCaseS(Rng.Cells(1).Value)
    Select Case Which
    Case "C"
        N = Application.WorksheetFunction.Combin(PopSize, SetSize)
    Case "P"
        N = Application.WorksheetFunction.Permut(PopSize, SetSize)
    Case Else
        Goto DataError
    End Select
    If N > Cells.CountLarge Then Goto DataError 
    
    Application.ScreenUpdating = False
    
    Set Results = Worksheets.Add
    
    vAllItems = Rng.Offset(2, 0).Resize(PopSize).Value
    Redim Buffer(1 To BufferSize) As String
    BufferPtr = 0
    
    If Which = "C" Then
        AddCombination PopSize, SetSize
    Else
        AddPermutation PopSize, SetSize
    End If
    vAllItems = 0
    
    Application.ScreenUpdating = True
    Exit Sub
    
DataError:
    If N = 0 Then
        Which = "Enter your data in a vertical range of at least 4 cells. " _
        & String$(2, 10) _
        & "Top cell must contain the letter C or P, 2nd cell is the number " _
        & "of items in a subset, the cells below are the values from which " _
        & "the subset is to be chosen."
        
    Else
        Which = "This requires " & Format$(N, "#,##0") & _
        " cells, more than are available on the worksheet!"
    End If
    MsgBox Which, vbOKOnly, "DATA ERROR"
    Exit Sub
End Sub

Private Sub AddPermutation(Optional PopSize As Integer = 0, _
    Optional SetSize As Integer = 0, _
    Optional NextMember As Integer = 0)
    
    Static iPopSize As Integer
    Static iSetSize As Integer
    Static SetMembers() As Integer
    Static Used() As Integer
    Dim i As Integer
    
    If PopSize <> 0 Then
        iPopSize = PopSize
        iSetSize = SetSize
        Redim SetMembers(1 To iSetSize) As Integer
        Redim Used(1 To iPopSize) As Integer
        NextMember = 1
    End If
    
    For i = 1 To iPopSize
        If Used(i) = 0 Then
            SetMembers(NextMember) = i
            If NextMember <> iSetSize Then
                Used(i) = True
                AddPermutation , , NextMember + 1
                Used(i) = False
            Else
                SavePermutation SetMembers()
            End If
        End If
    Next i
    
    If NextMember = 1 Then
        SavePermutation SetMembers(), True
        Erase SetMembers
        Erase Used
    End If
    
End Sub 'AddPermutation

Private Sub AddCombination(Optional PopSize As Integer = 0, _
    Optional SetSize As Integer = 0, _
    Optional NextMember As Integer = 0, _
    Optional NextItem As Integer = 0)
    
    Static iPopSize As Integer
    Static iSetSize As Integer
    Static SetMembers() As Integer
    Dim i As Integer
    
    If PopSize <> 0 Then
        iPopSize = PopSize
        iSetSize = SetSize
        Redim SetMembers(1 To iSetSize) As Integer
        NextMember = 1
        NextItem = 1
    End If
    
    For i = NextItem To iPopSize
        SetMembers(NextMember) = i
        If NextMember <> iSetSize Then
            AddCombination , , NextMember + 1, i + 1
        Else
            SavePermutation SetMembers()
        End If
    Next i
    
    If NextMember = 1 Then
        SavePermutation SetMembers(), True
        Erase SetMembers
    End If
    
End Sub 'AddCombination

Private Sub SavePermutation(ItemsChosen() As Integer, _
    Optional FlushBuffer As Boolean = False)
    
    Dim i As Integer, sValue As String
    Static RowNum As Long, ColNum As Long
    
    If RowNum = 0 Then RowNum = 1
    If ColNum = 0 Then ColNum = 1
    
    If FlushBuffer = True Or BufferPtr = UBound(Buffer()) Then
        If BufferPtr > 0 Then
            If (RowNum + BufferPtr - 1) > Rows.Count Then
                RowNum = 0
                ColNum = ColNum + 1
                If ColNum > 256 Then Exit Sub
            End If
            
            Results.Cells(RowNum, ColNum).Resize(BufferPtr, 1).Value _
            = Application.WorksheetFunction.Transpose(Buffer())
            RowNum = RowNum + BufferPtr
        End If
        
        BufferPtr = 0
        If FlushBuffer = True Then
            Erase Buffer
            RowNum = 1
            ColNum = 0
            Exit Sub
        Else
            Redim Buffer(1 To UBound(Buffer))
        End If
        
    End If
    
     'construct the next set
    For i = 1 To UBound(ItemsChosen)
        sValue = sValue & ", " & vAllItems(ItemsChosen(i), 1)
    Next i
    
     'and save it in the buffer
    BufferPtr = BufferPtr + 1
    Buffer(BufferPtr) = Mid$(sValue, 3)
End Sub 'SavePermutation


Ø  Since we have some repeated cells, we should use Data Tab – Data Tools – Remove Duplicates
Ø  Due to each subsets is into one cell, to split the elements of subsets into other cells, we should utilize Data Tab – Data Tools – Text to Columns
Ø  If the elements of your set are the numbers, you can multiply all subsets of 0 and 1 to numbers to return codes to numbers.
Ø  If the elements of your set are letters (no numbers), you can use from above step plus Vlookup formula in excel.
Here, I have brought an example as follows:
If we have set A = {a,b,c,d,e,f,}, the power set will generated as follows:
c
6
0
1
0
1
0
1
0
1
0
1
0
1










































As we can see, the power set of set A has 64 sets (2^6).
Each row (subset) can be considered as a scenario to analyze data.
If you want to analyze your data by using What – If – Analysis Tool in excel, Scenario analysis is limited to 32 rows. Therefore, the best tool is Solver – add.
To be continued….




Note:  “All spreadsheets and calculation notes are available. The people, who are interested in having my spreadsheets of this method as a template for further practice, do not hesitate to ask me by sending an email to: soleimani_gh@hotmail.com or call me on my cellphone: +989109250225. Please be informed these spreadsheets are not free of charge.”
  

No comments:

Post a Comment