【问题标题】:Solve a system of boolean equations in Matlab在 Matlab 中求解一个布尔方程组
【发布时间】:2019-02-18 14:37:52
【问题描述】:

我有一组布尔方程,即

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] 作为解。

【问题讨论】:

    标签: matlab boolean boolean-logic equation-solving


    【解决方案1】:

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

    如果您确实有很多变量,此方法将停止运行,因为它使用大小为K*(2^K) 的矩阵,其中K 是变量的数量,而combvec 对于较大的K 也会变得相当慢。

    虽然您必须注意变量的数量,但这种方法非常有能力以很少的开销处理许多逻辑“方程式”。


    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获取所有组合。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多