1

I have a set of boolean equations, i.e.

var1 = x AND y
var2 = x OR z
var3 = ...
var4 = ...

And the constraint that every output vari should equal 1.

I want every corresponding combination of input variables (x, y ,z ...) which satisfies these equations.

For example, the first two equations would allow [x y z] = [1 1 0] or [1 1 1] as solutions.

Wolfie
  • 27,562
  • 7
  • 28
  • 55
EagleOne
  • 541
  • 1
  • 10
  • 28

1 Answers1

3

You can do this pretty easily if you don't have too many variables.

This method will stall if you do have many variables, because it uses use a matrix of size K*(2^K), where K is the number of variables, and combvec gets pretty slow for large K too.

Whilst you have to be wary of the number of variables, this method is pretty capable of handling many logical 'equations' with little overhead.


In the x, y, z example:

% Get all combinations of x/y/z, where each is true or false
opts = repmat( {[true, false]}, 1, 3 );
xyz = combvec( opts{:} )
% Assign each row to a named variable 
x = xyz(1,:); y = xyz(2,:); z = xyz(3,:);
% Get the combinations which satisfy your conditions
results = xyz( :, (x & y) & (x | z) );
% Each column of results is a solution
>> results
results = 
    1    1
    1    1
    1    0

Written more generally, it might look something like this:

K = 3; % K variables (previously x, y and z so K = 3)
% Create all true/false combinations
opts = repmat( {[true, false]}, 1, K );
combs = combvec( opts{:} );

% Shorthand so we can write in(i) not combs(i,:)
in = @(k) combs(k,:); 
% Apply conditions
results = combs( :, (in(1) & in(2)) ...
                  & (in(1) | in(3)) );

Note: if you don't have the Neural Network Toolbox, you won't have combvec. There are many alternatives for getting all the combinations.

Wolfie
  • 27,562
  • 7
  • 28
  • 55