View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2008 Daniel Le Berre
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the Eclipse Public License v1.0
6    * which accompanies this distribution, and is available at
7    * http://www.eclipse.org/legal/epl-v10.html
8    *
9    * Alternatively, the contents of this file may be used under the terms of
10   * either the GNU Lesser General Public License Version 2.1 or later (the
11   * "LGPL"), in which case the provisions of the LGPL are applicable instead
12   * of those above. If you wish to allow use of your version of this file only
13   * under the terms of the LGPL, and not to allow others to use your version of
14   * this file under the terms of the EPL, indicate your decision by deleting
15   * the provisions above and replace them with the notice and other provisions
16   * required by the LGPL. If you do not delete the provisions above, a recipient
17   * may use your version of this file under the terms of the EPL or the LGPL.
18   * 
19   * Based on the pseudo boolean algorithms described in:
20   * A fast pseudo-Boolean constraint solver Chai, D.; Kuehlmann, A.
21   * Computer-Aided Design of Integrated Circuits and Systems, IEEE Transactions on
22   * Volume 24, Issue 3, March 2005 Page(s): 305 - 317
23   * 
24   * and 
25   * Heidi E. Dixon, 2004. Automating Pseudo-Boolean Inference within a DPLL 
26   * Framework. Ph.D. Dissertation, University of Oregon.
27   *******************************************************************************/
28  package org.sat4j.pb.constraints;
29  
30  import java.math.BigInteger;
31  
32  import org.sat4j.core.Vec;
33  import org.sat4j.core.VecInt;
34  import org.sat4j.minisat.constraints.cnf.Clauses;
35  import org.sat4j.minisat.core.Constr;
36  import org.sat4j.pb.constraints.pb.IDataStructurePB;
37  import org.sat4j.specs.ContradictionException;
38  import org.sat4j.specs.IVec;
39  import org.sat4j.specs.IVecInt;
40  
41  public abstract class AbstractPBClauseCardConstrDataStructure extends
42  		AbstractPBDataStructureFactory {
43  
44  	/**
45  	 * 
46  	 */
47  	private static final long serialVersionUID = 1L;
48  
49  	static final BigInteger MAX_INT_VALUE = BigInteger
50  			.valueOf(Integer.MAX_VALUE);
51  
52  	/*
53  	 * (non-Javadoc)
54  	 * 
55  	 * @seeorg.sat4j.minisat.constraints.AbstractPBDataStructureFactory#
56  	 * constraintFactory(org.sat4j.specs.VecInt, org.sat4j.specs.VecInt,
57  	 * boolean, int)
58  	 */
59  	@Override
60  	protected Constr constraintFactory(int[] literals, BigInteger[] coefs,
61  			BigInteger degree) throws ContradictionException {
62  		if (degree.equals(BigInteger.ONE)) {
63  			IVecInt v = Clauses.sanityCheck(new VecInt(literals),
64  					getVocabulary(), solver);
65  			if (v == null)
66  				return null;
67  			return constructClause(v);
68  		}
69  		if (coefficientsEqualToOne(coefs)) {
70  			assert degree.compareTo(MAX_INT_VALUE) < 0;
71  			return constructCard(new VecInt(literals), degree.intValue());
72  		}
73  		return constructPB(literals, coefs, degree);
74  	}
75  
76  	/*
77  	 * (non-Javadoc)
78  	 * 
79  	 * @seeorg.sat4j.minisat.constraints.AbstractPBDataStructureFactory#
80  	 * constraintFactory(org.sat4j.specs.VecInt, org.sat4j.specs.VecInt, int)
81  	 */
82  	@Override
83  	protected Constr learntConstraintFactory(IDataStructurePB dspb) {
84  		if (dspb.getDegree().equals(BigInteger.ONE)) {
85  			IVecInt literals = new VecInt();
86  			IVec<BigInteger> resCoefs = new Vec<BigInteger>();
87  			dspb.buildConstraintFromConflict(literals, resCoefs);
88  			// then assertive literal must be placed at the first place
89  			int indLit = dspb.getAssertiveLiteral();
90  			if (indLit > -1) {
91  				int tmp = literals.get(indLit);
92  				literals.set(indLit, literals.get(0));
93  				literals.set(0, tmp);
94  			}
95  			// ILits voc = getVocabulary();
96  			// boolean ok = false;
97  			// for (int indLit = 0; indLit < literals.size() && !ok; indLit++)
98  			// if (voc.isUnassigned(literals.get(indLit))) {
99  			// int tmp = literals.get(indLit);
100 			// literals.set(indLit, literals.get(0));
101 			// literals.set(0, tmp);
102 			// assert voc.isUnassigned(tmp);
103 			// assert voc.isUnassigned(literals.get(0));
104 			// ok = true;
105 			// }
106 			// assert voc.isUnassigned(literals.get(0));
107 			return constructLearntClause(literals);
108 		}
109 		if (dspb.isCardinality()) {
110 			return constructLearntCard(dspb);
111 		}
112 		return constructLearntPB(dspb);
113 	}
114 
115 	static boolean coefficientsEqualToOne(BigInteger[] coefs) {
116 		for (int i = 0; i < coefs.length; i++)
117 			if (!coefs[i].equals(BigInteger.ONE))
118 				return false;
119 		return true;
120 	}
121 
122 	abstract protected Constr constructClause(IVecInt v);
123 
124 	abstract protected Constr constructCard(IVecInt theLits, int degree)
125 			throws ContradictionException;
126 
127 	abstract protected Constr constructPB(int[] theLits, BigInteger[] coefs,
128 			BigInteger degree) throws ContradictionException;
129 
130 	abstract protected Constr constructLearntClause(IVecInt literals);
131 
132 	abstract protected Constr constructLearntCard(IDataStructurePB dspb);
133 
134 	abstract protected Constr constructLearntPB(IDataStructurePB dspb);
135 
136 }