Module logicng

Class CCEncoder


  • public class CCEncoder
    extends Object
    An encoder for cardinality constraints.

    An encoder is configured with a CCConfig configuration. There are two possible ways:

    1. Initialize the encoder with a given configuration in the constructor. Then this configuration will be bound to the encoder for its whole lifetime.
    2. Initialize the encoder only with a FormulaFactory. Then each encoding will be performed with the current cardinality constraint encoder configuration of the factory or the default configuration if the factory has no associated cardinality constraint encoder configuration. If you change the configuration in the factory, all encoders constructed for this factory will be affected.
    Since:
    1.1
    Version:
    2.3.0
    • Constructor Detail

      • CCEncoder

        public CCEncoder​(FormulaFactory f,
                         CCConfig config)
        Constructs a new cardinality constraint encoder with a given configuration.
        Parameters:
        f - the formula factory
        config - the configuration
      • CCEncoder

        public CCEncoder​(FormulaFactory f)
        Constructs a new cardinality constraint encoder which uses the configuration of the formula factory.
        Parameters:
        f - the formula factory
    • Method Detail

      • encode

        public List<Formula> encode​(CardinalityConstraint cc)
        Encodes a cardinality constraint and returns its CNF encoding.
        Parameters:
        cc - the cardinality constraint
        Returns:
        the CNF encoding of the cardinality constraint
      • encode

        public void encode​(CardinalityConstraint cc,
                           EncodingResult result)
        Encodes a cardinality constraint in a given result.
        Parameters:
        cc - the cardinality constraint
        result - the result of the encoding
      • encodeIncremental

        public Pair<List<Formula>,​CCIncrementalData> encodeIncremental​(CardinalityConstraint cc)
        Encodes an incremental cardinality constraint and returns its encoding.
        Parameters:
        cc - the cardinality constraint
        Returns:
        the encoding of the constraint and the incremental data
      • encodeIncremental

        public CCIncrementalData encodeIncremental​(CardinalityConstraint cc,
                                                   EncodingResult result)
        Encodes an incremental cardinality constraint on a given solver.
        Parameters:
        cc - the cardinality constraint
        result - the result of the encoding
        Returns:
        the incremental data
      • config

        public CCConfig config()
        Returns the current configuration of this encoder. If the encoder was constructed with a given configuration, this configuration will always be used. Otherwise, the current configuration from the formula factory is used.
        Returns:
        the current configuration of
      • encodeConstraint

        protected void encodeConstraint​(CardinalityConstraint cc,
                                        EncodingResult result)
        Encodes the constraint in the given result.
        Parameters:
        cc - the constraint
        result - the result
      • amo

        protected void amo​(EncodingResult result,
                           Variable... vars)
        Encodes an at-most-one constraint.
        Parameters:
        result - the result
        vars - the variables of the constraint
      • exo

        protected void exo​(EncodingResult result,
                           Variable... vars)
        Encodes an at-most-one constraint.
        Parameters:
        result - the result
        vars - the variables of the constraint
      • amk

        protected void amk​(EncodingResult result,
                           Variable[] vars,
                           int rhs)
        Encodes an at-most-k constraint.
        Parameters:
        result - the result
        vars - the variables of the constraint
        rhs - the right-hand side of the constraint
      • amkIncremental

        protected CCIncrementalData amkIncremental​(EncodingResult result,
                                                   Variable[] vars,
                                                   int rhs)
        Encodes an at-most-k constraint for incremental usage.
        Parameters:
        result - the result
        vars - the variables of the constraint
        rhs - the right-hand side of the constraint
        Returns:
        the incremental data
      • alk

        protected void alk​(EncodingResult result,
                           Variable[] vars,
                           int rhs)
        Encodes an at-lest-k constraint.
        Parameters:
        result - the result
        vars - the variables of the constraint
        rhs - the right-hand side of the constraint
      • alkIncremental

        protected CCIncrementalData alkIncremental​(EncodingResult result,
                                                   Variable[] vars,
                                                   int rhs)
        Encodes an at-lest-k constraint for incremental usage.
        Parameters:
        result - the result
        vars - the variables of the constraint
        rhs - the right-hand side of the constraint
        Returns:
        the incremental data
      • exk

        protected void exk​(EncodingResult result,
                           Variable[] vars,
                           int rhs)
        Encodes an exactly-k constraint.
        Parameters:
        result - the result
        vars - the variables of the constraint
        rhs - the right-hand side of the constraint
      • bestAMO

        protected CCAtMostOne bestAMO​(int n)
        Returns the best at-most-one encoder for a given number of variables. The valuation is based on theoretical and practical observations. For <= 10 the pure encoding without introduction of new variables is used, otherwise the product encoding is chosen.
        Parameters:
        n - the number of variables
        Returns:
        the best at-most-one encoder
      • bestAMK

        protected CCAtMostK bestAMK​(int n)
        Returns the best at-most-k encoder for a given number of variables. The valuation is based on theoretical and practical observations. Currently, the modular totalizer is the best encoder for all sizes and therefore is always chosen.
        Parameters:
        n - the number of variables
        Returns:
        the best at-most-one encoder
      • bestALK

        protected CCAtLeastK bestALK​(int n)
        Returns the best at-least-k encoder for a given number of variables. The valuation is based on theoretical and practical observations. Currently, the modular totalizer is the best encoder for all sizes and therefore is always chosen.
        Parameters:
        n - the number of variables
        Returns:
        the best at-most-one encoder
      • bestEXK

        protected CCExactlyK bestEXK​(int n)
        Returns the best exactly-k encoder for a given number of variables. The valuation is based on theoretical and practical observations. Currently, the totalizer is the best encoder for all sizes and therefore is always chosen.
        Parameters:
        n - the number of variables
        Returns:
        the best at-most-one encoder