View Javadoc

1   /*
2    * SAT4J: a SATisfiability library for Java Copyright (C) 2004-2006 Daniel Le Berre
3    * 
4    * Based on the original minisat specification from:
5    * 
6    * An extensible SAT solver. Niklas E?n and Niklas S?rensson. Proceedings of the
7    * Sixth International Conference on Theory and Applications of Satisfiability
8    * Testing, LNCS 2919, pp 502-518, 2003.
9    * 
10   * This library is free software; you can redistribute it and/or modify it under
11   * the terms of the GNU Lesser General Public License as published by the Free
12   * Software Foundation; either version 2.1 of the License, or (at your option)
13   * any later version.
14   * 
15   * This library is distributed in the hope that it will be useful, but WITHOUT
16   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18   * details.
19   * 
20   * You should have received a copy of the GNU Lesser General Public License
21   * along with this library; if not, write to the Free Software Foundation, Inc.,
22   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23   * 
24   */
25  package org.sat4j.minisat.orders;
26  
27  import static org.sat4j.minisat.core.LiteralsUtils.neg;
28  import static org.sat4j.minisat.core.LiteralsUtils.var;
29  
30  import java.math.BigInteger;
31  
32  import org.sat4j.minisat.core.ILits;
33  import org.sat4j.opt.ObjectiveFunction;
34  import org.sat4j.specs.IVec;
35  import org.sat4j.specs.IVecInt;
36  
37  public class VarOrderHeapObjective extends VarOrderHeap<ILits> {
38  
39      /**
40       * 
41       */
42      private static final long serialVersionUID = 1L;
43  
44      private ObjectiveFunction obj;
45  
46      public void setObjectiveFunction(ObjectiveFunction obj) {
47          this.obj = obj;
48      }
49  
50      @Override
51      public void init() {
52          super.init();
53          if (obj != null) {
54              IVecInt vars = obj.getVars();
55              IVec<BigInteger> coefs = obj.getCoeffs();
56              for (int i = 0; i < vars.size(); i++) {
57                  int p = lits.getFromPool(vars.get(i));
58                  BigInteger c = coefs.get(i);
59                  if (c.signum() < 0) {
60                      p = neg(p);
61                  }
62                  int var = var(p);
63                  activity[var] = c.abs().doubleValue();
64                  if (heap.inHeap(var))
65                      heap.increase(var);
66                  phase[var] = neg(p);
67              }
68          }
69      }
70      
71      @Override
72      public void updateVar(int p) {
73          int var = p >> 1;
74          updateActivity(var);
75          if (heap.inHeap(var))
76              heap.increase(var);
77      }   
78      
79      @Override
80      public void assignLiteral(int p) {
81          phase[p>>1] = p;
82      }
83  }