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.VecInt;
33  import org.sat4j.specs.ContradictionException;
34  import org.sat4j.specs.IConstr;
35  import org.sat4j.specs.IOptimizationProblem;
36  import org.sat4j.specs.ISolver;
37  import org.sat4j.specs.IVecInt;
38  import org.sat4j.specs.TimeoutException;
39  import org.sat4j.tools.SolverDecorator;
40  
41  /**
42   * Computes a solution with the smallest number of satisfied literals.
43   * 
44   * Please make sure that newVar(howmany) is called first to setup the decorator.
45   * 
46   * @author leberre
47   */
48  public final class MinOneDecorator extends SolverDecorator<ISolver> implements
49          IOptimizationProblem {
50  
51      /**
52       * 
53       */
54      private static final long serialVersionUID = 1L;
55  
56      private int[] prevmodel;
57  
58      private boolean isSolutionOptimal;
59  
60      public MinOneDecorator(ISolver solver) {
61          super(solver);
62      }
63  
64      public boolean admitABetterSolution() throws TimeoutException {
65          return admitABetterSolution(VecInt.EMPTY);
66      }
67  
68      /**
69       * @since 2.1
70       */
71      public boolean admitABetterSolution(IVecInt assumps)
72              throws TimeoutException {
73          this.isSolutionOptimal = false;
74          boolean result = isSatisfiable(assumps, true);
75          if (result) {
76              this.prevmodel = super.model();
77              calculateObjectiveValue();
78          } else {
79              this.isSolutionOptimal = true;
80          }
81          return result;
82      }
83  
84      public boolean hasNoObjectiveFunction() {
85          return false;
86      }
87  
88      public boolean nonOptimalMeansSatisfiable() {
89          return true;
90      }
91  
92      private int counter;
93  
94      public Number calculateObjective() {
95          calculateObjectiveValue();
96          return this.counter;
97      }
98  
99      private void calculateObjectiveValue() {
100         this.counter = 0;
101         for (int p : this.prevmodel) {
102             if (p > 0) {
103                 this.counter++;
104             }
105         }
106     }
107 
108     private final IVecInt literals = new VecInt();
109 
110     private IConstr previousConstr;
111 
112     /**
113      * @since 2.1
114      */
115     public void discardCurrentSolution() throws ContradictionException {
116         if (this.literals.isEmpty()) {
117             for (int i = 1; i <= nVars(); i++) {
118                 this.literals.push(i);
119             }
120         }
121         if (this.previousConstr != null) {
122             super.removeConstr(this.previousConstr);
123         }
124         this.previousConstr = addAtMost(this.literals, this.counter - 1);
125     }
126 
127     @Override
128     public int[] model() {
129         // DLB findbugs ok
130         return this.prevmodel;
131     }
132 
133     @Override
134     public void reset() {
135         this.literals.clear();
136         this.previousConstr = null;
137         super.reset();
138     }
139 
140     /**
141      * @since 2.1
142      */
143     public Number getObjectiveValue() {
144         return this.counter;
145     }
146 
147     public void discard() throws ContradictionException {
148         discardCurrentSolution();
149     }
150 
151     /**
152      * @since 2.1
153      */
154     public void forceObjectiveValueTo(Number forcedValue)
155             throws ContradictionException {
156         try {
157             addAtMost(this.literals, forcedValue.intValue());
158         } catch (ContradictionException ce) {
159             this.isSolutionOptimal = true;
160             throw ce;
161         }
162 
163     }
164 
165     public boolean isOptimal() {
166         return this.isSolutionOptimal;
167     }
168 }