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.pb;
29  
30  import java.math.BigInteger;
31  
32  import org.sat4j.core.Vec;
33  import org.sat4j.core.VecInt;
34  import org.sat4j.minisat.core.ILits;
35  import org.sat4j.minisat.core.UnitPropagationListener;
36  import org.sat4j.specs.ContradictionException;
37  import org.sat4j.specs.IVec;
38  import org.sat4j.specs.IVecInt;
39  
40  public class PuebloMinWatchPb extends MinWatchPb {
41  
42      private static final long serialVersionUID = 1L;
43  
44      /**
45       * Constructeur de base des contraintes
46       * 
47       * @param voc
48       *            Informations sur le vocabulaire employ???
49       * @param ps
50       *            Liste des litt???raux
51       * @param weightedLits
52       *            Liste des coefficients
53       * @param moreThan
54       *            Indication sur le comparateur
55       * @param degree
56       *            Stockage du degr??? de la contrainte
57       */
58      private PuebloMinWatchPb(ILits voc, IDataStructurePB mpb) {
59  
60          super(voc, mpb);
61      }
62  
63      /**
64       * @param s
65       *            outil pour la propagation des litt???raux
66       * @param ps
67       *            liste des litt???raux de la nouvelle contrainte
68       * @param coefs
69       *            liste des coefficients des litt???raux de la contrainte
70       * @param moreThan
71       *            d???termine si c'est une sup???rieure ou ???gal ??? l'origine
72       * @param degree
73       *            fournit le degr??? de la contrainte
74       * @return une nouvelle clause si tout va bien, ou null si un conflit est
75       *         d???tect???
76       */
77      public static PuebloMinWatchPb minWatchPbNew(UnitPropagationListener s,
78              ILits voc, IVecInt ps, IVecInt coefs, boolean moreThan, int degree)
79              throws ContradictionException {
80          return minWatchPbNew(s, voc, ps, Pseudos.toVecBigInt(coefs), moreThan,
81                  BigInteger.valueOf(degree));
82      }
83  
84      /**
85       * @param s
86       *            outil pour la propagation des litt???raux
87       * @param ps
88       *            liste des litt???raux de la nouvelle contrainte
89       * @param coefs
90       *            liste des coefficients des litt???raux de la contrainte
91       * @param moreThan
92       *            d???termine si c'est une sup???rieure ou ???gal ??? l'origine
93       * @param degree
94       *            fournit le degr??? de la contrainte
95       * @return une nouvelle clause si tout va bien, ou null si un conflit est
96       *         d???tect???
97       */
98      public static PuebloMinWatchPb minWatchPbNew(UnitPropagationListener s,
99              ILits voc, IVecInt ps, IVec<BigInteger> coefs, boolean moreThan,
100             BigInteger degree) throws ContradictionException {
101         // Il ne faut pas modifier les param?tres
102         VecInt litsVec = new VecInt(ps.size());
103         IVec<BigInteger> coefsVec = new Vec<BigInteger>(coefs.size());
104         ps.copyTo(litsVec);
105         coefs.copyTo(coefsVec);
106 
107         // Ajouter les simplifications quand la structure sera d???finitive
108         IDataStructurePB mpb = Pseudos.niceParameters(litsVec, coefsVec, moreThan,
109                 degree, voc);
110         PuebloMinWatchPb outclause = new PuebloMinWatchPb(voc, mpb);
111 
112         if (outclause.degree.signum() <= 0) {
113             return null;
114         }
115 
116         outclause.computeWatches();
117 
118         outclause.computePropagation(s);
119 
120         return outclause;
121 
122     }
123 
124     public static PuebloMinWatchPb minWatchPbNew(UnitPropagationListener s,
125             ILits voc, IDataStructurePB mpb) throws ContradictionException {
126         PuebloMinWatchPb outclause = new PuebloMinWatchPb(voc, mpb);
127 
128         if (outclause.degree.signum() <= 0) {
129             return null;
130         }
131 
132         outclause.computeWatches();
133 
134         outclause.computePropagation(s);
135 
136         return outclause;
137 
138     }
139 
140     /**
141      * 
142      */
143     public static WatchPb watchPbNew(ILits voc, IVecInt lits, IVecInt coefs,
144             boolean moreThan, int degree) {
145         return watchPbNew(voc, lits, Pseudos.toVecBigInt(coefs), moreThan,
146                 BigInteger.valueOf(degree));
147     }
148 
149     /**
150      * 
151      */
152     public static WatchPb watchPbNew(ILits voc, IVecInt lits,
153             IVec<BigInteger> coefs, boolean moreThan, BigInteger degree) {
154         IDataStructurePB mpb = null;
155         mpb = Pseudos.niceCheckedParameters(lits, coefs, moreThan, degree, voc);
156         return new PuebloMinWatchPb(voc, mpb);
157     }
158 
159     public static WatchPb normalizedWatchPbNew(ILits voc, IDataStructurePB mpb) {
160         return new PuebloMinWatchPb(voc, mpb);
161     }
162 
163     @Override
164     protected BigInteger maximalCoefficient(int pIndice) {
165         return coefs[0];
166     }
167 
168     @Override
169     protected BigInteger updateWatched(BigInteger mc, int pIndice) {
170         BigInteger maxCoef = mc;
171         if (watchingCount < size()) {
172             BigInteger upWatchCumul = watchCumul.subtract(coefs[pIndice]);
173             BigInteger borneSup = degree.add(maxCoef);
174             for (int ind = 0; ind < lits.length
175                     && upWatchCumul.compareTo(borneSup) < 0; ind++) {
176                 if (!voc.isFalsified(lits[ind]) && !watched[ind]) {
177                     upWatchCumul = upWatchCumul.add(coefs[ind]);
178                     watched[ind] = true;
179                     assert watchingCount < size();
180                     watching[watchingCount++] = ind;
181                     voc.attach(lits[ind] ^ 1, this);
182                 }
183             }
184             watchCumul = upWatchCumul.add(coefs[pIndice]);
185         }
186         return maxCoef;
187     }
188 
189 }