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.lang.reflect.Field;
31  import java.math.BigInteger;
32  
33  import org.sat4j.minisat.constraints.AbstractDataStructureFactory;
34  import org.sat4j.minisat.constraints.cnf.Clauses;
35  import org.sat4j.minisat.constraints.cnf.LearntBinaryClause;
36  import org.sat4j.minisat.constraints.cnf.LearntHTClause;
37  import org.sat4j.minisat.constraints.cnf.Lits;
38  import org.sat4j.minisat.constraints.cnf.OriginalBinaryClause;
39  import org.sat4j.minisat.constraints.cnf.OriginalHTClause;
40  import org.sat4j.minisat.constraints.cnf.UnitClause;
41  import org.sat4j.minisat.core.Constr;
42  import org.sat4j.minisat.core.ILits;
43  import org.sat4j.pb.constraints.pb.AtLeastPB;
44  import org.sat4j.pb.constraints.pb.IDataStructurePB;
45  import org.sat4j.pb.constraints.pb.Pseudos;
46  import org.sat4j.pb.core.PBDataStructureFactory;
47  import org.sat4j.specs.ContradictionException;
48  import org.sat4j.specs.IVec;
49  import org.sat4j.specs.IVecInt;
50  
51  /**
52   * @author leberre To change the template for this generated type comment go to
53   *         Window>Preferences>Java>Code Generation>Code and Comments
54   */
55  public abstract class AbstractPBDataStructureFactory extends
56  		AbstractDataStructureFactory implements PBDataStructureFactory {
57  
58  	interface INormalizer {
59  		PBContainer nice(IVecInt ps, IVec<BigInteger> bigCoefs,
60  				boolean moreThan, BigInteger bigDeg, ILits voc)
61  				throws ContradictionException;
62  	}
63  
64  	public static final INormalizer FOR_COMPETITION = new INormalizer() {
65  
66  		private static final long serialVersionUID = 1L;
67  
68  		public PBContainer nice(IVecInt literals, IVec<BigInteger> coefs,
69  				boolean moreThan, BigInteger degree, ILits voc)
70  				throws ContradictionException {
71  			int[] theLits = new int[literals.size()];
72  			literals.copyTo(theLits);
73  			BigInteger[] normCoefs = new BigInteger[coefs.size()];
74  			coefs.copyTo(normCoefs);
75  			BigInteger degRes = Pseudos.niceParametersForCompetition(theLits,
76  					normCoefs, moreThan, degree);
77  			return new PBContainer(theLits, normCoefs, degRes);
78  
79  		}
80  
81  	};
82  
83  	public static final INormalizer NO_COMPETITION = new INormalizer() {
84  
85  		private static final long serialVersionUID = 1L;
86  
87  		public PBContainer nice(IVecInt literals, IVec<BigInteger> coefs,
88  				boolean moreThan, BigInteger degree, ILits voc)
89  				throws ContradictionException {
90  			IDataStructurePB res = Pseudos.niceParameters(literals, coefs,
91  					moreThan, degree, voc);
92  			int size = res.size();
93  			int[] theLits = new int[size];
94  			BigInteger[] theCoefs = new BigInteger[size];
95  			res.buildConstraintFromMapPb(theLits, theCoefs);
96  			BigInteger theDegree = res.getDegree();
97  			return new PBContainer(theLits, theCoefs, theDegree);
98  		}
99  	};
100 
101 	private INormalizer norm = FOR_COMPETITION;
102 
103 	protected INormalizer getNormalizer() {
104 		return norm;
105 	}
106 
107 	public void setNormalizer(String simp) {
108 		Field f;
109 		try {
110 			f = AbstractPBDataStructureFactory.class.getDeclaredField(simp);
111 			norm = (INormalizer) f.get(this);
112 		} catch (Exception e) {
113 			e.printStackTrace();
114 			norm = FOR_COMPETITION;
115 		}
116 	}
117 
118 	public void setNormalizer(INormalizer normalizer) {
119 		norm = normalizer;
120 	}
121 
122 	/**
123 	 * 
124 	 */
125 	private static final long serialVersionUID = 1L;
126 
127 	public Constr createClause(IVecInt literals) throws ContradictionException {
128 		IVecInt v = Clauses.sanityCheck(literals, getVocabulary(), solver);
129 		if (v == null)
130 			return null;
131 		if (v.size() == 2) {
132 			return OriginalBinaryClause.brandNewClause(solver, getVocabulary(),
133 					v);
134 		}
135 		return OriginalHTClause.brandNewClause(solver, getVocabulary(), v);
136 	}
137 
138 	public Constr createUnregisteredClause(IVecInt literals) {
139 		if (literals.size() == 1) {
140 			return new UnitClause(literals.last());
141 		}
142 		if (literals.size() == 2) {
143 			return new LearntBinaryClause(literals, getVocabulary());
144 		}
145 		return new LearntHTClause(literals, getVocabulary());
146 	}
147 
148 	@Override
149 	public Constr createCardinalityConstraint(IVecInt literals, int degree)
150 			throws ContradictionException {
151 		return AtLeastPB.atLeastNew(solver, getVocabulary(), literals, degree);
152 	}
153 
154 	public Constr createPseudoBooleanConstraint(IVecInt literals,
155 			IVec<BigInteger> coefs, boolean moreThan, BigInteger degree)
156 			throws ContradictionException {
157 		PBContainer res = getNormalizer().nice(literals, coefs, moreThan,
158 				degree, getVocabulary());
159 		return constraintFactory(res.lits, res.coefs, res.degree);
160 	}
161 
162 	public Constr createUnregisteredPseudoBooleanConstraint(
163 			IDataStructurePB dspb) {
164 		return learntConstraintFactory(dspb);
165 	}
166 
167 	protected abstract Constr constraintFactory(int[] literals,
168 			BigInteger[] coefs, BigInteger degree)
169 			throws ContradictionException;
170 
171 	protected abstract Constr learntConstraintFactory(IDataStructurePB dspb);
172 
173 	@Override
174 	protected ILits createLits() {
175 		return new Lits();
176 	}
177 
178 }