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.tools;
31  
32  import java.math.BigInteger;
33  import java.util.Collection;
34  
35  import org.sat4j.core.Vec;
36  import org.sat4j.core.VecInt;
37  import org.sat4j.pb.OptToPBSATAdapter;
38  import org.sat4j.specs.IVec;
39  import org.sat4j.specs.IVecInt;
40  import org.sat4j.specs.TimeoutException;
41  import org.sat4j.tools.SolutionFoundListener;
42  
43  public abstract class AbstractLexicoHelper<T, C> extends DependencyHelper<T, C>
44          implements SolutionFoundListener {
45  
46      private final LexicoDecoratorPB lexico;
47  
48      public AbstractLexicoHelper(LexicoDecoratorPB lexico) {
49          super(new OptToPBSATAdapter(lexico));
50          this.lexico = lexico;
51          ((OptToPBSATAdapter) getSolver()).setSolutionFoundListener(this);
52      }
53  
54      public AbstractLexicoHelper(LexicoDecoratorPB lexico,
55              boolean explanationEnabled) {
56          super(new OptToPBSATAdapter(lexico), explanationEnabled);
57          this.lexico = lexico;
58          ((OptToPBSATAdapter) getSolver()).setSolutionFoundListener(this);
59      }
60  
61      public AbstractLexicoHelper(LexicoDecoratorPB lexico,
62              boolean explanationEnabled, boolean canonicalOptFunctionEnabled) {
63          super(new OptToPBSATAdapter(lexico), explanationEnabled,
64                  canonicalOptFunctionEnabled);
65          this.lexico = lexico;
66          ((OptToPBSATAdapter) getSolver()).setSolutionFoundListener(this);
67      }
68  
69      private boolean hasASolution;
70  
71      public void addCriterion(Collection<T> things) {
72          IVecInt literals = new VecInt(things.size());
73          for (T thing : things) {
74              literals.push(getIntValue(thing));
75          }
76          this.lexico.addCriterion(literals);
77      }
78  
79      public void addWeightedCriterion(Collection<WeightedObject<T>> things) {
80          IVecInt literals = new VecInt(things.size());
81          IVec<BigInteger> coefs = new Vec<BigInteger>(things.size());
82          for (WeightedObject<T> wo : things) {
83              literals.push(getIntValue(wo.thing));
84              coefs.push(wo.getWeight());
85          }
86          this.lexico.addCriterion(literals, coefs);
87      }
88  
89      /**
90       * 
91       * @return true if the set of constraints entered inside the solver can be
92       *         satisfied.
93       * @throws TimeoutException
94       */
95      @Override
96      public boolean hasASolution() throws TimeoutException {
97          try {
98              return super.hasASolution();
99          } catch (TimeoutException e) {
100             if (this.hasASolution) {
101                 return true;
102             } else {
103                 throw e;
104             }
105         }
106     }
107 
108     /**
109      * 
110      * @return true if the set of constraints entered inside the solver can be
111      *         satisfied.
112      * @throws TimeoutException
113      */
114     @Override
115     public boolean hasASolution(IVec<T> assumps) throws TimeoutException {
116         try {
117             return super.hasASolution(assumps);
118         } catch (TimeoutException e) {
119             if (this.hasASolution) {
120                 return true;
121             } else {
122                 throw e;
123             }
124         }
125     }
126 
127     /**
128      * 
129      * @return true if the set of constraints entered inside the solver can be
130      *         satisfied.
131      * @throws TimeoutException
132      */
133     @Override
134     public boolean hasASolution(Collection<T> assumps) throws TimeoutException {
135         try {
136             return super.hasASolution(assumps);
137         } catch (TimeoutException e) {
138             if (this.hasASolution) {
139                 return true;
140             } else {
141                 throw e;
142             }
143         }
144     }
145 
146     public boolean isOptimal() {
147         return ((OptToPBSATAdapter) getSolver()).isOptimal();
148     }
149 
150     public void onSolutionFound(int[] solution) {
151         this.hasASolution = true;
152     }
153 
154     public void onSolutionFound(IVecInt solution) {
155         this.hasASolution = true;
156 
157     }
158 
159     public void onUnsatTermination() {
160         // nothing to do here
161     }
162 
163 }