View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004, 2012 Artois University and CNRS
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   * Contributors:
28   *   CRIL - initial API and implementation
29   *******************************************************************************/
30  package org.sat4j.pb.constraints.pb;
31  
32  import java.math.BigInteger;
33  
34  import org.sat4j.minisat.constraints.card.MinWatchCard;
35  import org.sat4j.minisat.core.ILits;
36  import org.sat4j.specs.ContradictionException;
37  import org.sat4j.specs.IVecInt;
38  import org.sat4j.specs.UnitPropagationListener;
39  
40  public final class MinWatchCardPB extends MinWatchCard implements PBConstr {
41  
42      /**
43       * 
44       */
45      private static final long serialVersionUID = 1L;
46  
47      private final BigInteger bigDegree;
48  
49      public MinWatchCardPB(ILits voc, IVecInt ps, boolean moreThan, int degree) {
50          super(voc, ps, moreThan, degree);
51          // this.degree has been computed in the superclass constructor.
52          this.bigDegree = BigInteger.valueOf(this.degree);
53      }
54  
55      public MinWatchCardPB(ILits voc, IVecInt ps, int degree) {
56          super(voc, ps, degree);
57          this.bigDegree = BigInteger.valueOf(this.degree);
58      }
59  
60      /*
61       * (non-Javadoc)
62       * 
63       * @see org.sat4j.minisat.constraints.pb.PBConstr#getCoefficient(int)
64       */
65      public BigInteger getCoef(int literal) {
66          return BigInteger.ONE;
67      }
68  
69      /*
70       * (non-Javadoc)
71       * 
72       * @see org.sat4j.minisat.constraints.pb.PBConstr#getDegree()
73       */
74      public BigInteger getDegree() {
75          return this.bigDegree;
76      }
77  
78      public BigInteger[] getCoefs() {
79          BigInteger[] tmp = new BigInteger[size()];
80          for (int i = 0; i < tmp.length; i++) {
81              tmp[i] = BigInteger.ONE;
82          }
83          return tmp;
84      }
85  
86      /**
87       * Permet la cr?ation de contrainte de cardinalit? ? observation minimale
88       * 
89       * @param s
90       *            outil pour la propagation des litt?raux
91       * @param voc
92       *            vocabulaire utilis? par la contrainte
93       * @param ps
94       *            liste des litt?raux de la nouvelle contrainte
95       * @param degree
96       *            fournit le degr? de la contrainte
97       * @return une nouvelle clause si tout va bien, null sinon
98       * @throws ContradictionException
99       */
100     public static PBConstr normalizedMinWatchCardPBNew(
101             UnitPropagationListener s, ILits voc, IVecInt ps, int degree)
102             throws ContradictionException {
103         return minWatchCardPBNew(s, voc, ps, ATLEAST, degree, true);
104     }
105 
106     /**
107      * Permet la cr?ation de contrainte de cardinalit? ? observation minimale
108      * 
109      * @param s
110      *            outil pour la propagation des litt?raux
111      * @param voc
112      *            vocabulaire utilis? par la contrainte
113      * @param ps
114      *            liste des litt?raux de la nouvelle contrainte
115      * @param moreThan
116      *            d?termine si c'est une sup?rieure ou ?gal ? l'origine
117      * @param degree
118      *            fournit le degr? de la contrainte
119      * @return une nouvelle clause si tout va bien, null sinon
120      * @throws ContradictionException
121      */
122     public static PBConstr minWatchCardPBNew(UnitPropagationListener s,
123             ILits voc, IVecInt ps, boolean moreThan, int degree)
124             throws ContradictionException {
125         return minWatchCardPBNew(s, voc, ps, moreThan, degree, false);
126     }
127 
128     private static PBConstr minWatchCardPBNew(UnitPropagationListener s,
129             ILits voc, IVecInt ps, boolean moreThan, int degree,
130             boolean normalized) throws ContradictionException {
131         int mydegree = degree + linearisation(voc, ps);
132 
133         if (ps.size() < mydegree) {
134             throw new ContradictionException();
135         } else if (ps.size() == 0 && mydegree > 0) {
136             throw new ContradictionException();
137         } else if (ps.size() == mydegree || ps.size() <= 0) {
138             for (int i = 0; i < ps.size(); i++) {
139                 if (!s.enqueue(ps.get(i))) {
140                     throw new ContradictionException();
141                 }
142             }
143             return new UnitClausesPB(ps);
144         }
145 
146         // constraint is now instanciated
147         MinWatchCardPB retour = null;
148         if (normalized) {
149             retour = new MinWatchCardPB(voc, ps, mydegree);
150         } else {
151             retour = new MinWatchCardPB(voc, ps, moreThan, mydegree);
152         }
153 
154         if (retour.bigDegree.signum() <= 0) {
155             return null;
156         }
157 
158         retour.computeWatches();
159 
160         return (MinWatchCardPB) retour.computePropagation(s);
161     }
162 
163     /**
164      * 
165      */
166     private boolean learnt = false;
167 
168     /**
169      * D?termine si la contrainte est apprise
170      * 
171      * @return true si la contrainte est apprise, false sinon
172      * @see org.sat4j.specs.IConstr#learnt()
173      */
174     @Override
175     public boolean learnt() {
176         return this.learnt;
177     }
178 
179     @Override
180     public void setLearnt() {
181         this.learnt = true;
182     }
183 
184     @Override
185     public void register() {
186         assert this.learnt;
187         computeWatches();
188     }
189 
190     @Override
191     public void assertConstraint(UnitPropagationListener s) {
192         for (int i = 0; i < size(); i++) {
193             if (getVocabulary().isUnassigned(get(i))) {
194                 boolean ret = s.enqueue(get(i), this);
195                 assert ret;
196             }
197         }
198     }
199 
200     public IVecInt computeAnImpliedClause() {
201         return null;
202     }
203 
204 }