| 
 1 | 
  
 |  | 
| 
 2 | 
  
 |  | 
| 
 3 | 
  
 |  | 
| 
 4 | 
  
 |  | 
| 
 5 | 
  
 |  | 
| 
 6 | 
  
 |  | 
| 
 7 | 
  
 | package org.sat4j.opt; | 
| 
 8 | 
  
 |  | 
| 
 9 | 
  
 | import java.math.BigInteger; | 
| 
 10 | 
  
 |  | 
| 
 11 | 
  
 | import org.sat4j.core.Vec; | 
| 
 12 | 
  
 | import org.sat4j.core.VecInt; | 
| 
 13 | 
  
 | import org.sat4j.specs.ContradictionException; | 
| 
 14 | 
  
 | import org.sat4j.specs.IConstr; | 
| 
 15 | 
  
 | import org.sat4j.specs.IOptimizationProblem; | 
| 
 16 | 
  
 | import org.sat4j.specs.ISolver; | 
| 
 17 | 
  
 | import org.sat4j.specs.IVec; | 
| 
 18 | 
  
 | import org.sat4j.specs.IVecInt; | 
| 
 19 | 
  
 | import org.sat4j.specs.TimeoutException; | 
| 
 20 | 
  
 | import org.sat4j.tools.SolverDecorator; | 
| 
 21 | 
  
 |  | 
| 
 22 | 
  
 | public class WeightedMaxSatDecorator extends SolverDecorator implements | 
