View Javadoc

1   /*******************************************************************************
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2008 Daniel Le Berre
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 pseudo boolean algorithms described in:
20   * A fast pseudo-Boolean constraint solver Chai, D.; Kuehlmann, A.
21   * Computer-Aided Design of Integrated Circuits and Systems, IEEE Transactions on
22   * Volume 24, Issue 3, March 2005 Page(s): 305 - 317
23   * 
24   * and 
25   * Heidi E. Dixon, 2004. Automating Pseudo-Boolean Inference within a DPLL 
26   * Framework. Ph.D. Dissertation, University of Oregon.
27   *******************************************************************************/
28  package org.sat4j.pb.core;
29  
30  import org.sat4j.core.Vec;
31  import org.sat4j.minisat.core.AssertingClauseGenerator;
32  import org.sat4j.minisat.core.Constr;
33  import org.sat4j.minisat.core.IOrder;
34  import org.sat4j.minisat.core.LearningStrategy;
35  import org.sat4j.minisat.core.Pair;
36  import org.sat4j.minisat.core.RestartStrategy;
37  import org.sat4j.minisat.core.SearchParams;
38  import org.sat4j.minisat.restarts.MiniSATRestarts;
39  import org.sat4j.pb.constraints.pb.ConflictMap;
40  import org.sat4j.pb.constraints.pb.IConflict;
41  import org.sat4j.pb.constraints.pb.PBConstr;
42  import org.sat4j.specs.IVec;
43  
44  /**
45   * @author parrain To change the template for this generated type comment go to
46   *         Window - Preferences - Java - Code Generation - Code and Comments
47   */
48  public class PBSolverCP extends PBSolver {
49  
50  	private static final long serialVersionUID = 1L;
51  
52  	/**
53  	 * @param acg
54  	 * @param learner
55  	 * @param dsf
56  	 */
57  	public PBSolverCP(AssertingClauseGenerator acg,
58  			LearningStrategy<PBDataStructureFactory> learner,
59  			PBDataStructureFactory dsf, IOrder order) {
60  		super(acg, learner, dsf, new SearchParams(1.5, 100), order,
61  				new MiniSATRestarts());
62  	}
63  
64  	public PBSolverCP(AssertingClauseGenerator acg,
65  			LearningStrategy<PBDataStructureFactory> learner,
66  			PBDataStructureFactory dsf, SearchParams params, IOrder order,
67  			RestartStrategy restarter) {
68  		super(acg, learner, dsf, params, order, restarter);
69  	}
70  
71  	public PBSolverCP(AssertingClauseGenerator acg,
72  			LearningStrategy<PBDataStructureFactory> learner,
73  			PBDataStructureFactory dsf, SearchParams params, IOrder order) {
74  		super(acg, learner, dsf, params, order, new MiniSATRestarts());
75  	}
76  
77  	@Override
78  	public void analyze(Constr myconfl, Pair results) {
79  		int litImplied = trail.last();
80  		int currentLevel = voc.getLevel(litImplied);
81  		IConflict confl = chooseConflict((PBConstr) myconfl, currentLevel);
82  		assert confl.slackConflict().signum() < 0;
83  		while (!confl.isAssertive(currentLevel)) {
84  			PBConstr constraint = (PBConstr) voc.getReason(litImplied);
85  			// result of the resolution is in the conflict (confl)
86  			confl.resolve(constraint, litImplied, this);
87  			assert confl.slackConflict().signum() <= 0;
88  			// implication trail is reduced
89  			if (trail.size() == 1)
90  				break;
91  			undoOne();
92  			// assert decisionLevel() >= 0;
93  			if (decisionLevel() == 0)
94  				break;
95  			litImplied = trail.last();
96  			if (voc.getLevel(litImplied) != currentLevel) {
97  				trailLim.pop();
98  				confl.updateSlack(voc.getLevel(litImplied));
99  			}
100 			assert voc.getLevel(litImplied) <= currentLevel;
101 			currentLevel = voc.getLevel(litImplied);
102 			assert confl.slackIsCorrect(currentLevel);
103 			assert currentLevel == decisionLevel();
104 			assert litImplied > 1;
105 		}
106 		assert confl.isAssertive(currentLevel) || trail.size() == 1
107 				|| decisionLevel() == 0;
108 
109 		assert currentLevel == decisionLevel();
110 		undoOne();
111 
112 		// necessary informations to build a PB-constraint
113 		// are kept from the conflict
114 		if ((confl.size() == 0)
115 				|| ((decisionLevel() == 0 || trail.size() == 0) && confl
116 						.slackConflict().signum() < 0)) {
117 			results.reason = null;
118 			results.backtrackLevel = -1;
119 			return;
120 		}
121 
122 		// assertive PB-constraint is build and referenced
123 		PBConstr resConstr = (PBConstr) dsfactory
124 				.createUnregisteredPseudoBooleanConstraint(confl);
125 
126 		results.reason = resConstr;
127 
128 		// the conflict give the highest decision level for the backtrack
129 		// (which is less than current level)
130 		// assert confl.isAssertive(currentLevel);
131 		if (decisionLevel() == 0 || trail.size() == 0)
132 			results.backtrackLevel = -1;
133 		else
134 			results.backtrackLevel = confl.getBacktrackLevel(currentLevel);
135 	}
136 
137 	IConflict chooseConflict(PBConstr myconfl, int level) {
138 		return ConflictMap.createConflict(myconfl, level);
139 	}
140 
141 	@Override
142 	public String toString(String prefix) {
143 		return prefix + "Cutting planes based inference ("
144 				+ this.getClass().getName() + ")\n" + super.toString(prefix);
145 	}
146 
147 	private final IVec<String> conflictVariables = new Vec<String>();
148 	private final IVec<String> conflictConstraints = new Vec<String>();
149 
150 	void initExplanation() {
151 		conflictVariables.clear();
152 		conflictConstraints.clear();
153 	}
154 }