- java.lang.Object
-
- org.logicng.cardinalityconstraints.CCEncoder
-
public class CCEncoder extends Object
An encoder for cardinality constraints.An encoder is configured with a
CCConfigconfiguration. There are two possible ways:- Initialize the encoder with a given configuration in the constructor. Then this configuration will be bound to the encoder for its whole lifetime.
- 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
-
-
Field Summary
Fields Modifier and Type Field Description protected CCALKCardinalityNetworkalkCardinalityNetworkprotected CCALKModularTotalizeralkModularTotalizerprotected CCALKTotalizeralkTotalizerprotected CCAMKCardinalityNetworkamkCardinalityNetworkprotected CCAMKModularTotalizeramkModularTotalizerprotected CCAMKTotalizeramkTotalizerprotected CCAMOBimanderamoBimanderprotected CCAMOBinaryamoBinaryprotected CCAMOCommanderamoCommanderprotected CCAMOLadderamoLadderprotected CCAMONestedamoNestedprotected CCAMOProductamoProductprotected CCAMOPureamoPureprotected CCConfigconfigprotected CCEXKCardinalityNetworkexkCardinalityNetworkprotected CCEXKTotalizerexkTotalizerprotected FormulaFactoryf
-
Constructor Summary
Constructors Constructor Description CCEncoder(FormulaFactory f)Constructs a new cardinality constraint encoder which uses the configuration of the formula factory.CCEncoder(FormulaFactory f, CCConfig config)Constructs a new cardinality constraint encoder with a given configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidalk(EncodingResult result, Variable[] vars, int rhs)Encodes an at-lest-k constraint.protected CCIncrementalDataalkIncremental(EncodingResult result, Variable[] vars, int rhs)Encodes an at-lest-k constraint for incremental usage.protected voidamk(EncodingResult result, Variable[] vars, int rhs)Encodes an at-most-k constraint.protected CCIncrementalDataamkIncremental(EncodingResult result, Variable[] vars, int rhs)Encodes an at-most-k constraint for incremental usage.protected voidamo(EncodingResult result, Variable... vars)Encodes an at-most-one constraint.protected CCAtLeastKbestALK(int n)Returns the best at-least-k encoder for a given number of variables.protected CCAtMostKbestAMK(int n)Returns the best at-most-k encoder for a given number of variables.protected CCAtMostOnebestAMO(int n)Returns the best at-most-one encoder for a given number of variables.protected CCExactlyKbestEXK(int n)Returns the best exactly-k encoder for a given number of variables.CCConfigconfig()Returns the current configuration of this encoder.List<Formula>encode(CardinalityConstraint cc)Encodes a cardinality constraint and returns its CNF encoding.voidencode(CardinalityConstraint cc, EncodingResult result)Encodes a cardinality constraint in a given result.protected voidencodeConstraint(CardinalityConstraint cc, EncodingResult result)Encodes the constraint in the given result.Pair<List<Formula>,CCIncrementalData>encodeIncremental(CardinalityConstraint cc)Encodes an incremental cardinality constraint and returns its encoding.CCIncrementalDataencodeIncremental(CardinalityConstraint cc, EncodingResult result)Encodes an incremental cardinality constraint on a given solver.protected CCIncrementalDataencodeIncrementalConstraint(CardinalityConstraint cc, EncodingResult result)protected voidexk(EncodingResult result, Variable[] vars, int rhs)Encodes an exactly-k constraint.protected voidexo(EncodingResult result, Variable... vars)Encodes an at-most-one constraint.StringtoString()
-
-
-
Field Detail
-
f
protected final FormulaFactory f
-
config
protected final CCConfig config
-
amoPure
protected CCAMOPure amoPure
-
amoLadder
protected CCAMOLadder amoLadder
-
amoProduct
protected CCAMOProduct amoProduct
-
amoNested
protected CCAMONested amoNested
-
amoCommander
protected CCAMOCommander amoCommander
-
amoBinary
protected CCAMOBinary amoBinary
-
amoBimander
protected CCAMOBimander amoBimander
-
amkCardinalityNetwork
protected CCAMKCardinalityNetwork amkCardinalityNetwork
-
amkModularTotalizer
protected CCAMKModularTotalizer amkModularTotalizer
-
amkTotalizer
protected CCAMKTotalizer amkTotalizer
-
alkTotalizer
protected CCALKTotalizer alkTotalizer
-
alkModularTotalizer
protected CCALKModularTotalizer alkModularTotalizer
-
alkCardinalityNetwork
protected CCALKCardinalityNetwork alkCardinalityNetwork
-
exkTotalizer
protected CCEXKTotalizer exkTotalizer
-
exkCardinalityNetwork
protected CCEXKCardinalityNetwork exkCardinalityNetwork
-
-
Constructor Detail
-
CCEncoder
public CCEncoder(FormulaFactory f, CCConfig config)
Constructs a new cardinality constraint encoder with a given configuration.- Parameters:
f- the formula factoryconfig- 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 constraintresult- 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 constraintresult- the result of the encoding- Returns:
- the incremental data
-
encodeIncrementalConstraint
protected CCIncrementalData encodeIncrementalConstraint(CardinalityConstraint cc, EncodingResult result)
-
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 constraintresult- the result
-
amo
protected void amo(EncodingResult result, Variable... vars)
Encodes an at-most-one constraint.- Parameters:
result- the resultvars- the variables of the constraint
-
exo
protected void exo(EncodingResult result, Variable... vars)
Encodes an at-most-one constraint.- Parameters:
result- the resultvars- the variables of the constraint
-
amk
protected void amk(EncodingResult result, Variable[] vars, int rhs)
Encodes an at-most-k constraint.- Parameters:
result- the resultvars- the variables of the constraintrhs- 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 resultvars- the variables of the constraintrhs- 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 resultvars- the variables of the constraintrhs- 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 resultvars- the variables of the constraintrhs- 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 resultvars- the variables of the constraintrhs- 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
-
-