complexity_encoding.py
def encode_single_cardinality_with_dd(self, bound, variables: OrderedDict):
        """
        Enforces weighted cardinality constraint on list of variables
        (repeats allowed)
        """
        if bound <= 0:
            if self.debug:
                print(f"warning: non-positive bound {bound} provided, forcing UNSAT")
            self._add_clause(-1)
            self._add_clause(1)
            return

        dd = make_decision_diagram(bound, variables)
        dd_lookup = {vertex: self._add_var() for vertex in dd.nodes}
        dd_reverse_lookup = {idx: vertex for (vertex, idx) in dd_lookup.items()}

        self._add_clause(-dd_lookup['NO'])  # if NO is ever true, this results in contradiction
        self._add_clause(dd_lookup[dd.graph['root']])  # initialize root node as true
        for u, v, case in dd.edges(data="case"):
            if v == 'YES':
                pass  # nothing to do
            else:
                varid, level = u
                if case == "if":
                    # clause: u & var => v
                    self._add_comment(f"({u} & arc_{varid}) -> {v}")
                    self._add_clause(-dd_lookup[u], -varid, dd_lookup[v])
                else:
                    # clause: u & !var => v
                    self._add_comment(f"({u} & !arc_{varid}) -> {v}")
                    self._add_clause(-dd_lookup[u], varid, dd_lookup[v])

        if self.debug:
            self.cardinality_counter_vars.append((self.current_cardinality_outer_var, dd, dd_lookup, dd_reverse_lookup))

        Enforces weighted cardinality constraint on list of variables
        (repeats allowed)
        
Enforces weighted cardinality constraint on list of variables
(repeats allowed)
Enforces weighted cardinality constraint on list of variables ( repeats allowed )
def encode_single_cardinality_with_dd(self, bound, variables: OrderedDict):
        if bound <= 0:
            if self.debug:
                print(f"warning: non-positive bound {bound} provided, forcing UNSAT")
            self._add_clause(-1)
            self._add_clause(1)
            return
        dd = make_decision_diagram(bound, variables)
        dd_lookup = {vertex: self._add_var() for vertex in dd.nodes}
        dd_reverse_lookup = {idx: vertex for (vertex, idx) in dd_lookup.items()}
        self._add_clause(-dd_lookup['NO'])  
        self._add_clause(dd_lookup[dd.graph['root']])  
        for u, v, case in dd.edges(data="case"):
            if v == 'YES':
                pass  
            else:
                varid, level = u
                if case == "if":
                    self._add_comment(f"({u} & arc_{varid}) -> {v}")
                    self._add_clause(-dd_lookup[u], -varid, dd_lookup[v])
                else:
                    self._add_comment(f"({u} & !arc_{varid}) -> {v}")
                    self._add_clause(-dd_lookup[u], varid, dd_lookup[v])
        if self.debug:
            self.cardinality_counter_vars.append((self.current_cardinality_outer_var, dd, dd_lookup, dd_reverse_lookup))
def encode_single_cardinality_with_dd ( self , bound , variables : OrderedDict ) : if bound <= 0 : if self . debug : print ( f"warning: non-positive bound {bound} provided, forcing UNSAT" ) self . _add_clause ( - 1 ) self . _add_clause ( 1 ) return dd = make_decision_diagram ( bound , variables ) dd_lookup = { vertex : self . _add_var ( ) for vertex in dd . nodes } dd_reverse_lookup = { idx : vertex for ( vertex , idx ) in dd_lookup . items ( ) } self . _add_clause ( - dd_lookup [ 'NO' ] ) self . _add_clause ( dd_lookup [ dd . graph [ 'root' ] ] ) for u , v , case in dd . edges ( data = "case" ) : if v == 'YES' : pass else : varid , level = u if case == "if" : self . _add_comment ( f"({u} & arc_{varid}) -> {v}" ) self . _add_clause ( - dd_lookup [ u ] , - varid , dd_lookup [ v ] ) else : self . _add_comment ( f"({u} & !arc_{varid}) -> {v}" ) self . _add_clause ( - dd_lookup [ u ] , varid , dd_lookup [ v ] ) if self . debug : self . cardinality_counter_vars . append ( ( self . current_cardinality_outer_var , dd , dd_lookup , dd_reverse_lookup ) )
Enforces weighted cardinality constraint on list of variables
(repeats allowed)
Enforces weighted cardinality constraint on list of variables ( repeats allowed )
"""
        Enforces weighted cardinality constraint on list of variables
        (repeats allowed)
        """ # if NO is ever true, this results in contradiction # initialize root node as true # nothing to do # clause: u & var => v # clause: u & !var => v
self
bound
variables
OrderedDict