-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpossible_boards.rs
More file actions
86 lines (77 loc) · 2.61 KB
/
possible_boards.rs
File metadata and controls
86 lines (77 loc) · 2.61 KB
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
use crate::packed::{array_to_u64, is_possible_board_of_state, get_from_packed_state, u64_to_array};
use crate::csp_constraints::find_possible_boards;
// There are very few boards (low 2 digit number) most of the time,
// like a single digit number of boards, rarely more than 50
// so optimize for small sizes
pub fn accumulate_symbol_weights(
packed_state: u64,
possible_boards: &Vec<u64>,
indices: &Vec<usize>,
index_start: usize,
weights: &Vec<f64>,
) -> [[[f64;4];5];5]
{
// array for storing the probs of a square (row, col) turning out to be one of the four symbols
let mut acc = [[[0.0;4];5];5];
// for each weight group
for index_weight in 0..weights.len()
{
// for each board of that weight
for i in indices[index_start+index_weight]..indices[index_start+index_weight+1]
{
let packed_board = possible_boards[i];
for r in 0..5
{
for c in 0..5
{
// figure out the symbol and add the weight of the state
let symbol = get_from_packed_state(packed_board, r, c);
acc[r][c][symbol] += weights[index_weight];
}
}
}
}
if true
{
let total = acc[0][0][0] + acc[0][0][1] + acc[0][0][2] + acc[0][0][3];
for r in 0..5
{
for c in 0..5
{
for s in 0..4
{
acc[r][c][s] /= total;
}
}
}
}
acc
}
// removes all states without state[row][col] == symbol
pub fn filter_possible_boards_of_next_depth(
packed_state: u64,
possible_boards: &mut Vec<u64>,
indices: &mut Vec<usize>,
index_start: usize,
weights: &Vec<f64>,
) -> ()
{
// truncate because we're adding now!
possible_boards.truncate(indices[index_start+weights.len()]);
// for each weight group
for index_weight in 0..weights.len()
{
// for each board of that depth and weight
for i in indices[index_start+index_weight]..indices[index_start+index_weight+1]
{
let packed_board = possible_boards[i];
if is_possible_board_of_state(packed_board, packed_state) // faster than comparing symbols actually
{
possible_boards.push(packed_board);
}
}
// the index where we would've inserted next is the start of the next group.
// Or the end of the boards of that depth, anyway set the index correctly:
indices[index_start+weights.len()+index_weight+1] = possible_boards.len();
}
}