Commit 698a46ae authored by erwan's avatar erwan
Browse files

Chore: put all the seed handling in a dedicated module (so that I can use it in rdbgui)

parent 7b88e1cc
(* Time-stamp: <modified the 02/04/2021 (at 15:45) by Erwan Jahier> *)
(* Time-stamp: <modified the 20/05/2021 (at 09:02) by Erwan Jahier> *)
type t = {
......@@ -9,8 +9,6 @@ type t = {
mutable rif: bool;
mutable no_data_file: bool;
mutable quiet: bool;
mutable seed: int option;
mutable replay_seed: bool;
mutable ifi: bool;
mutable gen_lutin: bool;
mutable gen_oracle: bool;
......@@ -42,8 +40,6 @@ let (make_args : unit -> t) =
rif = false;
no_data_file = false;
quiet = false;
seed = None;
replay_seed = false;
ifi = false;
gen_lutin = false;
gen_oracle = false;
......@@ -95,58 +91,6 @@ let (mkopt : t -> string list -> ?hide:bool -> ?arg:string -> Arg.spec ->
let myexit i = exit i
(*******************************************************************************)
(* seeds stuff *)
let seed_set args s =
(match s with
| Some i ->
if args.verbose>0 then
Printf.fprintf stderr " [sasa] The sasa random engine seed is set to %i\n%!" i;
Random.init i;
flush stderr;
| None -> ()
);
args.seed <- s
let seed_file_name args =
Printf.sprintf "sasa-%s.seed" args.topo
(* for --replay *)
let reset_the_seed_to_last args =
let f = seed_file_name args in
try
let ic = open_in f in
let seed = int_of_string (input_line ic) in
args.seed <- Some seed;
seed_set args (Some seed);
Printf.eprintf " [sasa] Replay the sasa run using the seed in %s\n" f;
flush stderr;
true
with _ ->
Printf.eprintf " [sasa] W: cannot recover the seed in %s\n" f;
flush stderr;
false
let rec seed_get args =
match args.seed with
| Some i -> i
| None ->
(* No seed is set:
- in -replay mode, we first try to read the seed in the seed file
- otherwise, we create a random seed and save if into args, and
into a file for -replay *)
if args.replay_seed && reset_the_seed_to_last args then (seed_get args) else (
let seed = Random.self_init (); Random.int 1073741823 in
let seed_file = seed_file_name args in
let oc = open_out seed_file in
Printf.fprintf oc "%d\n%s\n" seed
(Mypervasives.entete "#" SasaVersion.str SasaVersion.sha);
flush oc;
close_out oc;
seed_set args (Some seed);
seed
)
(*******************************************************************************)
(*** User Options Tab **)
let (mkoptab : string array -> t -> unit) =
......@@ -197,11 +141,11 @@ let (mkoptab : string array -> t -> unit) =
["Do not generate any data file"];
mkopt args ["--seed";"-seed"]
(Arg.Int(fun i -> seed_set args (Some i)))
(Arg.Int(fun i -> Seed.set i))
["Set the pseudo-random generator seed of build-in daemons (wins over --replay)"];
mkopt args ["--replay";"-replay"]
(Arg.Unit(fun () -> args.replay_seed <- true))
(Arg.Unit(fun () -> Seed.replay_seed := true))
["Use the last generated seed to replay the last run"];
mkopt args ~hide:true ["--gen-lutin-daemon";"-gld"]
......
(* Time-stamp: <modified the 22/01/2020 (at 10:02) by Erwan Jahier> *)
(* Time-stamp: <modified the 20/05/2021 (at 09:02) by Erwan Jahier> *)
type t = {
mutable topo: string;
......@@ -8,8 +8,6 @@ type t = {
mutable rif: bool;
mutable no_data_file: bool;
mutable quiet: bool;
mutable seed: int option;
mutable replay_seed: bool;
mutable ifi: bool;
mutable gen_lutin: bool;
mutable gen_oracle: bool;
......@@ -25,7 +23,6 @@ type t = {
mutable _margin : int;
}
val seed_get : t -> int
val usage_msg : string -> string
val parse : string array -> t
(* Time-stamp: <modified the 20/05/2021 (at 09:23) by Erwan Jahier> *)
let seed = ref None
let replay_seed = ref false
let verbose = true
let set s =
if verbose then
Printf.fprintf stderr " [sasa] The sasa random engine seed is set to %i\n%!" s;
Random.init s;
seed := Some s
let seed_file_name label =
Printf.sprintf "sasa-%s.seed" label
(* for --replay *)
let reset_the_seed_to_last label =
let f = seed_file_name label in
try
let ic = open_in f in
let seed = int_of_string (input_line ic) in
set seed;
Printf.eprintf " [sasa] Replay the sasa run using the seed in %s\n" f;
flush stderr;
true
with _ ->
Printf.eprintf " [sasa] W: cannot recover the seed in %s\n" f;
flush stderr;
false
let rec (get : string -> int) =
fun label ->
match !seed with
| Some i -> i
| None ->
(* No seed is set:
- in -replay mode, we first try to read the seed in the seed file
- otherwise, we create a random seed and save if into args, and
into a file for -replay *)
if !replay_seed && reset_the_seed_to_last label then (get label) else (
let seed = Random.self_init (); Random.int 1073741823 in
let seed_file = seed_file_name label in
let oc = open_out seed_file in
Printf.fprintf oc "%d\n%s\n" seed
(Mypervasives.entete "#" SasaVersion.str SasaVersion.sha);
flush oc;
close_out oc;
set seed;
seed
)
(* Time-stamp: <modified the 20/05/2021 (at 09:25) by Erwan Jahier> *)
val set : int -> unit
(** The string is used to create a file name to save/restore the seed
when the --replay option is used *)
val get : string -> int
val replay_seed : bool ref
(* Time-stamp: <modified the 04/05/2021 (at 08:36) by Erwan Jahier> *)
(* Time-stamp: <modified the 20/05/2021 (at 09:09) by Erwan Jahier> *)
open Register
......@@ -198,7 +198,7 @@ let (make : bool -> string array -> 'v t) =
flush stdout;
exit 2
in
let seed = seed_get args in
let seed = Seed.get args.topo in
try
let dynlink = if args.output_algos then false else dynlink in
let dot_file = args.topo in
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment