def _process_expr(self):
        self._num_vars = self._expr.degree
        ast = self._expr.to_ast() if self._expr.is_cnf() else self._expr.to_cnf().to_ast()
        ast = LogicalExpressionOracle._normalize_literal_indices(ast, self._expr.usupport)

        if self._optimization == 'off':
            if ast[0] == 'or':
                self._nf = DNF(ast, num_vars=self._num_vars)
            else:
                self._nf = CNF(ast, num_vars=self._num_vars)
        else:  # self._optimization == 'espresso':
            expr_dnf = self._expr.to_dnf()
            if expr_dnf.is_zero() or expr_dnf.is_one():
                self._nf = CNF(('const', 0 if expr_dnf.is_zero() else 1), num_vars=self._num_vars)
            else:
                expr_dnf_m = espresso_exprs(expr_dnf)[0]
                expr_dnf_m_ast = LogicalExpressionOracle._normalize_literal_indices(
                    expr_dnf_m.to_ast(), expr_dnf_m.usupport
                )
                if isinstance(expr_dnf_m, AndOp) or isinstance(expr_dnf_m, Variable):
                    self._nf = CNF(expr_dnf_m_ast, num_vars=self._num_vars)
                elif isinstance(expr_dnf_m, OrOp):
                    self._nf = DNF(expr_dnf_m_ast, num_vars=self._num_vars)
                else:
                    raise AquaError('Unexpected espresso optimization result expr: {}'.format(expr_dnf_m))
    def _process_expr(self):
        self._num_vars = len(self._expr.binary_symbols)
        self._lit_to_var = [None] + sorted(self._expr.binary_symbols, key=str)
        self._var_to_lit = dict(zip(self._lit_to_var[1:], range(1, self._num_vars + 1)))
        cnf = to_cnf(self._expr, simplify=self._optimization)

        if isinstance(cnf, BooleanTrue):
            ast = 'const', 1
        elif isinstance(cnf, BooleanFalse):
            ast = 'const', 0
        else:
            ast = get_ast(self._var_to_lit, cnf)

        if ast[0] == 'or':
            self._nf = DNF(ast, num_vars=self._num_vars)
        else:
            self._nf = CNF(ast, num_vars=self._num_vars)
Example #3
0
    def _process_expr(self):
        self._num_vars = len(self._expr.binary_symbols)
        self._lit_to_var = [None] + sorted(self._expr.binary_symbols, key=str)
        self._var_to_lit = dict(
            zip(self._lit_to_var[1:], range(1, self._num_vars + 1)))

        if self._optimization or (not is_cnf(self._expr)
                                  and not is_dnf(self._expr)):
            expr = simplify_logic(self._expr)
        else:
            expr = self._expr

        if isinstance(expr, BooleanTrue):
            ast = 'const', 1
        elif isinstance(expr, BooleanFalse):
            ast = 'const', 0
        else:
            ast = get_ast(self._var_to_lit, expr)

        if ast[0] == 'or':
            self._nf = DNF(ast, num_vars=self._num_vars)
        else:
            self._nf = CNF(ast, num_vars=self._num_vars)