-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmajority_gpw.ml
209 lines (197 loc) · 6.87 KB
/
majority_gpw.ml
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
open Core
module MajorityGPW = struct
type t = (bool * (int * int * int) option) array * int
type p = int
type o = FakePointers | RandomPointers | FixedValidCount of int | RandomValidCount
let get_params (_, n) = n
let generate_instance ?options:(_ :o option) (n: p) =
let array = Array.create ~len:(n * n * n) (false, None) in
for i = 0 to n - 1 do
for j = 0 to n - 1 do
for k = 0 to n - 1 do
let value = Random.bool () in
let ptr = match Random.bool () with
| true -> None
| false ->
let x = Random.int n in
let y = Random.int n in
let z = Random.int n in
Some (x,y,z)
in
let index = i * n * n + j * n + k in
array.(index) <- (value,ptr)
done
done
done;
array, n
let get_value (array, n) (i,j,k) =
let index = n * n * i + n * j + k in
array.(index)
let to_string ((elements, n): t) : string =
let string = ref "" in
for i = 0 to n - 1 do
for k = 0 to n - 1 do
for j = 0 to n - 1 do
let value : bool = fst @@ get_value (elements, n) (i,j,k) in
let ptr = snd @@ get_value (elements, n) (i,j,k) in
string := !string ^ (Printf.sprintf "%8b" value);
match ptr with
| None -> string := !string ^ "[ NONE ]"
| Some (i,j,k) -> string := !string ^ (Printf.sprintf "(%2d, %2d, %2d)" i j k);
if not (j = n - 1) then string := !string ^ " ";
done;
string := !string ^ "\n";
done;
string := !string ^ "\n========================================\n";
done;
!string
let generate_true_instance ?options:(_options :o option) n =
let array = Array.create ~len:(n * n * n) (false, None) in
let set_value (i,j,k) v =
let index = n * n * i + n * j + k in
let (_, ptr) = array.(index) in
array.(index) <- (v, ptr) in
let set_ptr (i,j,k) ptr =
let index = n * n * i + n * j + k in
let (v, _) = array.(index) in
array.(index) <- (v, ptr) in
for i = 0 to n - 1 do
for j = 0 to n - 1 do
for _ = 0 to n - 2 do
let index = Random.int n in
set_value (i,j, index) true
done
done;
done;
let set = ref (Set.empty (module Int)) in
while Set.length !set <= n / 2 + 1 do
let index = Random.int n in
set := (Set.add !set index)
done;
Set.iter !set ~f:(fun i ->
let principal = Random.int n in
let pointer_prev = ref None in
let seen = ref (Set.empty (module Int)) in
while Set.length !seen < n do
let j = Random.int n in
if Set.mem !seen j then () else begin
begin
match Int.(j = principal) with
| true -> (* setup principal column *)
for k = 0 to n -1 do
set_value (i,j,k) true
done
| false -> (* select base element *)
let k = Random.int n in
for k' = 0 to n - 1 do
match k = k', _options with
| true, _ ->
set_value (i,j,k) false;
set_ptr (i,j,k) !pointer_prev;
pointer_prev := Some (i,j,k)
| _, Some FakePointers ->
let pointer =
let y = Random.int n in
let z = Random.int n in
Some (i,y,z)
in
set_ptr (i,j,k) pointer
| _, Some RandomPointers ->
let pointer =
match Random.bool () with
| true ->
let y = Random.int n in
let z = Random.int n in
Some (i,y,z)
| false -> None
in
set_ptr (i,j,k) pointer
| _, _ -> ()
done;
end;
seen := (Set.add !seen j)
end
done;
(* Connect to the pointer chain *)
let base = Random.int n in
set_ptr (i, principal, base) !pointer_prev
);
array, n
let generate_false_instance ?options:(_options :o option) n =
let array = Array.create ~len:(n * n * n) (false, None) in
let set_value (i,j,k) v =
let index = n * n * i + n * j + k in
let (_, ptr) = array.(index) in
array.(index) <- (v, ptr) in
let set_ptr (i,j,k) ptr =
let index = n * n * i + n * j + k in
let (v, _) = array.(index) in
array.(index) <- (v, ptr) in
for i = 0 to n - 1 do
for j = 0 to n - 1 do
for _ = 0 to n - 2 do
let index = Random.int n in
set_value (i,j, index) true
done
done;
done;
let set = ref (Set.empty (module Int)) in
let bound = match _options with
| Some RandomValidCount -> Random.int (n / 2 - 1)
| Some (FixedValidCount n) -> n
| _ -> (n / 2 - 1)
in
while Set.length !set < bound do
let index = Random.int n in
set := (Set.add !set index)
done;
Set.iter !set ~f:(fun i ->
let principal = Random.int n in
let pointer_prev = ref None in
let seen = ref (Set.empty (module Int)) in
while Set.length !seen < n do
let j = Random.int n in
if Set.mem !seen j then () else begin
begin
match Int.(j = principal) with
| true -> (* setup principal column *)
for k = 0 to n -1 do
set_value (i,j,k) true
done
| false -> (* select base element *)
let k = Random.int n in
for k' = 0 to n - 1 do
match k = k', _options with
| true, _ ->
set_value (i,j,k) false;
set_ptr (i,j,k) !pointer_prev;
pointer_prev := Some (i,j,k)
| _, Some FakePointers ->
let pointer =
let y = Random.int n in
let z = Random.int n in
Some (i,y,z)
in
set_ptr (i,j,k) pointer
| _, Some RandomPointers ->
let pointer =
match Random.bool () with
| true ->
let y = Random.int n in
let z = Random.int n in
Some (i,y,z)
| false -> None
in
set_ptr (i,j,k) pointer
| _, _ -> ()
done
end;
seen := (Set.add !seen j)
end
done;
(* Connect to the pointer chain *)
let base = Random.int n in
set_ptr (i, principal, base) !pointer_prev
);
array, n
end