1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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 }