| 
 23 | 
  
 |         IOptimizationProblem { | 
| 
 24 | 
  
 |  | 
| 
 25 | 
  
 |      | 
| 
 26 | 
  
 |  | 
| 
 27 | 
  
 |  | 
| 
 28 | 
  
 |     private static final long serialVersionUID = 1L; | 
| 
 29 | 
  
 |  | 
| 
 30 | 
  
 |     private int nborigvars; | 
| 
 31 | 
  
 |  | 
| 
 32 | 
  
 |     private int nbexpectedclauses; | 
| 
 33 | 
  
 |  | 
| 
 34 | 
  
 |     private int nbnewvar; | 
| 
 35 | 
  
 |  | 
| 
 36 | 
  
 |     private final IVec<BigInteger> coefs = new Vec<BigInteger>(); | 
| 
 37 | 
  
 |  | 
| 
 38 | 
  
 |     private int[] prevfullmodel; | 
| 
 39 | 
  
 |  | 
| 
 40 | 
 0
 |     public WeightedMaxSatDecorator(ISolver solver) {
 | 
| 
 41 | 
 0
 |         super(solver);
 | 
| 
 42 | 
  
 |     } | 
| 
 43 | 
  
 |  | 
| 
 44 | 
 0
 |     @Override
 | 
| 
 45 | 
  
 |     public int[] model() { | 
| 
 46 | 
 0
 |         prevfullmodel = super.model();
 | 
| 
 47 | 
 0
 |         int[] shortmodel = new int[nborigvars];
 | 
| 
 48 | 
 0
 |         for (int i = 0; i < nborigvars; i++) {
 | 
| 
 49 | 
 0
 |             shortmodel[i] = prevfullmodel[i];
 | 
| 
 50 | 
  
 |         } | 
| 
 51 | 
 0
 |         return shortmodel;
 | 
| 
 52 | 
  
 |     } | 
| 
 53 | 
  
 |  | 
| 
 54 | 
 0
 |     @Override
 | 
| 
 55 | 
  
 |     public int newVar(int howmany) { | 
| 
 56 | 
 0
 |         nborigvars = super.newVar(howmany);
 | 
| 
 57 | 
 0
 |         return nborigvars;
 | 
| 
 58 | 
  
 |     } | 
| 
 59 | 
  
 |  | 
| 
 60 | 
 0
 |     @Override
 | 
| 
 61 | 
  
 |     public void setExpectedNumberOfClauses(int nb) { | 
| 
 62 | 
 0
 |         nbexpectedclauses = nb;
 | 
| 
 63 | 
 0
 |         super.setExpectedNumberOfClauses(nb);
 | 
| 
 64 | 
 0
 |         super.newVar(nborigvars + nbexpectedclauses);
 | 
| 
 65 | 
  
 |     } | 
| 
 66 | 
  
 |  | 
| 
 67 | 
 0
 |     @Override
 | 
| 
 68 | 
  
 |     public IConstr addClause(IVecInt literals) throws ContradictionException { | 
| 
 69 | 
  
 |  | 
| 
 70 | 
 0
 |         coefs.push(BigInteger.valueOf(literals.get(0)));
 | 
| 
 71 | 
 0
 |         literals.set(0, nborigvars + ++nbnewvar);
 | 
| 
 72 | 
 0
 |         return super.addClause(literals);
 | 
| 
 73 | 
  
 |     } | 
| 
 74 | 
  
 |  | 
| 
 75 | 
 0
 |     @Override
 | 
| 
 76 | 
  
 |     public void reset() { | 
| 
 77 | 
 0
 |         nbnewvar = 0;
 | 
| 
 78 | 
 0
 |         coefs.clear();
 | 
| 
 79 | 
 0
 |         vec.clear();
 | 
| 
 80 | 
 0
 |         super.reset();
 | 
| 
 81 | 
  
 |     } | 
| 
 82 | 
  
 |  | 
| 
 83 | 
 0
 |     public boolean admitABetterSolution() throws TimeoutException {
 | 
| 
 84 | 
 0
 |         boolean result = super.isSatisfiable();
 | 
| 
 85 | 
 0
 |         if (result)
 | 
| 
 86 | 
 0
 |             prevfullmodel = super.model();
 | 
| 
 87 | 
 0
 |         return result;
 | 
| 
 88 | 
  
 |     } | 
| 
 89 | 
  
 |  | 
| 
 90 | 
 0
 |     public boolean hasNoObjectiveFunction() {
 | 
| 
 91 | 
 0
 |         return false;
 | 
| 
 92 | 
  
 |     } | 
| 
 93 | 
  
 |  | 
| 
 94 | 
 0
 |     public boolean nonOptimalMeansSatisfiable() {
 | 
| 
 95 | 
 0
 |         return false;
 | 
| 
 96 | 
  
 |     } | 
| 
 97 | 
  
 |  | 
| 
 98 | 
  
 |     private BigInteger counter; | 
| 
 99 | 
  
 |  | 
| 
 100 | 
 0
 |     public Number calculateObjective() {
 | 
| 
 101 | 
 0
 |         counter = BigInteger.ZERO;
 | 
| 
 102 | 
 0
 |         for (int q : prevfullmodel) {
 | 
| 
 103 | 
 0
 |             if (q > nborigvars) {
 | 
| 
 104 | 
 0
 |                 counter = counter.add(coefs.get(q - nborigvars - 1));
 | 
| 
 105 | 
  
 |             } | 
| 
 106 | 
  
 |         } | 
| 
 107 | 
 0
 |         return counter;
 | 
| 
 108 | 
  
 |     } | 
| 
 109 | 
  
 |  | 
| 
 110 | 
  
 |     private final IVecInt vec = new VecInt(); | 
| 
 111 | 
  
 |  | 
| 
 112 | 
 0
 |     public void discard() throws ContradictionException {
 | 
| 
 113 | 
 0
 |         if (vec.isEmpty()) {
 | 
| 
 114 | 
 0
 |             for (int i = nborigvars + 1; i <= nVars(); i++) {
 | 
| 
 115 | 
 0
 |                 vec.push(i);
 | 
| 
 116 | 
  
 |             } | 
| 
 117 | 
  
 |         } | 
| 
 118 | 
 0
 |         super.addPseudoBoolean(vec, coefs, false, counter
 | 
| 
 119 | 
  
 |                 .subtract(BigInteger.ONE)); | 
| 
 120 | 
  
 |     } | 
| 
 121 | 
  
 |  | 
| 
 122 | 
  
 | } |