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 original MiniSat specification from:
20   * 
21   * An extensible SAT solver. Niklas Een and Niklas Sorensson. Proceedings of the
22   * Sixth International Conference on Theory and Applications of Satisfiability
23   * Testing, LNCS 2919, pp 502-518, 2003.
24   *
25   * See www.minisat.se for the original solver in C++.
26   * 
27   *******************************************************************************/
28  package org.sat4j.minisat.constraints.cnf;
29  
30  import static org.sat4j.core.LiteralsUtils.neg;
31  
32  import java.io.Serializable;
33  
34  import org.sat4j.minisat.core.Constr;
35  import org.sat4j.minisat.core.ILits;
36  import org.sat4j.minisat.core.UnitPropagationListener;
37  import org.sat4j.specs.IVecInt;
38  
39  /**
40   * Data structure for binary clause.
41   * 
42   * @author leberre
43   */
44  public abstract class BinaryClause implements Constr, Serializable {
45  
46  	private static final long serialVersionUID = 1L;
47  
48  	private double activity;
49  
50  	private final ILits voc;
51  
52  	private int head;
53  
54  	private int tail;
55  
56  	/**
57  	 * Creates a new basic clause
58  	 * 
59  	 * @param voc
60  	 *            the vocabulary of the formula
61  	 * @param ps
62  	 *            A VecInt that WILL BE EMPTY after calling that method.
63  	 */
64  	public BinaryClause(IVecInt ps, ILits voc) {
65  		assert ps.size() == 2;
66  		head = ps.get(0);
67  		tail = ps.get(1);
68  		this.voc = voc;
69  		activity = 0;
70  	}
71  
72  	/*
73  	 * (non-Javadoc)
74  	 * 
75  	 * @see Constr#calcReason(Solver, Lit, Vec)
76  	 */
77  	public void calcReason(int p, IVecInt outReason) {
78  		if (voc.isFalsified(head)) {
79  			outReason.push(neg(head));
80  		}
81  		if (voc.isFalsified(tail)) {
82  			outReason.push(neg(tail));
83  		}
84  	}
85  
86  	/*
87  	 * (non-Javadoc)
88  	 * 
89  	 * @see Constr#remove(Solver)
90  	 */
91  	public void remove() {
92  		voc.attaches(neg(head)).remove(this);
93  		voc.attaches(neg(tail)).remove(this);
94  	}
95  
96  	/*
97  	 * (non-Javadoc)
98  	 * 
99  	 * @see Constr#simplify(Solver)
100 	 */
101 	public boolean simplify() {
102 		if (voc.isSatisfied(head) || voc.isSatisfied(tail)) {
103 			return true;
104 		}
105 		return false;
106 	}
107 
108 	public boolean propagate(UnitPropagationListener s, int p) {
109 		voc.attach(p, this);
110 		if (head == neg(p)) {
111 			return s.enqueue(tail, this);
112 		}
113 		assert tail == neg(p);
114 		return s.enqueue(head, this);
115 	}
116 
117 	/*
118 	 * For learnt clauses only @author leberre
119 	 */
120 	public boolean locked() {
121 		return voc.getReason(head) == this || voc.getReason(tail) == this;
122 	}
123 
124 	/**
125 	 * @return the activity of the clause
126 	 */
127 	public double getActivity() {
128 		return activity;
129 	}
130 
131 	@Override
132 	public String toString() {
133 		StringBuffer stb = new StringBuffer();
134 		stb.append(Lits.toString(head));
135 		stb.append("["); //$NON-NLS-1$
136 		stb.append(voc.valueToString(head));
137 		stb.append("]"); //$NON-NLS-1$
138 		stb.append(" "); //$NON-NLS-1$
139 		stb.append(Lits.toString(tail));
140 		stb.append("["); //$NON-NLS-1$
141 		stb.append(voc.valueToString(tail));
142 		stb.append("]"); //$NON-NLS-1$
143 		return stb.toString();
144 	}
145 
146 	/**
147 	 * Retourne le ieme literal de la clause. Attention, cet ordre change durant
148 	 * la recherche.
149 	 * 
150 	 * @param i
151 	 *            the index of the literal
152 	 * @return the literal
153 	 */
154 	public int get(int i) {
155 		if (i == 0)
156 			return head;
157 		assert i == 1;
158 		return tail;
159 	}
160 
161 	/**
162 	 * @param claInc
163 	 */
164 	public void incActivity(double claInc) {
165 		activity += claInc;
166 	}
167 
168 	/**
169 	 * @param d
170 	 */
171 	public void rescaleBy(double d) {
172 		activity *= d;
173 	}
174 
175 	public int size() {
176 		return 2;
177 	}
178 
179 	public void assertConstraint(UnitPropagationListener s) {
180 		boolean ret;
181 		if (voc.isUnassigned(head)) {
182 			ret = s.enqueue(head, this);
183 		} else {
184 			assert voc.isUnassigned(tail);
185 			ret = s.enqueue(tail, this);
186 		}
187 		assert ret;
188 	}
189 
190 	public ILits getVocabulary() {
191 		return voc;
192 	}
193 
194 	public int[] getLits() {
195 		int[] tmp = new int[2];
196 		tmp[0] = head;
197 		tmp[1] = tail;
198 		return tmp;
199 	}
200 
201 	@Override
202 	public boolean equals(Object obj) {
203 		if (obj == null)
204 			return false;
205 		try {
206 			BinaryClause wcl = (BinaryClause) obj;
207 			if (wcl.head != head || wcl.tail != tail) {
208 				return false;
209 			}
210 			return true;
211 		} catch (ClassCastException e) {
212 			return false;
213 		}
214 	}
215 
216 	@Override
217 	public int hashCode() {
218 		long sum = head + tail;
219 		return (int) sum / 2;
220 	}
221 
222 	public void register() {
223 		voc.attach(neg(head), this);
224 		voc.attach(neg(tail), this);
225 	}
226 }