Skip to content

Commit f17bcb0

Browse files
committed
Added JSON output
1 parent fb63910 commit f17bcb0

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

analysis/markov/Linleios/Evolve.lean

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11

22
import Std.Data.HashMap
33
import Batteries.Lean.HashMap
4+
import Lean.Data.Json.FromToJson
45

56
import Linleios.Types
67

78

9+
open Lean (Json)
10+
open Lean.ToJson (toJson)
811
open Std (HashMap)
912

1013

@@ -68,6 +71,9 @@ def ebDistribution : Probabilities → HashMap Nat Probability :=
6871
)
6972
7073

74+
def ebDistributionJson : Probabilities → Json :=
75+
Json.mkObj ∘ List.map (fun ⟨k, v⟩ => ⟨toString k, toJson v⟩) ∘ HashMap.toList ∘ ebDistribution
76+
7177
def ebEfficiency (states : Probabilities) : Float :=
7278
let rbCount := states.keys.head!.rbCount
7379
let ebCount :=

analysis/markov/Main.lean

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Parser.Char.Numeric
55
import Parser.Stream
66

77
open Cli
8+
open Lean (Json)
89

910

1011
instance : ParseableType Float where
@@ -28,31 +29,27 @@ def runMarkovCmd (p : Parsed) : IO UInt32 :=
2829
let rbCount : Nat := p.flag! "rb-count" |>.as! Nat
2930
let env := makeEnvironment activeSlotCoefficient pRbHeaderArrives pEbValidates committeeSize.toFloat τ Lheader Lvote Ldiff
3031
let sn := simulate env default ε rbCount
31-
let print {α : Type} [Repr α] (x : α) : IO Unit := IO.println (reprPrec x 0).pretty
32-
IO.println ""
33-
print sn
34-
IO.println ""
35-
print $ ebDistribution sn
36-
IO.println ""
37-
print $ ebEfficiency sn
38-
IO.println ""
39-
print $ 1 - totalProbability sn
32+
if p.hasFlag "output-file"
33+
then IO.FS.writeFile (p.flag! "output-file" |>.as! String) (Json.pretty $ ebDistributionJson sn)
34+
IO.println s!"Efficiency: {(reprPrec (ebEfficiency sn) 0).pretty}"
35+
IO.eprintln s!"Missing probability: {1 - totalProbability sn}"
4036
pure 0
4137

4238
def markovCmd : Cmd := `[Cli|
4339
markovCmd VIA runMarkovCmd; ["0.0.1"]
4440
"Run a Markov simulation of Linear Leios."
4541
FLAGS:
46-
"active-slot-coefficient" : Float ; "The active slot coefficient for RBs, in probability per slot."
47-
"l-header" : Nat ; "L_header protocol parameter, in slots."
48-
"l-vote" : Nat ; "L_vote protocol parameter, in slots."
49-
"l-diff" : Nat ; "L_diff protocol parameter, in slots."
50-
"committee-size" : Nat ; "Expected number of voters in the committee."
51-
"quorum-fraction" : Float ; "τ protocol parameter, in %/100."
52-
"p-rb-header-arrives" : Float ; "Probability that the RB header arrives before L_header."
53-
"p-eb-validates" : Float ; "Probability that the EB is fully validated before 3 L_header + L_vote."
54-
"tolerance" : Float ; "Discard states with less than this probability."
55-
"rb-count" : Nat ; "Number of RBs to simulate."
42+
"active-slot-coefficient" : Float ; "The active slot coefficient for RBs, in probability per slot."
43+
"l-header" : Nat ; "L_header protocol parameter, in slots."
44+
"l-vote" : Nat ; "L_vote protocol parameter, in slots."
45+
"l-diff" : Nat ; "L_diff protocol parameter, in slots."
46+
"committee-size" : Nat ; "Expected number of voters in the committee."
47+
"quorum-fraction" : Float ; "τ protocol parameter, in %/100."
48+
"p-rb-header-arrives" : Float ; "Probability that the RB header arrives before L_header."
49+
"p-eb-validates" : Float ; "Probability that the EB is fully validated before 3 L_header + L_vote."
50+
"tolerance" : Float ; "Discard states with less than this probability."
51+
"rb-count" : Nat ; "Number of RBs to simulate."
52+
"output-file" : String ; "Path to the JSON output file for the EB distribution."
5653
EXTENSIONS:
5754
author "bwbush";
5855
defaultValues! #[
@@ -64,7 +61,7 @@ def markovCmd : Cmd := `[Cli|
6461
, ("quorum-fraction" , "0.75")
6562
, ("p-rb-header-arrives" , "0.95")
6663
, ("p-eb-validates" , "0.90")
67-
, ("tolerance" , "1e-9")
64+
, ("tolerance" , "1e-8")
6865
, ("rb-count" , "100" )
6966
]
7067
]

0 commit comments

Comments
 (0)