在 Matlab 中求解布尔方程组

Solve a system of boolean equations in Matlab

我有一组布尔方程,即

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

以及每个输出 vari 应等于 1 的约束。

我想要满足这些等式的输入变量 (x, y ,z ...) 的每个对应组合。

例如,前两个方程允许 [x y z] = [1 1 0] or [1 1 1] 作为解。

如果你没有太多变量,你可以很容易地做到这一点。

如果你有很多变量,这个方法就会停止,因为它使用了一个大小为 K*(2^K) 的矩阵,其中 K 是变量的数量,combvec 变得很漂亮大 K 也很慢。

虽然您必须对变量的数量保持警惕,但此方法能够以很少的开销处理许多逻辑 'equations'。


xyz例子中:

% 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

写得比较笼统,可能是这样的:

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)) );

注意:如果您没有神经网络工具箱,您将不会拥有 combvec。得到所有的组合有many alternatives