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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use crate::traits::{Delete, EmaIF, LitIF};
use std::fmt;
use std::ops::Neg;
pub type VarId = usize;
pub type ClauseId = u32;
pub const NULL_CLAUSE: ClauseId = 0;
pub type Lit = u32;
pub const NULL_LIT: Lit = 0;
impl LitIF for Lit {
fn from_int(x: i32) -> Lit {
(if x < 0 { -2 * x + 1 } else { 2 * x }) as Lit
}
fn from_var(vi: VarId, p: Lbool) -> Lit {
(vi as Lit) << 1 | ((p == FALSE) as Lit)
}
fn vi(self) -> VarId {
(self >> 1) as VarId
}
fn to_i32(self) -> i32 {
if self % 2 == 0 {
(self >> 1) as i32
} else {
((self >> 1) as i32).neg()
}
}
fn lbool(self) -> Lbool {
(self & 1 == 0) as Lbool
}
fn is_positive(self) -> bool {
self & 1 == 0
}
fn negate(self) -> Lit {
self ^ 1
}
fn to_cid(self) -> ClauseId {
(self as ClauseId) | 0x8000_0000
}
}
pub type Lbool = u8;
pub const FALSE: u8 = 0;
pub const TRUE: u8 = 1;
pub const BOTTOM: u8 = 2;
#[allow(dead_code)]
fn negate_bool(b: Lbool) -> Lbool {
b ^ 1
}
#[derive(Debug)]
pub struct Ema {
val: f64,
cal: f64,
sca: f64,
}
impl EmaIF for Ema {
fn new(s: usize) -> Ema {
Ema {
val: 0.0,
cal: 0.0,
sca: 1.0 / (s as f64),
}
}
fn update(&mut self, x: f64) {
self.val = self.sca * x + (1.0 - self.sca) * self.val;
self.cal = self.sca + (1.0 - self.sca) * self.cal;
}
fn get(&self) -> f64 {
self.val / self.cal
}
}
struct Ema2 {
fast: f64,
slow: f64,
calf: f64,
cals: f64,
fe: f64,
se: f64,
}
impl EmaIF for Ema2 {
fn new(f: usize) -> Ema2 {
Ema2 {
fast: 0.0,
slow: 0.0,
calf: 0.0,
cals: 0.0,
fe: 1.0 / (f as f64),
se: 1.0 / (f as f64),
}
}
fn get(&self) -> f64 {
self.fast / self.calf
}
fn update(&mut self, x: f64) {
self.fast = self.fe * x + (1.0 - self.fe) * self.fast;
self.slow = self.se * x + (1.0 - self.se) * self.slow;
self.calf = self.fe + (1.0 - self.fe) * self.calf;
self.cals = self.se + (1.0 - self.se) * self.cals;
}
fn reset(&mut self) {
self.slow = self.fast;
self.cals = self.calf;
}
}
impl Ema2 {
#[allow(dead_code)]
fn rate(&self) -> f64 {
self.fast / self.slow * (self.cals / self.calf)
}
#[allow(dead_code)]
fn with_slow(mut self, s: u64) -> Ema2 {
self.se = 1.0 / (s as f64);
self
}
}
pub enum SolverError {
Inconsistent,
}
pub type MaybeInconsistent = Result<(), SolverError>;
#[derive(Clone, Debug)]
pub struct CNFDescription {
pub num_of_variables: usize,
pub num_of_clauses: usize,
pub pathname: String,
}
impl Default for CNFDescription {
fn default() -> CNFDescription {
CNFDescription {
num_of_variables: 0,
num_of_clauses: 0,
pathname: "".to_string(),
}
}
}
impl fmt::Display for CNFDescription {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let CNFDescription {
num_of_variables: nv,
num_of_clauses: nc,
pathname: path,
} = &self;
write!(f, "CNF({}, {}, {})", nv, nc, path)
}
}
pub fn vec2int(v: &[Lit]) -> Vec<i32> {
v.iter()
.map(|l| match l {
0 => 0,
1 => 0,
x => x.to_i32(),
})
.collect::<Vec<i32>>()
}
impl<T> Delete<T> for Vec<T> {
fn delete_unstable<F>(&mut self, mut filter: F)
where
F: FnMut(&T) -> bool,
{
let mut i = 0;
while i != self.len() {
if filter(&mut self[i]) {
self.swap_remove(i);
break;
} else {
i += 1;
}
}
}
}
bitflags! {
pub struct Flag: u16 {
const DEAD = 0b0000_0000_0000_0001;
const LEARNT = 0b0000_0000_0000_0010;
const JUST_USED = 0b0000_0000_0000_0100;
const OCCUR_LINKED = 0b0000_0000_0000_1000;
const ENQUEUED = 0b0000_0000_0001_0000;
const ELIMINATED = 0b0000_0000_0010_0000;
const TOUCHED = 0b0000_0000_0100_0000;
}
}