Skip to content

Commit bc355c2

Browse files
committed
added all experiments code and neatened definitions
1 parent 6215cea commit bc355c2

File tree

4 files changed

+132
-115
lines changed

4 files changed

+132
-115
lines changed

data/plt1_0inst_is.dat

-96
This file was deleted.

main.ml

+123-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
[@@@ocaml "-33"]
22
open Core
33
(* open Generalized_gpw *)
4-
open Majority_gpw
54
(* open Gpw *)
65
open Utils
76
open Gnuplot
87

98
module Spec : Majority_alg.SPEC = struct
109

11-
let constant_Maj_B = 30
10+
let constant_Maj_B = 10
1211

13-
let constant_2D_A = 5
12+
let constant_2D_A = 10
1413

1514
end
1615

17-
module MajoritySampler = Majority_alg.MajoritySamplingAlgorithm (Spec) (MajorityGPW)
16+
module MajoritySampler = Majority_alg.MajoritySamplingAlgorithm (Spec) (Majority_gpw.MajorityGPW)
1817

1918

2019
let plot_output x =
@@ -32,9 +31,9 @@ let print_output ~f ~filename data =
3231

3332

3433

35-
let () =
34+
let generate_input_tests () =
3635
i_to_j 4 100 |> List.map ~f:(fun size ->
37-
let instance = MajorityGPW.generate_true_instance size in
36+
let instance = Majority_gpw.MajorityGPW.generate_true_instance size in
3837
let count = 10 in
3938
Float.of_int size, i_to_j 0 count |> List.map ~f:(fun _ ->
4039
let (result, _) = MajoritySampler.evaluate instance in
@@ -48,7 +47,7 @@ let () =
4847
~filename:"data/plt1_1inst_is.dat";
4948

5049
i_to_j 4 100 |> List.map ~f:(fun size ->
51-
let instance = MajorityGPW.generate_false_instance size in
50+
let instance = Majority_gpw.MajorityGPW.generate_false_instance size in
5251
let count = 10 in
5352
Float.of_int size, i_to_j 0 count |> List.map ~f:(fun _ ->
5453
let (result, _) = MajoritySampler.evaluate instance in
@@ -61,11 +60,121 @@ let () =
6160
~f:(fun (a,b) -> Printf.sprintf "%2f %2f" a b)
6261
~filename:"data/plt1_0inst_is.dat"
6362

63+
let generate_experiment_1 () =
64+
let size = 20 in
65+
let samples = 100 in
66+
i_to_j 0 10
67+
|> List.map ~f:(fun valid_count ->
68+
let instance =
69+
Majority_gpw.MajorityGPW.generate_false_instance
70+
~options:(Majority_gpw.MajorityGPW.FixedValidCount valid_count) size in
71+
i_to_j 0 samples
72+
|> List.map ~f:begin fun _ ->
73+
fst (MajoritySampler.evaluate instance)
74+
end
75+
|> List.map ~f:(fun v -> if v then 1 else 0)
76+
|> List.fold ~init:0 ~f:(+)
77+
|> fun v -> valid_count, Float.(of_int v / of_int samples)
78+
)
79+
|> print_output
80+
~f:(fun (valid_count,v) -> Printf.sprintf "%d %f" valid_count v )
81+
~filename:"../data/experiment1.dat"
82+
83+
let generate_experiment_2 () =
84+
let samples = 100 in
85+
i_to_j 4 21
86+
|> List.map ~f:(fun input_size ->
87+
let instance =
88+
Majority_gpw.MajorityGPW.generate_true_instance input_size in
89+
i_to_j 0 samples
90+
|> List.map ~f:begin fun _ ->
91+
snd (MajoritySampler.evaluate instance)
92+
end
93+
|> List.fold ~init:0 ~f:(+)
94+
|> fun v -> input_size, Float.(of_int v / of_int samples)
95+
)
96+
|> print_output
97+
~f:(fun (valid_count,v) -> Printf.sprintf "%d %f" valid_count v )
98+
~filename:"../data/experiment2a.dat";
99+
i_to_j 4 21
100+
|> List.map ~f:(fun input_size ->
101+
let instance =
102+
Majority_gpw.MajorityGPW.generate_false_instance input_size in
103+
i_to_j 0 samples
104+
|> List.map ~f:begin fun _ ->
105+
snd (MajoritySampler.evaluate instance)
106+
end
107+
|> List.fold ~init:0 ~f:(+)
108+
|> fun v -> input_size, Float.(of_int v / of_int samples)
109+
)
110+
|> print_output
111+
~f:(fun (valid_count,v) -> Printf.sprintf "%d %f" valid_count v )
112+
~filename:"../data/experiment2b.dat"
113+
114+
115+
let generate_experiment_3 () =
116+
let samples = 100 in
117+
i_to_j 4 31
118+
|> List.map ~f:(fun input_size ->
119+
i_to_j 0 samples
120+
|> List.concat_map ~f:begin fun _ ->
121+
let instance = Majority_gpw.MajorityGPW.generate_true_instance input_size in
122+
i_to_j 0 10
123+
|> List.map ~f:(fun _ -> snd (MajoritySampler.evaluate instance))
124+
end
125+
|> List.fold ~init:0 ~f:(+)
126+
|> fun v -> input_size, Float.(of_int v / of_int samples)
127+
)
128+
|> print_output
129+
~f:(fun (valid_count,v) -> Printf.sprintf "%d %f" valid_count v )
130+
~filename:"../data/experiment3a.dat";
131+
i_to_j 4 31
132+
|> List.map ~f:(fun input_size ->
133+
i_to_j 0 samples
134+
|> List.concat_map ~f:begin fun _ ->
135+
let instance = Majority_gpw.MajorityGPW.generate_true_instance
136+
~options:(Majority_gpw.MajorityGPW.RandomPointers) input_size in
137+
i_to_j 0 10
138+
|> List.map ~f:(fun _ -> snd (MajoritySampler.evaluate instance))
139+
end
140+
|> List.fold ~init:0 ~f:(+)
141+
|> fun v -> input_size, Float.(of_int v / of_int samples)
142+
)
143+
|> print_output
144+
~f:(fun (valid_count,v) -> Printf.sprintf "%d %f" valid_count v )
145+
~filename:"../data/experiment3b.dat";
146+
i_to_j 4 31
147+
|> List.map ~f:(fun input_size ->
148+
i_to_j 0 samples
149+
|> List.concat_map ~f:begin fun _ ->
150+
let instance = Majority_gpw.MajorityGPW.generate_false_instance
151+
~options:(Majority_gpw.MajorityGPW.RandomPointers) input_size in
152+
i_to_j 0 10
153+
|> List.map ~f:(fun _ -> snd (MajoritySampler.evaluate instance))
154+
end
155+
|> List.fold ~init:0 ~f:(+)
156+
|> fun v -> input_size, Float.(of_int v / of_int samples)
157+
)
158+
|> print_output
159+
~f:(fun (valid_count,v) -> Printf.sprintf "%d %f" valid_count v )
160+
~filename:"../data/experiment3c.dat";
161+
i_to_j 4 31
162+
|> List.map ~f:(fun input_size ->
163+
i_to_j 0 samples
164+
|> List.concat_map ~f:begin fun _ ->
165+
let instance = Majority_gpw.MajorityGPW.generate_false_instance
166+
~options:(Majority_gpw.MajorityGPW.RandomPointers) input_size in
167+
i_to_j 0 10
168+
|> List.map ~f:(fun _ -> snd (MajoritySampler.evaluate instance))
169+
end
170+
|> List.fold ~init:0 ~f:(+)
171+
|> fun v -> input_size, Float.(of_int v / of_int samples)
172+
)
173+
|> print_output
174+
~f:(fun (valid_count,v) -> Printf.sprintf "%d %f" valid_count v )
175+
~filename:"../data/experiment3d.dat"
64176

65-
66-
(* List.iter ~f:(fun (size,no_false_positives) ->
67-
* Printf.printf "%3d: %3f\n" size no_false_positives
68-
* ) *)
69-
70-
71-
177+
let () =
178+
generate_experiment_1 ();
179+
generate_experiment_2 ();
180+
generate_experiment_3 ()

majority_alg.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module MajoritySamplingAlgorithm (Spec: SPEC) (Function: FUNCTION with type p =
4343
let threshhold =
4444
let open Float in
4545
let no_samples = of_int no_samples in
46-
to_int (no_samples / 2.0 - sqrt (no_samples)) in
46+
to_int (no_samples / 2.0 - sqrt (no_samples)) |> fun x -> Int.(x + 1) in
4747
let count = ref 0 in
4848
(* muks et al's algorithm for sampling *)
4949
let validate_plane instance =
@@ -138,6 +138,6 @@ module MajoritySamplingAlgorithm (Spec: SPEC) (Function: FUNCTION with type p =
138138
end
139139
done;
140140
(* if fewer than this threshold of 1-instances then 0-instance (1/3 prob of incorrect) *)
141-
(if !count <= threshhold then false else true), !query_count
141+
(if !count < threshhold then false else true), !query_count
142142

143143
end

majority_gpw.ml

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module MajorityGPW = struct
66

77
type p = int
88

9-
type o = FakePointers | RandomPointers
9+
type o = FakePointers | RandomPointers | FixedValidCount of int | RandomValidCount
1010

1111
let get_params (_, n) = n
1212

@@ -147,8 +147,12 @@ module MajorityGPW = struct
147147
done;
148148
done;
149149
let set = ref (Set.empty (module Int)) in
150-
let bound = (n / 2 - 1 ) in
151-
while Set.length !set <= bound do
150+
let bound = match _options with
151+
| Some RandomValidCount -> Random.int (n / 2 - 1)
152+
| Some (FixedValidCount n) -> n
153+
| _ -> (n / 2 - 1)
154+
in
155+
while Set.length !set < bound do
152156
let index = Random.int n in
153157
set := (Set.add !set index)
154158
done;

0 commit comments

Comments
 (0)