complexity_encoding.py
def encode_single_cardinality(self, bound, variables: OrderedDict):
        """Enforces 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

        variables = replicate(variables)
        jlim = len(variables)
        ctr = [[self._add_var() for _ in range(0, min(j+1, bound))] for j in range(jlim)]

        for j in range(1, jlim):
            # never decrements
            for l in range(0, len(ctr[j-1])):
                self._add_clause(-ctr[j-1][l], ctr[j][l])

            # increment if variable and ctr
            for l in range(1, len(ctr[j])):
                self._add_clause(-variables[j], -ctr[j-1][l-1], ctr[j][l])

        # initialize first counter if corr variable is true
        for j in range(jlim):
            self._add_clause(-variables[j], ctr[j][0])

        # conflict if target exceeded
        for j in range(bound, jlim):
            self._add_clause(-variables[j], -ctr[j-1][bound-1])

        if self.debug:
            for j in range(jlim):
                outer_var = self.current_cardinality_outer_var
                inner_var = self.current_cardinality_inner_vars[j]
                self.cardinality_counter_vars.append((outer_var, inner_var, ctr[j]))
Enforces cardinality constraint on list of variables (repeats allowed)
Enforces cardinality constraint on list of variables (repeats allowed)
Enforces cardinality constraint on list of variables ( repeats allowed )
def encode_single_cardinality(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
        variables = replicate(variables)
        jlim = len(variables)
        ctr = [[self._add_var() for _ in range(0, min(j+1, bound))] for j in range(jlim)]
        for j in range(1, jlim):
            for l in range(0, len(ctr[j-1])):
                self._add_clause(-ctr[j-1][l], ctr[j][l])
            for l in range(1, len(ctr[j])):
                self._add_clause(-variables[j], -ctr[j-1][l-1], ctr[j][l])
        for j in range(jlim):
            self._add_clause(-variables[j], ctr[j][0])
        for j in range(bound, jlim):
            self._add_clause(-variables[j], -ctr[j-1][bound-1])
        if self.debug:
            for j in range(jlim):
                outer_var = self.current_cardinality_outer_var
                inner_var = self.current_cardinality_inner_vars[j]
                self.cardinality_counter_vars.append((outer_var, inner_var, ctr[j]))
def encode_single_cardinality ( 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 variables = replicate ( variables ) jlim = len ( variables ) ctr = [ [ self . _add_var ( ) for _ in range ( 0 , min ( j + 1 , bound ) ) ] for j in range ( jlim ) ] for j in range ( 1 , jlim ) : for l in range ( 0 , len ( ctr [ j - 1 ] ) ) : self . _add_clause ( - ctr [ j - 1 ] [ l ] , ctr [ j ] [ l ] ) for l in range ( 1 , len ( ctr [ j ] ) ) : self . _add_clause ( - variables [ j ] , - ctr [ j - 1 ] [ l - 1 ] , ctr [ j ] [ l ] ) for j in range ( jlim ) : self . _add_clause ( - variables [ j ] , ctr [ j ] [ 0 ] ) for j in range ( bound , jlim ) : self . _add_clause ( - variables [ j ] , - ctr [ j - 1 ] [ bound - 1 ] ) if self . debug : for j in range ( jlim ) : outer_var = self . current_cardinality_outer_var inner_var = self . current_cardinality_inner_vars [ j ] self . cardinality_counter_vars . append ( ( outer_var , inner_var , ctr [ j ] ) )
Enforces cardinality constraint on list of variables (repeats allowed)
Enforces cardinality constraint on list of variables ( repeats allowed )
"""Enforces cardinality constraint on list of variables (repeats allowed)""" # never decrements # increment if variable and ctr # initialize first counter if corr variable is true # conflict if target exceeded
self
bound
variables
OrderedDict