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.pb; 29 30 import static org.sat4j.core.LiteralsUtils.neg; 31 32 import java.math.BigInteger; 33 34 import org.sat4j.minisat.constraints.cnf.HTClause; 35 import org.sat4j.minisat.core.ILits; 36 import org.sat4j.minisat.core.UnitPropagationListener; 37 import org.sat4j.specs.IVecInt; 38 39 public class HTClausePB extends HTClause implements PBConstr { 40 41 /** 42 * 43 */ 44 private static final long serialVersionUID = 1L; 45 46 private boolean learnt; 47 48 public HTClausePB(IVecInt ps, ILits voc) { 49 super(ps, voc); 50 // TODO Auto-generated constructor stub 51 } 52 53 /* 54 * (non-Javadoc) 55 * 56 * @see org.sat4j.minisat.constraints.pb.PBConstr#getCoefficient(int) 57 */ 58 public BigInteger getCoef(int literal) { 59 return BigInteger.ONE; 60 } 61 62 /* 63 * (non-Javadoc) 64 * 65 * @see org.sat4j.minisat.constraints.pb.PBConstr#getDegree() 66 */ 67 public BigInteger getDegree() { 68 return BigInteger.ONE; 69 } 70 71 public BigInteger[] getCoefs() { 72 BigInteger[] tmp = new BigInteger[size()]; 73 for (int i = 0; i < tmp.length; i++) 74 tmp[i] = BigInteger.ONE; 75 return tmp; 76 } 77 78 /** 79 * Creates a brand new clause, presumably from external data. Performs all 80 * sanity checks. 81 * 82 * @param s 83 * the object responsible for unit propagation 84 * @param voc 85 * the vocabulary 86 * @param literals 87 * the literals to store in the clause 88 * @return the created clause or null if the clause should be ignored 89 * (tautology for example) 90 */ 91 public static HTClausePB brandNewClause(UnitPropagationListener s, 92 ILits voc, IVecInt literals) { 93 HTClausePB c = new HTClausePB(literals, voc); 94 c.register(); 95 return c; 96 } 97 98 @Override 99 public void assertConstraint(UnitPropagationListener s) { 100 for (int i = 0; i < size(); i++) 101 if (getVocabulary().isUnassigned(get(i))) { 102 boolean ret = s.enqueue(get(i), this); 103 assert ret; 104 break; 105 } 106 } 107 108 public IVecInt computeAnImpliedClause() { 109 return null; 110 } 111 112 /** 113 * declares this clause as learnt 114 * 115 */ 116 public void setLearnt() { 117 learnt = true; 118 } 119 120 /* 121 * (non-Javadoc) 122 * 123 * @see org.sat4j.minisat.datatype.Constr#learnt() 124 */ 125 public boolean learnt() { 126 return learnt; 127 } 128 129 /** 130 * Register this clause which means watching the necessary literals If the 131 * clause is learnt, setLearnt() must be called before a call to register() 132 * 133 * @see #setLearnt() 134 */ 135 public void register() { 136 if (learnt) { 137 if (middleLits.length > 0) { 138 // looking for the literal to put in tail 139 int maxi = 0; 140 int maxlevel = voc.getLevel(middleLits[0]); 141 for (int i = 1; i < middleLits.length; i++) { 142 int level = voc.getLevel(middleLits[i]); 143 if (level > maxlevel) { 144 maxi = i; 145 maxlevel = level; 146 } 147 } 148 if (maxlevel > voc.getLevel(tail)) { 149 int l = tail; 150 tail = middleLits[maxi]; 151 middleLits[maxi] = l; 152 } 153 } 154 } 155 156 // watch both head and tail literals. 157 voc.attach(neg(head), this); 158 voc.attach(neg(tail), this); 159 } 160 161 162 }