View Javadoc

1   /*
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2006 Daniel Le Berre
3    * 
4    * Based on the original minisat specification from:
5    * 
6    * An extensible SAT solver. Niklas E?n and Niklas S?rensson. Proceedings of the
7    * Sixth International Conference on Theory and Applications of Satisfiability
8    * Testing, LNCS 2919, pp 502-518, 2003.
9    * 
10   * This library is free software; you can redistribute it and/or modify it under
11   * the terms of the GNU Lesser General Public License as published by the Free
12   * Software Foundation; either version 2.1 of the License, or (at your option)
13   * any later version.
14   * 
15   * This library is distributed in the hope that it will be useful, but WITHOUT
16   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18   * details.
19   * 
20   * You should have received a copy of the GNU Lesser General Public License
21   * along with this library; if not, write to the Free Software Foundation, Inc.,
22   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23   * 
24   */
25  
26  package org.sat4j.minisat.constraints.cnf;
27  
28  import java.io.Serializable;
29  
30  import org.sat4j.core.Vec;
31  import org.sat4j.minisat.core.Constr;
32  import org.sat4j.minisat.core.ILits;
33  import org.sat4j.minisat.core.Propagatable;
34  import org.sat4j.minisat.core.Undoable;
35  import org.sat4j.specs.IVec;
36  
37  /**
38   * @author laihem
39   * @author leberre To change the template for this generated type comment go to
40   *         Window - Preferences - Java - Code Generation - Code and Comments
41   */
42  public class Lits implements Serializable, ILits {
43  
44      private static final long serialVersionUID = 1L;
45  
46      private boolean pool[] = new boolean[1];
47  
48      private int realnVars = 0;
49  
50      @SuppressWarnings("unchecked")
51      protected IVec<Propagatable>[] watches = new IVec[0];
52  
53      private int[] level = new int[0];
54  
55      private Constr[] reason = new Constr[0];
56  
57      @SuppressWarnings("unchecked")
58      private IVec<Undoable>[] undos = new IVec[0];
59  
60      private boolean[] falsified = new boolean[0];
61  
62      public Lits() {
63      }
64  
65      @SuppressWarnings( { "unchecked" })
66      public void init(int nvar) {
67          assert nvar >= 0;
68          // let some space for unused 0 indexer.
69          int nvars = nvar + 1;
70          boolean[] npool = new boolean[nvars];
71          System.arraycopy(pool, 0, npool, 0, pool.length);
72          pool = npool;
73  
74          level = new int[nvars];
75          int[] nlevel = new int[nvars];
76          System.arraycopy(level, 0, nlevel, 0, level.length);
77          level = nlevel;
78  
79          IVec<Propagatable>[] nwatches = new IVec[2 * nvars];
80          System.arraycopy(watches, 0, nwatches, 0, watches.length);
81          watches = nwatches;
82  
83          IVec<Undoable>[] nundos = new IVec[nvars];
84          System.arraycopy(undos, 0, nundos, 0, undos.length);
85          undos = nundos;
86  
87          Constr[] nreason = new Constr[nvars];
88          System.arraycopy(reason, 0, nreason, 0, reason.length);
89          reason = nreason;
90  
91          boolean[] newFalsified = new boolean[2 * nvars];
92          System.arraycopy(falsified, 0, newFalsified, 0, falsified.length);
93          falsified = newFalsified;
94      }
95  
96      public int getFromPool(int x) {
97          int var = Math.abs(x);
98          assert var < pool.length;
99  
100         int lit = ((x < 0) ? (var << 1) ^ 1 : (var << 1));
101         assert lit > 1;
102         if (!pool[var]) {
103             realnVars++;
104             pool[var] = true;
105             watches[var << 1] = new Vec<Propagatable>();
106             watches[(var << 1) | 1] = new Vec<Propagatable>();
107             undos[var] = new Vec<Undoable>();
108             level[var] = -1;
109             falsified[var << 1] = false; // because truthValue[var] is
110                                             // UNDEFINED
111             falsified[var << 1 | 1] = false; // because truthValue[var] is
112                                                 // UNDEFINED
113         }
114         return lit;
115     }
116 
117     public boolean belongsToPool(int x) {
118         assert x > 0;
119         return pool[x];
120     }
121 
122     public void resetPool() {
123         for (int i = 0; i < pool.length; i++) {
124             if (pool[i]) {
125                 reset(i << 1);
126             }
127         }
128     }
129 
130     public void ensurePool(int howmany) {
131         init(howmany);
132     }
133 
134     public void unassign(int lit) {
135         assert falsified[lit] || falsified[lit ^ 1];
136         falsified[lit] = false;
137         falsified[lit ^ 1] = false;
138     }
139 
140     public void satisfies(int lit) {
141         assert !falsified[lit] && !falsified[lit ^ 1];
142         falsified[lit] = false;
143         falsified[lit ^ 1] = true;
144     }
145 
146     public boolean isSatisfied(int lit) {
147         return falsified[lit ^ 1];
148     }
149 
150     public final boolean isFalsified(int lit) {
151         return falsified[lit];
152     }
153 
154     public boolean isUnassigned(int lit) {
155         return !falsified[lit] && !falsified[lit ^ 1];
156     }
157 
158     public String valueToString(int lit) {
159         if (isUnassigned(lit))
160             return "?"; //$NON-NLS-1$
161         if (isSatisfied(lit))
162             return "T"; //$NON-NLS-1$
163         return "F"; //$NON-NLS-1$
164     }
165 
166     public int nVars() {
167         return pool.length - 1;
168     }
169 
170     public int not(int lit) {
171         return lit ^ 1;
172     }
173 
174     public static String toString(int lit) {
175         return ((lit & 1) == 0 ? "" : "-") + (lit >> 1); //$NON-NLS-1$//$NON-NLS-2$
176     }
177 
178     public void reset(int lit) {
179         watches[lit].clear();
180         watches[lit ^ 1].clear();
181         level[lit >> 1] = -1;
182         reason[lit >> 1] = null;
183         undos[lit >> 1].clear();
184         falsified[lit] = false;
185         falsified[lit ^ 1] = false;
186     }
187 
188     public int getLevel(int lit) {
189         return level[lit >> 1];
190     }
191 
192     public void setLevel(int lit, int l) {
193         level[lit >> 1] = l;
194     }
195 
196     public Constr getReason(int lit) {
197         return reason[lit >> 1];
198     }
199 
200     public void setReason(int lit, Constr r) {
201         reason[lit >> 1] = r;
202     }
203 
204     public IVec<Undoable> undos(int lit) {
205         return undos[lit >> 1];
206     }
207 
208     public void watch(int lit, Propagatable c) {
209         watches[lit].push(c);
210     }
211 
212     public IVec<Propagatable> watches(int lit) {
213         return watches[lit];
214     }
215 
216     public boolean isImplied(int lit) {
217         int var = lit >> 1;
218         assert reason[var] == null || falsified[lit] || falsified[lit ^ 1];
219         // a literal is implied if it is a unit clause, ie
220         // propagated without reason at decision level 0.
221         return reason[var] != null || level[var] == 0;
222     }
223 
224     public int realnVars() {
225         return realnVars;
226     }
227 }