Results 1 to 5 of 5

Thread: [RESOLVED] Full Enumeration of a Boolean Array

  1. #1

    Thread Starter
    New Member M_Saeidi's Avatar
    Join Date
    May 2008
    Location
    Iran, Tehran
    Posts
    6

    Resolved [RESOLVED] Full Enumeration of a Boolean Array

    Hi to all.

    I’m completely new here. I appreciate your assistances through my question.

    I have a Boolean Array like this:
    Dim blnarX(1 to 100) as Boolean
    Assume that every member of this array is a project that could be selected or not. In addition, these 100 members are just predefined spaces (i.e. just user knows how many projects are there). My code gets the number of projects from the user in a variable like intNumberOfProjects which is an integer smaller than 101. Since each project could be selected or not, there will be 2 ^ intNumberOfProjects different alternatives to select these projects. How can I enumerate all these alternatives by means of blnarX? For example if I (as a code writer) knew that my program should always enumerate just 3 projects, I used something like code below:

    Code:
    blnarX(1) = False
    blnarX(2) = False
    blnarX(3) = False
    For intCtrI = 1 To 2
        For intCtrJ = 1 To 2
            For intCtrK = 1 To 2
                ' Main part of code comes here which evaluates the alternative.
                ' This part of code is performed for 2 ^ 3 (=8) times, which I call
                ' it full enumeration of projects. For Example, in the first iteration no
                ' project is selected and in the last iteration all projects are selected.
                blnarX(3) = not blnarX(3)
            Next intCtrK
            blnarX(2) = not blnarX(2)
        Next intCtrJ
        blnarX(1) = Not blnarX(1)
    Next intCtrI
    However, I don’t know how many projects the user has. Furthermore, it’s not rational to write four lines for each project. What is the solution? We also have intNumberOfProjects. I want to generalize code above.

    Best Regards,
    Mohammad

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Full Enumeration of a Boolean Array

    I cannot understand what you want to achieve.

    With your code above, with 3 For loops from 1 to 2, that is an even number of times each line blnarX(i) = not blnarX(i) is run, you will go back to original value of blnarX(i). Nothing has been changed.

    Instead of Dim blnarX(1 to 100) as Boolean
    you can use dynamic array:
    Code:
    Dim blnarX() as Boolean
    '-- and later after get input from user with intNumberOfProjects >= 1:
    Redim blnarX(1 to intNumberOfProjects)
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  3. #3

    Thread Starter
    New Member M_Saeidi's Avatar
    Join Date
    May 2008
    Location
    Iran, Tehran
    Posts
    6

    Re: Full Enumeration of a Boolean Array

    Quote Originally Posted by anhn
    I cannot understand what you want to achieve.

    With your code above, with 3 For loops from 1 to 2, that is an even number of times each line blnarX(i) = not blnarX(i) is run, you will go back to original value of blnarX(i). Nothing has been changed.

    Instead of Dim blnarX(1 to 100) as Boolean
    you can use dynamic array:
    Code:
    Dim blnarX() as Boolean
    '-- and later after get input from user with intNumberOfProjects >= 1:
    Redim blnarX(1 to intNumberOfProjects)

    Thank you anhn. I didn't know Dynamic arrays. However, my main question has remained.

    It's not important that blnar(i) goes back to it's original amount. I don't want them. I have an Integer Programing like this:

    Max Z = a*A + b*B + c*C + ... + n* N

    s.t.:
    (1) A, B, C, ... , N = 0 or 1
    (2) alpha*A + beta*B + ... + nue*N <= Budget


    The easiest way (and of course, the most time-consuming way) for solving such a program is Full Enumeration of alternatives. In the main part of the code, first, I should check if the cost of the selected projects is possible (regarding the budget constraint) and if yes, I calculate amount of goal function Z (at the same place, i.e. main part of code). Then each time I compare the Z with a variable named sngMaxValue. If Z > sngMaxValue then (1:sngMaxValue = Z , and 2: store combination of selected projects in another variable), else (Do Nothing). After finishing all For loops, I have the best combination of the projects which gives the greatest Z. How can I escape from writing all these Fors? Please help me.

    Regards,
    Mohammad
    Last edited by M_Saeidi; Sep 2nd, 2008 at 11:21 AM.

  4. #4
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Full Enumeration of a Boolean Array

    Quote Originally Posted by M_Saeidi
    The easiest way (and of course, the most time-consuming way) for solving such a program is Full Enumeration of alternatives.
    Not possible. 2^100 possible values yields north of 1.2 nonillion permutations. If you could process a trillion per second -- which a supercomputer probably couldn't -- it would take something like 40 billion years to finish.

  5. #5

    Thread Starter
    New Member M_Saeidi's Avatar
    Join Date
    May 2008
    Location
    Iran, Tehran
    Posts
    6

    Re: [RESOLVED] Full Enumeration of a Boolean Array

    Thank you Ellis Dee for your useful post.
    You are absolutely right. I think I should use some methods of integer programming like Branch and Bound, or cutting planes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width