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.opt;
31  
32  import org.sat4j.core.ConstrGroup;
33  import org.sat4j.core.VecInt;
34  import org.sat4j.specs.ContradictionException;
35  import org.sat4j.specs.IConstr;
36  import org.sat4j.specs.ISolver;
37  import org.sat4j.specs.IVecInt;
38  import org.sat4j.specs.TimeoutException;
39  
40  /**
41   * Computes a solution that satisfies the maximum of clauses.
42   * 
43   * @author daniel
44   * 
45   */
46  public final class MaxSatDecorator extends AbstractSelectorVariablesDecorator {
47  
48      /**
49       * 
50       */
51      private static final long serialVersionUID = 1L;
52  
53      private final boolean equivalence;
54  
55      public MaxSatDecorator(ISolver solver) {
56          this(solver, false);
57      }
58  
59      public MaxSatDecorator(ISolver solver, boolean equivalence) {
60          super(solver);
61          this.equivalence = equivalence;
62      }
63  
64      @Override
65      public void setExpectedNumberOfClauses(int nb) {
66          super.setExpectedNumberOfClauses(nb);
67          this.lits.ensure(nb);
68      }
69  
70      @Override
71      public IConstr addClause(IVecInt literals) throws ContradictionException {
72          int newvar = nextFreeVarId(true);
73          this.lits.push(newvar);
74          literals.push(newvar);
75          if (this.equivalence) {
76              ConstrGroup constrs = new ConstrGroup();
77              constrs.add(super.addClause(literals));
78              IVecInt clause = new VecInt(2);
79              clause.push(-newvar);
80              for (int i = 0; i < literals.size() - 1; i++) {
81                  clause.push(-literals.get(i));
82                  constrs.add(super.addClause(clause));
83              }
84              clause.pop();
85              return constrs;
86          }
87          return super.addClause(literals);
88      }
89  
90      @Override
91      public void reset() {
92          this.lits.clear();
93          super.reset();
94          this.prevConstr = null;
95      }
96  
97      public boolean hasNoObjectiveFunction() {
98          return false;
99      }
100 
101     public boolean nonOptimalMeansSatisfiable() {
102         return false;
103     }
104 
105     public Number calculateObjective() {
106         calculateObjectiveValue();
107         return this.counter;
108     }
109 
110     private final IVecInt lits = new VecInt();
111 
112     private int counter;
113 
114     private IConstr prevConstr;
115 
116     /**
117      * @since 2.1
118      */
119     public void discardCurrentSolution() throws ContradictionException {
120         if (this.prevConstr != null) {
121             super.removeSubsumedConstr(this.prevConstr);
122         }
123         try {
124             this.prevConstr = super.addAtMost(this.lits, this.counter - 1);
125         } catch (ContradictionException ce) {
126             setSolutionOptimal(true);
127             throw ce;
128         }
129     }
130 
131     @Override
132     public boolean admitABetterSolution(IVecInt assumps)
133             throws TimeoutException {
134 
135         boolean result = super.admitABetterSolution(assumps);
136         if (!result && this.prevConstr != null) {
137             super.removeConstr(this.prevConstr);
138             this.prevConstr = null;
139         }
140         return result;
141     }
142 
143     public void discard() throws ContradictionException {
144         discardCurrentSolution();
145     }
146 
147     /**
148      * @since 2.1
149      */
150     public Number getObjectiveValue() {
151         return this.counter;
152     }
153 
154     @Override
155     void calculateObjectiveValue() {
156         this.counter = 0;
157         for (int q : getPrevfullmodel()) {
158             if (q > nVars()) {
159                 this.counter++;
160             }
161         }
162     }
163 
164     /**
165      * @since 2.1
166      */
167     public void forceObjectiveValueTo(Number forcedValue)
168             throws ContradictionException {
169         super.addAtMost(this.lits, forcedValue.intValue());
170     }
171 
172     public void setTimeoutForFindingBetterSolution(int seconds) {
173         // TODO
174         throw new UnsupportedOperationException("No implemented yet");
175     }
176 }