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.minisat.orders;
31  
32  import java.io.PrintWriter;
33  import java.io.Serializable;
34  import java.util.Random;
35  
36  import org.sat4j.minisat.core.ILits;
37  import org.sat4j.minisat.core.IOrder;
38  import org.sat4j.minisat.core.IPhaseSelectionStrategy;
39  
40  /**
41   * @since 2.2
42   */
43  public class RandomWalkDecorator implements IOrder, Serializable {
44  
45      /**
46       * 
47       */
48      private static final long serialVersionUID = 1L;
49  
50      private final VarOrderHeap decorated;
51  
52      private double p;
53  
54      private static final Random RAND = new Random(123456789);
55      private ILits voc;
56      private int nbRandomWalks;
57  
58      public RandomWalkDecorator(VarOrderHeap order) {
59          this(order, 0.01);
60      }
61  
62      public RandomWalkDecorator(VarOrderHeap order, double p) {
63          this.decorated = order;
64          this.p = p;
65      }
66  
67      public void assignLiteral(int q) {
68          this.decorated.assignLiteral(q);
69      }
70  
71      public IPhaseSelectionStrategy getPhaseSelectionStrategy() {
72          return this.decorated.getPhaseSelectionStrategy();
73      }
74  
75      public double getProbability() {
76          return this.p;
77      }
78  
79      public void setProbability(double p) {
80          this.p = p;
81      }
82  
83      public void init() {
84          this.decorated.init();
85      }
86  
87      public void printStat(PrintWriter out, String prefix) {
88          out.println(prefix + "random walks\t: " + this.nbRandomWalks);
89          this.decorated.printStat(out, prefix);
90      }
91  
92      public int select() {
93          if (RAND.nextDouble() < this.p) {
94              int var, lit, max;
95  
96              while (!this.decorated.heap.empty()) {
97                  max = this.decorated.heap.size();
98                  var = this.decorated.heap.get(RAND.nextInt(max) + 1);
99                  lit = getPhaseSelectionStrategy().select(var);
100                 if (this.voc.isUnassigned(lit)) {
101                     this.nbRandomWalks++;
102                     return lit;
103                 }
104             }
105         }
106         return this.decorated.select();
107     }
108 
109     public void setLits(ILits lits) {
110         this.decorated.setLits(lits);
111         this.voc = lits;
112         this.nbRandomWalks = 0;
113     }
114 
115     public void setPhaseSelectionStrategy(IPhaseSelectionStrategy strategy) {
116         this.decorated.setPhaseSelectionStrategy(strategy);
117     }
118 
119     public void setVarDecay(double d) {
120         this.decorated.setVarDecay(d);
121     }
122 
123     public void undo(int x) {
124         this.decorated.undo(x);
125     }
126 
127     public void updateVar(int q) {
128         this.decorated.updateVar(q);
129     }
130 
131     public double varActivity(int q) {
132         return this.decorated.varActivity(q);
133     }
134 
135     public void varDecayActivity() {
136         this.decorated.varDecayActivity();
137     }
138 
139     public void updateVarAtDecisionLevel(int q) {
140         this.decorated.updateVarAtDecisionLevel(q);
141     }
142 
143     @Override
144     public String toString() {
145         return this.decorated.toString() + " with random walks " + this.p;
146     }
147 
148     public double[] getVariableHeuristics() {
149         return this.decorated.getVariableHeuristics();
150     }
151 
152 }