summaryrefslogtreecommitdiffstats
path: root/sources/fabrice/simulator/simulGraphes.ml
blob: e179d48a6cffa1966b7428f690ea4e78fc7b95f8 (plain)
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
let int_of_string s =
  try
    int_of_string s
  with e ->
      failwith (Printf.sprintf  "int_of_string: error with [%s]" (String.escaped s))

let on_file filename = 
  let ic = open_in filename in
  let count = ref 0 in
  let mins = ref [||] in
  let maxs = ref [||] in
  (try
      let line = input_line ic in
      incr count;
      let line = String2.split_simplify line ' ' in
      mins := Array.create (List.length line) 0.;
      maxs := Array.create (List.length line) 0.;
      let mins = !mins in
      let maxs = !maxs in
      List2.iteri (fun i x ->
          let x = float_of_string x in
          mins.(i) <- x;
          maxs.(i) <- x) line;
      while true do
        let line = input_line ic in
        incr count;
        let line = String2.split_simplify line ' ' in
        List2.iteri (fun i x ->
            let x = float_of_string x in
            mins.(i) <- min x mins.(i);
            maxs.(i) <- max x maxs.(i)) line;
      done
      
    with End_of_file -> ());
  !count, !mins, !maxs

    
let value_per_time x_of_y filename int_table element =
  Printf.printf "%s...\n%!" filename;
  let oc = open_out (Printf.sprintf  "%s.txt" filename) in
  let maximum = ref int_table.(0) in
  let nrounds = Array.length int_table in  
  for round = 0 to nrounds - 1 do
    maximum := max !maximum int_table.(round);
    output_string oc (Printf.sprintf "%d %s\n" round
      (x_of_y int_table.(round)))
  done;
  close_out oc;

  let oc = open_out (Printf.sprintf "%s.plot" filename) in
  output_string oc (Printf.sprintf "
set out '%s.eps'
set terminal postscript eps enhanced \"Helvetica\" 14 
set out '%s.eps'
set yrange [0:%s+1]
set xrange [0:%d]
set ylabel 'Number of %s'
set xlabel 'Time (minutes)'

set data style lines
      
plot \"%s.txt\" using ($1):($2) title '%s' 
  " filename filename (x_of_y !maximum) nrounds element filename element);
  close_out oc



  let npoints = 1000
  
let cdf_of_list x_of_y filename int_list options xlabel ylabel end_plot =
  Printf.printf "%s...\n%!" filename;
  match int_list with
    [] -> Printf.printf "  not enough data\n"
  | init :: _ ->
      let int_list = List.sort compare int_list in
      let nints = List.length int_list in
      
      let oc = open_out (Printf.sprintf  "%s.txt" filename) in
      let maximum = ref init in
      List2.iteri (fun i x ->
          maximum := max !maximum  x;
          output_string oc (Printf.sprintf "%d %s\n" i 
            (x_of_y x))
      ) int_list;
      close_out oc;

      let every = 
        if nints > npoints then 
          Printf.sprintf " every %d " (nints / npoints) else "" in
      
    let oc = open_out (Printf.sprintf "%s.plot" filename) in
  output_string oc (Printf.sprintf "
set out '%s.eps'
set terminal postscript eps enhanced \"Helvetica\" 14 
set out '%s.eps'
set yrange [1:%s+1]
set xrange [0:%d]
set ylabel '%s'
set xlabel '%s'
set data style lines
      
%s

plot \"%s.txt\" %s using ($1):($2) %s
  " filename filename (x_of_y !maximum) (nints+2) ylabel xlabel options filename every end_plot);
  close_out oc

      
let for_every every filename =
  let ic = open_in filename in
  let nlines = ref 0 in
  let line = ref "" in
  try
    while true do
      line := input_line ic;
      incr nlines
    done
  with End_of_file ->
      close_in ic;
      if !nlines > 0 then        
        let x = (!nlines - 1) mod every in
        if x > 0 then
        let oc = open_out_gen [Open_append] 0o644 filename in       
        for i = x to every-1 do 
          Printf.printf "adding line\n";
          Printf.fprintf oc "%s\n" !line
        done;
        close_out oc