tags:

views:

515

answers:

5

I have this situation that I need to let users define decisions based on the number of given conditions. For example my program needs to automatically generate a matrix as below given that there are two conditions (IsMale and IsSmoker):

IsMale:   YES YES NO  NO
IsSmoker: YES NO  YES NO

And the deicsion is defined by user, therefore any of the following can be valid:

IsMale:   YES YES NO  NO
IsSmoker: YES NO  YES NO
Decision: T   F   T   F

IsMale:   YES YES NO  NO
IsSmoker: YES NO  YES NO
Decision: F   F   F   F

IsMale:   YES YES NO  NO
IsSmoker: YES NO  YES NO
Decision: T   T   T   T

For each condition there can only be two states, True and False. So the total number of combinations are calculated as below:

no of possible states (S) to the power of no of conditions (C) S ^ C = total no of combinations

4 possibilities (2 ^ 2 = 4)

Condition A   T T F F
Condition B   T F T F

8 possibilities (2 ^ 3 = 8)

Condition A   T T T T F F F F
Condition B   T T F F T F T F
Condition C   T F T F T T F F

Hope I have explained myself a bit better than the original question.

Updated: according to the answer given by Guffa. Below is the hand calculation of his algorithm to generate the different combinations.

4 possibilities (2^2=4)

index = 0, (right shift 0)

binary   8 4 2 1  Value

original 0 0 0 1  1
& 1      0 0 0 1  1 T

original 0 0 1 0  2
& 1      0 0 0 1  0 F

original 0 0 1 1  3
& 1      0 0 0 1  1 T

original 0 1 0 0  4
& 1      0 0 0 1  0 F

index = 1, (right shift 1)

binary   8 4 2 1  Value
original 0 0 0 1  1
shift    0 0 0 0  0
& 1      0 0 0 1  0 F

original 0 0 1 0  2
shift    0 0 0 1  1
& 1      0 0 0 1  1 T

original 0 0 1 1  3
shift    0 0 0 1  1
& 1      0 0 0 1  1 T

original 0 1 0 0  4
shift    0 0 1 0  2
& 1      0 0 0 1  0 F

combinations:

Condition 1: TFTF
Condition 2: FTTF
A: 
I am not sure about the math term but yes "Oh wait! I think you're looking to generate all the different combinations of conditions!" this is what I want to do.
Jeffrey C
A: 

Unless I'm missing something you're missing something in your problem definition. You've defined the range of possible inputs, that's surely easy to generate?

Condition A   T T F F
Condition B   T F T F
Decision      T F F F

The output needs to be defined, it can't be deduced. As an example I've filled in an AND.

Seems like my glib "easy to generate" is the problem. Doesn't a recursive solution work?

for (members of getListOfCombinedStates(n) )
    print theMember

getListOfCombinedStates(int howMany) {
    if (  n == 1 )
        return  list of possible States
    else {
        create empty resultlist
        for ( members of getListofCombinedStates(howMany -1) )
           for ( members of listOfStates ) 
               create new CombinedState by suffixing state, add to resultList

        return resultList
     }

So for n=2 we call getListOfCombinedStates(2), which calls getListOfCombinedStates(1), and that returns { T, F }.

getListOfCombinedStates(2) then iterates {T, F } and adds first T and them F to each member yielding { T, T } and { T, F} and then {F, T } and {F, F}.

I hope it's clear how getListOfCombinedStates(3) would in turn call getListOfCombinedStates(2) and generate the required values.

djna
A: 

I'm not sure what you mean, but maybe this is what you are looking for:

I made some little adjustments, because your second example wasn't consistent with the first one; and negated the bits (I substituted F with 0, and T with 1), to show my point.

Condition A   0 0 0 0 1 1 1 1
Condition B   0 0 1 1 0 0 1 1
Condition C   0 1 0 1 0 1 0 1

Now, observe the pattern of each column, and think about binary numbers ;).

(I hope you get the idea.)

Tamás Szelei
+1  A: 

As djna has mentioned in his/hers answer, you are missing the output for the decision.

For instance, if you have a operator that takes two inputs (for example: and, or operators) you have to try it for all possible inputs. For a simple operator that is quite simple since there are only four possible inputs, but for more complex operators you will have to generate 2^n possible inputs to calculate all possible outputs.

I suggest doing this in a array of n boolean variables, where you flip bits to get 2^n possible inputs, then testing your operator with the generated input array and printing the result.

One simple way of generating the array is to create a loop in which you increment a variable from 0 to 2^n - 1 and then converting the number to binary. You will get something like this: (for n = 3):

0: 0 0 0
1: 0 0 1
2: 0 1 0
3: 0 1 1
4: 1 0 0
5: 1 0 1
6: 1 1 0
7: 1 1 1

Hope this helps!

Rekreativc
+1  A: 

Outputting the matrix is rather trivial:

int conditions = 3;
for (int c = 0; c < conditions; c++) {
    Console.WriteLine(
       "Condition {0} : {1}",
       (char)('A' + c),
       new String(
          Enumerable.Range(0, (1 << conditions))
          .Select(n => "TF"[(n >> c) & 1])
          .ToArray()
       )
    );
}

So, what do you want to do with it?

Guffa
stackoverflow is really useful after 8 hours of programming that some one can jump to the rescue. Thanks. This is exactly what I want.
Jeffrey C
i think there's a bug in your code, you shoud remove - 1 from (1 << conditions) - 1
Jeffrey C
Yes, you are right. The second parameter of the Range method is a count, not an end index.
Guffa
Jeffrey C
Jeffrey C
Jeffrey C
The shift operation places a specific bit at position zero by shifting out the lower bits, then the and operation isolates that bit by clearing all other bits. The resulting value is 0 or 1, which is used to pick the first or second character from the string "TF".
Guffa