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.core;
31  
32  import org.sat4j.core.Vec;
33  import org.sat4j.minisat.core.Constr;
34  import org.sat4j.minisat.core.IOrder;
35  import org.sat4j.minisat.core.LearningStrategy;
36  import org.sat4j.minisat.core.Pair;
37  import org.sat4j.minisat.core.RestartStrategy;
38  import org.sat4j.minisat.core.SearchParams;
39  import org.sat4j.minisat.restarts.MiniSATRestarts;
40  import org.sat4j.pb.constraints.pb.ConflictMap;
41  import org.sat4j.pb.constraints.pb.IConflict;
42  import org.sat4j.pb.constraints.pb.PBConstr;
43  import org.sat4j.specs.IVec;
44  import org.sat4j.specs.TimeoutException;
45  
46  /**
47   * @author parrain To change the template for this generated type comment go to
48   *         Window - Preferences - Java - Code Generation - Code and Comments
49   */
50  public class PBSolverCP extends PBSolver {
51  
52      private static final long serialVersionUID = 1L;
53  
54      /**
55       * @param acg
56       * @param learner
57       * @param dsf
58       */
59      public PBSolverCP(LearningStrategy<PBDataStructureFactory> learner,
60              PBDataStructureFactory dsf, IOrder order) {
61          super(learner, dsf, new SearchParams(1.5, 100), order,
62                  new MiniSATRestarts());
63      }
64  
65      public PBSolverCP(LearningStrategy<PBDataStructureFactory> learner,
66              PBDataStructureFactory dsf, SearchParams params, IOrder order,
67              RestartStrategy restarter) {
68          super(learner, dsf, params, order, restarter);
69      }
70  
71      public PBSolverCP(LearningStrategy<PBDataStructureFactory> learner,
72              PBDataStructureFactory dsf, SearchParams params, IOrder order) {
73          super(learner, dsf, params, order, new MiniSATRestarts());
74      }
75  
76      @Override
77      public void analyze(Constr myconfl, Pair results) throws TimeoutException {
78          if (someCriteria()) {
79              analyzeCP(myconfl, results);
80          } else {
81              super.analyze(myconfl, results);
82          }
83      }
84  
85      public void analyzeCP(Constr myconfl, Pair results) throws TimeoutException {
86          int litImplied = this.trail.last();
87          int currentLevel = this.voc.getLevel(litImplied);
88          IConflict confl = chooseConflict((PBConstr) myconfl, currentLevel);
89          assert confl.slackConflict().signum() < 0;
90          while (!confl.isAssertive(currentLevel)) {
91              if (!this.undertimeout) {
92                  throw new TimeoutException();
93              }
94              PBConstr constraint = (PBConstr) this.voc.getReason(litImplied);
95              // result of the resolution is in the conflict (confl)
96              confl.resolve(constraint, litImplied, this);
97              updateNumberOfReductions(confl);
98              assert confl.slackConflict().signum() <= 0;
99              // implication trail is reduced
100             if (this.trail.size() == 1) {
101                 break;
102             }
103             undoOne();
104             // assert decisionLevel() >= 0;
105             if (decisionLevel() == 0) {
106                 break;
107             }
108             litImplied = this.trail.last();
109             if (this.voc.getLevel(litImplied) != currentLevel) {
110                 this.trailLim.pop();
111                 confl.updateSlack(this.voc.getLevel(litImplied));
112             }
113             assert this.voc.getLevel(litImplied) <= currentLevel;
114             currentLevel = this.voc.getLevel(litImplied);
115             assert confl.slackIsCorrect(currentLevel);
116             assert currentLevel == decisionLevel();
117             assert litImplied > 1;
118         }
119         assert confl.isAssertive(currentLevel) || this.trail.size() == 1
120                 || decisionLevel() == 0;
121 
122         assert currentLevel == decisionLevel();
123         undoOne();
124 
125         updateNumberOfReducedLearnedConstraints(confl);
126         // necessary informations to build a PB-constraint
127         // are kept from the conflict
128         if (confl.size() == 0
129                 || (decisionLevel() == 0 || this.trail.size() == 0)
130                 && confl.slackConflict().signum() < 0) {
131             results.reason = null;
132             results.backtrackLevel = -1;
133             return;
134         }
135 
136         // assertive PB-constraint is build and referenced
137         PBConstr resConstr = (PBConstr) this.dsfactory
138                 .createUnregisteredPseudoBooleanConstraint(confl);
139         this.slistener.learn(resConstr);
140         results.reason = resConstr;
141         getSearchListener().learn(resConstr);
142 
143         // the conflict give the highest decision level for the backtrack
144         // (which is less than current level)
145         // assert confl.isAssertive(currentLevel);
146         if (decisionLevel() == 0 || this.trail.size() == 0) {
147             results.backtrackLevel = -1;
148         } else {
149             results.backtrackLevel = confl.getBacktrackLevel(currentLevel);
150         }
151     }
152 
153     IConflict chooseConflict(PBConstr myconfl, int level) {
154         return ConflictMap.createConflict(myconfl, level);
155     }
156 
157     @Override
158     public String toString(String prefix) {
159         return prefix + "Cutting planes based inference ("
160                 + this.getClass().getName() + ")\n" + super.toString(prefix);
161     }
162 
163     private final IVec<String> conflictVariables = new Vec<String>();
164     private final IVec<String> conflictConstraints = new Vec<String>();
165 
166     void initExplanation() {
167         this.conflictVariables.clear();
168         this.conflictConstraints.clear();
169     }
170 
171     boolean someCriteria() {
172         return true;
173     }
174 
175     protected void updateNumberOfReductions(IConflict confl) {
176     }
177 
178     protected void updateNumberOfReducedLearnedConstraints(IConflict confl) {
179     }
180 
181 }