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 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  *******************************************************************************/
28  package org.sat4j.minisat.orders;
29  
30  import static org.sat4j.core.LiteralsUtils.neg;
31  import static org.sat4j.core.LiteralsUtils.posLit;
32  import static org.sat4j.core.LiteralsUtils.var;
33  
34  import java.util.ArrayList;
35  import java.util.Collections;
36  import java.util.List;
37  
38  import org.sat4j.minisat.core.ILits23;
39  
40  /**
41   * @author leberre Heuristique du prouveur. Changement par rapport au MiniSAT
42   *         original : la gestion activity est faite ici et non plus dans Solver.
43   */
44  public class JWOrder extends VarOrder<ILits23> {
45  
46      private static final long serialVersionUID = 1L;
47  
48      private int computeWeight(int var) {
49          final int p = posLit(var);
50          int pos2 = lits.nBinaryClauses(p);
51          int neg2 = lits.nBinaryClauses(neg(p));
52          int pos3 = lits.nTernaryClauses(p);
53          int neg3 = lits.nTernaryClauses(neg(p));
54          long weight = (pos2 * neg2 * 100L + pos2 + neg2) * 5L + pos3 * neg3 * 10L
55                  + pos3 + neg3;
56          assert weight <= Integer.MAX_VALUE;
57          if (weight == 0) {
58              int pos = lits.attaches(p).size();
59              int neg = lits.attaches(neg(p)).size();
60              weight = pos + neg;
61          }
62          return (int) weight;
63      }
64  
65      /*
66       * (non-Javadoc)
67       * 
68       * @see org.sat4j.minisat.IHeuristics#init()
69       */
70      @Override
71      public void init() {
72          super.init();
73          List<ValuedLit> v = new ArrayList<ValuedLit>(order.length);
74  
75          for (int i = 1; i < order.length; i++) {
76              ValuedLit t = new ValuedLit(order[i],computeWeight(order[i]>> 1));
77              v.add(t);
78          }
79          Collections.sort(v);
80          // System.out.println(v);
81          for (int i = 0; i < v.size(); i++) {
82              ValuedLit t = v.get(i);
83              order[i + 1] = t.id;
84              int index = var(t.id);
85              varpos[index] = i + 1;
86              activity[index] = t.count;
87          }
88          lastVar = 1;
89  
90      }
91  
92      /*
93       * (non-Javadoc)
94       * 
95       * @see org.sat4j.minisat.core.VarOrder#updateActivity(int)
96       */
97      @Override
98      protected void updateActivity(int var) {
99          activity[var] = computeWeight(var);
100     }
101 
102     @Override
103     public String toString() {
104         return "Jeroslow-Wang static like heuristics updated when new clauses are learnt"; //$NON-NLS-1$
105     }
106 }