package kappa-library

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file counter.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
(******************************************************************************)
(*  _  __ * The Kappa Language                                                *)
(* | |/ / * Copyright 2010-2020 CNRS - Harvard Medical School - INRIA - IRIF  *)
(* | ' /  *********************************************************************)
(* | . \  * This file is distributed under the terms of the                   *)
(* |_|\_\ * GNU Lesser General Public License Version 3                       *)
(******************************************************************************)

module Efficiency : sig
  type t = {
    consecutive: int array;
    mutable consecutive_blocked: int;
    mutable no_more_binary: int;
    mutable no_more_unary: int;
    mutable clashing_instance: int;
    mutable time_correction: int;
  }

  val init : int -> t
  val nb : t -> int
  val nb_consecutive : rule_id:int -> t -> int
  val nb_consecutive_blocked : t -> int
  val print_detail : current_event:int -> Format.formatter -> t -> unit
  val reset_consecutive : rule_id:int -> t -> t
  val reset_consecutive_blocked : t -> t
  val incr_no_more_binary : rule_id:int -> t -> t
  val incr_no_more_unary : rule_id:int -> t -> t
  val incr_clashing_instance : rule_id:int -> t -> t
  val incr_time_correction : t -> t
  val incr_consecutive_blocked : t -> t
  val write_t : Buffer.t -> t -> unit
  val string_of_t : ?len:int -> t -> string
  val read_t : Yojson.Safe.lexer_state -> Lexing.lexbuf -> t
  val t_of_string : string -> t
end = struct
  type t = {
    consecutive: int array;
    mutable consecutive_blocked: int;
    mutable no_more_binary: int;
    mutable no_more_unary: int;
    mutable clashing_instance: int;
    mutable time_correction: int;
  }

  let init size =
    {
      consecutive = Array.make size 0;
      consecutive_blocked = 0;
      no_more_binary = 0;
      no_more_unary = 0;
      clashing_instance = 0;
      time_correction = 0;
    }

  let nb t =
    t.no_more_binary + t.no_more_unary + t.clashing_instance + t.time_correction

  let nb_consecutive ~rule_id t = t.consecutive.(rule_id)
  let nb_consecutive_blocked t = t.consecutive_blocked

  let reset_consecutive ~rule_id t =
    let () = t.consecutive.(rule_id) <- 0 in
    t

  let reset_consecutive_blocked t =
    let () = t.consecutive_blocked <- 0 in
    t

  let incr_consecutive_blocked t =
    let () = t.consecutive_blocked <- succ t.consecutive_blocked in
    t

  let incr_no_more_binary ~rule_id t =
    let () = t.no_more_binary <- succ t.no_more_binary in
    let () = t.consecutive.(rule_id) <- succ t.consecutive.(rule_id) in
    t

  let incr_no_more_unary ~rule_id t =
    let () = t.no_more_unary <- succ t.no_more_unary in
    let () = t.consecutive.(rule_id) <- succ t.consecutive.(rule_id) in
    t

  let incr_clashing_instance ~rule_id t =
    let () = t.clashing_instance <- succ t.clashing_instance in
    let () = t.consecutive.(rule_id) <- succ t.consecutive.(rule_id) in
    t

  let incr_time_correction t =
    let () = t.time_correction <- succ t.time_correction in
    t

  let print_detail ~current_event f t =
    let all = float_of_int (nb t) in
    let events = float_of_int current_event in
    let () = Format.pp_open_vbox f 0 in
    let () =
      if all > 0. then
        Format.fprintf f
          "@[%.2f%% of event loops were productive.@ Null event cause:@]@,"
          (100. *. events /. (all +. events))
    in
    let () =
      if t.no_more_unary > 0 then
        Format.fprintf f
          "\tValid embedding but no longer unary when required: %.2f%%@,"
          (100. *. float_of_int t.no_more_unary /. all)
    in
    let () =
      if t.no_more_binary > 0 then
        Format.fprintf f
          "\tValid embedding but not binary when required: %.2f%%@,"
          (100. *. float_of_int t.no_more_binary /. all)
    in
    let () =
      if t.clashing_instance > 0 then
        Format.fprintf f "\tClashing instance: %.2f%%@,"
          (100. *. float_of_int t.clashing_instance /. all)
    in
    let () =
      if t.time_correction > 0 then
        Format.fprintf f "\tPerturbation interrupting time advance: %.2f%%@,"
          (100. *. float_of_int t.time_correction /. all)
    in
    Format.fprintf f "@]"

  let to_yojson t =
    `Assoc
      [
        "consecutive", JsonUtil.of_array JsonUtil.of_int t.consecutive;
        "consecutive_blocked", `Int t.consecutive_blocked;
        "no_more_binary", `Int t.no_more_binary;
        "no_more_unary", `Int t.no_more_unary;
        "clashing_instance", `Int t.clashing_instance;
        "time_correction", `Int t.time_correction;
      ]

  let of_yojson = function
    | `Assoc l as x when List.length l = 6 ->
      {
        consecutive =
          (JsonUtil.to_array Yojson.Basic.Util.to_int)
            (Yojson.Basic.Util.member "consecutive" x);
        consecutive_blocked =
          Yojson.Basic.Util.to_int
            (Yojson.Basic.Util.member "consecutive_blocked" x);
        no_more_binary =
          Yojson.Basic.Util.to_int (Yojson.Basic.Util.member "no_more_binary" x);
        no_more_unary =
          Yojson.Basic.Util.to_int (Yojson.Basic.Util.member "no_more_unary" x);
        clashing_instance =
          Yojson.Basic.Util.to_int
            (Yojson.Basic.Util.member "clashing_instance" x);
        time_correction =
          Yojson.Basic.Util.to_int
            (Yojson.Basic.Util.member "time_correction" x);
      }
    | x ->
      raise (Yojson.Basic.Util.Type_error ("Invalid simulation efficiency", x))

  let write_t ob f = Yojson.Basic.to_buffer ob (to_yojson f)

  let string_of_t ?(len = 1024) x =
    let ob = Buffer.create len in
    write_t ob x;
    Buffer.contents ob

  let read_t p lb = of_yojson (Yojson.Basic.from_lexbuf ~stream:true p lb)
  let t_of_string s = read_t (Yojson.Safe.init_lexer ()) (Lexing.from_string s)
end

type t = {
  mutable time: float;
  mutable events: int;
  mutable stories: int;
  mutable last_point: int;
  mutable stat_null: Efficiency.t;
  init_time: float;
  init_event: int;
  mutable plot_period: Configuration.period;
  mutable max_time: float option;
  mutable max_event: int option;
}

let current_story c = c.stories
let current_time c = c.time
let current_event c = c.events
let nb_null_event c = Efficiency.nb c.stat_null

let consecutive_null_event ~rule_id c =
  Efficiency.nb_consecutive ~rule_id c.stat_null

let consecutive_blocked c = Efficiency.nb_consecutive_blocked c.stat_null
let inc_stories c = c.stories <- c.stories + 1
let inc_events c = c.events <- c.events + 1

let check_time c =
  match c.max_time with
  | None -> true
  | Some max -> c.time <= max

let check_output_time c ot =
  match c.max_time with
  | None -> true
  | Some max -> ot <= max

let check_events c =
  match c.max_event with
  | None -> true
  | Some max -> c.events < max

let one_time_advance c dt =
  let () = c.time <- c.time +. dt in
  check_time c

let one_constructive_event ~rule_id c =
  let () = c.stat_null <- Efficiency.reset_consecutive ~rule_id c.stat_null in
  let () = c.stat_null <- Efficiency.reset_consecutive_blocked c.stat_null in
  let () = inc_events c in
  check_time c && check_events c

let one_no_more_binary_event ~rule_id c =
  let () = c.stat_null <- Efficiency.incr_no_more_binary ~rule_id c.stat_null in
  check_time c && check_events c

let one_no_more_unary_event ~rule_id c =
  let () = c.stat_null <- Efficiency.incr_no_more_unary ~rule_id c.stat_null in
  check_time c && check_events c

let one_clashing_instance_event ~rule_id c =
  let () =
    c.stat_null <- Efficiency.incr_clashing_instance ~rule_id c.stat_null
  in
  check_time c && check_events c

let one_time_correction_event ?ti c =
  match Option_util.bind Nbr.to_float ti with
  | None -> false
  | Some ti ->
    let () = c.time <- ti in
    let () = c.stat_null <- Efficiency.incr_time_correction c.stat_null in
    check_time c && check_events c

let one_blocked_event c =
  let () = c.stat_null <- Efficiency.incr_consecutive_blocked c.stat_null in
  check_time c && check_events c

let get_efficiency c = c.stat_null

let print_efficiency f c =
  Efficiency.print_detail ~current_event:(current_event c) f c.stat_null

let init_time c = c.init_time
let max_time c = c.max_time
let max_events c = c.max_event
let plot_period c = c.plot_period

let time_ratio t =
  match t.max_time with
  | None -> None
  | Some tmax ->
    if tmax > t.init_time then
      Some ((t.time -. t.init_time) /. (tmax -. t.init_time))
    else
      None

let event_ratio t =
  match t.max_event with
  | None -> None
  | Some emax ->
    if emax = 0 then
      None
    else
      Some
        (float_of_int (t.events - t.init_event)
        /. float_of_int (emax - t.init_event))

let set_max_time c t = c.max_time <- t
let set_max_events c e = c.max_event <- e

let tracked_events (counter : t) : int option =
  if counter.stories >= 0 then
    Some counter.stories
  else
    None

let set_plot_period (t : t) plot_period : unit = t.plot_period <- plot_period

let create ?(init_t = 0.) ?(init_e = 0) ?max_time ?max_event ~plot_period
    ~nb_rules () =
  {
    time = init_t;
    events = init_e;
    stories = -1;
    stat_null = Efficiency.init nb_rules;
    plot_period;
    init_time = init_t;
    init_event = init_e;
    max_time;
    max_event;
    last_point = 0;
  }

let reinitialize counter =
  counter.time <- counter.init_time;
  counter.events <- counter.init_event;
  counter.stories <- -1;
  counter.last_point <- 0;
  counter.stat_null <-
    Efficiency.init (Array.length counter.stat_null.Efficiency.consecutive)

let next_step_simulation_info c =
  {
    Trace.Simulation_info.story_id = current_story c;
    Trace.Simulation_info.story_time = current_time c;
    Trace.Simulation_info.story_event = current_event c + 1;
    Trace.Simulation_info.profiling_info = ();
  }

let current_simulation_info c =
  {
    Trace.Simulation_info.story_id = current_story c;
    Trace.Simulation_info.story_time = current_time c;
    Trace.Simulation_info.story_event = current_event c;
    Trace.Simulation_info.profiling_info = ();
  }

let next_story c =
  let () = inc_stories c in
  next_step_simulation_info c

let positive_plot_period counter =
  match plot_period counter with
  | Configuration.DE e -> e > 0
  | Configuration.DT t -> t > 0.

let next_point counter dt =
  match counter.plot_period with
  | Configuration.DT dT ->
    if dT <= 0. then
      0
    else
      int_of_float
        ((min
            (Option_util.unsome infinity (max_time counter))
            (dt +. current_time counter)
         -. counter.init_time)
        /. dT)
  | Configuration.DE dE ->
    if dE <= 0 then
      0
    else
      (current_event counter - counter.init_event) / dE

let to_plot_points counter dt =
  let next = next_point counter dt in
  let last = counter.last_point in
  let () = counter.last_point <- next in
  let n = next - last in
  match counter.plot_period with
  | Configuration.DT dT ->
    ( snd
        (Tools.recti
           (fun (time, acc) _ ->
             ( time -. dT,
               if check_output_time counter time then
                 time :: acc
               else
                 acc ))
           (counter.init_time +. (float_of_int next *. dT), [])
           n),
      counter )
  | Configuration.DE _ ->
    if n = 1 then
      [ counter.time ], counter
    else if n = 0 then
      [], counter
    else
      invalid_arg
        ("Counter.to_plot_points: invalid increment " ^ string_of_int n)

let fill ~outputs counter ~dt =
  let points, counter' = to_plot_points counter dt in
  List.iter (fun time -> outputs counter' time) points

let fake_time t time = { t with time }
OCaml

Innovation. Community. Security.